1 /* search.c - /etc/passwd backend search function */
10 #include <ac/string.h>
19 static Entry *pw2entry(
36 const char *filterstr,
47 int err = LDAP_SUCCESS;
54 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
56 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
58 stoptime = op->o_time + tlimit;
59 slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
65 if ( be->be_private != NULL ) {
66 (void) setpwfile( (char *) be->be_private );
68 #endif /* HAVE_SETPWFILE */
70 /* Handle a query for the base of this backend */
71 if ( be_issuffix( be, nbase ) ) {
72 struct berval val, *vals[2];
77 matched = ch_strdup( base );
79 if( scope != LDAP_SCOPE_ONELEVEL ) {
81 AttributeDescription *desc = NULL;
83 /* Create an entry corresponding to the base DN */
84 e = (Entry *) ch_calloc(1, sizeof(Entry));
86 e->e_dn = ch_strdup( base );
88 /* Use the first attribute of the DN
89 * as an attribute within the entry itself.
91 rdn = dn_rdn(NULL, base);
93 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
94 err = LDAP_INVALID_DN_SYNTAX;
99 val.bv_val = rdn_attr_value(rdn);
100 val.bv_len = strlen( val.bv_val );
102 type = rdn_attr_type(rdn);
107 rc = slap_str2ad( type, &desc, &text );
109 if( rc != LDAP_SUCCESS ) {
110 err = LDAP_NO_SUCH_OBJECT;
116 attr_merge( e, desc, vals );
123 /* Every entry needs an objectclass. We don't really
124 * know if our hardcoded choice here agrees with the
125 * DN that was configured for this backend, but it's
126 * better than nothing.
128 * should be a configuratable item
130 val.bv_val = "organizationalUnit";
131 val.bv_len = sizeof("organizationalUnit")-1;
132 attr_merge( e, ad_objectClass, vals );
134 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
135 send_search_entry( be, conn, op,
136 e, attrs, attrsonly, NULL );
141 if ( scope != LDAP_SCOPE_BASE ) {
142 /* check all our "children" */
144 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
145 /* check for abandon */
146 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
147 if ( op->o_abandon ) {
148 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
152 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
154 /* check time limit */
155 if ( slap_get_time() > stoptime ) {
156 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
157 NULL, NULL, NULL, NULL );
162 e = pw2entry( be, pw, NULL );
164 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
165 /* check size limit */
166 if ( --slimit == -1 ) {
167 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
168 NULL, NULL, NULL, NULL );
173 send_search_entry( be, conn, op,
174 e, attrs, attrsonly, NULL );
184 parent = dn_parent( be, base );
186 /* This backend is only one layer deep. Don't answer requests for
187 * anything deeper than that.
189 if( !be_issuffix( be, parent ) ) {
191 for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
192 if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
193 matched = ch_strdup( be->be_suffix[i] );
197 err = LDAP_NO_SUCH_OBJECT;
201 if( scope == LDAP_SCOPE_ONELEVEL ) {
205 rdn = dn_rdn( NULL, base );
207 if ( (user = rdn_attr_value(rdn)) == NULL) {
208 err = LDAP_OPERATIONS_ERROR;
212 if ( (pw = getpwnam( user )) == NULL ) {
215 err = LDAP_NO_SUCH_OBJECT;
219 e = pw2entry( be, pw, rdn );
221 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
222 send_search_entry( be, conn, op,
223 e, attrs, attrsonly, NULL );
231 send_ldap_result( conn, op,
232 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
235 if( matched != NULL ) free( matched );
236 if( parent != NULL ) free( parent );
237 if( rdn != NULL ) free( rdn );
238 if( user != NULL ) free( user );
244 pw2entry( Backend *be, struct passwd *pw, char *rdn )
249 struct berval *vals[2];
254 AttributeDescription *ad_objectClass = NULL;
255 AttributeDescription *ad_cn = NULL;
256 AttributeDescription *ad_sn = NULL;
257 AttributeDescription *ad_uid = NULL;
258 AttributeDescription *ad_description = NULL;
260 rc = slap_str2ad( "objectClass", &ad_objectClass, &text );
262 if(rc != LDAP_SUCCESS) return NULL;
263 rc = slap_str2ad( "cn", &ad_cn, &text );
264 if(rc != LDAP_SUCCESS) return NULL;
265 rc = slap_str2ad( "sn", &ad_sn, &text );
266 if(rc != LDAP_SUCCESS) return NULL;
267 rc = slap_str2ad( "uid", &ad_uid, &text );
268 if(rc != LDAP_SUCCESS) return NULL;
269 rc = slap_str2ad( "description", &ad_description, &text );
270 if(rc != LDAP_SUCCESS) return NULL;
277 * from pw we get pw_name and make it cn
278 * give it an objectclass of person.
281 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
284 /* objectclasses should be configuratable items */
286 val.bv_len = sizeof("top")-1;
287 attr_merge( e, ad_objectClass, vals );
289 val.bv_val = "person";
290 val.bv_len = sizeof("person")-1;
291 attr_merge( e, ad_objectClass, vals );
293 val.bv_val = "uidObject";
294 val.bv_len = sizeof("uidObject")-1;
295 attr_merge( e, ad_objectClass, vals );
297 /* rdn attribute type should be a configuratable item */
298 sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
299 e->e_dn = ch_strdup( buf );
300 e->e_ndn = ch_strdup( buf );
301 (void) dn_normalize( e->e_ndn );
303 val.bv_val = pw->pw_name;
304 val.bv_len = strlen( pw->pw_name );
305 attr_merge( e, ad_uid, vals ); /* required by uidObject */
306 attr_merge( e, ad_cn, vals ); /* required by person */
307 attr_merge( e, ad_sn, vals ); /* required by person */
311 * if gecos is present, add it as a cn. first process it
312 * according to standard BSD usage. If the processed cn has
313 * a space, use the tail as the surname.
315 if (pw->pw_gecos[0]) {
318 val.bv_val = pw->pw_gecos;
319 val.bv_len = strlen(val.bv_val);
320 attr_merge(e, ad_description, vals);
322 s = strchr(val.bv_val, ',');
325 s = strchr(val.bv_val, '&');
327 int i = s - val.bv_val;
328 strncpy(buf, val.bv_val, i);
330 strcpy(s, pw->pw_name);
332 strcat(s, val.bv_val+i+1);
335 val.bv_len = strlen(val.bv_val);
336 if ( strcmp( val.bv_val, pw->pw_name ))
337 attr_merge( e, ad_cn, vals );
338 if ( (s=strrchr(val.bv_val, ' '))) {
340 val.bv_len = strlen(val.bv_val);
341 attr_merge(e, ad_sn, vals);