1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <sys/socket.h>
11 #include <netinet/in.h>
16 #ifdef DECL_SYS_ERRLIST
18 extern char *sys_errlist[];
21 extern int active_threads;
22 extern pthread_mutex_t active_threads_mutex;
23 extern pthread_mutex_t new_conn_mutex;
24 extern pthread_t listener_tid;
25 extern struct acl *acl_get_applicable();
26 extern long num_entries_sent;
27 extern long num_bytes_sent;
28 extern pthread_mutex_t num_sent_mutex;
30 void close_connection();
44 unsigned long tag, bytes;
46 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
47 matched : "", text ? text : "" );
49 switch ( op->o_tag ) {
55 tag = LDAP_RES_SEARCH_RESULT;
59 tag = LDAP_RES_DELETE;
68 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
71 if ( (ber = der_alloc()) == NULLBER ) {
73 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
79 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
80 err, matched ? matched : "", text ? text : "" );
84 if ( conn->c_version == 30 ) {
85 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
86 matched ? matched : "", text ? text : "" );
89 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
90 matched ? matched : "", text ? text : "" );
93 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
97 /* write only one pdu at a time - wait til it's our turn */
98 pthread_mutex_lock( &conn->c_pdumutex );
101 bytes = ber->ber_ptr - ber->ber_buf;
102 pthread_mutex_lock( &new_conn_mutex );
103 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
105 pthread_mutex_unlock( &new_conn_mutex );
107 * we got an error. if it's ewouldblock, we need to
108 * wait on the socket being writable. otherwise, figure
109 * it's a hard error and return.
112 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
113 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
116 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
117 close_connection( conn, op->o_connid, op->o_opid );
119 pthread_mutex_unlock( &conn->c_pdumutex );
123 /* wait for socket to be write-ready */
124 pthread_mutex_lock( &active_threads_mutex );
126 conn->c_writewaiter = 1;
129 pthread_kill( listener_tid, SIGSTKFLT );
131 pthread_kill( listener_tid, SIGUSR1 );
134 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
135 pthread_mutex_unlock( &active_threads_mutex );
138 pthread_mutex_lock( &new_conn_mutex );
140 pthread_mutex_unlock( &new_conn_mutex );
141 pthread_mutex_unlock( &conn->c_pdumutex );
143 pthread_mutex_lock( &num_sent_mutex );
144 num_bytes_sent += bytes;
145 pthread_mutex_unlock( &num_sent_mutex );
147 Statslog( LDAP_DEBUG_STATS,
148 "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
149 op->o_opid, err, tag, nentries );
165 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
166 sizeof( struct sockaddr ));
167 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
168 inet_ntoa(((struct sockaddr_in *)
169 conn->c_sb.sb_useaddr)->sin_addr ),
170 ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
174 send_ldap_result2( conn, op, err, matched, text, 0 );
178 send_ldap_search_result(
187 send_ldap_result2( conn, op, err, matched, text, nentries );
202 int i, rc, bytes, sd;
206 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
208 if ( ! access_allowed( be, conn, op, e, "entry", NULL, op->o_dn,
210 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
215 edn = dn_normalize_case( strdup( e->e_dn ) );
218 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
221 if ( (ber = der_alloc()) == NULLBER )
224 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
225 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
232 if ( conn->c_version == 30 ) {
233 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
234 LDAP_RES_SEARCH_ENTRY, e->e_dn );
238 rc = ber_printf( ber, "{it{s{", op->o_msgid,
239 LDAP_RES_SEARCH_ENTRY, e->e_dn );
243 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
245 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
251 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
252 regmatch_t matches[MAXREMATCHES];
254 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
258 /* the lastmod attributes are ignored by ACL checking */
259 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
260 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
261 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
262 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
264 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
268 acl = acl_get_applicable( be, op, e, a->a_type, edn,
269 MAXREMATCHES, matches );
272 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op, ACL_READ,
278 if ( ber_printf( ber, "{s[", a->a_type ) == -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" );
288 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
289 if ( a->a_syntax & SYNTAX_DN &&
290 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
291 ACL_READ, edn, matches) )
296 if ( ber_printf( ber, "o",
297 a->a_vals[i]->bv_val,
298 a->a_vals[i]->bv_len ) == -1 )
300 Debug( LDAP_DEBUG_ANY,
301 "ber_printf failed\n", 0, 0, 0 );
303 send_ldap_result( conn, op,
304 LDAP_OPERATIONS_ERROR, NULL,
305 "ber_printf value" );
312 if ( ber_printf( ber, "]}" ) == -1 ) {
313 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
315 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
316 NULL, "ber_printf type end" );
325 if ( conn->c_version == 30 ) {
326 rc = ber_printf( ber, "}}}}" );
329 rc = ber_printf( ber, "}}}" );
332 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
334 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
335 "ber_printf entry end" );
339 /* write only one pdu at a time - wait til it's our turn */
340 pthread_mutex_lock( &conn->c_pdumutex );
342 bytes = ber->ber_ptr - ber->ber_buf;
343 pthread_mutex_lock( &new_conn_mutex );
344 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
346 pthread_mutex_unlock( &new_conn_mutex );
348 * we got an error. if it's ewouldblock, we need to
349 * wait on the socket being writable. otherwise, figure
350 * it's a hard error and return.
353 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
354 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
357 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
358 close_connection( conn, op->o_connid, op->o_opid );
360 pthread_mutex_unlock( &conn->c_pdumutex );
364 /* wait for socket to be write-ready */
365 pthread_mutex_lock( &active_threads_mutex );
367 conn->c_writewaiter = 1;
368 pthread_kill( listener_tid, SIGUSR1 );
369 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
370 pthread_mutex_unlock( &active_threads_mutex );
373 pthread_mutex_lock( &new_conn_mutex );
375 pthread_mutex_unlock( &new_conn_mutex );
376 pthread_mutex_unlock( &conn->c_pdumutex );
378 pthread_mutex_lock( &num_sent_mutex );
379 num_bytes_sent += bytes;
381 pthread_mutex_unlock( &num_sent_mutex );
383 pthread_mutex_lock( &new_conn_mutex );
384 if ( conn->c_connid == op->o_connid ) {
386 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
387 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
391 pthread_mutex_unlock( &new_conn_mutex );
393 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
409 *code = LDAP_SUCCESS;
413 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
414 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
421 while ( (s = strchr( s, '\n' )) != NULL ) {
426 if ( (c = strchr( s, ':' )) != NULL ) {
430 if ( strncasecmp( s, "code", 4 ) == 0 ) {
434 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
438 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
443 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
453 * close_connection - close a connection. takes the connection to close,
454 * the connid associated with the operation generating the close (so we
455 * don't accidentally close a connection that's not ours), and the opid
456 * of the operation generating the close (for logging purposes).
459 close_connection( Connection *conn, int opconnid, int opid )
461 pthread_mutex_lock( &new_conn_mutex );
462 if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
463 Statslog( LDAP_DEBUG_STATS,
464 "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
465 opid, conn->c_sb.sb_sd, errno, 0 );
466 close( conn->c_sb.sb_sd );
467 conn->c_sb.sb_sd = -1;
470 pthread_mutex_unlock( &new_conn_mutex );