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,
34 matched ? matched : "",
37 switch ( op->o_tag ) {
43 tag = LDAP_RES_SEARCH_RESULT;
47 tag = LDAP_RES_DELETE;
57 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
60 if ( (ber = der_alloc()) == NULLBER )
63 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
67 #ifdef LDAP_CONNECTIONLESS
69 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
70 err, matched ? matched : "", text ? text : "" );
74 if ( conn->c_version == 30 ) {
75 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
76 matched ? matched : "", text ? text : "" );
79 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
80 matched ? matched : "", text ? text : "" );
83 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
87 /* write only one pdu at a time - wait til it's our turn */
88 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
90 /* lock the connection */
91 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
94 bytes = ber->ber_ptr - ber->ber_buf;
99 if ( connection_state_closing( conn ) ) {
100 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
101 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
105 if ( ber_flush( &conn->c_sb, ber, 1 ) == 0 ) {
112 * we got an error. if it's ewouldblock, we need to
113 * wait on the socket being writable. otherwise, figure
114 * it's a hard error and return.
117 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
118 err, err > -1 && err < sys_nerr ? sys_errlist[err]
121 if ( err != EWOULDBLOCK && err != EAGAIN ) {
122 connection_closing( conn );
124 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
125 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
129 /* wait for socket to be write-ready */
130 conn->c_writewaiter = 1;
131 slapd_set_write( conn->c_sb.sb_sd, 1 );
133 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
134 conn->c_writewaiter = 0;
137 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
138 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
140 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
141 num_bytes_sent += bytes;
142 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
144 Statslog( LDAP_DEBUG_STATS,
145 "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
146 op->o_opid, err, tag, nentries );
160 #ifdef LDAP_CONNECTIONLESS
162 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
163 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
164 inet_ntoa(((struct sockaddr_in *)
165 &op->o_clientaddr)->sin_addr ),
166 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
170 send_ldap_result2( conn, op, err, matched, text, 0 );
174 send_ldap_search_result(
183 send_ldap_result2( conn, op, err, matched, text, nentries );
202 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
204 if ( ! access_allowed( be, conn, op, e,
205 "entry", NULL, ACL_READ ) )
207 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
215 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
218 if ( (ber = der_alloc()) == NULLBER )
221 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
222 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
228 if ( conn->c_version == 30 ) {
229 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
230 LDAP_RES_SEARCH_ENTRY, e->e_dn );
234 rc = ber_printf( ber, "{it{s{", op->o_msgid,
235 LDAP_RES_SEARCH_ENTRY, e->e_dn );
239 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
241 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
246 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
247 regmatch_t matches[MAXREMATCHES];
249 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
253 /* the lastmod attributes are ignored by ACL checking */
254 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
255 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
256 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
257 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
259 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
263 acl = acl_get_applicable( be, op, e, a->a_type,
264 MAXREMATCHES, matches );
267 if ( ! acl_access_allowed( acl, be, conn, e,
268 NULL, op, ACL_READ, edn, matches ) )
273 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
274 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
276 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
277 NULL, "ber_printf type" );
282 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
283 if ( a->a_syntax & SYNTAX_DN &&
284 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
285 ACL_READ, edn, matches) )
290 if (( rc = ber_printf( ber, "o",
291 a->a_vals[i]->bv_val,
292 a->a_vals[i]->bv_len )) == -1 )
294 Debug( LDAP_DEBUG_ANY,
295 "ber_printf failed\n", 0, 0, 0 );
297 send_ldap_result( conn, op,
298 LDAP_OPERATIONS_ERROR, NULL,
299 "ber_printf value" );
305 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
306 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
308 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
309 NULL, "ber_printf type end" );
315 if ( conn->c_version == 30 ) {
316 rc = ber_printf( ber, "}}}}" );
319 rc = ber_printf( ber, "}}}" );
322 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
324 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
325 "ber_printf entry end" );
329 bytes = ber->ber_ptr - ber->ber_buf;
331 /* write only one pdu at a time - wait til it's our turn */
332 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
334 /* lock the connection */
335 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
341 if ( connection_state_closing( conn ) ) {
342 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
343 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
347 if ( ber_flush( &conn->c_sb, ber, 1 ) == 0 ) {
354 * we got an error. if it's ewouldblock, we need to
355 * wait on the socket being writable. otherwise, figure
356 * it's a hard error and return.
359 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
360 err, err > -1 && err < sys_nerr ? sys_errlist[err]
363 if ( err != EWOULDBLOCK && err != EAGAIN ) {
364 connection_closing( conn );
366 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
367 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
371 /* wait for socket to be write-ready */
372 conn->c_writewaiter = 1;
373 slapd_set_write( conn->c_sb.sb_sd, 1 );
375 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
376 conn->c_writewaiter = 0;
379 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
380 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
382 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
383 num_bytes_sent += bytes;
385 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
387 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
388 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
390 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
409 *code = LDAP_SUCCESS;
413 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
414 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
421 while ( (s = strchr( s, '\n' )) != NULL ) {
426 if ( (c = strchr( s, ':' )) != NULL ) {
430 if ( strncasecmp( s, "code", 4 ) == 0 ) {
434 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
438 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
443 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",