]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
3e9a0f9a8c8dbcc63a19d1ccc398db95068461b5
[openldap] / servers / slapd / back-passwd / search.c
1 /* search.c - /etc/passwd backend search function */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/time.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <pwd.h>
11 #include "portable.h"
12 #include "slap.h"
13
14 extern time_t           currenttime;
15 extern pthread_mutex_t  currenttime_mutex;
16
17 static Entry    *pw2entry();
18
19 int
20 passwd_back_search(
21     Backend     *be,
22     Connection  *conn,
23     Operation   *op,
24     char        *base,
25     int         scope,
26     int         deref,
27     int         slimit,
28     int         tlimit,
29     Filter      *filter,
30     char        *filterstr,
31     char        **attrs,
32     int         attrsonly
33 )
34 {
35         struct passwd   *pw;
36         Entry           *e;
37         char            *s;
38         time_t          stoptime;
39
40         tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
41             : tlimit;
42         stoptime = op->o_time + tlimit;
43         slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
44             : slimit;
45
46 #ifdef HAVE_SETPWFILE
47         if ( be->be_private != NULL ) {
48                 endpwent();
49                 (void) setpwfile( (char *) be->be_private );
50         }
51 #endif /* HAVE_SETPWFILE */
52
53         if ( scope == LDAP_SCOPE_BASE ) {
54                 if ( (s = strchr( base, '@' )) != NULL ) {
55                         *s = '\0';
56                 }
57
58                 if ( (pw = getpwnam( base )) == NULL ) {
59                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
60                             s != NULL ? s + 1 : NULL, NULL );
61                         return( -1 );
62                 }
63
64                 e = pw2entry( be, pw );
65                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
66                         send_search_entry( be, conn, op, e, attrs, attrsonly );
67                 }
68                 entry_free( e );
69
70                 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
71
72                 return( 0 );
73         }
74
75         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
76                 /* check for abandon */
77                 pthread_mutex_lock( &op->o_abandonmutex );
78                 if ( op->o_abandon ) {
79                         pthread_mutex_unlock( &op->o_abandonmutex );
80                         endpwent();
81                         return( -1 );
82                 }
83                 pthread_mutex_unlock( &op->o_abandonmutex );
84
85                 /* check size limit */
86                 if ( --slimit == -1 ) {
87                         send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
88                             NULL, NULL );
89                         endpwent();
90                         return( 0 );
91                 }
92
93                 /* check time limit */
94                 pthread_mutex_lock( &currenttime_mutex );
95                 time( &currenttime );
96                 if ( currenttime > stoptime ) {
97                         pthread_mutex_unlock( &currenttime_mutex );
98                         send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
99                             NULL, NULL );
100                         endpwent();
101                         return( 0 );
102                 }
103                 pthread_mutex_unlock( &currenttime_mutex );
104
105                 e = pw2entry( be, pw );
106
107                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
108                         send_search_entry( be, conn, op, e, attrs, attrsonly );
109                 }
110
111                 entry_free( e );
112         }
113         endpwent();
114         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
115
116         return( 0 );
117 }
118
119 static Entry *
120 pw2entry( Backend *be, struct passwd *pw )
121 {
122         Entry           *e;
123         char            buf[256];
124         struct berval   val;
125         struct berval   *vals[2];
126
127         vals[0] = &val;
128         vals[1] = NULL;
129
130         /*
131          * from pw we get pw_name and make it uid and cn and sn and
132          * we get pw_gecos and make it cn and we give it an objectclass
133          * of person.
134          */
135
136         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
137         e->e_attrs = NULL;
138
139         sprintf( buf, "%s@%s", pw->pw_name, be->be_suffix[0] );
140         e->e_dn = strdup( buf );
141
142         val.bv_val = pw->pw_name;
143         val.bv_len = strlen( pw->pw_name );
144         attr_merge( e, "cn", vals );
145         attr_merge( e, "sn", vals );
146         attr_merge( e, "uid", vals );
147         val.bv_val = pw->pw_gecos;
148         val.bv_len = strlen( pw->pw_gecos );
149         attr_merge( e, "cn", vals );
150         val.bv_val = "person";
151         val.bv_len = strlen( val.bv_val );
152         attr_merge( e, "objectclass", vals );
153
154         return( e );
155 }