1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
12 #include <ac/unistd.h> /* get close() */
17 #define EWOULDBLOCK WSAEWOULDBLOCK
32 unsigned long tag, bytes;
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 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
64 if ( (ber = der_alloc()) == NULLBER )
67 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
71 #ifdef LDAP_CONNECTIONLESS
73 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
74 err, matched ? matched : "", text ? text : "" );
78 if ( conn->c_version == 30 ) {
79 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
80 matched ? matched : "", text ? text : "" );
83 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
84 matched ? matched : "", text ? text : "" );
87 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
91 /* write only one pdu at a time - wait til it's our turn */
92 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
94 /* lock the connection */
95 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
98 bytes = ber->ber_ptr - ber->ber_buf;
103 if ( connection_state_closing( conn ) ) {
104 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
105 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
109 if ( ber_flush( &conn->c_sb, ber, 1 ) == 0 ) {
116 * we got an error. if it's ewouldblock, we need to
117 * wait on the socket being writable. otherwise, figure
118 * it's a hard error and return.
121 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
122 err, err > -1 && err < sys_nerr ? sys_errlist[err]
125 if ( err != EWOULDBLOCK && err != EAGAIN ) {
126 connection_closing( conn );
128 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
129 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
133 /* wait for socket to be write-ready */
134 conn->c_writewaiter = 1;
135 slapd_set_write( conn->c_sb.sb_sd, 1 );
137 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
138 conn->c_writewaiter = 0;
141 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
142 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
144 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
145 num_bytes_sent += bytes;
146 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
148 Statslog( LDAP_DEBUG_STATS,
149 "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
150 op->o_opid, err, tag, nentries );
164 #ifdef LDAP_CONNECTIONLESS
166 lber_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 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
222 if ( (ber = der_alloc()) == NULLBER )
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",
295 a->a_vals[i]->bv_val,
296 a->a_vals[i]->bv_len )) == -1 )
298 Debug( LDAP_DEBUG_ANY,
299 "ber_printf failed\n", 0, 0, 0 );
301 send_ldap_result( conn, op,
302 LDAP_OPERATIONS_ERROR, NULL,
303 "ber_printf value" );
309 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
310 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
312 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
313 NULL, "ber_printf type end" );
319 if ( conn->c_version == 30 ) {
320 rc = ber_printf( ber, "}}}}" );
323 rc = ber_printf( ber, "}}}" );
326 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
328 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
329 "ber_printf entry end" );
333 bytes = ber->ber_ptr - ber->ber_buf;
335 /* write only one pdu at a time - wait til it's our turn */
336 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
338 /* lock the connection */
339 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
345 if ( connection_state_closing( conn ) ) {
346 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
347 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
351 if ( ber_flush( &conn->c_sb, ber, 1 ) == 0 ) {
358 * we got an error. if it's ewouldblock, we need to
359 * wait on the socket being writable. otherwise, figure
360 * it's a hard error and return.
363 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
364 err, err > -1 && err < sys_nerr ? sys_errlist[err]
367 if ( err != EWOULDBLOCK && err != EAGAIN ) {
368 connection_closing( conn );
370 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
371 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
375 /* wait for socket to be write-ready */
376 conn->c_writewaiter = 1;
377 slapd_set_write( conn->c_sb.sb_sd, 1 );
379 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
380 conn->c_writewaiter = 0;
383 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
384 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
386 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
387 num_bytes_sent += bytes;
389 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
391 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
392 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
394 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
413 *code = LDAP_SUCCESS;
417 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
418 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
425 while ( (s = strchr( s, '\n' )) != NULL ) {
430 if ( (c = strchr( s, ':' )) != NULL ) {
434 if ( strncasecmp( s, "code", 4 ) == 0 ) {
438 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
442 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
447 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",