1 /* search.c - /etc/passwd backend search function */
10 #include <ac/string.h>
19 static Entry *pw2entry(
46 int err = LDAP_SUCCESS;
53 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
55 stoptime = op->o_time + tlimit;
56 slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
62 if ( be->be_private != NULL ) {
63 (void) setpwfile( (char *) be->be_private );
65 #endif /* HAVE_SETPWFILE */
67 /* Handle a query for the base of this backend */
68 if ( be_issuffix( be, base ) ) {
69 struct berval val, *vals[2];
74 matched = ch_strdup( base );
76 if( scope != LDAP_SCOPE_ONELEVEL ) {
77 /* Create an entry corresponding to the base DN */
78 e = (Entry *) ch_calloc(1, sizeof(Entry));
80 e->e_dn = ch_strdup( base );
82 /* Use the first attribute of the DN
83 * as an attribute within the entry itself.
85 rdn = dn_rdn(NULL, base);
87 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
88 err = LDAP_INVALID_DN_SYNTAX;
92 val.bv_val = rdn_attr_value(rdn);
93 val.bv_len = strlen( val.bv_val );
94 attr_merge( e, rdn_attr_type(rdn), vals );
99 /* Every entry needs an objectclass. We don't really
100 * know if our hardcoded choice here agrees with the
101 * DN that was configured for this backend, but it's
102 * better than nothing.
104 * should be a configuratable item
106 val.bv_val = "organizationalUnit";
107 val.bv_len = strlen( val.bv_val );
108 attr_merge( e, "objectClass", vals );
110 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
111 send_search_entry( be, conn, op,
112 e, attrs, attrsonly, NULL );
117 if ( scope != LDAP_SCOPE_BASE ) {
118 /* check all our "children" */
120 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
121 /* check for abandon */
122 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
123 if ( op->o_abandon ) {
124 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
128 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
130 /* check time limit */
131 if ( slap_get_time() > stoptime ) {
132 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
133 NULL, NULL, NULL, NULL );
138 e = pw2entry( be, pw, NULL );
140 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
141 /* check size limit */
142 if ( --slimit == -1 ) {
143 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
144 NULL, NULL, NULL, NULL );
149 send_search_entry( be, conn, op,
150 e, attrs, attrsonly, NULL );
160 parent = dn_parent( be, base );
162 /* This backend is only one layer deep. Don't answer requests for
163 * anything deeper than that.
165 if( !be_issuffix( be, parent ) ) {
167 for( i=0; be->be_suffix[i] != NULL; i++ ) {
168 if( dn_issuffix( base, be->be_suffix[i] ) ) {
169 matched = ch_strdup( be->be_suffix[i] );
173 err = LDAP_NO_SUCH_OBJECT;
177 if( scope == LDAP_SCOPE_ONELEVEL ) {
181 rdn = dn_rdn( NULL, base );
183 if ( (user = rdn_attr_value(rdn)) == NULL) {
184 err = LDAP_OPERATIONS_ERROR;
188 user = ldap_pvt_str2lower( user );
190 if ( (pw = getpwnam( user )) == NULL ) {
193 err = LDAP_NO_SUCH_OBJECT;
197 e = pw2entry( be, pw, rdn );
199 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
200 send_search_entry( be, conn, op,
201 e, attrs, attrsonly, NULL );
209 send_ldap_result( conn, op,
210 err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
213 if( matched != NULL ) free( matched );
214 if( parent != NULL ) free( parent );
215 if( rdn != NULL ) free( rdn );
216 if( user != NULL ) free( user );
222 pw2entry( Backend *be, struct passwd *pw, char *rdn )
227 struct berval *vals[2];
233 * from pw we get pw_name and make it cn
234 * give it an objectclass of person.
237 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
240 /* rdn attribute type should be a configuratable item */
241 sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
242 e->e_dn = ch_strdup( buf );
243 e->e_ndn = ch_strdup( buf );
244 (void) dn_normalize_case( e->e_ndn );
246 val.bv_val = pw->pw_name;
247 val.bv_len = strlen( pw->pw_name );
248 attr_merge( e, "uid", vals ); /* required by uidObject */
249 attr_merge( e, "cn", vals ); /* required by person */
250 attr_merge( e, "sn", vals ); /* required by person */
254 * if gecos is present, add it as a cn. first process it
255 * according to standard BSD usage. If the processed cn has
256 * a space, use the tail as the surname.
258 if (pw->pw_gecos[0]) {
261 val.bv_val = pw->pw_gecos;
262 val.bv_len = strlen(val.bv_val);
263 attr_merge(e, "description", vals);
265 s = strchr(val.bv_val, ',');
268 s = strchr(val.bv_val, '&');
270 int i = s - val.bv_val;
271 strncpy(buf, val.bv_val, i);
273 strcpy(s, pw->pw_name);
275 strcat(s, val.bv_val+i+1);
278 val.bv_len = strlen(val.bv_val);
279 if ( strcmp( val.bv_val, pw->pw_name ))
280 attr_merge( e, "cn", vals );
281 if ( (s=strrchr(val.bv_val, ' '))) {
283 val.bv_len = strlen(val.bv_val);
284 attr_merge(e, "sn", vals);
289 /* objectclasses should be configuratable items */
290 val.bv_val = "person";
291 val.bv_len = strlen( val.bv_val );
292 attr_merge( e, "objectclass", vals );
294 val.bv_val = "uidObject";
295 val.bv_len = strlen( val.bv_val );
296 attr_merge( e, "objectclass", vals );