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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
55 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
57 static char *ad_objectClass = "objectClass";
60 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
62 stoptime = op->o_time + tlimit;
63 slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
69 if ( be->be_private != NULL ) {
70 (void) setpwfile( (char *) be->be_private );
72 #endif /* HAVE_SETPWFILE */
74 /* Handle a query for the base of this backend */
75 if ( be_issuffix( be, nbase ) ) {
76 struct berval val, *vals[2];
81 matched = ch_strdup( base );
83 if( scope != LDAP_SCOPE_ONELEVEL ) {
85 #ifdef SLAPD_SCHEMA_NOT_COMPAT
86 AttributeDescription *desc = NULL;
91 /* Create an entry corresponding to the base DN */
92 e = (Entry *) ch_calloc(1, sizeof(Entry));
94 e->e_dn = ch_strdup( base );
96 /* Use the first attribute of the DN
97 * as an attribute within the entry itself.
99 rdn = dn_rdn(NULL, base);
101 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
102 err = LDAP_INVALID_DN_SYNTAX;
107 val.bv_val = rdn_attr_value(rdn);
108 val.bv_len = strlen( val.bv_val );
110 type = rdn_attr_type(rdn);
112 #ifdef SLAPD_SCHEMA_NOT_COMPAT
116 rc = slap_str2ad( type, &desc, &text );
118 if( rc != LDAP_SUCCESS ) {
119 err = LDAP_NO_SUCH_OBJECT;
128 attr_merge( e, desc, vals );
130 #ifdef SLAPD_SCHEMA_NOT_COMPAT
137 /* Every entry needs an objectclass. We don't really
138 * know if our hardcoded choice here agrees with the
139 * DN that was configured for this backend, but it's
140 * better than nothing.
142 * should be a configuratable item
144 val.bv_val = "organizationalUnit";
145 val.bv_len = sizeof("organizationalUnit")-1;
146 attr_merge( e, ad_objectClass, vals );
148 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
149 send_search_entry( be, conn, op,
150 e, attrs, attrsonly, NULL );
155 if ( scope != LDAP_SCOPE_BASE ) {
156 /* check all our "children" */
158 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
159 /* check for abandon */
160 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
161 if ( op->o_abandon ) {
162 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
166 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
168 /* check time limit */
169 if ( slap_get_time() > stoptime ) {
170 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
171 NULL, NULL, NULL, NULL );
176 e = pw2entry( be, pw, NULL );
178 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
179 /* check size limit */
180 if ( --slimit == -1 ) {
181 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
182 NULL, NULL, NULL, NULL );
187 send_search_entry( be, conn, op,
188 e, attrs, attrsonly, NULL );
198 parent = dn_parent( be, base );
200 /* This backend is only one layer deep. Don't answer requests for
201 * anything deeper than that.
203 if( !be_issuffix( be, parent ) ) {
205 for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
206 if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
207 matched = ch_strdup( be->be_suffix[i] );
211 err = LDAP_NO_SUCH_OBJECT;
215 if( scope == LDAP_SCOPE_ONELEVEL ) {
219 rdn = dn_rdn( NULL, base );
221 if ( (user = rdn_attr_value(rdn)) == NULL) {
222 err = LDAP_OPERATIONS_ERROR;
226 if ( (pw = getpwnam( user )) == NULL ) {
229 err = LDAP_NO_SUCH_OBJECT;
233 e = pw2entry( be, pw, rdn );
235 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
236 send_search_entry( be, conn, op,
237 e, attrs, attrsonly, NULL );
245 send_ldap_result( conn, op,
246 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
249 if( matched != NULL ) free( matched );
250 if( parent != NULL ) free( parent );
251 if( rdn != NULL ) free( rdn );
252 if( user != NULL ) free( user );
258 pw2entry( Backend *be, struct passwd *pw, char *rdn )
263 struct berval *vals[2];
265 #ifdef SLAPD_SCHEMA_NOT_COMPAT
269 AttributeDescription *ad_objectClass = NULL;
270 AttributeDescription *ad_cn = NULL;
271 AttributeDescription *ad_sn = NULL;
272 AttributeDescription *ad_uid = NULL;
273 AttributeDescription *ad_description = NULL;
275 rc = slap_str2ad( "objectClass", &ad_objectClass, &text );
277 if(rc != LDAP_SUCCESS) return NULL;
278 rc = slap_str2ad( "cn", &ad_cn, &text );
279 if(rc != LDAP_SUCCESS) return NULL;
280 rc = slap_str2ad( "sn", &ad_sn, &text );
281 if(rc != LDAP_SUCCESS) return NULL;
282 rc = slap_str2ad( "uid", &ad_uid, &text );
283 if(rc != LDAP_SUCCESS) return NULL;
284 rc = slap_str2ad( "description", &ad_description, &text );
285 if(rc != LDAP_SUCCESS) return NULL;
288 static char *ad_objectClass = "objectClass";
289 static char *ad_cn = "cn";
290 static char *ad_sn = "sn";
291 static char *ad_uid = "uid";
292 static char *ad_description = "description";
299 * from pw we get pw_name and make it cn
300 * give it an objectclass of person.
303 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
306 /* objectclasses should be configuratable items */
308 val.bv_len = sizeof("top")-1;
309 attr_merge( e, ad_objectClass, vals );
311 val.bv_val = "person";
312 val.bv_len = sizeof("person")-1;
313 attr_merge( e, ad_objectClass, vals );
315 val.bv_val = "uidObject";
316 val.bv_len = sizeof("uidObject")-1;
317 attr_merge( e, ad_objectClass, vals );
319 /* rdn attribute type should be a configuratable item */
320 sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
321 e->e_dn = ch_strdup( buf );
322 e->e_ndn = ch_strdup( buf );
323 (void) dn_normalize( e->e_ndn );
325 val.bv_val = pw->pw_name;
326 val.bv_len = strlen( pw->pw_name );
327 attr_merge( e, ad_uid, vals ); /* required by uidObject */
328 attr_merge( e, ad_cn, vals ); /* required by person */
329 attr_merge( e, ad_sn, vals ); /* required by person */
333 * if gecos is present, add it as a cn. first process it
334 * according to standard BSD usage. If the processed cn has
335 * a space, use the tail as the surname.
337 if (pw->pw_gecos[0]) {
340 val.bv_val = pw->pw_gecos;
341 val.bv_len = strlen(val.bv_val);
342 attr_merge(e, ad_description, vals);
344 s = strchr(val.bv_val, ',');
347 s = strchr(val.bv_val, '&');
349 int i = s - val.bv_val;
350 strncpy(buf, val.bv_val, i);
352 strcpy(s, pw->pw_name);
354 strcat(s, val.bv_val+i+1);
357 val.bv_len = strlen(val.bv_val);
358 if ( strcmp( val.bv_val, pw->pw_name ))
359 attr_merge( e, ad_cn, vals );
360 if ( (s=strrchr(val.bv_val, ' '))) {
362 val.bv_len = strlen(val.bv_val);
363 attr_merge(e, ad_sn, vals);