]> 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-2005 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
49 static void pw_start( Backend *be );
50
51 static Entry *pw2entry(
52         Backend *be,
53         struct passwd *pw,
54         const char **text);
55
56 int
57 passwd_back_search(
58     Operation   *op,
59     SlapReply   *rs )
60 {
61         struct passwd   *pw;
62         Entry           *e;
63         char            *s;
64         time_t          stoptime;
65
66         LDAPRDN rdn = NULL;
67         struct berval parent = BER_BVNULL;
68
69         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
70
71         if (op->ors_tlimit != SLAP_NO_LIMIT ) {
72                 stoptime = op->o_time + op->ors_tlimit;
73         }
74
75         /* Handle a query for the base of this backend */
76         if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
77                 struct berval   vals[2];
78
79                 vals[1].bv_val = NULL;
80
81                 rs->sr_matched = op->o_req_dn.bv_val;
82
83                 if( op->ors_scope != LDAP_SCOPE_ONELEVEL ) {
84                         AttributeDescription *desc = NULL;
85
86                         /* Create an entry corresponding to the base DN */
87                         e = (Entry *) ch_calloc(1, sizeof(Entry));
88                         e->e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
89                         e->e_name.bv_len = op->o_req_dn.bv_len;
90                         e->e_nname.bv_val =  ch_strdup( op->o_req_ndn.bv_val );
91                         e->e_nname.bv_len = op->o_req_ndn.bv_len;
92                         e->e_attrs = NULL;
93                         e->e_private = NULL;
94
95                         /* Use the first attribute of the DN
96                         * as an attribute within the entry itself.
97                         */
98                         if( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text, 
99                                 LDAP_DN_FORMAT_LDAP ) )
100                         {
101                                 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
102                                 goto done;
103                         }
104
105                         if( slap_bv2ad( &rdn[0]->la_attr, &desc, &rs->sr_text )) {
106                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
107                                 ldap_rdnfree(rdn);
108                                 goto done;
109                         }
110
111                         vals[0] = rdn[0]->la_value;
112                         attr_mergeit( e, desc, vals );
113
114                         ldap_rdnfree(rdn);
115                         rdn = NULL;
116
117                         /* Every entry needs an objectclass. We don't really
118                          * know if our hardcoded choice here agrees with the
119                          * DN that was configured for this backend, but it's
120                          * better than nothing.
121                          *
122                          * should be a configuratable item
123                          */
124                         vals[0].bv_val = "organizationalUnit";
125                         vals[0].bv_len = sizeof("organizationalUnit")-1;
126                         attr_mergeit( e, ad_objectClass, vals );
127         
128                         if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
129                                 rs->sr_entry = e;
130                                 rs->sr_attrs = op->ors_attrs;
131                                 rs->sr_flags = REP_ENTRY_MODIFIABLE;
132                                 send_search_entry( op, rs );
133                         }
134                 }
135
136                 if ( op->ors_scope != LDAP_SCOPE_BASE ) {
137                         /* check all our "children" */
138
139                         ldap_pvt_thread_mutex_lock( &passwd_mutex );
140                         pw_start( op->o_bd );
141                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
142                                 /* check for abandon */
143                                 if ( op->o_abandon ) {
144                                         endpwent();
145                                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
146                                         return( -1 );
147                                 }
148
149                                 /* check time limit */
150                                 if ( op->ors_tlimit != SLAP_NO_LIMIT
151                                                 && slap_get_time() > stoptime )
152                                 {
153                                         send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
154                                         endpwent();
155                                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
156                                         return( 0 );
157                                 }
158
159                                 if ( !(e = pw2entry( op->o_bd, pw, &rs->sr_text )) ) {
160                                         rs->sr_err = LDAP_OTHER;
161                                         endpwent();
162                                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
163                                         goto done;
164                                 }
165
166                                 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
167                                         /* check size limit */
168                                         if ( --op->ors_slimit == -1 ) {
169                                                 send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
170                                                 endpwent();
171                                                 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
172                                                 return( 0 );
173                                         }
174
175                                         rs->sr_entry = e;
176                                         rs->sr_attrs = op->ors_attrs;
177                                         rs->sr_flags = REP_ENTRY_MODIFIABLE;
178                                         send_search_entry( op, rs );
179                                 }
180
181                                 entry_free( e );
182                         }
183                         endpwent();
184                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
185                 }
186
187         } else {
188                 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
189                         dnParent( &op->o_req_ndn, &parent );
190                 }
191
192                 /* This backend is only one layer deep. Don't answer requests for
193                  * anything deeper than that.
194                  */
195                 if( !be_issuffix( op->o_bd, &parent ) ) {
196                         int i;
197                         for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
198                                 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
199                                         rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
200                                         break;
201                                 }
202                         }
203                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
204                         goto done;
205                 }
206
207                 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
208                         goto done;
209                 }
210
211                 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
212                         LDAP_DN_FORMAT_LDAP ))
213                 { 
214                         rs->sr_err = LDAP_OTHER;
215                         goto done;
216                 }
217
218                 ldap_pvt_thread_mutex_lock( &passwd_mutex );
219                 pw_start( op->o_bd );
220                 if ( (pw = getpwnam( rdn[0]->la_value.bv_val )) == NULL ) {
221                         rs->sr_matched = parent.bv_val;
222                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
223                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
224                         goto done;
225                 }
226
227                 e = pw2entry( op->o_bd, pw, &rs->sr_text );
228                 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
229                 if ( !e ) {
230                         rs->sr_err = LDAP_OTHER;
231                         goto done;
232                 }
233
234                 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
235                         rs->sr_entry = e;
236                         rs->sr_attrs = op->ors_attrs;
237                         rs->sr_flags = REP_ENTRY_MODIFIABLE;
238                         send_search_entry( op, rs );
239                 }
240
241                 entry_free( e );
242         }
243
244 done:
245         if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
246         send_ldap_result( op, rs );
247
248         if( rdn != NULL ) ldap_rdnfree( rdn );
249
250         return( 0 );
251 }
252
253 static void
254 pw_start(
255         Backend *be
256 )
257 {
258         endpwent();
259
260 #ifdef HAVE_SETPWFILE
261         if ( be->be_private != NULL ) {
262                 (void) setpwfile( (char *) be->be_private );
263         }
264 #endif /* HAVE_SETPWFILE */
265 }
266
267 static Entry *
268 pw2entry( Backend *be, struct passwd *pw, const char **text )
269 {
270         size_t pwlen;
271         Entry           *e;
272         struct berval   vals[2];
273         struct berval   bv;
274
275         int rc;
276
277         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
278         AttributeDescription *ad_cn = NULL;
279         AttributeDescription *ad_sn = NULL;
280         AttributeDescription *ad_uid = NULL;
281         AttributeDescription *ad_description = NULL;
282
283         rc = slap_str2ad( "cn", &ad_cn, text );
284         if(rc != LDAP_SUCCESS) return NULL;
285         rc = slap_str2ad( "sn", &ad_sn, text );
286         if(rc != LDAP_SUCCESS) return NULL;
287         rc = slap_str2ad( "uid", &ad_uid, text );
288         if(rc != LDAP_SUCCESS) return NULL;
289         rc = slap_str2ad( "description", &ad_description, text );
290         if(rc != LDAP_SUCCESS) return NULL;
291
292         /*
293          * from pw we get pw_name and make it cn
294          * give it an objectclass of person.
295          */
296
297         pwlen = strlen( pw->pw_name );
298         vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
299         vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
300
301         /* rdn attribute type should be a configuratable item */
302         sprintf( vals[0].bv_val, "uid=%s,%s",
303                 pw->pw_name, be->be_suffix[0].bv_val );
304
305         rc = dnNormalize( 0, NULL, NULL, vals, &bv, NULL );
306         if( rc != LDAP_SUCCESS ) {
307                 free( vals[0].bv_val );
308                 return NULL;
309         }
310
311         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
312         e->e_name = vals[0];
313         e->e_nname = bv;
314
315         e->e_attrs = NULL;
316
317         vals[1].bv_val = NULL;
318
319         /* objectclasses should be configurable items */
320         vals[0].bv_val = "top";
321         vals[0].bv_len = sizeof("top")-1;
322         attr_mergeit( e, ad_objectClass, vals );
323
324         vals[0].bv_val = "person";
325         vals[0].bv_len = sizeof("person")-1;
326         attr_mergeit( e, ad_objectClass, vals );
327
328         vals[0].bv_val = "uidObject";
329         vals[0].bv_len = sizeof("uidObject")-1;
330         attr_mergeit( e, ad_objectClass, vals );
331
332         vals[0].bv_val = pw->pw_name;
333         vals[0].bv_len = pwlen;
334         attr_mergeit( e, ad_uid, vals );        /* required by uidObject */
335         attr_mergeit( e, ad_cn, vals ); /* required by person */
336         attr_mergeit( e, ad_sn, vals ); /* required by person */
337
338 #ifdef HAVE_PW_GECOS
339         /*
340          * if gecos is present, add it as a cn. first process it
341          * according to standard BSD usage. If the processed cn has
342          * a space, use the tail as the surname.
343          */
344         if (pw->pw_gecos[0]) {
345                 char *s;
346
347                 vals[0].bv_val = pw->pw_gecos;
348                 vals[0].bv_len = strlen(vals[0].bv_val);
349                 attr_mergeit(e, ad_description, vals);
350
351                 s = strchr(vals[0].bv_val, ',');
352                 if (s) *s = '\0';
353
354                 s = strchr(vals[0].bv_val, '&');
355                 if (s) {
356                         char buf[1024];
357
358                         if( vals[0].bv_len + pwlen < sizeof(buf) ) {
359                                 int i = s - vals[0].bv_val;
360                                 strncpy(buf, vals[0].bv_val, i);
361                                 s = buf+i;
362                                 strcpy(s, pw->pw_name);
363                                 *s = TOUPPER((unsigned char)*s);
364                                 strcat(s, vals[0].bv_val+i+1);
365                                 vals[0].bv_val = buf;
366                         }
367                 }
368                 vals[0].bv_len = strlen(vals[0].bv_val);
369
370                 if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
371                         attr_mergeit( e, ad_cn, vals );
372                 }
373
374                 if ( (s=strrchr(vals[0].bv_val, ' '))) {
375                         vals[0].bv_val = s + 1;
376                         vals[0].bv_len = strlen(vals[0].bv_val);
377                         attr_mergeit(e, ad_sn, vals);
378                 }
379         }
380 #endif
381
382         return( e );
383 }