3 passwd-shell.c - /etc/passwd shell-based backend for standalone ldap server
5 Copyright (c) 1995 Regents of the University of Michigan.
8 Redistribution and use in source and binary forms are permitted
9 provided that this notice is preserved and that due credit is given
10 to the University of Michigan at Ann Arbor. The name of the University
11 may not be used to endorse or promote products derived from this
12 software without specific prior written permission. This software
13 is provided ``as is'' without express or implied warranty.
21 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
31 #include "shellutil.h"
32 #include "passwd-shell.h"
35 static void pwdfile_search LDAP_P(( struct ldop *op, FILE *ofp ));
36 static struct ldentry *pw2entry LDAP_P(( struct ldop *op, struct passwd *pw ));
38 static char tmpbuf[ MAXLINELEN * 2 ];
42 main( int argc, char **argv )
47 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
48 progname = estrdup( argv[ 0 ] );
50 progname = estrdup( progname + 1 );
53 errflg = debugflg = 0;
55 while (( c = getopt( argc, argv, "d" )) != EOF ) {
60 #else /* LDAP_DEBUG */
61 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
63 #endif /* LDAP_DEBUG */
70 if ( errflg || optind < argc ) {
71 fprintf( stderr, "usage: %s [-d]\n", progname );
75 debug_printf( "started\n" );
77 (void) memset( (char *)&op, '\0', sizeof( op ));
79 if ( parse_input( stdin, stdout, &op ) < 0 ) {
83 if ( op.ldop_op != LDOP_SEARCH ) {
84 write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
85 "Command Not Implemented" );
91 #endif /* LDAP_DEBUG */
93 pwdfile_search( &op, stdout );
100 pwdfile_search( struct ldop *op, FILE *ofp )
103 struct ldentry *entry;
106 oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
108 for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
109 if (( entry = pw2entry( op, pw )) != NULL ) {
111 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
112 write_entry( op, entry, ofp );
115 } else if ( test_filter( op, entry ) == LDAP_COMPARE_TRUE ) {
116 write_entry( op, entry, ofp );
123 write_result( ofp, LDAP_SUCCESS, NULL, NULL );
127 static struct ldentry *
128 pw2entry( struct ldop *op, struct passwd *pw )
130 struct ldentry *entry;
134 entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
137 * construct the DN from pw_name
139 if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
143 sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
148 sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
150 entry->lde_dn = estrdup( tmpbuf );
153 * for now, we simply derive the LDAP attribute values as follows:
154 * objectClass = person
158 * cn = pw_gecos (second common name)
160 entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
162 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
163 attr->lda_name = estrdup( "objectClass" );
164 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
165 attr->lda_values[ 0 ] = estrdup( "person" );
166 entry->lde_attrs[ i++ ] = attr;
168 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
169 attr->lda_name = estrdup( "uid" );
170 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
171 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
172 entry->lde_attrs[ i++ ] = attr;
174 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
175 attr->lda_name = estrdup( "sn" );
176 attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
177 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
178 entry->lde_attrs[ i++ ] = attr;
180 attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
181 attr->lda_name = estrdup( "cn" );
182 attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
183 attr->lda_values[ 0 ] = estrdup( pw->pw_name );
184 if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
185 attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
187 entry->lde_attrs[ i++ ] = attr;