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 );
125 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
126 num_bytes_sent += bytes;
127 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
129 Statslog( LDAP_DEBUG_STATS,
130 "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
131 op->o_opid, err, tag, nentries );
145 #ifdef LDAP_CONNECTIONLESS
147 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
148 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
149 inet_ntoa(((struct sockaddr_in *)
150 &op->o_clientaddr)->sin_addr ),
151 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
155 send_ldap_result2( conn, op, err, matched, text, 0 );
159 send_ldap_search_result(
168 send_ldap_result2( conn, op, err, matched, text, nentries );
187 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
189 if ( ! access_allowed( be, conn, op, e,
190 "entry", NULL, ACL_READ ) )
192 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
200 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
203 if ( (ber = der_alloc()) == NULLBER )
206 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
207 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
213 if ( conn->c_version == 30 ) {
214 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
215 LDAP_RES_SEARCH_ENTRY, e->e_dn );
219 rc = ber_printf( ber, "{it{s{", op->o_msgid,
220 LDAP_RES_SEARCH_ENTRY, e->e_dn );
224 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
226 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
231 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
232 regmatch_t matches[MAXREMATCHES];
234 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
238 /* the lastmod attributes are ignored by ACL checking */
239 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
240 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
241 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
242 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
244 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
248 acl = acl_get_applicable( be, op, e, a->a_type,
249 MAXREMATCHES, matches );
252 if ( ! acl_access_allowed( acl, be, conn, e,
253 NULL, op, ACL_READ, edn, matches ) )
258 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
259 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
261 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
262 NULL, "ber_printf type" );
267 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
268 if ( a->a_syntax & SYNTAX_DN &&
269 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
270 ACL_READ, edn, matches) )
275 if (( rc = ber_printf( ber, "o",
276 a->a_vals[i]->bv_val,
277 a->a_vals[i]->bv_len )) == -1 )
279 Debug( LDAP_DEBUG_ANY,
280 "ber_printf failed\n", 0, 0, 0 );
282 send_ldap_result( conn, op,
283 LDAP_OPERATIONS_ERROR, NULL,
284 "ber_printf value" );
290 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
291 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
293 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
294 NULL, "ber_printf type end" );
300 if ( conn->c_version == 30 ) {
301 rc = ber_printf( ber, "}}}}" );
304 rc = ber_printf( ber, "}}}" );
307 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
309 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
310 "ber_printf entry end" );
314 bytes = ber->ber_ptr - ber->ber_buf;
316 /* write only one pdu at a time - wait til it's our turn */
317 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
319 /* lock the connection */
320 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
323 while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
325 * we got an error. if it's ewouldblock, we need to
326 * wait on the socket being writable. otherwise, figure
327 * it's a hard error and return.
330 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
331 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
334 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
335 connection_closing( conn );
337 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
338 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
342 /* wait for socket to be write-ready */
343 conn->c_writewaiter = 1;
344 slapd_set_write( conn->c_sb.sb_sd, 1 );
346 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
347 conn->c_writewaiter = 0;
350 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
351 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
353 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
354 num_bytes_sent += bytes;
356 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
358 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
359 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
380 *code = LDAP_SUCCESS;
384 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
385 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
392 while ( (s = strchr( s, '\n' )) != NULL ) {
397 if ( (c = strchr( s, ':' )) != NULL ) {
401 if ( strncasecmp( s, "code", 4 ) == 0 ) {
405 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
409 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
414 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",