]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
f7320bdf8be29a9c2909b82df40bc02984e36c83
[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                 int err = errno;
95                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
96                 /*
97                  * we got an error.  if it's ewouldblock, we need to
98                  * wait on the socket being writable.  otherwise, figure
99                  * it's a hard error and return.
100                  */
101
102                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
103                     err, err > -1 && err < sys_nerr ? sys_errlist[err]
104                     : "unknown", 0 );
105
106                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
107                         close_connection( conn, op->o_connid, op->o_opid );
108
109                         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
110                         ber_free( ber, 1 );
111                         return;
112                 }
113
114                 /* wait for socket to be write-ready */
115                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
116                 active_threads--;
117                 conn->c_writewaiter = 1;
118
119                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
120
121                 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
122
123                 if( active_threads < 1 ) {
124                         ldap_pvt_thread_cond_signal(&active_threads_cond);
125                 }
126                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
127
128                 ldap_pvt_thread_yield();
129                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
130         }
131         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
132         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
133
134         ber_free( ber, 1 );
135
136         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
137         num_bytes_sent += bytes;
138         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
139
140         Statslog( LDAP_DEBUG_STATS,
141             "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
142             op->o_opid, err, tag, nentries );
143
144         return;
145 }
146
147 void
148 send_ldap_result(
149     Connection  *conn,
150     Operation   *op,
151     int         err,
152     char        *matched,
153     char        *text
154 )
155 {
156 #ifdef LDAP_CONNECTIONLESS
157         if ( op->o_cldap ) {
158                 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
159                     sizeof( struct sockaddr ));
160                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
161                     inet_ntoa(((struct sockaddr_in *)
162                     conn->c_sb.sb_useaddr)->sin_addr ),
163                     ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
164                     0 );
165         }
166 #endif
167         send_ldap_result2( conn, op, err, matched, text, 0 );
168 }
169
170 void
171 send_ldap_search_result(
172     Connection  *conn,
173     Operation   *op,
174     int         err,
175     char        *matched,
176     char        *text,
177     int         nentries
178 )
179 {
180         send_ldap_result2( conn, op, err, matched, text, nentries );
181 }
182
183 int
184 send_search_entry(
185     Backend     *be,
186     Connection  *conn,
187     Operation   *op,
188     Entry       *e,
189     char        **attrs,
190     int         attrsonly
191 )
192 {
193         BerElement      *ber;
194         Attribute       *a;
195         int             i, rc, bytes, sd;
196         struct acl      *acl;
197         char            *edn;
198         int allattrs;
199
200         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
201
202         if ( ! access_allowed( be, conn, op, e,
203                 "entry", NULL, ACL_READ ) )
204         {
205                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
206                     0, 0, 0 );
207                 return( 1 );
208         }
209
210         edn = e->e_ndn;
211
212 #ifdef LDAP_COMPAT30
213         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
214                 == NULLBER )
215 #else
216         if ( (ber = der_alloc()) == NULLBER )
217 #endif
218         {
219                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
220                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
221                         "ber_alloc" );
222                 goto error_return;
223         }
224
225 #ifdef LDAP_COMPAT30
226         if ( conn->c_version == 30 ) {
227                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
228                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
229         } else
230 #endif
231         {
232                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
233                         LDAP_RES_SEARCH_ENTRY, e->e_dn );
234         }
235
236         if ( rc == -1 ) {
237                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
238                 ber_free( ber, 1 );
239                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
240                     "ber_printf dn" );
241                 goto error_return;
242         }
243
244         allattrs = ( (attrs == NULL) || charray_inlist( attrs, "*" ) );
245
246         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
247                 regmatch_t       matches[MAXREMATCHES];
248
249                 if ( !allattrs && !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,
264                                 MAXREMATCHES, matches );
265                 }
266
267                 if ( ! acl_access_allowed( acl, be, conn, e,
268                         NULL, op, ACL_READ, 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 #ifdef LDAP_COMPAT30
315         if ( conn->c_version == 30 ) {
316                 rc = ber_printf( ber, "}}}}" );
317         } else
318 #endif
319                 rc = ber_printf( ber, "}}}" );
320
321         if ( rc == -1 ) {
322                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
323                 ber_free( ber, 1 );
324                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
325                     "ber_printf entry end" );
326                 return( 1 );
327         }
328
329         /* write only one pdu at a time - wait til it's our turn */
330         ldap_pvt_thread_mutex_lock( &conn->c_pdumutex );
331
332         bytes = ber->ber_ptr - ber->ber_buf;
333         ldap_pvt_thread_mutex_lock( &new_conn_mutex );
334         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
335             0 ) != 0 ) {
336                 int err = errno;
337                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
338                 /*
339                  * we got an error.  if it's ewouldblock, we need to
340                  * wait on the socket being writable.  otherwise, figure
341                  * it's a hard error and return.
342                  */
343
344                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
345                     err, err > -1 && err < sys_nerr ? sys_errlist[err]
346                     : "unknown", 0 );
347
348                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
349                         close_connection( conn, op->o_connid, op->o_opid );
350
351                         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
352                         ber_free( ber, 1 );
353                         return( -1 );
354                 }
355
356                 /* wait for socket to be write-ready */
357                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
358                 active_threads--;
359                 conn->c_writewaiter = 1;
360                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
361                 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
362
363                 if( active_threads < 1 ) {
364                         ldap_pvt_thread_cond_signal(&active_threads_cond);
365                 }
366                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
367
368                 ldap_pvt_thread_yield();
369                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
370         }
371         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
372         ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
373
374         ber_free( ber, 1 );
375
376         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
377         num_bytes_sent += bytes;
378         num_entries_sent++;
379         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
380
381         ldap_pvt_thread_mutex_lock( &new_conn_mutex );
382         if ( conn->c_connid == op->o_connid ) {
383                 rc = 0;
384                 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
385                     conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
386         } else {
387                 rc = -1;
388         }
389         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
390
391         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
392
393         return( rc );
394
395 error_return:;
396         return( 1 );
397 }
398
399 int
400 str2result(
401     char        *s,
402     int         *code,
403     char        **matched,
404     char        **info
405 )
406 {
407         int     rc;
408         char    *c;
409
410         *code = LDAP_SUCCESS;
411         *matched = NULL;
412         *info = NULL;
413
414         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
415                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
416                     s, 0, 0 );
417
418                 return( -1 );
419         }
420
421         rc = 0;
422         while ( (s = strchr( s, '\n' )) != NULL ) {
423                 *s++ = '\0';
424                 if ( *s == '\0' ) {
425                         break;
426                 }
427                 if ( (c = strchr( s, ':' )) != NULL ) {
428                         c++;
429                 }
430
431                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
432                         if ( c != NULL ) {
433                                 *code = atoi( c );
434                         }
435                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
436                         if ( c != NULL ) {
437                                 *matched = c;
438                         }
439                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
440                         if ( c != NULL ) {
441                                 *info = c;
442                         }
443                 } else {
444                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
445                             s, 0, 0 );
446                         rc = -1;
447                 }
448         }
449
450         return( rc );
451 }
452
453 /*
454  * close_connection - close a connection. takes the connection to close,
455  * the connid associated with the operation generating the close (so we
456  * don't accidentally close a connection that's not ours), and the opid
457  * of the operation generating the close (for logging purposes).
458  */
459 void
460 close_connection( Connection *conn, int opconnid, int opid )
461 {
462         ldap_pvt_thread_mutex_lock( &new_conn_mutex );
463         if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
464                 int err;
465                 close( conn->c_sb.sb_sd );
466                 err = errno;
467                 Statslog( LDAP_DEBUG_STATS,
468                     "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
469                     opid, conn->c_sb.sb_sd, err, 0 );
470                 conn->c_sb.sb_sd = -1;
471                 conn->c_version = 0;
472         }
473         ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
474 }