]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
Const'ification
[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         tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
55             : tlimit;
56         stoptime = op->o_time + tlimit;
57         slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
58             : slimit;
59
60         endpwent();
61
62 #ifdef HAVE_SETPWFILE
63         if ( be->be_private != NULL ) {
64                 (void) setpwfile( (char *) be->be_private );
65         }
66 #endif /* HAVE_SETPWFILE */
67
68         /* Handle a query for the base of this backend */
69         if ( be_issuffix( be,  nbase ) ) {
70                 struct berval   val, *vals[2];
71
72                 vals[0] = &val;
73                 vals[1] = NULL;
74
75                 matched = ch_strdup( base );
76
77                 if( scope != LDAP_SCOPE_ONELEVEL ) {
78                         /* Create an entry corresponding to the base DN */
79                         e = (Entry *) ch_calloc(1, sizeof(Entry));
80                         e->e_attrs = NULL;
81                         e->e_dn = ch_strdup( base );
82
83                         /* Use the first attribute of the DN
84                         * as an attribute within the entry itself.
85                         */
86                         rdn = dn_rdn(NULL, base);
87
88                         if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
89                                 err = LDAP_INVALID_DN_SYNTAX;
90                                 goto done;
91                         }
92
93                         val.bv_val = rdn_attr_value(rdn);
94                         val.bv_len = strlen( val.bv_val );
95                         attr_merge( e, rdn_attr_type(rdn), vals );
96
97                         free(rdn);
98                         rdn = NULL;
99
100                         /* Every entry needs an objectclass. We don't really
101                          * know if our hardcoded choice here agrees with the
102                          * DN that was configured for this backend, but it's
103                          * better than nothing.
104                          *
105                          * should be a configuratable item
106                          */
107                         val.bv_val = "organizationalUnit";
108                         val.bv_len = strlen( val.bv_val );
109                         attr_merge( e, "objectClass", vals );
110         
111                         if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
112                                 send_search_entry( be, conn, op,
113                                         e, attrs, attrsonly, NULL );
114                                 sent++;
115                         }
116                 }
117
118                 if ( scope != LDAP_SCOPE_BASE ) {
119                         /* check all our "children" */
120
121                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
122                                 /* check for abandon */
123                                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
124                                 if ( op->o_abandon ) {
125                                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
126                                         endpwent();
127                                         return( -1 );
128                                 }
129                                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
130
131                                 /* check time limit */
132                                 if ( slap_get_time() > stoptime ) {
133                                         send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
134                                         NULL, NULL, NULL, NULL );
135                                         endpwent();
136                                         return( 0 );
137                                 }
138
139                                 e = pw2entry( be, pw, NULL );
140
141                                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
142                                         /* check size limit */
143                                         if ( --slimit == -1 ) {
144                                                 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
145                                                 NULL, NULL, NULL, NULL );
146                                                 endpwent();
147                                                 return( 0 );
148                                         }
149
150                                         send_search_entry( be, conn, op,
151                                                 e, attrs, attrsonly, NULL );
152                                         sent++;
153                                 }
154
155                                 entry_free( e );
156                         }
157                         endpwent();
158                 }
159
160         } else {
161                 parent = dn_parent( be, base );
162
163                 /* This backend is only one layer deep. Don't answer requests for
164                  * anything deeper than that.
165                  */
166                 if( !be_issuffix( be, parent ) ) {
167                         int i;
168                         for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
169                                 if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
170                                         matched = ch_strdup( be->be_suffix[i] );
171                                         break;
172                                 }
173                         }
174                         err = LDAP_NO_SUCH_OBJECT;
175                         goto done;
176                 }
177
178                 if( scope == LDAP_SCOPE_ONELEVEL ) {
179                         goto done;
180                 }
181
182                 rdn = dn_rdn( NULL, base );
183
184                 if ( (user = rdn_attr_value(rdn)) == NULL) {
185                         err = LDAP_OPERATIONS_ERROR;
186                         goto done;
187                 }
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 ) == LDAP_COMPARE_TRUE ) {
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( 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 }