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