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.
22 #include <ac/string.h>
27 #include "shellutil.h"
28 #include "passwd-shell.h"
31 static void pwdfile_search LDAP_P(( struct ldop *op, FILE *ofp ));
32 static struct ldentry *pw2entry LDAP_P(( struct ldop *op, struct passwd *pw ));
34 static char tmpbuf[ MAXLINELEN * 2 ];
37 main( int argc, char **argv )
44 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
45 progname = estrdup( argv[ 0 ] );
47 progname = estrdup( progname + 1 );
50 errflg = debugflg = 0;
52 while (( c = getopt( argc, argv, "d" )) != EOF ) {
57 #else /* LDAP_DEBUG */
58 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
60 #endif /* LDAP_DEBUG */
67 if ( errflg || optind < argc ) {
68 fprintf( stderr, "usage: %s [-d]\n", progname );
72 debug_printf( "started\n" );
74 (void) memset( (char *)&op, '\0', sizeof( op ));
76 if ( parse_input( stdin, stdout, &op ) < 0 ) {
80 if ( op.ldop_op != LDOP_SEARCH ) {
81 write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
82 "Command Not Implemented" );
88 #endif /* LDAP_DEBUG */
90 pwdfile_search( &op, stdout );
97 pwdfile_search( struct ldop *op, FILE *ofp )
100 struct ldentry *entry;
103 oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
105 for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
106 if (( entry = pw2entry( op, pw )) != NULL ) {
108 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
109 write_entry( op, entry, ofp );
112 } else if ( test_filter( op, entry )) {
113 write_entry( op, entry, ofp );
120 write_result( ofp, LDAP_SUCCESS, NULL, NULL );
124 static struct ldentry *
125 pw2entry( struct ldop *op, struct passwd *pw )
127 struct ldentry *entry;
131 entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
134 * construct the DN from pw_name
136 if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
140 sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
145 sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
147 entry->lde_dn = estrdup( tmpbuf );
150 * for now, we simply derive the LDAP attribute values as follows:
151 * objectClass = person
155 * cn = pw_gecos (second common name)
157 entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
159 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
160 attr->lda_name = estrdup( "objectClass" );
161 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
162 attr->lda_values[ 0 ] = estrdup( "person" );
163 entry->lde_attrs[ i++ ] = attr;
165 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
166 attr->lda_name = estrdup( "uid" );
167 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
168 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
169 entry->lde_attrs[ i++ ] = attr;
171 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
172 attr->lda_name = estrdup( "sn" );
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( "cn" );
179 attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
180 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
181 if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
182 attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
184 entry->lde_attrs[ i++ ] = attr;