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