1 /* search.c - /etc/passwd backend search function */
17 static Entry *pw2entry(
44 int err = LDAP_SUCCESS;
51 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
53 stoptime = op->o_time + tlimit;
54 slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
60 if ( be->be_private != NULL ) {
61 (void) setpwfile( (char *) be->be_private );
63 #endif /* HAVE_SETPWFILE */
65 /* Handle a query for the base of this backend */
66 if ( be_issuffix( be, base ) ) {
67 struct berval val, *vals[2];
72 matched = ch_strdup( base );
74 if( scope != LDAP_SCOPE_ONELEVEL ) {
75 /* Create an entry corresponding to the base DN */
76 e = (Entry *) ch_calloc(1, sizeof(Entry));
78 e->e_dn = ch_strdup( base );
80 /* Use the first attribute of the DN
81 * as an attribute within the entry itself.
83 rdn = dn_rdn(NULL, base);
85 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
86 err = LDAP_INVALID_DN_SYNTAX;
90 val.bv_val = rdn_attr_value(rdn);
91 val.bv_len = strlen( val.bv_val );
92 attr_merge( e, rdn_attr_type(rdn), vals );
97 /* Every entry needs an objectclass. We don't really
98 * know if our hardcoded choice here agrees with the
99 * DN that was configured for this backend, but it's
100 * better than nothing.
102 * should be a configuratable item
104 val.bv_val = "organizationalUnit";
105 val.bv_len = strlen( val.bv_val );
106 attr_merge( e, "objectClass", vals );
108 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
109 send_search_entry( be, conn, op,
110 e, attrs, attrsonly, NULL );
115 if ( scope != LDAP_SCOPE_BASE ) {
116 /* check all our "children" */
118 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
119 /* check for abandon */
120 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
121 if ( op->o_abandon ) {
122 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
126 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
128 /* check time limit */
129 if ( slap_get_time() > stoptime ) {
130 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
131 NULL, NULL, NULL, NULL );
136 e = pw2entry( be, pw, NULL );
138 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
139 /* check size limit */
140 if ( --slimit == -1 ) {
141 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
142 NULL, NULL, NULL, NULL );
147 send_search_entry( be, conn, op,
148 e, attrs, attrsonly, NULL );
158 parent = dn_parent( be, base );
160 /* This backend is only one layer deep. Don't answer requests for
161 * anything deeper than that.
163 if( !be_issuffix( be, parent ) ) {
165 for( i=0; be->be_suffix[i] != NULL; i++ ) {
166 if( dn_issuffix( base, be->be_suffix[i] ) ) {
167 matched = ch_strdup( be->be_suffix[i] );
171 err = LDAP_NO_SUCH_OBJECT;
175 if( scope == LDAP_SCOPE_ONELEVEL ) {
179 rdn = dn_rdn( NULL, base );
181 if ( (user = rdn_attr_value(rdn)) == NULL) {
182 err = LDAP_OPERATIONS_ERROR;
186 user = str2lower( user );
188 if ( (pw = getpwnam( user )) == NULL ) {
191 err = LDAP_NO_SUCH_OBJECT;
195 e = pw2entry( be, pw, rdn );
197 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
198 send_search_entry( be, conn, op,
199 e, attrs, attrsonly, NULL );
207 send_ldap_result( conn, op,
208 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
211 if( matched != NULL ) free( matched );
212 if( parent != NULL ) free( parent );
213 if( rdn != NULL ) free( rdn );
214 if( user != NULL ) free( user );
220 pw2entry( Backend *be, struct passwd *pw, char *rdn )
225 struct berval *vals[2];
231 * from pw we get pw_name and make it cn
232 * give it an objectclass of person.
235 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
238 /* rdn attribute type should be a configuratable item */
239 sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
240 e->e_dn = ch_strdup( buf );
241 e->e_ndn = ch_strdup( buf );
242 (void) dn_normalize_case( e->e_ndn );
244 val.bv_val = pw->pw_name;
245 val.bv_len = strlen( pw->pw_name );
246 attr_merge( e, "uid", vals ); /* required by uidObject */
247 attr_merge( e, "cn", vals ); /* required by person */
248 attr_merge( e, "sn", vals ); /* required by person */
252 * if gecos is present, add it as a cn. first process it
253 * according to standard BSD usage. If the processed cn has
254 * a space, use the tail as the surname.
256 if (pw->pw_gecos[0]) {
259 val.bv_val = pw->pw_gecos;
260 val.bv_len = strlen(val.bv_val);
261 attr_merge(e, "description", vals);
263 s = strchr(val.bv_val, ',');
266 s = strchr(val.bv_val, '&');
268 int i = s - val.bv_val;
269 strncpy(buf, val.bv_val, i);
271 strcpy(s, pw->pw_name);
273 strcat(s, val.bv_val+i+1);
276 val.bv_len = strlen(val.bv_val);
277 if ( strcmp( val.bv_val, pw->pw_name ))
278 attr_merge( e, "cn", vals );
279 if ( (s=strrchr(val.bv_val, ' '))) {
281 val.bv_len = strlen(val.bv_val);
282 attr_merge(e, "sn", vals);
287 /* objectclasses should be configuratable items */
288 val.bv_val = "person";
289 val.bv_len = strlen( val.bv_val );
290 attr_merge( e, "objectclass", vals );
292 val.bv_val = "uidObject";
293 val.bv_len = strlen( val.bv_val );
294 attr_merge( e, "objectclass", vals );