1 /* search.c - /etc/passwd backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
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.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP). Additional significant contributors
30 * Hallvard B. Furuseth
40 #include <ac/socket.h>
41 #include <ac/string.h>
47 #include "back-passwd.h"
50 static void pw_start( Backend *be );
52 static Entry *pw2entry(
68 struct berval parent = { 0, NULL };
70 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
72 op->ors_tlimit = (op->ors_tlimit > op->o_bd->be_timelimit || op->ors_tlimit < 1) ? op->o_bd->be_timelimit
74 stoptime = op->o_time + op->ors_tlimit;
75 op->ors_slimit = (op->ors_slimit > op->o_bd->be_sizelimit || op->ors_slimit < 1) ? op->o_bd->be_sizelimit
78 /* Handle a query for the base of this backend */
79 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
80 struct berval vals[2];
82 vals[1].bv_val = NULL;
84 rs->sr_matched = op->o_req_dn.bv_val;
86 if( op->ors_scope != LDAP_SCOPE_ONELEVEL ) {
87 AttributeDescription *desc = NULL;
89 /* Create an entry corresponding to the base DN */
90 e = (Entry *) ch_calloc(1, sizeof(Entry));
91 e->e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
92 e->e_name.bv_len = op->o_req_dn.bv_len;
93 e->e_nname.bv_val = ch_strdup( op->o_req_ndn.bv_val );
94 e->e_nname.bv_len = op->o_req_ndn.bv_len;
98 /* Use the first attribute of the DN
99 * as an attribute within the entry itself.
101 if( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
102 LDAP_DN_FORMAT_LDAP ) )
104 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
108 if( slap_bv2ad( &rdn[0]->la_attr, &desc, &rs->sr_text )) {
109 rs->sr_err = LDAP_NO_SUCH_OBJECT;
114 vals[0] = rdn[0]->la_value;
115 attr_mergeit( e, desc, vals );
120 /* Every entry needs an objectclass. We don't really
121 * know if our hardcoded choice here agrees with the
122 * DN that was configured for this backend, but it's
123 * better than nothing.
125 * should be a configuratable item
127 vals[0].bv_val = "organizationalUnit";
128 vals[0].bv_len = sizeof("organizationalUnit")-1;
129 attr_mergeit( e, ad_objectClass, vals );
131 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
133 rs->sr_attrs = op->ors_attrs;
134 rs->sr_flags = REP_ENTRY_MODIFIABLE;
135 send_search_entry( op, rs );
139 if ( op->ors_scope != LDAP_SCOPE_BASE ) {
140 /* check all our "children" */
142 ldap_pvt_thread_mutex_lock( &passwd_mutex );
143 pw_start( op->o_bd );
144 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
145 /* check for abandon */
146 if ( op->o_abandon ) {
148 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
152 /* check time limit */
153 if ( slap_get_time() > stoptime ) {
154 send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
156 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
160 if ( !(e = pw2entry( op->o_bd, pw, &rs->sr_text )) ) {
161 rs->sr_err = LDAP_OTHER;
163 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
167 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
168 /* check size limit */
169 if ( --op->ors_slimit == -1 ) {
170 send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
172 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
177 rs->sr_attrs = op->ors_attrs;
178 rs->sr_flags = REP_ENTRY_MODIFIABLE;
179 send_search_entry( op, rs );
185 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
189 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
190 dnParent( &op->o_req_ndn, &parent );
193 /* This backend is only one layer deep. Don't answer requests for
194 * anything deeper than that.
196 if( !be_issuffix( op->o_bd, &parent ) ) {
198 for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
199 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
200 rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
204 rs->sr_err = LDAP_NO_SUCH_OBJECT;
208 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
212 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
213 LDAP_DN_FORMAT_LDAP ))
215 rs->sr_err = LDAP_OTHER;
219 ldap_pvt_thread_mutex_lock( &passwd_mutex );
220 pw_start( op->o_bd );
221 if ( (pw = getpwnam( rdn[0]->la_value.bv_val )) == NULL ) {
222 rs->sr_matched = parent.bv_val;
223 rs->sr_err = LDAP_NO_SUCH_OBJECT;
224 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
228 e = pw2entry( op->o_bd, pw, &rs->sr_text );
229 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
231 rs->sr_err = LDAP_OTHER;
235 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
237 rs->sr_attrs = op->ors_attrs;
238 rs->sr_flags = REP_ENTRY_MODIFIABLE;
239 send_search_entry( op, rs );
246 if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
247 send_ldap_result( op, rs );
249 if( rdn != NULL ) ldap_rdnfree( rdn );
261 #ifdef HAVE_SETPWFILE
262 if ( be->be_private != NULL ) {
263 (void) setpwfile( (char *) be->be_private );
265 #endif /* HAVE_SETPWFILE */
269 pw2entry( Backend *be, struct passwd *pw, const char **text )
273 struct berval vals[2];
278 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
279 AttributeDescription *ad_cn = NULL;
280 AttributeDescription *ad_sn = NULL;
281 AttributeDescription *ad_uid = NULL;
282 AttributeDescription *ad_description = NULL;
284 rc = slap_str2ad( "cn", &ad_cn, text );
285 if(rc != LDAP_SUCCESS) return NULL;
286 rc = slap_str2ad( "sn", &ad_sn, text );
287 if(rc != LDAP_SUCCESS) return NULL;
288 rc = slap_str2ad( "uid", &ad_uid, text );
289 if(rc != LDAP_SUCCESS) return NULL;
290 rc = slap_str2ad( "description", &ad_description, text );
291 if(rc != LDAP_SUCCESS) return NULL;
294 * from pw we get pw_name and make it cn
295 * give it an objectclass of person.
298 pwlen = strlen( pw->pw_name );
299 vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
300 vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
302 /* rdn attribute type should be a configuratable item */
303 sprintf( vals[0].bv_val, "uid=%s,%s",
304 pw->pw_name, be->be_suffix[0].bv_val );
306 rc = dnNormalize( 0, NULL, NULL, vals, &bv, NULL );
307 if( rc != LDAP_SUCCESS ) {
308 free( vals[0].bv_val );
312 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
318 vals[1].bv_val = NULL;
320 /* objectclasses should be configurable items */
321 vals[0].bv_val = "top";
322 vals[0].bv_len = sizeof("top")-1;
323 attr_mergeit( e, ad_objectClass, vals );
325 vals[0].bv_val = "person";
326 vals[0].bv_len = sizeof("person")-1;
327 attr_mergeit( e, ad_objectClass, vals );
329 vals[0].bv_val = "uidObject";
330 vals[0].bv_len = sizeof("uidObject")-1;
331 attr_mergeit( e, ad_objectClass, vals );
333 vals[0].bv_val = pw->pw_name;
334 vals[0].bv_len = pwlen;
335 attr_mergeit( e, ad_uid, vals ); /* required by uidObject */
336 attr_mergeit( e, ad_cn, vals ); /* required by person */
337 attr_mergeit( e, ad_sn, vals ); /* required by person */
341 * if gecos is present, add it as a cn. first process it
342 * according to standard BSD usage. If the processed cn has
343 * a space, use the tail as the surname.
345 if (pw->pw_gecos[0]) {
348 vals[0].bv_val = pw->pw_gecos;
349 vals[0].bv_len = strlen(vals[0].bv_val);
350 attr_mergeit(e, ad_description, vals);
352 s = strchr(vals[0].bv_val, ',');
355 s = strchr(vals[0].bv_val, '&');
359 if( vals[0].bv_len + pwlen < sizeof(buf) ) {
360 int i = s - vals[0].bv_val;
361 strncpy(buf, vals[0].bv_val, i);
363 strcpy(s, pw->pw_name);
364 *s = TOUPPER((unsigned char)*s);
365 strcat(s, vals[0].bv_val+i+1);
366 vals[0].bv_val = buf;
369 vals[0].bv_len = strlen(vals[0].bv_val);
371 if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
372 attr_mergeit( e, ad_cn, vals );
375 if ( (s=strrchr(vals[0].bv_val, ' '))) {
376 vals[0].bv_val = s + 1;
377 vals[0].bv_len = strlen(vals[0].bv_val);
378 attr_mergeit(e, ad_sn, vals);