1 /* search.c - /etc/passwd backend search function */
17 static Entry *pw2entry(
43 int err = LDAP_NO_SUCH_OBJECT;
50 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
52 stoptime = op->o_time + tlimit;
53 slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
59 if ( be->be_private != NULL ) {
60 (void) setpwfile( (char *) be->be_private );
62 #endif /* HAVE_SETPWFILE */
64 /* Handle a query for the base of this backend */
65 if ( be_issuffix( be, base ) ) {
66 struct berval val, *vals[2];
71 /* Create an entry corresponding to the base DN */
72 e = (Entry *) ch_calloc(1, sizeof(Entry));
74 e->e_dn = strdup(base);
76 /* Use the first attribute of the DN
77 * as an attribute within the entry itself.
79 rdn = dn_rdn(NULL, base);
81 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
82 err = LDAP_INVALID_DN_SYNTAX;
86 val.bv_val = rdn_attr_value(rdn);
87 val.bv_len = strlen( val.bv_val );
88 attr_merge( e, rdn_attr_type(rdn), vals );
93 /* Every entry needs an objectclass. We don't really
94 * know if our hardcoded choice here agrees with the
95 * DN that was configured for this backend, but it's
96 * better than nothing.
98 * should be a configuratable item
100 val.bv_val = "organizationalUnit";
101 val.bv_len = strlen( val.bv_val );
102 attr_merge( e, "objectClass", vals );
104 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
105 send_search_entry( be, conn, op, e, attrs, attrsonly );
106 matched = strdup( be->be_suffix[0] );
110 if ( scope != LDAP_SCOPE_BASE ) {
111 /* check all our "children" */
113 for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
114 /* check for abandon */
115 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
116 if ( op->o_abandon ) {
117 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
121 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
123 /* check time limit */
124 if ( slap_get_time() > stoptime ) {
125 send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
131 e = pw2entry( be, pw, NULL );
133 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
134 /* check size limit */
135 if ( --slimit == -1 ) {
136 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
142 send_search_entry( be, conn, op, e, attrs, attrsonly );
152 parent = dn_parent( be, base );
154 /* This backend is only one layer deep. Don't answer requests for
155 * anything deeper than that.
157 if( !be_issuffix( be, parent ) ) {
161 rdn = dn_rdn( NULL, base );
163 if ( (user = rdn_attr_value(rdn)) == NULL) {
164 err = LDAP_INVALID_DN_SYNTAX;
168 for( s = user; *s ; s++ ) {
172 if ( (pw = getpwnam( user )) == NULL ) {
176 e = pw2entry( be, pw, rdn );
178 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
179 send_search_entry( be, conn, op, e, attrs, attrsonly );
188 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
191 send_ldap_result( conn, op, err, matched, NULL );
194 if( matched != NULL ) free( matched );
195 if( parent != NULL ) free( parent );
196 if( rdn != NULL ) free( rdn );
197 if( user != NULL ) free( user );
203 pw2entry( Backend *be, struct passwd *pw, char *rdn )
208 struct berval *vals[2];
214 * from pw we get pw_name and make it cn
215 * give it an objectclass of person.
218 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
221 /* rdn attribute type should be a configuratable item */
222 sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
223 e->e_dn = ch_strdup( buf );
224 e->e_ndn = dn_normalize_case( ch_strdup( buf ) );
226 val.bv_val = pw->pw_name;
227 val.bv_len = strlen( pw->pw_name );
228 attr_merge( e, "uid", vals ); /* required by uidObject */
229 attr_merge( e, "cn", vals ); /* required by person */
230 attr_merge( e, "sn", vals ); /* required by person */
234 * if gecos is present, add it as a cn. first process it
235 * according to standard BSD usage. If the processed cn has
236 * a space, use the tail as the surname.
238 if (pw->pw_gecos[0]) {
241 val.bv_val = pw->pw_gecos;
242 val.bv_len = strlen(val.bv_val);
243 attr_merge(e, "description", vals);
245 s = strchr(val.bv_val, ',');
248 s = strchr(val.bv_val, '&');
250 int i = s - val.bv_val;
251 strncpy(buf, val.bv_val, i);
253 strcpy(s, pw->pw_name);
256 strcat(s, val.bv_val+i+1);
259 val.bv_len = strlen(val.bv_val);
260 if ( strcmp( val.bv_val, pw->pw_name ))
261 attr_merge( e, "cn", vals );
262 if ( (s=strrchr(val.bv_val, ' '))) {
264 val.bv_len = strlen(val.bv_val);
265 attr_merge(e, "sn", vals);
270 /* objectclasses should be configuratable items */
271 val.bv_val = "person";
272 val.bv_len = strlen( val.bv_val );
273 attr_merge( e, "objectclass", vals );
275 val.bv_val = "uidObject";
276 val.bv_len = strlen( val.bv_val );
277 attr_merge( e, "objectclass", vals );