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