]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
Sync with HEAD
[openldap] / servers / slapd / back-passwd / search.c
1 /* search.c - /etc/passwd backend search function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26 /* ACKNOWLEDGEMENTS:
27  * This work was originally developed by the University of Michigan
28  * (as part of U-MICH LDAP).  Additional significant contributors
29  * include:
30  *     Hallvard B. Furuseth
31  *     Howard Chu
32  *     Kurt D. Zeilenga
33  */
34
35 #include "portable.h"
36
37 #include <stdio.h>
38
39 #include <ac/ctype.h>
40 #include <ac/socket.h>
41 #include <ac/string.h>
42 #include <ac/time.h>
43
44 #include <pwd.h>
45
46 #include "slap.h"
47 #include "back-passwd.h"
48 #include <ldap_pvt.h>
49
50 static void pw_start( Backend *be );
51
52 static Entry *pw2entry(
53         Backend *be,
54         struct passwd *pw,
55         const char **text);
56
57 int
58 passwd_back_search(
59     Operation   *op,
60     SlapReply   *rs )
61 {
62         struct passwd   *pw;
63         Entry           *e;
64         char            *s;
65         time_t          stoptime;
66
67         LDAPRDN rdn = NULL;
68         struct berval parent = { 0, NULL };
69
70         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
71
72         op->ors_tlimit = (op->ors_tlimit > op->o_bd->be_timelimit || op->ors_tlimit < 1) ? op->o_bd->be_timelimit
73             : op->ors_tlimit;
74         stoptime = op->o_time + op->ors_tlimit;
75         op->ors_slimit = (op->ors_slimit > op->o_bd->be_sizelimit || op->ors_slimit < 1) ? op->o_bd->be_sizelimit
76             : op->ors_slimit;
77
78         /* Handle a query for the base of this backend */
79         if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
80                 struct berval   vals[2];
81
82                 vals[1].bv_val = NULL;
83
84                 rs->sr_matched = op->o_req_dn.bv_val;
85
86                 if( op->ors_scope != LDAP_SCOPE_ONELEVEL ) {
87                         AttributeDescription *desc = NULL;
88
89                         /* Create an entry corresponding to the base DN */
90                         e = (Entry *) ch_calloc(1, sizeof(Entry));
91                         e->e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
92                         e->e_name.bv_len = op->o_req_dn.bv_len;
93                         e->e_nname.bv_val =  ch_strdup( op->o_req_ndn.bv_val );
94                         e->e_nname.bv_len = op->o_req_ndn.bv_len;
95                         e->e_attrs = NULL;
96                         e->e_private = NULL;
97
98                         /* Use the first attribute of the DN
99                         * as an attribute within the entry itself.
100                         */
101                         if( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text, 
102                                 LDAP_DN_FORMAT_LDAP ) )
103                         {
104                                 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
105                                 goto done;
106                         }
107
108                         if( slap_bv2ad( &rdn[0]->la_attr, &desc, &rs->sr_text )) {
109                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
110                                 ldap_rdnfree(rdn);
111                                 goto done;
112                         }
113
114                         vals[0] = rdn[0]->la_value;
115                         attr_mergeit( e, desc, vals );
116
117                         ldap_rdnfree(rdn);
118                         rdn = NULL;
119
120                         /* Every entry needs an objectclass. We don't really
121                          * know if our hardcoded choice here agrees with the
122                          * DN that was configured for this backend, but it's
123                          * better than nothing.
124                          *
125                          * should be a configuratable item
126                          */
127                         vals[0].bv_val = "organizationalUnit";
128                         vals[0].bv_len = sizeof("organizationalUnit")-1;
129                         attr_mergeit( e, ad_objectClass, vals );
130         
131                         if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
132                                 rs->sr_entry = e;
133                                 rs->sr_attrs = op->ors_attrs;
134                                 rs->sr_flags = REP_ENTRY_MODIFIABLE;
135                                 send_search_entry( op, rs );
136                         }
137                 }
138
139                 if ( op->ors_scope != LDAP_SCOPE_BASE ) {
140                         /* check all our "children" */
141
142                         ldap_pvt_thread_mutex_lock( &passwd_mutex );
143                         pw_start( op->o_bd );
144                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
145                                 /* check for abandon */
146                                 if ( op->o_abandon ) {
147                                         endpwent();
148                                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
149                                         return( -1 );
150                                 }
151
152                                 /* check time limit */
153                                 if ( slap_get_time() > stoptime ) {
154                                         send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
155                                         endpwent();
156                                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
157                                         return( 0 );
158                                 }
159
160                                 if ( !(e = pw2entry( op->o_bd, pw, &rs->sr_text )) ) {
161                                         rs->sr_err = LDAP_OTHER;
162                                         endpwent();
163                                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
164                                         goto done;
165                                 }
166
167                                 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
168                                         /* check size limit */
169                                         if ( --op->ors_slimit == -1 ) {
170                                                 send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
171                                                 endpwent();
172                                                 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
173                                                 return( 0 );
174                                         }
175
176                                         rs->sr_entry = e;
177                                         rs->sr_attrs = op->ors_attrs;
178                                         rs->sr_flags = REP_ENTRY_MODIFIABLE;
179                                         send_search_entry( op, rs );
180                                 }
181
182                                 entry_free( e );
183                         }
184                         endpwent();
185                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
186                 }
187
188         } else {
189                 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
190                         dnParent( &op->o_req_ndn, &parent );
191                 }
192
193                 /* This backend is only one layer deep. Don't answer requests for
194                  * anything deeper than that.
195                  */
196                 if( !be_issuffix( op->o_bd, &parent ) ) {
197                         int i;
198                         for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
199                                 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
200                                         rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
201                                         break;
202                                 }
203                         }
204                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
205                         goto done;
206                 }
207
208                 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
209                         goto done;
210                 }
211
212                 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
213                         LDAP_DN_FORMAT_LDAP ))
214                 { 
215                         rs->sr_err = LDAP_OTHER;
216                         goto done;
217                 }
218
219                 ldap_pvt_thread_mutex_lock( &passwd_mutex );
220                 pw_start( op->o_bd );
221                 if ( (pw = getpwnam( rdn[0]->la_value.bv_val )) == NULL ) {
222                         rs->sr_matched = parent.bv_val;
223                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
224                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
225                         goto done;
226                 }
227
228                 e = pw2entry( op->o_bd, pw, &rs->sr_text );
229                 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
230                 if ( !e ) {
231                         rs->sr_err = LDAP_OTHER;
232                         goto done;
233                 }
234
235                 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
236                         rs->sr_entry = e;
237                         rs->sr_attrs = op->ors_attrs;
238                         rs->sr_flags = REP_ENTRY_MODIFIABLE;
239                         send_search_entry( op, rs );
240                 }
241
242                 entry_free( e );
243         }
244
245 done:
246         if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
247         send_ldap_result( op, rs );
248
249         if( rdn != NULL ) ldap_rdnfree( rdn );
250
251         return( 0 );
252 }
253
254 static void
255 pw_start(
256         Backend *be
257 )
258 {
259         endpwent();
260
261 #ifdef HAVE_SETPWFILE
262         if ( be->be_private != NULL ) {
263                 (void) setpwfile( (char *) be->be_private );
264         }
265 #endif /* HAVE_SETPWFILE */
266 }
267
268 static Entry *
269 pw2entry( Backend *be, struct passwd *pw, const char **text )
270 {
271         size_t pwlen;
272         Entry           *e;
273         struct berval   vals[2];
274         struct berval   bv;
275
276         int rc;
277
278         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
279         AttributeDescription *ad_cn = NULL;
280         AttributeDescription *ad_sn = NULL;
281         AttributeDescription *ad_uid = NULL;
282         AttributeDescription *ad_description = NULL;
283
284         rc = slap_str2ad( "cn", &ad_cn, text );
285         if(rc != LDAP_SUCCESS) return NULL;
286         rc = slap_str2ad( "sn", &ad_sn, text );
287         if(rc != LDAP_SUCCESS) return NULL;
288         rc = slap_str2ad( "uid", &ad_uid, text );
289         if(rc != LDAP_SUCCESS) return NULL;
290         rc = slap_str2ad( "description", &ad_description, text );
291         if(rc != LDAP_SUCCESS) return NULL;
292
293         /*
294          * from pw we get pw_name and make it cn
295          * give it an objectclass of person.
296          */
297
298         pwlen = strlen( pw->pw_name );
299         vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
300         vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
301
302         /* rdn attribute type should be a configuratable item */
303         sprintf( vals[0].bv_val, "uid=%s,%s",
304                 pw->pw_name, be->be_suffix[0].bv_val );
305
306         rc = dnNormalize( 0, NULL, NULL, vals, &bv, NULL );
307         if( rc != LDAP_SUCCESS ) {
308                 free( vals[0].bv_val );
309                 return NULL;
310         }
311
312         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
313         e->e_name = vals[0];
314         e->e_nname = bv;
315
316         e->e_attrs = NULL;
317
318         vals[1].bv_val = NULL;
319
320         /* objectclasses should be configurable items */
321         vals[0].bv_val = "top";
322         vals[0].bv_len = sizeof("top")-1;
323         attr_mergeit( e, ad_objectClass, vals );
324
325         vals[0].bv_val = "person";
326         vals[0].bv_len = sizeof("person")-1;
327         attr_mergeit( e, ad_objectClass, vals );
328
329         vals[0].bv_val = "uidObject";
330         vals[0].bv_len = sizeof("uidObject")-1;
331         attr_mergeit( e, ad_objectClass, vals );
332
333         vals[0].bv_val = pw->pw_name;
334         vals[0].bv_len = pwlen;
335         attr_mergeit( e, ad_uid, vals );        /* required by uidObject */
336         attr_mergeit( e, ad_cn, vals ); /* required by person */
337         attr_mergeit( e, ad_sn, vals ); /* required by person */
338
339 #ifdef HAVE_PW_GECOS
340         /*
341          * if gecos is present, add it as a cn. first process it
342          * according to standard BSD usage. If the processed cn has
343          * a space, use the tail as the surname.
344          */
345         if (pw->pw_gecos[0]) {
346                 char *s;
347
348                 vals[0].bv_val = pw->pw_gecos;
349                 vals[0].bv_len = strlen(vals[0].bv_val);
350                 attr_mergeit(e, ad_description, vals);
351
352                 s = strchr(vals[0].bv_val, ',');
353                 if (s) *s = '\0';
354
355                 s = strchr(vals[0].bv_val, '&');
356                 if (s) {
357                         char buf[1024];
358
359                         if( vals[0].bv_len + pwlen < sizeof(buf) ) {
360                                 int i = s - vals[0].bv_val;
361                                 strncpy(buf, vals[0].bv_val, i);
362                                 s = buf+i;
363                                 strcpy(s, pw->pw_name);
364                                 *s = TOUPPER((unsigned char)*s);
365                                 strcat(s, vals[0].bv_val+i+1);
366                                 vals[0].bv_val = buf;
367                         }
368                 }
369                 vals[0].bv_len = strlen(vals[0].bv_val);
370
371                 if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
372                         attr_mergeit( e, ad_cn, vals );
373                 }
374
375                 if ( (s=strrchr(vals[0].bv_val, ' '))) {
376                         vals[0].bv_val = s + 1;
377                         vals[0].bv_len = strlen(vals[0].bv_val);
378                         attr_mergeit(e, ad_sn, vals);
379                 }
380         }
381 #endif
382
383         return( e );
384 }