]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
5f954f19df30bf20659328b527e12b66d0c5e99c
[openldap] / servers / slapd / result.c
1 /* result.c - routines to send ldap results, errors, and referrals */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/errno.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/ctype.h>
17 #include <ac/time.h>
18 #include <ac/unistd.h>
19
20 #include "slap.h"
21
22 static char *v2ref( struct berval **ref, const char *text )
23 {
24         size_t len = 0, i = 0;
25         char *v2;
26
27         if(ref == NULL) {
28                 if (text) {
29                         return ch_strdup(text);
30                 } else {
31                         return NULL;
32                 }
33         }
34         
35         if ( text != NULL ) {
36                 len = strlen( text );
37                 if (text[len-1] != '\n') {
38                     i = 1;
39                 }
40         }
41
42         v2 = ch_malloc( len+i+sizeof("Referral:") );
43         if( text != NULL ) {
44                 strcpy(v2, text);
45                 if( i ) {
46                         v2[len++] = '\n';
47                 }
48         }
49         strcpy( v2+len, "Referral:" );
50         len += sizeof("Referral:");
51
52         for( i=0; ref[i] != NULL; i++ ) {
53                 v2 = ch_realloc( v2, len + ref[i]->bv_len + 1 );
54                 v2[len-1] = '\n';
55                 AC_MEMCPY(&v2[len], ref[i]->bv_val, ref[i]->bv_len );
56                 len += ref[i]->bv_len;
57                 if (ref[i]->bv_val[ref[i]->bv_len-1] != '/') {
58                         ++len;
59                 }
60         }
61
62         v2[len-1] = '\0';
63         return v2;
64 }
65
66 static ber_tag_t req2res( ber_tag_t tag )
67 {
68         switch( tag ) {
69         case LDAP_REQ_ADD:
70         case LDAP_REQ_BIND:
71         case LDAP_REQ_COMPARE:
72         case LDAP_REQ_EXTENDED:
73         case LDAP_REQ_MODIFY:
74         case LDAP_REQ_MODRDN:
75                 tag++;
76                 break;
77
78         case LDAP_REQ_DELETE:
79                 tag = LDAP_RES_DELETE;
80                 break;
81
82         case LDAP_REQ_ABANDON:
83         case LDAP_REQ_UNBIND:
84                 tag = LBER_SEQUENCE;
85                 break;
86
87         case LDAP_REQ_SEARCH:
88                 tag = LDAP_RES_SEARCH_RESULT;
89                 break;
90
91         default:
92                 tag = LBER_SEQUENCE;
93         }
94
95         return tag;
96 }
97
98 static long send_ldap_ber(
99         Connection *conn,
100         BerElement *ber )
101 {
102         ber_len_t bytes;
103
104         ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
105
106         /* write only one pdu at a time - wait til it's our turn */
107         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
108
109         /* lock the connection */ 
110         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
111
112         /* write the pdu */
113         while( 1 ) {
114                 int err;
115                 ber_socket_t    sd;
116
117                 if ( connection_state_closing( conn ) ) {
118                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
119                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
120
121                         return 0;
122                 }
123
124                 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
125                         break;
126                 }
127
128                 err = errno;
129
130                 /*
131                  * we got an error.  if it's ewouldblock, we need to
132                  * wait on the socket being writable.  otherwise, figure
133                  * it's a hard error and return.
134                  */
135
136 #ifdef NEW_LOGGING
137                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
138                            "send_ldap_ber: conn %d  ber_flush failed err=%d (%s)\n",
139                            conn ? conn->c_connid : 0, err, sock_errstr(err) ));
140 #else
141                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
142                     err, sock_errstr(err), 0 );
143 #endif
144
145
146                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
147                         connection_closing( conn );
148
149                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
150                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
151
152                         return( -1 );
153                 }
154
155                 /* wait for socket to be write-ready */
156                 conn->c_writewaiter = 1;
157                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
158                 slapd_set_write( sd, 1 );
159
160                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
161                 conn->c_writewaiter = 0;
162         }
163
164         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
165         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
166
167         return bytes;
168 }
169
170 static void
171 send_ldap_response(
172     Connection  *conn,
173     Operation   *op,
174         ber_tag_t       tag,
175         ber_int_t       msgid,
176     ber_int_t   err,
177     const char  *matched,
178     const char  *text,
179         struct berval   **ref,
180         const char      *resoid,
181         struct berval   *resdata,
182         struct berval   *sasldata,
183         LDAPControl **ctrls
184 )
185 {
186         BerElement      *ber;
187         int             rc;
188         long    bytes;
189
190         assert( ctrls == NULL ); /* ctrls not implemented */
191
192         if (op->o_response)
193                 return op->o_response( conn, op, tag, msgid, err, matched,
194                         text, ref, resoid, resdata, sasldata, ctrls);
195                 
196         ber = ber_alloc_t( LBER_USE_DER );
197
198 #ifdef NEW_LOGGING
199         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
200                    "send_ldap_response: conn %d  msgid=%ld tag=%ld err=%ld\n",
201                    conn ? conn->c_connid : 0, (long)msgid, (long)tag, (long)err ));
202 #else
203         Debug( LDAP_DEBUG_TRACE,
204                 "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
205                 (long) msgid, (long) tag, (long) err );
206 #endif
207
208
209         if( ref ) {
210 #ifdef NEW_LOGGING
211                 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
212                            "send_ldap_response: conn %d  ref=\"%s\"\n",
213                            conn ? conn->c_connid : 0,
214                            ref[0] && ref[0]->bv_val ? ref[0]->bv_val : "NULL" ));
215 #else
216                 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
217                         ref[0] && ref[0]->bv_val ? ref[0]->bv_val : "NULL",
218                         NULL, NULL );
219 #endif
220
221         }
222
223         if ( ber == NULL ) {
224 #ifdef NEW_LOGGING
225                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
226                            "send_ldap_response: conn %d  ber_alloc failed\n",
227                            conn ? conn->c_connid : 0 ));
228 #else
229                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
230 #endif
231
232                 return;
233         }
234
235 #ifdef LDAP_CONNECTIONLESS
236         if (conn->c_is_udp) {
237             rc = ber_write(ber, (char *)&op->o_peeraddr, sizeof(struct sockaddr), 0);
238             if (rc != sizeof(struct sockaddr)) {
239 #ifdef NEW_LOGGING
240                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
241                            "send_ldap_response: conn %d  ber_write failed\n",
242                            conn ? conn->c_connid : 0 ));
243 #else
244                 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
245 #endif
246                 ber_free(ber, 1);
247                 return;
248             }
249         }
250         if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
251             rc = ber_printf( ber, "{is{t{ess",
252                 msgid, "", tag, err,
253                 matched == NULL ? "" : matched,
254                 text == NULL ? "" : text );
255         } else
256 #endif
257         {
258             rc = ber_printf( ber, "{it{ess",
259                 msgid, tag, err,
260                 matched == NULL ? "" : matched,
261                 text == NULL ? "" : text );
262         }
263
264         if( rc != -1 ) {
265                 if ( ref != NULL ) {
266                         assert( err == LDAP_REFERRAL );
267                         rc = ber_printf( ber, "t{V}",
268                                 LDAP_TAG_REFERRAL, ref );
269                 } else {
270                         assert( err != LDAP_REFERRAL );
271                 }
272         }
273
274         if( rc != -1 && sasldata != NULL ) {
275                 rc = ber_printf( ber, "tO",
276                         LDAP_TAG_SASL_RES_CREDS, sasldata );
277         }
278
279         if( rc != -1 && resoid != NULL ) {
280                 rc = ber_printf( ber, "ts",
281                         LDAP_TAG_EXOP_RES_OID, resoid );
282         }
283
284         if( rc != -1 && resdata != NULL ) {
285                 rc = ber_printf( ber, "tO",
286                         LDAP_TAG_EXOP_RES_VALUE, resdata );
287         }
288
289         if( rc != -1 ) {
290                 rc = ber_printf( ber, "N}N}" );
291         }
292 #ifdef LDAP_CONNECTIONLESS
293         if( conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
294                 rc = ber_printf( ber, "N}" );
295         }
296 #endif
297
298         if ( rc == -1 ) {
299 #ifdef NEW_LOGGING
300                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
301                            "send_ldap_response: conn %d  ber_printf failed\n",
302                            conn ? conn->c_connid : 0 ));
303 #else
304                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
305 #endif
306
307                 ber_free( ber, 1 );
308                 return;
309         }
310
311         /* send BER */
312         bytes = send_ldap_ber( conn, ber );
313         ber_free( ber, 1 );
314
315         if ( bytes < 0 ) {
316 #ifdef NEW_LOGGING
317                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
318                            "send_ldap_response: conn %d ber write failed\n",
319                            conn ? conn->c_connid : 0 ));
320 #else
321                 Debug( LDAP_DEBUG_ANY,
322                         "send_ldap_response: ber write failed\n",
323                         0, 0, 0 );
324 #endif
325
326                 return;
327         }
328
329         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
330         num_bytes_sent += bytes;
331         num_pdu_sent++;
332         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
333         return;
334 }
335
336
337 void
338 send_ldap_disconnect(
339     Connection  *conn,
340     Operation   *op,
341     ber_int_t   err,
342     const char  *text
343 )
344 {
345         ber_tag_t tag;
346         ber_int_t msgid;
347         char *reqoid;
348
349 #define LDAP_UNSOLICITED_ERROR(e) \
350         (  (e) == LDAP_PROTOCOL_ERROR \
351         || (e) == LDAP_STRONG_AUTH_REQUIRED \
352         || (e) == LDAP_UNAVAILABLE )
353
354         assert( LDAP_UNSOLICITED_ERROR( err ) );
355
356 #ifdef NEW_LOGGING
357         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
358                    "send_ldap_disconnect: conn %d  %d:%s\n",
359                    conn ? conn->c_connid : 0, err, text ? text : "" ));
360 #else
361         Debug( LDAP_DEBUG_TRACE,
362                 "send_ldap_disconnect %d:%s\n",
363                 err, text ? text : "", NULL );
364 #endif
365
366
367         if ( op->o_protocol < LDAP_VERSION3 ) {
368                 reqoid = NULL;
369                 tag = req2res( op->o_tag );
370                 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
371
372         } else {
373                 reqoid = LDAP_NOTICE_DISCONNECT;
374                 tag = LDAP_RES_EXTENDED;
375                 msgid = 0;
376         }
377
378         send_ldap_response( conn, op, tag, msgid,
379                 err, NULL, text, NULL,
380                 reqoid, NULL, NULL, NULL );
381
382         Statslog( LDAP_DEBUG_STATS,
383             "conn=%ld op=%ld DISCONNECT tag=%lu err=%ld text=%s\n",
384                 (long) op->o_connid, (long) op->o_opid,
385                 (unsigned long) tag, (long) err, text ? text : "" );
386 }
387
388 void
389 send_ldap_result(
390     Connection  *conn,
391     Operation   *op,
392     ber_int_t   err,
393     const char  *matched,
394     const char  *text,
395         struct berval **ref,
396         LDAPControl **ctrls
397 )
398 {
399         ber_tag_t tag;
400         ber_int_t msgid;
401         char *tmp = NULL;
402
403         assert( !LDAP_API_ERROR( err ) );
404
405 #ifdef NEW_LOGGING
406         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
407                    "send_ldap_result : conn %ld   op=%ld p=%d\n",
408                    (long)op->o_connid, (long)op->o_opid, op->o_protocol ));
409 #else
410         Debug( LDAP_DEBUG_TRACE,
411                 "send_ldap_result: conn=%ld op=%ld p=%d\n",
412                 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
413 #endif
414
415 #ifdef NEW_LOGGING
416         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
417                    "send_ldap_result: conn=%ld err=%d matched=\"%s\" text=\"%s\"\n",
418                    (long)op->o_connid, err, matched ? matched : "", text ? text : "" ));
419 #else
420         Debug( LDAP_DEBUG_ARGS,
421                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
422                 err, matched ?  matched : "", text ? text : "" );
423 #endif
424
425
426         if( ref ) {
427 #ifdef NEW_LOGGING
428                 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
429                         "send_ldap_result: referral=\"%s\"\n",
430                         ref[0] && ref[0]->bv_val ? ref[0]->bv_val : "NULL" ));
431 #else
432                 Debug( LDAP_DEBUG_ARGS,
433                         "send_ldap_result: referral=\"%s\"\n",
434                         ref[0] && ref[0]->bv_val ? ref[0]->bv_val : "NULL",
435                         NULL, NULL );
436 #endif
437         }
438
439         assert( err != LDAP_PARTIAL_RESULTS );
440
441         if ( err == LDAP_REFERRAL ) {
442                 if( ref == NULL ) {
443                         err = LDAP_NO_SUCH_OBJECT;
444                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
445                         err = LDAP_PARTIAL_RESULTS;
446                 }
447         }
448
449         if ( op->o_protocol < LDAP_VERSION3 ) {
450                 tmp = v2ref( ref, text );
451                 text = tmp;
452                 ref = NULL;
453         }
454
455         tag = req2res( op->o_tag );
456         msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
457
458         send_ldap_response( conn, op, tag, msgid,
459                 err, matched, text, ref,
460                 NULL, NULL, NULL, ctrls );
461
462         Statslog( LDAP_DEBUG_STATS,
463             "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
464                 (long) op->o_connid, (long) op->o_opid,
465                 (unsigned long) tag, (long) err, text ? text : "" );
466
467         if( tmp != NULL ) {
468                 ch_free(tmp);
469         }
470 }
471
472 void
473 send_ldap_sasl(
474     Connection  *conn,
475     Operation   *op,
476     ber_int_t   err,
477     const char  *matched,
478     const char  *text,
479         struct berval **ref,
480         LDAPControl **ctrls,
481         struct berval *cred
482 )
483 {
484         ber_tag_t tag;
485         ber_int_t msgid;
486
487 #ifdef NEW_LOGGING
488         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
489                    "send_ldap_sasl: conn %d err=%ld len=%ld\n",
490                    op->o_connid, (long)err, cred ? cred->bv_len : -1 ));
491 #else
492         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%ld len=%ld\n",
493                 (long) err, cred ? cred->bv_len : -1, NULL );
494 #endif
495
496
497         tag = req2res( op->o_tag );
498         msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
499
500         send_ldap_response( conn, op, tag, msgid,
501                 err, matched, text, ref,
502                 NULL, NULL, cred, ctrls  );
503 }
504
505 void
506 send_ldap_extended(
507     Connection  *conn,
508     Operation   *op,
509     ber_int_t   err,
510     const char  *matched,
511     const char  *text,
512     struct berval **refs,
513     const char          *rspoid,
514         struct berval *rspdata,
515         LDAPControl **ctrls
516 )
517 {
518         ber_tag_t tag;
519         ber_int_t msgid;
520
521 #ifdef NEW_LOGGING
522         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
523                    "send_ldap_extended: conn %d  err=%ld oid=%s len=%ld\n",
524                    op->o_connid, (long)err, rspoid ? rspoid : "",
525                    rspdata != NULL ? (long)rspdata->bv_len : (long)0 ));
526 #else
527         Debug( LDAP_DEBUG_TRACE,
528                 "send_ldap_extended err=%ld oid=%s len=%ld\n",
529                 (long) err,
530                 rspoid ? rspoid : "",
531                 rspdata != NULL ? (long) rspdata->bv_len : (long) 0 );
532 #endif
533
534
535         tag = req2res( op->o_tag );
536         msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
537
538         send_ldap_response( conn, op, tag, msgid,
539                 err, matched, text, refs,
540                 rspoid, rspdata, NULL, ctrls );
541 }
542
543
544 void
545 send_search_result(
546     Connection  *conn,
547     Operation   *op,
548     ber_int_t   err,
549     const char  *matched,
550         const char      *text,
551     struct berval **refs,
552         LDAPControl **ctrls,
553     int         nentries
554 )
555 {
556         ber_tag_t tag;
557         ber_int_t msgid;
558         char *tmp = NULL;
559         assert( !LDAP_API_ERROR( err ) );
560
561         if (op->o_sresult)
562                 return op->o_sresult(conn, op, err, matched, text, refs,
563                         ctrls, nentries);
564
565 #ifdef NEW_LOGGING
566         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
567                    "send_search_result: conn %d err=%d matched=\"%s\"\n",
568                    op->o_connid, err, matched ? matched : "",
569                    text ? text : "" ));
570 #else
571         Debug( LDAP_DEBUG_TRACE,
572                 "send_search_result: err=%d matched=\"%s\" text=\"%s\"\n",
573                 err, matched ?  matched : "", text ? text : "" );
574 #endif
575
576
577         assert( err != LDAP_PARTIAL_RESULTS );
578
579         if( op->o_protocol < LDAP_VERSION3 ) {
580                 /* send references in search results */
581                 if( err == LDAP_REFERRAL ) {
582                         err = LDAP_PARTIAL_RESULTS;
583                 }
584
585                 tmp = v2ref( refs, text );
586                 text = tmp;
587                 refs = NULL;
588
589         } else {
590                 /* don't send references in search results */
591                 assert( refs == NULL );
592                 refs = NULL;
593
594                 if( err == LDAP_REFERRAL ) {
595                         err = LDAP_SUCCESS;
596                 }
597         }
598
599         tag = req2res( op->o_tag );
600         msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
601
602         send_ldap_response( conn, op, tag, msgid,
603                 err, matched, text, refs,
604                 NULL, NULL, NULL, ctrls );
605
606         Statslog( LDAP_DEBUG_STATS,
607             "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
608                 (long) op->o_connid, (long) op->o_opid,
609                 (unsigned long) tag, (long) err, text ? text : "" );
610
611         if (tmp != NULL) {
612             ch_free(tmp);
613         }
614 }
615
616
617 int
618 send_search_entry(
619     Backend     *be,
620     Connection  *conn,
621     Operation   *op,
622     Entry       *e,
623     char        **attrs,
624     int         attrsonly,
625         LDAPControl **ctrls
626 )
627 {
628         BerElement      *ber;
629         Attribute       *a, *aa;
630         int             i, rc=-1, bytes;
631         char            *edn;
632         int             userattrs;
633         int             opattrs;
634
635         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
636
637 #ifdef NEW_LOGGING
638         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
639                    "send_search_entry: conn %d  dn=\"%s\"%s\n",
640                    op->o_connid, e->e_dn,
641                    attrsonly ? " (attrsOnly)" : "" ));
642 #else
643         Debug( LDAP_DEBUG_TRACE,
644                 "=> send_search_entry: dn=\"%s\"%s\n",
645                 e->e_dn, attrsonly ? " (attrsOnly)" : "", 0 );
646 #endif
647
648
649         if ( ! access_allowed( be, conn, op, e,
650                 ad_entry, NULL, ACL_READ ) )
651         {
652 #ifdef NEW_LOGGING
653                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
654                            "send_search_entry: conn %d access to entry (%s) not allowed\n",
655                            op->o_connid, e->e_dn ));
656 #else
657                 Debug( LDAP_DEBUG_ACL,
658                         "send_search_entry: access to entry not allowed\n",
659                     0, 0, 0 );
660 #endif
661
662                 return( 1 );
663         }
664
665         edn = e->e_ndn;
666
667         ber = ber_alloc_t( LBER_USE_DER );
668
669         if ( ber == NULL ) {
670 #ifdef NEW_LOGGING
671                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
672                            "send_search_entry: conn %d  ber_alloc failed\n",
673                            op->o_connid ));
674 #else
675                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
676 #endif
677
678                 send_ldap_result( conn, op, LDAP_OTHER,
679                         NULL, "BER allocation error", NULL, NULL );
680                 goto error_return;
681         }
682
683 #ifdef LDAP_CONNECTIONLESS
684         if (conn->c_is_udp) {
685             rc = ber_write(ber, (char *)&op->o_peeraddr, sizeof(struct sockaddr), 0);
686             if (rc != sizeof(struct sockaddr)) {
687 #ifdef NEW_LOGGING
688                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
689                            "send_search_entry: conn %d  ber_printf failed\n",
690                            conn ? conn->c_connid : 0 ));
691 #else
692                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
693 #endif
694                 ber_free(ber, 1);
695                 return;
696             }
697         }
698         if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
699             rc = ber_printf( ber, "{is{t{s{" /*}}}*/,
700                 op->o_msgid, "", LDAP_RES_SEARCH_ENTRY, e->e_dn );
701         } else
702 #endif
703         {
704             rc = ber_printf( ber, "{it{s{" /*}}}*/, op->o_msgid,
705                 LDAP_RES_SEARCH_ENTRY, e->e_dn );
706         }
707
708         if ( rc == -1 ) {
709 #ifdef NEW_LOGGING
710                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
711                            "send_search_entry: conn %d  ber_printf failed\n",
712                            op->o_connid ));
713 #else
714                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
715 #endif
716
717                 ber_free( ber, 1 );
718                 send_ldap_result( conn, op, LDAP_OTHER,
719                     NULL, "encoding DN error", NULL, NULL );
720                 goto error_return;
721         }
722
723         /* check for special all user attributes ("*") type */
724         userattrs = ( attrs == NULL ) ? 1
725                 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
726
727         /* check for special all operational attributes ("+") type */
728         opattrs = ( attrs == NULL ) ? 0
729                 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
730
731         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
732                 AttributeDescription *desc = a->a_desc;
733                 char *type = desc->ad_cname.bv_val;
734
735                 if ( attrs == NULL ) {
736                         /* all addrs request, skip operational attributes */
737                         if( is_at_operational( desc->ad_type ) ) {
738                                 continue;
739                         }
740
741                 } else {
742                         /* specific addrs requested */
743                         if ( is_at_operational( desc->ad_type ) ) {
744                                 if( !opattrs && !ad_inlist( desc, attrs ) ) {
745                                         continue;
746                                 }
747
748                         } else {
749                                 if (!userattrs && !ad_inlist( desc, attrs ) ) {
750                                         continue;
751                                 }
752                         }
753                 }
754
755                 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
756 #ifdef NEW_LOGGING
757                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
758                                    "send_search_entry: conn %d  access to attribute %s not allowed\n",
759                                    op->o_connid, desc->ad_cname.bv_val ));
760 #else
761                         Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
762                             desc->ad_cname.bv_val, 0, 0 );
763 #endif
764
765                         continue;
766                 }
767
768                 if (( rc = ber_printf( ber, "{s[" /*]}*/ , type )) == -1 ) {
769 #ifdef NEW_LOGGING
770                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
771                                    "send_search_entry: conn %d  ber_printf failed\n",
772                                    op->o_connid ));
773 #else
774                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
775 #endif
776
777                         ber_free( ber, 1 );
778                         send_ldap_result( conn, op, LDAP_OTHER,
779                             NULL, "encoding description error", NULL, NULL );
780                         goto error_return;
781                 }
782
783                 if ( ! attrsonly ) {
784                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
785                                 if ( ! access_allowed( be, conn, op, e,
786                                         desc, a->a_vals[i], ACL_READ ) )
787                                 {
788 #ifdef NEW_LOGGING
789                                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
790                                                    "send_search_entry: conn %d  access to attribute %s, value %d not allowed\n",
791                                                    op->o_connid, desc->ad_cname.bv_val, i ));
792 #else
793                                         Debug( LDAP_DEBUG_ACL,
794                                                 "acl: access to attribute %s, value %d not allowed\n",
795                                         desc->ad_cname.bv_val, i, 0 );
796 #endif
797
798                                         continue;
799                                 }
800
801                                 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
802 #ifdef NEW_LOGGING
803                                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
804                                                    "send_search_entry: conn %d  ber_printf failed.\n",
805                                                    op->o_connid ));
806 #else
807                                         Debug( LDAP_DEBUG_ANY,
808                                             "ber_printf failed\n", 0, 0, 0 );
809 #endif
810
811                                         ber_free( ber, 1 );
812                                         send_ldap_result( conn, op, LDAP_OTHER,
813                                                 NULL, "encoding values error", NULL, NULL );
814                                         goto error_return;
815                                 }
816                         }
817                 }
818
819                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
820 #ifdef NEW_LOGGING
821                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
822                                    "send_search_entry: conn %d  ber_printf failed\n",
823                                    op->o_connid ));
824 #else
825                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
826 #endif
827
828                         ber_free( ber, 1 );
829                         send_ldap_result( conn, op, LDAP_OTHER,
830                             NULL, "encode end error", NULL, NULL );
831                         goto error_return;
832                 }
833         }
834
835         /* eventually will loop through generated operational attributes */
836         /* only have subschemaSubentry implemented */
837         aa = backend_operational( be, conn, op, e );
838         
839         for (a = aa ; a != NULL; a = a->a_next ) {
840                 AttributeDescription *desc = a->a_desc;
841
842                 if ( attrs == NULL ) {
843                         /* all addrs request, skip operational attributes */
844                         if( is_at_operational( desc->ad_type ) ) {
845                                 continue;
846                         }
847
848                 } else {
849                         /* specific addrs requested */
850                         if( is_at_operational( desc->ad_type ) ) {
851                                 if( !opattrs && !ad_inlist( desc, attrs ) ) {
852                                         continue;
853                                 }
854                         } else {
855                                 if (!userattrs && !ad_inlist( desc, attrs ) )
856                                 {
857                                         continue;
858                                 }
859                         }
860                 }
861
862                 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
863 #ifdef NEW_LOGGING
864                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
865                                    "send_search_entry: conn %s  access to attribute %s not allowed\n",
866                                    op->o_connid, desc->ad_cname.bv_val ));
867 #else
868                         Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
869                             desc->ad_cname.bv_val, 0, 0 );
870 #endif
871
872                         continue;
873                 }
874
875                 rc = ber_printf( ber, "{s[" /*]}*/ , desc->ad_cname.bv_val );
876                 if ( rc == -1 ) {
877 #ifdef NEW_LOGGING
878                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
879                                    "send_search_entry: conn %d  ber_printf failed\n",
880                                    op->o_connid ));
881 #else
882                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
883 #endif
884
885                         ber_free( ber, 1 );
886                         send_ldap_result( conn, op, LDAP_OTHER,
887                             NULL, "encoding description error", NULL, NULL );
888                         attrs_free( aa );
889                         goto error_return;
890                 }
891
892                 if ( ! attrsonly ) {
893                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
894                                 if ( ! access_allowed( be, conn, op, e,
895                                         desc, a->a_vals[i], ACL_READ ) )
896                                 {
897 #ifdef NEW_LOGGING
898                                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
899                                                    "send_search_entry: conn %d access to %s, value %d not allowed\n",
900                                                    op->o_connid, desc->ad_cname.bv_val, i ));
901 #else
902                                         Debug( LDAP_DEBUG_ACL,
903                                                 "acl: access to attribute %s, value %d not allowed\n",
904                                         desc->ad_cname.bv_val, i, 0 );
905 #endif
906
907                                         continue;
908                                 }
909
910
911                                 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
912 #ifdef NEW_LOGGING
913                                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
914                                                    "send_search_entry: conn %d  ber_printf failed\n",
915                                                    op->o_connid ));
916 #else
917                                         Debug( LDAP_DEBUG_ANY,
918                                             "ber_printf failed\n", 0, 0, 0 );
919 #endif
920
921                                         ber_free( ber, 1 );
922                                         send_ldap_result( conn, op, LDAP_OTHER,
923                                                 NULL, "encoding values error", NULL, NULL );
924                                         attrs_free( aa );
925                                         goto error_return;
926                                 }
927                         }
928                 }
929
930                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
931 #ifdef NEW_LOGGING
932                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
933                                    "send_search_entry: conn %d  ber_printf failed\n",
934                                    op->o_connid ));
935 #else
936                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
937 #endif
938
939                         ber_free( ber, 1 );
940                         send_ldap_result( conn, op, LDAP_OTHER,
941                             NULL, "encode end error", NULL, NULL );
942                         attrs_free( aa );
943                         goto error_return;
944                 }
945         }
946
947         attrs_free( aa );
948
949         rc = ber_printf( ber, /*{{{*/ "}N}N}" );
950
951 #ifdef LDAP_CONNECTIONLESS
952         if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1)
953                 rc = ber_printf( ber, "}" );
954 #endif
955         if ( rc == -1 ) {
956 #ifdef NEW_LOGGING
957                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
958                            "send_search_entry: conn %d ber_printf failed\n",
959                            op->o_connid ));
960 #else
961                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
962 #endif
963
964                 ber_free( ber, 1 );
965                 send_ldap_result( conn, op, LDAP_OTHER,
966                         NULL, "encode entry end error", NULL, NULL );
967                 return( 1 );
968         }
969
970         bytes = send_ldap_ber( conn, ber );
971         ber_free( ber, 1 );
972
973         if ( bytes < 0 ) {
974 #ifdef NEW_LOGGING
975                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
976                            "send_ldap_response: conn %d  ber write failed.\n",
977                            op->o_connid ));
978 #else
979                 Debug( LDAP_DEBUG_ANY,
980                         "send_ldap_response: ber write failed\n",
981                         0, 0, 0 );
982 #endif
983
984                 return -1;
985         }
986
987         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
988         num_bytes_sent += bytes;
989         num_entries_sent++;
990         num_pdu_sent++;
991         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
992
993         Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
994             (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
995
996 #ifdef NEW_LOGGING
997         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
998                    "send_search_entry: conn %d exit.\n",
999                    op->o_connid ));
1000 #else
1001         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1002 #endif
1003
1004
1005         rc = 0;
1006
1007 error_return:;
1008         return( rc );
1009 }
1010
1011 int
1012 send_search_reference(
1013     Backend     *be,
1014     Connection  *conn,
1015     Operation   *op,
1016     Entry       *e,
1017         struct berval **refs,
1018         LDAPControl **ctrls,
1019     struct berval ***v2refs
1020 )
1021 {
1022         BerElement      *ber;
1023         int rc;
1024         int bytes;
1025
1026         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1027         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1028
1029 #ifdef NEW_LOGGING
1030         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1031                 "send_search_reference: conn %d  dn=\"%s\"\n",
1032                 op->o_connid, e->e_dn ));
1033 #else
1034         Debug( LDAP_DEBUG_TRACE,
1035                 "=> send_search_reference: dn=\"%s\"\n",
1036                 e->e_dn, 0, 0 );
1037 #endif
1038
1039
1040         if ( ! access_allowed( be, conn, op, e,
1041                 ad_entry, NULL, ACL_READ ) )
1042         {
1043 #ifdef NEW_LOGGING
1044                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
1045                         "send_search_reference: conn %d access to entry %s not allowed\n",
1046                         op->o_connid, e->e_dn ));
1047 #else
1048                 Debug( LDAP_DEBUG_ACL,
1049                         "send_search_reference: access to entry not allowed\n",
1050                     0, 0, 0 );
1051 #endif
1052
1053                 return( 1 );
1054         }
1055
1056         if ( ! access_allowed( be, conn, op, e,
1057                 ad_ref, NULL, ACL_READ ) )
1058         {
1059 #ifdef NEW_LOGGING
1060                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
1061                         "send_search_reference: conn %d access to reference not allowed.\n",
1062                         op->o_connid ));
1063 #else
1064                 Debug( LDAP_DEBUG_ACL,
1065                         "send_search_reference: access to reference not allowed\n",
1066                     0, 0, 0 );
1067 #endif
1068
1069                 return( 1 );
1070         }
1071
1072         if( refs == NULL ) {
1073 #ifdef NEW_LOGGING
1074                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1075                         "send_search_reference: null ref in (%s).\n",
1076                         op->o_connid, e->e_dn ));
1077 #else
1078                 Debug( LDAP_DEBUG_ANY,
1079                         "send_search_reference: null ref in (%s)\n", 
1080                         e->e_dn, 0, 0 );
1081 #endif
1082
1083                 return( 1 );
1084         }
1085
1086         if( op->o_protocol < LDAP_VERSION3 ) {
1087                 /* save the references for the result */
1088                 if( *refs != NULL ) {
1089                         value_add( v2refs, refs );
1090                 }
1091                 return 0;
1092         }
1093
1094         ber = ber_alloc_t( LBER_USE_DER );
1095
1096         if ( ber == NULL ) {
1097 #ifdef NEW_LOGGING
1098                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1099                         "send_search_reference: conn %d ber_alloc failed\n",
1100                         op->o_connid ));
1101 #else
1102                 Debug( LDAP_DEBUG_ANY,
1103                         "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
1104 #endif
1105
1106                 send_ldap_result( conn, op, LDAP_OTHER,
1107                         NULL, "alloc BER error", NULL, NULL );
1108                 return -1;
1109         }
1110
1111         rc = ber_printf( ber, "{it{V}N}", op->o_msgid,
1112                 LDAP_RES_SEARCH_REFERENCE, refs );
1113
1114         if ( rc == -1 ) {
1115 #ifdef NEW_LOGGING
1116                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1117                         "send_search_reference: conn %d ber_printf failed.\n",
1118                         op->o_connid ));
1119 #else
1120                 Debug( LDAP_DEBUG_ANY,
1121                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1122 #endif
1123
1124                 ber_free( ber, 1 );
1125                 send_ldap_result( conn, op, LDAP_OTHER,
1126                         NULL, "encode DN error", NULL, NULL );
1127                 return -1;
1128         }
1129
1130         bytes = send_ldap_ber( conn, ber );
1131         ber_free( ber, 1 );
1132
1133         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1134         num_bytes_sent += bytes;
1135         num_refs_sent++;
1136         num_pdu_sent++;
1137         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1138
1139         Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld REF dn=\"%s\"\n",
1140                 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
1141
1142 #ifdef NEW_LOGGING
1143         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1144                 "send_search_reference: conn %d exit.\n", op->o_connid ));
1145 #else
1146         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1147 #endif
1148
1149         return 0;
1150 }
1151
1152
1153 int
1154 str2result(
1155     char        *s,
1156     int         *code,
1157     char        **matched,
1158     char        **info
1159 )
1160 {
1161         int     rc;
1162         char    *c;
1163
1164         *code = LDAP_SUCCESS;
1165         *matched = NULL;
1166         *info = NULL;
1167
1168         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1169 #ifdef NEW_LOGGING
1170                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
1171                            "str2result: (%s), expecting \"RESULT\"\n", s ));
1172 #else
1173                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1174                     s, 0, 0 );
1175 #endif
1176
1177
1178                 return( -1 );
1179         }
1180
1181         rc = 0;
1182         while ( (s = strchr( s, '\n' )) != NULL ) {
1183                 *s++ = '\0';
1184                 if ( *s == '\0' ) {
1185                         break;
1186                 }
1187                 if ( (c = strchr( s, ':' )) != NULL ) {
1188                         c++;
1189                 }
1190
1191                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1192                         if ( c != NULL ) {
1193                                 *code = atoi( c );
1194                         }
1195                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1196                         if ( c != NULL ) {
1197                                 *matched = c;
1198                         }
1199                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1200                         if ( c != NULL ) {
1201                                 *info = c;
1202                         }
1203                 } else {
1204 #ifdef NEW_LOGGING
1205                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
1206                                    "str2result: (%s) unknown.\n", s ));
1207 #else
1208                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1209                             s, 0, 0 );
1210 #endif
1211
1212                         rc = -1;
1213                 }
1214         }
1215
1216         return( rc );
1217 }