1 /* search.c - /etc/passwd backend search function */
10 #include <ac/string.h>
19 static Entry *pw2entry(
36 const char *filterstr,
37 struct berval **attrs,
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 = (char *) 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 e->e_ndn = ch_strdup( e->e_dn );
89 (void) dn_normalize( e->e_ndn );
91 /* Use the first attribute of the DN
92 * as an attribute within the entry itself.
94 rdn = dn_rdn(NULL, base);
96 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
97 err = LDAP_INVALID_DN_SYNTAX;
102 val.bv_val = rdn_attr_value(rdn);
103 val.bv_len = strlen( val.bv_val );
105 type = rdn_attr_type(rdn);
110 rc = slap_str2ad( type, &desc, &text );
112 if( rc != LDAP_SUCCESS ) {
113 err = LDAP_NO_SUCH_OBJECT;
119 attr_merge( e, desc, vals );
124 /* Every entry needs an objectclass. We don't really
125 * know if our hardcoded choice here agrees with the
126 * DN that was configured for this backend, but it's
127 * better than nothing.
129 * should be a configuratable item
131 val.bv_val = "organizationalUnit";
132 val.bv_len = sizeof("organizationalUnit")-1;
133 attr_merge( e, ad_objectClass, vals );
135 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
136 send_search_entry( be, conn, op,
137 e, attrs, attrsonly, NULL );
142 if ( scope != LDAP_SCOPE_BASE ) {
143 /* check all our "children" */
145 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
146 /* check for abandon */
147 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
148 if ( op->o_abandon ) {
149 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
153 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
155 /* check time limit */
156 if ( slap_get_time() > stoptime ) {
157 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
158 NULL, NULL, NULL, NULL );
163 e = pw2entry( be, pw, NULL );
165 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
166 /* check size limit */
167 if ( --slimit == -1 ) {
168 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
169 NULL, NULL, NULL, NULL );
174 send_search_entry( be, conn, op,
175 e, attrs, attrsonly, NULL );
185 parent = dn_parent( be, nbase );
187 /* This backend is only one layer deep. Don't answer requests for
188 * anything deeper than that.
190 if( !be_issuffix( be, parent ) ) {
192 for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
193 if( dn_issuffix( nbase, be->be_nsuffix[i]->bv_val ) ) {
194 matched = be->be_suffix[i]->bv_val;
198 err = LDAP_NO_SUCH_OBJECT;
202 if( scope == LDAP_SCOPE_ONELEVEL ) {
206 rdn = dn_rdn( NULL, base );
208 if ( (user = rdn_attr_value(rdn)) == NULL) {
209 err = LDAP_OPERATIONS_ERROR;
213 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( rdn != NULL ) free( rdn );
236 if( user != NULL ) free( user );
242 pw2entry( Backend *be, struct passwd *pw, char *rdn )
247 struct berval *vals[2];
252 AttributeDescription *ad_objectClass = NULL;
253 AttributeDescription *ad_cn = NULL;
254 AttributeDescription *ad_sn = NULL;
255 AttributeDescription *ad_uid = NULL;
256 AttributeDescription *ad_description = NULL;
258 rc = slap_str2ad( "objectClass", &ad_objectClass, &text );
260 if(rc != LDAP_SUCCESS) return NULL;
261 rc = slap_str2ad( "cn", &ad_cn, &text );
262 if(rc != LDAP_SUCCESS) return NULL;
263 rc = slap_str2ad( "sn", &ad_sn, &text );
264 if(rc != LDAP_SUCCESS) return NULL;
265 rc = slap_str2ad( "uid", &ad_uid, &text );
266 if(rc != LDAP_SUCCESS) return NULL;
267 rc = slap_str2ad( "description", &ad_description, &text );
268 if(rc != LDAP_SUCCESS) return NULL;
275 * from pw we get pw_name and make it cn
276 * give it an objectclass of person.
279 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
282 /* objectclasses should be configuratable items */
284 val.bv_len = sizeof("top")-1;
285 attr_merge( e, ad_objectClass, vals );
287 val.bv_val = "person";
288 val.bv_len = sizeof("person")-1;
289 attr_merge( e, ad_objectClass, vals );
291 val.bv_val = "uidObject";
292 val.bv_len = sizeof("uidObject")-1;
293 attr_merge( e, ad_objectClass, vals );
295 /* rdn attribute type should be a configuratable item */
296 sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0]->bv_val );
297 e->e_name.bv_val = ch_strdup( buf );
298 e->e_name.bv_len = strlen( e->e_name.bv_val );
300 /* FIXME: use dnNormalize() !! */
301 e->e_nname.bv_val = ch_strdup( buf );
302 (void) dn_normalize( e->e_nname.bv_val );
303 e->e_nname.bv_len = strlen( e->e_name.bv_val );
305 val.bv_val = pw->pw_name;
306 val.bv_len = strlen( pw->pw_name );
307 attr_merge( e, ad_uid, vals ); /* required by uidObject */
308 attr_merge( e, ad_cn, vals ); /* required by person */
309 attr_merge( e, ad_sn, vals ); /* required by person */
313 * if gecos is present, add it as a cn. first process it
314 * according to standard BSD usage. If the processed cn has
315 * a space, use the tail as the surname.
317 if (pw->pw_gecos[0]) {
320 val.bv_val = pw->pw_gecos;
321 val.bv_len = strlen(val.bv_val);
322 attr_merge(e, ad_description, vals);
324 s = strchr(val.bv_val, ',');
327 s = strchr(val.bv_val, '&');
329 int i = s - val.bv_val;
330 strncpy(buf, val.bv_val, i);
332 strcpy(s, pw->pw_name);
334 strcat(s, val.bv_val+i+1);
337 val.bv_len = strlen(val.bv_val);
338 if ( strcmp( val.bv_val, pw->pw_name ))
339 attr_merge( e, ad_cn, vals );
340 if ( (s=strrchr(val.bv_val, ' '))) {
342 val.bv_len = strlen(val.bv_val);
343 attr_merge(e, ad_sn, vals);