1 /* search.c - /etc/passwd backend search function */
10 #include <ac/string.h>
16 #include "back-passwd.h"
19 static void pw_start( Backend *be );
21 static Entry *pw2entry(
37 struct berval parent = { 0, NULL };
39 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
41 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
42 : op->oq_search.rs_tlimit;
43 stoptime = op->o_time + op->oq_search.rs_tlimit;
44 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
45 : op->oq_search.rs_slimit;
47 /* Handle a query for the base of this backend */
48 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
49 struct berval vals[2];
51 vals[1].bv_val = NULL;
53 rs->sr_matched = op->o_req_dn.bv_val;
55 if( op->oq_search.rs_scope != LDAP_SCOPE_ONELEVEL ) {
56 AttributeDescription *desc = NULL;
58 /* Create an entry corresponding to the base DN */
59 e = (Entry *) ch_calloc(1, sizeof(Entry));
60 e->e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
61 e->e_name.bv_len = op->o_req_dn.bv_len;
62 e->e_nname.bv_val = ch_strdup( op->o_req_ndn.bv_val );
63 e->e_nname.bv_len = op->o_req_ndn.bv_len;
67 /* Use the first attribute of the DN
68 * as an attribute within the entry itself.
70 if( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
71 LDAP_DN_FORMAT_LDAP ) )
73 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
77 if( slap_bv2ad( &rdn[0][0]->la_attr, &desc, &rs->sr_text )) {
78 rs->sr_err = LDAP_NO_SUCH_OBJECT;
83 vals[0] = rdn[0][0]->la_value;
84 attr_mergeit( e, desc, vals );
89 /* Every entry needs an objectclass. We don't really
90 * know if our hardcoded choice here agrees with the
91 * DN that was configured for this backend, but it's
92 * better than nothing.
94 * should be a configuratable item
96 vals[0].bv_val = "organizationalUnit";
97 vals[0].bv_len = sizeof("organizationalUnit")-1;
98 attr_mergeit( e, ad_objectClass, vals );
100 if ( test_filter( op, e, op->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
102 rs->sr_attrs = op->oq_search.rs_attrs;
103 send_search_entry( op, rs );
107 if ( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
108 /* check all our "children" */
110 ldap_pvt_thread_mutex_lock( &passwd_mutex );
111 pw_start( op->o_bd );
112 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
113 /* check for abandon */
114 if ( op->o_abandon ) {
116 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
120 /* check time limit */
121 if ( slap_get_time() > stoptime ) {
122 send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
124 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
128 if ( !(e = pw2entry( op->o_bd, pw, &rs->sr_text )) ) {
129 rs->sr_err = LDAP_OTHER;
131 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
135 if ( test_filter( op, e, op->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
136 /* check size limit */
137 if ( --op->oq_search.rs_slimit == -1 ) {
138 send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
140 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
145 rs->sr_attrs = op->oq_search.rs_attrs;
146 send_search_entry( op, rs );
152 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
156 if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
157 dnParent( &op->o_req_ndn, &parent );
160 /* This backend is only one layer deep. Don't answer requests for
161 * anything deeper than that.
163 if( !be_issuffix( op->o_bd, &parent ) ) {
165 for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
166 if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
167 rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
171 rs->sr_err = LDAP_NO_SUCH_OBJECT;
175 if( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
179 if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
180 LDAP_DN_FORMAT_LDAP ))
182 rs->sr_err = LDAP_OTHER;
186 ldap_pvt_thread_mutex_lock( &passwd_mutex );
187 pw_start( op->o_bd );
188 if ( (pw = getpwnam( rdn[0][0]->la_value.bv_val )) == NULL ) {
189 rs->sr_matched = parent.bv_val;
190 rs->sr_err = LDAP_NO_SUCH_OBJECT;
191 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
195 e = pw2entry( op->o_bd, pw, &rs->sr_text );
196 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
198 rs->sr_err = LDAP_OTHER;
202 if ( test_filter( op, e, op->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
204 rs->sr_attrs = op->oq_search.rs_attrs;
205 send_search_entry( op, rs );
212 if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
213 send_ldap_result( op, rs );
215 if( rdn != NULL ) ldap_rdnfree( rdn );
227 #ifdef HAVE_SETPWFILE
228 if ( be->be_private != NULL ) {
229 (void) setpwfile( (char *) be->be_private );
231 #endif /* HAVE_SETPWFILE */
235 pw2entry( Backend *be, struct passwd *pw, const char **text )
239 struct berval vals[2];
244 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
245 AttributeDescription *ad_cn = NULL;
246 AttributeDescription *ad_sn = NULL;
247 AttributeDescription *ad_uid = NULL;
248 AttributeDescription *ad_description = NULL;
250 rc = slap_str2ad( "cn", &ad_cn, text );
251 if(rc != LDAP_SUCCESS) return NULL;
252 rc = slap_str2ad( "sn", &ad_sn, text );
253 if(rc != LDAP_SUCCESS) return NULL;
254 rc = slap_str2ad( "uid", &ad_uid, text );
255 if(rc != LDAP_SUCCESS) return NULL;
256 rc = slap_str2ad( "description", &ad_description, text );
257 if(rc != LDAP_SUCCESS) return NULL;
260 * from pw we get pw_name and make it cn
261 * give it an objectclass of person.
264 pwlen = strlen( pw->pw_name );
265 vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
266 vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
268 /* rdn attribute type should be a configuratable item */
269 sprintf( vals[0].bv_val, "uid=%s,%s",
270 pw->pw_name, be->be_suffix[0].bv_val );
272 rc = dnNormalize2( NULL, vals, &bv );
273 if( rc != LDAP_SUCCESS ) {
274 free( vals[0].bv_val );
278 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
284 vals[1].bv_val = NULL;
286 /* objectclasses should be configurable items */
287 vals[0].bv_val = "top";
288 vals[0].bv_len = sizeof("top")-1;
289 attr_mergeit( e, ad_objectClass, vals );
291 vals[0].bv_val = "person";
292 vals[0].bv_len = sizeof("person")-1;
293 attr_mergeit( e, ad_objectClass, vals );
295 vals[0].bv_val = "uidObject";
296 vals[0].bv_len = sizeof("uidObject")-1;
297 attr_mergeit( e, ad_objectClass, vals );
299 vals[0].bv_val = pw->pw_name;
300 vals[0].bv_len = pwlen;
301 attr_mergeit( e, ad_uid, vals ); /* required by uidObject */
302 attr_mergeit( e, ad_cn, vals ); /* required by person */
303 attr_mergeit( e, ad_sn, vals ); /* required by person */
307 * if gecos is present, add it as a cn. first process it
308 * according to standard BSD usage. If the processed cn has
309 * a space, use the tail as the surname.
311 if (pw->pw_gecos[0]) {
314 vals[0].bv_val = pw->pw_gecos;
315 vals[0].bv_len = strlen(vals[0].bv_val);
316 attr_mergeit(e, ad_description, vals);
318 s = strchr(vals[0].bv_val, ',');
321 s = strchr(vals[0].bv_val, '&');
325 if( vals[0].bv_len + pwlen < sizeof(buf) ) {
326 int i = s - vals[0].bv_val;
327 strncpy(buf, vals[0].bv_val, i);
329 strcpy(s, pw->pw_name);
330 *s = TOUPPER((unsigned char)*s);
331 strcat(s, vals[0].bv_val+i+1);
332 vals[0].bv_val = buf;
335 vals[0].bv_len = strlen(vals[0].bv_val);
337 if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
338 attr_mergeit( e, ad_cn, vals );
341 if ( (s=strrchr(vals[0].bv_val, ' '))) {
342 vals[0].bv_val = s + 1;
343 vals[0].bv_len = strlen(vals[0].bv_val);
344 attr_mergeit(e, ad_sn, vals);