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