1 /* search.c - /etc/passwd backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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->oq_search.rs_tlimit = (op->oq_search.rs_tlimit > op->o_bd->be_timelimit || op->oq_search.rs_tlimit < 1) ? op->o_bd->be_timelimit
73 : op->oq_search.rs_tlimit;
74 stoptime = op->o_time + op->oq_search.rs_tlimit;
75 op->oq_search.rs_slimit = (op->oq_search.rs_slimit > op->o_bd->be_sizelimit || op->oq_search.rs_slimit < 1) ? op->o_bd->be_sizelimit
76 : op->oq_search.rs_slimit;
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->oq_search.rs_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->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
133 rs->sr_attrs = op->oq_search.rs_attrs;
134 send_search_entry( op, rs );
138 if ( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
139 /* check all our "children" */
141 ldap_pvt_thread_mutex_lock( &passwd_mutex );
142 pw_start( op->o_bd );
143 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
144 /* check for abandon */
145 if ( op->o_abandon ) {
147 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
151 /* check time limit */
152 if ( 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->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
167 /* check size limit */
168 if ( --op->oq_search.rs_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->oq_search.rs_attrs;
177 send_search_entry( op, rs );
183 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
187 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
188 dnParent( &op->o_req_ndn, &parent );
191 /* This backend is only one layer deep. Don't answer requests for
192 * anything deeper than that.
194 if( !be_issuffix( op->o_bd, &parent ) ) {
196 for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
197 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
198 rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
202 rs->sr_err = LDAP_NO_SUCH_OBJECT;
206 if( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
210 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
211 LDAP_DN_FORMAT_LDAP ))
213 rs->sr_err = LDAP_OTHER;
217 ldap_pvt_thread_mutex_lock( &passwd_mutex );
218 pw_start( op->o_bd );
219 if ( (pw = getpwnam( rdn[0]->la_value.bv_val )) == NULL ) {
220 rs->sr_matched = parent.bv_val;
221 rs->sr_err = LDAP_NO_SUCH_OBJECT;
222 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
226 e = pw2entry( op->o_bd, pw, &rs->sr_text );
227 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
229 rs->sr_err = LDAP_OTHER;
233 if ( test_filter( op, e, op->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
235 rs->sr_attrs = op->oq_search.rs_attrs;
236 send_search_entry( op, rs );
243 if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
244 send_ldap_result( op, rs );
246 if( rdn != NULL ) ldap_rdnfree( rdn );
258 #ifdef HAVE_SETPWFILE
259 if ( be->be_private != NULL ) {
260 (void) setpwfile( (char *) be->be_private );
262 #endif /* HAVE_SETPWFILE */
266 pw2entry( Backend *be, struct passwd *pw, const char **text )
270 struct berval vals[2];
275 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
276 AttributeDescription *ad_cn = NULL;
277 AttributeDescription *ad_sn = NULL;
278 AttributeDescription *ad_uid = NULL;
279 AttributeDescription *ad_description = NULL;
281 rc = slap_str2ad( "cn", &ad_cn, text );
282 if(rc != LDAP_SUCCESS) return NULL;
283 rc = slap_str2ad( "sn", &ad_sn, text );
284 if(rc != LDAP_SUCCESS) return NULL;
285 rc = slap_str2ad( "uid", &ad_uid, text );
286 if(rc != LDAP_SUCCESS) return NULL;
287 rc = slap_str2ad( "description", &ad_description, text );
288 if(rc != LDAP_SUCCESS) return NULL;
291 * from pw we get pw_name and make it cn
292 * give it an objectclass of person.
295 pwlen = strlen( pw->pw_name );
296 vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
297 vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
299 /* rdn attribute type should be a configuratable item */
300 sprintf( vals[0].bv_val, "uid=%s,%s",
301 pw->pw_name, be->be_suffix[0].bv_val );
303 rc = dnNormalize( 0, NULL, NULL, vals, &bv, NULL );
304 if( rc != LDAP_SUCCESS ) {
305 free( vals[0].bv_val );
309 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
315 vals[1].bv_val = NULL;
317 /* objectclasses should be configurable items */
318 vals[0].bv_val = "top";
319 vals[0].bv_len = sizeof("top")-1;
320 attr_mergeit( e, ad_objectClass, vals );
322 vals[0].bv_val = "person";
323 vals[0].bv_len = sizeof("person")-1;
324 attr_mergeit( e, ad_objectClass, vals );
326 vals[0].bv_val = "uidObject";
327 vals[0].bv_len = sizeof("uidObject")-1;
328 attr_mergeit( e, ad_objectClass, vals );
330 vals[0].bv_val = pw->pw_name;
331 vals[0].bv_len = pwlen;
332 attr_mergeit( e, ad_uid, vals ); /* required by uidObject */
333 attr_mergeit( e, ad_cn, vals ); /* required by person */
334 attr_mergeit( e, ad_sn, vals ); /* required by person */
338 * if gecos is present, add it as a cn. first process it
339 * according to standard BSD usage. If the processed cn has
340 * a space, use the tail as the surname.
342 if (pw->pw_gecos[0]) {
345 vals[0].bv_val = pw->pw_gecos;
346 vals[0].bv_len = strlen(vals[0].bv_val);
347 attr_mergeit(e, ad_description, vals);
349 s = strchr(vals[0].bv_val, ',');
352 s = strchr(vals[0].bv_val, '&');
356 if( vals[0].bv_len + pwlen < sizeof(buf) ) {
357 int i = s - vals[0].bv_val;
358 strncpy(buf, vals[0].bv_val, i);
360 strcpy(s, pw->pw_name);
361 *s = TOUPPER((unsigned char)*s);
362 strcat(s, vals[0].bv_val+i+1);
363 vals[0].bv_val = buf;
366 vals[0].bv_len = strlen(vals[0].bv_val);
368 if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
369 attr_mergeit( e, ad_cn, vals );
372 if ( (s=strrchr(vals[0].bv_val, ' '))) {
373 vals[0].bv_val = s + 1;
374 vals[0].bv_len = strlen(vals[0].bv_val);
375 attr_mergeit(e, ad_sn, vals);