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