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