]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
ITS#1467 passwd fix from Brad Thompson <brad@vecna.com>
[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                         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] ) ) {
194                                         matched = ch_strdup( be->be_suffix[i] );
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                         parent = NULL;
216                         err = LDAP_NO_SUCH_OBJECT;
217                         goto done;
218                 }
219
220                 e = pw2entry( be, pw, rdn );
221
222                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
223                         send_search_entry( be, conn, op,
224                                 e, attrs, attrsonly, NULL );
225                         sent++;
226                 }
227
228                 entry_free( e );
229         }
230
231 done:
232         send_ldap_result( conn, op,
233                 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
234                 NULL, NULL );
235
236         if( matched != NULL ) free( matched );
237         if( parent != NULL ) free( parent );
238         if( rdn != NULL ) free( rdn );
239         if( user != NULL ) free( user );
240
241         return( 0 );
242 }
243
244 static Entry *
245 pw2entry( Backend *be, struct passwd *pw, char *rdn )
246 {
247         Entry           *e;
248         char            buf[256];
249         struct berval   val;
250         struct berval   *vals[2];
251
252         int rc;
253         const char *text;
254
255         AttributeDescription *ad_objectClass = NULL;
256         AttributeDescription *ad_cn = NULL;
257         AttributeDescription *ad_sn = NULL;
258         AttributeDescription *ad_uid = NULL;
259         AttributeDescription *ad_description = NULL;
260
261         rc = slap_str2ad( "objectClass", &ad_objectClass, &text );
262
263         if(rc != LDAP_SUCCESS) return NULL;
264         rc = slap_str2ad( "cn", &ad_cn, &text );
265         if(rc != LDAP_SUCCESS) return NULL;
266         rc = slap_str2ad( "sn", &ad_sn, &text );
267         if(rc != LDAP_SUCCESS) return NULL;
268         rc = slap_str2ad( "uid", &ad_uid, &text );
269         if(rc != LDAP_SUCCESS) return NULL;
270         rc = slap_str2ad( "description", &ad_description, &text );
271         if(rc != LDAP_SUCCESS) return NULL;
272
273
274         vals[0] = &val;
275         vals[1] = NULL;
276
277         /*
278          * from pw we get pw_name and make it cn
279          * give it an objectclass of person.
280          */
281
282         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
283         e->e_attrs = NULL;
284
285         /* objectclasses should be configuratable items */
286         val.bv_val = "top";
287         val.bv_len = sizeof("top")-1;
288         attr_merge( e, ad_objectClass, vals );
289
290         val.bv_val = "person";
291         val.bv_len = sizeof("person")-1;
292         attr_merge( e, ad_objectClass, vals );
293
294         val.bv_val = "uidObject";
295         val.bv_len = sizeof("uidObject")-1;
296         attr_merge( e, ad_objectClass, vals );
297
298         /* rdn attribute type should be a configuratable item */
299         sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
300         e->e_dn = ch_strdup( buf );
301         e->e_ndn = ch_strdup( buf );
302         (void) dn_normalize( e->e_ndn );
303
304         val.bv_val = pw->pw_name;
305         val.bv_len = strlen( pw->pw_name );
306         attr_merge( e, ad_uid, vals );  /* required by uidObject */
307         attr_merge( e, ad_cn, vals );   /* required by person */
308         attr_merge( e, ad_sn, vals );   /* required by person */
309
310 #ifdef HAVE_PW_GECOS
311         /*
312          * if gecos is present, add it as a cn. first process it
313          * according to standard BSD usage. If the processed cn has
314          * a space, use the tail as the surname.
315          */
316         if (pw->pw_gecos[0]) {
317                 char *s;
318
319                 val.bv_val = pw->pw_gecos;
320                 val.bv_len = strlen(val.bv_val);
321                 attr_merge(e, ad_description, vals);
322
323                 s = strchr(val.bv_val, ',');
324                 if (s)
325                         *s = '\0';
326                 s = strchr(val.bv_val, '&');
327                 if (s) {
328                         int i = s - val.bv_val;
329                         strncpy(buf, val.bv_val, i);
330                         s = buf+i;
331                         strcpy(s, pw->pw_name);
332                         *s = TOUPPER(*s);
333                         strcat(s, val.bv_val+i+1);
334                         val.bv_val = buf;
335                 }
336                 val.bv_len = strlen(val.bv_val);
337                 if ( strcmp( val.bv_val, pw->pw_name ))
338                         attr_merge( e, ad_cn, vals );
339                 if ( (s=strrchr(val.bv_val, ' '))) {
340                         val.bv_val = s + 1;
341                         val.bv_len = strlen(val.bv_val);
342                         attr_merge(e, ad_sn, vals);
343                 }
344         }
345 #endif
346
347         return( e );
348 }