]> git.sur5r.net Git - openldap/blob - servers/slapd/back-passwd/search.c
bd61a8070cddda575c93f3ecb9f4f854fe1510f2
[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/ctype.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/time.h>
11
12 #include <pwd.h>
13
14 #include "slap.h"
15 #include "external.h"
16
17 static Entry *pw2entry(
18         Backend *be,
19         struct passwd *pw,
20         char *rdn);
21
22 int
23 passwd_back_search(
24     Backend     *be,
25     Connection  *conn,
26     Operation   *op,
27     char        *base,
28     int         scope,
29     int         deref,
30     int         slimit,
31     int         tlimit,
32     Filter      *filter,
33     char        *filterstr,
34     char        **attrs,
35     int         attrsonly
36 )
37 {
38         int sent = 0;
39         struct passwd   *pw;
40         Entry           *e;
41         char            *s;
42         time_t          stoptime;
43         int err = LDAP_NO_SUCH_OBJECT;
44
45         char *rdn = NULL;
46         char *parent = NULL;
47         char *matched = NULL;
48         char *user = NULL;
49
50         tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
51             : tlimit;
52         stoptime = op->o_time + tlimit;
53         slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
54             : slimit;
55
56         endpwent();
57
58 #ifdef HAVE_SETPWFILE
59         if ( be->be_private != NULL ) {
60                 (void) setpwfile( (char *) be->be_private );
61         }
62 #endif /* HAVE_SETPWFILE */
63
64         /* Handle a query for the base of this backend */
65         if ( be_issuffix( be,  base ) ) {
66                 struct berval   val, *vals[2];
67
68                 vals[0] = &val;
69                 vals[1] = NULL;
70
71                 /* Create an entry corresponding to the base DN */
72                 e = (Entry *) ch_calloc(1, sizeof(Entry));
73                 e->e_attrs = NULL;
74                 e->e_dn = strdup(base);
75
76                 /* Use the first attribute of the DN
77                  * as an attribute within the entry itself.
78                  */
79                 rdn = dn_rdn(NULL, base);
80
81                 if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
82                         err = LDAP_INVALID_DN_SYNTAX;
83                         goto done;
84                 }
85
86                 val.bv_val = rdn_attr_value(rdn);
87                 val.bv_len = strlen( val.bv_val );
88                 attr_merge( e, rdn_attr_type(rdn), vals );
89
90                 free(rdn);
91                 rdn = NULL;
92
93                 /* Every entry needs an objectclass. We don't really
94                  * know if our hardcoded choice here agrees with the
95                  * DN that was configured for this backend, but it's
96                  * better than nothing.
97                  *
98                  * should be a configuratable item
99                  */
100                 val.bv_val = "organizationalUnit";
101                 val.bv_len = strlen( val.bv_val );
102                 attr_merge( e, "objectClass", vals );
103         
104                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
105                         send_search_entry( be, conn, op, e, attrs, attrsonly );
106                         matched = strdup( be->be_suffix[0] );
107                         sent++;
108                 }
109
110                 if ( scope != LDAP_SCOPE_BASE ) {
111                         /* check all our "children" */
112
113                         for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
114                                 /* check for abandon */
115                                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
116                                 if ( op->o_abandon ) {
117                                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
118                                         endpwent();
119                                         return( -1 );
120                                 }
121                                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
122
123                                 /* check time limit */
124                                 if ( slap_get_time() > stoptime ) {
125                                         send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
126                                         NULL, NULL );
127                                         endpwent();
128                                         return( 0 );
129                                 }
130
131                                 e = pw2entry( be, pw, NULL );
132
133                                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
134                                         /* check size limit */
135                                         if ( --slimit == -1 ) {
136                                                 send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
137                                                 NULL, NULL );
138                                                 endpwent();
139                                                 return( 0 );
140                                         }
141
142                                         send_search_entry( be, conn, op, e, attrs, attrsonly, 0 );
143                                         sent++;
144                                 }
145
146                                 entry_free( e );
147                         }
148                         endpwent();
149                 }
150
151         } else {
152                 parent = dn_parent( be, base );
153
154                 /* This backend is only one layer deep. Don't answer requests for
155                  * anything deeper than that.
156                  */
157                 if( !be_issuffix( be, parent ) ) {
158                         goto done;
159                 }
160
161                 rdn = dn_rdn( NULL, base );
162
163                 if ( (user = rdn_attr_value(rdn)) == NULL) {
164                         err = LDAP_INVALID_DN_SYNTAX;
165                         goto done;
166                 }
167
168                 for( s = user; *s ; s++ ) {
169                         *s = TOLOWER( *s );
170                 }
171
172                 if ( (pw = getpwnam( user )) == NULL ) {
173                         goto done;
174                 }
175
176                 e = pw2entry( be, pw, rdn );
177
178                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
179                         send_search_entry( be, conn, op, e, attrs, attrsonly, 0 );
180                         sent++;
181                 }
182
183                 entry_free( e );
184         }
185
186 done:
187         if( sent ) {
188                 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
189
190         } else {
191                 send_ldap_result( conn, op, err, matched, NULL );
192         }
193
194         if( matched != NULL ) free( matched );
195         if( parent != NULL ) free( parent );
196         if( rdn != NULL ) free( rdn );
197         if( user != NULL ) free( user );
198
199         return( 0 );
200 }
201
202 static Entry *
203 pw2entry( Backend *be, struct passwd *pw, char *rdn )
204 {
205         Entry           *e;
206         char            buf[256];
207         struct berval   val;
208         struct berval   *vals[2];
209
210         vals[0] = &val;
211         vals[1] = NULL;
212
213         /*
214          * from pw we get pw_name and make it cn
215          * give it an objectclass of person.
216          */
217
218         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
219         e->e_attrs = NULL;
220
221         /* rdn attribute type should be a configuratable item */
222         sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
223         e->e_dn = ch_strdup( buf );
224         e->e_ndn = dn_normalize_case( ch_strdup( buf ) );
225
226         val.bv_val = pw->pw_name;
227         val.bv_len = strlen( pw->pw_name );
228         attr_merge( e, "uid", vals );   /* required by uidObject */
229         attr_merge( e, "cn", vals );    /* required by person */
230         attr_merge( e, "sn", vals );    /* required by person */
231
232 #ifdef HAVE_PW_GECOS
233         /*
234          * if gecos is present, add it as a cn. first process it
235          * according to standard BSD usage. If the processed cn has
236          * a space, use the tail as the surname.
237          */
238         if (pw->pw_gecos[0]) {
239                 char *s;
240
241                 val.bv_val = pw->pw_gecos;
242                 val.bv_len = strlen(val.bv_val);
243                 attr_merge(e, "description", vals);
244
245                 s = strchr(val.bv_val, ',');
246                 if (s)
247                         *s = '\0';
248                 s = strchr(val.bv_val, '&');
249                 if (s) {
250                         int i = s - val.bv_val;
251                         strncpy(buf, val.bv_val, i);
252                         s = buf+i;
253                         strcpy(s, pw->pw_name);
254                         if (islower(*s))
255                                 *s = toupper(*s);
256                         strcat(s, val.bv_val+i+1);
257                         val.bv_val = buf;
258                 }
259                 val.bv_len = strlen(val.bv_val);
260                 if ( strcmp( val.bv_val, pw->pw_name ))
261                         attr_merge( e, "cn", vals );
262                 if ( (s=strrchr(val.bv_val, ' '))) {
263                         val.bv_val = s + 1;
264                         val.bv_len = strlen(val.bv_val);
265                         attr_merge(e, "sn", vals);
266                 }
267         }
268 #endif
269
270         /* objectclasses should be configuratable items */
271         val.bv_val = "person";
272         val.bv_len = strlen( val.bv_val );
273         attr_merge( e, "objectclass", vals );
274
275         val.bv_val = "uidObject";
276         val.bv_len = strlen( val.bv_val );
277         attr_merge( e, "objectclass", vals );
278         return( e );
279 }