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