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