1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
12 #include <ac/unistd.h> /* get close() */
28 unsigned long tag, bytes;
30 if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
31 err = LDAP_NO_SUCH_OBJECT;
33 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
34 matched : "", text ? text : "" );
36 switch ( op->o_tag ) {
42 tag = LDAP_RES_SEARCH_RESULT;
46 tag = LDAP_RES_DELETE;
56 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
59 if ( (ber = der_alloc()) == NULLBER )
62 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
66 #ifdef LDAP_CONNECTIONLESS
68 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
69 err, matched ? matched : "", text ? text : "" );
73 if ( conn->c_version == 30 ) {
74 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
75 matched ? matched : "", text ? text : "" );
78 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
79 matched ? matched : "", text ? text : "" );
82 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
86 /* write only one pdu at a time - wait til it's our turn */
87 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
89 /* lock the connection */
90 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
93 bytes = ber->ber_ptr - ber->ber_buf;
95 while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
97 * we got an error. if it's ewouldblock, we need to
98 * wait on the socket being writable. otherwise, figure
99 * it's a hard error and return.
102 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
103 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
106 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
107 connection_closing( conn );
109 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
110 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
114 /* wait for socket to be write-ready */
115 conn->c_writewaiter = 1;
116 slapd_set_write( conn->c_sb.sb_sd, 1 );
118 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
119 conn->c_writewaiter = 0;
122 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
123 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
126 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
127 num_bytes_sent += bytes;
128 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
131 Statslog( LDAP_DEBUG_STATS,
132 "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
133 op->o_opid, err, tag, nentries );
147 #ifdef LDAP_CONNECTIONLESS
149 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
150 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
151 inet_ntoa(((struct sockaddr_in *)
152 &op->o_clientaddr)->sin_addr ),
153 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
157 send_ldap_result2( conn, op, err, matched, text, 0 );
161 send_ldap_search_result(
170 send_ldap_result2( conn, op, err, matched, text, nentries );
189 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
191 if ( ! access_allowed( be, conn, op, e,
192 "entry", NULL, ACL_READ ) )
194 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
202 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
205 if ( (ber = der_alloc()) == NULLBER )
208 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
209 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
215 if ( conn->c_version == 30 ) {
216 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
217 LDAP_RES_SEARCH_ENTRY, e->e_dn );
221 rc = ber_printf( ber, "{it{s{", op->o_msgid,
222 LDAP_RES_SEARCH_ENTRY, e->e_dn );
226 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
228 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
233 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
234 regmatch_t matches[MAXREMATCHES];
236 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
240 /* the lastmod attributes are ignored by ACL checking */
241 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
242 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
243 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
244 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
246 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
250 acl = acl_get_applicable( be, op, e, a->a_type,
251 MAXREMATCHES, matches );
254 if ( ! acl_access_allowed( acl, be, conn, e,
255 NULL, op, ACL_READ, edn, matches ) )
260 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
261 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
263 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
264 NULL, "ber_printf type" );
269 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
270 if ( a->a_syntax & SYNTAX_DN &&
271 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
272 ACL_READ, edn, matches) )
277 if (( rc = ber_printf( ber, "o",
278 a->a_vals[i]->bv_val,
279 a->a_vals[i]->bv_len )) == -1 )
281 Debug( LDAP_DEBUG_ANY,
282 "ber_printf failed\n", 0, 0, 0 );
284 send_ldap_result( conn, op,
285 LDAP_OPERATIONS_ERROR, NULL,
286 "ber_printf value" );
292 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
293 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
295 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
296 NULL, "ber_printf type end" );
302 if ( conn->c_version == 30 ) {
303 rc = ber_printf( ber, "}}}}" );
306 rc = ber_printf( ber, "}}}" );
309 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
311 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
312 "ber_printf entry end" );
316 bytes = ber->ber_ptr - ber->ber_buf;
318 /* write only one pdu at a time - wait til it's our turn */
319 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
321 /* lock the connection */
322 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
325 while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
327 * we got an error. if it's ewouldblock, we need to
328 * wait on the socket being writable. otherwise, figure
329 * it's a hard error and return.
332 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
333 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
336 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
337 connection_closing( conn );
339 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
340 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
344 /* wait for socket to be write-ready */
345 conn->c_writewaiter = 1;
346 slapd_set_write( conn->c_sb.sb_sd, 1 );
348 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
349 conn->c_writewaiter = 0;
352 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
353 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
356 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
357 num_bytes_sent += bytes;
359 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",