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.
20 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
30 #include "shellutil.h"
31 #include "passwd-shell.h"
34 static void pwdfile_search LDAP_P(( struct ldop *op, FILE *ofp ));
35 static struct ldentry *pw2entry LDAP_P(( struct ldop *op, struct passwd *pw ));
37 static char tmpbuf[ MAXLINELEN * 2 ];
41 main( int argc, char **argv )
46 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
47 progname = estrdup( argv[ 0 ] );
49 progname = estrdup( progname + 1 );
52 errflg = debugflg = 0;
54 while (( c = getopt( argc, argv, "d" )) != EOF ) {
59 #else /* LDAP_DEBUG */
60 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
62 #endif /* LDAP_DEBUG */
69 if ( errflg || optind < argc ) {
70 fprintf( stderr, "usage: %s [-d]\n", progname );
74 debug_printf( "started\n" );
76 (void) memset( (char *)&op, '\0', sizeof( op ));
78 if ( parse_input( stdin, stdout, &op ) < 0 ) {
82 if ( op.ldop_op != LDOP_SEARCH ) {
83 write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
84 "Command Not Implemented" );
90 #endif /* LDAP_DEBUG */
92 pwdfile_search( &op, stdout );
99 pwdfile_search( struct ldop *op, FILE *ofp )
102 struct ldentry *entry;
105 oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
107 for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
108 if (( entry = pw2entry( op, pw )) != NULL ) {
110 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
111 write_entry( op, entry, ofp );
114 } else if ( test_filter( op, entry )) {
115 write_entry( op, entry, ofp );
122 write_result( ofp, LDAP_SUCCESS, NULL, NULL );
126 static struct ldentry *
127 pw2entry( struct ldop *op, struct passwd *pw )
129 struct ldentry *entry;
133 entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
136 * construct the DN from pw_name
138 if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
142 sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
147 sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
149 entry->lde_dn = estrdup( tmpbuf );
152 * for now, we simply derive the LDAP attribute values as follows:
153 * objectClass = person
157 * cn = pw_gecos (second common name)
159 entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
161 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
162 attr->lda_name = estrdup( "objectClass" );
163 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
164 attr->lda_values[ 0 ] = estrdup( "person" );
165 entry->lde_attrs[ i++ ] = attr;
167 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
168 attr->lda_name = estrdup( "uid" );
169 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
170 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
171 entry->lde_attrs[ i++ ] = attr;
173 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
174 attr->lda_name = estrdup( "sn" );
175 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
176 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
177 entry->lde_attrs[ i++ ] = attr;
179 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
180 attr->lda_name = estrdup( "cn" );
181 attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
182 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
183 if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
184 attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
186 entry->lde_attrs[ i++ ] = attr;