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