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.
16 #include <sys/types.h>
24 #include "shellutil.h"
25 #include "passwd-shell.h"
30 #else /* LDAP_DEBUG */
31 #define debug_printf()
32 #endif /* LDAP_DEBUG */
35 static void pwdfile_search( struct ldop *op, FILE *ofp );
36 static struct ldentry *pw2entry( struct ldop *op, struct passwd *pw );
38 static char tmpbuf[ MAXLINELEN * 2 ];
41 main( int argc, char **argv )
48 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
49 progname = estrdup( argv[ 0 ] );
51 progname = estrdup( progname + 1 );
54 errflg = debugflg = 0;
56 while (( c = getopt( argc, argv, "d" )) != EOF ) {
61 #else /* LDAP_DEBUG */
62 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
64 #endif /* LDAP_DEBUG */
71 if ( errflg || optind < argc ) {
72 fprintf( stderr, "usage: %s [-d]\n", progname );
76 debug_printf( "started\n" );
78 (void) memset( (char *)&op, '\0', sizeof( op ));
80 if ( parse_input( stdin, stdout, &op ) < 0 ) {
84 if ( op.ldop_op != LDOP_SEARCH ) {
85 write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
86 "Command Not Implemented" );
92 #endif /* LDAP_DEBUG */
94 pwdfile_search( &op, stdout );
101 pwdfile_search( struct ldop *op, FILE *ofp )
104 struct ldentry *entry;
107 oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
109 for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
110 if (( entry = pw2entry( op, pw )) != NULL ) {
112 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
113 write_entry( op, entry, ofp );
116 } else if ( test_filter( op, entry )) {
117 write_entry( op, entry, ofp );
124 write_result( ofp, LDAP_SUCCESS, NULL, NULL );
128 static struct ldentry *
129 pw2entry( struct ldop *op, struct passwd *pw )
131 struct ldentry *entry;
135 entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
138 * construct the DN from pw_name
140 if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
144 sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
149 sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
151 entry->lde_dn = estrdup( tmpbuf );
154 * for now, we simply derive the LDAP attribute values as follows:
155 * objectClass = person
159 * cn = pw_gecos (second common name)
161 entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
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;
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;
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;
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 );
188 entry->lde_attrs[ i++ ] = attr;