2 passwd-shell.c - /etc/passwd shell-based backend for standalone ldap server
4 Copyright (c) 1995 Regents of the University of Michigan.
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.
18 #include <sys/types.h>
26 #include "shellutil.h"
27 #include "passwd-shell.h"
32 #else /* LDAP_DEBUG */
33 #define debug_printf()
34 #endif /* LDAP_DEBUG */
37 static void pwdfile_search( struct ldop *op, FILE *ofp );
38 static struct ldentry *pw2entry( struct ldop *op, struct passwd *pw );
40 static char tmpbuf[ MAXLINELEN * 2 ];
43 main( int argc, char **argv )
50 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
51 progname = estrdup( argv[ 0 ] );
53 progname = estrdup( progname + 1 );
56 errflg = debugflg = 0;
58 while (( c = getopt( argc, argv, "d" )) != EOF ) {
63 #else /* LDAP_DEBUG */
64 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
66 #endif /* LDAP_DEBUG */
73 if ( errflg || optind < argc ) {
74 fprintf( stderr, "usage: %s [-d]\n", progname );
78 debug_printf( "started\n" );
80 (void) memset( (char *)&op, '\0', sizeof( op ));
82 if ( parse_input( stdin, stdout, &op ) < 0 ) {
86 if ( op.ldop_op != LDOP_SEARCH ) {
87 write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
88 "Command Not Implemented" );
94 #endif /* LDAP_DEBUG */
96 pwdfile_search( &op, stdout );
103 pwdfile_search( struct ldop *op, FILE *ofp )
106 struct ldentry *entry;
109 oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
111 for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
112 if (( entry = pw2entry( op, pw )) != NULL ) {
114 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
115 write_entry( op, entry, ofp );
118 } else if ( test_filter( op, entry )) {
119 write_entry( op, entry, ofp );
126 write_result( ofp, LDAP_SUCCESS, NULL, NULL );
130 static struct ldentry *
131 pw2entry( struct ldop *op, struct passwd *pw )
133 struct ldentry *entry;
137 entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
140 * construct the DN from pw_name
142 if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
146 sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
151 sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
153 entry->lde_dn = estrdup( tmpbuf );
156 * for now, we simply derive the LDAP attribute values as follows:
157 * objectClass = person
161 * cn = pw_gecos (second common name)
163 entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
165 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
166 attr->lda_name = estrdup( "objectClass" );
167 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
168 attr->lda_values[ 0 ] = estrdup( "person" );
169 entry->lde_attrs[ i++ ] = attr;
171 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
172 attr->lda_name = estrdup( "uid" );
173 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
174 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
175 entry->lde_attrs[ i++ ] = attr;
177 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
178 attr->lda_name = estrdup( "sn" );
179 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
180 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
181 entry->lde_attrs[ i++ ] = attr;
183 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
184 attr->lda_name = estrdup( "cn" );
185 attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
186 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
187 if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
188 attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
190 entry->lde_attrs[ i++ ] = attr;