]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
minor cleanup; some error handling and log fixes
[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, err = LDAP_SUCCESS;
96
97 #ifdef NEW_LOGGING
98         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
99                         "meta_back_bind: dn: %s.\n", dn ));
100 #else /* !NEW_LOGGING */
101         Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s", dn, "", "" );
102 #endif /* !NEW_LOGGING */
103
104         *edn = NULL;
105
106         lc = meta_back_getconn( li, conn, op, META_OP_ALLOW_MULTIPLE,
107                         ndn, NULL );
108         if ( !lc ) {
109 #ifdef NEW_LOGGING
110                 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
111                                 "meta_back_bind: no target for dn %s.\n",
112                                 dn ));
113 #else /* !NEW_LOGGING */
114                 Debug( LDAP_DEBUG_ANY,
115                                 "meta_back_bind: no target for dn %s.\n%s%s",
116                                 dn, "", "");
117 #endif /* !NEW_LOGGING */
118                 return -1;
119         }
120
121         /*
122          * Each target is scanned ...
123          */
124         lc->bound_target = -1;
125         ndnlen = strlen( ndn );
126         for ( i = 0; i < li->ntargets; i++ ) {
127                 int lerr;
128
129                 /*
130                  * Skip non-candidates
131                  */
132                 if ( lc->conns[ i ]->candidate != META_CANDIDATE ) {
133                         continue;
134                 }
135
136                 if ( gotit == 0 ) {
137                         gotit = 1;
138                 } else {
139                         /*
140                          * A bind operation is expected to have
141                          * ONE CANDIDATE ONLY!
142                          */
143 #ifdef NEW_LOGGING
144                         LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
145                                         "==>meta_back_bind: more than one"
146                                         " candidate is attempting to bind"
147                                         " ...\n" ));
148 #else /* !NEW_LOGGING */
149                         Debug( LDAP_DEBUG_ANY,
150                                         "==>meta_back_bind: more than one"
151                                         " candidate is attempting to bind"
152                                         " ...\n%s%s%s", 
153                                         "", "", "" );
154 #endif /* !NEW_LOGGING */
155                 }
156
157
158                 lerr = meta_back_do_single_bind( li, lc, dn, ndn, cred,
159                                 method, i );
160                 if ( lerr != LDAP_SUCCESS ) {
161                         err = lerr;
162                         ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
163                 } else {
164                         rc = LDAP_SUCCESS;
165                 }
166         }
167
168         /*
169          * rc is LDAP_SUCCESS if at least one bind succeeded,
170          * err is the last error that occurred during a bind;
171          * if at least (and at most?) one bind succeedes, fine.
172          */
173         if ( rc != LDAP_SUCCESS /* && err != LDAP_SUCCESS */ ) {
174                 
175                 /*
176                  * deal with bind failure ...
177                  */
178                 err = ldap_back_map_result( err );
179                 send_ldap_result( conn, op, err, NULL, "", NULL, NULL );
180         }
181
182         return 0;
183 }
184
185 /*
186  * meta_back_do_single_bind
187  *
188  * attempts to perform a bind with creds
189  */
190 int
191 meta_back_do_single_bind(
192                 struct metainfo         *li,
193                 struct metaconn         *lc,
194                 const char              *dn,
195                 const char              *ndn,
196                 struct berval           *cred,
197                 int                     method,
198                 int                     candidate
199 )
200 {
201         char *mdn = NULL;
202         int rc;
203         
204         /*
205          * Rewrite the bind dn if needed
206          */
207         switch ( rewrite_session( li->targets[ candidate ]->rwinfo,
208                                 "bindDn", dn, lc->conn, &mdn ) ) {
209         case REWRITE_REGEXEC_OK:
210                 if ( mdn == NULL ) {
211                         mdn = ( char * )dn;
212                 }
213 #ifdef NEW_LOGGING
214                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
215                                 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn, mdn ));
216 #else /* !NEW_LOGGING */
217                 Debug( LDAP_DEBUG_ARGS,
218                                 "rw> bindDn: \"%s\" -> \"%s\"\n%s",
219                                 dn, mdn, "" );
220 #endif /* !NEW_LOGGING */
221                 break;
222                 
223         case REWRITE_REGEXEC_UNWILLING:
224                 return LDAP_UNWILLING_TO_PERFORM;
225
226         case REWRITE_REGEXEC_ERR:
227                 return LDAP_OPERATIONS_ERROR;
228         }
229
230         rc = ldap_bind_s( lc->conns[ candidate ]->ld, mdn,
231                         cred->bv_val, method );
232         if ( rc != LDAP_SUCCESS ) {
233                 rc = ldap_back_map_result( rc );
234         } else {
235                 lc->conns[ candidate ]->bound_dn = ch_strdup( dn );
236                 lc->conns[ candidate ]->bound = META_BOUND;
237                 lc->bound_target = candidate;
238
239                 if ( li->cache.ttl != META_DNCACHE_DISABLED
240                                 && ndn[ 0 ] != '\0' ) {
241                         ( void )meta_dncache_update_entry( &li->cache,
242                                         ch_strdup( ndn ), candidate );
243                 }
244         }
245         
246         if ( mdn != dn ) {
247                 free( mdn );
248         }
249
250         return rc;
251 }
252
253 /*
254  * meta_back_dobind
255  */
256 int
257 meta_back_dobind( struct metaconn *lc, Operation *op )
258 {
259         struct metasingleconn **lsc;
260         int bound = 0, i;
261
262         for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
263                 int rc;
264
265                 /*
266                  * Not a candidate or something wrong with this target ...
267                  */
268                 if ( lsc[ 0 ]->ld == NULL ) {
269                         continue;
270                 }
271
272                 /*
273                  * If the target is already bound it is skipped
274                  */
275                 if ( lsc[ 0 ]->bound == META_BOUND && lc->bound_target == i ) {
276                         ++bound;
277                         continue;
278                 }
279
280                 /*
281                  * Otherwise an anonymous bind is performed
282                  * (note: if the target was already bound, the anonymous
283                  * bind clears the previous bind).
284                  */
285                 rc = ldap_bind_s( lsc[ 0 ]->ld, lsc[ 0 ]->bound_dn,
286                                 NULL, LDAP_AUTH_SIMPLE );
287                 if ( rc != LDAP_SUCCESS ) {
288                         
289 #ifdef NEW_LOGGING
290                         LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
291                                         "meta_back_dobind: (anonymous)"
292                                         " bind as \"%s\" failed"
293                                         " with error \"%s\"\n",
294                                         lsc[ 0 ]->bound_dn,
295                                         ldap_err2string( rc ) ));
296 #else /* !NEW_LOGGING */
297                         Debug( LDAP_DEBUG_ANY,
298                                         "==>meta_back_dobind: (anonymous)"
299                                         " bind as \"%s\" failed"
300                                         " with error \"%s\"\n%s",
301                                         lsc[ 0 ]->bound_dn,
302                                         ldap_err2string( rc ), "" );
303 #endif /* !NEW_LOGGING */
304
305                         /*
306                          * null cred bind should always succeed
307                          * as anonymous, so a failure means
308                          * the target is no longer candidate possibly
309                          * due to technical reasons (remote host down?)
310                          * so better clear the handle
311                          */
312                         ( void )meta_clear_one_candidate( lsc[ 0 ], 1 );
313                         continue;
314                 } /* else */
315                 
316                 lsc[ 0 ]->bound = META_ANONYMOUS;
317                 ++bound;
318         }
319
320         return( bound > 0 );
321 }
322
323 /*
324  * FIXME: error return must be handled in a cleaner way ...
325  */
326 int
327 meta_back_op_result( struct metaconn *lc, Operation *op )
328 {
329         int i, err = LDAP_SUCCESS;
330         char *msg = NULL;
331         char *match = NULL;
332         struct metasingleconn **lsc;
333
334         for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
335                 ldap_get_option( lsc[ 0 ]->ld, LDAP_OPT_ERROR_NUMBER, &err );
336                 if ( err != LDAP_SUCCESS ) {
337                         /*
338                          * better check the type of error. In some cases
339                          * (search ?) it might be better to return a
340                          * success if at least one of the targets gave
341                          * positive result ...
342                          */
343                         ldap_get_option( lsc[ 0 ]->ld,
344                                         LDAP_OPT_ERROR_STRING, &msg );
345                         ldap_get_option( lsc[ 0 ]->ld,
346                                         LDAP_OPT_MATCHED_DN, &match );
347                         err = ldap_back_map_result( err );
348
349                         /*
350                          * FIXME: need to rewrite "match" (need rwinfo)
351                          */
352                         send_ldap_result( lc->conn, op, err, match, msg,
353                                         NULL, NULL );
354                         
355 #ifdef NEW_LOGGING
356                         LDAP_LOG(( "backend", LDAP_DEBUG_NOTICE,
357                                         "meta_back_op_result: target"
358                                         " <%d> sending msg \"%s\""
359                                         " (matched \"%s\")\n",
360                                         i, ( msg ? msg : "" ),
361                                         ( match ? match : "" ) ));
362 #else /* !NEW_LOGGING */
363                         Debug(LDAP_DEBUG_ANY,
364                                         "==> meta_back_op_result: target"
365                                         " <%d> sending msg \"%s\""
366                                         " (matched \"%s\")\n", 
367                                         i, ( msg ? msg : "" ),
368                                         ( match ? match : "" ) );
369 #endif /* !NEW_LOGGING */
370
371                         /* better test the pointers before freeing? */
372                         if ( match ) {
373                                 free( match );
374                         }
375                         if ( msg ) {
376                                 free( msg );
377                         }
378                         return -1;
379                 }
380         }
381
382         return ( err == LDAP_SUCCESS ) ? 0 : -1;
383 }
384