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