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