]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
More struct berval fixes for modrdn
[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 static struct berval AllUser = { sizeof(LDAP_ALL_USER_ATTRIBUTES)-1,
622         LDAP_ALL_USER_ATTRIBUTES };
623 static struct berval AllOper = { sizeof(LDAP_ALL_OPERATIONAL_ATTRIBUTES)-1,
624         LDAP_ALL_OPERATIONAL_ATTRIBUTES };
625
626 int
627 send_search_entry(
628     Backend     *be,
629     Connection  *conn,
630     Operation   *op,
631     Entry       *e,
632     struct berval       **attrs,
633     int         attrsonly,
634         LDAPControl **ctrls
635 )
636 {
637         BerElement      *ber;
638         Attribute       *a, *aa;
639         int             i, rc=-1, bytes;
640         char            *edn;
641         int             userattrs;
642         int             opattrs;
643
644         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
645
646 #ifdef NEW_LOGGING
647         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
648                    "send_search_entry: conn %d  dn=\"%s\"%s\n",
649                    op->o_connid, e->e_dn,
650                    attrsonly ? " (attrsOnly)" : "" ));
651 #else
652         Debug( LDAP_DEBUG_TRACE,
653                 "=> send_search_entry: dn=\"%s\"%s\n",
654                 e->e_dn, attrsonly ? " (attrsOnly)" : "", 0 );
655 #endif
656
657
658         if ( ! access_allowed( be, conn, op, e,
659                 ad_entry, NULL, ACL_READ ) )
660         {
661 #ifdef NEW_LOGGING
662                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
663                            "send_search_entry: conn %d access to entry (%s) not allowed\n",
664                            op->o_connid, e->e_dn ));
665 #else
666                 Debug( LDAP_DEBUG_ACL,
667                         "send_search_entry: access to entry not allowed\n",
668                     0, 0, 0 );
669 #endif
670
671                 return( 1 );
672         }
673
674         edn = e->e_ndn;
675
676         ber = ber_alloc_t( LBER_USE_DER );
677
678         if ( ber == NULL ) {
679 #ifdef NEW_LOGGING
680                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
681                            "send_search_entry: conn %d  ber_alloc failed\n",
682                            op->o_connid ));
683 #else
684                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
685 #endif
686
687                 send_ldap_result( conn, op, LDAP_OTHER,
688                         NULL, "BER allocation error", NULL, NULL );
689                 goto error_return;
690         }
691
692 #ifdef LDAP_CONNECTIONLESS
693         if (conn->c_is_udp) {
694             rc = ber_write(ber, (char *)&op->o_peeraddr, sizeof(struct sockaddr), 0);
695             if (rc != sizeof(struct sockaddr)) {
696 #ifdef NEW_LOGGING
697                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
698                            "send_search_entry: conn %d  ber_printf failed\n",
699                            conn ? conn->c_connid : 0 ));
700 #else
701                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
702 #endif
703                 ber_free(ber, 1);
704                 return;
705             }
706         }
707         if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
708             rc = ber_printf( ber, "{is{t{O{" /*}}}*/,
709                 op->o_msgid, "", LDAP_RES_SEARCH_ENTRY, &e->e_name );
710         } else
711 #endif
712         {
713             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
714                 LDAP_RES_SEARCH_ENTRY, &e->e_name );
715         }
716
717         if ( rc == -1 ) {
718 #ifdef NEW_LOGGING
719                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
720                            "send_search_entry: conn %d  ber_printf failed\n",
721                            op->o_connid ));
722 #else
723                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
724 #endif
725
726                 ber_free( ber, 1 );
727                 send_ldap_result( conn, op, LDAP_OTHER,
728                     NULL, "encoding DN error", NULL, NULL );
729                 goto error_return;
730         }
731
732         /* check for special all user attributes ("*") type */
733         userattrs = ( attrs == NULL ) ? 1
734                 : bvec_inlist( attrs, &AllUser );
735
736         /* check for special all operational attributes ("+") type */
737         opattrs = ( attrs == NULL ) ? 0
738                 : bvec_inlist( attrs, &AllOper );
739
740         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
741                 AttributeDescription *desc = a->a_desc;
742
743                 if ( attrs == NULL ) {
744                         /* all attrs request, skip operational attributes */
745                         if( is_at_operational( desc->ad_type ) ) {
746                                 continue;
747                         }
748
749                 } else {
750                         /* specific attrs requested */
751                         if ( is_at_operational( desc->ad_type ) ) {
752                                 if( !opattrs && !ad_inlist( desc, attrs ) ) {
753                                         continue;
754                                 }
755
756                         } else {
757                                 if (!userattrs && !ad_inlist( desc, attrs ) ) {
758                                         continue;
759                                 }
760                         }
761                 }
762
763                 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
764 #ifdef NEW_LOGGING
765                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO, "send_search_entry: "
766                                 "conn %d  access to attribute %s not allowed\n",
767                                 op->o_connid, desc->ad_cname.bv_val ));
768 #else
769                         Debug( LDAP_DEBUG_ACL, "acl: "
770                                 "access to attribute %s not allowed\n",
771                             desc->ad_cname.bv_val, 0, 0 );
772 #endif
773
774                         continue;
775                 }
776
777                 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
778 #ifdef NEW_LOGGING
779                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
780                                 "send_search_entry: conn %d  ber_printf failed\n",
781                                 op->o_connid ));
782 #else
783                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
784 #endif
785
786                         ber_free( ber, 1 );
787                         send_ldap_result( conn, op, LDAP_OTHER,
788                             NULL, "encoding description error", NULL, NULL );
789                         goto error_return;
790                 }
791
792                 if ( ! attrsonly ) {
793                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
794                                 if ( ! access_allowed( be, conn, op, e,
795                                         desc, a->a_vals[i], ACL_READ ) )
796                                 {
797 #ifdef NEW_LOGGING
798                                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
799                                                    "send_search_entry: conn %d  access to attribute %s, value %d not allowed\n",
800                                                    op->o_connid, desc->ad_cname.bv_val, i ));
801 #else
802                                         Debug( LDAP_DEBUG_ACL,
803                                                 "acl: access to attribute %s, value %d not allowed\n",
804                                         desc->ad_cname.bv_val, i, 0 );
805 #endif
806
807                                         continue;
808                                 }
809
810                                 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
811 #ifdef NEW_LOGGING
812                                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
813                                                    "send_search_entry: conn %d  ber_printf failed.\n",
814                                                    op->o_connid ));
815 #else
816                                         Debug( LDAP_DEBUG_ANY,
817                                             "ber_printf failed\n", 0, 0, 0 );
818 #endif
819
820                                         ber_free( ber, 1 );
821                                         send_ldap_result( conn, op, LDAP_OTHER,
822                                                 NULL, "encoding values error", NULL, NULL );
823                                         goto error_return;
824                                 }
825                         }
826                 }
827
828                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
829 #ifdef NEW_LOGGING
830                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
831                                    "send_search_entry: conn %d  ber_printf failed\n",
832                                    op->o_connid ));
833 #else
834                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
835 #endif
836
837                         ber_free( ber, 1 );
838                         send_ldap_result( conn, op, LDAP_OTHER,
839                             NULL, "encode end error", NULL, NULL );
840                         goto error_return;
841                 }
842         }
843
844         /* eventually will loop through generated operational attributes */
845         /* only have subschemaSubentry implemented */
846         aa = backend_operational( be, conn, op, e, attrs, opattrs );
847         
848         for (a = aa ; a != NULL; a = a->a_next ) {
849                 AttributeDescription *desc = a->a_desc;
850
851                 if ( attrs == NULL ) {
852                         /* all attrs request, skip operational attributes */
853                         if( is_at_operational( desc->ad_type ) ) {
854                                 continue;
855                         }
856
857                 } else {
858                         /* specific attrs requested */
859                         if( is_at_operational( desc->ad_type ) ) {
860                                 if( !opattrs && !ad_inlist( desc, attrs ) ) {
861                                         continue;
862                                 }
863                         } else {
864                                 if (!userattrs && !ad_inlist( desc, attrs ) )
865                                 {
866                                         continue;
867                                 }
868                         }
869                 }
870
871                 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
872 #ifdef NEW_LOGGING
873                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
874                                    "send_search_entry: conn %s  access to attribute %s not allowed\n",
875                                    op->o_connid, desc->ad_cname.bv_val ));
876 #else
877                         Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
878                             desc->ad_cname.bv_val, 0, 0 );
879 #endif
880
881                         continue;
882                 }
883
884                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
885                 if ( rc == -1 ) {
886 #ifdef NEW_LOGGING
887                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
888                                    "send_search_entry: conn %d  ber_printf failed\n",
889                                    op->o_connid ));
890 #else
891                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
892 #endif
893
894                         ber_free( ber, 1 );
895                         send_ldap_result( conn, op, LDAP_OTHER,
896                             NULL, "encoding description error", NULL, NULL );
897                         attrs_free( aa );
898                         goto error_return;
899                 }
900
901                 if ( ! attrsonly ) {
902                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
903                                 if ( ! access_allowed( be, conn, op, e,
904                                         desc, a->a_vals[i], ACL_READ ) )
905                                 {
906 #ifdef NEW_LOGGING
907                                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
908                                                    "send_search_entry: conn %d access to %s, value %d not allowed\n",
909                                                    op->o_connid, desc->ad_cname.bv_val, i ));
910 #else
911                                         Debug( LDAP_DEBUG_ACL,
912                                                 "acl: access to attribute %s, value %d not allowed\n",
913                                         desc->ad_cname.bv_val, i, 0 );
914 #endif
915
916                                         continue;
917                                 }
918
919
920                                 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
921 #ifdef NEW_LOGGING
922                                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
923                                                    "send_search_entry: conn %d  ber_printf failed\n",
924                                                    op->o_connid ));
925 #else
926                                         Debug( LDAP_DEBUG_ANY,
927                                             "ber_printf failed\n", 0, 0, 0 );
928 #endif
929
930                                         ber_free( ber, 1 );
931                                         send_ldap_result( conn, op, LDAP_OTHER,
932                                                 NULL, "encoding values error", NULL, NULL );
933                                         attrs_free( aa );
934                                         goto error_return;
935                                 }
936                         }
937                 }
938
939                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
940 #ifdef NEW_LOGGING
941                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
942                                    "send_search_entry: conn %d  ber_printf failed\n",
943                                    op->o_connid ));
944 #else
945                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
946 #endif
947
948                         ber_free( ber, 1 );
949                         send_ldap_result( conn, op, LDAP_OTHER,
950                             NULL, "encode end error", NULL, NULL );
951                         attrs_free( aa );
952                         goto error_return;
953                 }
954         }
955
956         attrs_free( aa );
957
958         rc = ber_printf( ber, /*{{{*/ "}N}N}" );
959
960 #ifdef LDAP_CONNECTIONLESS
961         if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1)
962                 rc = ber_printf( ber, "}" );
963 #endif
964         if ( rc == -1 ) {
965 #ifdef NEW_LOGGING
966                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
967                            "send_search_entry: conn %d ber_printf failed\n",
968                            op->o_connid ));
969 #else
970                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
971 #endif
972
973                 ber_free( ber, 1 );
974                 send_ldap_result( conn, op, LDAP_OTHER,
975                         NULL, "encode entry end error", NULL, NULL );
976                 return( 1 );
977         }
978
979         bytes = send_ldap_ber( conn, ber );
980         ber_free( ber, 1 );
981
982         if ( bytes < 0 ) {
983 #ifdef NEW_LOGGING
984                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
985                            "send_ldap_response: conn %d  ber write failed.\n",
986                            op->o_connid ));
987 #else
988                 Debug( LDAP_DEBUG_ANY,
989                         "send_ldap_response: ber write failed\n",
990                         0, 0, 0 );
991 #endif
992
993                 return -1;
994         }
995
996         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
997         num_bytes_sent += bytes;
998         num_entries_sent++;
999         num_pdu_sent++;
1000         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1001
1002         Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
1003             (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
1004
1005 #ifdef NEW_LOGGING
1006         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1007                    "send_search_entry: conn %d exit.\n",
1008                    op->o_connid ));
1009 #else
1010         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1011 #endif
1012
1013
1014         rc = 0;
1015
1016 error_return:;
1017         return( rc );
1018 }
1019
1020 int
1021 send_search_reference(
1022     Backend     *be,
1023     Connection  *conn,
1024     Operation   *op,
1025     Entry       *e,
1026         struct berval **refs,
1027         LDAPControl **ctrls,
1028     struct berval ***v2refs
1029 )
1030 {
1031         BerElement      *ber;
1032         int rc;
1033         int bytes;
1034
1035         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1036         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1037
1038 #ifdef NEW_LOGGING
1039         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1040                 "send_search_reference: conn %d  dn=\"%s\"\n",
1041                 op->o_connid, e->e_dn ));
1042 #else
1043         Debug( LDAP_DEBUG_TRACE,
1044                 "=> send_search_reference: dn=\"%s\"\n",
1045                 e->e_dn, 0, 0 );
1046 #endif
1047
1048
1049         if ( ! access_allowed( be, conn, op, e,
1050                 ad_entry, NULL, ACL_READ ) )
1051         {
1052 #ifdef NEW_LOGGING
1053                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
1054                         "send_search_reference: conn %d access to entry %s not allowed\n",
1055                         op->o_connid, e->e_dn ));
1056 #else
1057                 Debug( LDAP_DEBUG_ACL,
1058                         "send_search_reference: access to entry not allowed\n",
1059                     0, 0, 0 );
1060 #endif
1061
1062                 return( 1 );
1063         }
1064
1065         if ( ! access_allowed( be, conn, op, e,
1066                 ad_ref, NULL, ACL_READ ) )
1067         {
1068 #ifdef NEW_LOGGING
1069                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
1070                         "send_search_reference: conn %d access to reference not allowed.\n",
1071                         op->o_connid ));
1072 #else
1073                 Debug( LDAP_DEBUG_ACL,
1074                         "send_search_reference: access to reference not allowed\n",
1075                     0, 0, 0 );
1076 #endif
1077
1078                 return( 1 );
1079         }
1080
1081         if( refs == NULL ) {
1082 #ifdef NEW_LOGGING
1083                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1084                         "send_search_reference: null ref in (%s).\n",
1085                         op->o_connid, e->e_dn ));
1086 #else
1087                 Debug( LDAP_DEBUG_ANY,
1088                         "send_search_reference: null ref in (%s)\n", 
1089                         e->e_dn, 0, 0 );
1090 #endif
1091
1092                 return( 1 );
1093         }
1094
1095         if( op->o_protocol < LDAP_VERSION3 ) {
1096                 /* save the references for the result */
1097                 if( *refs != NULL ) {
1098                         value_add( v2refs, refs );
1099                 }
1100                 return 0;
1101         }
1102
1103         ber = ber_alloc_t( LBER_USE_DER );
1104
1105         if ( ber == NULL ) {
1106 #ifdef NEW_LOGGING
1107                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1108                         "send_search_reference: conn %d ber_alloc failed\n",
1109                         op->o_connid ));
1110 #else
1111                 Debug( LDAP_DEBUG_ANY,
1112                         "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
1113 #endif
1114
1115                 send_ldap_result( conn, op, LDAP_OTHER,
1116                         NULL, "alloc BER error", NULL, NULL );
1117                 return -1;
1118         }
1119
1120         rc = ber_printf( ber, "{it{V}N}", op->o_msgid,
1121                 LDAP_RES_SEARCH_REFERENCE, refs );
1122
1123         if ( rc == -1 ) {
1124 #ifdef NEW_LOGGING
1125                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1126                         "send_search_reference: conn %d ber_printf failed.\n",
1127                         op->o_connid ));
1128 #else
1129                 Debug( LDAP_DEBUG_ANY,
1130                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1131 #endif
1132
1133                 ber_free( ber, 1 );
1134                 send_ldap_result( conn, op, LDAP_OTHER,
1135                         NULL, "encode DN error", NULL, NULL );
1136                 return -1;
1137         }
1138
1139         bytes = send_ldap_ber( conn, ber );
1140         ber_free( ber, 1 );
1141
1142         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1143         num_bytes_sent += bytes;
1144         num_refs_sent++;
1145         num_pdu_sent++;
1146         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1147
1148         Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld REF dn=\"%s\"\n",
1149                 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
1150
1151 #ifdef NEW_LOGGING
1152         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1153                 "send_search_reference: conn %d exit.\n", op->o_connid ));
1154 #else
1155         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1156 #endif
1157
1158         return 0;
1159 }
1160
1161
1162 int
1163 str2result(
1164     char        *s,
1165     int         *code,
1166     char        **matched,
1167     char        **info
1168 )
1169 {
1170         int     rc;
1171         char    *c;
1172
1173         *code = LDAP_SUCCESS;
1174         *matched = NULL;
1175         *info = NULL;
1176
1177         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1178 #ifdef NEW_LOGGING
1179                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
1180                            "str2result: (%s), expecting \"RESULT\"\n", s ));
1181 #else
1182                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1183                     s, 0, 0 );
1184 #endif
1185
1186
1187                 return( -1 );
1188         }
1189
1190         rc = 0;
1191         while ( (s = strchr( s, '\n' )) != NULL ) {
1192                 *s++ = '\0';
1193                 if ( *s == '\0' ) {
1194                         break;
1195                 }
1196                 if ( (c = strchr( s, ':' )) != NULL ) {
1197                         c++;
1198                 }
1199
1200                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1201                         if ( c != NULL ) {
1202                                 *code = atoi( c );
1203                         }
1204                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1205                         if ( c != NULL ) {
1206                                 *matched = c;
1207                         }
1208                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1209                         if ( c != NULL ) {
1210                                 *info = c;
1211                         }
1212                 } else {
1213 #ifdef NEW_LOGGING
1214                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
1215                                 "str2result: (%s) unknown.\n", s ));
1216 #else
1217                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1218                             s, 0, 0 );
1219 #endif
1220
1221                         rc = -1;
1222                 }
1223         }
1224
1225         return( rc );
1226 }