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