1 /* result.c - routines to send ldap results, errors, and referrals */
7 #include <sys/socket.h>
8 #include <netinet/in.h>
14 #ifndef SYSERRLIST_IN_STDIO
16 extern char *sys_errlist[];
18 extern int active_threads;
19 extern pthread_mutex_t active_threads_mutex;
20 extern pthread_mutex_t new_conn_mutex;
21 extern pthread_t listener_tid;
22 extern struct acl *acl_get_applicable();
23 extern long num_entries_sent;
24 extern long num_bytes_sent;
25 extern pthread_mutex_t num_sent_mutex;
27 void close_connection();
41 unsigned long tag, bytes;
43 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
44 matched : "", text ? text : "" );
46 switch ( op->o_tag ) {
52 tag = LDAP_RES_SEARCH_RESULT;
56 tag = LDAP_RES_DELETE;
65 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
68 if ( (ber = der_alloc()) == NULLBER ) {
70 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
76 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
77 err, matched ? matched : "", text ? text : "" );
81 if ( conn->c_version == 30 ) {
82 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
83 matched ? matched : "", text ? text : "" );
86 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
87 matched ? matched : "", text ? text : "" );
90 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
94 /* write only one pdu at a time - wait til it's our turn */
95 pthread_mutex_lock( &conn->c_pdumutex );
98 bytes = ber->ber_ptr - ber->ber_buf;
99 pthread_mutex_lock( &new_conn_mutex );
100 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
102 pthread_mutex_unlock( &new_conn_mutex );
104 * we got an error. if it's ewouldblock, we need to
105 * wait on the socket being writable. otherwise, figure
106 * it's a hard error and return.
109 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
110 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
113 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
114 close_connection( conn, op->o_connid, op->o_opid );
116 pthread_mutex_unlock( &conn->c_pdumutex );
120 /* wait for socket to be write-ready */
121 pthread_mutex_lock( &active_threads_mutex );
123 conn->c_writewaiter = 1;
124 pthread_kill( listener_tid, SIGUSR1 );
125 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
126 pthread_mutex_unlock( &active_threads_mutex );
129 pthread_mutex_lock( &new_conn_mutex );
131 pthread_mutex_unlock( &new_conn_mutex );
132 pthread_mutex_unlock( &conn->c_pdumutex );
134 pthread_mutex_lock( &num_sent_mutex );
135 num_bytes_sent += bytes;
136 pthread_mutex_unlock( &num_sent_mutex );
138 Statslog( LDAP_DEBUG_STATS,
139 "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
140 op->o_opid, err, tag, nentries );
156 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
157 sizeof( struct sockaddr ));
158 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
159 inet_ntoa(((struct sockaddr_in *)
160 conn->c_sb.sb_useaddr)->sin_addr ),
161 ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
165 send_ldap_result2( conn, op, err, matched, text, 0 );
169 send_ldap_search_result(
178 send_ldap_result2( conn, op, err, matched, text, nentries );
193 int i, rc, bytes, sd;
196 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
198 if ( ! access_allowed( be, conn, op, e, "entry", NULL, op->o_dn,
200 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
206 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
209 if ( (ber = der_alloc()) == NULLBER ) {
211 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
212 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
218 if ( conn->c_version == 30 ) {
219 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
220 LDAP_RES_SEARCH_ENTRY, e->e_dn );
223 rc = ber_printf( ber, "{it{s{", op->o_msgid,
224 LDAP_RES_SEARCH_ENTRY, e->e_dn );
227 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
229 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
234 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
235 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
239 acl = acl_get_applicable( be, op, e, a->a_type );
241 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op,
246 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
247 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
249 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
250 NULL, "ber_printf type" );
255 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
256 if ( a->a_syntax & SYNTAX_DN &&
257 ! acl_access_allowed( acl, be, conn, e,
258 a->a_vals[i], op, ACL_READ ) )
263 if ( ber_printf( ber, "o",
264 a->a_vals[i]->bv_val,
265 a->a_vals[i]->bv_len ) == -1 )
267 Debug( LDAP_DEBUG_ANY,
268 "ber_printf failed\n", 0, 0, 0 );
270 send_ldap_result( conn, op,
271 LDAP_OPERATIONS_ERROR, NULL,
272 "ber_printf value" );
278 if ( ber_printf( ber, "]}" ) == -1 ) {
279 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
281 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
282 NULL, "ber_printf type end" );
288 if ( conn->c_version == 30 ) {
289 rc = ber_printf( ber, "}}}}" );
292 rc = ber_printf( ber, "}}}" );
295 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
297 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
298 "ber_printf entry end" );
302 /* write only one pdu at a time - wait til it's our turn */
303 pthread_mutex_lock( &conn->c_pdumutex );
305 bytes = ber->ber_ptr - ber->ber_buf;
306 pthread_mutex_lock( &new_conn_mutex );
307 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
309 pthread_mutex_unlock( &new_conn_mutex );
311 * we got an error. if it's ewouldblock, we need to
312 * wait on the socket being writable. otherwise, figure
313 * it's a hard error and return.
316 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
317 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
320 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
321 close_connection( conn, op->o_connid, op->o_opid );
323 pthread_mutex_unlock( &conn->c_pdumutex );
327 /* wait for socket to be write-ready */
328 pthread_mutex_lock( &active_threads_mutex );
330 conn->c_writewaiter = 1;
331 pthread_kill( listener_tid, SIGUSR1 );
332 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
333 pthread_mutex_unlock( &active_threads_mutex );
336 pthread_mutex_lock( &new_conn_mutex );
338 pthread_mutex_unlock( &new_conn_mutex );
339 pthread_mutex_unlock( &conn->c_pdumutex );
341 pthread_mutex_lock( &num_sent_mutex );
342 num_bytes_sent += bytes;
344 pthread_mutex_unlock( &num_sent_mutex );
346 pthread_mutex_lock( &new_conn_mutex );
347 if ( conn->c_connid == op->o_connid ) {
349 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
350 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
354 pthread_mutex_unlock( &new_conn_mutex );
356 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
372 *code = LDAP_SUCCESS;
376 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
377 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
384 while ( (s = strchr( s, '\n' )) != NULL ) {
389 if ( (c = strchr( s, ':' )) != NULL ) {
393 if ( strncasecmp( s, "code", 4 ) == 0 ) {
397 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
401 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
406 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
416 * close_connection - close a connection. takes the connection to close,
417 * the connid associated with the operation generating the close (so we
418 * don't accidentally close a connection that's not ours), and the opid
419 * of the operation generating the close (for logging purposes).
422 close_connection( Connection *conn, int opconnid, int opid )
424 pthread_mutex_lock( &new_conn_mutex );
425 if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
426 Statslog( LDAP_DEBUG_STATS,
427 "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
428 opid, conn->c_sb.sb_sd, errno, 0 );
429 close( conn->c_sb.sb_sd );
430 conn->c_sb.sb_sd = -1;
433 pthread_mutex_unlock( &new_conn_mutex );