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