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 );
121 /* Every entry needs an objectclass. We don't really
122 * know if our hardcoded choice here agrees with the
123 * DN that was configured for this backend, but it's
124 * better than nothing.
126 * should be a configuratable item
128 val.bv_val = "organizationalUnit";
129 val.bv_len = sizeof("organizationalUnit")-1;
130 attr_merge( e, ad_objectClass, vals );
132 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
133 send_search_entry( be, conn, op,
134 e, attrs, attrsonly, NULL );
139 if ( scope != LDAP_SCOPE_BASE ) {
140 /* check all our "children" */
142 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
143 /* check for abandon */
144 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
145 if ( op->o_abandon ) {
146 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
150 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
152 /* check time limit */
153 if ( slap_get_time() > stoptime ) {
154 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
155 NULL, NULL, NULL, NULL );
160 e = pw2entry( be, pw, NULL );
162 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
163 /* check size limit */
164 if ( --slimit == -1 ) {
165 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
166 NULL, NULL, NULL, NULL );
171 send_search_entry( be, conn, op,
172 e, attrs, attrsonly, NULL );
182 parent = dn_parent( be, nbase );
184 /* This backend is only one layer deep. Don't answer requests for
185 * anything deeper than that.
187 if( !be_issuffix( be, parent ) ) {
189 for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
190 if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
191 matched = ch_strdup( be->be_suffix[i] );
195 err = LDAP_NO_SUCH_OBJECT;
199 if( scope == LDAP_SCOPE_ONELEVEL ) {
203 rdn = dn_rdn( NULL, base );
205 if ( (user = rdn_attr_value(rdn)) == NULL) {
206 err = LDAP_OPERATIONS_ERROR;
210 if ( (pw = getpwnam( user )) == NULL ) {
213 err = LDAP_NO_SUCH_OBJECT;
217 e = pw2entry( be, pw, rdn );
219 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
220 send_search_entry( be, conn, op,
221 e, attrs, attrsonly, NULL );
229 send_ldap_result( conn, op,
230 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
233 if( matched != NULL ) free( matched );
234 if( parent != NULL ) free( parent );
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] );
297 e->e_dn = ch_strdup( buf );
298 e->e_ndn = ch_strdup( buf );
299 (void) dn_normalize( e->e_ndn );
301 val.bv_val = pw->pw_name;
302 val.bv_len = strlen( pw->pw_name );
303 attr_merge( e, ad_uid, vals ); /* required by uidObject */
304 attr_merge( e, ad_cn, vals ); /* required by person */
305 attr_merge( e, ad_sn, vals ); /* required by person */
309 * if gecos is present, add it as a cn. first process it
310 * according to standard BSD usage. If the processed cn has
311 * a space, use the tail as the surname.
313 if (pw->pw_gecos[0]) {
316 val.bv_val = pw->pw_gecos;
317 val.bv_len = strlen(val.bv_val);
318 attr_merge(e, ad_description, vals);
320 s = strchr(val.bv_val, ',');
323 s = strchr(val.bv_val, '&');
325 int i = s - val.bv_val;
326 strncpy(buf, val.bv_val, i);
328 strcpy(s, pw->pw_name);
330 strcat(s, val.bv_val+i+1);
333 val.bv_len = strlen(val.bv_val);
334 if ( strcmp( val.bv_val, pw->pw_name ))
335 attr_merge( e, ad_cn, vals );
336 if ( (s=strrchr(val.bv_val, ' '))) {
338 val.bv_len = strlen(val.bv_val);
339 attr_merge(e, ad_sn, vals);