]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
1932583d17a1c5d2e0a2c7785159b9bd36ea331d
[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                                 send_search_entry( op, rs );
135                         }
136                 }
137
138                 if ( op->ors_scope != LDAP_SCOPE_BASE ) {
139                         /* check all our "children" */
140
141                         ldap_pvt_thread_mutex_lock( &passwd_mutex );
142                         pw_start( op->o_bd );
143                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
144                                 /* check for abandon */
145                                 if ( op->o_abandon ) {
146                                         endpwent();
147                                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
148                                         return( -1 );
149                                 }
150
151                                 /* check time limit */
152                                 if ( slap_get_time() > stoptime ) {
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                                         send_search_entry( op, rs );
178                                 }
179
180                                 entry_free( e );
181                         }
182                         endpwent();
183                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
184                 }
185
186         } else {
187                 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
188                         dnParent( &op->o_req_ndn, &parent );
189                 }
190
191                 /* This backend is only one layer deep. Don't answer requests for
192                  * anything deeper than that.
193                  */
194                 if( !be_issuffix( op->o_bd, &parent ) ) {
195                         int i;
196                         for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
197                                 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
198                                         rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
199                                         break;
200                                 }
201                         }
202                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
203                         goto done;
204                 }
205
206                 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
207                         goto done;
208                 }
209
210                 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
211                         LDAP_DN_FORMAT_LDAP ))
212                 { 
213                         rs->sr_err = LDAP_OTHER;
214                         goto done;
215                 }
216
217                 ldap_pvt_thread_mutex_lock( &passwd_mutex );
218                 pw_start( op->o_bd );
219                 if ( (pw = getpwnam( rdn[0]->la_value.bv_val )) == NULL ) {
220                         rs->sr_matched = parent.bv_val;
221                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
222                         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
223                         goto done;
224                 }
225
226                 e = pw2entry( op->o_bd, pw, &rs->sr_text );
227                 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
228                 if ( !e ) {
229                         rs->sr_err = LDAP_OTHER;
230                         goto done;
231                 }
232
233                 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
234                         rs->sr_entry = e;
235                         rs->sr_attrs = op->ors_attrs;
236                         send_search_entry( op, rs );
237                 }
238
239                 entry_free( e );
240         }
241
242 done:
243         if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
244         send_ldap_result( op, rs );
245
246         if( rdn != NULL ) ldap_rdnfree( rdn );
247
248         return( 0 );
249 }
250
251 static void
252 pw_start(
253         Backend *be
254 )
255 {
256         endpwent();
257
258 #ifdef HAVE_SETPWFILE
259         if ( be->be_private != NULL ) {
260                 (void) setpwfile( (char *) be->be_private );
261         }
262 #endif /* HAVE_SETPWFILE */
263 }
264
265 static Entry *
266 pw2entry( Backend *be, struct passwd *pw, const char **text )
267 {
268         size_t pwlen;
269         Entry           *e;
270         struct berval   vals[2];
271         struct berval   bv;
272
273         int rc;
274
275         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
276         AttributeDescription *ad_cn = NULL;
277         AttributeDescription *ad_sn = NULL;
278         AttributeDescription *ad_uid = NULL;
279         AttributeDescription *ad_description = NULL;
280
281         rc = slap_str2ad( "cn", &ad_cn, text );
282         if(rc != LDAP_SUCCESS) return NULL;
283         rc = slap_str2ad( "sn", &ad_sn, text );
284         if(rc != LDAP_SUCCESS) return NULL;
285         rc = slap_str2ad( "uid", &ad_uid, text );
286         if(rc != LDAP_SUCCESS) return NULL;
287         rc = slap_str2ad( "description", &ad_description, text );
288         if(rc != LDAP_SUCCESS) return NULL;
289
290         /*
291          * from pw we get pw_name and make it cn
292          * give it an objectclass of person.
293          */
294
295         pwlen = strlen( pw->pw_name );
296         vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
297         vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
298
299         /* rdn attribute type should be a configuratable item */
300         sprintf( vals[0].bv_val, "uid=%s,%s",
301                 pw->pw_name, be->be_suffix[0].bv_val );
302
303         rc = dnNormalize( 0, NULL, NULL, vals, &bv, NULL );
304         if( rc != LDAP_SUCCESS ) {
305                 free( vals[0].bv_val );
306                 return NULL;
307         }
308
309         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
310         e->e_name = vals[0];
311         e->e_nname = bv;
312
313         e->e_attrs = NULL;
314
315         vals[1].bv_val = NULL;
316
317         /* objectclasses should be configurable items */
318         vals[0].bv_val = "top";
319         vals[0].bv_len = sizeof("top")-1;
320         attr_mergeit( e, ad_objectClass, vals );
321
322         vals[0].bv_val = "person";
323         vals[0].bv_len = sizeof("person")-1;
324         attr_mergeit( e, ad_objectClass, vals );
325
326         vals[0].bv_val = "uidObject";
327         vals[0].bv_len = sizeof("uidObject")-1;
328         attr_mergeit( e, ad_objectClass, vals );
329
330         vals[0].bv_val = pw->pw_name;
331         vals[0].bv_len = pwlen;
332         attr_mergeit( e, ad_uid, vals );        /* required by uidObject */
333         attr_mergeit( e, ad_cn, vals ); /* required by person */
334         attr_mergeit( e, ad_sn, vals ); /* required by person */
335
336 #ifdef HAVE_PW_GECOS
337         /*
338          * if gecos is present, add it as a cn. first process it
339          * according to standard BSD usage. If the processed cn has
340          * a space, use the tail as the surname.
341          */
342         if (pw->pw_gecos[0]) {
343                 char *s;
344
345                 vals[0].bv_val = pw->pw_gecos;
346                 vals[0].bv_len = strlen(vals[0].bv_val);
347                 attr_mergeit(e, ad_description, vals);
348
349                 s = strchr(vals[0].bv_val, ',');
350                 if (s) *s = '\0';
351
352                 s = strchr(vals[0].bv_val, '&');
353                 if (s) {
354                         char buf[1024];
355
356                         if( vals[0].bv_len + pwlen < sizeof(buf) ) {
357                                 int i = s - vals[0].bv_val;
358                                 strncpy(buf, vals[0].bv_val, i);
359                                 s = buf+i;
360                                 strcpy(s, pw->pw_name);
361                                 *s = TOUPPER((unsigned char)*s);
362                                 strcat(s, vals[0].bv_val+i+1);
363                                 vals[0].bv_val = buf;
364                         }
365                 }
366                 vals[0].bv_len = strlen(vals[0].bv_val);
367
368                 if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
369                         attr_mergeit( e, ad_cn, vals );
370                 }
371
372                 if ( (s=strrchr(vals[0].bv_val, ' '))) {
373                         vals[0].bv_val = s + 1;
374                         vals[0].bv_len = strlen(vals[0].bv_val);
375                         attr_mergeit(e, ad_sn, vals);
376                 }
377         }
378 #endif
379
380         return( e );
381 }