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"
31 unsigned long tag, bytes;
33 if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
34 err = LDAP_NO_SUCH_OBJECT;
36 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err,
37 matched ? matched : "",
40 switch ( op->o_tag ) {
46 tag = LDAP_RES_SEARCH_RESULT;
50 tag = LDAP_RES_DELETE;
60 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
63 if ( (ber = der_alloc()) == NULLBER )
66 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
70 #ifdef LDAP_CONNECTIONLESS
72 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
73 err, matched ? matched : "", text ? text : "" );
77 if ( conn->c_version == 30 ) {
78 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
79 matched ? matched : "", text ? text : "" );
82 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
83 matched ? matched : "", text ? text : "" );
86 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
90 /* write only one pdu at a time - wait til it's our turn */
91 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
93 /* lock the connection */
94 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
97 bytes = ber_pvt_ber_bytes( ber );
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 );
108 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
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.
120 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
121 err, err > -1 && err < sys_nerr ? sys_errlist[err]
124 if ( err != EWOULDBLOCK && err != EAGAIN ) {
125 connection_closing( conn );
127 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
128 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
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 );
136 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
137 conn->c_writewaiter = 0;
140 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
141 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
143 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
144 num_bytes_sent += bytes;
145 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
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 );
163 #ifdef LDAP_CONNECTIONLESS
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,
173 send_ldap_result2( conn, op, err, matched, text, 0 );
177 send_ldap_search_result(
186 send_ldap_result2( conn, op, err, matched, text, nentries );
205 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
207 if ( ! access_allowed( be, conn, op, e,
208 "entry", NULL, ACL_READ ) )
210 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
218 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
221 if ( (ber = der_alloc()) == NULLBER )
224 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
225 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
231 if ( conn->c_version == 30 ) {
232 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
233 LDAP_RES_SEARCH_ENTRY, e->e_dn );
237 rc = ber_printf( ber, "{it{s{", op->o_msgid,
238 LDAP_RES_SEARCH_ENTRY, e->e_dn );
242 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
244 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
249 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
250 regmatch_t matches[MAXREMATCHES];
252 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
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 )
262 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
266 acl = acl_get_applicable( be, op, e, a->a_type,
267 MAXREMATCHES, matches );
270 if ( ! acl_access_allowed( acl, be, conn, e,
271 NULL, op, ACL_READ, edn, matches ) )
276 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
277 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
279 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
280 NULL, "ber_printf type" );
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) )
293 if (( rc = ber_printf( ber, "o",
294 a->a_vals[i]->bv_val,
295 a->a_vals[i]->bv_len )) == -1 )
297 Debug( LDAP_DEBUG_ANY,
298 "ber_printf failed\n", 0, 0, 0 );
300 send_ldap_result( conn, op,
301 LDAP_OPERATIONS_ERROR, NULL,
302 "ber_printf value" );
308 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
309 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
311 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
312 NULL, "ber_printf type end" );
318 if ( conn->c_version == 30 ) {
319 rc = ber_printf( ber, "}}}}" );
322 rc = ber_printf( ber, "}}}" );
325 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
327 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
328 "ber_printf entry end" );
332 bytes = ber_pvt_ber_bytes( ber );
334 /* write only one pdu at a time - wait til it's our turn */
335 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
337 /* lock the connection */
338 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
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 );
350 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
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.
362 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
363 err, err > -1 && err < sys_nerr ? sys_errlist[err]
366 if ( err != EWOULDBLOCK && err != EAGAIN ) {
367 connection_closing( conn );
369 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
370 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
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 );
378 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
379 conn->c_writewaiter = 0;
382 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
383 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
385 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
386 num_bytes_sent += bytes;
388 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
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 );
393 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
412 *code = LDAP_SUCCESS;
416 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
417 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
424 while ( (s = strchr( s, '\n' )) != NULL ) {
429 if ( (c = strchr( s, ':' )) != NULL ) {
433 if ( strncasecmp( s, "code", 4 ) == 0 ) {
437 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
441 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
446 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",