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