]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/chain.c
fix compilation when the chaining control is undefined
[openldap] / servers / slapd / back-ldap / chain.c
index 60cb2ad4279ba49eeec0ea1866fb39f754d78812..bf8353c00da26bebde2471622b671162b9c902a2 100644 (file)
@@ -101,6 +101,7 @@ typedef struct ldap_chain_t {
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
 } ldap_chain_t;
 
+static int ldap_chain_db_init_common( BackendDB        *be );
 static int ldap_chain_db_init_one( BackendDB *be );
 #define        ldap_chain_db_open_one(be)      (lback)->bi_db_open( (be) )
 #define        ldap_chain_db_close_one(be)     (0)
@@ -359,7 +360,7 @@ ldap_chain_op(
 {
        slap_overinst   *on = (slap_overinst *) op->o_bd->bd_info;
        ldap_chain_t    *lc = (ldap_chain_t *)on->on_bi.bi_private;
-       ldapinfo_t      li = *lc->lc_common_li, *lip = NULL;
+       ldapinfo_t      li = { 0 }, *lip = NULL;
        struct berval   bvuri[ 2 ] = { { 0 } };
 
        /* NOTE: returned if ref is empty... */
@@ -497,9 +498,9 @@ ldap_chain_response( Operation *op, SlapReply *rs )
        BerVarray       ref;
        struct berval   ndn = op->o_ndn;
 
-#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
        int             sr_err = rs->sr_err;
        slap_reply_t    sr_type = rs->sr_type;
+#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
        slap_mask_t     chain_mask = 0;
        ber_len_t       chain_shift = 0;
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
@@ -602,7 +603,7 @@ ldap_chain_response( Operation *op, SlapReply *rs )
        case LDAP_REQ_SEARCH:
                if ( rs->sr_type == REP_SEARCHREF ) {
                        ldap_chain_t    *lc = (ldap_chain_t *)on->on_bi.bi_private;
-                       ldapinfo_t      li = *lc->lc_common_li, *lip = NULL;
+                       ldapinfo_t      li = { 0 }, *lip = NULL;
                        struct berval   bvuri[ 2 ] = { { 0 } };
 
                        struct berval   *curr = ref,
@@ -812,9 +813,7 @@ cannot_chain:;
                send_ldap_result( op, rs );
        }
 
-#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
 dont_chain:;
-#endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
        rs->sr_err = sr_err;
        rs->sr_type = sr_type;
        rs->sr_matched = matched;
@@ -864,9 +863,7 @@ enum {
        CH_LAST
 };
 
-#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
 static ConfigDriver chain_cf_gen;
-#endif
 static ConfigCfAdd chain_cfadd;
 static ConfigLDAPadd chain_ldadd;
 
@@ -887,16 +884,17 @@ static ConfigTable chaincfg[] = {
 };
 
 static ConfigOCs chainocs[] = {
-#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
        { "( OLcfgOvOc:3.1 "
                "NAME 'olcChainConfig' "
                "DESC 'Chain configuration' "
                "SUP olcOverlayConfig "
-               "MAY ( olcChainingBehavior "
+               "MAY ( "
+#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
+                       "olcChainingBehavior "
+#endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
                        "$ olcCacheURIs "
                        ") )",
                Cft_Overlay, chaincfg, NULL, chain_cfadd },
-#endif
        { "( OLcfgOvOc:3.2 "
                "NAME 'olcChainDatabase' "
                "DESC 'Chain remote server configuration' "
@@ -908,6 +906,17 @@ static ConfigOCs chainocs[] = {
 static int
 chain_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
 {
+       slap_overinst           *on;
+       ldap_chain_t            *lc;
+
+       ldapinfo_t              *li;
+
+       AttributeDescription    *ad = NULL;
+       Attribute               *at;
+       const char              *text;
+
+       int                     rc;
+
        if ( p->ce_type != Cft_Overlay
                || !p->ce_bi
                || p->ce_bi->bi_cf_ocs != chainocs )
@@ -915,7 +924,76 @@ chain_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
                return LDAP_CONSTRAINT_VIOLATION;
        }
 
-       return LDAP_SUCCESS;
+       on = (slap_overinst *)p->ce_bi;
+       lc = (ldap_chain_t *)on->on_bi.bi_private;
+
+       assert( ca->be == NULL );
+       ca->be = (BackendDB *)ch_calloc( 1, sizeof( BackendDB ) );
+
+       ca->be->bd_info = (BackendInfo *)on;
+
+       rc = slap_str2ad( "olcDbURI", &ad, &text );
+       assert( rc == LDAP_SUCCESS );
+
+       at = attr_find( e->e_attrs, ad );
+       if ( lc->lc_common_li == NULL && at != NULL ) {
+               /* FIXME: we should generate an empty default entry
+                * if none is supplied */
+               Debug( LDAP_DEBUG_ANY, "slapd-chain: "
+                       "first underlying database \"%s\" "
+                       "cannot contain attribute \"%s\".\n",
+                       e->e_name.bv_val, ad->ad_cname.bv_val, 0 );
+               rc = LDAP_CONSTRAINT_VIOLATION;
+               goto done;
+
+       } else if ( lc->lc_common_li != NULL && at == NULL ) {
+               /* FIXME: we should generate an empty default entry
+                * if none is supplied */
+               Debug( LDAP_DEBUG_ANY, "slapd-chain: "
+                       "subsequent underlying database \"%s\" "
+                       "must contain attribute \"%s\".\n",
+                       e->e_name.bv_val, ad->ad_cname.bv_val, 0 );
+               rc = LDAP_CONSTRAINT_VIOLATION;
+               goto done;
+       }
+
+       if ( lc->lc_common_li == NULL ) {
+               rc = ldap_chain_db_init_common( ca->be );
+
+       } else {
+               rc = ldap_chain_db_init_one( ca->be );
+       }
+
+       if ( rc != 0 ) {
+               Debug( LDAP_DEBUG_ANY, "slapd-chain: "
+                       "unable to init %sunderlying database \"%s\".\n",
+                       lc->lc_common_li == NULL ? "common " : "", e->e_name.bv_val, 0 );
+               return LDAP_CONSTRAINT_VIOLATION;
+       }
+
+       li = ca->be->be_private;
+
+       if ( lc->lc_common_li == NULL ) {
+               lc->lc_common_li = li;
+
+       } else if ( avl_insert( &lc->lc_lai.lai_tree, (caddr_t)li,
+               ldap_chain_uri_cmp, ldap_chain_uri_dup ) )
+       {
+               Debug( LDAP_DEBUG_ANY, "slapd-chain: "
+                       "database \"%s\" insert failed.\n",
+                       e->e_name.bv_val, 0, 0 );
+               rc = LDAP_CONSTRAINT_VIOLATION;
+               goto done;
+       }
+
+done:;
+       if ( rc != LDAP_SUCCESS ) {
+               (void)ldap_chain_db_destroy_one( ca->be );
+               ch_free( ca->be );
+               ca->be = NULL;
+       }
+
+       return rc;
 }
 
 typedef struct ldap_chain_cfadd_apply_t {
@@ -963,7 +1041,7 @@ chain_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
                lca.rs = rs;
                lca.p = p;
                lca.ca = ca;
-               lca.count = -1;
+               lca.count = 0;
 
                (void)ldap_chain_cfadd_apply( (void *)lc->lc_common_li, (void *)&lca );
 
@@ -977,7 +1055,6 @@ chain_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
 }
 
 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
-
 static slap_verbmasks chaining_mode[] = {
        { BER_BVC("referralsRequired"),         LDAP_REFERRALS_REQUIRED },
        { BER_BVC("referralsPreferred"),        LDAP_REFERRALS_PREFERRED },
@@ -985,14 +1062,13 @@ static slap_verbmasks chaining_mode[] = {
        { BER_BVC("chainingPreferred"),         LDAP_CHAINING_PREFERRED },
        { BER_BVNULL,                           0 }
 };
+#endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
 
 static int
 chain_cf_gen( ConfigArgs *c )
 {
        slap_overinst   *on = (slap_overinst *)c->bi;
-#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
        ldap_chain_t    *lc = (ldap_chain_t *)on->on_bi.bi_private;
-#endif
 
        int             rc = 0;
 
@@ -1056,8 +1132,8 @@ chain_cf_gen( ConfigArgs *c )
        }
 
        switch( c->type ) {
-#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
        case CH_CHAINING: {
+#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
                char                    **argv = c->argv;
                int                     argc = c->argc;
                BerElementBuffer        berbuf;
@@ -1074,20 +1150,20 @@ chain_cf_gen( ConfigArgs *c )
                        if ( strncasecmp( argv[ 0 ], "resolve=", STRLENOF( "resolve=" ) ) == 0 ) {
                                resolve = str2chain( argv[ 0 ] + STRLENOF( "resolve=" ) );
                                if ( resolve == -1 ) {
-                                       fprintf( stderr, "%s line %d: "
+                                       Debug( LDAP_DEBUG_ANY, "%s: "
                                                "illegal <resolve> value %s "
-                                               "in \"chain-chaining>\"\n",
-                                               c->fname, c->lineno, argv[ 0 ] );
+                                               "in \"chain-chaining>\".\n",
+                                               c->log, argv[ 0 ], 0 );
                                        return 1;
                                }
 
                        } else if ( strncasecmp( argv[ 0 ], "continuation=", STRLENOF( "continuation=" ) ) == 0 ) {
                                continuation = str2chain( argv[ 0 ] + STRLENOF( "continuation=" ) );
                                if ( continuation == -1 ) {
-                                       fprintf( stderr, "%s line %d: "
+                                       Debug( LDAP_DEBUG_ANY, "%s: "
                                                "illegal <continuation> value %s "
-                                               "in \"chain-chaining\"\n",
-                                               c->fname, c->lineno, argv[ 0 ] );
+                                               "in \"chain-chaining\".\n",
+                                               c->log, argv[ 0 ], 0 );
                                        return 1;
                                }
 
@@ -1095,9 +1171,9 @@ chain_cf_gen( ConfigArgs *c )
                                iscritical = 1;
 
                        } else {
-                               fprintf( stderr, "%s line %d: "
-                                       "unknown option in \"chain-chaining\"\n",
-                                       c->fname, c->lineno );
+                               Debug( LDAP_DEBUG_ANY, "%s: "
+                                       "unknown option in \"chain-chaining\".\n",
+                                       c->log, 0, 0 );
                                return 1;
                        }
                }
@@ -1115,9 +1191,9 @@ chain_cf_gen( ConfigArgs *c )
                        err = ber_printf( ber, "{e" /* } */, resolve );
                        if ( err == -1 ) {
                                ber_free( ber, 1 );
-                               fprintf( stderr, "%s line %d: "
+                               Debug( LDAP_DEBUG_ANY, "%s: "
                                        "chaining behavior control encoding error!\n",
-                                       c->fname, c->lineno );
+                                       c->log, 0, 0 );
                                return 1;
                        }
 
@@ -1125,9 +1201,9 @@ chain_cf_gen( ConfigArgs *c )
                                err = ber_printf( ber, "e", continuation );
                                if ( err == -1 ) {
                                        ber_free( ber, 1 );
-                                       fprintf( stderr, "%s line %d: "
+                                       Debug( LDAP_DEBUG_ANY, "%s: "
                                                "chaining behavior control encoding error!\n",
-                                               c->fname, c->lineno );
+                                               c->log, 0, 0 );
                                        return 1;
                                }
                        }
@@ -1135,9 +1211,9 @@ chain_cf_gen( ConfigArgs *c )
                        err = ber_printf( ber, /* { */ "N}" );
                        if ( err == -1 ) {
                                ber_free( ber, 1 );
-                               fprintf( stderr, "%s line %d: "
+                               Debug( LDAP_DEBUG_ANY, "%s: "
                                        "chaining behavior control encoding error!\n",
-                                       c->fname, c->lineno );
+                                       c->log, 0, 0 );
                                return 1;
                        }
 
@@ -1154,10 +1230,9 @@ chain_cf_gen( ConfigArgs *c )
 
                if ( ldap_chain_parse_ctrl( &op, &rs, &lc->lc_chaining_ctrl ) != LDAP_SUCCESS )
                {
-                       fprintf( stderr, "%s line %d: "
-                               "unable to parse chaining control%s%s\n",
-                               c->fname, c->lineno,
-                               rs.sr_text ? ": " : "",
+                       Debug( LDAP_DEBUG_ANY, "%s: "
+                               "unable to parse chaining control%s%s.\n",
+                               c->log, rs.sr_text ? ": " : "",
                                rs.sr_text ? rs.sr_text : "" );
                        return 1;
                }
@@ -1167,10 +1242,12 @@ chain_cf_gen( ConfigArgs *c )
                lc->lc_flags |= LDAP_CHAIN_F_CHAINING;
 
                rc = 0;
-
-               break;
-       }
+#else /* ! LDAP_CONTROL_X_CHAINING_BEHAVIOR */
+               Debug( LDAP_DEBUG_ANY, "%s: "
+                       "\"chaining\" control unsupported (ignored).\n",
+                       c->log, 0, 0 );
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
+               } break;
 
        case CH_CACHE_URI:
                if ( c->value_int ) {
@@ -1187,34 +1264,30 @@ chain_cf_gen( ConfigArgs *c )
        return rc;
 }
 
-#endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
-
 static int
 ldap_chain_db_init(
        BackendDB *be )
 {
        slap_overinst   *on = (slap_overinst *)be->bd_info;
        ldap_chain_t    *lc = NULL;
-       int             rc;
-       BackendDB       bd = *be;
 
        if ( lback == NULL ) {
                lback = backend_info( "ldap" );
 
                if ( lback == NULL ) {
-                       return -1;
+                       return 1;
                }
        }
 
        lc = ch_malloc( sizeof( ldap_chain_t ) );
+       if ( lc == NULL ) {
+               return 1;
+       }
        memset( lc, 0, sizeof( ldap_chain_t ) );
 
-       bd.be_private = NULL;
-       rc = lback->bi_db_init( &bd );
-       lc->lc_cfg_li = lc->lc_common_li = (ldapinfo_t *)bd.be_private;
        on->on_bi.bi_private = (void *)lc;
 
-       return rc;
+       return 0;
 }
 
 static int
@@ -1230,7 +1303,14 @@ ldap_chain_db_config(
 
        int             rc = SLAP_CONF_UNKNOWN;
                
-       /* Something for the cache database? */
+       if ( lc->lc_common_li == NULL ) {
+               void    *be_private = be->be_private;
+               ldap_chain_db_init_common( be );
+               lc->lc_common_li = lc->lc_cfg_li = (ldapinfo_t *)be->be_private;
+               be->be_private = be_private;
+       }
+
+       /* Something for the chain database? */
        if ( strncasecmp( argv[ 0 ], "chain-", STRLENOF( "chain-" ) ) == 0 ) {
                char            *save_argv0 = argv[ 0 ];
                BackendInfo     *bd_info = bd_info;
@@ -1240,13 +1320,11 @@ ldap_chain_db_config(
 
                argv[ 0 ] += STRLENOF( "chain-" );
 
-               /* TODO: create a new structure and, after parsing the URI,
-                * put it in the lc->lc_lai tree */
                if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
                        rc = ldap_chain_db_init_one( be );
                        if ( rc != 0 ) {
                                Debug( LDAP_DEBUG_ANY, "%s: line %d: "
-                                       "underlying slapd-ldap initialization failed\n.",
+                                       "underlying slapd-ldap initialization failed.\n.",
                                        fname, lineno, 0 );
                                return 1;
                        }
@@ -1254,6 +1332,13 @@ ldap_chain_db_config(
                        is_uri = 1;
                }
 
+               /* TODO: add checks on what other slapd-ldap(5) args
+                * should be put in the template; this is not quite
+                * harmful, because attributes that shouldn't don't
+                * get actually used, but the user should at least
+                * be warned.
+                */
+
                be->bd_info = lback;
                be->be_private = (void *)lc->lc_cfg_li;
                be->be_cf_ocs = lback->bi_cf_ocs;
@@ -1342,7 +1427,7 @@ ldap_chain_db_func(
        if ( lc ) {
                BI_db_func      *func = (&lback->bi_db_open)[ which ];
 
-               if ( func != NULL ) {
+               if ( func != NULL && lc->lc_common_li != NULL ) {
                        BackendDB               db = *be;
 
                        db.bd_info = lback;
@@ -1383,9 +1468,6 @@ ldap_chain_db_open(
        }
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
 
-       /* FIXME: right now slapd-ldap has no open function;
-        * in case one is introduced, this needs be fixed */
-
        return ldap_chain_db_func( be, db_open );
 }
 
@@ -1415,6 +1497,36 @@ ldap_chain_db_destroy(
        return rc;
 }
 
+/*
+ * inits one instance of the slapd-ldap backend, and stores
+ * the private info in be_private of the arg
+ */
+static int
+ldap_chain_db_init_common(
+       BackendDB       *be )
+{
+       BackendInfo     *bi = be->bd_info;
+       int             t;
+
+       be->bd_info = lback;
+       be->be_private = NULL;
+       t = lback->bi_db_init( be );
+       if ( t != 0 ) {
+               return t;
+       }
+       be->bd_info = bi;
+
+       return 0;
+}
+
+/*
+ * inits one instance of the slapd-ldap backend, stores
+ * the private info in be_private of the arg and fills
+ * selected fields with data from the template.
+ *
+ * NOTE: add checks about the other fields of the template,
+ * which are ignored and SHOULD NOT be configured by the user.
+ */
 static int
 ldap_chain_db_init_one(
        BackendDB       *be )
@@ -1422,17 +1534,18 @@ ldap_chain_db_init_one(
        slap_overinst   *on = (slap_overinst *)be->bd_info;
        ldap_chain_t    *lc = (ldap_chain_t *)on->on_bi.bi_private;
 
-       BackendDB       db = *be;
-       int             t;
+       BackendInfo     *bi = be->bd_info;
        ldapinfo_t      *li;
 
-       db.bd_info = lback;
-       db.be_private = NULL;
-       t = lback->bi_db_init( &db );
+       int             t;
+
+       be->bd_info = lback;
+       be->be_private = NULL;
+       t = lback->bi_db_init( be );
        if ( t != 0 ) {
                return t;
        }
-       li = (ldapinfo_t *)db.be_private;
+       li = (ldapinfo_t *)be->be_private;
 
        /* copy common data */
        li->li_nretries = lc->lc_common_li->li_nretries;
@@ -1441,8 +1554,7 @@ ldap_chain_db_init_one(
        for ( t = 0; t < LDAP_BACK_OP_LAST; t++ ) {
                li->li_timeout[ t ] = lc->lc_common_li->li_timeout[ t ];
        }
-
-       be->be_private = li;
+       be->bd_info = bi;
 
        return 0;
 }
@@ -1631,7 +1743,7 @@ chain_init( void )
                        ldap_chain_parse_ctrl, &sc_chainingBehavior );
        if ( rc != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_ANY, "slapd-chain: "
-                       "unable to register chaining behavior control: %d\n",
+                       "unable to register chaining behavior control: %d.\n",
                        rc, 0, 0 );
                return rc;
        }