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,
94 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
96 * we got an error. if it's ewouldblock, we need to
97 * wait on the socket being writable. otherwise, figure
98 * it's a hard error and return.
101 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
102 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
105 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
106 close_connection( conn, op->o_connid, op->o_opid );
108 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
113 /* wait for socket to be write-ready */
114 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
116 conn->c_writewaiter = 1;
118 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
120 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
122 if( active_threads < 1 ) {
123 ldap_pvt_thread_cond_signal(&active_threads_cond);
125 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
127 ldap_pvt_thread_yield();
128 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
130 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
131 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
135 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
136 num_bytes_sent += bytes;
137 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
139 Statslog( LDAP_DEBUG_STATS,
140 "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
141 op->o_opid, err, tag, nentries );
155 #ifdef LDAP_CONNECTIONLESS
157 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
158 sizeof( struct sockaddr ));
159 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
160 inet_ntoa(((struct sockaddr_in *)
161 conn->c_sb.sb_useaddr)->sin_addr ),
162 ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
166 send_ldap_result2( conn, op, err, matched, text, 0 );
170 send_ldap_search_result(
179 send_ldap_result2( conn, op, err, matched, text, nentries );
194 int i, rc, bytes, sd;
199 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
201 if ( ! access_allowed( be, conn, op, e,
202 "entry", NULL, ACL_READ ) )
204 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
212 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
215 if ( (ber = der_alloc()) == NULLBER )
218 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
219 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
225 if ( conn->c_version == 30 ) {
226 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
227 LDAP_RES_SEARCH_ENTRY, e->e_dn );
231 rc = ber_printf( ber, "{it{s{", op->o_msgid,
232 LDAP_RES_SEARCH_ENTRY, e->e_dn );
236 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
238 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
243 allattrs = ( (attrs == NULL) || charray_inlist( attrs, "*" ) );
245 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
246 regmatch_t matches[MAXREMATCHES];
248 if ( !allattrs && !charray_inlist( attrs, a->a_type ) ) {
252 /* the lastmod attributes are ignored by ACL checking */
253 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
254 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
255 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
256 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
258 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
262 acl = acl_get_applicable( be, op, e, a->a_type,
263 MAXREMATCHES, matches );
266 if ( ! acl_access_allowed( acl, be, conn, e,
267 NULL, op, ACL_READ, edn, matches ) )
272 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
273 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
275 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
276 NULL, "ber_printf type" );
281 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
282 if ( a->a_syntax & SYNTAX_DN &&
283 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
284 ACL_READ, edn, matches) )
289 if ( ber_printf( ber, "o",
290 a->a_vals[i]->bv_val,
291 a->a_vals[i]->bv_len ) == -1 )
293 Debug( LDAP_DEBUG_ANY,
294 "ber_printf failed\n", 0, 0, 0 );
296 send_ldap_result( conn, op,
297 LDAP_OPERATIONS_ERROR, NULL,
298 "ber_printf value" );
304 if ( ber_printf( ber, "]}" ) == -1 ) {
305 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
307 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
308 NULL, "ber_printf type end" );
314 if ( conn->c_version == 30 ) {
315 rc = ber_printf( ber, "}}}}" );
318 rc = ber_printf( ber, "}}}" );
321 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
323 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
324 "ber_printf entry end" );
328 /* write only one pdu at a time - wait til it's our turn */
329 ldap_pvt_thread_mutex_lock( &conn->c_pdumutex );
331 bytes = ber->ber_ptr - ber->ber_buf;
332 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
333 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
335 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
337 * we got an error. if it's ewouldblock, we need to
338 * wait on the socket being writable. otherwise, figure
339 * it's a hard error and return.
342 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
343 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
346 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
347 close_connection( conn, op->o_connid, op->o_opid );
349 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
354 /* wait for socket to be write-ready */
355 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
357 conn->c_writewaiter = 1;
358 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
359 ldap_pvt_thread_cond_wait( &conn->c_wcv, &active_threads_mutex );
361 if( active_threads < 1 ) {
362 ldap_pvt_thread_cond_signal(&active_threads_cond);
364 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
366 ldap_pvt_thread_yield();
367 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
369 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
370 ldap_pvt_thread_mutex_unlock( &conn->c_pdumutex );
374 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
375 num_bytes_sent += bytes;
377 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
379 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
380 if ( conn->c_connid == op->o_connid ) {
382 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
383 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
387 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
389 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
408 *code = LDAP_SUCCESS;
412 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
413 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
420 while ( (s = strchr( s, '\n' )) != NULL ) {
425 if ( (c = strchr( s, ':' )) != NULL ) {
429 if ( strncasecmp( s, "code", 4 ) == 0 ) {
433 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
437 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
442 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
452 * close_connection - close a connection. takes the connection to close,
453 * the connid associated with the operation generating the close (so we
454 * don't accidentally close a connection that's not ours), and the opid
455 * of the operation generating the close (for logging purposes).
458 close_connection( Connection *conn, int opconnid, int opid )
460 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
461 if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
462 Statslog( LDAP_DEBUG_STATS,
463 "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
464 opid, conn->c_sb.sb_sd, errno, 0 );
465 close( conn->c_sb.sb_sd );
466 conn->c_sb.sb_sd = -1;
469 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );