]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
integrated changed from rage.net glibc.patch
[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 #ifdef SIGSTKFLT
125                 pthread_kill( listener_tid, SIGSTKFLT );
126 #else
127                 pthread_kill( listener_tid, SIGUSR1 );
128 #endif
129                 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
130                 pthread_mutex_unlock( &active_threads_mutex );
131
132                 pthread_yield();
133                 pthread_mutex_lock( &new_conn_mutex );
134         }
135         pthread_mutex_unlock( &new_conn_mutex );
136         pthread_mutex_unlock( &conn->c_pdumutex );
137
138         pthread_mutex_lock( &num_sent_mutex );
139         num_bytes_sent += bytes;
140         pthread_mutex_unlock( &num_sent_mutex );
141
142         Statslog( LDAP_DEBUG_STATS,
143             "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
144             op->o_opid, err, tag, nentries );
145
146         return;
147 }
148
149 void
150 send_ldap_result(
151     Connection  *conn,
152     Operation   *op,
153     int         err,
154     char        *matched,
155     char        *text
156 )
157 {
158 #ifdef CLDAP
159         if ( op->o_cldap ) {
160                 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
161                     sizeof( struct sockaddr ));
162                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
163                     inet_ntoa(((struct sockaddr_in *)
164                     conn->c_sb.sb_useaddr)->sin_addr ),
165                     ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
166                     0 );
167         }
168 #endif
169         send_ldap_result2( conn, op, err, matched, text, 0 );
170 }
171
172 void
173 send_ldap_search_result(
174     Connection  *conn,
175     Operation   *op,
176     int         err,
177     char        *matched,
178     char        *text,
179     int         nentries
180 )
181 {
182         send_ldap_result2( conn, op, err, matched, text, nentries );
183 }
184
185 int
186 send_search_entry(
187     Backend     *be,
188     Connection  *conn,
189     Operation   *op,
190     Entry       *e,
191     char        **attrs,
192     int         attrsonly
193 )
194 {
195         BerElement      *ber;
196         Attribute       *a;
197         int             i, rc, bytes, sd;
198         struct acl      *acl;
199
200         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
201
202         if ( ! access_allowed( be, conn, op, e, "entry", NULL, op->o_dn,
203             ACL_READ ) ) {
204                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
205                     0, 0, 0 );
206                 return( 1 );
207         }
208
209 #ifdef COMPAT30
210         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
211             == NULLBER ) {
212 #else
213         if ( (ber = der_alloc()) == NULLBER ) {
214 #endif
215                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
216                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
217                     "ber_alloc" );
218                 return( 1 );
219         }
220
221 #ifdef COMPAT30
222         if ( conn->c_version == 30 ) {
223                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
224                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
225         } else
226 #endif
227                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
228                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
229
230         if ( rc == -1 ) {
231                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
232                 ber_free( ber, 1 );
233                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
234                     "ber_printf dn" );
235                 return( 1 );
236         }
237
238         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
239                 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
240                         continue;
241                 }
242
243                 acl = acl_get_applicable( be, op, e, a->a_type );
244
245                 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op,
246                     ACL_READ ) ) {
247                         continue;
248                 }
249
250                 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
251                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
252                         ber_free( ber, 1 );
253                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
254                             NULL, "ber_printf type" );
255                         return( 1 );
256                 }
257
258                 if ( ! attrsonly ) {
259                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
260                                 if ( a->a_syntax & SYNTAX_DN &&
261                                     ! acl_access_allowed( acl, be, conn, e,
262                                     a->a_vals[i], op, ACL_READ ) )
263                                 {
264                                         continue;
265                                 }
266
267                                 if ( ber_printf( ber, "o",
268                                     a->a_vals[i]->bv_val,
269                                     a->a_vals[i]->bv_len ) == -1 )
270                                 {
271                                         Debug( LDAP_DEBUG_ANY,
272                                             "ber_printf failed\n", 0, 0, 0 );
273                                         ber_free( ber, 1 );
274                                         send_ldap_result( conn, op,
275                                             LDAP_OPERATIONS_ERROR, NULL,
276                                             "ber_printf value" );
277                                         return( 1 );
278                                 }
279                         }
280                 }
281
282                 if ( ber_printf( ber, "]}" ) == -1 ) {
283                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
284                         ber_free( ber, 1 );
285                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
286                             NULL, "ber_printf type end" );
287                         return( 1 );
288                 }
289         }
290
291 #ifdef COMPAT30
292         if ( conn->c_version == 30 ) {
293                 rc = ber_printf( ber, "}}}}" );
294         } else
295 #endif
296                 rc = ber_printf( ber, "}}}" );
297
298         if ( rc == -1 ) {
299                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
300                 ber_free( ber, 1 );
301                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
302                     "ber_printf entry end" );
303                 return( 1 );
304         }
305
306         /* write only one pdu at a time - wait til it's our turn */
307         pthread_mutex_lock( &conn->c_pdumutex );
308
309         bytes = ber->ber_ptr - ber->ber_buf;
310         pthread_mutex_lock( &new_conn_mutex );
311         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
312             1 ) != 0 ) {
313                 pthread_mutex_unlock( &new_conn_mutex );
314                 /*
315                  * we got an error.  if it's ewouldblock, we need to
316                  * wait on the socket being writable.  otherwise, figure
317                  * it's a hard error and return.
318                  */
319
320                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
321                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
322                     : "unknown", 0 );
323
324                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
325                         close_connection( conn, op->o_connid, op->o_opid );
326
327                         pthread_mutex_unlock( &conn->c_pdumutex );
328                         return( -1 );
329                 }
330
331                 /* wait for socket to be write-ready */
332                 pthread_mutex_lock( &active_threads_mutex );
333                 active_threads--;
334                 conn->c_writewaiter = 1;
335                 pthread_kill( listener_tid, SIGUSR1 );
336                 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
337                 pthread_mutex_unlock( &active_threads_mutex );
338
339                 pthread_yield();
340                 pthread_mutex_lock( &new_conn_mutex );
341         }
342         pthread_mutex_unlock( &new_conn_mutex );
343         pthread_mutex_unlock( &conn->c_pdumutex );
344
345         pthread_mutex_lock( &num_sent_mutex );
346         num_bytes_sent += bytes;
347         num_entries_sent++;
348         pthread_mutex_unlock( &num_sent_mutex );
349
350         pthread_mutex_lock( &new_conn_mutex );
351         if ( conn->c_connid == op->o_connid ) {
352                 rc = 0;
353                 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
354                     conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
355         } else {
356                 rc = -1;
357         }
358         pthread_mutex_unlock( &new_conn_mutex );
359
360         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
361
362         return( rc );
363 }
364
365 int
366 str2result(
367     char        *s,
368     int         *code,
369     char        **matched,
370     char        **info
371 )
372 {
373         int     rc;
374         char    *c;
375
376         *code = LDAP_SUCCESS;
377         *matched = NULL;
378         *info = NULL;
379
380         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
381                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
382                     s, 0, 0 );
383
384                 return( -1 );
385         }
386
387         rc = 0;
388         while ( (s = strchr( s, '\n' )) != NULL ) {
389                 *s++ = '\0';
390                 if ( *s == '\0' ) {
391                         break;
392                 }
393                 if ( (c = strchr( s, ':' )) != NULL ) {
394                         c++;
395                 }
396
397                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
398                         if ( c != NULL ) {
399                                 *code = atoi( c );
400                         }
401                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
402                         if ( c != NULL ) {
403                                 *matched = c;
404                         }
405                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
406                         if ( c != NULL ) {
407                                 *info = c;
408                         }
409                 } else {
410                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
411                             s, 0, 0 );
412                         rc = -1;
413                 }
414         }
415
416         return( rc );
417 }
418
419 /*
420  * close_connection - close a connection. takes the connection to close,
421  * the connid associated with the operation generating the close (so we
422  * don't accidentally close a connection that's not ours), and the opid
423  * of the operation generating the close (for logging purposes).
424  */
425 void
426 close_connection( Connection *conn, int opconnid, int opid )
427 {
428         pthread_mutex_lock( &new_conn_mutex );
429         if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
430                 Statslog( LDAP_DEBUG_STATS,
431                     "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
432                     opid, conn->c_sb.sb_sd, errno, 0 );
433                 close( conn->c_sb.sb_sd );
434                 conn->c_sb.sb_sd = -1;
435                 conn->c_version = 0;
436         }
437         pthread_mutex_unlock( &new_conn_mutex );
438 }