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