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