]> git.sur5r.net Git - openldap/blob - servers/slapd/shell-backends/passwd-shell.c
Changed FD_SETSIZE checks for consistency. Added checks where needed.
[openldap] / servers / slapd / shell-backends / passwd-shell.c
1 /*
2  passwd-shell.c - /etc/passwd shell-based backend for standalone ldap server
3
4  Copyright (c) 1995 Regents of the University of Michigan.
5  All rights reserved.
6
7  Redistribution and use in source and binary forms are permitted
8  provided that this notice is preserved and that due credit is given
9  to the University of Michigan at Ann Arbor. The name of the University
10  may not be used to endorse or promote products derived from this
11  software without specific prior written permission. This software
12  is provided ``as is'' without express or implied warranty.
13 */
14
15
16 #include <sys/types.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <pwd.h>
21 #include <varargs.h>
22 #include <lber.h>
23 #include <ldap.h>
24 #include "shellutil.h"
25 #include "passwd-shell.h"
26
27
28 #ifdef LDAP_DEBUG
29 void debug_printf();
30 #else /* LDAP_DEBUG */
31 #define debug_printf()
32 #endif /* LDAP_DEBUG */
33
34
35 static void pwdfile_search( struct ldop *op, FILE *ofp );
36 static struct ldentry *pw2entry( struct ldop *op, struct passwd *pw );
37
38 static char     tmpbuf[ MAXLINELEN * 2 ];
39
40
41 main( int argc, char **argv )
42 {
43     int                 c, errflg;
44     struct ldop         op;
45     extern int          optind;
46     extern char         *optarg;
47
48     if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
49         progname = estrdup( argv[ 0 ] );
50     } else {
51         progname = estrdup( progname + 1 );
52     }
53
54     errflg = debugflg = 0;
55
56     while (( c = getopt( argc, argv, "d" )) != EOF ) {
57         switch( c ) {
58         case 'd':
59 #ifdef LDAP_DEBUG
60             ++debugflg;
61 #else /* LDAP_DEBUG */
62             fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
63                     progname );
64 #endif /* LDAP_DEBUG */
65             break;
66         default:
67             ++errflg;
68         }
69     }
70
71     if ( errflg || optind < argc ) {
72         fprintf( stderr, "usage: %s [-d]\n", progname );
73         exit( 1 );
74     }
75
76     debug_printf( "started\n" );
77
78     (void) memset( (char *)&op, '\0', sizeof( op ));
79
80     if ( parse_input( stdin, stdout, &op ) < 0 ) {
81         exit( 0 );
82     }
83
84     if ( op.ldop_op != LDOP_SEARCH ) {
85         write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
86                 "Command Not Implemented" );
87         exit( 0 );
88     }
89
90 #ifdef LDAP_DEBUG
91     dump_ldop( &op );
92 #endif /* LDAP_DEBUG */
93
94     pwdfile_search( &op, stdout );
95
96     exit( 0 );
97 }
98
99
100 static void
101 pwdfile_search( struct ldop *op, FILE *ofp )
102 {
103     struct passwd       *pw;
104     struct ldentry      *entry;
105     int                 oneentry;
106
107     oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
108
109     for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
110         if (( entry = pw2entry( op, pw )) != NULL ) {
111             if ( oneentry ) {
112                 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
113                     write_entry( op, entry, ofp );
114                     break;
115                 }
116             } else if ( test_filter( op, entry )) {
117                 write_entry( op, entry, ofp );
118             }
119             free_entry( entry );
120         }
121     }
122     endpwent();
123
124     write_result( ofp, LDAP_SUCCESS, NULL, NULL );
125 }
126
127
128 static struct ldentry *
129 pw2entry( struct ldop *op, struct passwd *pw )
130 {
131     struct ldentry      *entry;
132     struct ldattr       *attr;
133     int                 i;
134
135     entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
136
137     /* 
138      * construct the DN from pw_name
139      */
140     if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
141         /*
142          * X.500 style DN
143          */
144         sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
145     } else {
146         /*
147          * RFC-822 style DN
148          */
149         sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
150     }
151     entry->lde_dn = estrdup( tmpbuf );
152
153     /*
154      * for now, we simply derive the LDAP attribute values as follows:
155      *  objectClass = person
156      *  uid = pw_name
157      *  sn = pw_name
158      *  cn = pw_name
159      *  cn = pw_gecos   (second common name)
160      */
161     entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
162     i = 0;
163     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
164     attr->lda_name = estrdup( "objectClass" );
165     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
166     attr->lda_values[ 0 ] = estrdup( "person" );
167     entry->lde_attrs[ i++ ] = attr;
168
169     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
170     attr->lda_name = estrdup( "uid" );
171     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
172     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
173     entry->lde_attrs[ i++ ] = attr;
174
175     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
176     attr->lda_name = estrdup( "sn" );
177     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
178     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
179     entry->lde_attrs[ i++ ] = attr;
180
181     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
182     attr->lda_name = estrdup( "cn" );
183     attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
184     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
185     if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
186         attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
187     }
188     entry->lde_attrs[ i++ ] = attr;
189
190     return( entry );
191 }