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;
61 ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER );
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=%ld op=%ld RESULT err=%ld tag=%lu nentries=%d\n",
149 (long) conn->c_connid, (long) op->o_opid,
150 (long) err, (long) tag, nentries );
164 #ifdef LDAP_CONNECTIONLESS
166 ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
167 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
168 inet_ntoa(((struct sockaddr_in *)
169 &op->o_clientaddr)->sin_addr ),
170 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
174 send_ldap_result2( conn, op, err, matched, text, 0 );
178 send_ldap_search_result(
187 send_ldap_result2( conn, op, err, matched, text, nentries );
206 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
208 if ( ! access_allowed( be, conn, op, e,
209 "entry", NULL, ACL_READ ) )
211 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
219 ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER );
225 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
226 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
232 if ( conn->c_version == 30 ) {
233 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
234 LDAP_RES_SEARCH_ENTRY, e->e_dn );
238 rc = ber_printf( ber, "{it{s{", op->o_msgid,
239 LDAP_RES_SEARCH_ENTRY, e->e_dn );
243 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
245 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
250 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
251 regmatch_t matches[MAXREMATCHES];
253 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
257 /* the lastmod attributes are ignored by ACL checking */
258 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
259 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
260 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
261 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
263 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
267 acl = acl_get_applicable( be, op, e, a->a_type,
268 MAXREMATCHES, matches );
271 if ( ! acl_access_allowed( acl, be, conn, e,
272 NULL, op, ACL_READ, edn, matches ) )
277 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
278 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
280 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
281 NULL, "ber_printf type" );
286 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
287 if ( a->a_syntax & SYNTAX_DN &&
288 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
289 ACL_READ, edn, matches) )
294 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
295 Debug( LDAP_DEBUG_ANY,
296 "ber_printf failed\n", 0, 0, 0 );
298 send_ldap_result( conn, op,
299 LDAP_OPERATIONS_ERROR, NULL,
300 "ber_printf value" );
306 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
307 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
309 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
310 NULL, "ber_printf type end" );
316 if ( conn->c_version == 30 ) {
317 rc = ber_printf( ber, /*{{{{{*/ "}}}}" );
320 rc = ber_printf( ber, /*{{{{*/ "}}}" );
323 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
325 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
326 "ber_printf entry end" );
330 bytes = ber_pvt_ber_bytes( ber );
332 /* write only one pdu at a time - wait til it's our turn */
333 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
335 /* lock the connection */
336 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
342 if ( connection_state_closing( conn ) ) {
343 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
344 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
348 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
355 * we got an error. if it's ewouldblock, we need to
356 * wait on the socket being writable. otherwise, figure
357 * it's a hard error and return.
360 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
361 err, err > -1 && err < sys_nerr ? sys_errlist[err]
364 if ( err != EWOULDBLOCK && err != EAGAIN ) {
365 connection_closing( conn );
367 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
368 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
372 /* wait for socket to be write-ready */
373 conn->c_writewaiter = 1;
374 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
376 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
377 conn->c_writewaiter = 0;
380 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
381 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
383 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
384 num_bytes_sent += bytes;
386 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
388 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
389 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
391 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
410 *code = LDAP_SUCCESS;
414 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
415 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
422 while ( (s = strchr( s, '\n' )) != NULL ) {
427 if ( (c = strchr( s, ':' )) != NULL ) {
431 if ( strncasecmp( s, "code", 4 ) == 0 ) {
435 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
439 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
444 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",