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 assert( !LDAP_API_ERROR( err ) );
36 if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
37 err = LDAP_NO_SUCH_OBJECT;
39 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err,
40 matched ? matched : "",
43 switch ( op->o_tag ) {
49 tag = LDAP_RES_SEARCH_RESULT;
53 tag = LDAP_RES_DELETE;
62 ber = ber_alloc_t( LBER_USE_DER );
65 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
69 #ifdef LDAP_CONNECTIONLESS
71 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
72 err, matched ? matched : "", text ? text : "" );
76 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
77 matched ? matched : "", text ? text : "" );
81 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
85 /* write only one pdu at a time - wait til it's our turn */
86 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
88 /* lock the connection */
89 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
92 bytes = ber_pvt_ber_bytes( ber );
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 );
103 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
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.
115 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
116 err, err > -1 && err < sys_nerr ? sys_errlist[err]
119 if ( err != EWOULDBLOCK && err != EAGAIN ) {
120 connection_closing( conn );
122 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
123 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
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 );
131 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
132 conn->c_writewaiter = 0;
135 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
136 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
138 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
139 num_bytes_sent += bytes;
140 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
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 );
159 assert( !LDAP_API_ERROR( err ) );
161 #ifdef LDAP_CONNECTIONLESS
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,
171 send_ldap_result2( conn, op, err, matched, text, 0 );
175 send_ldap_search_result(
184 send_ldap_result2( conn, op, err, matched, text, nentries );
203 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
205 if ( ! access_allowed( be, conn, op, e,
206 "entry", NULL, ACL_READ ) )
208 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
215 ber = ber_alloc_t( LBER_USE_DER );
218 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
219 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
224 rc = ber_printf( ber, "{it{s{", op->o_msgid,
225 LDAP_RES_SEARCH_ENTRY, e->e_dn );
228 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
230 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
235 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
236 regmatch_t matches[MAXREMATCHES];
238 if ( attrs == NULL ) {
239 /* all addrs request, skip operational attributes */
240 if( oc_check_operational( a->a_type )) {
244 /* specific addrs requested */
245 if ( !charray_inlist( attrs, a->a_type )) {
250 acl = acl_get_applicable( be, op, e, a->a_type,
251 MAXREMATCHES, matches );
253 if ( ! acl_access_allowed( acl, be, conn, e,
254 NULL, op, ACL_READ, edn, matches ) )
259 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
260 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
262 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
263 NULL, "ber_printf type" );
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) )
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 );
280 send_ldap_result( conn, op,
281 LDAP_OPERATIONS_ERROR, NULL,
282 "ber_printf value" );
288 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
289 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
291 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
292 NULL, "ber_printf type end" );
297 rc = ber_printf( ber, /*{{{*/ "}}}" );
300 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
302 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
303 "ber_printf entry end" );
307 bytes = ber_pvt_ber_bytes( ber );
309 /* write only one pdu at a time - wait til it's our turn */
310 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
312 /* lock the connection */
313 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
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 );
325 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
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.
337 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
338 err, err > -1 && err < sys_nerr ? sys_errlist[err]
341 if ( err != EWOULDBLOCK && err != EAGAIN ) {
342 connection_closing( conn );
344 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
345 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
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 );
353 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
354 conn->c_writewaiter = 0;
357 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
358 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
360 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
361 num_bytes_sent += bytes;
363 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
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 );
368 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
387 *code = LDAP_SUCCESS;
391 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
392 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
399 while ( (s = strchr( s, '\n' )) != NULL ) {
404 if ( (c = strchr( s, ':' )) != NULL ) {
408 if ( strncasecmp( s, "code", 4 ) == 0 ) {
412 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
416 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
421 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",