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