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;
100 while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
103 * we got an error. if it's ewouldblock, we need to
104 * wait on the socket being writable. otherwise, figure
105 * it's a hard error and return.
108 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
109 err, err > -1 && err < sys_nerr ? sys_errlist[err]
112 if ( err != EWOULDBLOCK && err != EAGAIN ) {
113 connection_closing( conn );
115 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
116 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
120 /* wait for socket to be write-ready */
121 conn->c_writewaiter = 1;
122 slapd_set_write( conn->c_sb.sb_sd, 1 );
124 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
125 conn->c_writewaiter = 0;
128 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
129 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
131 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
132 num_bytes_sent += bytes;
133 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
135 Statslog( LDAP_DEBUG_STATS,
136 "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
137 op->o_opid, err, tag, nentries );
151 #ifdef LDAP_CONNECTIONLESS
153 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
154 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
155 inet_ntoa(((struct sockaddr_in *)
156 &op->o_clientaddr)->sin_addr ),
157 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
161 send_ldap_result2( conn, op, err, matched, text, 0 );
165 send_ldap_search_result(
174 send_ldap_result2( conn, op, err, matched, text, nentries );
193 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
195 if ( ! access_allowed( be, conn, op, e,
196 "entry", NULL, ACL_READ ) )
198 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
206 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
209 if ( (ber = der_alloc()) == NULLBER )
212 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
213 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
219 if ( conn->c_version == 30 ) {
220 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
221 LDAP_RES_SEARCH_ENTRY, e->e_dn );
225 rc = ber_printf( ber, "{it{s{", op->o_msgid,
226 LDAP_RES_SEARCH_ENTRY, e->e_dn );
230 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
232 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
237 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
238 regmatch_t matches[MAXREMATCHES];
240 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
244 /* the lastmod attributes are ignored by ACL checking */
245 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
246 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
247 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
248 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
250 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
254 acl = acl_get_applicable( be, op, e, a->a_type,
255 MAXREMATCHES, matches );
258 if ( ! acl_access_allowed( acl, be, conn, e,
259 NULL, op, ACL_READ, edn, matches ) )
264 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
265 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
267 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
268 NULL, "ber_printf type" );
273 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
274 if ( a->a_syntax & SYNTAX_DN &&
275 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
276 ACL_READ, edn, matches) )
281 if (( rc = ber_printf( ber, "o",
282 a->a_vals[i]->bv_val,
283 a->a_vals[i]->bv_len )) == -1 )
285 Debug( LDAP_DEBUG_ANY,
286 "ber_printf failed\n", 0, 0, 0 );
288 send_ldap_result( conn, op,
289 LDAP_OPERATIONS_ERROR, NULL,
290 "ber_printf value" );
296 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
297 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
299 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
300 NULL, "ber_printf type end" );
306 if ( conn->c_version == 30 ) {
307 rc = ber_printf( ber, "}}}}" );
310 rc = ber_printf( ber, "}}}" );
313 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
315 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
316 "ber_printf entry end" );
320 bytes = ber->ber_ptr - ber->ber_buf;
322 /* write only one pdu at a time - wait til it's our turn */
323 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
325 /* lock the connection */
326 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
329 while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
332 * we got an error. if it's ewouldblock, we need to
333 * wait on the socket being writable. otherwise, figure
334 * it's a hard error and return.
337 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
338 err, err > -1 && err < sys_nerr ? sys_errlist[err]
341 if ( err != EWOULDBLOCK && err != EAGAIN ) {
342 connection_closing( conn );
344 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
345 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
349 /* wait for socket to be write-ready */
350 conn->c_writewaiter = 1;
351 slapd_set_write( conn->c_sb.sb_sd, 1 );
353 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
354 conn->c_writewaiter = 0;
357 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
358 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
360 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
361 num_bytes_sent += bytes;
363 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
365 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
366 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
368 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
387 *code = LDAP_SUCCESS;
391 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
392 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
399 while ( (s = strchr( s, '\n' )) != NULL ) {
404 if ( (c = strchr( s, ':' )) != NULL ) {
408 if ( strncasecmp( s, "code", 4 ) == 0 ) {
412 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
416 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
421 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",