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