1 /* search.c - /etc/passwd backend search function */
10 #include <ac/string.h>
19 static Entry *pw2entry(
35 const char *filterstr,
46 int err = LDAP_SUCCESS;
53 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
55 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
57 stoptime = op->o_time + tlimit;
58 slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
64 if ( be->be_private != NULL ) {
65 (void) setpwfile( (char *) be->be_private );
67 #endif /* HAVE_SETPWFILE */
69 /* Handle a query for the base of this backend */
70 if ( be_issuffix( be, nbase->bv_val ) ) {
71 struct berval val, *vals[2];
76 matched = (char *) base;
78 if( scope != LDAP_SCOPE_ONELEVEL ) {
80 AttributeDescription *desc = NULL;
82 /* Create an entry corresponding to the base DN */
83 e = (Entry *) ch_calloc(1, sizeof(Entry));
84 e->e_name.bv_val = ch_strdup( base->bv_val );
85 e->e_name.bv_len = base->bv_len;
86 e->e_nname.bv_val = ch_strdup( nbase->bv_val );
87 e->e_nname.bv_len = nbase->bv_len;
91 /* Use the first attribute of the DN
92 * as an attribute within the entry itself.
94 if( ldap_str2rdn( base->bv_val, &rdn, &text,
95 LDAP_DN_FORMAT_LDAP ) ) {
96 err = LDAP_INVALID_DN_SYNTAX;
100 if( slap_bv2ad( &rdn[0][0]->la_attr, &desc, &text )) {
101 err = LDAP_NO_SUCH_OBJECT;
106 val = rdn[0][0]->la_value;
107 attr_merge( e, desc, vals );
112 /* Every entry needs an objectclass. We don't really
113 * know if our hardcoded choice here agrees with the
114 * DN that was configured for this backend, but it's
115 * better than nothing.
117 * should be a configuratable item
119 val.bv_val = "organizationalUnit";
120 val.bv_len = sizeof("organizationalUnit")-1;
121 attr_merge( e, ad_objectClass, vals );
123 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
124 send_search_entry( be, conn, op,
125 e, attrs, attrsonly, NULL );
130 if ( scope != LDAP_SCOPE_BASE ) {
131 /* check all our "children" */
133 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
134 /* check for abandon */
135 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
136 if ( op->o_abandon ) {
137 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
141 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
143 /* check time limit */
144 if ( slap_get_time() > stoptime ) {
145 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
146 NULL, NULL, NULL, NULL );
151 e = pw2entry( be, pw );
153 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
154 /* check size limit */
155 if ( --slimit == -1 ) {
156 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
157 NULL, NULL, NULL, NULL );
162 send_search_entry( be, conn, op,
163 e, attrs, attrsonly, NULL );
173 const char *text = NULL;
175 parent = dn_parent( be, nbase->bv_val );
177 /* This backend is only one layer deep. Don't answer requests for
178 * anything deeper than that.
180 if( !be_issuffix( be, parent ) ) {
182 for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
183 if( dnIsSuffix( nbase, be->be_nsuffix[i] ) ) {
184 matched = be->be_suffix[i]->bv_val;
188 err = LDAP_NO_SUCH_OBJECT;
192 if( scope == LDAP_SCOPE_ONELEVEL ) {
196 if ( ldap_str2rdn( base->bv_val, &rdn, &text, LDAP_DN_FORMAT_LDAP )) {
197 err = LDAP_OPERATIONS_ERROR;
201 if ( (pw = getpwnam( rdn[0][0]->la_value.bv_val )) == NULL ) {
203 err = LDAP_NO_SUCH_OBJECT;
207 e = pw2entry( be, pw );
209 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
210 send_search_entry( be, conn, op,
211 e, attrs, attrsonly, NULL );
219 send_ldap_result( conn, op,
220 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
223 if( rdn != NULL ) ldap_rdnfree( rdn );
229 pw2entry( Backend *be, struct passwd *pw )
234 struct berval *vals[2];
240 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
241 AttributeDescription *ad_cn = NULL;
242 AttributeDescription *ad_sn = NULL;
243 AttributeDescription *ad_uid = NULL;
244 AttributeDescription *ad_description = NULL;
246 rc = slap_str2ad( "cn", &ad_cn, &text );
247 if(rc != LDAP_SUCCESS) return NULL;
248 rc = slap_str2ad( "sn", &ad_sn, &text );
249 if(rc != LDAP_SUCCESS) return NULL;
250 rc = slap_str2ad( "uid", &ad_uid, &text );
251 if(rc != LDAP_SUCCESS) return NULL;
252 rc = slap_str2ad( "description", &ad_description, &text );
253 if(rc != LDAP_SUCCESS) return NULL;
256 * from pw we get pw_name and make it cn
257 * give it an objectclass of person.
260 pwlen = strlen( pw->pw_name );
261 val.bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0]->bv_len );
262 val.bv_val = ch_malloc( val.bv_len + 1 );
264 /* rdn attribute type should be a configuratable item */
265 sprintf( val.bv_val, "uid=%s,%s",
266 pw->pw_name, be->be_suffix[0]->bv_val );
268 rc = dnNormalize2( NULL, &val, &bv );
269 if( rc != LDAP_SUCCESS ) {
274 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
283 /* objectclasses should be configurable items */
285 val.bv_len = sizeof("top")-1;
286 attr_merge( e, ad_objectClass, vals );
288 val.bv_val = "person";
289 val.bv_len = sizeof("person")-1;
290 attr_merge( e, ad_objectClass, vals );
292 val.bv_val = "uidObject";
293 val.bv_len = sizeof("uidObject")-1;
294 attr_merge( e, ad_objectClass, vals );
296 val.bv_val = pw->pw_name;
298 attr_merge( e, ad_uid, vals ); /* required by uidObject */
299 attr_merge( e, ad_cn, vals ); /* required by person */
300 attr_merge( e, ad_sn, vals ); /* required by person */
304 * if gecos is present, add it as a cn. first process it
305 * according to standard BSD usage. If the processed cn has
306 * a space, use the tail as the surname.
308 if (pw->pw_gecos[0]) {
311 val.bv_val = pw->pw_gecos;
312 val.bv_len = strlen(val.bv_val);
313 attr_merge(e, ad_description, vals);
315 s = strchr(val.bv_val, ',');
318 s = strchr(val.bv_val, '&');
321 int i = s - val.bv_val;
322 strncpy(buf, val.bv_val, i);
324 strcpy(s, pw->pw_name);
326 strcat(s, val.bv_val+i+1);
329 val.bv_len = strlen(val.bv_val);
330 if ( strcmp( val.bv_val, pw->pw_name ))
331 attr_merge( e, ad_cn, vals );
332 if ( (s=strrchr(val.bv_val, ' '))) {
334 val.bv_len = strlen(val.bv_val);
335 attr_merge(e, ad_sn, vals);