]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
Initial revision
[openldap] / servers / slapd / result.c
1 /* result.c - routines to send ldap results, errors, and referrals */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include "portable.h"
12 #include "slap.h"
13
14 #ifndef SYSERRLIST_IN_STDIO
15 extern int              sys_nerr;
16 extern char             *sys_errlist[];
17 #endif
18 extern int              active_threads;
19 extern pthread_mutex_t  active_threads_mutex;
20 extern pthread_mutex_t  new_conn_mutex;
21 extern pthread_t        listener_tid;
22 extern struct acl       *acl_get_applicable();
23 extern long             num_entries_sent;
24 extern long             num_bytes_sent;
25 extern pthread_mutex_t  num_sent_mutex;
26
27 void    close_connection();
28
29 static void
30 send_ldap_result2(
31     Connection  *conn,
32     Operation   *op,
33     int         err,
34     char        *matched,
35     char        *text,
36     int         nentries
37 )
38 {
39         BerElement      *ber;
40         int             rc, sd;
41         unsigned long   tag, bytes;
42
43         Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
44             matched : "", text ? text : "" );
45
46         switch ( op->o_tag ) {
47         case LBER_DEFAULT:
48                 tag = LBER_SEQUENCE;
49                 break;
50
51         case LDAP_REQ_SEARCH:
52                 tag = LDAP_RES_SEARCH_RESULT;
53                 break;
54
55         case LDAP_REQ_DELETE:
56                 tag = LDAP_RES_DELETE;
57                 break;
58
59         default:
60                 tag = op->o_tag + 1;
61                 break;
62         }
63
64 #ifdef COMPAT30
65         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
66             == NULLBER ) {
67 #else
68         if ( (ber = der_alloc()) == NULLBER ) {
69 #endif
70                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
71                 return;
72         }
73
74 #ifdef CLDAP
75         if ( op->o_cldap ) {
76                 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
77                     err, matched ? matched : "", text ? text : "" );
78         } else
79 #endif
80 #ifdef COMPAT30
81         if ( conn->c_version == 30 ) {
82                 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
83                     matched ? matched : "", text ? text : "" );
84         } else
85 #endif
86                 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
87                     matched ? matched : "", text ? text : "" );
88
89         if ( rc == -1 ) {
90                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
91                 return;
92         }
93
94         /* write only one pdu at a time - wait til it's our turn */
95         pthread_mutex_lock( &conn->c_pdumutex );
96
97         /* write the pdu */
98         bytes = ber->ber_ptr - ber->ber_buf;
99         pthread_mutex_lock( &new_conn_mutex );
100         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
101             1 ) != 0 ) {
102                 pthread_mutex_unlock( &new_conn_mutex );
103                 /*
104                  * we got an error.  if it's ewouldblock, we need to
105                  * wait on the socket being writable.  otherwise, figure
106                  * it's a hard error and return.
107                  */
108
109                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
110                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
111                     : "unknown", 0 );
112
113                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
114                         close_connection( conn, op->o_connid, op->o_opid );
115
116                         pthread_mutex_unlock( &conn->c_pdumutex );
117                         return;
118                 }
119
120                 /* wait for socket to be write-ready */
121                 pthread_mutex_lock( &active_threads_mutex );
122                 active_threads--;
123                 conn->c_writewaiter = 1;
124                 pthread_kill( listener_tid, SIGUSR1 );
125                 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
126                 pthread_mutex_unlock( &active_threads_mutex );
127
128                 pthread_yield();
129                 pthread_mutex_lock( &new_conn_mutex );
130         }
131         pthread_mutex_unlock( &new_conn_mutex );
132         pthread_mutex_unlock( &conn->c_pdumutex );
133
134         pthread_mutex_lock( &num_sent_mutex );
135         num_bytes_sent += bytes;
136         pthread_mutex_unlock( &num_sent_mutex );
137
138         Statslog( LDAP_DEBUG_STATS,
139             "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
140             op->o_opid, err, tag, nentries );
141
142         return;
143 }
144
145 void
146 send_ldap_result(
147     Connection  *conn,
148     Operation   *op,
149     int         err,
150     char        *matched,
151     char        *text
152 )
153 {
154 #ifdef CLDAP
155         if ( op->o_cldap ) {
156                 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
157                     sizeof( struct sockaddr ));
158                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
159                     inet_ntoa(((struct sockaddr_in *)
160                     conn->c_sb.sb_useaddr)->sin_addr ),
161                     ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
162                     0 );
163         }
164 #endif
165         send_ldap_result2( conn, op, err, matched, text, 0 );
166 }
167
168 void
169 send_ldap_search_result(
170     Connection  *conn,
171     Operation   *op,
172     int         err,
173     char        *matched,
174     char        *text,
175     int         nentries
176 )
177 {
178         send_ldap_result2( conn, op, err, matched, text, nentries );
179 }
180
181 int
182 send_search_entry(
183     Backend     *be,
184     Connection  *conn,
185     Operation   *op,
186     Entry       *e,
187     char        **attrs,
188     int         attrsonly
189 )
190 {
191         BerElement      *ber;
192         Attribute       *a;
193         int             i, rc, bytes, sd;
194         struct acl      *acl;
195
196         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
197
198         if ( ! access_allowed( be, conn, op, e, "entry", NULL, op->o_dn,
199             ACL_READ ) ) {
200                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
201                     0, 0, 0 );
202                 return( 1 );
203         }
204
205 #ifdef COMPAT30
206         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
207             == NULLBER ) {
208 #else
209         if ( (ber = der_alloc()) == NULLBER ) {
210 #endif
211                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
212                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
213                     "ber_alloc" );
214                 return( 1 );
215         }
216
217 #ifdef COMPAT30
218         if ( conn->c_version == 30 ) {
219                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
220                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
221         } else
222 #endif
223                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
224                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
225
226         if ( rc == -1 ) {
227                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
228                 ber_free( ber, 1 );
229                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
230                     "ber_printf dn" );
231                 return( 1 );
232         }
233
234         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
235                 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
236                         continue;
237                 }
238
239                 acl = acl_get_applicable( be, op, e, a->a_type );
240
241                 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op,
242                     ACL_READ ) ) {
243                         continue;
244                 }
245
246                 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
247                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
248                         ber_free( ber, 1 );
249                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
250                             NULL, "ber_printf type" );
251                         return( 1 );
252                 }
253
254                 if ( ! attrsonly ) {
255                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
256                                 if ( a->a_syntax & SYNTAX_DN &&
257                                     ! acl_access_allowed( acl, be, conn, e,
258                                     a->a_vals[i], op, ACL_READ ) )
259                                 {
260                                         continue;
261                                 }
262
263                                 if ( ber_printf( ber, "o",
264                                     a->a_vals[i]->bv_val,
265                                     a->a_vals[i]->bv_len ) == -1 )
266                                 {
267                                         Debug( LDAP_DEBUG_ANY,
268                                             "ber_printf failed\n", 0, 0, 0 );
269                                         ber_free( ber, 1 );
270                                         send_ldap_result( conn, op,
271                                             LDAP_OPERATIONS_ERROR, NULL,
272                                             "ber_printf value" );
273                                         return( 1 );
274                                 }
275                         }
276                 }
277
278                 if ( ber_printf( ber, "]}" ) == -1 ) {
279                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
280                         ber_free( ber, 1 );
281                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
282                             NULL, "ber_printf type end" );
283                         return( 1 );
284                 }
285         }
286
287 #ifdef COMPAT30
288         if ( conn->c_version == 30 ) {
289                 rc = ber_printf( ber, "}}}}" );
290         } else
291 #endif
292                 rc = ber_printf( ber, "}}}" );
293
294         if ( rc == -1 ) {
295                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
296                 ber_free( ber, 1 );
297                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
298                     "ber_printf entry end" );
299                 return( 1 );
300         }
301
302         /* write only one pdu at a time - wait til it's our turn */
303         pthread_mutex_lock( &conn->c_pdumutex );
304
305         bytes = ber->ber_ptr - ber->ber_buf;
306         pthread_mutex_lock( &new_conn_mutex );
307         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
308             1 ) != 0 ) {
309                 pthread_mutex_unlock( &new_conn_mutex );
310                 /*
311                  * we got an error.  if it's ewouldblock, we need to
312                  * wait on the socket being writable.  otherwise, figure
313                  * it's a hard error and return.
314                  */
315
316                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
317                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
318                     : "unknown", 0 );
319
320                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
321                         close_connection( conn, op->o_connid, op->o_opid );
322
323                         pthread_mutex_unlock( &conn->c_pdumutex );
324                         return( -1 );
325                 }
326
327                 /* wait for socket to be write-ready */
328                 pthread_mutex_lock( &active_threads_mutex );
329                 active_threads--;
330                 conn->c_writewaiter = 1;
331                 pthread_kill( listener_tid, SIGUSR1 );
332                 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
333                 pthread_mutex_unlock( &active_threads_mutex );
334
335                 pthread_yield();
336                 pthread_mutex_lock( &new_conn_mutex );
337         }
338         pthread_mutex_unlock( &new_conn_mutex );
339         pthread_mutex_unlock( &conn->c_pdumutex );
340
341         pthread_mutex_lock( &num_sent_mutex );
342         num_bytes_sent += bytes;
343         num_entries_sent++;
344         pthread_mutex_unlock( &num_sent_mutex );
345
346         pthread_mutex_lock( &new_conn_mutex );
347         if ( conn->c_connid == op->o_connid ) {
348                 rc = 0;
349                 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
350                     conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
351         } else {
352                 rc = -1;
353         }
354         pthread_mutex_unlock( &new_conn_mutex );
355
356         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
357
358         return( rc );
359 }
360
361 int
362 str2result(
363     char        *s,
364     int         *code,
365     char        **matched,
366     char        **info
367 )
368 {
369         int     rc;
370         char    *c;
371
372         *code = LDAP_SUCCESS;
373         *matched = NULL;
374         *info = NULL;
375
376         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
377                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
378                     s, 0, 0 );
379
380                 return( -1 );
381         }
382
383         rc = 0;
384         while ( (s = strchr( s, '\n' )) != NULL ) {
385                 *s++ = '\0';
386                 if ( *s == '\0' ) {
387                         break;
388                 }
389                 if ( (c = strchr( s, ':' )) != NULL ) {
390                         c++;
391                 }
392
393                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
394                         if ( c != NULL ) {
395                                 *code = atoi( c );
396                         }
397                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
398                         if ( c != NULL ) {
399                                 *matched = c;
400                         }
401                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
402                         if ( c != NULL ) {
403                                 *info = c;
404                         }
405                 } else {
406                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
407                             s, 0, 0 );
408                         rc = -1;
409                 }
410         }
411
412         return( rc );
413 }
414
415 /*
416  * close_connection - close a connection. takes the connection to close,
417  * the connid associated with the operation generating the close (so we
418  * don't accidentally close a connection that's not ours), and the opid
419  * of the operation generating the close (for logging purposes).
420  */
421 void
422 close_connection( Connection *conn, int opconnid, int opid )
423 {
424         pthread_mutex_lock( &new_conn_mutex );
425         if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
426                 Statslog( LDAP_DEBUG_STATS,
427                     "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
428                     opid, conn->c_sb.sb_sd, errno, 0 );
429                 close( conn->c_sb.sb_sd );
430                 conn->c_sb.sb_sd = -1;
431                 conn->c_version = 0;
432         }
433         pthread_mutex_unlock( &new_conn_mutex );
434 }