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