]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
fe386246d9265ecb4982bc3e3c09d543ff5b4d50
[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                         free(rdn);
119                         rdn = NULL;
120
121                         /* Every entry needs an objectclass. We don't really
122                          * know if our hardcoded choice here agrees with the
123                          * DN that was configured for this backend, but it's
124                          * better than nothing.
125                          *
126                          * should be a configuratable item
127                          */
128                         val.bv_val = "organizationalUnit";
129                         val.bv_len = sizeof("organizationalUnit")-1;
130                         attr_merge( e, ad_objectClass, vals );
131         
132                         if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
133                                 send_search_entry( be, conn, op,
134                                         e, attrs, attrsonly, NULL );
135                                 sent++;
136                         }
137                 }
138
139                 if ( scope != LDAP_SCOPE_BASE ) {
140                         /* check all our "children" */
141
142                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
143                                 /* check for abandon */
144                                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
145                                 if ( op->o_abandon ) {
146                                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
147                                         endpwent();
148                                         return( -1 );
149                                 }
150                                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
151
152                                 /* check time limit */
153                                 if ( slap_get_time() > stoptime ) {
154                                         send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
155                                         NULL, NULL, NULL, NULL );
156                                         endpwent();
157                                         return( 0 );
158                                 }
159
160                                 e = pw2entry( be, pw, NULL );
161
162                                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
163                                         /* check size limit */
164                                         if ( --slimit == -1 ) {
165                                                 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
166                                                 NULL, NULL, NULL, NULL );
167                                                 endpwent();
168                                                 return( 0 );
169                                         }
170
171                                         send_search_entry( be, conn, op,
172                                                 e, attrs, attrsonly, NULL );
173                                         sent++;
174                                 }
175
176                                 entry_free( e );
177                         }
178                         endpwent();
179                 }
180
181         } else {
182                 parent = dn_parent( be, nbase );
183
184                 /* This backend is only one layer deep. Don't answer requests for
185                  * anything deeper than that.
186                  */
187                 if( !be_issuffix( be, parent ) ) {
188                         int i;
189                         for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
190                                 if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
191                                         matched = ch_strdup( be->be_suffix[i] );
192                                         break;
193                                 }
194                         }
195                         err = LDAP_NO_SUCH_OBJECT;
196                         goto done;
197                 }
198
199                 if( scope == LDAP_SCOPE_ONELEVEL ) {
200                         goto done;
201                 }
202
203                 rdn = dn_rdn( NULL, base );
204
205                 if ( (user = rdn_attr_value(rdn)) == NULL) {
206                         err = LDAP_OPERATIONS_ERROR;
207                         goto done;
208                 }
209
210                 if ( (pw = getpwnam( user )) == NULL ) {
211                         matched = parent;
212                         parent = NULL;
213                         err = LDAP_NO_SUCH_OBJECT;
214                         goto done;
215                 }
216
217                 e = pw2entry( be, pw, rdn );
218
219                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
220                         send_search_entry( be, conn, op,
221                                 e, attrs, attrsonly, NULL );
222                         sent++;
223                 }
224
225                 entry_free( e );
226         }
227
228 done:
229         send_ldap_result( conn, op,
230                 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
231                 NULL, NULL );
232
233         if( matched != NULL ) free( matched );
234         if( parent != NULL ) free( parent );
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] );
297         e->e_dn = ch_strdup( buf );
298         e->e_ndn = ch_strdup( buf );
299         (void) dn_normalize( e->e_ndn );
300
301         val.bv_val = pw->pw_name;
302         val.bv_len = strlen( pw->pw_name );
303         attr_merge( e, ad_uid, vals );  /* required by uidObject */
304         attr_merge( e, ad_cn, vals );   /* required by person */
305         attr_merge( e, ad_sn, vals );   /* required by person */
306
307 #ifdef HAVE_PW_GECOS
308         /*
309          * if gecos is present, add it as a cn. first process it
310          * according to standard BSD usage. If the processed cn has
311          * a space, use the tail as the surname.
312          */
313         if (pw->pw_gecos[0]) {
314                 char *s;
315
316                 val.bv_val = pw->pw_gecos;
317                 val.bv_len = strlen(val.bv_val);
318                 attr_merge(e, ad_description, vals);
319
320                 s = strchr(val.bv_val, ',');
321                 if (s)
322                         *s = '\0';
323                 s = strchr(val.bv_val, '&');
324                 if (s) {
325                         int i = s - val.bv_val;
326                         strncpy(buf, val.bv_val, i);
327                         s = buf+i;
328                         strcpy(s, pw->pw_name);
329                         *s = TOUPPER(*s);
330                         strcat(s, val.bv_val+i+1);
331                         val.bv_val = buf;
332                 }
333                 val.bv_len = strlen(val.bv_val);
334                 if ( strcmp( val.bv_val, pw->pw_name ))
335                         attr_merge( e, ad_cn, vals );
336                 if ( (s=strrchr(val.bv_val, ' '))) {
337                         val.bv_val = s + 1;
338                         val.bv_len = strlen(val.bv_val);
339                         attr_merge(e, ad_sn, vals);
340                 }
341         }
342 #endif
343
344         return( e );
345 }