1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
12 #include <ac/unistd.h> /* get close() */
29 unsigned long tag, bytes;
31 if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
32 err = LDAP_NO_SUCH_OBJECT;
34 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
35 matched : "", text ? text : "" );
37 switch ( op->o_tag ) {
43 tag = LDAP_RES_SEARCH_RESULT;
47 tag = LDAP_RES_DELETE;
56 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
59 if ( (ber = der_alloc()) == NULLBER ) {
61 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
65 #ifdef LDAP_CONNECTIONLESS
67 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
68 err, matched ? matched : "", text ? text : "" );
72 if ( conn->c_version == 30 ) {
73 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
74 matched ? matched : "", text ? text : "" );
77 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
78 matched ? matched : "", text ? text : "" );
81 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_pdumutex );
90 bytes = ber->ber_ptr - ber->ber_buf;
91 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
92 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
95 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
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 err, err > -1 && err < sys_nerr ? sys_errlist[err]
106 if ( err != EWOULDBLOCK && err != EAGAIN ) {
107 close_connection( conn, op->o_connid, op->o_opid );
109 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
114 /* wait for socket to be write-ready */
115 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
117 conn->c_writewaiter = 1;
119 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
121 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
123 if( active_threads < 1 ) {
124 ldap_pvt_thread_cond_signal(&active_threads_cond);
126 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
128 ldap_pvt_thread_yield();
129 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
131 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
132 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
136 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
137 num_bytes_sent += bytes;
138 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
140 Statslog( LDAP_DEBUG_STATS,
141 "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
142 op->o_opid, err, tag, nentries );
156 #ifdef LDAP_CONNECTIONLESS
158 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
159 sizeof( struct sockaddr ));
160 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
161 inet_ntoa(((struct sockaddr_in *)
162 conn->c_sb.sb_useaddr)->sin_addr ),
163 ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
167 send_ldap_result2( conn, op, err, matched, text, 0 );
171 send_ldap_search_result(
180 send_ldap_result2( conn, op, err, matched, text, nentries );
195 int i, rc, bytes, sd;
200 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
202 if ( ! access_allowed( be, conn, op, e,
203 "entry", NULL, ACL_READ ) )
205 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
213 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
216 if ( (ber = der_alloc()) == NULLBER )
219 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
220 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
226 if ( conn->c_version == 30 ) {
227 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
228 LDAP_RES_SEARCH_ENTRY, e->e_dn );
232 rc = ber_printf( ber, "{it{s{", op->o_msgid,
233 LDAP_RES_SEARCH_ENTRY, e->e_dn );
237 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
239 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
244 allattrs = ( (attrs == NULL) || charray_inlist( attrs, "*" ) );
246 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
247 regmatch_t matches[MAXREMATCHES];
249 if ( !allattrs && !charray_inlist( attrs, a->a_type ) ) {
253 acl = acl_get_applicable( be, op, e, a->a_type,
254 MAXREMATCHES, matches );
256 if ( ! acl_access_allowed( acl, be, conn, e,
257 NULL, op, ACL_READ, edn, matches ) )
262 if ( 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 ( 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 ( 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 /* write only one pdu at a time - wait til it's our turn */
319 ldap_pvt_thread_mutex_lock( &conn->c_pdumutex );
321 bytes = ber->ber_ptr - ber->ber_buf;
322 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
323 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
326 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
328 * we got an error. if it's ewouldblock, we need to
329 * wait on the socket being writable. otherwise, figure
330 * it's a hard error and return.
333 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
334 err, err > -1 && err < sys_nerr ? sys_errlist[err]
337 if ( err != EWOULDBLOCK && err != EAGAIN ) {
338 close_connection( conn, op->o_connid, op->o_opid );
340 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
345 /* wait for socket to be write-ready */
346 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
348 conn->c_writewaiter = 1;
349 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
350 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
352 if( active_threads < 1 ) {
353 ldap_pvt_thread_cond_signal(&active_threads_cond);
355 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
357 ldap_pvt_thread_yield();
358 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
360 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
361 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
365 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
366 num_bytes_sent += bytes;
368 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
370 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
371 if ( conn->c_connid == op->o_connid ) {
373 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
374 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
378 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
380 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
399 *code = LDAP_SUCCESS;
403 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
404 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
411 while ( (s = strchr( s, '\n' )) != NULL ) {
416 if ( (c = strchr( s, ':' )) != NULL ) {
420 if ( strncasecmp( s, "code", 4 ) == 0 ) {
424 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
428 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
433 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
443 * close_connection - close a connection. takes the connection to close,
444 * the connid associated with the operation generating the close (so we
445 * don't accidentally close a connection that's not ours), and the opid
446 * of the operation generating the close (for logging purposes).
449 close_connection( Connection *conn, int opconnid, int opid )
451 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
452 if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
454 close( conn->c_sb.sb_sd );
456 Statslog( LDAP_DEBUG_STATS,
457 "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
458 opid, conn->c_sb.sb_sd, err, 0 );
459 conn->c_sb.sb_sd = -1;
462 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );