]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
6c12f949afb333bcab0c7142fedac0c827e0ca8b
[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                 if ( slap_get_time() > stoptime ) {
86                         send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
87                             NULL, NULL );
88                         endpwent();
89                         return( 0 );
90                 }
91
92                 e = pw2entry( be, pw );
93
94                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
95                         /* check size limit */
96                         if ( --slimit == -1 ) {
97                                 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
98                                     NULL, NULL );
99                                 endpwent();
100                                 return( 0 );
101                         }
102
103                         send_search_entry( be, conn, op, e, attrs, attrsonly );
104                 }
105
106                 entry_free( e );
107         }
108         endpwent();
109         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
110
111         return( 0 );
112 }
113
114 static Entry *
115 pw2entry( Backend *be, struct passwd *pw )
116 {
117         Entry           *e;
118         char            buf[256];
119         struct berval   val;
120         struct berval   *vals[2];
121
122         vals[0] = &val;
123         vals[1] = NULL;
124
125         /*
126          * from pw we get pw_name and make it uid and cn and sn and
127          * we get pw_gecos and make it cn and we give it an objectclass
128          * of person.
129          */
130
131         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
132         e->e_attrs = NULL;
133
134         sprintf( buf, "%s@%s", pw->pw_name, be->be_suffix[0] );
135         e->e_dn = ch_strdup( buf );
136         e->e_ndn = dn_normalize_case( ch_strdup( buf ) );
137
138         val.bv_val = pw->pw_name;
139         val.bv_len = strlen( pw->pw_name );
140         attr_merge( e, "cn", vals );
141         attr_merge( e, "sn", vals );
142         attr_merge( e, "uid", vals );
143 #ifdef HAVE_PW_GECOS
144         val.bv_val = pw->pw_gecos;
145         val.bv_len = strlen( pw->pw_gecos );
146         attr_merge( e, "cn", vals );
147 #endif
148         val.bv_val = "person";
149         val.bv_len = strlen( val.bv_val );
150         attr_merge( e, "objectclass", vals );
151
152         return( e );
153 }