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.
21 #include <ac/string.h>
22 #include <ac/unistd.h>
29 #include "shellutil.h"
30 #include "passwd-shell.h"
33 static void pwdfile_search LDAP_P(( struct ldop *op, FILE *ofp ));
34 static struct ldentry *pw2entry LDAP_P(( struct ldop *op, struct passwd *pw ));
36 static char tmpbuf[ MAXLINELEN * 2 ];
40 main( int argc, char **argv )
45 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
46 progname = estrdup( argv[ 0 ] );
48 progname = estrdup( progname + 1 );
51 errflg = debugflg = 0;
53 while (( c = getopt( argc, argv, "d" )) != EOF ) {
58 #else /* LDAP_DEBUG */
59 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
61 #endif /* LDAP_DEBUG */
68 if ( errflg || optind < argc ) {
69 fprintf( stderr, "usage: %s [-d]\n", progname );
73 debug_printf( "started\n" );
75 (void) memset( (char *)&op, '\0', sizeof( op ));
77 if ( parse_input( stdin, stdout, &op ) < 0 ) {
81 if ( op.ldop_op != LDOP_SEARCH ) {
82 write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
83 "Command Not Implemented" );
89 #endif /* LDAP_DEBUG */
91 pwdfile_search( &op, stdout );
98 pwdfile_search( struct ldop *op, FILE *ofp )
101 struct ldentry *entry;
104 oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
106 for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
107 if (( entry = pw2entry( op, pw )) != NULL ) {
109 if ( dn_casecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
110 write_entry( op, entry, ofp );
113 } else if ( test_filter( op, entry )) {
114 write_entry( op, entry, ofp );
121 write_result( ofp, LDAP_SUCCESS, NULL, NULL );
125 static struct ldentry *
126 pw2entry( struct ldop *op, struct passwd *pw )
128 struct ldentry *entry;
132 entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
135 * construct the DN from pw_name
137 if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
141 sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
146 sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
148 entry->lde_dn = estrdup( tmpbuf );
151 * for now, we simply derive the LDAP attribute values as follows:
152 * objectClass = person
156 * cn = pw_gecos (second common name)
158 entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
160 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
161 attr->lda_name = estrdup( "objectClass" );
162 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
163 attr->lda_values[ 0 ] = estrdup( "person" );
164 entry->lde_attrs[ i++ ] = attr;
166 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
167 attr->lda_name = estrdup( "uid" );
168 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
169 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
170 entry->lde_attrs[ i++ ] = attr;
172 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
173 attr->lda_name = estrdup( "sn" );
174 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
175 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
176 entry->lde_attrs[ i++ ] = attr;
178 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
179 attr->lda_name = estrdup( "cn" );
180 attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
181 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
182 if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
183 attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
185 entry->lde_attrs[ i++ ] = attr;