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 if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
44 err = LDAP_NO_SUCH_OBJECT;
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,
231 if ( conn->c_version == 30 ) {
232 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
233 LDAP_RES_SEARCH_ENTRY, e->e_dn );
237 rc = ber_printf( ber, "{it{s{", op->o_msgid,
238 LDAP_RES_SEARCH_ENTRY, e->e_dn );
242 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
244 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
249 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
250 regmatch_t matches[MAXREMATCHES];
252 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
256 /* the lastmod attributes are ignored by ACL checking */
257 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
258 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
259 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
260 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
262 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
266 acl = acl_get_applicable( be, op, e, a->a_type, edn,
267 MAXREMATCHES, matches );
270 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op, ACL_READ,
276 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
277 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
279 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
280 NULL, "ber_printf type" );
285 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
286 if ( a->a_syntax & SYNTAX_DN &&
287 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
288 ACL_READ, edn, matches) )
293 if ( ber_printf( ber, "o",
294 a->a_vals[i]->bv_val,
295 a->a_vals[i]->bv_len ) == -1 )
297 Debug( LDAP_DEBUG_ANY,
298 "ber_printf failed\n", 0, 0, 0 );
300 send_ldap_result( conn, op,
301 LDAP_OPERATIONS_ERROR, NULL,
302 "ber_printf value" );
308 if ( ber_printf( ber, "]}" ) == -1 ) {
309 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
311 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
312 NULL, "ber_printf type end" );
320 if ( conn->c_version == 30 ) {
321 rc = ber_printf( ber, "}}}}" );
324 rc = ber_printf( ber, "}}}" );
327 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
329 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
330 "ber_printf entry end" );
334 /* write only one pdu at a time - wait til it's our turn */
335 pthread_mutex_lock( &conn->c_pdumutex );
337 bytes = ber->ber_ptr - ber->ber_buf;
338 pthread_mutex_lock( &new_conn_mutex );
339 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
341 pthread_mutex_unlock( &new_conn_mutex );
343 * we got an error. if it's ewouldblock, we need to
344 * wait on the socket being writable. otherwise, figure
345 * it's a hard error and return.
348 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
349 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
352 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
353 close_connection( conn, op->o_connid, op->o_opid );
355 pthread_mutex_unlock( &conn->c_pdumutex );
359 /* wait for socket to be write-ready */
360 pthread_mutex_lock( &active_threads_mutex );
362 conn->c_writewaiter = 1;
363 pthread_kill( listener_tid, SIGUSR1 );
364 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
365 pthread_mutex_unlock( &active_threads_mutex );
368 pthread_mutex_lock( &new_conn_mutex );
370 pthread_mutex_unlock( &new_conn_mutex );
371 pthread_mutex_unlock( &conn->c_pdumutex );
373 pthread_mutex_lock( &num_sent_mutex );
374 num_bytes_sent += bytes;
376 pthread_mutex_unlock( &num_sent_mutex );
378 pthread_mutex_lock( &new_conn_mutex );
379 if ( conn->c_connid == op->o_connid ) {
381 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
382 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
386 pthread_mutex_unlock( &new_conn_mutex );
388 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 pthread_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 pthread_mutex_unlock( &new_conn_mutex );