]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
BDB_INDEX code does no harm (but no good yet, not used by filters yet).
[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     const char  *base,
30     const char  *nbase,
31     int         scope,
32     int         deref,
33     int         slimit,
34     int         tlimit,
35     Filter      *filter,
36     const char  *filterstr,
37     char        **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 ) ) {
72                 struct berval   val, *vals[2];
73
74                 vals[0] = &val;
75                 vals[1] = NULL;
76
77                 matched = ch_strdup( 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_attrs = NULL;
86                         e->e_dn = ch_strdup( base );
87
88                         /* Use the first attribute of the DN
89                         * as an attribute within the entry itself.
90                         */
91                         rdn = dn_rdn(NULL, base);
92
93                         if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
94                                 err = LDAP_INVALID_DN_SYNTAX;
95                                 free(rdn);
96                                 goto done;
97                         }
98
99                         val.bv_val = rdn_attr_value(rdn);
100                         val.bv_len = strlen( val.bv_val );
101
102                         type = rdn_attr_type(rdn);
103
104                         {
105                                 int rc;
106                                 const char *text;
107                                 rc = slap_str2ad( type, &desc, &text );
108
109                                 if( rc != LDAP_SUCCESS ) {
110                                         err = LDAP_NO_SUCH_OBJECT;
111                                         free(rdn);
112                                         goto done;
113                                 }
114                         }
115
116                         attr_merge( e, desc, vals );
117
118                         ad_free( desc, 1 );
119
120                         free(rdn);
121                         rdn = NULL;
122
123                         /* Every entry needs an objectclass. We don't really
124                          * know if our hardcoded choice here agrees with the
125                          * DN that was configured for this backend, but it's
126                          * better than nothing.
127                          *
128                          * should be a configuratable item
129                          */
130                         val.bv_val = "organizationalUnit";
131                         val.bv_len = sizeof("organizationalUnit")-1;
132                         attr_merge( e, ad_objectClass, vals );
133         
134                         if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
135                                 send_search_entry( be, conn, op,
136                                         e, attrs, attrsonly, NULL );
137                                 sent++;
138                         }
139                 }
140
141                 if ( scope != LDAP_SCOPE_BASE ) {
142                         /* check all our "children" */
143
144                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
145                                 /* check for abandon */
146                                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
147                                 if ( op->o_abandon ) {
148                                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
149                                         endpwent();
150                                         return( -1 );
151                                 }
152                                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
153
154                                 /* check time limit */
155                                 if ( slap_get_time() > stoptime ) {
156                                         send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
157                                         NULL, NULL, NULL, NULL );
158                                         endpwent();
159                                         return( 0 );
160                                 }
161
162                                 e = pw2entry( be, pw, NULL );
163
164                                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
165                                         /* check size limit */
166                                         if ( --slimit == -1 ) {
167                                                 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
168                                                 NULL, NULL, NULL, NULL );
169                                                 endpwent();
170                                                 return( 0 );
171                                         }
172
173                                         send_search_entry( be, conn, op,
174                                                 e, attrs, attrsonly, NULL );
175                                         sent++;
176                                 }
177
178                                 entry_free( e );
179                         }
180                         endpwent();
181                 }
182
183         } else {
184                 parent = dn_parent( be, nbase );
185
186                 /* This backend is only one layer deep. Don't answer requests for
187                  * anything deeper than that.
188                  */
189                 if( !be_issuffix( be, parent ) ) {
190                         int i;
191                         for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
192                                 if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
193                                         matched = ch_strdup( be->be_suffix[i] );
194                                         break;
195                                 }
196                         }
197                         err = LDAP_NO_SUCH_OBJECT;
198                         goto done;
199                 }
200
201                 if( scope == LDAP_SCOPE_ONELEVEL ) {
202                         goto done;
203                 }
204
205                 rdn = dn_rdn( NULL, base );
206
207                 if ( (user = rdn_attr_value(rdn)) == NULL) {
208                         err = LDAP_OPERATIONS_ERROR;
209                         goto done;
210                 }
211
212                 if ( (pw = getpwnam( user )) == NULL ) {
213                         matched = parent;
214                         parent = NULL;
215                         err = LDAP_NO_SUCH_OBJECT;
216                         goto done;
217                 }
218
219                 e = pw2entry( be, pw, rdn );
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, NULL,
233                 NULL, NULL );
234
235         if( matched != NULL ) free( matched );
236         if( parent != NULL ) free( parent );
237         if( rdn != NULL ) free( rdn );
238         if( user != NULL ) free( user );
239
240         return( 0 );
241 }
242
243 static Entry *
244 pw2entry( Backend *be, struct passwd *pw, char *rdn )
245 {
246         Entry           *e;
247         char            buf[256];
248         struct berval   val;
249         struct berval   *vals[2];
250
251         int rc;
252         const char *text;
253
254         AttributeDescription *ad_objectClass = NULL;
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( "objectClass", &ad_objectClass, &text );
261
262         if(rc != LDAP_SUCCESS) return NULL;
263         rc = slap_str2ad( "cn", &ad_cn, &text );
264         if(rc != LDAP_SUCCESS) return NULL;
265         rc = slap_str2ad( "sn", &ad_sn, &text );
266         if(rc != LDAP_SUCCESS) return NULL;
267         rc = slap_str2ad( "uid", &ad_uid, &text );
268         if(rc != LDAP_SUCCESS) return NULL;
269         rc = slap_str2ad( "description", &ad_description, &text );
270         if(rc != LDAP_SUCCESS) return NULL;
271
272
273         vals[0] = &val;
274         vals[1] = NULL;
275
276         /*
277          * from pw we get pw_name and make it cn
278          * give it an objectclass of person.
279          */
280
281         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
282         e->e_attrs = NULL;
283
284         /* objectclasses should be configuratable items */
285         val.bv_val = "top";
286         val.bv_len = sizeof("top")-1;
287         attr_merge( e, ad_objectClass, vals );
288
289         val.bv_val = "person";
290         val.bv_len = sizeof("person")-1;
291         attr_merge( e, ad_objectClass, vals );
292
293         val.bv_val = "uidObject";
294         val.bv_len = sizeof("uidObject")-1;
295         attr_merge( e, ad_objectClass, vals );
296
297         /* rdn attribute type should be a configuratable item */
298         sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
299         e->e_dn = ch_strdup( buf );
300         e->e_ndn = ch_strdup( buf );
301         (void) dn_normalize( e->e_ndn );
302
303         val.bv_val = pw->pw_name;
304         val.bv_len = strlen( pw->pw_name );
305         attr_merge( e, ad_uid, vals );  /* required by uidObject */
306         attr_merge( e, ad_cn, vals );   /* required by person */
307         attr_merge( e, ad_sn, vals );   /* required by person */
308
309 #ifdef HAVE_PW_GECOS
310         /*
311          * if gecos is present, add it as a cn. first process it
312          * according to standard BSD usage. If the processed cn has
313          * a space, use the tail as the surname.
314          */
315         if (pw->pw_gecos[0]) {
316                 char *s;
317
318                 val.bv_val = pw->pw_gecos;
319                 val.bv_len = strlen(val.bv_val);
320                 attr_merge(e, ad_description, vals);
321
322                 s = strchr(val.bv_val, ',');
323                 if (s)
324                         *s = '\0';
325                 s = strchr(val.bv_val, '&');
326                 if (s) {
327                         int i = s - val.bv_val;
328                         strncpy(buf, val.bv_val, i);
329                         s = buf+i;
330                         strcpy(s, pw->pw_name);
331                         *s = TOUPPER(*s);
332                         strcat(s, val.bv_val+i+1);
333                         val.bv_val = buf;
334                 }
335                 val.bv_len = strlen(val.bv_val);
336                 if ( strcmp( val.bv_val, pw->pw_name ))
337                         attr_merge( e, ad_cn, vals );
338                 if ( (s=strrchr(val.bv_val, ' '))) {
339                         val.bv_val = s + 1;
340                         val.bv_len = strlen(val.bv_val);
341                         attr_merge(e, ad_sn, vals);
342                 }
343         }
344 #endif
345
346         return( e );
347 }