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