]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
68af4b3fa44ed6f670cc83a7c51cbd58e3cb0581
[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     char        *base,
30     int         scope,
31     int         deref,
32     int         slimit,
33     int         tlimit,
34     Filter      *filter,
35     char        *filterstr,
36     char        **attrs,
37     int         attrsonly
38 )
39 {
40         struct passwd   *pw;
41         Entry           *e;
42         char            *s;
43         time_t          stoptime;
44
45         int sent = 0;
46         int err = LDAP_SUCCESS;
47
48         char *rdn = NULL;
49         char *parent = NULL;
50         char *matched = NULL;
51         char *user = NULL;
52
53         tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
54             : tlimit;
55         stoptime = op->o_time + tlimit;
56         slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
57             : slimit;
58
59         endpwent();
60
61 #ifdef HAVE_SETPWFILE
62         if ( be->be_private != NULL ) {
63                 (void) setpwfile( (char *) be->be_private );
64         }
65 #endif /* HAVE_SETPWFILE */
66
67         /* Handle a query for the base of this backend */
68         if ( be_issuffix( be,  base ) ) {
69                 struct berval   val, *vals[2];
70
71                 vals[0] = &val;
72                 vals[1] = NULL;
73
74                 matched = ch_strdup( base );
75
76                 if( scope != LDAP_SCOPE_ONELEVEL ) {
77                         /* Create an entry corresponding to the base DN */
78                         e = (Entry *) ch_calloc(1, sizeof(Entry));
79                         e->e_attrs = NULL;
80                         e->e_dn = ch_strdup( base );
81
82                         /* Use the first attribute of the DN
83                         * as an attribute within the entry itself.
84                         */
85                         rdn = dn_rdn(NULL, base);
86
87                         if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
88                                 err = LDAP_INVALID_DN_SYNTAX;
89                                 goto done;
90                         }
91
92                         val.bv_val = rdn_attr_value(rdn);
93                         val.bv_len = strlen( val.bv_val );
94                         attr_merge( e, rdn_attr_type(rdn), vals );
95
96                         free(rdn);
97                         rdn = NULL;
98
99                         /* Every entry needs an objectclass. We don't really
100                          * know if our hardcoded choice here agrees with the
101                          * DN that was configured for this backend, but it's
102                          * better than nothing.
103                          *
104                          * should be a configuratable item
105                          */
106                         val.bv_val = "organizationalUnit";
107                         val.bv_len = strlen( val.bv_val );
108                         attr_merge( e, "objectClass", vals );
109         
110                         if ( test_filter( be, conn, op, e, filter ) == 0 ) {
111                                 send_search_entry( be, conn, op,
112                                         e, attrs, attrsonly, NULL );
113                                 sent++;
114                         }
115                 }
116
117                 if ( scope != LDAP_SCOPE_BASE ) {
118                         /* check all our "children" */
119
120                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
121                                 /* check for abandon */
122                                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
123                                 if ( op->o_abandon ) {
124                                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
125                                         endpwent();
126                                         return( -1 );
127                                 }
128                                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
129
130                                 /* check time limit */
131                                 if ( slap_get_time() > stoptime ) {
132                                         send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
133                                         NULL, NULL, NULL, NULL );
134                                         endpwent();
135                                         return( 0 );
136                                 }
137
138                                 e = pw2entry( be, pw, NULL );
139
140                                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
141                                         /* check size limit */
142                                         if ( --slimit == -1 ) {
143                                                 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
144                                                 NULL, NULL, NULL, NULL );
145                                                 endpwent();
146                                                 return( 0 );
147                                         }
148
149                                         send_search_entry( be, conn, op,
150                                                 e, attrs, attrsonly, NULL );
151                                         sent++;
152                                 }
153
154                                 entry_free( e );
155                         }
156                         endpwent();
157                 }
158
159         } else {
160                 parent = dn_parent( be, base );
161
162                 /* This backend is only one layer deep. Don't answer requests for
163                  * anything deeper than that.
164                  */
165                 if( !be_issuffix( be, parent ) ) {
166                         int i;
167                         for( i=0; be->be_suffix[i] != NULL; i++ ) {
168                                 if( dn_issuffix( base, be->be_suffix[i] ) ) {
169                                         matched = ch_strdup( be->be_suffix[i] );
170                                         break;
171                                 }
172                         }
173                         err = LDAP_NO_SUCH_OBJECT;
174                         goto done;
175                 }
176
177                 if( scope == LDAP_SCOPE_ONELEVEL ) {
178                         goto done;
179                 }
180
181                 rdn = dn_rdn( NULL, base );
182
183                 if ( (user = rdn_attr_value(rdn)) == NULL) {
184                         err = LDAP_OPERATIONS_ERROR;
185                         goto done;
186                 }
187
188                 user = ldap_pvt_str2lower( user );
189
190                 if ( (pw = getpwnam( user )) == NULL ) {
191                         matched = parent;
192                         parent = NULL;
193                         err = LDAP_NO_SUCH_OBJECT;
194                         goto done;
195                 }
196
197                 e = pw2entry( be, pw, rdn );
198
199                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
200                         send_search_entry( be, conn, op,
201                                 e, attrs, attrsonly, NULL );
202                         sent++;
203                 }
204
205                 entry_free( e );
206         }
207
208 done:
209         send_ldap_result( conn, op,
210                 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
211                 NULL, NULL );
212
213         if( matched != NULL ) free( matched );
214         if( parent != NULL ) free( parent );
215         if( rdn != NULL ) free( rdn );
216         if( user != NULL ) free( user );
217
218         return( 0 );
219 }
220
221 static Entry *
222 pw2entry( Backend *be, struct passwd *pw, char *rdn )
223 {
224         Entry           *e;
225         char            buf[256];
226         struct berval   val;
227         struct berval   *vals[2];
228
229         vals[0] = &val;
230         vals[1] = NULL;
231
232         /*
233          * from pw we get pw_name and make it cn
234          * give it an objectclass of person.
235          */
236
237         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
238         e->e_attrs = NULL;
239
240         /* rdn attribute type should be a configuratable item */
241         sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
242         e->e_dn = ch_strdup( buf );
243         e->e_ndn = ch_strdup( buf );
244         (void) dn_normalize_case( e->e_ndn );
245
246         val.bv_val = pw->pw_name;
247         val.bv_len = strlen( pw->pw_name );
248         attr_merge( e, "uid", vals );   /* required by uidObject */
249         attr_merge( e, "cn", vals );    /* required by person */
250         attr_merge( e, "sn", vals );    /* required by person */
251
252 #ifdef HAVE_PW_GECOS
253         /*
254          * if gecos is present, add it as a cn. first process it
255          * according to standard BSD usage. If the processed cn has
256          * a space, use the tail as the surname.
257          */
258         if (pw->pw_gecos[0]) {
259                 char *s;
260
261                 val.bv_val = pw->pw_gecos;
262                 val.bv_len = strlen(val.bv_val);
263                 attr_merge(e, "description", vals);
264
265                 s = strchr(val.bv_val, ',');
266                 if (s)
267                         *s = '\0';
268                 s = strchr(val.bv_val, '&');
269                 if (s) {
270                         int i = s - val.bv_val;
271                         strncpy(buf, val.bv_val, i);
272                         s = buf+i;
273                         strcpy(s, pw->pw_name);
274                         *s = TOUPPER(*s);
275                         strcat(s, val.bv_val+i+1);
276                         val.bv_val = buf;
277                 }
278                 val.bv_len = strlen(val.bv_val);
279                 if ( strcmp( val.bv_val, pw->pw_name ))
280                         attr_merge( e, "cn", vals );
281                 if ( (s=strrchr(val.bv_val, ' '))) {
282                         val.bv_val = s + 1;
283                         val.bv_len = strlen(val.bv_val);
284                         attr_merge(e, "sn", vals);
285                 }
286         }
287 #endif
288
289         /* objectclasses should be configuratable items */
290         val.bv_val = "person";
291         val.bv_len = strlen( val.bv_val );
292         attr_merge( e, "objectclass", vals );
293
294         val.bv_val = "uidObject";
295         val.bv_len = strlen( val.bv_val );
296         attr_merge( e, "objectclass", vals );
297         return( e );
298 }