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