]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
cc9fc94b88cc007fbf41ab322ad73fe82236f266
[openldap] / servers / slapd / back-meta / bind.c
1 /*
2  * Copyright 1998-2001 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  *
5  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
6  *
7  * This work has been developed to fulfill the requirements
8  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9  * to the OpenLDAP Foundation in the hope that it may be useful
10  * to the Open Source community, but WITHOUT ANY WARRANTY.
11  *
12  * Permission is granted to anyone to use this software for any purpose
13  * on any computer system, and to alter it and redistribute it, subject
14  * to the following restrictions:
15  *
16  * 1. The author and SysNet s.n.c. are not responsible for the consequences
17  *    of use of this software, no matter how awful, even if they arise from 
18  *    flaws in it.
19  *
20  * 2. The origin of this software must not be misrepresented, either by
21  *    explicit claim or by omission.  Since few users ever read sources,
22  *    credits should appear in the documentation.
23  *
24  * 3. Altered versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.  Since few users
26  *    ever read sources, credits should appear in the documentation.
27  *    SysNet s.n.c. cannot be responsible for the consequences of the
28  *    alterations.
29  *
30  * 4. This notice may not be removed or altered.
31  *
32  *
33  * This software is based on the backend back-ldap, implemented
34  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36  * contributors. The contribution of the original software to the present
37  * implementation is acknowledged in this copyright statement.
38  *
39  * A special acknowledgement goes to Howard for the overall architecture
40  * (and for borrowing large pieces of code), and to Mark, who implemented
41  * from scratch the attribute/objectclass mapping.
42  *
43  * The original copyright statement follows.
44  *
45  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
46  *
47  * Permission is granted to anyone to use this software for any purpose
48  * on any computer system, and to alter it and redistribute it, subject
49  * to the following restrictions:
50  *
51  * 1. The author is not responsible for the consequences of use of this
52  *    software, no matter how awful, even if they arise from flaws in it.
53  *
54  * 2. The origin of this software must not be misrepresented, either by
55  *    explicit claim or by omission.  Since few users ever read sources,
56  *    credits should appear in the documentation.
57  *
58  * 3. Altered versions must be plainly marked as such, and must not be
59  *    misrepresented as being the original software.  Since few users
60  *    ever read sources, credits should appear in the
61  *    documentation.
62  *
63  * 4. This notice may not be removed or altered.
64  *
65  */
66
67 #include "portable.h"
68
69 #include <stdio.h>
70
71 #include <ac/socket.h>
72 #include <ac/string.h>
73
74
75 #define AVL_INTERNAL
76 #include "slap.h"
77 #include "../back-ldap/back-ldap.h"
78 #include "back-meta.h"
79
80 int
81 meta_back_bind(
82                 Backend         *be,
83                 Connection      *conn,
84                 Operation       *op,
85                 const char      *dn,
86                 const char      *ndn,
87                 int             method,
88                 struct berval   *cred,
89                 char            **edn
90 )
91 {
92         struct metainfo *li = ( struct metainfo * )be->be_private;
93         struct metaconn *lc;
94
95         int rc = -1, i, gotit = 0, ndnlen, isroot = 0;
96         int op_type = META_OP_ALLOW_MULTIPLE;
97         int err = LDAP_SUCCESS;
98
99         char *realdn = (char *)dn;
100         char *realndn = (char *)ndn;
101         char *realcred = cred->bv_val;
102         int realmethod = method;
103
104 #ifdef NEW_LOGGING
105         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
106                         "meta_back_bind: dn: %s.\n", dn ));
107 #else /* !NEW_LOGGING */
108         Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s", dn, "", "" );
109 #endif /* !NEW_LOGGING */
110
111         *edn = NULL;
112
113         if ( method == LDAP_AUTH_SIMPLE 
114                         && be_isroot_pw( be, conn, ndn, cred ) ) {
115                 isroot = 1;
116                 *edn = ch_strdup( be_root_dn( be ) );
117                 op_type = META_OP_REQUIRE_ALL;
118         }
119         lc = meta_back_getconn( li, conn, op, op_type, ndn, NULL );
120         if ( !lc ) {
121 #ifdef NEW_LOGGING
122                 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
123                                 "meta_back_bind: no target for dn %s.\n",
124                                 dn ));
125 #else /* !NEW_LOGGING */
126                 Debug( LDAP_DEBUG_ANY,
127                                 "meta_back_bind: no target for dn %s.\n%s%s",
128                                 dn, "", "");
129 #endif /* !NEW_LOGGING */
130                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, 
131                                 NULL, NULL, NULL, NULL );
132                 return -1;
133         }
134
135         /*
136          * Each target is scanned ...
137          */
138         lc->bound_target = META_BOUND_NONE;
139         ndnlen = strlen( ndn );
140         for ( i = 0; i < li->ntargets; i++ ) {
141                 int lerr;
142
143                 /*
144                  * Skip non-candidates
145                  */
146                 if ( lc->conns[ i ]->candidate != META_CANDIDATE ) {
147                         continue;
148                 }
149
150                 if ( gotit == 0 ) {
151                         gotit = 1;
152                 } else {
153                         /*
154                          * A bind operation is expected to have
155                          * ONE CANDIDATE ONLY!
156                          */
157 #ifdef NEW_LOGGING
158                         LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
159                                         "==>meta_back_bind: more than one"
160                                         " candidate is attempting to bind"
161                                         " ...\n" ));
162 #else /* !NEW_LOGGING */
163                         Debug( LDAP_DEBUG_ANY,
164                                         "==>meta_back_bind: more than one"
165                                         " candidate is attempting to bind"
166                                         " ...\n%s%s%s", 
167                                         "", "", "" );
168 #endif /* !NEW_LOGGING */
169                 }
170
171                 if ( isroot && li->targets[ i ]->pseudorootdn != NULL ) {
172                         realdn = li->targets[ i ]->pseudorootdn;
173                         realndn = li->targets[ i ]->pseudorootdn;
174                         realcred = li->targets[ i ]->pseudorootpw;
175                         realmethod = LDAP_AUTH_SIMPLE;
176                 }
177                 
178                 lerr = meta_back_do_single_bind( li, lc,
179                                 realdn, realndn, realcred, realmethod, i );
180                 if ( lerr != LDAP_SUCCESS ) {
181                         err = lerr;
182                         ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
183                 } else {
184                         rc = LDAP_SUCCESS;
185                 }
186         }
187
188         if ( isroot ) {
189                 lc->bound_target = META_BOUND_ALL;
190         }
191
192         /*
193          * rc is LDAP_SUCCESS if at least one bind succeeded,
194          * err is the last error that occurred during a bind;
195          * if at least (and at most?) one bind succeedes, fine.
196          */
197         if ( rc != LDAP_SUCCESS /* && err != LDAP_SUCCESS */ ) {
198                 
199                 /*
200                  * deal with bind failure ...
201                  */
202                 err = ldap_back_map_result( err );
203                 send_ldap_result( conn, op, err, NULL, NULL, NULL, NULL );
204                 return -1;
205         }
206
207         return 0;
208 }
209
210 /*
211  * meta_back_do_single_bind
212  *
213  * attempts to perform a bind with creds
214  */
215 int
216 meta_back_do_single_bind(
217                 struct metainfo         *li,
218                 struct metaconn         *lc,
219                 const char              *dn,
220                 const char              *ndn,
221                 const char              *cred,
222                 int                     method,
223                 int                     candidate
224 )
225 {
226         char *mdn = NULL;
227         int rc;
228         
229         /*
230          * Rewrite the bind dn if needed
231          */
232         switch ( rewrite_session( li->targets[ candidate ]->rwinfo,
233                                 "bindDn", dn, lc->conn, &mdn ) ) {
234         case REWRITE_REGEXEC_OK:
235                 if ( mdn == NULL ) {
236                         mdn = ( char * )dn;
237                 }
238 #ifdef NEW_LOGGING
239                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
240                                 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn, mdn ));
241 #else /* !NEW_LOGGING */
242                 Debug( LDAP_DEBUG_ARGS,
243                                 "rw> bindDn: \"%s\" -> \"%s\"\n%s",
244                                 dn, mdn, "" );
245 #endif /* !NEW_LOGGING */
246                 break;
247                 
248         case REWRITE_REGEXEC_UNWILLING:
249                 return LDAP_UNWILLING_TO_PERFORM;
250
251         case REWRITE_REGEXEC_ERR:
252                 return LDAP_OPERATIONS_ERROR;
253         }
254
255         rc = ldap_bind_s( lc->conns[ candidate ]->ld, mdn, cred, method );
256         if ( rc != LDAP_SUCCESS ) {
257                 rc = ldap_back_map_result( rc );
258         } else {
259                 lc->conns[ candidate ]->bound_dn = ch_strdup( dn );
260                 lc->conns[ candidate ]->bound = META_BOUND;
261                 lc->bound_target = candidate;
262
263                 if ( li->cache.ttl != META_DNCACHE_DISABLED
264                                 && ndn[ 0 ] != '\0' ) {
265                         ( void )meta_dncache_update_entry( &li->cache,
266                                         ch_strdup( ndn ), candidate );
267                 }
268         }
269         
270         if ( mdn != dn ) {
271                 free( mdn );
272         }
273
274         return rc;
275 }
276
277 /*
278  * meta_back_dobind
279  */
280 int
281 meta_back_dobind( struct metaconn *lc, Operation *op )
282 {
283         struct metasingleconn **lsc;
284         int bound = 0, i;
285
286         /*
287          * all the targets are bound as pseudoroot
288          */
289         if ( lc->bound_target == META_BOUND_ALL ) {
290                 return 1;
291         }
292
293         for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
294                 int rc;
295
296                 /*
297                  * Not a candidate or something wrong with this target ...
298                  */
299                 if ( lsc[ 0 ]->ld == NULL ) {
300                         continue;
301                 }
302
303                 /*
304                  * If the target is already bound it is skipped
305                  */
306                 if ( lsc[ 0 ]->bound == META_BOUND && lc->bound_target == i ) {
307                         ++bound;
308                         continue;
309                 }
310
311                 /*
312                  * Otherwise an anonymous bind is performed
313                  * (note: if the target was already bound, the anonymous
314                  * bind clears the previous bind).
315                  */
316                 rc = ldap_bind_s( lsc[ 0 ]->ld, lsc[ 0 ]->bound_dn,
317                                 NULL, LDAP_AUTH_SIMPLE );
318                 if ( rc != LDAP_SUCCESS ) {
319                         
320 #ifdef NEW_LOGGING
321                         LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
322                                         "meta_back_dobind: (anonymous)"
323                                         " bind as \"%s\" failed"
324                                         " with error \"%s\"\n",
325                                         lsc[ 0 ]->bound_dn,
326                                         ldap_err2string( rc ) ));
327 #else /* !NEW_LOGGING */
328                         Debug( LDAP_DEBUG_ANY,
329                                         "==>meta_back_dobind: (anonymous)"
330                                         " bind as \"%s\" failed"
331                                         " with error \"%s\"\n%s",
332                                         lsc[ 0 ]->bound_dn,
333                                         ldap_err2string( rc ), "" );
334 #endif /* !NEW_LOGGING */
335
336                         /*
337                          * null cred bind should always succeed
338                          * as anonymous, so a failure means
339                          * the target is no longer candidate possibly
340                          * due to technical reasons (remote host down?)
341                          * so better clear the handle
342                          */
343                         ( void )meta_clear_one_candidate( lsc[ 0 ], 1 );
344                         continue;
345                 } /* else */
346                 
347                 lsc[ 0 ]->bound = META_ANONYMOUS;
348                 ++bound;
349         }
350
351         return( bound > 0 );
352 }
353
354 /*
355  * FIXME: error return must be handled in a cleaner way ...
356  */
357 int
358 meta_back_op_result( struct metaconn *lc, Operation *op )
359 {
360         int i, rerr = LDAP_SUCCESS;
361         struct metasingleconn **lsc;
362
363         for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
364                 int err = LDAP_SUCCESS;
365                 char *msg = NULL;
366                 char *match = NULL;
367
368                 ldap_get_option( lsc[ 0 ]->ld, LDAP_OPT_ERROR_NUMBER, &err );
369                 if ( err != LDAP_SUCCESS ) {
370                         /*
371                          * better check the type of error. In some cases
372                          * (search ?) it might be better to return a
373                          * success if at least one of the targets gave
374                          * positive result ...
375                          */
376                         ldap_get_option( lsc[ 0 ]->ld,
377                                         LDAP_OPT_ERROR_STRING, &msg );
378                         ldap_get_option( lsc[ 0 ]->ld,
379                                         LDAP_OPT_MATCHED_DN, &match );
380                         err = ldap_back_map_result( err );
381
382                         rerr = err;
383                 }
384
385                 /*
386                  * FIXME: need to rewrite "match" (need rwinfo)
387                  */
388                 send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
389                 
390 #ifdef NEW_LOGGING
391                 LDAP_LOG(( "backend", LDAP_DEBUG_NOTICE,
392                                 "meta_back_op_result: target"
393                                 " <%d> sending msg \"%s\""
394                                 " (matched \"%s\")\n",
395                                 i, ( msg ? msg : "" ),
396                                 ( match ? match : "" ) ));
397 #else /* !NEW_LOGGING */
398                 Debug(LDAP_DEBUG_ANY,
399                                 "==> meta_back_op_result: target"
400                                 " <%d> sending msg \"%s\""
401                                 " (matched \"%s\")\n", 
402                                 i, ( msg ? msg : "" ),
403                                 ( match ? match : "" ) );
404 #endif /* !NEW_LOGGING */
405
406                 /* better test the pointers before freeing? */
407                 if ( match ) {
408                         free( match );
409                 }
410                 if ( msg ) {
411                         free( msg );
412                 }
413         }
414
415         return ( rerr == LDAP_SUCCESS ) ? 0 : -1;
416 }
417