]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
e9c05de439232c1a142dc5a5ce8ce60056822af7
[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/socket.h>
8 #include <ac/errno.h>
9 #include <ac/signal.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 /* we need LBER internals */
17 #include "../../libraries/liblber/lber-int.h"
18
19 static void
20 send_ldap_result2(
21     Connection  *conn,
22     Operation   *op,
23     ber_int_t   err,
24     char        *matched,
25     char        *text,
26     int         nentries
27 )
28 {
29         BerElement      *ber;
30         int             rc;
31         ber_tag_t       tag;
32         ber_len_t       bytes;
33
34         assert( !LDAP_API_ERROR( err ) );
35
36         if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
37                 err = LDAP_NO_SUCH_OBJECT;
38
39         Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err,
40                 matched ?  matched : "",
41                 text ? text : "" );
42
43         switch ( op->o_tag ) {
44         case LBER_DEFAULT:
45                 tag = LBER_SEQUENCE;
46                 break;
47
48         case LDAP_REQ_SEARCH:
49                 tag = LDAP_RES_SEARCH_RESULT;
50                 break;
51
52         case LDAP_REQ_DELETE:
53                 tag = LDAP_RES_DELETE;
54                 break;
55
56         default:
57                 tag = op->o_tag + 1;
58                 break;
59         }
60
61
62         ber = ber_alloc_t( LBER_USE_DER );
63
64         if ( ber == NULL ) {
65                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
66                 return;
67         }
68
69 #ifdef LDAP_CONNECTIONLESS
70         if ( op->o_cldap ) {
71                 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
72                     err, matched ? matched : "", text ? text : "" );
73         } else
74 #endif
75         {
76                 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
77                     matched ? matched : "", text ? text : "" );
78         }
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_write_mutex );
87
88         /* lock the connection */
89         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
90
91         /* write the pdu */
92         bytes = ber_pvt_ber_bytes( ber );
93
94         while ( 1 ) {
95                 int err;
96
97                 if ( connection_state_closing( conn ) ) {
98                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
99                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
100                         return;
101                 }
102
103                 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
104                         break;
105                 }
106
107                 err = errno;
108
109                 /*
110                  * we got an error.  if it's ewouldblock, we need to
111                  * wait on the socket being writable.  otherwise, figure
112                  * it's a hard error and return.
113                  */
114
115                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
116                     err, err > -1 && err < sys_nerr ? sys_errlist[err]
117                     : "unknown", 0 );
118
119                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
120                         connection_closing( conn );
121
122                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
123                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
124                         return;
125                 }
126
127                 /* wait for socket to be write-ready */
128                 conn->c_writewaiter = 1;
129                 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
130
131                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
132                 conn->c_writewaiter = 0;
133         }
134
135         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
136         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
137
138         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
139         num_bytes_sent += bytes;
140         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
141
142         Statslog( LDAP_DEBUG_STATS,
143             "conn=%ld op=%ld RESULT err=%ld tag=%lu nentries=%d\n",
144                 (long) conn->c_connid, (long) op->o_opid,
145                 (long) err, (long) tag, nentries );
146
147         return;
148 }
149
150 void
151 send_ldap_result(
152     Connection  *conn,
153     Operation   *op,
154     ber_int_t   err,
155     char        *matched,
156     char        *text
157 )
158 {
159         assert( !LDAP_API_ERROR( err ) );
160
161 #ifdef LDAP_CONNECTIONLESS
162         if ( op->o_cldap ) {
163                 ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
164                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
165                     inet_ntoa(((struct sockaddr_in *)
166                     &op->o_clientaddr)->sin_addr ),
167                     ((struct sockaddr_in *) &op->o_clientaddr)->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     ber_int_t   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=-1, bytes;
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,
206                 "entry", NULL, ACL_READ ) )
207         {
208                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
209                     0, 0, 0 );
210                 return( 1 );
211         }
212
213         edn = e->e_ndn;
214
215         ber = ber_alloc_t( LBER_USE_DER );
216
217         if ( ber == NULL ) {
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         rc = ber_printf( ber, "{it{s{", op->o_msgid,
225                 LDAP_RES_SEARCH_ENTRY, e->e_dn );
226
227         if ( rc == -1 ) {
228                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
229                 ber_free( ber, 1 );
230                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
231                     "ber_printf dn" );
232                 goto error_return;
233         }
234
235         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
236                 regmatch_t       matches[MAXREMATCHES];
237
238                 if ( attrs == NULL ) {
239                         /* all addrs request, skip operational attributes */
240                         if( oc_check_operational( a->a_type )) {
241                                 continue;
242                         }
243                 } else {
244                         /* specific addrs requested */
245                         if ( !charray_inlist( attrs, a->a_type )) {
246                                 continue;
247                         }
248                 }
249
250                 acl = acl_get_applicable( be, op, e, a->a_type,
251                         MAXREMATCHES, matches );
252
253                 if ( ! acl_access_allowed( acl, be, conn, e,
254                         NULL, op, ACL_READ, edn, matches ) ) 
255                 {
256                         continue;
257                 }
258
259                 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
260                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
261                         ber_free( ber, 1 );
262                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
263                             NULL, "ber_printf type" );
264                         goto error_return;
265                 }
266
267                 if ( ! attrsonly ) {
268                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
269                                 if ( a->a_syntax & SYNTAX_DN && 
270                                         ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
271                                                 ACL_READ, edn, matches) )
272                                 {
273                                         continue;
274                                 }
275
276                                 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
277                                         Debug( LDAP_DEBUG_ANY,
278                                             "ber_printf failed\n", 0, 0, 0 );
279                                         ber_free( ber, 1 );
280                                         send_ldap_result( conn, op,
281                                             LDAP_OPERATIONS_ERROR, NULL,
282                                             "ber_printf value" );
283                                         goto error_return;
284                                 }
285                         }
286                 }
287
288                 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
289                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
290                         ber_free( ber, 1 );
291                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
292                             NULL, "ber_printf type end" );
293                         goto error_return;
294                 }
295         }
296
297         rc = ber_printf( ber, /*{{{*/ "}}}" );
298
299         if ( rc == -1 ) {
300                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
301                 ber_free( ber, 1 );
302                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
303                     "ber_printf entry end" );
304                 return( 1 );
305         }
306
307         bytes = ber_pvt_ber_bytes( ber );
308
309         /* write only one pdu at a time - wait til it's our turn */
310         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
311
312         /* lock the connection */ 
313         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
314
315         /* write the pdu */
316         while( 1 ) {
317                 int err;
318
319                 if ( connection_state_closing( conn ) ) {
320                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
321                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
322                         return 0;
323                 }
324
325                 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
326                         break;
327                 }
328
329                 err = errno;
330
331                 /*
332                  * we got an error.  if it's ewouldblock, we need to
333                  * wait on the socket being writable.  otherwise, figure
334                  * it's a hard error and return.
335                  */
336
337                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
338                     err, err > -1 && err < sys_nerr ? sys_errlist[err]
339                     : "unknown", 0 );
340
341                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
342                         connection_closing( conn );
343
344                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
345                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
346                         return( -1 );
347                 }
348
349                 /* wait for socket to be write-ready */
350                 conn->c_writewaiter = 1;
351                 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
352
353                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
354                 conn->c_writewaiter = 0;
355         }
356
357         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
358         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
359
360         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
361         num_bytes_sent += bytes;
362         num_entries_sent++;
363         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
364
365         Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
366             (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
367
368         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
369
370         rc = 0;
371
372 error_return:;
373         return( rc );
374 }
375
376 int
377 str2result(
378     char        *s,
379     int         *code,
380     char        **matched,
381     char        **info
382 )
383 {
384         int     rc;
385         char    *c;
386
387         *code = LDAP_SUCCESS;
388         *matched = NULL;
389         *info = NULL;
390
391         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
392                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
393                     s, 0, 0 );
394
395                 return( -1 );
396         }
397
398         rc = 0;
399         while ( (s = strchr( s, '\n' )) != NULL ) {
400                 *s++ = '\0';
401                 if ( *s == '\0' ) {
402                         break;
403                 }
404                 if ( (c = strchr( s, ':' )) != NULL ) {
405                         c++;
406                 }
407
408                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
409                         if ( c != NULL ) {
410                                 *code = atoi( c );
411                         }
412                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
413                         if ( c != NULL ) {
414                                 *matched = c;
415                         }
416                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
417                         if ( c != NULL ) {
418                                 *info = c;
419                         }
420                 } else {
421                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
422                             s, 0, 0 );
423                         rc = -1;
424                 }
425         }
426
427         return( rc );
428 }