search.c 3.28 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* search.c - /etc/passwd backend search function */

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pwd.h>
#include "portable.h"
#include "slap.h"

extern time_t		currenttime;
extern pthread_mutex_t	currenttime_mutex;

static Entry	*pw2entry();

int
passwd_back_search(
    Backend	*be,
    Connection	*conn,
    Operation	*op,
    char	*base,
    int		scope,
    int		deref,
    int		slimit,
    int		tlimit,
    Filter	*filter,
    char	*filterstr,
    char	**attrs,
    int		attrsonly
)
{
	struct passwd	*pw;
	Entry		*e;
	char		*s;
	time_t		stoptime;

	tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
	    : tlimit;
	stoptime = op->o_time + tlimit;
	slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
	    : slimit;

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

	if ( scope == LDAP_SCOPE_BASE ) {
		if ( (s = strchr( base, '@' )) != NULL ) {
			*s = '\0';
		}

		if ( (pw = getpwnam( base )) == NULL ) {
			send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
			    s != NULL ? s + 1 : NULL, NULL );
			return( -1 );
		}

		e = pw2entry( be, pw );
		if ( test_filter( be, conn, op, e, filter ) == 0 ) {
			send_search_entry( be, conn, op, e, attrs, attrsonly );
		}
		entry_free( e );

		send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );

		return( 0 );
	}

	for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
		/* check for abandon */
		pthread_mutex_lock( &op->o_abandonmutex );
		if ( op->o_abandon ) {
			pthread_mutex_unlock( &op->o_abandonmutex );
			endpwent();
			return( -1 );
		}
		pthread_mutex_unlock( &op->o_abandonmutex );

		/* check time limit */
		pthread_mutex_lock( &currenttime_mutex );
		time( &currenttime );
		if ( currenttime > stoptime ) {
			pthread_mutex_unlock( &currenttime_mutex );
			send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
			    NULL, NULL );
			endpwent();
			return( 0 );
		}
		pthread_mutex_unlock( &currenttime_mutex );

		e = pw2entry( be, pw );

		if ( test_filter( be, conn, op, e, filter ) == 0 ) {
98
99
100
101
102
103
104
105
			/* check size limit */
			if ( --slimit == -1 ) {
				send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
				    NULL, NULL );
				endpwent();
				return( 0 );
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
			send_search_entry( be, conn, op, e, attrs, attrsonly );
		}

		entry_free( e );
	}
	endpwent();
	send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );

	return( 0 );
}

static Entry *
pw2entry( Backend *be, struct passwd *pw )
{
	Entry		*e;
	char		buf[256];
	struct berval	val;
	struct berval	*vals[2];

	vals[0] = &val;
	vals[1] = NULL;

	/*
	 * from pw we get pw_name and make it uid and cn and sn and
	 * we get pw_gecos and make it cn and we give it an objectclass
	 * of person.
	 */

	e = (Entry *) ch_calloc( 1, sizeof(Entry) );
	e->e_attrs = NULL;

	sprintf( buf, "%s@%s", pw->pw_name, be->be_suffix[0] );
	e->e_dn = strdup( buf );

	val.bv_val = pw->pw_name;
	val.bv_len = strlen( pw->pw_name );
	attr_merge( e, "cn", vals );
	attr_merge( e, "sn", vals );
	attr_merge( e, "uid", vals );
	val.bv_val = pw->pw_gecos;
	val.bv_len = strlen( pw->pw_gecos );
	attr_merge( e, "cn", vals );
	val.bv_val = "person";
	val.bv_len = strlen( val.bv_val );
	attr_merge( e, "objectclass", vals );

	return( e );
}