]> git.sur5r.net Git - openldap/blob - servers/slapd/shell-backends/passwd-shell.c
fd354e0fe4b6530df645ffc1c98ba6a1422c91a7
[openldap] / servers / slapd / shell-backends / passwd-shell.c
1 /*
2  passwd-shell.c - /etc/passwd shell-based backend for standalone ldap server
3
4  Copyright (c) 1995 Regents of the University of Michigan.
5  All rights reserved.
6
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.
13 */
14
15
16 #include "portable.h"
17
18 #include <sys/types.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <pwd.h>
23 #include <varargs.h>
24 #include <lber.h>
25 #include <ldap.h>
26 #include "shellutil.h"
27 #include "passwd-shell.h"
28
29
30 #ifdef LDAP_DEBUG
31 void debug_printf();
32 #else /* LDAP_DEBUG */
33 #define debug_printf()
34 #endif /* LDAP_DEBUG */
35
36
37 static void pwdfile_search( struct ldop *op, FILE *ofp );
38 static struct ldentry *pw2entry( struct ldop *op, struct passwd *pw );
39
40 static char     tmpbuf[ MAXLINELEN * 2 ];
41
42
43 main( int argc, char **argv )
44 {
45     int                 c, errflg;
46     struct ldop         op;
47     extern int          optind;
48     extern char         *optarg;
49
50     if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
51         progname = estrdup( argv[ 0 ] );
52     } else {
53         progname = estrdup( progname + 1 );
54     }
55
56     errflg = debugflg = 0;
57
58     while (( c = getopt( argc, argv, "d" )) != EOF ) {
59         switch( c ) {
60         case 'd':
61 #ifdef LDAP_DEBUG
62             ++debugflg;
63 #else /* LDAP_DEBUG */
64             fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
65                     progname );
66 #endif /* LDAP_DEBUG */
67             break;
68         default:
69             ++errflg;
70         }
71     }
72
73     if ( errflg || optind < argc ) {
74         fprintf( stderr, "usage: %s [-d]\n", progname );
75         exit( 1 );
76     }
77
78     debug_printf( "started\n" );
79
80     (void) memset( (char *)&op, '\0', sizeof( op ));
81
82     if ( parse_input( stdin, stdout, &op ) < 0 ) {
83         exit( 0 );
84     }
85
86     if ( op.ldop_op != LDOP_SEARCH ) {
87         write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
88                 "Command Not Implemented" );
89         exit( 0 );
90     }
91
92 #ifdef LDAP_DEBUG
93     dump_ldop( &op );
94 #endif /* LDAP_DEBUG */
95
96     pwdfile_search( &op, stdout );
97
98     exit( 0 );
99 }
100
101
102 static void
103 pwdfile_search( struct ldop *op, FILE *ofp )
104 {
105     struct passwd       *pw;
106     struct ldentry      *entry;
107     int                 oneentry;
108
109     oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
110
111     for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
112         if (( entry = pw2entry( op, pw )) != NULL ) {
113             if ( oneentry ) {
114                 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
115                     write_entry( op, entry, ofp );
116                     break;
117                 }
118             } else if ( test_filter( op, entry )) {
119                 write_entry( op, entry, ofp );
120             }
121             free_entry( entry );
122         }
123     }
124     endpwent();
125
126     write_result( ofp, LDAP_SUCCESS, NULL, NULL );
127 }
128
129
130 static struct ldentry *
131 pw2entry( struct ldop *op, struct passwd *pw )
132 {
133     struct ldentry      *entry;
134     struct ldattr       *attr;
135     int                 i;
136
137     entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
138
139     /* 
140      * construct the DN from pw_name
141      */
142     if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
143         /*
144          * X.500 style DN
145          */
146         sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
147     } else {
148         /*
149          * RFC-822 style DN
150          */
151         sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
152     }
153     entry->lde_dn = estrdup( tmpbuf );
154
155     /*
156      * for now, we simply derive the LDAP attribute values as follows:
157      *  objectClass = person
158      *  uid = pw_name
159      *  sn = pw_name
160      *  cn = pw_name
161      *  cn = pw_gecos   (second common name)
162      */
163     entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
164     i = 0;
165     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
166     attr->lda_name = estrdup( "objectClass" );
167     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
168     attr->lda_values[ 0 ] = estrdup( "person" );
169     entry->lde_attrs[ i++ ] = attr;
170
171     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
172     attr->lda_name = estrdup( "uid" );
173     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
174     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
175     entry->lde_attrs[ i++ ] = attr;
176
177     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
178     attr->lda_name = estrdup( "sn" );
179     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
180     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
181     entry->lde_attrs[ i++ ] = attr;
182
183     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
184     attr->lda_name = estrdup( "cn" );
185     attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
186     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
187     if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
188         attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
189     }
190     entry->lde_attrs[ i++ ] = attr;
191
192     return( entry );
193 }