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, matched ?
38 matched : "", text ? text : "" );
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->ber_ptr - ber->ber_buf;
99 while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
102 * we got an error. if it's ewouldblock, we need to
103 * wait on the socket being writable. otherwise, figure
104 * it's a hard error and return.
107 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
108 err, err > -1 && err < sys_nerr ? sys_errlist[err]
111 if ( err != EWOULDBLOCK && err != EAGAIN ) {
112 connection_closing( conn );
114 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
115 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
119 /* wait for socket to be write-ready */
120 conn->c_writewaiter = 1;
121 slapd_set_write( conn->c_sb.sb_sd, 1 );
123 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
124 conn->c_writewaiter = 0;
127 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
128 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
130 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
131 num_bytes_sent += bytes;
132 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
134 Statslog( LDAP_DEBUG_STATS,
135 "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
136 op->o_opid, err, tag, nentries );
150 #ifdef LDAP_CONNECTIONLESS
152 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
153 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
154 inet_ntoa(((struct sockaddr_in *)
155 &op->o_clientaddr)->sin_addr ),
156 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
160 send_ldap_result2( conn, op, err, matched, text, 0 );
164 send_ldap_search_result(
173 send_ldap_result2( conn, op, err, matched, text, nentries );
192 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
194 if ( ! access_allowed( be, conn, op, e,
195 "entry", NULL, ACL_READ ) )
197 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
205 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
208 if ( (ber = der_alloc()) == NULLBER )
211 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
212 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
218 if ( conn->c_version == 30 ) {
219 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
220 LDAP_RES_SEARCH_ENTRY, e->e_dn );
224 rc = ber_printf( ber, "{it{s{", op->o_msgid,
225 LDAP_RES_SEARCH_ENTRY, e->e_dn );
229 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
231 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
236 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
237 regmatch_t matches[MAXREMATCHES];
239 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
243 /* the lastmod attributes are ignored by ACL checking */
244 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
245 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
246 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
247 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
249 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
253 acl = acl_get_applicable( be, op, e, a->a_type,
254 MAXREMATCHES, matches );
257 if ( ! acl_access_allowed( acl, be, conn, e,
258 NULL, op, ACL_READ, edn, matches ) )
263 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
264 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
266 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
267 NULL, "ber_printf type" );
272 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
273 if ( a->a_syntax & SYNTAX_DN &&
274 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
275 ACL_READ, edn, matches) )
280 if (( rc = ber_printf( ber, "o",
281 a->a_vals[i]->bv_val,
282 a->a_vals[i]->bv_len )) == -1 )
284 Debug( LDAP_DEBUG_ANY,
285 "ber_printf failed\n", 0, 0, 0 );
287 send_ldap_result( conn, op,
288 LDAP_OPERATIONS_ERROR, NULL,
289 "ber_printf value" );
295 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
296 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
298 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
299 NULL, "ber_printf type end" );
305 if ( conn->c_version == 30 ) {
306 rc = ber_printf( ber, "}}}}" );
309 rc = ber_printf( ber, "}}}" );
312 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
314 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
315 "ber_printf entry end" );
319 bytes = ber->ber_ptr - ber->ber_buf;
321 /* write only one pdu at a time - wait til it's our turn */
322 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
324 /* lock the connection */
325 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
328 while ( 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( conn->c_sb.sb_sd, 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=%d op=%d ENTRY dn=\"%s\"\n",
365 conn->c_connid, 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",