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