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