Debian Codegraph @

servers/slapd/back-passwd/search.c

raw
1 /* search.c - /etc/passwd backend search function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 1998-2024 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
15 */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
25 */
26 /* ACKNOWLEDGEMENTS:
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP). Additional significant contributors
29 * include:
30 * Hallvard B. Furuseth
31 * Howard Chu
32 * Kurt D. Zeilenga
33 */
34
35 #include "portable.h"
36
37 #include <stdio.h>
38
39 #include <ac/ctype.h>
40 #include <ac/socket.h>
41 #include <ac/string.h>
42 #include <ac/time.h>
43
44 #include <pwd.h>
45
46 #include "slap.h"
47 #include "back-passwd.h"
48
49 static void pw_start( Backend *be );
50
51 static int pw2entry(
52 Backend *be,
53 struct passwd *pw,
54 Entry *ep );
55
56 int
57 passwd_back_search(
58 Operation *op,
59 SlapReply *rs )
60 {
61 struct passwd *pw;
62 time_t stoptime = (time_t)-1;
63
64 LDAPRDN rdn = NULL;
65 struct berval parent = BER_BVNULL;
66
67 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
68
69 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
70 stoptime = op->o_time + op->ors_tlimit;
71 }
72
73 /* Handle a query for the base of this backend */
74 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
75 struct berval val;
76
77 rs->sr_matched = op->o_req_dn.bv_val;
78
79 if( op->ors_scope != LDAP_SCOPE_ONELEVEL ) {
80 AttributeDescription *desc = NULL;
81 char *next;
82 Entry e = { 0 };
83
84 /* Create an entry corresponding to the base DN */
85 e.e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
86 e.e_name.bv_len = op->o_req_dn.bv_len;
87 e.e_nname.bv_val = ch_strdup( op->o_req_ndn.bv_val );
88 e.e_nname.bv_len = op->o_req_ndn.bv_len;
89
90 /* Use the first attribute of the DN
91 * as an attribute within the entry itself.
92 */
93 if( ldap_bv2rdn( &op->o_req_dn, &rdn, &next,
94 LDAP_DN_FORMAT_LDAP ) )
95 {
96 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
97 goto done;
98 }
99
100 if( slap_bv2ad( &rdn[0]->la_attr, &desc, &rs->sr_text )) {
101 rs->sr_err = LDAP_NO_SUCH_OBJECT;
102 ldap_rdnfree(rdn);
103 goto done;
104 }
105
106 attr_merge_normalize_one( &e, desc, &rdn[0]->la_value, NULL );
107
108 ldap_rdnfree(rdn);
109 rdn = NULL;
110
111 /* Every entry needs an objectclass. We don't really
112 * know if our hardcoded choice here agrees with the
113 * DN that was configured for this backend, but it's
114 * better than nothing.
115 *
116 * should be a configurable item
117 */
118 BER_BVSTR( &val, "organizationalUnit" );
119 attr_merge_one( &e, ad_objectClass, &val, NULL );
120
121 if ( test_filter( op, &e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
122 rs->sr_entry = &e;
123 rs->sr_attrs = op->ors_attrs;
124 rs->sr_flags = REP_ENTRY_MODIFIABLE;
125 send_search_entry( op, rs );
126 rs->sr_flags = 0;
127 rs->sr_attrs = NULL;
128 }
129
130 entry_clean( &e );
131 }
132
133 if ( op->ors_scope != LDAP_SCOPE_BASE ) {
134 /* check all our "children" */
135
136 ldap_pvt_thread_mutex_lock( &passwd_mutex );
137 pw_start( op->o_bd );
138 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
139 Entry e = { 0 };
140
141 /* check for abandon */
142 if ( op->o_abandon ) {
143 endpwent();
144 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
145 return( SLAPD_ABANDON );
146 }
147
148 /* check time limit */
149 if ( op->ors_tlimit != SLAP_NO_LIMIT
150 && slap_get_time() > stoptime )
151 {
152 send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
153 endpwent();
154 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
155 return( 0 );
156 }
157
158 if ( pw2entry( op->o_bd, pw, &e ) ) {
159 rs->sr_err = LDAP_OTHER;
160 endpwent();
161 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
162 goto done;
163 }
164
165 if ( test_filter( op, &e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
166 /* check size limit */
167 if ( --op->ors_slimit == -1 ) {
168 send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
169 endpwent();
170 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
171 return( 0 );
172 }
173
174 rs->sr_entry = &e;
175 rs->sr_attrs = op->ors_attrs;
176 rs->sr_flags = REP_ENTRY_MODIFIABLE;
177 send_search_entry( op, rs );
178 rs->sr_flags = 0;
179 rs->sr_entry = NULL;
180 }
181
182 entry_clean( &e );
183 }
184 endpwent();
185 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
186 }
187
188 } else {
189 char *next;
190 Entry e = { 0 };
191 int rc;
192
193 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
194 dnParent( &op->o_req_ndn, &parent );
195 }
196
197 /* This backend is only one layer deep. Don't answer requests for
198 * anything deeper than that.
199 */
200 if( !be_issuffix( op->o_bd, &parent ) ) {
201 int i;
202 for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
203 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
204 rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
205 break;
206 }
207 }
208 rs->sr_err = LDAP_NO_SUCH_OBJECT;
209 goto done;
210 }
211
212 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
213 goto done;
214 }
215
216 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, &next,
217 LDAP_DN_FORMAT_LDAP ))
218 {
219 rs->sr_err = LDAP_OTHER;
220 goto done;
221 }
222
223 ldap_pvt_thread_mutex_lock( &passwd_mutex );
224 pw_start( op->o_bd );
225 pw = getpwnam( rdn[0]->la_value.bv_val );
226 if ( pw == NULL ) {
227 rs->sr_matched = parent.bv_val;
228 rs->sr_err = LDAP_NO_SUCH_OBJECT;
229 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
230 goto done;
231 }
232
233 rc = pw2entry( op->o_bd, pw, &e );
234 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
235 if ( rc ) {
236 rs->sr_err = LDAP_OTHER;
237 goto done;
238 }
239
240 if ( test_filter( op, &e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
241 rs->sr_entry = &e;
242 rs->sr_attrs = op->ors_attrs;
243 rs->sr_flags = REP_ENTRY_MODIFIABLE;
244 send_search_entry( op, rs );
245 rs->sr_flags = 0;
246 rs->sr_entry = NULL;
247 rs->sr_attrs = NULL;
248 }
249
250 entry_clean( &e );
251 }
252
253 done:
254 if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
255 send_ldap_result( op, rs );
256
257 if( rdn != NULL ) ldap_rdnfree( rdn );
258
259 return( 0 );
260 }
261
262 static void
263 pw_start(
264 Backend *be
265 )
266 {
267 endpwent();
268
269 #ifdef HAVE_SETPWFILE
270 if ( be->be_private != NULL ) {
271 (void) setpwfile( (char *) be->be_private );
272 }
273 #endif /* HAVE_SETPWFILE */
274 }
275
276 static int
277 pw2entry( Backend *be, struct passwd *pw, Entry *e )
278 {
279 size_t pwlen;
280 struct berval val;
281 struct berval bv;
282
283 int rc;
284
285 /*
286 * from pw we get pw_name and make it cn
287 * give it an objectclass of person.
288 */
289
290 pwlen = strlen( pw->pw_name );
291 val.bv_len = STRLENOF("uid=,") + ( pwlen + be->be_suffix[0].bv_len );
292 val.bv_val = ch_malloc( val.bv_len + 1 );
293
294 /* rdn attribute type should be a configurable item */
295 sprintf( val.bv_val, "uid=%s,%s",
296 pw->pw_name, be->be_suffix[0].bv_val );
297
298 rc = dnNormalize( 0, NULL, NULL, &val, &bv, NULL );
299 if( rc != LDAP_SUCCESS ) {
300 free( val.bv_val );
301 return( -1 );
302 }
303
304 e->e_name = val;
305 e->e_nname = bv;
306
307 e->e_attrs = NULL;
308
309 /* objectclasses should be configurable items */
310 BER_BVSTR( &val, "person" );
311 attr_merge_one( e, slap_schema.si_ad_objectClass, &val, NULL );
312
313 BER_BVSTR( &val, "uidObject" );
314 attr_merge_one( e, slap_schema.si_ad_objectClass, &val, NULL );
315
316 val.bv_val = pw->pw_name;
317 val.bv_len = pwlen;
318 attr_merge_normalize_one( e, slap_schema.si_ad_uid, &val, NULL ); /* required by uidObject */
319 attr_merge_normalize_one( e, slap_schema.si_ad_cn, &val, NULL ); /* required by person */
320 attr_merge_normalize_one( e, ad_sn, &val, NULL ); /* required by person */
321
322 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
323 /*
324 * if gecos is present, add user's full name as a cn. first process it
325 * according to standard BSD usage. If the processed cn has
326 * a space, use the tail as the surname.
327 */
328 if (pw->pw_gecos[0]) {
329 char *s;
330 char buf[1024];
331
332 ber_str2bv( pw->pw_gecos, 0, 0, &val );
333 attr_merge_normalize_one( e, ad_desc, &val, NULL );
334
335 s = strchr( val.bv_val, (unsigned char)',' );
336 if ( s ) {
337 *s = '\0';
338 val.bv_len = s - val.bv_val;
339 }
340
341 s = strchr( val.bv_val, (unsigned char)'&' );
342 if ( s ) {
343 unsigned r = sizeof buf;
344 /* if name with expanded `&` fits in buf */
345 if ( val.bv_len + pwlen <= r ) {
346 char * d = buf;
347
348 for (;;) {
349 size_t const i = s - val.bv_val;
350 memcpy( d, val.bv_val, i );
351 d += i;
352 memcpy( d, pw->pw_name, pwlen );
353 *d = TOUPPER((unsigned char)*pw->pw_name);
354 d += pwlen;
355 r -= pwlen + i;
356 val.bv_len -= i + 1;
357 val.bv_val = s + 1;
358
359 s = strchr( val.bv_val, (unsigned char)'&' );
360 if (!(s && ( val.bv_len + pwlen <= r )))
361 break;
362 }
363 strcpy( d, val.bv_val );
364 val.bv_len = d - buf + val.bv_len;
365 val.bv_val = buf;
366 } // 1st fits
367 } // 1st &
368
369 if ( val.bv_len && strcasecmp( val.bv_val, pw->pw_name ) ) {
370 attr_merge_normalize_one( e, slap_schema.si_ad_cn, &val, NULL );
371 }
372
373 if ( ( s = strrchr(val.bv_val, ' ' ) ) ) {
374 ber_str2bv( s + 1, 0, 0, &val );
375 attr_merge_normalize_one( e, ad_sn, &val, NULL );
376 }
377 }
378 #endif /* HAVE_STRUCT_PASSWD_PW_GECOS */
379
380 return( 0 );
381 }