]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
use BER_BVNULL
[openldap] / servers / slapd / back-ldap / bind.c
1 /* bind.c - ldap backend bind function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2000-2003 Pierangelo Masarati.
7  * Portions Copyright 1999-2003 Howard Chu.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30
31
32 #define AVL_INTERNAL
33 #include "slap.h"
34 #include "back-ldap.h"
35
36 #define PRINT_CONNTREE 0
37
38 static LDAP_REBIND_PROC ldap_back_rebind;
39
40 int
41 ldap_back_bind(
42     Operation           *op,
43     SlapReply           *rs )
44 {
45         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
46         struct ldapconn *lc;
47
48         struct berval mdn = BER_BVNULL;
49         int rc = 0;
50         ber_int_t msgid;
51         dncookie dc;
52
53         lc = ldap_back_getconn(op, rs);
54         if ( !lc ) {
55                 return( -1 );
56         }
57
58         /*
59          * Rewrite the bind dn if needed
60          */
61         dc.rwmap = &li->rwmap;
62 #ifdef ENABLE_REWRITE
63         dc.conn = op->o_conn;
64         dc.rs = rs;
65         dc.ctx = "bindDN";
66 #else
67         dc.tofrom = 1;
68         dc.normalized = 0;
69 #endif
70         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
71                 send_ldap_result( op, rs );
72                 return -1;
73         }
74
75         if ( lc->bound_dn.bv_val ) {
76                 ch_free( lc->bound_dn.bv_val );
77                 lc->bound_dn.bv_len = 0;
78                 lc->bound_dn.bv_val = NULL;
79         }
80         lc->bound = 0;
81         /* method is always LDAP_AUTH_SIMPLE if we got here */
82         rs->sr_err = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
83                 &op->oq_bind.rb_cred, op->o_ctrls, NULL, &msgid);
84         rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
85         if (rc == LDAP_SUCCESS) {
86                 lc->bound = 1;
87                 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
88                         lc->bound_dn = mdn;
89                 } else {
90                         ber_dupbv( &lc->bound_dn, &op->o_req_dn );
91                 }
92                 mdn.bv_val = NULL;
93
94                 if ( li->savecred ) {
95                         if ( lc->cred.bv_val ) {
96                                 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
97                                 ch_free( lc->cred.bv_val );
98                         }
99                         ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
100                         ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
101                 }
102         }
103
104         /* must re-insert if local DN changed as result of bind */
105         if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
106                 int lerr;
107
108                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
109                 lc = avl_delete( &li->conntree, (caddr_t)lc,
110                                 ldap_back_conn_cmp );
111                 if ( lc->local_dn.bv_val )
112                         ch_free( lc->local_dn.bv_val );
113                 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
114                 lerr = avl_insert( &li->conntree, (caddr_t)lc,
115                         ldap_back_conn_cmp, ldap_back_conn_dup );
116                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
117                 if ( lerr == -1 ) {
118                         ldap_back_conn_free( lc );
119                 }
120         }
121
122         if ( mdn.bv_val && mdn.bv_val != op->o_req_dn.bv_val ) {
123                 free( mdn.bv_val );
124         }
125
126         return( rc );
127 }
128
129 /*
130  * ldap_back_conn_cmp
131  *
132  * compares two struct ldapconn based on the value of the conn pointer;
133  * used by avl stuff
134  */
135 int
136 ldap_back_conn_cmp(
137         const void *c1,
138         const void *c2
139         )
140 {
141         const struct ldapconn *lc1 = (const struct ldapconn *)c1;
142         const struct ldapconn *lc2 = (const struct ldapconn *)c2;
143         int rc;
144         
145         /* If local DNs don't match, it is definitely not a match */
146         if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
147                 return rc;
148
149         /* For shared sessions, conn is NULL. Only explicitly
150          * bound sessions will have non-NULL conn.
151          */
152         return SLAP_PTRCMP(lc1->conn, lc2->conn);
153 }
154
155 /*
156  * ldap_back_conn_dup
157  *
158  * returns -1 in case a duplicate struct ldapconn has been inserted;
159  * used by avl stuff
160  */
161 int
162 ldap_back_conn_dup(
163         void *c1,
164         void *c2
165         )
166 {
167         struct ldapconn *lc1 = (struct ldapconn *)c1;
168         struct ldapconn *lc2 = (struct ldapconn *)c2;
169
170         /* Cannot have more than one shared session with same DN */
171         if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
172                  lc1->conn == lc2->conn ) return -1;
173                 
174         return 0;
175 }
176
177 #if PRINT_CONNTREE > 0
178 static void ravl_print( Avlnode *root, int depth )
179 {
180         int     i;
181         struct ldapconn *lc;
182         
183         if ( root == 0 )
184                 return;
185         
186         ravl_print( root->avl_right, depth+1 );
187         
188         for ( i = 0; i < depth; i++ )
189                 printf( "   " );
190
191         lc = root->avl_data;
192         printf( "lc(%lx) local(%s) conn(%lx) %d\n",
193                         lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
194         
195         ravl_print( root->avl_left, depth+1 );
196 }
197
198 static void myprint( Avlnode *root )
199 {
200         printf( "********\n" );
201         
202         if ( root == 0 )
203                 printf( "\tNULL\n" );
204
205         else
206                 ravl_print( root, 0 );
207         
208         printf( "********\n" );
209 }
210 #endif /* PRINT_CONNTREE */
211
212 struct ldapconn *
213 ldap_back_getconn(Operation *op, SlapReply *rs)
214 {
215         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
216         struct ldapconn *lc, lc_curr;
217         LDAP *ld;
218         int is_priv = 0;
219
220         /* Searches for a ldapconn in the avl tree */
221
222         /* Explicit binds must not be shared */
223         if ( op->o_tag == LDAP_REQ_BIND
224                 || (op->o_conn
225                   && (op->o_bd == op->o_conn->c_authz_backend ))) {
226                 lc_curr.conn = op->o_conn;
227         } else {
228                 lc_curr.conn = NULL;
229         }
230         
231         /* Internal searches are privileged and shared. So is root. */
232         if ( op->o_do_not_cache || be_isroot_dn( li->be, &op->o_ndn ) ) {
233                 lc_curr.local_dn = li->be->be_rootndn;
234                 lc_curr.conn = NULL;
235                 is_priv = 1;
236         } else {
237                 lc_curr.local_dn = op->o_ndn;
238         }
239
240         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
241         lc = (struct ldapconn *)avl_find( li->conntree, 
242                 (caddr_t)&lc_curr, ldap_back_conn_cmp );
243         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
244
245         /* Looks like we didn't get a bind. Open a new session... */
246         if (!lc) {
247                 int vers = op->o_protocol;
248                 rs->sr_err = ldap_initialize(&ld, li->url);
249                 
250                 if (rs->sr_err != LDAP_SUCCESS) {
251                         rs->sr_err = slap_map_api2result( rs );
252                         if (rs->sr_text == NULL) {
253                                 rs->sr_text = "ldap_initialize() failed";
254                         }
255                         if (op->o_conn) send_ldap_result( op, rs );
256                         rs->sr_text = NULL;
257                         return( NULL );
258                 }
259                 /* Set LDAP version. This will always succeed: If the client
260                  * bound with a particular version, then so can we.
261                  */
262                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
263                                 (const void *)&vers);
264                 /* FIXME: configurable? */
265                 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
266
267                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
268                 lc->conn = lc_curr.conn;
269                 lc->ld = ld;
270                 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
271
272 #ifdef ENABLE_REWRITE
273                 /*
274                  * Sets a cookie for the rewrite session
275                  *
276                  * FIXME: the o_conn might be no longer valid,
277                  * since we may have different entries
278                  * for the same connection
279                  */
280                 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
281 #endif /* ENABLE_REWRITE */
282
283                 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
284
285                 if ( is_priv ) {
286                         ber_dupbv( &lc->cred, &li->bindpw );
287                         ber_dupbv( &lc->bound_dn, &li->binddn );
288                 } else {
289                         lc->cred.bv_len = 0;
290                         lc->cred.bv_val = NULL;
291                         lc->bound_dn.bv_val = NULL;
292                         lc->bound_dn.bv_len = 0;
293                         if ( op->o_conn && op->o_conn->c_dn.bv_len != 0
294                                         && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
295                                 
296                                 dncookie dc;
297                                 struct berval bv;
298
299                                 /*
300                                  * Rewrite the bind dn if needed
301                                  */
302                                 dc.rwmap = &li->rwmap;
303 #ifdef ENABLE_REWRITE
304                                 dc.conn = op->o_conn;
305                                 dc.rs = rs;
306                                 dc.ctx = "bindDN";
307 #else
308                                 dc.tofrom = 1;
309                                 dc.normalized = 0;
310 #endif
311
312                                 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
313                                         send_ldap_result( op, rs );
314                                         return NULL;
315                                 }
316
317                                 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
318                                         ber_dupbv( &lc->bound_dn, &bv );
319                                 } else {
320                                         lc->bound_dn = bv;
321                                 }
322                         }
323                 }
324
325                 lc->bound = 0;
326
327                 /* Inserts the newly created ldapconn in the avl tree */
328                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
329                 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
330                         ldap_back_conn_cmp, ldap_back_conn_dup );
331
332 #if PRINT_CONNTREE > 0
333                 myprint( li->conntree );
334 #endif /* PRINT_CONNTREE */
335         
336                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
337
338 #ifdef NEW_LOGGING
339                 LDAP_LOG( BACK_LDAP, INFO, 
340                         "ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0);
341 #else /* !NEW_LOGGING */
342                 Debug( LDAP_DEBUG_TRACE,
343                         "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
344 #endif /* !NEW_LOGGING */
345         
346                 /* Err could be -1 in case a duplicate ldapconn is inserted */
347                 if ( rs->sr_err != 0 ) {
348                         ldap_back_conn_free( lc );
349                         if (op->o_conn) {
350                                 send_ldap_error( op, rs, LDAP_OTHER,
351                                 "internal server error" );
352                         }
353                         return( NULL );
354                 }
355         } else {
356 #ifdef NEW_LOGGING
357                 LDAP_LOG( BACK_LDAP, INFO, 
358                         "ldap_back_getconn: conn %p fetched\n", 
359                         (void *) lc, 0, 0 );
360 #else /* !NEW_LOGGING */
361                 Debug( LDAP_DEBUG_TRACE,
362                         "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
363 #endif /* !NEW_LOGGING */
364         }
365         
366         return( lc );
367 }
368
369 /*
370  * ldap_back_dobind
371  *
372  * Note: as the check for the value of lc->bound was already here, I removed
373  * it from all the callers, and I made the function return the flag, so
374  * it can be used to simplify the check.
375  */
376 int
377 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
378 {       
379         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
380         int rc;
381         ber_int_t msgid;
382
383         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
384         if ( !lc->bound ) {
385 #ifdef LDAP_BACK_PROXY_AUTHZ
386                 int     gotit = 0;
387 #if 0
388                 /*
389                  * FIXME: we need to let clients use proxyAuthz
390                  * otherwise we cannot do symmetric pools of servers;
391                  * we have to live with the fact that a user can
392                  * authorize itself as any ID that is allowed
393                  * by the saslAuthzTo directive of the "proxyauthzdn".
394                  */
395                 /*
396                  * NOTE: current Proxy Authorization specification
397                  * and implementation do not allow proxy authorization
398                  * control to be provided with Bind requests
399                  */
400                 gotit = op->o_proxy_authz;
401 #endif
402
403                 /*
404                  * if no bind took place yet, but the connection is bound
405                  * and the "proxyauthzdn" is set, then bind as 
406                  * "proxyauthzdn" and explicitly add the proxyAuthz 
407                  * control to every operation with the dn bound 
408                  * to the connection as control value.
409                  */
410                 if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
411                                 && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
412                                 && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 ) 
413                                 && ! gotit ) {
414                         rs->sr_err = ldap_sasl_bind(lc->ld, li->proxyauthzdn.bv_val,
415                                 LDAP_SASL_SIMPLE, &li->proxyauthzpw, NULL, NULL, &msgid);
416
417                 } else
418 #endif /* LDAP_BACK_PROXY_AUTHZ */
419                 {
420                         rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
421                                 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
422                 }
423                 
424                 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
425                 if (rc == LDAP_SUCCESS) {
426                         lc->bound = 1;
427                 }
428         }
429         rc = lc->bound;
430         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
431         return rc;
432 }
433
434 /*
435  * ldap_back_rebind
436  *
437  * This is a callback used for chasing referrals using the same
438  * credentials as the original user on this session.
439  */
440 static int 
441 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
442         ber_int_t msgid, void *params )
443 {
444         struct ldapconn *lc = params;
445
446         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
447 }
448
449 #if 0 /* deprecated in favour of slap_map_api2result() */
450 /* Map API errors to protocol errors... */
451 int
452 ldap_back_map_result( SlapReply *rs )
453 {
454         switch(rs->sr_err)
455         {
456         case LDAP_SERVER_DOWN:
457                 return LDAP_UNAVAILABLE;
458         case LDAP_LOCAL_ERROR:
459                 return LDAP_OTHER;
460         case LDAP_ENCODING_ERROR:
461         case LDAP_DECODING_ERROR:
462                 return LDAP_PROTOCOL_ERROR;
463         case LDAP_TIMEOUT:
464                 return LDAP_UNAVAILABLE;
465         case LDAP_AUTH_UNKNOWN:
466                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
467         case LDAP_FILTER_ERROR:
468                 rs->sr_text = "Filter error";
469                 return LDAP_OTHER;
470         case LDAP_USER_CANCELLED:
471                 rs->sr_text = "User cancelled";
472                 return LDAP_OTHER;
473         case LDAP_PARAM_ERROR:
474                 return LDAP_PROTOCOL_ERROR;
475         case LDAP_NO_MEMORY:
476                 return LDAP_OTHER;
477         case LDAP_CONNECT_ERROR:
478                 return LDAP_UNAVAILABLE;
479         case LDAP_NOT_SUPPORTED:
480                 return LDAP_UNWILLING_TO_PERFORM;
481         case LDAP_CONTROL_NOT_FOUND:
482                 return LDAP_PROTOCOL_ERROR;
483         case LDAP_NO_RESULTS_RETURNED:
484                 return LDAP_NO_SUCH_OBJECT;
485         case LDAP_MORE_RESULTS_TO_RETURN:
486                 rs->sr_text = "More results to return";
487                 return LDAP_OTHER;
488         case LDAP_CLIENT_LOOP:
489         case LDAP_REFERRAL_LIMIT_EXCEEDED:
490                 return LDAP_LOOP_DETECT;
491         default:
492                 if ( LDAP_API_ERROR(rs->sr_err) )
493                         return LDAP_OTHER;
494                 return rs->sr_err;
495         }
496 }
497 #endif
498
499 int
500 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
501         ber_int_t msgid, int sendok)
502 {
503         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
504         char *match = NULL;
505         LDAPMessage *res = NULL;
506         char *text = NULL;
507
508 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
509
510         rs->sr_text = NULL;
511         rs->sr_matched = NULL;
512
513         /* if the error recorded in the reply corresponds
514          * to a successful state, get the error from the
515          * remote server response */
516         if ( ERR_OK( rs->sr_err ) ) {
517                 /* if result parsing fails, note the failure reason */
518                 if ( ldap_result( lc->ld, msgid, 1, NULL, &res ) == -1 ) {
519                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
520                                         &rs->sr_err);
521
522                 /* otherwise get the result; if it is not
523                  * LDAP_SUCCESS, record it in the reply
524                  * structure (this includes 
525                  * LDAP_COMPARE_{TRUE|FALSE}) */
526                 } else {
527                         int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
528                                         &match, &text, NULL, NULL, 1);
529                         rs->sr_text = text;
530                         if ( rc != LDAP_SUCCESS ) rs->sr_err = rc;
531                 }
532         }
533
534         /* if the error in the reply structure is not
535          * LDAP_SUCCESS, try to map it from client 
536          * to server error */
537         if ( !ERR_OK( rs->sr_err ) ) {
538                 rs->sr_err = slap_map_api2result( rs );
539
540                 /* internal ops ( op->o_conn == NULL ) 
541                  * must not reply to client */
542                 if ( op->o_conn && !op->o_do_not_cache && match ) {
543                         struct berval dn, mdn;
544                         dncookie dc;
545
546                         dc.rwmap = &li->rwmap;
547 #ifdef ENABLE_REWRITE
548                         dc.conn = op->o_conn;
549                         dc.rs = rs;
550                         dc.ctx = "matchedDN";
551 #else
552                         dc.tofrom = 0;
553                         dc.normalized = 0;
554 #endif
555                         ber_str2bv(match, 0, 0, &dn);
556                         ldap_back_dn_massage(&dc, &dn, &mdn);
557
558                         /* record the (massaged) matched
559                          * DN into the reply structure */
560                         rs->sr_matched = mdn.bv_val;
561                                 
562                 }
563         }
564         if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
565                 send_ldap_result( op, rs );
566         }
567         if ( match ) {
568                 if ( rs->sr_matched != match ) {
569                         free( (char *)rs->sr_matched );
570                 }
571                 rs->sr_matched = NULL;
572                 ldap_memfree( match );
573         }
574         if ( text ) {
575                 ldap_memfree( text );
576         }
577         rs->sr_text = NULL;
578         return( ERR_OK( rs->sr_err ) ? 0 : -1 );
579 }
580
581 #ifdef LDAP_BACK_PROXY_AUTHZ
582 /*
583  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
584  * to existing server-side controls if required; if not,
585  * the existing server-side controls are placed in *pctrls.
586  * The caller, after using the controls in client API 
587  * operations, if ( *pctrls != op->o_ctrls ), should
588  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
589  * The function returns success if the control could
590  * be added if required, or if it did nothing; in the future,
591  * it might return some error if it failed.
592  * 
593  * if no bind took place yet, but the connection is bound
594  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
595  * and explicitly add proxyAuthz the control to every operation
596  * with the dn bound to the connection as control value.
597  *
598  * If no server-side controls are defined for the operation,
599  * simply add the proxyAuthz control; otherwise, if the
600  * proxyAuthz control is not already set, add it as
601  * the first one (FIXME: is controls order significant
602  * for security?).
603  */
604 int
605 ldap_back_proxy_authz_ctrl(
606                 struct ldapconn *lc,
607                 Operation       *op,
608                 SlapReply       *rs,
609                 LDAPControl     ***pctrls )
610 {
611         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
612         LDAPControl     **ctrls = NULL;
613
614         *pctrls = NULL;
615
616         if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
617                         && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
618                         && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 ) ) {
619                 int     i = 0;
620
621                 if ( !op->o_proxy_authz ) {
622                         ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
623                         ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
624                         
625                         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
626                         ctrls[ 0 ]->ldctl_iscritical = 1;
627                         ctrls[ 0 ]->ldctl_value.bv_len = op->o_conn->c_dn.bv_len + 3;
628                         ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
629                         AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", sizeof( "dn:" ) - 1 );
630                         AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + sizeof( "dn:") - 1,
631                                         op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_len + 1 );
632
633                         if ( op->o_ctrls ) {
634                                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
635                                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
636                                 }
637                         }
638                         ctrls[ i + 1 ] = NULL;
639
640                 } else {
641                         /*
642                          * FIXME: we do not want to perform proxyAuthz
643                          * on behalf of the client, because this would
644                          * be performed with "proxyauthzdn" privileges.
645                          *
646                          * This might actually be too strict, since
647                          * the "proxyauthzdn" saslAuthzTo, and each entry's
648                          * saslAuthzFrom attributes may be crafted
649                          * to avoid unwanted proxyAuthz to take place.
650                          */
651 #if 0
652                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
653                         rs->sr_text = "proxyAuthz not allowed within namingContext";
654 #endif
655                 }
656         }
657
658         if ( ctrls == NULL ) {
659                 ctrls = op->o_ctrls;
660         }
661
662         *pctrls = ctrls;
663         
664         return rs->sr_err;
665 }
666 #endif /* LDAP_BACK_PROXY_AUTHZ */