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