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