]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
More system schema checks
[openldap] / servers / slapd / back-meta / bind.c
1 /*
2  * Copyright 1998-2002 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                 struct berval   *dn,
86                 struct berval   *ndn,
87                 int             method,
88                 struct berval   *cred,
89                 struct berval   *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         struct berval *realdn = dn;
100         struct berval *realndn = ndn;
101         struct berval *realcred = cred;
102         int realmethod = method;
103
104 #ifdef NEW_LOGGING
105         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
106                         "meta_back_bind: dn: %s.\n", dn->bv_val ));
107 #else /* !NEW_LOGGING */
108         Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s", dn->bv_val, "", "" );
109 #endif /* !NEW_LOGGING */
110
111         if ( method == LDAP_AUTH_SIMPLE 
112                         && be_isroot_pw( be, conn, ndn, cred ) ) {
113                 isroot = 1;
114                 ber_dupbv( edn, be_root_dn( be ) );
115                 op_type = META_OP_REQUIRE_ALL;
116         }
117         lc = meta_back_getconn( li, conn, op, op_type, ndn, NULL );
118         if ( !lc ) {
119 #ifdef NEW_LOGGING
120                 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
121                                 "meta_back_bind: no target for dn %s.\n",
122                                 dn->bv_val ));
123 #else /* !NEW_LOGGING */
124                 Debug( LDAP_DEBUG_ANY,
125                                 "meta_back_bind: no target for dn %s.\n%s%s",
126                                 dn->bv_val, "", "");
127 #endif /* !NEW_LOGGING */
128                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, 
129                                 NULL, NULL, NULL, NULL );
130                 return -1;
131         }
132
133         /*
134          * Each target is scanned ...
135          */
136         lc->bound_target = META_BOUND_NONE;
137         ndnlen = ndn->bv_len;
138         for ( i = 0; i < li->ntargets; i++ ) {
139                 int lerr;
140
141                 /*
142                  * Skip non-candidates
143                  */
144                 if ( lc->conns[ i ]->candidate != META_CANDIDATE ) {
145                         continue;
146                 }
147
148                 if ( gotit == 0 ) {
149                         gotit = 1;
150                 } else {
151                         /*
152                          * A bind operation is expected to have
153                          * ONE CANDIDATE ONLY!
154                          */
155 #ifdef NEW_LOGGING
156                         LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
157                                         "==>meta_back_bind: more than one"
158                                         " candidate is attempting to bind"
159                                         " ...\n" ));
160 #else /* !NEW_LOGGING */
161                         Debug( LDAP_DEBUG_ANY,
162                                         "==>meta_back_bind: more than one"
163                                         " candidate is attempting to bind"
164                                         " ...\n%s%s%s", 
165                                         "", "", "" );
166 #endif /* !NEW_LOGGING */
167                 }
168
169                 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
170                         realdn = &li->targets[ i ]->pseudorootdn;
171                         realndn = &li->targets[ i ]->pseudorootdn;
172                         realcred = &li->targets[ i ]->pseudorootpw;
173                         realmethod = LDAP_AUTH_SIMPLE;
174                 } else {
175                         realdn = dn;
176                         realndn = ndn;
177                         realcred = cred;
178                         realmethod = method;
179                 }
180                 
181                 lerr = meta_back_do_single_bind( li, lc,
182                                 realdn, realndn, realcred, realmethod, i );
183                 if ( lerr != LDAP_SUCCESS ) {
184                         err = lerr;
185                         ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
186                 } else {
187                         rc = LDAP_SUCCESS;
188                 }
189         }
190
191         if ( isroot ) {
192                 lc->bound_target = META_BOUND_ALL;
193         }
194
195         /*
196          * rc is LDAP_SUCCESS if at least one bind succeeded,
197          * err is the last error that occurred during a bind;
198          * if at least (and at most?) one bind succeedes, fine.
199          */
200         if ( rc != LDAP_SUCCESS /* && err != LDAP_SUCCESS */ ) {
201                 
202                 /*
203                  * deal with bind failure ...
204                  */
205
206                 /*
207                  * no target was found within the naming context, 
208                  * so bind must fail with invalid credentials
209                  */
210                 if ( err == LDAP_SUCCESS && gotit == 0 ) {
211                         err = LDAP_INVALID_CREDENTIALS;
212                 }
213
214                 err = ldap_back_map_result( err );
215                 send_ldap_result( conn, op, err, NULL, NULL, NULL, NULL );
216                 return -1;
217         }
218
219         return 0;
220 }
221
222 /*
223  * meta_back_do_single_bind
224  *
225  * attempts to perform a bind with creds
226  */
227 int
228 meta_back_do_single_bind(
229                 struct metainfo         *li,
230                 struct metaconn         *lc,
231                 struct berval           *dn,
232                 struct berval           *ndn,
233                 struct berval           *cred,
234                 int                     method,
235                 int                     candidate
236 )
237 {
238         struct berval mdn = { 0, NULL };
239         int rc;
240         
241         /*
242          * Rewrite the bind dn if needed
243          */
244         switch ( rewrite_session( li->targets[ candidate ]->rwinfo,
245                                 "bindDn", dn->bv_val, lc->conn, &mdn.bv_val ) ) {
246         case REWRITE_REGEXEC_OK:
247                 if ( mdn.bv_val == NULL ) {
248                         mdn = *dn;
249                 }
250 #ifdef NEW_LOGGING
251                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
252                                 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn.bv_val ));
253 #else /* !NEW_LOGGING */
254                 Debug( LDAP_DEBUG_ARGS,
255                                 "rw> bindDn: \"%s\" -> \"%s\"\n%s",
256                                 dn->bv_val, mdn.bv_val, "" );
257 #endif /* !NEW_LOGGING */
258                 break;
259                 
260         case REWRITE_REGEXEC_UNWILLING:
261                 return LDAP_UNWILLING_TO_PERFORM;
262
263         case REWRITE_REGEXEC_ERR:
264                 return LDAP_OPERATIONS_ERROR;
265         }
266
267         rc = ldap_bind_s( lc->conns[ candidate ]->ld, mdn.bv_val, cred->bv_val, method );
268         if ( rc != LDAP_SUCCESS ) {
269                 rc = ldap_back_map_result( rc );
270         } else {
271                 ber_dupbv( &lc->conns[ candidate ]->bound_dn, dn );
272                 lc->conns[ candidate ]->bound = META_BOUND;
273                 lc->bound_target = candidate;
274
275                 if ( li->cache.ttl != META_DNCACHE_DISABLED
276                                 && ndn->bv_len != 0 ) {
277                         ( void )meta_dncache_update_entry( &li->cache,
278                                         ndn, candidate );
279                 }
280         }
281         
282         if ( mdn.bv_val != dn->bv_val ) {
283                 free( mdn.bv_val );
284         }
285
286         return rc;
287 }
288
289 /*
290  * meta_back_dobind
291  */
292 int
293 meta_back_dobind( struct metaconn *lc, Operation *op )
294 {
295         struct metasingleconn **lsc;
296         int bound = 0, i;
297
298         /*
299          * all the targets are bound as pseudoroot
300          */
301         if ( lc->bound_target == META_BOUND_ALL ) {
302                 return 1;
303         }
304
305         for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
306                 int rc;
307
308                 /*
309                  * Not a candidate or something wrong with this target ...
310                  */
311                 if ( lsc[ 0 ]->ld == NULL ) {
312                         continue;
313                 }
314
315                 /*
316                  * If the target is already bound it is skipped
317                  */
318                 if ( lsc[ 0 ]->bound == META_BOUND && lc->bound_target == i ) {
319                         ++bound;
320                         continue;
321                 }
322
323                 /*
324                  * Otherwise an anonymous bind is performed
325                  * (note: if the target was already bound, the anonymous
326                  * bind clears the previous bind).
327                  */
328                 if ( lsc[ 0 ]->bound_dn.bv_val ) {
329                         ch_free( lsc[ 0 ]->bound_dn.bv_val );
330                         lsc[ 0 ]->bound_dn.bv_val = NULL;
331                         lsc[ 0 ]->bound_dn.bv_len = 0;
332                 }
333                 rc = ldap_bind_s( lsc[ 0 ]->ld, 0, NULL, LDAP_AUTH_SIMPLE );
334                 if ( rc != LDAP_SUCCESS ) {
335                         
336 #ifdef NEW_LOGGING
337                         LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
338                                         "meta_back_dobind: (anonymous)"
339                                         " bind as \"%s\" failed"
340                                         " with error \"%s\"\n",
341                                         lsc[ 0 ]->bound_dn.bv_val,
342                                         ldap_err2string( rc ) ));
343 #else /* !NEW_LOGGING */
344                         Debug( LDAP_DEBUG_ANY,
345                                         "==>meta_back_dobind: (anonymous)"
346                                         " bind as \"%s\" failed"
347                                         " with error \"%s\"\n%s",
348                                         lsc[ 0 ]->bound_dn.bv_val,
349                                         ldap_err2string( rc ), "" );
350 #endif /* !NEW_LOGGING */
351
352                         /*
353                          * null cred bind should always succeed
354                          * as anonymous, so a failure means
355                          * the target is no longer candidate possibly
356                          * due to technical reasons (remote host down?)
357                          * so better clear the handle
358                          */
359                         ( void )meta_clear_one_candidate( lsc[ 0 ], 1 );
360                         continue;
361                 } /* else */
362                 
363                 lsc[ 0 ]->bound = META_ANONYMOUS;
364                 ++bound;
365         }
366
367         return( bound > 0 );
368 }
369
370 /*
371  *
372  */
373 int
374 meta_back_is_valid( struct metaconn *lc, int candidate )
375 {
376         struct metasingleconn   **lsc;
377         int                     i;
378
379         assert( lc );
380
381         if ( candidate < 0 ) {
382                 return 0;
383         }
384
385         for ( i = 0, lsc = lc->conns; 
386                         lsc[ 0 ] != NULL && i < candidate; 
387                         ++i, ++lsc );
388         
389         if ( lsc[ 0 ] ) {
390                 return( lsc[ 0 ]->ld != NULL );
391         }
392
393         return 0;
394 }
395
396 /*
397  * FIXME: error return must be handled in a cleaner way ...
398  */
399 int
400 meta_back_op_result( struct metaconn *lc, Operation *op )
401 {
402         int i, rerr = LDAP_SUCCESS;
403         struct metasingleconn **lsc;
404         char *rmsg = NULL;
405         char *rmatch = NULL;
406
407         for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
408                 int err = LDAP_SUCCESS;
409                 char *msg = NULL;
410                 char *match = NULL;
411
412                 ldap_get_option( lsc[ 0 ]->ld, LDAP_OPT_ERROR_NUMBER, &err );
413                 if ( err != LDAP_SUCCESS ) {
414                         /*
415                          * better check the type of error. In some cases
416                          * (search ?) it might be better to return a
417                          * success if at least one of the targets gave
418                          * positive result ...
419                          */
420                         ldap_get_option( lsc[ 0 ]->ld,
421                                         LDAP_OPT_ERROR_STRING, &msg );
422                         ldap_get_option( lsc[ 0 ]->ld,
423                                         LDAP_OPT_MATCHED_DN, &match );
424                         err = ldap_back_map_result( err );
425
426 #ifdef NEW_LOGGING
427                         LDAP_LOG(( "backend", LDAP_DEBUG_NOTICE,
428                                         "meta_back_op_result: target"
429                                         " <%d> sending msg \"%s\""
430                                         " (matched \"%s\")\n",
431                                         i, ( msg ? msg : "" ),
432                                         ( match ? match : "" ) ));
433 #else /* !NEW_LOGGING */
434                         Debug(LDAP_DEBUG_ANY,
435                                         "==> meta_back_op_result: target"
436                                         " <%d> sending msg \"%s\""
437                                         " (matched \"%s\")\n", 
438                                         i, ( msg ? msg : "" ),
439                                         ( match ? match : "" ) );
440 #endif /* !NEW_LOGGING */
441
442                         /*
443                          * FIXME: need to rewrite "match" (need rwinfo)
444                          */
445                         switch ( err ) {
446                         default:
447                                 rerr = err;
448                                 rmsg = msg;
449                                 msg = NULL;
450                                 rmatch = match;
451                                 match = NULL;
452                                 break;
453                         }
454
455                         /* better test the pointers before freeing? */
456                         if ( match ) {
457                                 free( match );
458                         }
459                         if ( msg ) {
460                                 free( msg );
461                         }
462                 }
463         }
464
465         send_ldap_result( lc->conn, op, rerr, rmatch, rmsg, NULL, NULL );
466
467         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
468 }
469