]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
assert administrative identity instead of the required one if doing auth check in...
[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 #define AVL_INTERNAL
32 #include "slap.h"
33 #include "back-ldap.h"
34
35 #include <lutil_ldap.h>
36
37 #define PRINT_CONNTREE 0
38
39 static LDAP_REBIND_PROC ldap_back_rebind;
40
41 int
42 ldap_back_bind(
43     Operation           *op,
44     SlapReply           *rs )
45 {
46         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
47         struct ldapconn *lc;
48
49         struct berval mdn = BER_BVNULL;
50         int rc = 0;
51         ber_int_t msgid;
52         dncookie dc;
53
54         lc = ldap_back_getconn(op, rs);
55         if ( !lc ) {
56                 return( -1 );
57         }
58
59         /*
60          * Rewrite the bind dn if needed
61          */
62         dc.rwmap = &li->rwmap;
63 #ifdef ENABLE_REWRITE
64         dc.conn = op->o_conn;
65         dc.rs = rs;
66         dc.ctx = "bindDN";
67 #else
68         dc.tofrom = 1;
69         dc.normalized = 0;
70 #endif
71         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
72                 send_ldap_result( op, rs );
73                 return -1;
74         }
75
76         if ( !BER_BVISNULL( &lc->bound_dn ) ) {
77                 ch_free( lc->bound_dn.bv_val );
78                 BER_BVZERO( &lc->bound_dn );
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                 BER_BVZERO( &mdn );
93
94                 if ( li->savecred ) {
95                         if ( !BER_BVISNULL( &lc->cred ) ) {
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 ( !BER_BVISNULL( &lc->local_dn ) )
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 ( !BER_BVISNULL( &mdn ) && 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 && op->o_bd == op->o_conn->c_authz_backend ) ) {
239                 lc_curr.conn = op->o_conn;
240
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
251         } else {
252                 lc_curr.local_dn = op->o_ndn;
253         }
254
255         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
256         lc = (struct ldapconn *)avl_find( li->conntree, 
257                 (caddr_t)&lc_curr, ldap_back_conn_cmp );
258         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
259
260         /* Looks like we didn't get a bind. Open a new session... */
261         if (!lc) {
262                 int vers = op->o_protocol;
263                 rs->sr_err = ldap_initialize(&ld, li->url);
264                 
265                 if (rs->sr_err != LDAP_SUCCESS) {
266                         rs->sr_err = slap_map_api2result( rs );
267                         if (rs->sr_text == NULL) {
268                                 rs->sr_text = "ldap_initialize() failed";
269                         }
270                         if (op->o_conn) send_ldap_result( op, rs );
271                         rs->sr_text = NULL;
272                         return( NULL );
273                 }
274                 /* Set LDAP version. This will always succeed: If the client
275                  * bound with a particular version, then so can we.
276                  */
277                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
278                                 (const void *)&vers);
279                 /* FIXME: configurable? */
280                 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
281
282                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
283                 lc->conn = lc_curr.conn;
284                 lc->ld = ld;
285                 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
286
287 #ifdef ENABLE_REWRITE
288                 /*
289                  * Sets a cookie for the rewrite session
290                  *
291                  * FIXME: the o_conn might be no longer valid,
292                  * since we may have different entries
293                  * for the same connection
294                  */
295                 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
296 #endif /* ENABLE_REWRITE */
297
298                 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
299
300                 if ( is_priv ) {
301                         ber_dupbv( &lc->cred, &li->acl_passwd );
302                         ber_dupbv( &lc->bound_dn, &li->acl_authcDN );
303
304                 } else {
305                         BER_BVZERO( &lc->cred );
306                         BER_BVZERO( &lc->bound_dn );
307                         if ( op->o_conn && !BER_BVISEMPTY( &op->o_conn->c_dn )
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                 /*
401                  * FIXME: we need to let clients use proxyAuthz
402                  * otherwise we cannot do symmetric pools of servers;
403                  * we have to live with the fact that a user can
404                  * authorize itself as any ID that is allowed
405                  * by the authzTo directive of the "proxyauthzdn".
406                  */
407                 /*
408                  * NOTE: current Proxy Authorization specification
409                  * and implementation do not allow proxy authorization
410                  * control to be provided with Bind requests
411                  */
412                 /*
413                  * if no bind took place yet, but the connection is bound
414                  * and the "proxyauthzdn" is set, then bind as 
415                  * "proxyauthzdn" and explicitly add the proxyAuthz 
416                  * control to every operation with the dn bound 
417                  * to the connection as control value.
418                  */
419                 if ( op->o_conn != NULL && BER_BVISNULL( &lc->bound_dn ) ) {
420                         struct berval   binddn = slap_empty_bv;
421                         struct berval   bindcred = slap_empty_bv;
422                         int             dobind = 0;
423
424                         /* bind as proxyauthzdn only if no idassert mode
425                          * is requested, or if the client's identity
426                          * is authorized */
427                         switch ( li->idassert_mode ) {
428                         case LDAP_BACK_IDASSERT_LEGACY:
429                                 if ( !BER_BVISNULL( &op->o_conn->c_dn ) && !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
430                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
431                                         {
432                                                 binddn = li->idassert_authcDN;
433                                                 bindcred = li->idassert_passwd;
434                                                 dobind = 1;
435                                         }
436                                 }
437                                 break;
438
439                         default:
440                                 if ( li->idassert_authz ) {
441                                         struct berval   authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
442
443                                         rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
444                                                         &authcDN, &authcDN );
445                                         if ( rs->sr_err != LDAP_SUCCESS ) {
446                                                 send_ldap_result( op, rs );
447                                                 lc->bound = 0;
448                                                 goto done;
449                                         }
450                                 }
451
452                                 binddn = li->idassert_authcDN;
453                                 bindcred = li->idassert_passwd;
454                                 dobind = 1;
455                                 break;
456                         }
457
458                         if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
459 #ifdef HAVE_CYRUS_SASL
460                                 void            *defaults = NULL;
461                                 struct berval   authzID = BER_BVNULL;
462                                 int             freeauthz = 0;
463
464                                 /* if SASL supports native authz, prepare for it */
465                                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
466                                                 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
467                                 {
468                                         switch ( li->idassert_mode ) {
469                                         case LDAP_BACK_IDASSERT_OTHERID:
470                                         case LDAP_BACK_IDASSERT_OTHERDN:
471                                                 authzID = li->idassert_authzID;
472                                                 break;
473
474                                         case LDAP_BACK_IDASSERT_ANONYMOUS:
475                                                 BER_BVSTR( &authzID, "dn:" );
476                                                 break;
477
478                                         case LDAP_BACK_IDASSERT_SELF:
479                                                 if ( BER_BVISNULL( &op->o_conn->c_dn ) ) {
480                                                         /* connection is not authc'd, so don't idassert */
481                                                         BER_BVSTR( &authzID, "dn:" );
482                                                         break;
483                                                 }
484                                                 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_dn.bv_len;
485                                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
486                                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
487                                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
488                                                                 op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_len + 1 );
489                                                 freeauthz = 1;
490                                                 break;
491
492                                         default:
493                                                 break;
494                                         }
495                                 }
496
497 #if 0   /* will deal with this later... */
498                                 if ( sasl_secprops != NULL ) {
499                                         rs->sr_err = ldap_set_option( lc->ld, LDAP_OPT_X_SASL_SECPROPS,
500                                                 (void *) sasl_secprops );
501
502                                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
503                                                 send_ldap_result( op, rs );
504                                                 lc->bound = 0;
505                                                 goto done;
506                                                 
507                                         }
508                                 }
509 #endif
510
511                                 defaults = lutil_sasl_defaults( lc->ld,
512                                                 li->idassert_sasl_mech.bv_val,
513                                                 li->idassert_sasl_realm.bv_val,
514                                                 li->idassert_authcID.bv_val,
515                                                 li->idassert_passwd.bv_val,
516                                                 authzID.bv_val );
517
518                                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->ld, binddn.bv_val,
519                                                 li->idassert_sasl_mech.bv_val, NULL, NULL,
520                                                 li->idassert_sasl_flags, lutil_sasl_interact,
521                                                 defaults );
522
523                                 lutil_sasl_freedefs( defaults );
524                                 if ( freeauthz ) {
525                                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
526                                 }
527
528                                 rs->sr_err = slap_map_api2result( rs );
529                                 if ( rs->sr_err != LDAP_SUCCESS ) {
530                                         lc->bound = 0;
531                                         send_ldap_result( op, rs );
532
533                                 } else {
534                                         lc->bound = 1;
535                                 }
536                                 goto done;
537 #endif /* HAVE_CYRUS_SASL */
538                         }
539
540                         switch ( li->idassert_authmethod ) {
541                         case LDAP_AUTH_SIMPLE:
542                                 rs->sr_err = ldap_sasl_bind(lc->ld,
543                                                 binddn.bv_val, LDAP_SASL_SIMPLE,
544                                                 &bindcred, NULL, NULL, &msgid);
545                                 break;
546
547                         case LDAP_AUTH_NONE:
548                                 lc->bound = 1;
549                                 goto done;
550
551                         default:
552                                 /* unsupported! */
553                                 lc->bound = 0;
554                                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
555                                 send_ldap_result( op, rs );
556                                 goto done;
557                         }
558
559                 } else
560 #endif /* LDAP_BACK_PROXY_AUTHZ */
561                 {
562                         rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
563                                 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
564                 }
565                 
566                 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
567                 if (rc == LDAP_SUCCESS) {
568                         lc->bound = 1;
569                 }
570         }
571
572 done:;
573         rc = lc->bound;
574         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
575         return rc;
576 }
577
578 /*
579  * ldap_back_rebind
580  *
581  * This is a callback used for chasing referrals using the same
582  * credentials as the original user on this session.
583  */
584 static int 
585 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
586         ber_int_t msgid, void *params )
587 {
588         struct ldapconn *lc = params;
589
590         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
591 }
592
593 #if 0 /* deprecated in favour of slap_map_api2result() */
594 /* Map API errors to protocol errors... */
595 int
596 ldap_back_map_result( SlapReply *rs )
597 {
598         switch(rs->sr_err)
599         {
600         case LDAP_SERVER_DOWN:
601                 return LDAP_UNAVAILABLE;
602         case LDAP_LOCAL_ERROR:
603                 return LDAP_OTHER;
604         case LDAP_ENCODING_ERROR:
605         case LDAP_DECODING_ERROR:
606                 return LDAP_PROTOCOL_ERROR;
607         case LDAP_TIMEOUT:
608                 return LDAP_UNAVAILABLE;
609         case LDAP_AUTH_UNKNOWN:
610                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
611         case LDAP_FILTER_ERROR:
612                 rs->sr_text = "Filter error";
613                 return LDAP_OTHER;
614         case LDAP_USER_CANCELLED:
615                 rs->sr_text = "User cancelled";
616                 return LDAP_OTHER;
617         case LDAP_PARAM_ERROR:
618                 return LDAP_PROTOCOL_ERROR;
619         case LDAP_NO_MEMORY:
620                 return LDAP_OTHER;
621         case LDAP_CONNECT_ERROR:
622                 return LDAP_UNAVAILABLE;
623         case LDAP_NOT_SUPPORTED:
624                 return LDAP_UNWILLING_TO_PERFORM;
625         case LDAP_CONTROL_NOT_FOUND:
626                 return LDAP_PROTOCOL_ERROR;
627         case LDAP_NO_RESULTS_RETURNED:
628                 return LDAP_NO_SUCH_OBJECT;
629         case LDAP_MORE_RESULTS_TO_RETURN:
630                 rs->sr_text = "More results to return";
631                 return LDAP_OTHER;
632         case LDAP_CLIENT_LOOP:
633         case LDAP_REFERRAL_LIMIT_EXCEEDED:
634                 return LDAP_LOOP_DETECT;
635         default:
636                 if ( LDAP_API_ERROR(rs->sr_err) )
637                         return LDAP_OTHER;
638                 return rs->sr_err;
639         }
640 }
641 #endif
642
643 int
644 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
645         ber_int_t msgid, int sendok)
646 {
647         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
648         char *match = NULL;
649         LDAPMessage *res = NULL;
650         char *text = NULL;
651
652 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
653
654         rs->sr_text = NULL;
655         rs->sr_matched = NULL;
656
657         /* if the error recorded in the reply corresponds
658          * to a successful state, get the error from the
659          * remote server response */
660         if ( ERR_OK( rs->sr_err ) ) {
661                 /* if result parsing fails, note the failure reason */
662                 if ( ldap_result( lc->ld, msgid, 1, NULL, &res ) == -1 ) {
663                         ldap_get_option( lc->ld, LDAP_OPT_ERROR_NUMBER,
664                                         &rs->sr_err );
665
666                 /* otherwise get the result; if it is not
667                  * LDAP_SUCCESS, record it in the reply
668                  * structure (this includes 
669                  * LDAP_COMPARE_{TRUE|FALSE}) */
670                 } else {
671                         int rc = ldap_parse_result( lc->ld, res, &rs->sr_err,
672                                         &match, &text, NULL, NULL, 1 );
673                         rs->sr_text = text;
674                         if ( rc != LDAP_SUCCESS ) rs->sr_err = rc;
675                 }
676         }
677
678         /* if the error in the reply structure is not
679          * LDAP_SUCCESS, try to map it from client 
680          * to server error */
681         if ( !ERR_OK( rs->sr_err ) ) {
682                 rs->sr_err = slap_map_api2result( rs );
683
684                 /* internal ops ( op->o_conn == NULL ) 
685                  * must not reply to client */
686                 if ( op->o_conn && !op->o_do_not_cache && match ) {
687                         struct berval dn, mdn;
688                         dncookie dc;
689
690                         dc.rwmap = &li->rwmap;
691 #ifdef ENABLE_REWRITE
692                         dc.conn = op->o_conn;
693                         dc.rs = rs;
694                         dc.ctx = "matchedDN";
695 #else
696                         dc.tofrom = 0;
697                         dc.normalized = 0;
698 #endif
699                         ber_str2bv(match, 0, 0, &dn);
700                         ldap_back_dn_massage(&dc, &dn, &mdn);
701
702                         /* record the (massaged) matched
703                          * DN into the reply structure */
704                         rs->sr_matched = mdn.bv_val;
705                                 
706                 }
707         }
708         if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
709                 send_ldap_result( op, rs );
710         }
711         if ( match ) {
712                 if ( rs->sr_matched != match ) {
713                         free( (char *)rs->sr_matched );
714                 }
715                 rs->sr_matched = NULL;
716                 ldap_memfree( match );
717         }
718         if ( text ) {
719                 ldap_memfree( text );
720         }
721         rs->sr_text = NULL;
722         return( ERR_OK( rs->sr_err ) ? 0 : -1 );
723 }
724
725 #ifdef LDAP_BACK_PROXY_AUTHZ
726 /*
727  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
728  * to existing server-side controls if required; if not,
729  * the existing server-side controls are placed in *pctrls.
730  * The caller, after using the controls in client API 
731  * operations, if ( *pctrls != op->o_ctrls ), should
732  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
733  * The function returns success if the control could
734  * be added if required, or if it did nothing; in the future,
735  * it might return some error if it failed.
736  * 
737  * if no bind took place yet, but the connection is bound
738  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
739  * and explicitly add proxyAuthz the control to every operation
740  * with the dn bound to the connection as control value.
741  *
742  * If no server-side controls are defined for the operation,
743  * simply add the proxyAuthz control; otherwise, if the
744  * proxyAuthz control is not already set, add it as
745  * the first one (FIXME: is controls order significant
746  * for security?).
747  */
748 int
749 ldap_back_proxy_authz_ctrl(
750                 struct ldapconn *lc,
751                 Operation       *op,
752                 SlapReply       *rs,
753                 LDAPControl     ***pctrls )
754 {
755         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
756         LDAPControl     **ctrls = NULL;
757         int             i = 0,
758                         mode;
759         struct berval   assertedID;
760
761         *pctrls = NULL;
762
763         if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
764                         && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
765                 goto done;
766         }
767
768         if ( !op->o_conn ) {
769                 goto done;
770         }
771
772         if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
773                 if ( op->o_proxy_authz ) {
774                         /*
775                          * FIXME: we do not want to perform proxyAuthz
776                          * on behalf of the client, because this would
777                          * be performed with "proxyauthzdn" privileges.
778                          *
779                          * This might actually be too strict, since
780                          * the "proxyauthzdn" authzTo, and each entry's
781                          * authzFrom attributes may be crafted
782                          * to avoid unwanted proxyAuthz to take place.
783                          */
784 #if 0
785                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
786                         rs->sr_text = "proxyAuthz not allowed within namingContext";
787 #endif
788                         goto done;
789                 }
790
791                 if ( !BER_BVISNULL( &lc->bound_dn ) ) {
792                         goto done;
793                 }
794
795                 if ( BER_BVISNULL( &op->o_conn->c_dn ) ) {
796                         goto done;
797                 }
798
799                 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
800                         goto done;
801                 }
802
803         } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
804                 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
805                                 /* && ( !BER_BVISNULL( &op->o_conn->c_dn ) || lc->bound ) */ )
806                 {
807                         /* already asserted in SASL via native authz */
808                         /* NOTE: the test on lc->bound is used to trap
809                          * native authorization of anonymous users,
810                          * since in that case op->o_conn->c_dn is NULL */
811                         goto done;
812                 }
813
814         } else if ( li->idassert_authz ) {
815                 int             rc;
816                 struct berval   authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
817
818
819                 rc = slap_sasl_matches( op, li->idassert_authz,
820                                 &authcDN, & authcDN );
821                 if ( rc != LDAP_SUCCESS ) {
822                         /* op->o_conn->c_dn is not authorized
823                          * to use idassert */
824                         return rc;
825                 }
826         }
827
828         if ( op->o_proxy_authz ) {
829                 /*
830                  * FIXME: we can:
831                  * 1) ignore the already set proxyAuthz control
832                  * 2) leave it in place, and don't set ours
833                  * 3) add both
834                  * 4) reject the operation
835                  *
836                  * option (4) is very drastic
837                  * option (3) will make the remote server reject
838                  * the operation, thus being equivalent to (4)
839                  * option (2) will likely break the idassert
840                  * assumptions, so we cannot accept it;
841                  * option (1) means that we are contradicting
842                  * the client's reques.
843                  *
844                  * I think (4) is the only correct choice.
845                  */
846                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
847                 rs->sr_text = "proxyAuthz not allowed within namingContext";
848         }
849
850         if ( op->o_do_not_cache && op->o_is_auth_check ) {
851                 mode = LDAP_BACK_IDASSERT_NOASSERT;
852
853         } else {
854                 mode = li->idassert_mode;
855         }
856
857         switch ( mode ) {
858         case LDAP_BACK_IDASSERT_LEGACY:
859         case LDAP_BACK_IDASSERT_SELF:
860                 /* original behavior:
861                  * assert the client's identity */
862                 assertedID = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
863                 break;
864
865         case LDAP_BACK_IDASSERT_ANONYMOUS:
866                 /* assert "anonymous" */
867                 assertedID = slap_empty_bv;
868                 break;
869
870         case LDAP_BACK_IDASSERT_NOASSERT:
871                 /* don't assert; bind as proxyauthzdn */
872                 goto done;
873
874         case LDAP_BACK_IDASSERT_OTHERID:
875         case LDAP_BACK_IDASSERT_OTHERDN:
876                 /* assert idassert DN */
877                 assertedID = li->idassert_authzID;
878                 break;
879
880         default:
881                 assert( 0 );
882         }
883
884         if ( BER_BVISNULL( &assertedID ) ) {
885                 assertedID = slap_empty_bv;
886         }
887
888         ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
889         ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
890         
891         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
892         ctrls[ 0 ]->ldctl_iscritical = 1;
893
894         switch ( li->idassert_mode ) {
895         /* already in u:ID or dn:DN form */
896         case LDAP_BACK_IDASSERT_OTHERID:
897         case LDAP_BACK_IDASSERT_OTHERDN:
898                 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
899                 break;
900
901         /* needs the dn: prefix */
902         default:
903                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
904                 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
905                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
906                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
907                                 assertedID.bv_val, assertedID.bv_len + 1 );
908                 break;
909         }
910
911         if ( op->o_ctrls ) {
912                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
913                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
914                 }
915         }
916         ctrls[ i + 1 ] = NULL;
917
918 done:;
919         if ( ctrls == NULL ) {
920                 ctrls = op->o_ctrls;
921         }
922
923         *pctrls = ctrls;
924         
925         return rs->sr_err;
926 }
927 #endif /* LDAP_BACK_PROXY_AUTHZ */