1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
15 extern int active_threads;
16 extern pthread_mutex_t active_threads_mutex;
17 extern pthread_mutex_t new_conn_mutex;
18 extern pthread_t listener_tid;
19 extern struct acl *acl_get_applicable();
20 extern long num_entries_sent;
21 extern long num_bytes_sent;
22 extern pthread_mutex_t num_sent_mutex;
24 void close_connection();
38 unsigned long tag, bytes;
40 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
41 matched : "", text ? text : "" );
43 switch ( op->o_tag ) {
49 tag = LDAP_RES_SEARCH_RESULT;
53 tag = LDAP_RES_DELETE;
62 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
65 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 pthread_mutex_lock( &conn->c_pdumutex );
95 bytes = ber->ber_ptr - ber->ber_buf;
96 pthread_mutex_lock( &new_conn_mutex );
97 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
99 pthread_mutex_unlock( &new_conn_mutex );
101 * we got an error. if it's ewouldblock, we need to
102 * wait on the socket being writable. otherwise, figure
103 * it's a hard error and return.
106 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
107 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
110 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
111 close_connection( conn, op->o_connid, op->o_opid );
113 pthread_mutex_unlock( &conn->c_pdumutex );
117 /* wait for socket to be write-ready */
118 pthread_mutex_lock( &active_threads_mutex );
120 conn->c_writewaiter = 1;
122 #ifdef HAVE_LINUX_THREADS
123 pthread_kill( listener_tid, SIGSTKFLT );
125 pthread_kill( listener_tid, SIGUSR1 );
128 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
129 pthread_mutex_unlock( &active_threads_mutex );
132 pthread_mutex_lock( &new_conn_mutex );
134 pthread_mutex_unlock( &new_conn_mutex );
135 pthread_mutex_unlock( &conn->c_pdumutex );
137 pthread_mutex_lock( &num_sent_mutex );
138 num_bytes_sent += bytes;
139 pthread_mutex_unlock( &num_sent_mutex );
141 Statslog( LDAP_DEBUG_STATS,
142 "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
143 op->o_opid, err, tag, nentries );
157 #ifdef LDAP_CONNECTIONLESS
159 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
160 sizeof( struct sockaddr ));
161 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
162 inet_ntoa(((struct sockaddr_in *)
163 conn->c_sb.sb_useaddr)->sin_addr ),
164 ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
168 send_ldap_result2( conn, op, err, matched, text, 0 );
172 send_ldap_search_result(
181 send_ldap_result2( conn, op, err, matched, text, nentries );
196 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, "entry", NULL, op->o_dn,
204 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
209 edn = dn_normalize_case( strdup( e->e_dn ) );
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 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
244 regmatch_t matches[MAXREMATCHES];
246 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
250 /* the lastmod attributes are ignored by ACL checking */
251 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
252 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
253 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
254 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
256 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
260 acl = acl_get_applicable( be, op, e, a->a_type, edn,
261 MAXREMATCHES, matches );
264 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op, ACL_READ,
270 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
271 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
273 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
274 NULL, "ber_printf type" );
279 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
280 if ( a->a_syntax & SYNTAX_DN &&
281 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
282 ACL_READ, edn, matches) )
287 if ( ber_printf( ber, "o",
288 a->a_vals[i]->bv_val,
289 a->a_vals[i]->bv_len ) == -1 )
291 Debug( LDAP_DEBUG_ANY,
292 "ber_printf failed\n", 0, 0, 0 );
294 send_ldap_result( conn, op,
295 LDAP_OPERATIONS_ERROR, NULL,
296 "ber_printf value" );
302 if ( ber_printf( ber, "]}" ) == -1 ) {
303 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
305 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
306 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 pthread_mutex_lock( &conn->c_pdumutex );
331 bytes = ber->ber_ptr - ber->ber_buf;
332 pthread_mutex_lock( &new_conn_mutex );
333 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
335 pthread_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 pthread_mutex_unlock( &conn->c_pdumutex );
353 /* wait for socket to be write-ready */
354 pthread_mutex_lock( &active_threads_mutex );
356 conn->c_writewaiter = 1;
357 pthread_kill( listener_tid, SIGUSR1 );
358 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
359 pthread_mutex_unlock( &active_threads_mutex );
362 pthread_mutex_lock( &new_conn_mutex );
364 pthread_mutex_unlock( &new_conn_mutex );
365 pthread_mutex_unlock( &conn->c_pdumutex );
367 pthread_mutex_lock( &num_sent_mutex );
368 num_bytes_sent += bytes;
370 pthread_mutex_unlock( &num_sent_mutex );
372 pthread_mutex_lock( &new_conn_mutex );
373 if ( conn->c_connid == op->o_connid ) {
375 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
376 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
380 pthread_mutex_unlock( &new_conn_mutex );
382 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
402 *code = LDAP_SUCCESS;
406 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
407 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
414 while ( (s = strchr( s, '\n' )) != NULL ) {
419 if ( (c = strchr( s, ':' )) != NULL ) {
423 if ( strncasecmp( s, "code", 4 ) == 0 ) {
427 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
431 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
436 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
446 * close_connection - close a connection. takes the connection to close,
447 * the connid associated with the operation generating the close (so we
448 * don't accidentally close a connection that's not ours), and the opid
449 * of the operation generating the close (for logging purposes).
452 close_connection( Connection *conn, int opconnid, int opid )
454 pthread_mutex_lock( &new_conn_mutex );
455 if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
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, errno, 0 );
459 close( conn->c_sb.sb_sd );
460 conn->c_sb.sb_sd = -1;
463 pthread_mutex_unlock( &new_conn_mutex );