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