1 /* search.c - /etc/passwd backend search function */
10 #include <ac/string.h>
19 static Entry *pw2entry(
47 int err = LDAP_SUCCESS;
54 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
56 stoptime = op->o_time + tlimit;
57 slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
63 if ( be->be_private != NULL ) {
64 (void) setpwfile( (char *) be->be_private );
66 #endif /* HAVE_SETPWFILE */
68 /* Handle a query for the base of this backend */
69 if ( be_issuffix( be, nbase ) ) {
70 struct berval val, *vals[2];
75 matched = ch_strdup( base );
77 if( scope != LDAP_SCOPE_ONELEVEL ) {
78 /* Create an entry corresponding to the base DN */
79 e = (Entry *) ch_calloc(1, sizeof(Entry));
81 e->e_dn = ch_strdup( base );
83 /* Use the first attribute of the DN
84 * as an attribute within the entry itself.
86 rdn = dn_rdn(NULL, base);
88 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
89 err = LDAP_INVALID_DN_SYNTAX;
93 val.bv_val = rdn_attr_value(rdn);
94 val.bv_len = strlen( val.bv_val );
95 attr_merge( e, rdn_attr_type(rdn), vals );
100 /* Every entry needs an objectclass. We don't really
101 * know if our hardcoded choice here agrees with the
102 * DN that was configured for this backend, but it's
103 * better than nothing.
105 * should be a configuratable item
107 val.bv_val = "organizationalUnit";
108 val.bv_len = strlen( val.bv_val );
109 attr_merge( e, "objectClass", vals );
111 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
112 send_search_entry( be, conn, op,
113 e, attrs, attrsonly, NULL );
118 if ( scope != LDAP_SCOPE_BASE ) {
119 /* check all our "children" */
121 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
122 /* check for abandon */
123 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
124 if ( op->o_abandon ) {
125 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
129 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
131 /* check time limit */
132 if ( slap_get_time() > stoptime ) {
133 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
134 NULL, NULL, NULL, NULL );
139 e = pw2entry( be, pw, NULL );
141 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
142 /* check size limit */
143 if ( --slimit == -1 ) {
144 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
145 NULL, NULL, NULL, NULL );
150 send_search_entry( be, conn, op,
151 e, attrs, attrsonly, NULL );
161 parent = dn_parent( be, base );
163 /* This backend is only one layer deep. Don't answer requests for
164 * anything deeper than that.
166 if( !be_issuffix( be, parent ) ) {
168 for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
169 if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
170 matched = ch_strdup( be->be_suffix[i] );
174 err = LDAP_NO_SUCH_OBJECT;
178 if( scope == LDAP_SCOPE_ONELEVEL ) {
182 rdn = dn_rdn( NULL, base );
184 if ( (user = rdn_attr_value(rdn)) == NULL) {
185 err = LDAP_OPERATIONS_ERROR;
189 if ( (pw = getpwnam( user )) == NULL ) {
192 err = LDAP_NO_SUCH_OBJECT;
196 e = pw2entry( be, pw, rdn );
198 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
199 send_search_entry( be, conn, op,
200 e, attrs, attrsonly, NULL );
208 send_ldap_result( conn, op,
209 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
212 if( matched != NULL ) free( matched );
213 if( parent != NULL ) free( parent );
214 if( rdn != NULL ) free( rdn );
215 if( user != NULL ) free( user );
221 pw2entry( Backend *be, struct passwd *pw, char *rdn )
226 struct berval *vals[2];
232 * from pw we get pw_name and make it cn
233 * give it an objectclass of person.
236 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
239 /* rdn attribute type should be a configuratable item */
240 sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
241 e->e_dn = ch_strdup( buf );
242 e->e_ndn = ch_strdup( buf );
243 (void) dn_normalize_case( e->e_ndn );
245 val.bv_val = pw->pw_name;
246 val.bv_len = strlen( pw->pw_name );
247 attr_merge( e, "uid", vals ); /* required by uidObject */
248 attr_merge( e, "cn", vals ); /* required by person */
249 attr_merge( e, "sn", vals ); /* required by person */
253 * if gecos is present, add it as a cn. first process it
254 * according to standard BSD usage. If the processed cn has
255 * a space, use the tail as the surname.
257 if (pw->pw_gecos[0]) {
260 val.bv_val = pw->pw_gecos;
261 val.bv_len = strlen(val.bv_val);
262 attr_merge(e, "description", vals);
264 s = strchr(val.bv_val, ',');
267 s = strchr(val.bv_val, '&');
269 int i = s - val.bv_val;
270 strncpy(buf, val.bv_val, i);
272 strcpy(s, pw->pw_name);
274 strcat(s, val.bv_val+i+1);
277 val.bv_len = strlen(val.bv_val);
278 if ( strcmp( val.bv_val, pw->pw_name ))
279 attr_merge( e, "cn", vals );
280 if ( (s=strrchr(val.bv_val, ' '))) {
282 val.bv_len = strlen(val.bv_val);
283 attr_merge(e, "sn", vals);
288 /* objectclasses should be configuratable items */
289 val.bv_val = "person";
290 val.bv_len = strlen( val.bv_val );
291 attr_merge( e, "objectclass", vals );
293 val.bv_val = "uidObject";
294 val.bv_len = strlen( val.bv_val );
295 attr_merge( e, "objectclass", vals );