]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
014cdc42e905765c5f16c5697602b2325970a0e5
[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 #ifdef HAVE_WINSOCK
17 #define EWOULDBLOCK WSAEWOULDBLOCK
18 #endif
19
20 static void
21 send_ldap_result2(
22     Connection  *conn,
23     Operation   *op,
24     int         err,
25     char        *matched,
26     char        *text,
27     int         nentries
28 )
29 {
30         BerElement      *ber;
31         int             rc;
32         unsigned long   tag, bytes;
33
34         if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
35                 err = LDAP_NO_SUCH_OBJECT;
36
37         Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err,
38                 matched ?  matched : "",
39                 text ? text : "" );
40
41         switch ( op->o_tag ) {
42         case LBER_DEFAULT:
43                 tag = LBER_SEQUENCE;
44                 break;
45
46         case LDAP_REQ_SEARCH:
47                 tag = LDAP_RES_SEARCH_RESULT;
48                 break;
49
50         case LDAP_REQ_DELETE:
51                 tag = LDAP_RES_DELETE;
52                 break;
53
54         default:
55                 tag = op->o_tag + 1;
56                 break;
57         }
58
59
60 #ifdef LDAP_COMPAT30
61         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
62             == NULLBER )
63 #else
64         if ( (ber = der_alloc()) == NULLBER )
65 #endif
66         {
67                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
68                 return;
69         }
70
71 #ifdef LDAP_CONNECTIONLESS
72         if ( op->o_cldap ) {
73                 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
74                     err, matched ? matched : "", text ? text : "" );
75         } else
76 #endif
77 #ifdef LDAP_COMPAT30
78         if ( conn->c_version == 30 ) {
79                 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
80                     matched ? matched : "", text ? text : "" );
81         } else
82 #endif
83                 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
84                     matched ? matched : "", text ? text : "" );
85
86         if ( rc == -1 ) {
87                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
88                 return;
89         }
90
91         /* write only one pdu at a time - wait til it's our turn */
92         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
93
94         /* lock the connection */
95         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
96
97         /* write the pdu */
98         bytes = ber->ber_ptr - ber->ber_buf;
99
100         while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
101                 int err = errno;
102                 /*
103                  * we got an error.  if it's ewouldblock, we need to
104                  * wait on the socket being writable.  otherwise, figure
105                  * it's a hard error and return.
106                  */
107
108                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
109                     err, err > -1 && err < sys_nerr ? sys_errlist[err]
110                     : "unknown", 0 );
111
112                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
113                         connection_closing( conn );
114
115                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
116                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
117                         return;
118                 }
119
120                 /* wait for socket to be write-ready */
121                 conn->c_writewaiter = 1;
122                 slapd_set_write( conn->c_sb.sb_sd, 1 );
123
124                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
125                 conn->c_writewaiter = 0;
126         }
127
128         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
129         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
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=%lu 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                 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
154                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
155                     inet_ntoa(((struct sockaddr_in *)
156                     &op->o_clientaddr)->sin_addr ),
157                     ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
158                     0 );
159         }
160 #endif
161         send_ldap_result2( conn, op, err, matched, text, 0 );
162 }
163
164 void
165 send_ldap_search_result(
166     Connection  *conn,
167     Operation   *op,
168     int         err,
169     char        *matched,
170     char        *text,
171     int         nentries
172 )
173 {
174         send_ldap_result2( conn, op, err, matched, text, nentries );
175 }
176
177 int
178 send_search_entry(
179     Backend     *be,
180     Connection  *conn,
181     Operation   *op,
182     Entry       *e,
183     char        **attrs,
184     int         attrsonly
185 )
186 {
187         BerElement      *ber;
188         Attribute       *a;
189         int             i, rc=-1, bytes;
190         struct acl      *acl;
191         char            *edn;
192
193         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
194
195         if ( ! access_allowed( be, conn, op, e,
196                 "entry", NULL, ACL_READ ) )
197         {
198                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
199                     0, 0, 0 );
200                 return( 1 );
201         }
202
203         edn = e->e_ndn;
204
205 #ifdef LDAP_COMPAT30
206         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
207                 == NULLBER )
208 #else
209         if ( (ber = der_alloc()) == NULLBER )
210 #endif
211         {
212                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
213                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
214                         "ber_alloc" );
215                 goto error_return;
216         }
217
218 #ifdef LDAP_COMPAT30
219         if ( conn->c_version == 30 ) {
220                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
221                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
222         } else
223 #endif
224         {
225                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
226                         LDAP_RES_SEARCH_ENTRY, e->e_dn );
227         }
228
229         if ( rc == -1 ) {
230                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
231                 ber_free( ber, 1 );
232                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
233                     "ber_printf dn" );
234                 goto error_return;
235         }
236
237         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
238                 regmatch_t       matches[MAXREMATCHES];
239
240                 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
241                         continue;
242                 }
243
244                 /* the lastmod attributes are ignored by ACL checking */
245                 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
246                         strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
247                         strcasecmp( a->a_type, "creatorsname" ) == 0 ||
248                         strcasecmp( a->a_type, "createtimestamp" ) == 0 ) 
249                 {
250                         Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
251                                 a->a_type, 0, 0 );
252                         acl = NULL;
253                 } else {
254                         acl = acl_get_applicable( be, op, e, a->a_type,
255                                 MAXREMATCHES, matches );
256                 }
257
258                 if ( ! acl_access_allowed( acl, be, conn, e,
259                         NULL, op, ACL_READ, edn, matches ) ) 
260                 {
261                         continue;
262                 }
263
264                 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
265                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
266                         ber_free( ber, 1 );
267                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
268                             NULL, "ber_printf type" );
269                         goto error_return;
270                 }
271
272                 if ( ! attrsonly ) {
273                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
274                                 if ( a->a_syntax & SYNTAX_DN && 
275                                         ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
276                                                 ACL_READ, edn, matches) )
277                                 {
278                                         continue;
279                                 }
280
281                                 if (( rc = ber_printf( ber, "o",
282                                     a->a_vals[i]->bv_val,
283                                     a->a_vals[i]->bv_len )) == -1 )
284                                 {
285                                         Debug( LDAP_DEBUG_ANY,
286                                             "ber_printf failed\n", 0, 0, 0 );
287                                         ber_free( ber, 1 );
288                                         send_ldap_result( conn, op,
289                                             LDAP_OPERATIONS_ERROR, NULL,
290                                             "ber_printf value" );
291                                         goto error_return;
292                                 }
293                         }
294                 }
295
296                 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
297                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
298                         ber_free( ber, 1 );
299                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
300                             NULL, "ber_printf type end" );
301                         goto error_return;
302                 }
303         }
304
305 #ifdef LDAP_COMPAT30
306         if ( conn->c_version == 30 ) {
307                 rc = ber_printf( ber, "}}}}" );
308         } else
309 #endif
310                 rc = ber_printf( ber, "}}}" );
311
312         if ( rc == -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, NULL,
316                     "ber_printf entry end" );
317                 return( 1 );
318         }
319
320         bytes = ber->ber_ptr - ber->ber_buf;
321
322         /* write only one pdu at a time - wait til it's our turn */
323         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
324
325         /* lock the connection */ 
326         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
327
328         /* write the pdu */
329         while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
330                 int err = errno;
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( conn->c_sb.sb_sd, 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=%d op=%d ENTRY dn=\"%s\"\n",
366             conn->c_connid, 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 }