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