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->bv_val ) ) {
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));
85 e->e_name.bv_val = ch_strdup( base->bv_val );
86 e->e_name.bv_len = base->bv_len;
87 e->e_nname.bv_val = ch_strdup( nbase->bv_val );
88 e->e_nname.bv_len = nbase->bv_len;
92 /* Use the first attribute of the DN
93 * as an attribute within the entry itself.
95 rdn = dn_rdn(NULL, base->bv_val);
97 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
98 err = LDAP_INVALID_DN_SYNTAX;
103 val.bv_val = rdn_attr_value(rdn);
104 val.bv_len = strlen( val.bv_val );
106 type = rdn_attr_type(rdn);
111 rc = slap_str2ad( type, &desc, &text );
113 if( rc != LDAP_SUCCESS ) {
114 err = LDAP_NO_SUCH_OBJECT;
120 attr_merge( e, desc, vals );
125 /* Every entry needs an objectclass. We don't really
126 * know if our hardcoded choice here agrees with the
127 * DN that was configured for this backend, but it's
128 * better than nothing.
130 * should be a configuratable item
132 val.bv_val = "organizationalUnit";
133 val.bv_len = sizeof("organizationalUnit")-1;
134 attr_merge( e, ad_objectClass, vals );
136 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
137 send_search_entry( be, conn, op,
138 e, attrs, attrsonly, NULL );
143 if ( scope != LDAP_SCOPE_BASE ) {
144 /* check all our "children" */
146 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
147 /* check for abandon */
148 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
149 if ( op->o_abandon ) {
150 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
154 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
156 /* check time limit */
157 if ( slap_get_time() > stoptime ) {
158 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
159 NULL, NULL, NULL, NULL );
164 e = pw2entry( be, pw, NULL );
166 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
167 /* check size limit */
168 if ( --slimit == -1 ) {
169 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
170 NULL, NULL, NULL, NULL );
175 send_search_entry( be, conn, op,
176 e, attrs, attrsonly, NULL );
186 parent = dn_parent( be, nbase->bv_val );
188 /* This backend is only one layer deep. Don't answer requests for
189 * anything deeper than that.
191 if( !be_issuffix( be, parent ) ) {
193 for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
194 if( dnIsSuffix( nbase, be->be_nsuffix[i] ) ) {
195 matched = be->be_suffix[i]->bv_val;
199 err = LDAP_NO_SUCH_OBJECT;
203 if( scope == LDAP_SCOPE_ONELEVEL ) {
207 rdn = dn_rdn( NULL, base->bv_val );
209 if ( (user = rdn_attr_value(rdn)) == NULL) {
210 err = LDAP_OPERATIONS_ERROR;
214 if ( (pw = getpwnam( user )) == NULL ) {
216 err = LDAP_NO_SUCH_OBJECT;
220 e = pw2entry( be, pw, rdn );
222 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
223 send_search_entry( be, conn, op,
224 e, attrs, attrsonly, NULL );
232 send_ldap_result( conn, op,
233 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
236 if( rdn != NULL ) free( rdn );
237 if( user != NULL ) free( user );
243 pw2entry( Backend *be, struct passwd *pw, char *rdn )
248 struct berval *vals[2];
249 struct berval *bv = NULL;
254 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
255 AttributeDescription *ad_cn = NULL;
256 AttributeDescription *ad_sn = NULL;
257 AttributeDescription *ad_uid = NULL;
258 AttributeDescription *ad_description = NULL;
260 rc = slap_str2ad( "cn", &ad_cn, &text );
261 if(rc != LDAP_SUCCESS) return NULL;
262 rc = slap_str2ad( "sn", &ad_sn, &text );
263 if(rc != LDAP_SUCCESS) return NULL;
264 rc = slap_str2ad( "uid", &ad_uid, &text );
265 if(rc != LDAP_SUCCESS) return NULL;
266 rc = slap_str2ad( "description", &ad_description, &text );
267 if(rc != LDAP_SUCCESS) return NULL;
270 * from pw we get pw_name and make it cn
271 * give it an objectclass of person.
274 pwlen = strlen( pw->pw_name );
275 val.bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0]->bv_len );
276 val.bv_val = ch_malloc( val.bv_len + 1 );
278 /* rdn attribute type should be a configuratable item */
279 sprintf( val.bv_val, "uid=%s,%s",
280 pw->pw_name, be->be_suffix[0]->bv_val );
282 rc = dnNormalize( NULL, &val, &bv );
283 if( rc != LDAP_SUCCESS ) {
288 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
298 /* objectclasses should be configurable items */
300 val.bv_len = sizeof("top")-1;
301 attr_merge( e, ad_objectClass, vals );
303 val.bv_val = "person";
304 val.bv_len = sizeof("person")-1;
305 attr_merge( e, ad_objectClass, vals );
307 val.bv_val = "uidObject";
308 val.bv_len = sizeof("uidObject")-1;
309 attr_merge( e, ad_objectClass, vals );
311 val.bv_val = pw->pw_name;
313 attr_merge( e, ad_uid, vals ); /* required by uidObject */
314 attr_merge( e, ad_cn, vals ); /* required by person */
315 attr_merge( e, ad_sn, vals ); /* required by person */
319 * if gecos is present, add it as a cn. first process it
320 * according to standard BSD usage. If the processed cn has
321 * a space, use the tail as the surname.
323 if (pw->pw_gecos[0]) {
326 val.bv_val = pw->pw_gecos;
327 val.bv_len = strlen(val.bv_val);
328 attr_merge(e, ad_description, vals);
330 s = strchr(val.bv_val, ',');
333 s = strchr(val.bv_val, '&');
335 int i = s - val.bv_val;
336 strncpy(buf, val.bv_val, i);
338 strcpy(s, pw->pw_name);
340 strcat(s, val.bv_val+i+1);
343 val.bv_len = strlen(val.bv_val);
344 if ( strcmp( val.bv_val, pw->pw_name ))
345 attr_merge( e, ad_cn, vals );
346 if ( (s=strrchr(val.bv_val, ' '))) {
348 val.bv_len = strlen(val.bv_val);
349 attr_merge(e, ad_sn, vals);