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 ) {
101 * we got an error. if it's ewouldblock, we need to
102 * wait on the socket being writable. otherwise, figure
103 * it's a hard error and return.
106 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
107 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
110 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
111 connection_closing( conn );
113 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
114 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
118 /* wait for socket to be write-ready */
119 conn->c_writewaiter = 1;
120 slapd_set_write( conn->c_sb.sb_sd, 1 );
122 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
123 conn->c_writewaiter = 0;
126 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
127 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
129 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
130 num_bytes_sent += bytes;
131 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
133 Statslog( LDAP_DEBUG_STATS,
134 "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
135 op->o_opid, err, tag, nentries );
149 #ifdef LDAP_CONNECTIONLESS
151 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
152 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
153 inet_ntoa(((struct sockaddr_in *)
154 &op->o_clientaddr)->sin_addr ),
155 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
159 send_ldap_result2( conn, op, err, matched, text, 0 );
163 send_ldap_search_result(
172 send_ldap_result2( conn, op, err, matched, text, nentries );
191 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
193 if ( ! access_allowed( be, conn, op, e,
194 "entry", NULL, ACL_READ ) )
196 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
204 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
207 if ( (ber = der_alloc()) == NULLBER )
210 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
211 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
217 if ( conn->c_version == 30 ) {
218 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
219 LDAP_RES_SEARCH_ENTRY, e->e_dn );
223 rc = ber_printf( ber, "{it{s{", op->o_msgid,
224 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 && ! charray_inlist( attrs, a->a_type ) ) {
242 /* the lastmod attributes are ignored by ACL checking */
243 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
244 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
245 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
246 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
248 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
252 acl = acl_get_applicable( be, op, e, a->a_type,
253 MAXREMATCHES, matches );
256 if ( ! acl_access_allowed( acl, be, conn, e,
257 NULL, op, ACL_READ, edn, matches ) )
262 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
263 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
265 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
266 NULL, "ber_printf type" );
271 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
272 if ( a->a_syntax & SYNTAX_DN &&
273 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
274 ACL_READ, edn, matches) )
279 if (( rc = ber_printf( ber, "o",
280 a->a_vals[i]->bv_val,
281 a->a_vals[i]->bv_len )) == -1 )
283 Debug( LDAP_DEBUG_ANY,
284 "ber_printf failed\n", 0, 0, 0 );
286 send_ldap_result( conn, op,
287 LDAP_OPERATIONS_ERROR, NULL,
288 "ber_printf value" );
294 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
295 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
297 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
298 NULL, "ber_printf type end" );
304 if ( conn->c_version == 30 ) {
305 rc = ber_printf( ber, "}}}}" );
308 rc = ber_printf( ber, "}}}" );
311 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
313 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
314 "ber_printf entry end" );
318 bytes = ber->ber_ptr - ber->ber_buf;
320 /* write only one pdu at a time - wait til it's our turn */
321 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
323 /* lock the connection */
324 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
327 while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
329 * we got an error. if it's ewouldblock, we need to
330 * wait on the socket being writable. otherwise, figure
331 * it's a hard error and return.
334 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
335 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
338 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
339 connection_closing( conn );
341 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
342 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
346 /* wait for socket to be write-ready */
347 conn->c_writewaiter = 1;
348 slapd_set_write( conn->c_sb.sb_sd, 1 );
350 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
351 conn->c_writewaiter = 0;
354 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
355 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
357 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
358 num_bytes_sent += bytes;
360 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
362 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
363 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
365 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
384 *code = LDAP_SUCCESS;
388 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
389 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
396 while ( (s = strchr( s, '\n' )) != NULL ) {
401 if ( (c = strchr( s, ':' )) != NULL ) {
405 if ( strncasecmp( s, "code", 4 ) == 0 ) {
409 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
413 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
418 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",