]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
e6f16ae79bc46ad8905b93f730a08442a31f9df8
[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,
766                                    "send_search_entry: 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: access to attribute %s not allowed\n",
770                             desc->ad_cname.bv_val, 0, 0 );
771 #endif
772
773                         continue;
774                 }
775
776                 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
777 #ifdef NEW_LOGGING
778                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
779                                    "send_search_entry: conn %d  ber_printf failed\n",
780                                    op->o_connid ));
781 #else
782                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
783 #endif
784
785                         ber_free( ber, 1 );
786                         send_ldap_result( conn, op, LDAP_OTHER,
787                             NULL, "encoding description error", NULL, NULL );
788                         goto error_return;
789                 }
790
791                 if ( ! attrsonly ) {
792                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
793                                 if ( ! access_allowed( be, conn, op, e,
794                                         desc, a->a_vals[i], ACL_READ ) )
795                                 {
796 #ifdef NEW_LOGGING
797                                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
798                                                    "send_search_entry: conn %d  access to attribute %s, value %d not allowed\n",
799                                                    op->o_connid, desc->ad_cname.bv_val, i ));
800 #else
801                                         Debug( LDAP_DEBUG_ACL,
802                                                 "acl: access to attribute %s, value %d not allowed\n",
803                                         desc->ad_cname.bv_val, i, 0 );
804 #endif
805
806                                         continue;
807                                 }
808
809                                 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
810 #ifdef NEW_LOGGING
811                                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
812                                                    "send_search_entry: conn %d  ber_printf failed.\n",
813                                                    op->o_connid ));
814 #else
815                                         Debug( LDAP_DEBUG_ANY,
816                                             "ber_printf failed\n", 0, 0, 0 );
817 #endif
818
819                                         ber_free( ber, 1 );
820                                         send_ldap_result( conn, op, LDAP_OTHER,
821                                                 NULL, "encoding values error", NULL, NULL );
822                                         goto error_return;
823                                 }
824                         }
825                 }
826
827                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
828 #ifdef NEW_LOGGING
829                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
830                                    "send_search_entry: conn %d  ber_printf failed\n",
831                                    op->o_connid ));
832 #else
833                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
834 #endif
835
836                         ber_free( ber, 1 );
837                         send_ldap_result( conn, op, LDAP_OTHER,
838                             NULL, "encode end error", NULL, NULL );
839                         goto error_return;
840                 }
841         }
842
843         /* eventually will loop through generated operational attributes */
844         /* only have subschemaSubentry implemented */
845         aa = backend_operational( be, conn, op, e, attrs, opattrs );
846         
847         for (a = aa ; a != NULL; a = a->a_next ) {
848                 AttributeDescription *desc = a->a_desc;
849
850                 if ( attrs == NULL ) {
851                         /* all attrs request, skip operational attributes */
852                         if( is_at_operational( desc->ad_type ) ) {
853                                 continue;
854                         }
855
856                 } else {
857                         /* specific attrs requested */
858                         if( is_at_operational( desc->ad_type ) ) {
859                                 if( !opattrs && !ad_inlist( desc, attrs ) ) {
860                                         continue;
861                                 }
862                         } else {
863                                 if (!userattrs && !ad_inlist( desc, attrs ) )
864                                 {
865                                         continue;
866                                 }
867                         }
868                 }
869
870                 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
871 #ifdef NEW_LOGGING
872                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
873                                    "send_search_entry: conn %s  access to attribute %s not allowed\n",
874                                    op->o_connid, desc->ad_cname.bv_val ));
875 #else
876                         Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
877                             desc->ad_cname.bv_val, 0, 0 );
878 #endif
879
880                         continue;
881                 }
882
883                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
884                 if ( rc == -1 ) {
885 #ifdef NEW_LOGGING
886                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
887                                    "send_search_entry: conn %d  ber_printf failed\n",
888                                    op->o_connid ));
889 #else
890                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
891 #endif
892
893                         ber_free( ber, 1 );
894                         send_ldap_result( conn, op, LDAP_OTHER,
895                             NULL, "encoding description error", NULL, NULL );
896                         attrs_free( aa );
897                         goto error_return;
898                 }
899
900                 if ( ! attrsonly ) {
901                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
902                                 if ( ! access_allowed( be, conn, op, e,
903                                         desc, a->a_vals[i], ACL_READ ) )
904                                 {
905 #ifdef NEW_LOGGING
906                                         LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
907                                                    "send_search_entry: conn %d access to %s, value %d not allowed\n",
908                                                    op->o_connid, desc->ad_cname.bv_val, i ));
909 #else
910                                         Debug( LDAP_DEBUG_ACL,
911                                                 "acl: access to attribute %s, value %d not allowed\n",
912                                         desc->ad_cname.bv_val, i, 0 );
913 #endif
914
915                                         continue;
916                                 }
917
918
919                                 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
920 #ifdef NEW_LOGGING
921                                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
922                                                    "send_search_entry: conn %d  ber_printf failed\n",
923                                                    op->o_connid ));
924 #else
925                                         Debug( LDAP_DEBUG_ANY,
926                                             "ber_printf failed\n", 0, 0, 0 );
927 #endif
928
929                                         ber_free( ber, 1 );
930                                         send_ldap_result( conn, op, LDAP_OTHER,
931                                                 NULL, "encoding values error", NULL, NULL );
932                                         attrs_free( aa );
933                                         goto error_return;
934                                 }
935                         }
936                 }
937
938                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
939 #ifdef NEW_LOGGING
940                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
941                                    "send_search_entry: conn %d  ber_printf failed\n",
942                                    op->o_connid ));
943 #else
944                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
945 #endif
946
947                         ber_free( ber, 1 );
948                         send_ldap_result( conn, op, LDAP_OTHER,
949                             NULL, "encode end error", NULL, NULL );
950                         attrs_free( aa );
951                         goto error_return;
952                 }
953         }
954
955         attrs_free( aa );
956
957         rc = ber_printf( ber, /*{{{*/ "}N}N}" );
958
959 #ifdef LDAP_CONNECTIONLESS
960         if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1)
961                 rc = ber_printf( ber, "}" );
962 #endif
963         if ( rc == -1 ) {
964 #ifdef NEW_LOGGING
965                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
966                            "send_search_entry: conn %d ber_printf failed\n",
967                            op->o_connid ));
968 #else
969                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
970 #endif
971
972                 ber_free( ber, 1 );
973                 send_ldap_result( conn, op, LDAP_OTHER,
974                         NULL, "encode entry end error", NULL, NULL );
975                 return( 1 );
976         }
977
978         bytes = send_ldap_ber( conn, ber );
979         ber_free( ber, 1 );
980
981         if ( bytes < 0 ) {
982 #ifdef NEW_LOGGING
983                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
984                            "send_ldap_response: conn %d  ber write failed.\n",
985                            op->o_connid ));
986 #else
987                 Debug( LDAP_DEBUG_ANY,
988                         "send_ldap_response: ber write failed\n",
989                         0, 0, 0 );
990 #endif
991
992                 return -1;
993         }
994
995         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
996         num_bytes_sent += bytes;
997         num_entries_sent++;
998         num_pdu_sent++;
999         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1000
1001         Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
1002             (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
1003
1004 #ifdef NEW_LOGGING
1005         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1006                    "send_search_entry: conn %d exit.\n",
1007                    op->o_connid ));
1008 #else
1009         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1010 #endif
1011
1012
1013         rc = 0;
1014
1015 error_return:;
1016         return( rc );
1017 }
1018
1019 int
1020 send_search_reference(
1021     Backend     *be,
1022     Connection  *conn,
1023     Operation   *op,
1024     Entry       *e,
1025         struct berval **refs,
1026         LDAPControl **ctrls,
1027     struct berval ***v2refs
1028 )
1029 {
1030         BerElement      *ber;
1031         int rc;
1032         int bytes;
1033
1034         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1035         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1036
1037 #ifdef NEW_LOGGING
1038         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1039                 "send_search_reference: conn %d  dn=\"%s\"\n",
1040                 op->o_connid, e->e_dn ));
1041 #else
1042         Debug( LDAP_DEBUG_TRACE,
1043                 "=> send_search_reference: dn=\"%s\"\n",
1044                 e->e_dn, 0, 0 );
1045 #endif
1046
1047
1048         if ( ! access_allowed( be, conn, op, e,
1049                 ad_entry, NULL, ACL_READ ) )
1050         {
1051 #ifdef NEW_LOGGING
1052                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
1053                         "send_search_reference: conn %d access to entry %s not allowed\n",
1054                         op->o_connid, e->e_dn ));
1055 #else
1056                 Debug( LDAP_DEBUG_ACL,
1057                         "send_search_reference: access to entry not allowed\n",
1058                     0, 0, 0 );
1059 #endif
1060
1061                 return( 1 );
1062         }
1063
1064         if ( ! access_allowed( be, conn, op, e,
1065                 ad_ref, NULL, ACL_READ ) )
1066         {
1067 #ifdef NEW_LOGGING
1068                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
1069                         "send_search_reference: conn %d access to reference not allowed.\n",
1070                         op->o_connid ));
1071 #else
1072                 Debug( LDAP_DEBUG_ACL,
1073                         "send_search_reference: access to reference not allowed\n",
1074                     0, 0, 0 );
1075 #endif
1076
1077                 return( 1 );
1078         }
1079
1080         if( refs == NULL ) {
1081 #ifdef NEW_LOGGING
1082                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1083                         "send_search_reference: null ref in (%s).\n",
1084                         op->o_connid, e->e_dn ));
1085 #else
1086                 Debug( LDAP_DEBUG_ANY,
1087                         "send_search_reference: null ref in (%s)\n", 
1088                         e->e_dn, 0, 0 );
1089 #endif
1090
1091                 return( 1 );
1092         }
1093
1094         if( op->o_protocol < LDAP_VERSION3 ) {
1095                 /* save the references for the result */
1096                 if( *refs != NULL ) {
1097                         value_add( v2refs, refs );
1098                 }
1099                 return 0;
1100         }
1101
1102         ber = ber_alloc_t( LBER_USE_DER );
1103
1104         if ( ber == NULL ) {
1105 #ifdef NEW_LOGGING
1106                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1107                         "send_search_reference: conn %d ber_alloc failed\n",
1108                         op->o_connid ));
1109 #else
1110                 Debug( LDAP_DEBUG_ANY,
1111                         "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
1112 #endif
1113
1114                 send_ldap_result( conn, op, LDAP_OTHER,
1115                         NULL, "alloc BER error", NULL, NULL );
1116                 return -1;
1117         }
1118
1119         rc = ber_printf( ber, "{it{V}N}", op->o_msgid,
1120                 LDAP_RES_SEARCH_REFERENCE, refs );
1121
1122         if ( rc == -1 ) {
1123 #ifdef NEW_LOGGING
1124                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
1125                         "send_search_reference: conn %d ber_printf failed.\n",
1126                         op->o_connid ));
1127 #else
1128                 Debug( LDAP_DEBUG_ANY,
1129                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1130 #endif
1131
1132                 ber_free( ber, 1 );
1133                 send_ldap_result( conn, op, LDAP_OTHER,
1134                         NULL, "encode DN error", NULL, NULL );
1135                 return -1;
1136         }
1137
1138         bytes = send_ldap_ber( conn, ber );
1139         ber_free( ber, 1 );
1140
1141         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1142         num_bytes_sent += bytes;
1143         num_refs_sent++;
1144         num_pdu_sent++;
1145         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1146
1147         Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld REF dn=\"%s\"\n",
1148                 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
1149
1150 #ifdef NEW_LOGGING
1151         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
1152                 "send_search_reference: conn %d exit.\n", op->o_connid ));
1153 #else
1154         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1155 #endif
1156
1157         return 0;
1158 }
1159
1160
1161 int
1162 str2result(
1163     char        *s,
1164     int         *code,
1165     char        **matched,
1166     char        **info
1167 )
1168 {
1169         int     rc;
1170         char    *c;
1171
1172         *code = LDAP_SUCCESS;
1173         *matched = NULL;
1174         *info = NULL;
1175
1176         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1177 #ifdef NEW_LOGGING
1178                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
1179                            "str2result: (%s), expecting \"RESULT\"\n", s ));
1180 #else
1181                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1182                     s, 0, 0 );
1183 #endif
1184
1185
1186                 return( -1 );
1187         }
1188
1189         rc = 0;
1190         while ( (s = strchr( s, '\n' )) != NULL ) {
1191                 *s++ = '\0';
1192                 if ( *s == '\0' ) {
1193                         break;
1194                 }
1195                 if ( (c = strchr( s, ':' )) != NULL ) {
1196                         c++;
1197                 }
1198
1199                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1200                         if ( c != NULL ) {
1201                                 *code = atoi( c );
1202                         }
1203                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1204                         if ( c != NULL ) {
1205                                 *matched = c;
1206                         }
1207                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1208                         if ( c != NULL ) {
1209                                 *info = c;
1210                         }
1211                 } else {
1212 #ifdef NEW_LOGGING
1213                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
1214                                    "str2result: (%s) unknown.\n", s ));
1215 #else
1216                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1217                             s, 0, 0 );
1218 #endif
1219
1220                         rc = -1;
1221                 }
1222         }
1223
1224         return( rc );
1225 }