]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
Fix *=+=* bug.
[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                 ber_free( ber, 1 );
83                 return;
84         }
85
86         /* write only one pdu at a time - wait til it's our turn */
87         ldap_pvt_thread_mutex_lock( &conn->c_pdumutex );
88
89         /* write the pdu */
90         bytes = ber->ber_ptr - ber->ber_buf;
91         ldap_pvt_thread_mutex_lock( &new_conn_mutex );
92         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
93             0 ) != 0 ) {
94                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
95                 /*
96                  * we got an error.  if it's ewouldblock, we need to
97                  * wait on the socket being writable.  otherwise, figure
98                  * it's a hard error and return.
99                  */
100
101                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
102                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
103                     : "unknown", 0 );
104
105                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
106                         close_connection( conn, op->o_connid, op->o_opid );
107
108                         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
109                         ber_free( ber, 1 );
110                         return;
111                 }
112
113                 /* wait for socket to be write-ready */
114                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
115                 active_threads--;
116                 conn->c_writewaiter = 1;
117
118                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
119
120                 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
121
122                 if( active_threads < 1 ) {
123                         ldap_pvt_thread_cond_signal(&active_threads_cond);
124                 }
125                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
126
127                 ldap_pvt_thread_yield();
128                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
129         }
130         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
131         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
132
133         ber_free( ber, 1 );
134
135         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
136         num_bytes_sent += bytes;
137         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
138
139         Statslog( LDAP_DEBUG_STATS,
140             "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
141             op->o_opid, err, tag, nentries );
142
143         return;
144 }
145
146 void
147 send_ldap_result(
148     Connection  *conn,
149     Operation   *op,
150     int         err,
151     char        *matched,
152     char        *text
153 )
154 {
155 #ifdef LDAP_CONNECTIONLESS
156         if ( op->o_cldap ) {
157                 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
158                     sizeof( struct sockaddr ));
159                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
160                     inet_ntoa(((struct sockaddr_in *)
161                     conn->c_sb.sb_useaddr)->sin_addr ),
162                     ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
163                     0 );
164         }
165 #endif
166         send_ldap_result2( conn, op, err, matched, text, 0 );
167 }
168
169 void
170 send_ldap_search_result(
171     Connection  *conn,
172     Operation   *op,
173     int         err,
174     char        *matched,
175     char        *text,
176     int         nentries
177 )
178 {
179         send_ldap_result2( conn, op, err, matched, text, nentries );
180 }
181
182 int
183 send_search_entry(
184     Backend     *be,
185     Connection  *conn,
186     Operation   *op,
187     Entry       *e,
188     char        **attrs,
189     int         attrsonly
190 )
191 {
192         BerElement      *ber;
193         Attribute       *a;
194         int             i, rc, bytes, sd;
195         struct acl      *acl;
196         char            *edn;
197         int allattrs;
198
199         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
200
201         if ( ! access_allowed( be, conn, op, e,
202                 "entry", NULL, ACL_READ ) )
203         {
204                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
205                     0, 0, 0 );
206                 return( 1 );
207         }
208
209         edn = e->e_ndn;
210
211 #ifdef LDAP_COMPAT30
212         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
213                 == NULLBER )
214 #else
215         if ( (ber = der_alloc()) == NULLBER )
216 #endif
217         {
218                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
219                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
220                         "ber_alloc" );
221                 goto error_return;
222         }
223
224 #ifdef LDAP_COMPAT30
225         if ( conn->c_version == 30 ) {
226                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
227                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
228         } else
229 #endif
230         {
231                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
232                         LDAP_RES_SEARCH_ENTRY, e->e_dn );
233         }
234
235         if ( rc == -1 ) {
236                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
237                 ber_free( ber, 1 );
238                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
239                     "ber_printf dn" );
240                 goto error_return;
241         }
242
243         allattrs = ( (attrs == NULL) || charray_inlist( attrs, "*" ) );
244
245         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
246                 regmatch_t       matches[MAXREMATCHES];
247
248                 if ( !allattrs && !charray_inlist( attrs, a->a_type ) ) {
249                         continue;
250                 }
251
252                 /* the lastmod attributes are ignored by ACL checking */
253                 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
254                         strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
255                         strcasecmp( a->a_type, "creatorsname" ) == 0 ||
256                         strcasecmp( a->a_type, "createtimestamp" ) == 0 ) 
257                 {
258                         Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
259                                 a->a_type, 0, 0 );
260                         acl = NULL;
261                 } else {
262                         acl = acl_get_applicable( be, op, e, a->a_type,
263                                 MAXREMATCHES, matches );
264                 }
265
266                 if ( ! acl_access_allowed( acl, be, conn, e,
267                         NULL, op, ACL_READ, edn, matches ) ) 
268                 {
269                         continue;
270                 }
271
272                 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
273                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
274                         ber_free( ber, 1 );
275                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
276                             NULL, "ber_printf type" );
277                         goto error_return;
278                 }
279
280                 if ( ! attrsonly ) {
281                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
282                                 if ( a->a_syntax & SYNTAX_DN && 
283                                         ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
284                                                 ACL_READ, edn, matches) )
285                                 {
286                                         continue;
287                                 }
288
289                                 if ( ber_printf( ber, "o",
290                                     a->a_vals[i]->bv_val,
291                                     a->a_vals[i]->bv_len ) == -1 )
292                                 {
293                                         Debug( LDAP_DEBUG_ANY,
294                                             "ber_printf failed\n", 0, 0, 0 );
295                                         ber_free( ber, 1 );
296                                         send_ldap_result( conn, op,
297                                             LDAP_OPERATIONS_ERROR, NULL,
298                                             "ber_printf value" );
299                                         goto error_return;
300                                 }
301                         }
302                 }
303
304                 if ( ber_printf( ber, "]}" ) == -1 ) {
305                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
306                         ber_free( ber, 1 );
307                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
308                             NULL, "ber_printf type end" );
309                         goto error_return;
310                 }
311         }
312
313 #ifdef LDAP_COMPAT30
314         if ( conn->c_version == 30 ) {
315                 rc = ber_printf( ber, "}}}}" );
316         } else
317 #endif
318                 rc = ber_printf( ber, "}}}" );
319
320         if ( rc == -1 ) {
321                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
322                 ber_free( ber, 1 );
323                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
324                     "ber_printf entry end" );
325                 return( 1 );
326         }
327
328         /* write only one pdu at a time - wait til it's our turn */
329         ldap_pvt_thread_mutex_lock( &conn->c_pdumutex );
330
331         bytes = ber->ber_ptr - ber->ber_buf;
332         ldap_pvt_thread_mutex_lock( &new_conn_mutex );
333         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
334             0 ) != 0 ) {
335                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
336                 /*
337                  * we got an error.  if it's ewouldblock, we need to
338                  * wait on the socket being writable.  otherwise, figure
339                  * it's a hard error and return.
340                  */
341
342                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
343                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
344                     : "unknown", 0 );
345
346                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
347                         close_connection( conn, op->o_connid, op->o_opid );
348
349                         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
350                         ber_free( ber, 1 );
351                         return( -1 );
352                 }
353
354                 /* wait for socket to be write-ready */
355                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
356                 active_threads--;
357                 conn->c_writewaiter = 1;
358                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
359                 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
360
361                 if( active_threads < 1 ) {
362                         ldap_pvt_thread_cond_signal(&active_threads_cond);
363                 }
364                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
365
366                 ldap_pvt_thread_yield();
367                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
368         }
369         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
370         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
371
372         ber_free( ber, 1 );
373
374         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
375         num_bytes_sent += bytes;
376         num_entries_sent++;
377         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
378
379         ldap_pvt_thread_mutex_lock( &new_conn_mutex );
380         if ( conn->c_connid == op->o_connid ) {
381                 rc = 0;
382                 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
383                     conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
384         } else {
385                 rc = -1;
386         }
387         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
388
389         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
390
391         return( rc );
392
393 error_return:;
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         ldap_pvt_thread_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         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
470 }