1 /* search.c - /etc/passwd backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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"
49 static void pw_start( Backend *be );
51 static Entry *pw2entry(
67 struct berval parent = BER_BVNULL;
69 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
71 if (op->ors_tlimit != SLAP_NO_LIMIT ) {
72 stoptime = op->o_time + op->ors_tlimit;
75 /* Handle a query for the base of this backend */
76 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
77 struct berval vals[2];
79 vals[1].bv_val = NULL;
81 rs->sr_matched = op->o_req_dn.bv_val;
83 if( op->ors_scope != LDAP_SCOPE_ONELEVEL ) {
84 AttributeDescription *desc = NULL;
86 /* Create an entry corresponding to the base DN */
87 e = (Entry *) ch_calloc(1, sizeof(Entry));
88 e->e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
89 e->e_name.bv_len = op->o_req_dn.bv_len;
90 e->e_nname.bv_val = ch_strdup( op->o_req_ndn.bv_val );
91 e->e_nname.bv_len = op->o_req_ndn.bv_len;
95 /* Use the first attribute of the DN
96 * as an attribute within the entry itself.
98 if( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
99 LDAP_DN_FORMAT_LDAP ) )
101 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
105 if( slap_bv2ad( &rdn[0]->la_attr, &desc, &rs->sr_text )) {
106 rs->sr_err = LDAP_NO_SUCH_OBJECT;
111 vals[0] = rdn[0]->la_value;
112 attr_mergeit( e, desc, vals );
117 /* Every entry needs an objectclass. We don't really
118 * know if our hardcoded choice here agrees with the
119 * DN that was configured for this backend, but it's
120 * better than nothing.
122 * should be a configuratable item
124 vals[0].bv_val = "organizationalUnit";
125 vals[0].bv_len = sizeof("organizationalUnit")-1;
126 attr_mergeit( e, ad_objectClass, vals );
128 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
130 rs->sr_attrs = op->ors_attrs;
131 rs->sr_flags = REP_ENTRY_MODIFIABLE;
132 send_search_entry( op, rs );
136 if ( op->ors_scope != LDAP_SCOPE_BASE ) {
137 /* check all our "children" */
139 ldap_pvt_thread_mutex_lock( &passwd_mutex );
140 pw_start( op->o_bd );
141 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
142 /* check for abandon */
143 if ( op->o_abandon ) {
145 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
149 /* check time limit */
150 if ( op->ors_tlimit != SLAP_NO_LIMIT
151 && slap_get_time() > stoptime )
153 send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
155 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
159 if ( !(e = pw2entry( op->o_bd, pw, &rs->sr_text )) ) {
160 rs->sr_err = LDAP_OTHER;
162 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
166 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
167 /* check size limit */
168 if ( --op->ors_slimit == -1 ) {
169 send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
171 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
176 rs->sr_attrs = op->ors_attrs;
177 rs->sr_flags = REP_ENTRY_MODIFIABLE;
178 send_search_entry( op, rs );
184 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
188 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
189 dnParent( &op->o_req_ndn, &parent );
192 /* This backend is only one layer deep. Don't answer requests for
193 * anything deeper than that.
195 if( !be_issuffix( op->o_bd, &parent ) ) {
197 for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
198 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
199 rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
203 rs->sr_err = LDAP_NO_SUCH_OBJECT;
207 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
211 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
212 LDAP_DN_FORMAT_LDAP ))
214 rs->sr_err = LDAP_OTHER;
218 ldap_pvt_thread_mutex_lock( &passwd_mutex );
219 pw_start( op->o_bd );
220 if ( (pw = getpwnam( rdn[0]->la_value.bv_val )) == NULL ) {
221 rs->sr_matched = parent.bv_val;
222 rs->sr_err = LDAP_NO_SUCH_OBJECT;
223 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
227 e = pw2entry( op->o_bd, pw, &rs->sr_text );
228 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
230 rs->sr_err = LDAP_OTHER;
234 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
236 rs->sr_attrs = op->ors_attrs;
237 rs->sr_flags = REP_ENTRY_MODIFIABLE;
238 send_search_entry( op, rs );
245 if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
246 send_ldap_result( op, rs );
248 if( rdn != NULL ) ldap_rdnfree( rdn );
260 #ifdef HAVE_SETPWFILE
261 if ( be->be_private != NULL ) {
262 (void) setpwfile( (char *) be->be_private );
264 #endif /* HAVE_SETPWFILE */
268 pw2entry( Backend *be, struct passwd *pw, const char **text )
272 struct berval vals[2];
277 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
278 AttributeDescription *ad_cn = NULL;
279 AttributeDescription *ad_sn = NULL;
280 AttributeDescription *ad_uid = NULL;
281 AttributeDescription *ad_description = NULL;
283 rc = slap_str2ad( "cn", &ad_cn, text );
284 if(rc != LDAP_SUCCESS) return NULL;
285 rc = slap_str2ad( "sn", &ad_sn, text );
286 if(rc != LDAP_SUCCESS) return NULL;
287 rc = slap_str2ad( "uid", &ad_uid, text );
288 if(rc != LDAP_SUCCESS) return NULL;
289 rc = slap_str2ad( "description", &ad_description, text );
290 if(rc != LDAP_SUCCESS) return NULL;
293 * from pw we get pw_name and make it cn
294 * give it an objectclass of person.
297 pwlen = strlen( pw->pw_name );
298 vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
299 vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
301 /* rdn attribute type should be a configuratable item */
302 sprintf( vals[0].bv_val, "uid=%s,%s",
303 pw->pw_name, be->be_suffix[0].bv_val );
305 rc = dnNormalize( 0, NULL, NULL, vals, &bv, NULL );
306 if( rc != LDAP_SUCCESS ) {
307 free( vals[0].bv_val );
311 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
317 vals[1].bv_val = NULL;
319 /* objectclasses should be configurable items */
320 vals[0].bv_val = "top";
321 vals[0].bv_len = sizeof("top")-1;
322 attr_mergeit( e, ad_objectClass, vals );
324 vals[0].bv_val = "person";
325 vals[0].bv_len = sizeof("person")-1;
326 attr_mergeit( e, ad_objectClass, vals );
328 vals[0].bv_val = "uidObject";
329 vals[0].bv_len = sizeof("uidObject")-1;
330 attr_mergeit( e, ad_objectClass, vals );
332 vals[0].bv_val = pw->pw_name;
333 vals[0].bv_len = pwlen;
334 attr_mergeit( e, ad_uid, vals ); /* required by uidObject */
335 attr_mergeit( e, ad_cn, vals ); /* required by person */
336 attr_mergeit( e, ad_sn, vals ); /* required by person */
340 * if gecos is present, add it as a cn. first process it
341 * according to standard BSD usage. If the processed cn has
342 * a space, use the tail as the surname.
344 if (pw->pw_gecos[0]) {
347 vals[0].bv_val = pw->pw_gecos;
348 vals[0].bv_len = strlen(vals[0].bv_val);
349 attr_mergeit(e, ad_description, vals);
351 s = strchr(vals[0].bv_val, ',');
354 s = strchr(vals[0].bv_val, '&');
358 if( vals[0].bv_len + pwlen < sizeof(buf) ) {
359 int i = s - vals[0].bv_val;
360 strncpy(buf, vals[0].bv_val, i);
362 strcpy(s, pw->pw_name);
363 *s = TOUPPER((unsigned char)*s);
364 strcat(s, vals[0].bv_val+i+1);
365 vals[0].bv_val = buf;
368 vals[0].bv_len = strlen(vals[0].bv_val);
370 if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
371 attr_mergeit( e, ad_cn, vals );
374 if ( (s=strrchr(vals[0].bv_val, ' '))) {
375 vals[0].bv_val = s + 1;
376 vals[0].bv_len = strlen(vals[0].bv_val);
377 attr_mergeit(e, ad_sn, vals);