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