]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
cbbf620ada5505deedded7ff8b414a43168b7ab6
[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                 free(edn);
228                 return( 1 );
229         }
230
231 #ifdef COMPAT30
232         if ( conn->c_version == 30 ) {
233                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
234                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
235         } else
236 #endif
237         {
238                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
239                         LDAP_RES_SEARCH_ENTRY, e->e_dn );
240         }
241
242         if ( rc == -1 ) {
243                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
244                 ber_free( ber, 1 );
245                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
246                     "ber_printf dn" );
247                 free(edn);
248                 return( 1 );
249         }
250
251         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
252                 regmatch_t       matches[MAXREMATCHES];
253
254                 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
255                         continue;
256                 }
257
258                 /* the lastmod attributes are ignored by ACL checking */
259                 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
260                         strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
261                         strcasecmp( a->a_type, "creatorsname" ) == 0 ||
262                         strcasecmp( a->a_type, "createtimestamp" ) == 0 ) 
263                 {
264                         Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
265                                 a->a_type, 0, 0 );
266                         acl = NULL;
267                 } else {
268                         acl = acl_get_applicable( be, op, e, a->a_type, edn,
269                                 MAXREMATCHES, matches );
270                 }
271
272                 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op, ACL_READ,
273                         edn, matches ) ) 
274                 {
275                         continue;
276                 }
277
278                 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
279                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
280                         ber_free( ber, 1 );
281                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
282                             NULL, "ber_printf type" );
283                         free(edn);
284                         return( 1 );
285                 }
286
287                 if ( ! attrsonly ) {
288                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
289                                 if ( a->a_syntax & SYNTAX_DN && 
290                                         ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
291                                                 ACL_READ, edn, matches) )
292                                 {
293                                         continue;
294                                 }
295
296                                 if ( ber_printf( ber, "o",
297                                     a->a_vals[i]->bv_val,
298                                     a->a_vals[i]->bv_len ) == -1 )
299                                 {
300                                         Debug( LDAP_DEBUG_ANY,
301                                             "ber_printf failed\n", 0, 0, 0 );
302                                         ber_free( ber, 1 );
303                                         send_ldap_result( conn, op,
304                                             LDAP_OPERATIONS_ERROR, NULL,
305                                             "ber_printf value" );
306                                         free(edn);
307                                         return( 1 );
308                                 }
309                         }
310                 }
311
312                 if ( ber_printf( ber, "]}" ) == -1 ) {
313                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
314                         ber_free( ber, 1 );
315                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
316                             NULL, "ber_printf type end" );
317                         free(edn);
318                         return( 1 );
319                 }
320         }
321
322         free(edn);
323
324 #ifdef COMPAT30
325         if ( conn->c_version == 30 ) {
326                 rc = ber_printf( ber, "}}}}" );
327         } else
328 #endif
329                 rc = ber_printf( ber, "}}}" );
330
331         if ( rc == -1 ) {
332                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
333                 ber_free( ber, 1 );
334                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
335                     "ber_printf entry end" );
336                 return( 1 );
337         }
338
339         /* write only one pdu at a time - wait til it's our turn */
340         pthread_mutex_lock( &conn->c_pdumutex );
341
342         bytes = ber->ber_ptr - ber->ber_buf;
343         pthread_mutex_lock( &new_conn_mutex );
344         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
345             1 ) != 0 ) {
346                 pthread_mutex_unlock( &new_conn_mutex );
347                 /*
348                  * we got an error.  if it's ewouldblock, we need to
349                  * wait on the socket being writable.  otherwise, figure
350                  * it's a hard error and return.
351                  */
352
353                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
354                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
355                     : "unknown", 0 );
356
357                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
358                         close_connection( conn, op->o_connid, op->o_opid );
359
360                         pthread_mutex_unlock( &conn->c_pdumutex );
361                         return( -1 );
362                 }
363
364                 /* wait for socket to be write-ready */
365                 pthread_mutex_lock( &active_threads_mutex );
366                 active_threads--;
367                 conn->c_writewaiter = 1;
368                 pthread_kill( listener_tid, SIGUSR1 );
369                 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
370                 pthread_mutex_unlock( &active_threads_mutex );
371
372                 pthread_yield();
373                 pthread_mutex_lock( &new_conn_mutex );
374         }
375         pthread_mutex_unlock( &new_conn_mutex );
376         pthread_mutex_unlock( &conn->c_pdumutex );
377
378         pthread_mutex_lock( &num_sent_mutex );
379         num_bytes_sent += bytes;
380         num_entries_sent++;
381         pthread_mutex_unlock( &num_sent_mutex );
382
383         pthread_mutex_lock( &new_conn_mutex );
384         if ( conn->c_connid == op->o_connid ) {
385                 rc = 0;
386                 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
387                     conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
388         } else {
389                 rc = -1;
390         }
391         pthread_mutex_unlock( &new_conn_mutex );
392
393         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
394
395         return( rc );
396 }
397
398 int
399 str2result(
400     char        *s,
401     int         *code,
402     char        **matched,
403     char        **info
404 )
405 {
406         int     rc;
407         char    *c;
408
409         *code = LDAP_SUCCESS;
410         *matched = NULL;
411         *info = NULL;
412
413         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
414                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
415                     s, 0, 0 );
416
417                 return( -1 );
418         }
419
420         rc = 0;
421         while ( (s = strchr( s, '\n' )) != NULL ) {
422                 *s++ = '\0';
423                 if ( *s == '\0' ) {
424                         break;
425                 }
426                 if ( (c = strchr( s, ':' )) != NULL ) {
427                         c++;
428                 }
429
430                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
431                         if ( c != NULL ) {
432                                 *code = atoi( c );
433                         }
434                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
435                         if ( c != NULL ) {
436                                 *matched = c;
437                         }
438                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
439                         if ( c != NULL ) {
440                                 *info = c;
441                         }
442                 } else {
443                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
444                             s, 0, 0 );
445                         rc = -1;
446                 }
447         }
448
449         return( rc );
450 }
451
452 /*
453  * close_connection - close a connection. takes the connection to close,
454  * the connid associated with the operation generating the close (so we
455  * don't accidentally close a connection that's not ours), and the opid
456  * of the operation generating the close (for logging purposes).
457  */
458 void
459 close_connection( Connection *conn, int opconnid, int opid )
460 {
461         pthread_mutex_lock( &new_conn_mutex );
462         if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
463                 Statslog( LDAP_DEBUG_STATS,
464                     "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
465                     opid, conn->c_sb.sb_sd, errno, 0 );
466                 close( conn->c_sb.sb_sd );
467                 conn->c_sb.sb_sd = -1;
468                 conn->c_version = 0;
469         }
470         pthread_mutex_unlock( &new_conn_mutex );
471 }