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