]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
26e94c62c04638c2aed4bc36e2c03c1a0d0b10f0
[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                 /*
384                  * FIXME: we need to let clients use proxyAuthz
385                  * otherwise we cannot do symmetric pools of servers;
386                  * we have to live with the fact that a user can
387                  * authorize itself as any ID that is allowed
388                  * by the saslAuthzTo directive of the "proxyauthzdn".
389                  */
390                 /*
391                  * NOTE: current Proxy Authorization specification
392                  * and implementation do not allow proxy authorization
393                  * control to be provided with Bind requests
394                  */
395                 /*
396                  * if no bind took place yet, but the connection is bound
397                  * and the "proxyauthzdn" is set, then bind as 
398                  * "proxyauthzdn" and explicitly add the proxyAuthz 
399                  * control to every operation with the dn bound 
400                  * to the connection as control value.
401                  */
402                 if ( op->o_conn != NULL
403                                 && ( BER_BVISNULL( &lc->bound_dn ) || BER_BVISEMPTY( &lc->bound_dn ) ) ) {
404                         struct berval   binddn = slap_empty_bv;
405                         struct berval   bindcred = slap_empty_bv;
406
407                         /* bind as proxyauthzdn only if no idassert mode is requested,
408                          * or if the client's identity is authorized */
409                         switch ( li->idassert_mode ) {
410                         case LDAP_BACK_IDASSERT_LEGACY:
411                                 if ( !BER_BVISNULL( &op->o_conn->c_dn ) && !BER_BVISEMPTY( &op->o_conn->c_dn )
412                                                 && !BER_BVISNULL( &li->proxyauthzdn ) && !BER_BVISEMPTY( &li->proxyauthzdn ) )
413                                 {
414                                         binddn = li->proxyauthzdn;
415                                         bindcred = li->proxyauthzpw;
416                                 }
417                                 break;
418
419                         default:
420                                 if ( li->idassert_authz ) {
421                                         struct berval   authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
422
423                                         rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
424                                                         &authcDN, &authcDN );
425                                         if ( rs->sr_err != LDAP_SUCCESS ) {
426                                                 send_ldap_result( op, rs );
427                                                 lc->bound = 0;
428                                                 goto done;
429                                         }
430                                 }
431                                 binddn = li->proxyauthzdn;
432                                 bindcred = li->proxyauthzpw;
433                                 break;
434                         }
435
436                         rs->sr_err = ldap_sasl_bind(lc->ld, binddn.bv_val,
437                                 LDAP_SASL_SIMPLE, &bindcred, NULL, NULL, &msgid);
438
439                 } else
440 #endif /* LDAP_BACK_PROXY_AUTHZ */
441                 {
442                         rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
443                                 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
444                 }
445                 
446                 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
447                 if (rc == LDAP_SUCCESS) {
448                         lc->bound = 1;
449                 }
450         }
451
452 done:;
453         rc = lc->bound;
454         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
455         return rc;
456 }
457
458 /*
459  * ldap_back_rebind
460  *
461  * This is a callback used for chasing referrals using the same
462  * credentials as the original user on this session.
463  */
464 static int 
465 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
466         ber_int_t msgid, void *params )
467 {
468         struct ldapconn *lc = params;
469
470         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
471 }
472
473 #if 0 /* deprecated in favour of slap_map_api2result() */
474 /* Map API errors to protocol errors... */
475 int
476 ldap_back_map_result( SlapReply *rs )
477 {
478         switch(rs->sr_err)
479         {
480         case LDAP_SERVER_DOWN:
481                 return LDAP_UNAVAILABLE;
482         case LDAP_LOCAL_ERROR:
483                 return LDAP_OTHER;
484         case LDAP_ENCODING_ERROR:
485         case LDAP_DECODING_ERROR:
486                 return LDAP_PROTOCOL_ERROR;
487         case LDAP_TIMEOUT:
488                 return LDAP_UNAVAILABLE;
489         case LDAP_AUTH_UNKNOWN:
490                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
491         case LDAP_FILTER_ERROR:
492                 rs->sr_text = "Filter error";
493                 return LDAP_OTHER;
494         case LDAP_USER_CANCELLED:
495                 rs->sr_text = "User cancelled";
496                 return LDAP_OTHER;
497         case LDAP_PARAM_ERROR:
498                 return LDAP_PROTOCOL_ERROR;
499         case LDAP_NO_MEMORY:
500                 return LDAP_OTHER;
501         case LDAP_CONNECT_ERROR:
502                 return LDAP_UNAVAILABLE;
503         case LDAP_NOT_SUPPORTED:
504                 return LDAP_UNWILLING_TO_PERFORM;
505         case LDAP_CONTROL_NOT_FOUND:
506                 return LDAP_PROTOCOL_ERROR;
507         case LDAP_NO_RESULTS_RETURNED:
508                 return LDAP_NO_SUCH_OBJECT;
509         case LDAP_MORE_RESULTS_TO_RETURN:
510                 rs->sr_text = "More results to return";
511                 return LDAP_OTHER;
512         case LDAP_CLIENT_LOOP:
513         case LDAP_REFERRAL_LIMIT_EXCEEDED:
514                 return LDAP_LOOP_DETECT;
515         default:
516                 if ( LDAP_API_ERROR(rs->sr_err) )
517                         return LDAP_OTHER;
518                 return rs->sr_err;
519         }
520 }
521 #endif
522
523 int
524 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
525         ber_int_t msgid, int sendok)
526 {
527         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
528         char *match = NULL;
529         LDAPMessage *res = NULL;
530         char *text = NULL;
531
532 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
533
534         rs->sr_text = NULL;
535         rs->sr_matched = NULL;
536
537         /* if the error recorded in the reply corresponds
538          * to a successful state, get the error from the
539          * remote server response */
540         if ( ERR_OK( rs->sr_err ) ) {
541                 /* if result parsing fails, note the failure reason */
542                 if ( ldap_result( lc->ld, msgid, 1, NULL, &res ) == -1 ) {
543                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
544                                         &rs->sr_err);
545
546                 /* otherwise get the result; if it is not
547                  * LDAP_SUCCESS, record it in the reply
548                  * structure (this includes 
549                  * LDAP_COMPARE_{TRUE|FALSE}) */
550                 } else {
551                         int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
552                                         &match, &text, NULL, NULL, 1);
553                         rs->sr_text = text;
554                         if ( rc != LDAP_SUCCESS ) rs->sr_err = rc;
555                 }
556         }
557
558         /* if the error in the reply structure is not
559          * LDAP_SUCCESS, try to map it from client 
560          * to server error */
561         if ( !ERR_OK( rs->sr_err ) ) {
562                 rs->sr_err = slap_map_api2result( rs );
563
564                 /* internal ops ( op->o_conn == NULL ) 
565                  * must not reply to client */
566                 if ( op->o_conn && !op->o_do_not_cache && match ) {
567                         struct berval dn, mdn;
568                         dncookie dc;
569
570                         dc.rwmap = &li->rwmap;
571 #ifdef ENABLE_REWRITE
572                         dc.conn = op->o_conn;
573                         dc.rs = rs;
574                         dc.ctx = "matchedDN";
575 #else
576                         dc.tofrom = 0;
577                         dc.normalized = 0;
578 #endif
579                         ber_str2bv(match, 0, 0, &dn);
580                         ldap_back_dn_massage(&dc, &dn, &mdn);
581
582                         /* record the (massaged) matched
583                          * DN into the reply structure */
584                         rs->sr_matched = mdn.bv_val;
585                                 
586                 }
587         }
588         if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
589                 send_ldap_result( op, rs );
590         }
591         if ( match ) {
592                 if ( rs->sr_matched != match ) {
593                         free( (char *)rs->sr_matched );
594                 }
595                 rs->sr_matched = NULL;
596                 ldap_memfree( match );
597         }
598         if ( text ) {
599                 ldap_memfree( text );
600         }
601         rs->sr_text = NULL;
602         return( ERR_OK( rs->sr_err ) ? 0 : -1 );
603 }
604
605 #ifdef LDAP_BACK_PROXY_AUTHZ
606 /*
607  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
608  * to existing server-side controls if required; if not,
609  * the existing server-side controls are placed in *pctrls.
610  * The caller, after using the controls in client API 
611  * operations, if ( *pctrls != op->o_ctrls ), should
612  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
613  * The function returns success if the control could
614  * be added if required, or if it did nothing; in the future,
615  * it might return some error if it failed.
616  * 
617  * if no bind took place yet, but the connection is bound
618  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
619  * and explicitly add proxyAuthz the control to every operation
620  * with the dn bound to the connection as control value.
621  *
622  * If no server-side controls are defined for the operation,
623  * simply add the proxyAuthz control; otherwise, if the
624  * proxyAuthz control is not already set, add it as
625  * the first one (FIXME: is controls order significant
626  * for security?).
627  */
628 int
629 ldap_back_proxy_authz_ctrl(
630                 struct ldapconn *lc,
631                 Operation       *op,
632                 SlapReply       *rs,
633                 LDAPControl     ***pctrls )
634 {
635         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
636         LDAPControl     **ctrls = NULL;
637         int             i = 0;
638         struct berval   assertedID;
639
640         *pctrls = NULL;
641
642         if ( BER_BVISNULL( &li->proxyauthzdn ) ) {
643                 goto done;
644         }
645
646         if ( !op->o_conn ) {
647                 goto done;
648         }
649
650         if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
651                 if ( op->o_proxy_authz ) {
652                         /*
653                          * FIXME: we do not want to perform proxyAuthz
654                          * on behalf of the client, because this would
655                          * be performed with "proxyauthzdn" privileges.
656                          *
657                          * This might actually be too strict, since
658                          * the "proxyauthzdn" saslAuthzTo, and each entry's
659                          * saslAuthzFrom attributes may be crafted
660                          * to avoid unwanted proxyAuthz to take place.
661                          */
662 #if 0
663                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
664                         rs->sr_text = "proxyAuthz not allowed within namingContext";
665 #endif
666                         goto done;
667                 }
668
669                 if ( !BER_BVISNULL( &lc->bound_dn ) && !BER_BVISEMPTY( &lc->bound_dn ) ) {
670                         goto done;
671                 }
672
673                 if ( BER_BVISNULL( &op->o_conn->c_dn ) || BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
674                         goto done;
675                 }
676
677                 if ( BER_BVISEMPTY( &li->proxyauthzdn ) ) {
678                         goto done;
679                 }
680
681         } else if ( li->idassert_authz ) {
682                 int             rc;
683                 struct berval   authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
684
685
686                 rc = slap_sasl_matches( op, li->idassert_authz,
687                                 &authcDN, & authcDN );
688                 if ( rc != LDAP_SUCCESS ) {
689                         /* op->o_conn->c_dn is not authorized
690                          * to use idassert */
691                         return rc;
692                 }
693         }
694
695         switch ( li->idassert_mode ) {
696         case LDAP_BACK_IDASSERT_LEGACY:
697         case LDAP_BACK_IDASSERT_SELF:
698                 /* original behavior:
699                  * assert the client's identity */
700                 assertedID = op->o_conn->c_dn;
701                 break;
702
703         case LDAP_BACK_IDASSERT_ANONYMOUS:
704                 /* assert "anonymous" */
705                 assertedID = slap_empty_bv;
706                 break;
707
708         case LDAP_BACK_IDASSERT_NOASSERT:
709                 /* don't assert; bind as proxyauthzdn */
710                 goto done;
711
712         case LDAP_BACK_IDASSERT_OTHERID:
713         case LDAP_BACK_IDASSERT_OTHERDN:
714                 /* assert idassert DN */
715                 assertedID = li->idassert_id;
716                 break;
717
718         default:
719                 assert( 0 );
720         }
721
722         if ( BER_BVISNULL( &assertedID ) ) {
723                 assertedID = 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
732         /* already in u:ID form */
733         if ( li->idassert_mode == LDAP_BACK_IDASSERT_OTHERID ) {
734                 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
735
736         /* needs the dn: prefix */
737         } else {
738                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
739                 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
740                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
741                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
742                                 assertedID.bv_val, assertedID.bv_len + 1 );
743         }
744
745         if ( op->o_ctrls ) {
746                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
747                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
748                 }
749         }
750         ctrls[ i + 1 ] = NULL;
751
752 done:;
753         if ( ctrls == NULL ) {
754                 ctrls = op->o_ctrls;
755         }
756
757         *pctrls = ctrls;
758         
759         return rs->sr_err;
760 }
761 #endif /* LDAP_BACK_PROXY_AUTHZ */