]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
Apply fix suggested by Ben Collins <bmc@visi.net>
[openldap] / servers / slapd / result.c
1 /* result.c - routines to send ldap results, errors, and referrals */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/errno.h>
8 #include <ac/signal.h>
9 #include <ac/socket.h>
10 #include <ac/string.h>
11 #include <ac/time.h>
12 #include <ac/unistd.h>          /* get close() */
13
14 #include "slap.h"
15
16
17 static void
18 send_ldap_result2(
19     Connection  *conn,
20     Operation   *op,
21     int         err,
22     char        *matched,
23     char        *text,
24     int         nentries
25 )
26 {
27         BerElement      *ber;
28         int             rc, sd;
29         unsigned long   tag, bytes;
30
31         if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
32                 err = LDAP_NO_SUCH_OBJECT;
33
34         Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
35             matched : "", text ? text : "" );
36
37         switch ( op->o_tag ) {
38         case LBER_DEFAULT:
39                 tag = LBER_SEQUENCE;
40                 break;
41
42         case LDAP_REQ_SEARCH:
43                 tag = LDAP_RES_SEARCH_RESULT;
44                 break;
45
46         case LDAP_REQ_DELETE:
47                 tag = LDAP_RES_DELETE;
48                 break;
49
50         default:
51                 tag = op->o_tag + 1;
52                 break;
53         }
54
55 #ifdef LDAP_COMPAT30
56         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
57             == NULLBER ) {
58 #else
59         if ( (ber = der_alloc()) == NULLBER ) {
60 #endif
61                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
62                 return;
63         }
64
65 #ifdef LDAP_CONNECTIONLESS
66         if ( op->o_cldap ) {
67                 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
68                     err, matched ? matched : "", text ? text : "" );
69         } else
70 #endif
71 #ifdef LDAP_COMPAT30
72         if ( conn->c_version == 30 ) {
73                 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
74                     matched ? matched : "", text ? text : "" );
75         } else
76 #endif
77                 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
78                     matched ? matched : "", text ? text : "" );
79
80         if ( rc == -1 ) {
81                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
82                 return;
83         }
84
85         /* write only one pdu at a time - wait til it's our turn */
86         pthread_mutex_lock( &conn->c_pdumutex );
87
88         /* write the pdu */
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,
92             1 ) != 0 ) {
93                 pthread_mutex_unlock( &new_conn_mutex );
94                 /*
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.
98                  */
99
100                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
101                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
102                     : "unknown", 0 );
103
104                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
105                         close_connection( conn, op->o_connid, op->o_opid );
106
107                         pthread_mutex_unlock( &conn->c_pdumutex );
108                         return;
109                 }
110
111                 /* wait for socket to be write-ready */
112                 pthread_mutex_lock( &active_threads_mutex );
113                 active_threads--;
114                 conn->c_writewaiter = 1;
115
116                 pthread_kill( listener_tid, LDAP_SIGUSR1 );
117
118                 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
119                 pthread_mutex_unlock( &active_threads_mutex );
120
121                 pthread_yield();
122                 pthread_mutex_lock( &new_conn_mutex );
123         }
124         pthread_mutex_unlock( &new_conn_mutex );
125         pthread_mutex_unlock( &conn->c_pdumutex );
126
127         pthread_mutex_lock( &num_sent_mutex );
128         num_bytes_sent += bytes;
129         pthread_mutex_unlock( &num_sent_mutex );
130
131         Statslog( LDAP_DEBUG_STATS,
132             "conn=%d op=%d RESULT err=%d tag=%d nentries=%d\n", conn->c_connid,
133             op->o_opid, err, tag, nentries );
134
135         return;
136 }
137
138 void
139 send_ldap_result(
140     Connection  *conn,
141     Operation   *op,
142     int         err,
143     char        *matched,
144     char        *text
145 )
146 {
147 #ifdef LDAP_CONNECTIONLESS
148         if ( op->o_cldap ) {
149                 SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
150                     sizeof( struct sockaddr ));
151                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
152                     inet_ntoa(((struct sockaddr_in *)
153                     conn->c_sb.sb_useaddr)->sin_addr ),
154                     ((struct sockaddr_in *) conn->c_sb.sb_useaddr)->sin_port,
155                     0 );
156         }
157 #endif
158         send_ldap_result2( conn, op, err, matched, text, 0 );
159 }
160
161 void
162 send_ldap_search_result(
163     Connection  *conn,
164     Operation   *op,
165     int         err,
166     char        *matched,
167     char        *text,
168     int         nentries
169 )
170 {
171         send_ldap_result2( conn, op, err, matched, text, nentries );
172 }
173
174 int
175 send_search_entry(
176     Backend     *be,
177     Connection  *conn,
178     Operation   *op,
179     Entry       *e,
180     char        **attrs,
181     int         attrsonly
182 )
183 {
184         BerElement      *ber;
185         Attribute       *a;
186         int             i, rc, bytes, sd;
187         struct acl      *acl;
188         char            *edn;
189
190         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
191
192         if ( ! access_allowed( be, conn, op, e, "entry", NULL, op->o_dn,
193             ACL_READ ) ) {
194                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
195                     0, 0, 0 );
196                 return( 1 );
197         }
198
199         edn = dn_normalize_case( ch_strdup( e->e_dn ) );
200
201 #ifdef LDAP_COMPAT30
202         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
203                 == NULLBER )
204 #else
205         if ( (ber = der_alloc()) == NULLBER )
206 #endif
207         {
208                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
209                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
210                         "ber_alloc" );
211                 goto error_return;
212         }
213
214 #ifdef LDAP_COMPAT30
215         if ( conn->c_version == 30 ) {
216                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
217                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
218         } else
219 #endif
220         {
221                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
222                         LDAP_RES_SEARCH_ENTRY, e->e_dn );
223         }
224
225         if ( rc == -1 ) {
226                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
227                 ber_free( ber, 1 );
228                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
229                     "ber_printf dn" );
230                 goto error_return;
231         }
232
233         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
234                 regmatch_t       matches[MAXREMATCHES];
235
236                 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
237                         continue;
238                 }
239
240                 /* the lastmod attributes are ignored by ACL checking */
241                 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
242                         strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
243                         strcasecmp( a->a_type, "creatorsname" ) == 0 ||
244                         strcasecmp( a->a_type, "createtimestamp" ) == 0 ) 
245                 {
246                         Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
247                                 a->a_type, 0, 0 );
248                         acl = NULL;
249                 } else {
250                         acl = acl_get_applicable( be, op, e, a->a_type, edn,
251                                 MAXREMATCHES, matches );
252                 }
253
254                 if ( ! acl_access_allowed( acl, be, conn, e, NULL, op, ACL_READ,
255                         edn, matches ) ) 
256                 {
257                         continue;
258                 }
259
260                 if ( ber_printf( ber, "{s[", a->a_type ) == -1 ) {
261                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
262                         ber_free( ber, 1 );
263                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
264                             NULL, "ber_printf type" );
265                         goto error_return;
266                 }
267
268                 if ( ! attrsonly ) {
269                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
270                                 if ( a->a_syntax & SYNTAX_DN && 
271                                         ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
272                                                 ACL_READ, edn, matches) )
273                                 {
274                                         continue;
275                                 }
276
277                                 if ( ber_printf( ber, "o",
278                                     a->a_vals[i]->bv_val,
279                                     a->a_vals[i]->bv_len ) == -1 )
280                                 {
281                                         Debug( LDAP_DEBUG_ANY,
282                                             "ber_printf failed\n", 0, 0, 0 );
283                                         ber_free( ber, 1 );
284                                         send_ldap_result( conn, op,
285                                             LDAP_OPERATIONS_ERROR, NULL,
286                                             "ber_printf value" );
287                                         goto error_return;
288                                 }
289                         }
290                 }
291
292                 if ( ber_printf( ber, "]}" ) == -1 ) {
293                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
294                         ber_free( ber, 1 );
295                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
296                             NULL, "ber_printf type end" );
297                         goto error_return;
298                 }
299         }
300
301         free(edn);
302
303 #ifdef LDAP_COMPAT30
304         if ( conn->c_version == 30 ) {
305                 rc = ber_printf( ber, "}}}}" );
306         } else
307 #endif
308                 rc = ber_printf( ber, "}}}" );
309
310         if ( rc == -1 ) {
311                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
312                 ber_free( ber, 1 );
313                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
314                     "ber_printf entry end" );
315                 return( 1 );
316         }
317
318         /* write only one pdu at a time - wait til it's our turn */
319         pthread_mutex_lock( &conn->c_pdumutex );
320
321         bytes = ber->ber_ptr - ber->ber_buf;
322         pthread_mutex_lock( &new_conn_mutex );
323         while ( conn->c_connid == op->o_connid && ber_flush( &conn->c_sb, ber,
324             1 ) != 0 ) {
325                 pthread_mutex_unlock( &new_conn_mutex );
326                 /*
327                  * we got an error.  if it's ewouldblock, we need to
328                  * wait on the socket being writable.  otherwise, figure
329                  * it's a hard error and return.
330                  */
331
332                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
333                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
334                     : "unknown", 0 );
335
336                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
337                         close_connection( conn, op->o_connid, op->o_opid );
338
339                         pthread_mutex_unlock( &conn->c_pdumutex );
340                         return( -1 );
341                 }
342
343                 /* wait for socket to be write-ready */
344                 pthread_mutex_lock( &active_threads_mutex );
345                 active_threads--;
346                 conn->c_writewaiter = 1;
347                 pthread_kill( listener_tid, LDAP_SIGUSR1 );
348                 pthread_cond_wait( &conn->c_wcv, &active_threads_mutex );
349                 pthread_mutex_unlock( &active_threads_mutex );
350
351                 pthread_yield();
352                 pthread_mutex_lock( &new_conn_mutex );
353         }
354         pthread_mutex_unlock( &new_conn_mutex );
355         pthread_mutex_unlock( &conn->c_pdumutex );
356
357         pthread_mutex_lock( &num_sent_mutex );
358         num_bytes_sent += bytes;
359         num_entries_sent++;
360         pthread_mutex_unlock( &num_sent_mutex );
361
362         pthread_mutex_lock( &new_conn_mutex );
363         if ( conn->c_connid == op->o_connid ) {
364                 rc = 0;
365                 Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
366                     conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
367         } else {
368                 rc = -1;
369         }
370         pthread_mutex_unlock( &new_conn_mutex );
371
372         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
373
374         return( rc );
375
376 error_return:;
377         free(edn);
378         return( 1 );
379 }
380
381 int
382 str2result(
383     char        *s,
384     int         *code,
385     char        **matched,
386     char        **info
387 )
388 {
389         int     rc;
390         char    *c;
391
392         *code = LDAP_SUCCESS;
393         *matched = NULL;
394         *info = NULL;
395
396         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
397                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
398                     s, 0, 0 );
399
400                 return( -1 );
401         }
402
403         rc = 0;
404         while ( (s = strchr( s, '\n' )) != NULL ) {
405                 *s++ = '\0';
406                 if ( *s == '\0' ) {
407                         break;
408                 }
409                 if ( (c = strchr( s, ':' )) != NULL ) {
410                         c++;
411                 }
412
413                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
414                         if ( c != NULL ) {
415                                 *code = atoi( c );
416                         }
417                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
418                         if ( c != NULL ) {
419                                 *matched = c;
420                         }
421                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
422                         if ( c != NULL ) {
423                                 *info = c;
424                         }
425                 } else {
426                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
427                             s, 0, 0 );
428                         rc = -1;
429                 }
430         }
431
432         return( rc );
433 }
434
435 /*
436  * close_connection - close a connection. takes the connection to close,
437  * the connid associated with the operation generating the close (so we
438  * don't accidentally close a connection that's not ours), and the opid
439  * of the operation generating the close (for logging purposes).
440  */
441 void
442 close_connection( Connection *conn, int opconnid, int opid )
443 {
444         pthread_mutex_lock( &new_conn_mutex );
445         if ( conn->c_sb.sb_sd != -1 && conn->c_connid == opconnid ) {
446                 Statslog( LDAP_DEBUG_STATS,
447                     "conn=%d op=%d fd=%d closed errno=%d\n", conn->c_connid,
448                     opid, conn->c_sb.sb_sd, errno, 0 );
449                 close( conn->c_sb.sb_sd );
450                 conn->c_sb.sb_sd = -1;
451                 conn->c_version = 0;
452         }
453         pthread_mutex_unlock( &new_conn_mutex );
454 }