1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
12 #include <ac/unistd.h> /* get close() */
16 /* we need LBER internals */
17 #include "../../libraries/liblber/lber-int.h"
34 if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
35 err = LDAP_NO_SUCH_OBJECT;
37 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err,
38 matched ? matched : "",
41 switch ( op->o_tag ) {
47 tag = LDAP_RES_SEARCH_RESULT;
51 tag = LDAP_RES_DELETE;
60 ber = ber_alloc_t( LBER_USE_DER );
63 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
67 #ifdef LDAP_CONNECTIONLESS
69 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
70 err, matched ? matched : "", text ? text : "" );
74 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
75 matched ? matched : "", text ? text : "" );
79 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
83 /* write only one pdu at a time - wait til it's our turn */
84 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
86 /* lock the connection */
87 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
90 bytes = ber_pvt_ber_bytes( ber );
95 if ( connection_state_closing( conn ) ) {
96 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
97 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
101 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
108 * we got an error. if it's ewouldblock, we need to
109 * wait on the socket being writable. otherwise, figure
110 * it's a hard error and return.
113 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
114 err, err > -1 && err < sys_nerr ? sys_errlist[err]
117 if ( err != EWOULDBLOCK && err != EAGAIN ) {
118 connection_closing( conn );
120 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
121 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
125 /* wait for socket to be write-ready */
126 conn->c_writewaiter = 1;
127 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
129 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
130 conn->c_writewaiter = 0;
133 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
134 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
136 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
137 num_bytes_sent += bytes;
138 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
140 Statslog( LDAP_DEBUG_STATS,
141 "conn=%ld op=%ld RESULT err=%ld tag=%lu nentries=%d\n",
142 (long) conn->c_connid, (long) op->o_opid,
143 (long) err, (long) tag, nentries );
157 #ifdef LDAP_CONNECTIONLESS
159 ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
160 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
161 inet_ntoa(((struct sockaddr_in *)
162 &op->o_clientaddr)->sin_addr ),
163 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
167 send_ldap_result2( conn, op, err, matched, text, 0 );
171 send_ldap_search_result(
180 send_ldap_result2( conn, op, err, matched, text, nentries );
199 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
201 if ( ! access_allowed( be, conn, op, e,
202 "entry", NULL, ACL_READ ) )
204 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
211 ber = ber_alloc_t( LBER_USE_DER );
214 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
215 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
220 rc = ber_printf( ber, "{it{s{", op->o_msgid,
221 LDAP_RES_SEARCH_ENTRY, e->e_dn );
224 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
226 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
231 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
232 regmatch_t matches[MAXREMATCHES];
234 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
238 /* the lastmod attributes are ignored by ACL checking */
239 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
240 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
241 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
242 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
244 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
248 acl = acl_get_applicable( be, op, e, a->a_type,
249 MAXREMATCHES, matches );
252 if ( ! acl_access_allowed( acl, be, conn, e,
253 NULL, op, ACL_READ, edn, matches ) )
258 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
259 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
261 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
262 NULL, "ber_printf type" );
267 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
268 if ( a->a_syntax & SYNTAX_DN &&
269 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
270 ACL_READ, edn, matches) )
275 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
276 Debug( LDAP_DEBUG_ANY,
277 "ber_printf failed\n", 0, 0, 0 );
279 send_ldap_result( conn, op,
280 LDAP_OPERATIONS_ERROR, NULL,
281 "ber_printf value" );
287 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
288 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
290 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
291 NULL, "ber_printf type end" );
296 rc = ber_printf( ber, /*{{{*/ "}}}" );
299 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
301 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
302 "ber_printf entry end" );
306 bytes = ber_pvt_ber_bytes( ber );
308 /* write only one pdu at a time - wait til it's our turn */
309 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
311 /* lock the connection */
312 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
318 if ( connection_state_closing( conn ) ) {
319 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
320 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
324 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
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.
336 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
337 err, err > -1 && err < sys_nerr ? sys_errlist[err]
340 if ( err != EWOULDBLOCK && err != EAGAIN ) {
341 connection_closing( conn );
343 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
344 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
348 /* wait for socket to be write-ready */
349 conn->c_writewaiter = 1;
350 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
352 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
353 conn->c_writewaiter = 0;
356 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
357 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
359 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
360 num_bytes_sent += bytes;
362 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
364 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
365 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
367 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
386 *code = LDAP_SUCCESS;
390 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
391 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
398 while ( (s = strchr( s, '\n' )) != NULL ) {
403 if ( (c = strchr( s, ':' )) != NULL ) {
407 if ( strncasecmp( s, "code", 4 ) == 0 ) {
411 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
415 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
420 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",