search.c 8.62 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* search.c - /etc/passwd backend search function */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3

Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
5
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7

8
#include <ac/ctype.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
10
11
12
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
13
#include <pwd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
14

Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
#include "slap.h"
16
#include "back-passwd.h"
17
#include <ldap_pvt.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
18

Kurt Zeilenga's avatar
Kurt Zeilenga committed
19
static void pw_start( Backend *be );
20

21
22
static Entry *pw2entry(
	Backend *be,
Howard Chu's avatar
Cleanup    
Howard Chu committed
23
24
	struct passwd *pw,
	const char **text);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
25
26
27
28

int
passwd_back_search(
    Operation	*op,
29
    SlapReply	*rs )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
31
32
33
34
{
	struct passwd	*pw;
	Entry		*e;
	char		*s;
	time_t		stoptime;
35

Howard Chu's avatar
Howard Chu committed
36
	LDAPRDN rdn = NULL;
37
	struct berval parent = { 0, NULL };
Kurt Zeilenga's avatar
Kurt Zeilenga committed
38

39
40
	AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;

41
42
43
44
45
	op->oq_search.rs_tlimit = (op->oq_search.rs_tlimit > op->o_bd->be_timelimit || op->oq_search.rs_tlimit < 1) ? op->o_bd->be_timelimit
	    : op->oq_search.rs_tlimit;
	stoptime = op->o_time + op->oq_search.rs_tlimit;
	op->oq_search.rs_slimit = (op->oq_search.rs_slimit > op->o_bd->be_sizelimit || op->oq_search.rs_slimit < 1) ? op->o_bd->be_sizelimit
	    : op->oq_search.rs_slimit;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
46

47
	/* Handle a query for the base of this backend */
48
	if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
49
		struct berval	vals[2];
50

51
		vals[1].bv_val = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
52

53
		rs->sr_matched = op->o_req_dn.bv_val;
54

55
		if( op->oq_search.rs_scope != LDAP_SCOPE_ONELEVEL ) {
56
57
			AttributeDescription *desc = NULL;

58
59
			/* Create an entry corresponding to the base DN */
			e = (Entry *) ch_calloc(1, sizeof(Entry));
60
61
62
63
			e->e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
			e->e_name.bv_len = op->o_req_dn.bv_len;
			e->e_nname.bv_val =  ch_strdup( op->o_req_ndn.bv_val );
			e->e_nname.bv_len = op->o_req_ndn.bv_len;
64
			e->e_attrs = NULL;
65
			e->e_private = NULL;
66

67
68
69
			/* Use the first attribute of the DN
		 	* as an attribute within the entry itself.
		 	*/
70
			if( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text, 
71
72
				LDAP_DN_FORMAT_LDAP ) )
			{
73
				rs->sr_err = LDAP_INVALID_DN_SYNTAX;
74
75
				goto done;
			}
76

Howard Chu's avatar
Howard Chu committed
77
			if( slap_bv2ad( &rdn[0]->la_attr, &desc, &rs->sr_text )) {
78
				rs->sr_err = LDAP_NO_SUCH_OBJECT;
Howard Chu's avatar
Howard Chu committed
79
80
				ldap_rdnfree(rdn);
				goto done;
81
82
			}

Howard Chu's avatar
Howard Chu committed
83
			vals[0] = rdn[0]->la_value;
84
			attr_mergeit( e, desc, vals );
85

Howard Chu's avatar
Howard Chu committed
86
			ldap_rdnfree(rdn);
87
88
89
90
91
92
93
94
95
			rdn = NULL;

			/* Every entry needs an objectclass. We don't really
			 * know if our hardcoded choice here agrees with the
			 * DN that was configured for this backend, but it's
			 * better than nothing.
			 *
			 * should be a configuratable item
			 */
96
97
			vals[0].bv_val = "organizationalUnit";
			vals[0].bv_len = sizeof("organizationalUnit")-1;
98
			attr_mergeit( e, ad_objectClass, vals );
99
	
100
101
102
103
			if ( test_filter( op, e, op->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
				rs->sr_entry = e;
				rs->sr_attrs = op->oq_search.rs_attrs;
				send_search_entry( op, rs );
104
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
105
106
		}

107
		if ( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
108
			/* check all our "children" */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
109

110
			ldap_pvt_thread_mutex_lock( &passwd_mutex );
111
			pw_start( op->o_bd );
112
113
114
115
			for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
				/* check for abandon */
				if ( op->o_abandon ) {
					endpwent();
116
					ldap_pvt_thread_mutex_unlock( &passwd_mutex );
117
118
119
120
121
					return( -1 );
				}

				/* check time limit */
				if ( slap_get_time() > stoptime ) {
122
					send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
123
					endpwent();
124
					ldap_pvt_thread_mutex_unlock( &passwd_mutex );
125
126
127
					return( 0 );
				}

128
129
				if ( !(e = pw2entry( op->o_bd, pw, &rs->sr_text )) ) {
					rs->sr_err = LDAP_OTHER;
Howard Chu's avatar
Cleanup    
Howard Chu committed
130
					endpwent();
131
					ldap_pvt_thread_mutex_unlock( &passwd_mutex );
Howard Chu's avatar
Cleanup    
Howard Chu committed
132
133
					goto done;
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
134

135
				if ( test_filter( op, e, op->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
136
					/* check size limit */
137
138
					if ( --op->oq_search.rs_slimit == -1 ) {
						send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
139
						endpwent();
140
						ldap_pvt_thread_mutex_unlock( &passwd_mutex );
141
142
143
						return( 0 );
					}

144
145
146
					rs->sr_entry = e;
					rs->sr_attrs = op->oq_search.rs_attrs;
					send_search_entry( op, rs );
147
148
149
150
				}

				entry_free( e );
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
151
			endpwent();
152
			ldap_pvt_thread_mutex_unlock( &passwd_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
153
154
		}

155
	} else {
156
157
		if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
			dnParent( &op->o_req_ndn, &parent );
158
		}
159
160
161
162

		/* This backend is only one layer deep. Don't answer requests for
		 * anything deeper than that.
		 */
163
		if( !be_issuffix( op->o_bd, &parent ) ) {
164
			int i;
165
166
167
			for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
				if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
					rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
168
169
170
					break;
				}
			}
171
			rs->sr_err = LDAP_NO_SUCH_OBJECT;
172
173
174
			goto done;
		}

175
		if( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
176
			goto done;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
177
178
		}

179
		if ( ldap_bv2rdn( &op->o_req_dn, &rdn, (char **)&rs->sr_text,
180
181
			LDAP_DN_FORMAT_LDAP ))
		{ 
182
			rs->sr_err = LDAP_OTHER;
183
184
			goto done;
		}
185

186
		ldap_pvt_thread_mutex_lock( &passwd_mutex );
187
		pw_start( op->o_bd );
Howard Chu's avatar
Howard Chu committed
188
		if ( (pw = getpwnam( rdn[0]->la_value.bv_val )) == NULL ) {
189
190
			rs->sr_matched = parent.bv_val;
			rs->sr_err = LDAP_NO_SUCH_OBJECT;
191
			ldap_pvt_thread_mutex_unlock( &passwd_mutex );
192
193
194
			goto done;
		}

195
		e = pw2entry( op->o_bd, pw, &rs->sr_text );
196
197
		ldap_pvt_thread_mutex_unlock( &passwd_mutex );
		if ( !e ) {
198
			rs->sr_err = LDAP_OTHER;
Howard Chu's avatar
Cleanup    
Howard Chu committed
199
200
			goto done;
		}
201

202
203
204
205
		if ( test_filter( op, e, op->oq_search.rs_filter ) == LDAP_COMPARE_TRUE ) {
			rs->sr_entry = e;
			rs->sr_attrs = op->oq_search.rs_attrs;
			send_search_entry( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
206
207
208
209
		}

		entry_free( e );
	}
210
211

done:
212
	if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
213
	send_ldap_result( op, rs );
214

Howard Chu's avatar
Howard Chu committed
215
	if( rdn != NULL ) ldap_rdnfree( rdn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
216
217
218
219

	return( 0 );
}

220
221
222
223
224
225
226
227
228
229
230
231
232
233
static void
pw_start(
	Backend *be
)
{
	endpwent();

#ifdef HAVE_SETPWFILE
	if ( be->be_private != NULL ) {
		(void) setpwfile( (char *) be->be_private );
	}
#endif /* HAVE_SETPWFILE */
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
234
static Entry *
Howard Chu's avatar
Cleanup    
Howard Chu committed
235
pw2entry( Backend *be, struct passwd *pw, const char **text )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
236
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
237
	size_t pwlen;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
	Entry		*e;
239
	struct berval	vals[2];
240
	struct berval	bv;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
241

242
243
	int rc;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
244
	AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
245
246
247
248
249
	AttributeDescription *ad_cn = NULL;
	AttributeDescription *ad_sn = NULL;
	AttributeDescription *ad_uid = NULL;
	AttributeDescription *ad_description = NULL;

Howard Chu's avatar
Cleanup    
Howard Chu committed
250
	rc = slap_str2ad( "cn", &ad_cn, text );
251
	if(rc != LDAP_SUCCESS) return NULL;
Howard Chu's avatar
Cleanup    
Howard Chu committed
252
	rc = slap_str2ad( "sn", &ad_sn, text );
253
	if(rc != LDAP_SUCCESS) return NULL;
Howard Chu's avatar
Cleanup    
Howard Chu committed
254
	rc = slap_str2ad( "uid", &ad_uid, text );
255
	if(rc != LDAP_SUCCESS) return NULL;
Howard Chu's avatar
Cleanup    
Howard Chu committed
256
	rc = slap_str2ad( "description", &ad_description, text );
257
258
	if(rc != LDAP_SUCCESS) return NULL;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
259
	/*
260
261
	 * from pw we get pw_name and make it cn
	 * give it an objectclass of person.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
262
263
	 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
264
	pwlen = strlen( pw->pw_name );
265
	vals[0].bv_len = (sizeof("uid=,")-1) + ( pwlen + be->be_suffix[0].bv_len );
266
	vals[0].bv_val = ch_malloc( vals[0].bv_len + 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
267
268

	/* rdn attribute type should be a configuratable item */
269
	sprintf( vals[0].bv_val, "uid=%s,%s",
270
		pw->pw_name, be->be_suffix[0].bv_val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
271

Kurt Zeilenga's avatar
Kurt Zeilenga committed
272
	rc = dnNormalize( 0, NULL, NULL, vals, &bv );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
273
	if( rc != LDAP_SUCCESS ) {
274
		free( vals[0].bv_val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
275
276
277
		return NULL;
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
278
	e = (Entry *) ch_calloc( 1, sizeof(Entry) );
279
	e->e_name = vals[0];
280
	e->e_nname = bv;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
281

Kurt Zeilenga's avatar
Kurt Zeilenga committed
282
283
	e->e_attrs = NULL;

284
	vals[1].bv_val = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
285
286

	/* objectclasses should be configurable items */
287
288
	vals[0].bv_val = "top";
	vals[0].bv_len = sizeof("top")-1;
289
	attr_mergeit( e, ad_objectClass, vals );
290

291
292
	vals[0].bv_val = "person";
	vals[0].bv_len = sizeof("person")-1;
293
	attr_mergeit( e, ad_objectClass, vals );
294

295
296
	vals[0].bv_val = "uidObject";
	vals[0].bv_len = sizeof("uidObject")-1;
297
	attr_mergeit( e, ad_objectClass, vals );
298

299
300
	vals[0].bv_val = pw->pw_name;
	vals[0].bv_len = pwlen;
301
302
303
	attr_mergeit( e, ad_uid, vals );	/* required by uidObject */
	attr_mergeit( e, ad_cn, vals );	/* required by person */
	attr_mergeit( e, ad_sn, vals );	/* required by person */
304

Kurt Zeilenga's avatar
Kurt Zeilenga committed
305
#ifdef HAVE_PW_GECOS
306
307
308
309
310
311
312
313
	/*
	 * if gecos is present, add it as a cn. first process it
	 * according to standard BSD usage. If the processed cn has
	 * a space, use the tail as the surname.
	 */
	if (pw->pw_gecos[0]) {
		char *s;

314
315
		vals[0].bv_val = pw->pw_gecos;
		vals[0].bv_len = strlen(vals[0].bv_val);
316
		attr_mergeit(e, ad_description, vals);
317

318
		s = strchr(vals[0].bv_val, ',');
319
320
		if (s) *s = '\0';

321
		s = strchr(vals[0].bv_val, '&');
322
		if (s) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
323
324
325
326
327
328
329
330
331
332
333
			char buf[1024];

			if( vals[0].bv_len + pwlen < sizeof(buf) ) {
				int i = s - vals[0].bv_val;
				strncpy(buf, vals[0].bv_val, i);
				s = buf+i;
				strcpy(s, pw->pw_name);
				*s = TOUPPER((unsigned char)*s);
				strcat(s, vals[0].bv_val+i+1);
				vals[0].bv_val = buf;
			}
334
		}
335
		vals[0].bv_len = strlen(vals[0].bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
336
337

		if ( vals[0].bv_len && strcasecmp( vals[0].bv_val, pw->pw_name )) {
338
			attr_mergeit( e, ad_cn, vals );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
339
340
		}

341
342
343
		if ( (s=strrchr(vals[0].bv_val, ' '))) {
			vals[0].bv_val = s + 1;
			vals[0].bv_len = strlen(vals[0].bv_val);
344
			attr_mergeit(e, ad_sn, vals);
345
346
		}
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
347
#endif
348

Kurt Zeilenga's avatar
Kurt Zeilenga committed
349
350
	return( e );
}