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