]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
8eccce15e222208e9e33248e2b911904b118c34a
[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-2005 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 int
213 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
214 {
215         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
216
217         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
218         lc = avl_delete( &li->conntree, (caddr_t)lc,
219                         ldap_back_conn_cmp );
220         ldap_back_conn_free( (void *)lc );
221         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
222
223         return 0;
224 }
225
226 struct ldapconn *
227 ldap_back_getconn(Operation *op, SlapReply *rs)
228 {
229         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
230         struct ldapconn *lc, lc_curr;
231         LDAP *ld;
232         int is_priv = 0;
233
234         /* Searches for a ldapconn in the avl tree */
235
236         /* Explicit binds must not be shared */
237         if ( op->o_tag == LDAP_REQ_BIND
238                 || (op->o_conn
239                   && (op->o_bd == op->o_conn->c_authz_backend ))) {
240                 lc_curr.conn = op->o_conn;
241         } else {
242                 lc_curr.conn = NULL;
243         }
244         
245         /* Internal searches are privileged and shared. So is root. */
246         if ( op->o_do_not_cache || be_isroot( op ) ) {
247                 lc_curr.local_dn = op->o_bd->be_rootndn;
248                 lc_curr.conn = NULL;
249                 is_priv = 1;
250         } else {
251                 lc_curr.local_dn = op->o_ndn;
252         }
253
254         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
255         lc = (struct ldapconn *)avl_find( li->conntree, 
256                 (caddr_t)&lc_curr, ldap_back_conn_cmp );
257         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
258
259         /* Looks like we didn't get a bind. Open a new session... */
260         if (!lc) {
261                 int vers = op->o_protocol;
262                 rs->sr_err = ldap_initialize(&ld, li->url);
263                 
264                 if (rs->sr_err != LDAP_SUCCESS) {
265                         rs->sr_err = slap_map_api2result( rs );
266                         if (rs->sr_text == NULL) {
267                                 rs->sr_text = "ldap_initialize() failed";
268                         }
269                         if (op->o_conn) send_ldap_result( op, rs );
270                         rs->sr_text = NULL;
271                         return( NULL );
272                 }
273                 /* Set LDAP version. This will always succeed: If the client
274                  * bound with a particular version, then so can we.
275                  */
276                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
277                                 (const void *)&vers);
278                 /* FIXME: configurable? */
279                 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
280
281                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
282                 lc->conn = lc_curr.conn;
283                 lc->ld = ld;
284                 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
285
286 #ifdef ENABLE_REWRITE
287                 /*
288                  * Sets a cookie for the rewrite session
289                  *
290                  * FIXME: the o_conn might be no longer valid,
291                  * since we may have different entries
292                  * for the same connection
293                  */
294                 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
295 #endif /* ENABLE_REWRITE */
296
297                 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
298
299                 if ( is_priv ) {
300                         ber_dupbv( &lc->cred, &li->bindpw );
301                         ber_dupbv( &lc->bound_dn, &li->binddn );
302                 } else {
303                         lc->cred.bv_len = 0;
304                         lc->cred.bv_val = NULL;
305                         lc->bound_dn.bv_val = NULL;
306                         lc->bound_dn.bv_len = 0;
307                         if ( op->o_conn && op->o_conn->c_dn.bv_len != 0
308                                         && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
309                                 
310                                 dncookie dc;
311                                 struct berval bv;
312
313                                 /*
314                                  * Rewrite the bind dn if needed
315                                  */
316                                 dc.rwmap = &li->rwmap;
317 #ifdef ENABLE_REWRITE
318                                 dc.conn = op->o_conn;
319                                 dc.rs = rs;
320                                 dc.ctx = "bindDN";
321 #else
322                                 dc.tofrom = 1;
323                                 dc.normalized = 0;
324 #endif
325
326                                 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
327                                         send_ldap_result( op, rs );
328                                         return NULL;
329                                 }
330
331                                 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
332                                         ber_dupbv( &lc->bound_dn, &bv );
333                                 } else {
334                                         lc->bound_dn = bv;
335                                 }
336                         }
337                 }
338
339                 lc->bound = 0;
340
341                 /* Inserts the newly created ldapconn in the avl tree */
342                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
343                 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
344                         ldap_back_conn_cmp, ldap_back_conn_dup );
345
346 #if PRINT_CONNTREE > 0
347                 myprint( li->conntree );
348 #endif /* PRINT_CONNTREE */
349         
350                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
351
352 #ifdef NEW_LOGGING
353                 LDAP_LOG( BACK_LDAP, INFO, 
354                         "ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0);
355 #else /* !NEW_LOGGING */
356                 Debug( LDAP_DEBUG_TRACE,
357                         "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
358 #endif /* !NEW_LOGGING */
359         
360                 /* Err could be -1 in case a duplicate ldapconn is inserted */
361                 if ( rs->sr_err != 0 ) {
362                         ldap_back_conn_free( lc );
363                         if (op->o_conn) {
364                                 send_ldap_error( op, rs, LDAP_OTHER,
365                                 "internal server error" );
366                         }
367                         return( NULL );
368                 }
369         } else {
370 #ifdef NEW_LOGGING
371                 LDAP_LOG( BACK_LDAP, INFO, 
372                         "ldap_back_getconn: conn %p fetched\n", 
373                         (void *) lc, 0, 0 );
374 #else /* !NEW_LOGGING */
375                 Debug( LDAP_DEBUG_TRACE,
376                         "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
377 #endif /* !NEW_LOGGING */
378         }
379         
380         return( lc );
381 }
382
383 /*
384  * ldap_back_dobind
385  *
386  * Note: as the check for the value of lc->bound was already here, I removed
387  * it from all the callers, and I made the function return the flag, so
388  * it can be used to simplify the check.
389  */
390 int
391 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
392 {       
393         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
394         int rc;
395         ber_int_t msgid;
396
397         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
398         if ( !lc->bound ) {
399 #ifdef LDAP_BACK_PROXY_AUTHZ
400                 int     gotit = 0;
401 #if 0
402                 /*
403                  * FIXME: we need to let clients use proxyAuthz
404                  * otherwise we cannot do symmetric pools of servers;
405                  * we have to live with the fact that a user can
406                  * authorize itself as any ID that is allowed
407                  * by the saslAuthzTo directive of the "proxyauthzdn".
408                  */
409                 /*
410                  * NOTE: current Proxy Authorization specification
411                  * and implementation do not allow proxy authorization
412                  * control to be provided with Bind requests
413                  */
414                 gotit = op->o_proxy_authz;
415 #endif
416
417                 /*
418                  * if no bind took place yet, but the connection is bound
419                  * and the "proxyauthzdn" is set, then bind as 
420                  * "proxyauthzdn" and explicitly add the proxyAuthz 
421                  * control to every operation with the dn bound 
422                  * to the connection as control value.
423                  */
424                 if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
425                                 && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
426                                 && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 ) 
427                                 && ! gotit ) {
428                         rs->sr_err = ldap_sasl_bind(lc->ld, li->proxyauthzdn.bv_val,
429                                 LDAP_SASL_SIMPLE, &li->proxyauthzpw, NULL, NULL, &msgid);
430
431                 } else
432 #endif /* LDAP_BACK_PROXY_AUTHZ */
433                 {
434                         rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
435                                 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
436                 }
437                 
438                 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
439                 if (rc == LDAP_SUCCESS) {
440                         lc->bound = 1;
441                 }
442         }
443         rc = lc->bound;
444         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
445         return rc;
446 }
447
448 /*
449  * ldap_back_rebind
450  *
451  * This is a callback used for chasing referrals using the same
452  * credentials as the original user on this session.
453  */
454 static int 
455 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
456         ber_int_t msgid, void *params )
457 {
458         struct ldapconn *lc = params;
459
460         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
461 }
462
463 #if 0 /* deprecated in favour of slap_map_api2result() */
464 /* Map API errors to protocol errors... */
465 int
466 ldap_back_map_result( SlapReply *rs )
467 {
468         switch(rs->sr_err)
469         {
470         case LDAP_SERVER_DOWN:
471                 return LDAP_UNAVAILABLE;
472         case LDAP_LOCAL_ERROR:
473                 return LDAP_OTHER;
474         case LDAP_ENCODING_ERROR:
475         case LDAP_DECODING_ERROR:
476                 return LDAP_PROTOCOL_ERROR;
477         case LDAP_TIMEOUT:
478                 return LDAP_UNAVAILABLE;
479         case LDAP_AUTH_UNKNOWN:
480                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
481         case LDAP_FILTER_ERROR:
482                 rs->sr_text = "Filter error";
483                 return LDAP_OTHER;
484         case LDAP_USER_CANCELLED:
485                 rs->sr_text = "User cancelled";
486                 return LDAP_OTHER;
487         case LDAP_PARAM_ERROR:
488                 return LDAP_PROTOCOL_ERROR;
489         case LDAP_NO_MEMORY:
490                 return LDAP_OTHER;
491         case LDAP_CONNECT_ERROR:
492                 return LDAP_UNAVAILABLE;
493         case LDAP_NOT_SUPPORTED:
494                 return LDAP_UNWILLING_TO_PERFORM;
495         case LDAP_CONTROL_NOT_FOUND:
496                 return LDAP_PROTOCOL_ERROR;
497         case LDAP_NO_RESULTS_RETURNED:
498                 return LDAP_NO_SUCH_OBJECT;
499         case LDAP_MORE_RESULTS_TO_RETURN:
500                 rs->sr_text = "More results to return";
501                 return LDAP_OTHER;
502         case LDAP_CLIENT_LOOP:
503         case LDAP_REFERRAL_LIMIT_EXCEEDED:
504                 return LDAP_LOOP_DETECT;
505         default:
506                 if ( LDAP_API_ERROR(rs->sr_err) )
507                         return LDAP_OTHER;
508                 return rs->sr_err;
509         }
510 }
511 #endif
512
513 int
514 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
515         ber_int_t msgid, int sendok)
516 {
517         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
518         char *match = NULL;
519         LDAPMessage *res = NULL;
520         char *text = NULL;
521
522 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
523
524         rs->sr_text = NULL;
525         rs->sr_matched = NULL;
526
527         /* if the error recorded in the reply corresponds
528          * to a successful state, get the error from the
529          * remote server response */
530         if ( ERR_OK( rs->sr_err ) ) {
531                 /* if result parsing fails, note the failure reason */
532                 if ( ldap_result( lc->ld, msgid, 1, NULL, &res ) == -1 ) {
533                         ldap_get_option( lc->ld, LDAP_OPT_ERROR_NUMBER,
534                                         &rs->sr_err );
535
536                 /* otherwise get the result; if it is not
537                  * LDAP_SUCCESS, record it in the reply
538                  * structure (this includes 
539                  * LDAP_COMPARE_{TRUE|FALSE}) */
540                 } else {
541                         int rc = ldap_parse_result( lc->ld, res, &rs->sr_err,
542                                         &match, &text, NULL, NULL, 1 );
543                         rs->sr_text = text;
544                         if ( rc != LDAP_SUCCESS ) rs->sr_err = rc;
545                 }
546         }
547
548         /* if the error in the reply structure is not
549          * LDAP_SUCCESS, try to map it from client 
550          * to server error */
551         if ( !ERR_OK( rs->sr_err ) ) {
552                 rs->sr_err = slap_map_api2result( rs );
553
554                 /* internal ops ( op->o_conn == NULL ) 
555                  * must not reply to client */
556                 if ( op->o_conn && !op->o_do_not_cache && match ) {
557                         struct berval dn, mdn;
558                         dncookie dc;
559
560                         dc.rwmap = &li->rwmap;
561 #ifdef ENABLE_REWRITE
562                         dc.conn = op->o_conn;
563                         dc.rs = rs;
564                         dc.ctx = "matchedDN";
565 #else
566                         dc.tofrom = 0;
567                         dc.normalized = 0;
568 #endif
569                         ber_str2bv(match, 0, 0, &dn);
570                         ldap_back_dn_massage(&dc, &dn, &mdn);
571
572                         /* record the (massaged) matched
573                          * DN into the reply structure */
574                         rs->sr_matched = mdn.bv_val;
575                                 
576                 }
577         }
578         if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
579                 send_ldap_result( op, rs );
580         }
581         if ( match ) {
582                 if ( rs->sr_matched != match ) {
583                         free( (char *)rs->sr_matched );
584                 }
585                 rs->sr_matched = NULL;
586                 ldap_memfree( match );
587         }
588         if ( text ) {
589                 ldap_memfree( text );
590         }
591         rs->sr_text = NULL;
592         return( ERR_OK( rs->sr_err ) ? 0 : -1 );
593 }
594
595 /* return true if bound, false if failed */
596 int
597 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs )
598 {
599         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
600         int vers = op->o_protocol;
601         LDAP *ld;
602
603         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
604         ldap_unbind( lc->ld );
605         lc->bound = 0;
606         rs->sr_err = ldap_initialize(&ld, li->url);
607                 
608         if (rs->sr_err != LDAP_SUCCESS) {
609                 rs->sr_err = slap_map_api2result( rs );
610                 if (rs->sr_text == NULL) {
611                         rs->sr_text = "ldap_initialize() failed";
612                 }
613                 if (op->o_conn) send_ldap_result( op, rs );
614                 rs->sr_text = NULL;
615                 return 0;
616         }
617         /* Set LDAP version. This will always succeed: If the client
618          * bound with a particular version, then so can we.
619          */
620         ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers);
621         /* FIXME: configurable? */
622         ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
623         lc->ld = ld;
624         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
625         return ldap_back_dobind( lc, op, rs );
626 }
627
628 #ifdef LDAP_BACK_PROXY_AUTHZ
629 /*
630  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
631  * to existing server-side controls if required; if not,
632  * the existing server-side controls are placed in *pctrls.
633  * The caller, after using the controls in client API 
634  * operations, if ( *pctrls != op->o_ctrls ), should
635  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
636  * The function returns success if the control could
637  * be added if required, or if it did nothing; in the future,
638  * it might return some error if it failed.
639  * 
640  * if no bind took place yet, but the connection is bound
641  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
642  * and explicitly add proxyAuthz the control to every operation
643  * with the dn bound to the connection as control value.
644  *
645  * If no server-side controls are defined for the operation,
646  * simply add the proxyAuthz control; otherwise, if the
647  * proxyAuthz control is not already set, add it as
648  * the first one (FIXME: is controls order significant
649  * for security?).
650  */
651 int
652 ldap_back_proxy_authz_ctrl(
653                 struct ldapconn *lc,
654                 Operation       *op,
655                 SlapReply       *rs,
656                 LDAPControl     ***pctrls )
657 {
658         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
659         LDAPControl     **ctrls = NULL;
660
661         *pctrls = NULL;
662
663         if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
664                         && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
665                         && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 ) ) {
666                 int     i = 0;
667
668                 if ( !op->o_proxy_authz ) {
669                         ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
670                         ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
671                         
672                         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
673                         ctrls[ 0 ]->ldctl_iscritical = 1;
674                         ctrls[ 0 ]->ldctl_value.bv_len = op->o_conn->c_dn.bv_len + 3;
675                         ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
676                         AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", sizeof( "dn:" ) - 1 );
677                         AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + sizeof( "dn:") - 1,
678                                         op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_len + 1 );
679
680                         if ( op->o_ctrls ) {
681                                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
682                                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
683                                 }
684                         }
685                         ctrls[ i + 1 ] = NULL;
686
687                 } else {
688                         /*
689                          * FIXME: we do not want to perform proxyAuthz
690                          * on behalf of the client, because this would
691                          * be performed with "proxyauthzdn" privileges.
692                          *
693                          * This might actually be too strict, since
694                          * the "proxyauthzdn" saslAuthzTo, and each entry's
695                          * saslAuthzFrom attributes may be crafted
696                          * to avoid unwanted proxyAuthz to take place.
697                          */
698 #if 0
699                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
700                         rs->sr_text = "proxyAuthz not allowed within namingContext";
701 #endif
702                 }
703         }
704
705         if ( ctrls == NULL ) {
706                 ctrls = op->o_ctrls;
707         }
708
709         *pctrls = ctrls;
710         
711         return rs->sr_err;
712 }
713 #endif /* LDAP_BACK_PROXY_AUTHZ */