1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
12 #include <ac/unistd.h> /* get close() */
29 unsigned long tag, bytes;
31 if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
32 err = LDAP_NO_SUCH_OBJECT;
34 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
35 matched : "", text ? text : "" );
37 switch ( op->o_tag ) {
43 tag = LDAP_RES_SEARCH_RESULT;
47 tag = LDAP_RES_DELETE;
56 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
59 if ( (ber = der_alloc()) == NULLBER ) {
61 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
65 #ifdef LDAP_CONNECTIONLESS
67 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
68 err, matched ? matched : "", text ? text : "" );
72 if ( conn->c_version == 30 ) {
73 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
74 matched ? matched : "", text ? text : "" );
77 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
78 matched ? matched : "", text ? text : "" );
81 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
85 /* write only one pdu at a time - wait til it's our turn */
86 pthread_mutex_lock( &conn->c_pdumutex );
89 bytes = ber->ber_ptr - ber->ber_buf;
90 pthread_mutex_lock( &new_conn_mutex );
91 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
93 pthread_mutex_unlock( &new_conn_mutex );
95 * we got an error. if it's ewouldblock, we need to
96 * wait on the socket being writable. otherwise, figure
97 * it's a hard error and return.
100 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
101 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
104 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
105 close_connection( conn, op->o_connid, op->o_opid );
107 pthread_mutex_unlock( &conn->c_pdumutex );
111 /* wait for socket to be write-ready */
112 pthread_mutex_lock( &active_threads_mutex );
114 conn->c_writewaiter = 1;
116 #ifdef HAVE_LINUX_THREADS
117 pthread_kill( listener_tid, SIGSTKFLT );
119 pthread_kill( listener_tid, SIGUSR1 );
122 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
123 pthread_mutex_unlock( &active_threads_mutex );
126 pthread_mutex_lock( &new_conn_mutex );
128 pthread_mutex_unlock( &new_conn_mutex );
129 pthread_mutex_unlock( &conn->c_pdumutex );
131 pthread_mutex_lock( &num_sent_mutex );
132 num_bytes_sent += bytes;
133 pthread_mutex_unlock( &num_sent_mutex );
135 Statslog( LDAP_DEBUG_STATS,
136 "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
137 op->o_opid, err, tag, nentries );
151 #ifdef LDAP_CONNECTIONLESS
153 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
154 sizeof( struct sockaddr ));
155 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
156 inet_ntoa(((struct sockaddr_in *)
157 conn->c_sb.sb_useaddr)->sin_addr ),
158 ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
162 send_ldap_result2( conn, op, err, matched, text, 0 );
166 send_ldap_search_result(
175 send_ldap_result2( conn, op, err, matched, text, nentries );
190 int i, rc, bytes, sd;
194 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
196 if ( ! access_allowed( be, conn, op, e, "entry", NULL, op->o_dn,
198 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
203 edn = dn_normalize_case( strdup( e->e_dn ) );
206 if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
209 if ( (ber = der_alloc()) == NULLBER )
212 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
213 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
219 if ( conn->c_version == 30 ) {
220 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
221 LDAP_RES_SEARCH_ENTRY, e->e_dn );
225 rc = ber_printf( ber, "{it{s{", op->o_msgid,
226 LDAP_RES_SEARCH_ENTRY, e->e_dn );
230 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
232 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
237 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
238 regmatch_t matches[MAXREMATCHES];
240 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
244 /* the lastmod attributes are ignored by ACL checking */
245 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
246 strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
247 strcasecmp( a->a_type, "creatorsname" ) == 0 ||
248 strcasecmp( a->a_type, "createtimestamp" ) == 0 )
250 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
254 acl = acl_get_applicable( be, op, e, a->a_type, edn,
255 MAXREMATCHES, matches );
258 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op, ACL_READ,
264 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
265 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
267 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
268 NULL, "ber_printf type" );
273 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
274 if ( a->a_syntax & SYNTAX_DN &&
275 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
276 ACL_READ, edn, matches) )
281 if ( ber_printf( ber, "o",
282 a->a_vals[i]->bv_val,
283 a->a_vals[i]->bv_len ) == -1 )
285 Debug( LDAP_DEBUG_ANY,
286 "ber_printf failed\n", 0, 0, 0 );
288 send_ldap_result( conn, op,
289 LDAP_OPERATIONS_ERROR, NULL,
290 "ber_printf value" );
296 if ( ber_printf( ber, "]}" ) == -1 ) {
297 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
299 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
300 NULL, "ber_printf type end" );
308 if ( conn->c_version == 30 ) {
309 rc = ber_printf( ber, "}}}}" );
312 rc = ber_printf( ber, "}}}" );
315 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
317 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
318 "ber_printf entry end" );
322 /* write only one pdu at a time - wait til it's our turn */
323 pthread_mutex_lock( &conn->c_pdumutex );
325 bytes = ber->ber_ptr - ber->ber_buf;
326 pthread_mutex_lock( &new_conn_mutex );
327 while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
329 pthread_mutex_unlock( &new_conn_mutex );
331 * we got an error. if it's ewouldblock, we need to
332 * wait on the socket being writable. otherwise, figure
333 * it's a hard error and return.
336 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
337 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
340 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
341 close_connection( conn, op->o_connid, op->o_opid );
343 pthread_mutex_unlock( &conn->c_pdumutex );
347 /* wait for socket to be write-ready */
348 pthread_mutex_lock( &active_threads_mutex );
350 conn->c_writewaiter = 1;
351 pthread_kill( listener_tid, SIGUSR1 );
352 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
353 pthread_mutex_unlock( &active_threads_mutex );
356 pthread_mutex_lock( &new_conn_mutex );
358 pthread_mutex_unlock( &new_conn_mutex );
359 pthread_mutex_unlock( &conn->c_pdumutex );
361 pthread_mutex_lock( &num_sent_mutex );
362 num_bytes_sent += bytes;
364 pthread_mutex_unlock( &num_sent_mutex );
366 pthread_mutex_lock( &new_conn_mutex );
367 if ( conn->c_connid == op->o_connid ) {
369 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
370 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
374 pthread_mutex_unlock( &new_conn_mutex );
376 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
396 *code = LDAP_SUCCESS;
400 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
401 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
408 while ( (s = strchr( s, '\n' )) != NULL ) {
413 if ( (c = strchr( s, ':' )) != NULL ) {
417 if ( strncasecmp( s, "code", 4 ) == 0 ) {
421 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
425 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
430 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
440 * close_connection - close a connection. takes the connection to close,
441 * the connid associated with the operation generating the close (so we
442 * don't accidentally close a connection that's not ours), and the opid
443 * of the operation generating the close (for logging purposes).
446 close_connection( Connection *conn, int opconnid, int opid )
448 pthread_mutex_lock( &new_conn_mutex );
449 if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
450 Statslog( LDAP_DEBUG_STATS,
451 "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
452 opid, conn->c_sb.sb_sd, errno, 0 );
453 close( conn->c_sb.sb_sd );
454 conn->c_sb.sb_sd = -1;
457 pthread_mutex_unlock( &new_conn_mutex );