1 /* result.c - routines to send ldap results, errors, and referrals */
9 #include <sys/socket.h>
10 #include <netinet/in.h>
15 #ifdef DECL_SYS_ERRLIST
17 extern char *sys_errlist[];
20 extern int active_threads;
21 extern pthread_mutex_t active_threads_mutex;
22 extern pthread_mutex_t new_conn_mutex;
23 extern pthread_t listener_tid;
24 extern struct acl *acl_get_applicable();
25 extern long num_entries_sent;
26 extern long num_bytes_sent;
27 extern pthread_mutex_t num_sent_mutex;
29 void close_connection();
43 unsigned long tag, bytes;
45 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
46 matched : "", text ? text : "" );
48 switch ( op->o_tag ) {
54 tag = LDAP_RES_SEARCH_RESULT;
58 tag = LDAP_RES_DELETE;
67 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
70 if ( (ber = der_alloc()) == NULLBER ) {
72 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
78 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
79 err, matched ? matched : "", text ? text : "" );
83 if ( conn->c_version == 30 ) {
84 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
85 matched ? matched : "", text ? text : "" );
88 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
89 matched ? matched : "", text ? text : "" );
92 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
96 /* write only one pdu at a time - wait til it's our turn */
97 pthread_mutex_lock( &conn->c_pdumutex );
100 bytes = ber->ber_ptr - ber->ber_buf;
101 pthread_mutex_lock( &new_conn_mutex );
102 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
104 pthread_mutex_unlock( &new_conn_mutex );
106 * we got an error. if it's ewouldblock, we need to
107 * wait on the socket being writable. otherwise, figure
108 * it's a hard error and return.
111 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
112 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
115 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
116 close_connection( conn, op->o_connid, op->o_opid );
118 pthread_mutex_unlock( &conn->c_pdumutex );
122 /* wait for socket to be write-ready */
123 pthread_mutex_lock( &active_threads_mutex );
125 conn->c_writewaiter = 1;
128 pthread_kill( listener_tid, SIGSTKFLT );
130 pthread_kill( listener_tid, SIGUSR1 );
133 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
134 pthread_mutex_unlock( &active_threads_mutex );
137 pthread_mutex_lock( &new_conn_mutex );
139 pthread_mutex_unlock( &new_conn_mutex );
140 pthread_mutex_unlock( &conn->c_pdumutex );
142 pthread_mutex_lock( &num_sent_mutex );
143 num_bytes_sent += bytes;
144 pthread_mutex_unlock( &num_sent_mutex );
146 Statslog( LDAP_DEBUG_STATS,
147 "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
148 op->o_opid, err, tag, nentries );
164 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
165 sizeof( struct sockaddr ));
166 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
167 inet_ntoa(((struct sockaddr_in *)
168 conn->c_sb.sb_useaddr)->sin_addr ),
169 ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
173 send_ldap_result2( conn, op, err, matched, text, 0 );
177 send_ldap_search_result(
186 send_ldap_result2( conn, op, err, matched, text, nentries );
201 int i, rc, bytes, sd;
205 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
207 if ( ! access_allowed( be, conn, op, e, "entry", NULL, op->o_dn,
209 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
214 edn = dn_normalize_case( strdup( e->e_dn ) );
217 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
220 if ( (ber = der_alloc()) == NULLBER )
223 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
224 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
230 if ( conn->c_version == 30 ) {
231 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
232 LDAP_RES_SEARCH_ENTRY, e->e_dn );
236 rc = ber_printf( ber, "{it{s{", op->o_msgid,
237 LDAP_RES_SEARCH_ENTRY, e->e_dn );
241 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
243 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
248 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
249 regmatch_t matches[MAXREMATCHES];
251 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
255 /* the lastmod attributes are ignored by ACL checking */
256 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
257 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
258 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
259 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
261 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
265 acl = acl_get_applicable( be, op, e, a->a_type, edn,
266 MAXREMATCHES, matches );
269 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op, ACL_READ,
275 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
276 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
278 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
279 NULL, "ber_printf type" );
284 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
285 if ( a->a_syntax & SYNTAX_DN &&
286 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
287 ACL_READ, edn, matches) )
292 if ( ber_printf( ber, "o",
293 a->a_vals[i]->bv_val,
294 a->a_vals[i]->bv_len ) == -1 )
296 Debug( LDAP_DEBUG_ANY,
297 "ber_printf failed\n", 0, 0, 0 );
299 send_ldap_result( conn, op,
300 LDAP_OPERATIONS_ERROR, NULL,
301 "ber_printf value" );
307 if ( ber_printf( ber, "]}" ) == -1 ) {
308 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
310 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
311 NULL, "ber_printf type end" );
319 if ( conn->c_version == 30 ) {
320 rc = ber_printf( ber, "}}}}" );
323 rc = ber_printf( ber, "}}}" );
326 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
328 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
329 "ber_printf entry end" );
333 /* write only one pdu at a time - wait til it's our turn */
334 pthread_mutex_lock( &conn->c_pdumutex );
336 bytes = ber->ber_ptr - ber->ber_buf;
337 pthread_mutex_lock( &new_conn_mutex );
338 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
340 pthread_mutex_unlock( &new_conn_mutex );
342 * we got an error. if it's ewouldblock, we need to
343 * wait on the socket being writable. otherwise, figure
344 * it's a hard error and return.
347 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
348 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
351 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
352 close_connection( conn, op->o_connid, op->o_opid );
354 pthread_mutex_unlock( &conn->c_pdumutex );
358 /* wait for socket to be write-ready */
359 pthread_mutex_lock( &active_threads_mutex );
361 conn->c_writewaiter = 1;
362 pthread_kill( listener_tid, SIGUSR1 );
363 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
364 pthread_mutex_unlock( &active_threads_mutex );
367 pthread_mutex_lock( &new_conn_mutex );
369 pthread_mutex_unlock( &new_conn_mutex );
370 pthread_mutex_unlock( &conn->c_pdumutex );
372 pthread_mutex_lock( &num_sent_mutex );
373 num_bytes_sent += bytes;
375 pthread_mutex_unlock( &num_sent_mutex );
377 pthread_mutex_lock( &new_conn_mutex );
378 if ( conn->c_connid == op->o_connid ) {
380 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
381 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
385 pthread_mutex_unlock( &new_conn_mutex );
387 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
407 *code = LDAP_SUCCESS;
411 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
412 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
419 while ( (s = strchr( s, '\n' )) != NULL ) {
424 if ( (c = strchr( s, ':' )) != NULL ) {
428 if ( strncasecmp( s, "code", 4 ) == 0 ) {
432 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
436 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
441 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
451 * close_connection - close a connection. takes the connection to close,
452 * the connid associated with the operation generating the close (so we
453 * don't accidentally close a connection that's not ours), and the opid
454 * of the operation generating the close (for logging purposes).
457 close_connection( Connection *conn, int opconnid, int opid )
459 pthread_mutex_lock( &new_conn_mutex );
460 if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
461 Statslog( LDAP_DEBUG_STATS,
462 "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
463 opid, conn->c_sb.sb_sd, errno, 0 );
464 close( conn->c_sb.sb_sd );
465 conn->c_sb.sb_sd = -1;
468 pthread_mutex_unlock( &new_conn_mutex );