Back to index

openldap  2.4.31
init.c
Go to the documentation of this file.
00001 /* init.c - initialize relay backend */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2004-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2004 Pierangelo Masarati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Pierangelo Masarati for inclusion
00019  * in OpenLDAP Software.
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #include <stdio.h>
00025 #include <ac/string.h>
00026 
00027 #include "slap.h"
00028 #include "config.h"
00029 #include "back-relay.h"
00030 
00031 static ConfigDriver relay_back_cf;
00032 
00033 static ConfigTable relaycfg[] = {
00034        { "relay", "relay", 2, 2, 0,
00035               ARG_MAGIC|ARG_DN,
00036               relay_back_cf, "( OLcfgDbAt:5.1 "
00037                      "NAME 'olcRelay' "
00038                      "DESC 'Relay DN' "
00039                      "SYNTAX OMsDN "
00040                      "SINGLE-VALUE )",
00041               NULL, NULL },
00042        { NULL }
00043 };
00044 
00045 static ConfigOCs relayocs[] = {
00046        { "( OLcfgDbOc:5.1 "
00047               "NAME 'olcRelayConfig' "
00048               "DESC 'Relay backend configuration' "
00049               "SUP olcDatabaseConfig "
00050               "MAY ( olcRelay "
00051               ") )",
00052                      Cft_Database, relaycfg},
00053        { NULL, 0, NULL }
00054 };
00055 
00056 static int
00057 relay_back_cf( ConfigArgs *c )
00058 {
00059        relay_back_info      *ri = ( relay_back_info * )c->be->be_private;
00060        int           rc = 0;
00061 
00062        if ( c->op == SLAP_CONFIG_EMIT ) {
00063               if ( ri != NULL && !BER_BVISNULL( &ri->ri_realsuffix ) ) {
00064                      value_add_one( &c->rvalue_vals, &ri->ri_realsuffix );
00065                      return 0;
00066               }
00067               return 1;
00068 
00069        } else if ( c->op == LDAP_MOD_DELETE ) {
00070               if ( !BER_BVISNULL( &ri->ri_realsuffix ) ) {
00071                      ch_free( ri->ri_realsuffix.bv_val );
00072                      BER_BVZERO( &ri->ri_realsuffix );
00073                      ri->ri_bd = NULL;
00074                      return 0;
00075               }
00076               return 1;
00077 
00078        } else {
00079               BackendDB *bd;
00080 
00081               assert( ri != NULL );
00082               assert( BER_BVISNULL( &ri->ri_realsuffix ) );
00083 
00084               if ( c->be->be_nsuffix == NULL ) {
00085                      snprintf( c->cr_msg, sizeof( c->cr_msg),
00086                             "\"relay\" directive "
00087                             "must appear after \"suffix\"" );
00088                      Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00089                             "%s: %s.\n", c->log, c->cr_msg );
00090                      rc = 1;
00091                      goto relay_done;
00092               }
00093 
00094               if ( !BER_BVISNULL( &c->be->be_nsuffix[ 1 ] ) ) {
00095                      snprintf( c->cr_msg, sizeof( c->cr_msg),
00096                             "relaying of multiple suffix "
00097                             "database not supported" );
00098                      Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00099                             "%s: %s.\n", c->log, c->cr_msg );
00100                      rc = 1;
00101                      goto relay_done;
00102               }
00103 
00104               bd = select_backend( &c->value_ndn, 1 );
00105               if ( bd == NULL ) {
00106                      snprintf( c->cr_msg, sizeof( c->cr_msg),
00107                             "cannot find database "
00108                             "of relay dn \"%s\" "
00109                             "in \"olcRelay <dn>\"\n",
00110                             c->value_dn.bv_val );
00111                      Log2( LDAP_DEBUG_CONFIG, LDAP_LEVEL_ERR,
00112                             "%s: %s.\n", c->log, c->cr_msg );
00113 
00114               } else if ( bd->be_private == c->be->be_private ) {
00115                      snprintf( c->cr_msg, sizeof( c->cr_msg),
00116                             "relay dn \"%s\" would call self "
00117                             "in \"relay <dn>\" line\n",
00118                             c->value_dn.bv_val );
00119                      Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00120                             "%s: %s.\n", c->log, c->cr_msg );
00121                      rc = 1;
00122                      goto relay_done;
00123               }
00124 
00125               ri->ri_realsuffix = c->value_ndn;
00126               BER_BVZERO( &c->value_ndn );
00127 
00128 relay_done:;
00129               ch_free( c->value_dn.bv_val );
00130               ch_free( c->value_ndn.bv_val );
00131        }
00132 
00133        return rc;
00134 }
00135 
00136 int
00137 relay_back_initialize( BackendInfo *bi )
00138 {
00139        bi->bi_init = 0;
00140        bi->bi_open = 0;
00141        bi->bi_config = 0;
00142        bi->bi_close = 0;
00143        bi->bi_destroy = 0;
00144 
00145        bi->bi_db_init = relay_back_db_init;
00146        bi->bi_db_config = config_generic_wrapper;
00147        bi->bi_db_open = relay_back_db_open;
00148 #if 0
00149        bi->bi_db_close = relay_back_db_close;
00150 #endif
00151        bi->bi_db_destroy = relay_back_db_destroy;
00152 
00153        bi->bi_op_bind = relay_back_op_bind;
00154        bi->bi_op_search = relay_back_op_search;
00155        bi->bi_op_compare = relay_back_op_compare;
00156        bi->bi_op_modify = relay_back_op_modify;
00157        bi->bi_op_modrdn = relay_back_op_modrdn;
00158        bi->bi_op_add = relay_back_op_add;
00159        bi->bi_op_delete = relay_back_op_delete;
00160        bi->bi_extended = relay_back_op_extended;
00161        bi->bi_entry_release_rw = relay_back_entry_release_rw;
00162        bi->bi_entry_get_rw = relay_back_entry_get_rw;
00163        bi->bi_operational = relay_back_operational;
00164        bi->bi_has_subordinates = relay_back_has_subordinates;
00165 
00166        bi->bi_cf_ocs = relayocs;
00167 
00168        return config_register_schema( relaycfg, relayocs );
00169 }
00170 
00171 int
00172 relay_back_db_init( Backend *be, ConfigReply *cr)
00173 {
00174        relay_back_info             *ri;
00175 
00176        be->be_private = NULL;
00177 
00178        ri = (relay_back_info *) ch_calloc( 1, RELAY_INFO_SIZE );
00179        if ( ri == NULL ) {
00180               return -1;
00181        }
00182 
00183        ri->ri_bd = NULL;
00184        BER_BVZERO( &ri->ri_realsuffix );
00185        ri->ri_massage = 0;
00186 
00187        be->be_cf_ocs = be->bd_info->bi_cf_ocs;
00188 
00189        be->be_private = (void *)ri;
00190 
00191        return 0;
00192 }
00193 
00194 int
00195 relay_back_db_open( Backend *be, ConfigReply *cr )
00196 {
00197        relay_back_info             *ri = (relay_back_info *)be->be_private;
00198 
00199        assert( ri != NULL );
00200 
00201        if ( !BER_BVISNULL( &ri->ri_realsuffix ) ) {
00202               ri->ri_bd = select_backend( &ri->ri_realsuffix, 1 );
00203 
00204               /* must be there: it was during config! */
00205               if ( ri->ri_bd == NULL ) {
00206                      snprintf( cr->msg, sizeof( cr->msg),
00207                             "cannot find database "
00208                             "of relay dn \"%s\" "
00209                             "in \"olcRelay <dn>\"\n",
00210                             ri->ri_realsuffix.bv_val );
00211                      Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00212                             "relay_back_db_open: %s.\n", cr->msg );
00213 
00214                      return 1;
00215               }
00216 
00217               /* inherit controls */
00218               AC_MEMCPY( be->bd_self->be_ctrls, ri->ri_bd->be_ctrls, sizeof( be->be_ctrls ) );
00219 
00220        } else {
00221               /* inherit all? */
00222               AC_MEMCPY( be->bd_self->be_ctrls, frontendDB->be_ctrls, sizeof( be->be_ctrls ) );
00223        }
00224 
00225        return 0;
00226 }
00227 
00228 int
00229 relay_back_db_close( Backend *be, ConfigReply *cr )
00230 {
00231        return 0;
00232 }
00233 
00234 int
00235 relay_back_db_destroy( Backend *be, ConfigReply *cr)
00236 {
00237        relay_back_info             *ri = (relay_back_info *)be->be_private;
00238 
00239        if ( ri ) {
00240               if ( !BER_BVISNULL( &ri->ri_realsuffix ) ) {
00241                      ch_free( ri->ri_realsuffix.bv_val );
00242               }
00243               ch_free( ri );
00244        }
00245 
00246        return 0;
00247 }
00248 
00249 #if SLAPD_RELAY == SLAPD_MOD_DYNAMIC
00250 
00251 /* conditionally define the init_module() function */
00252 SLAP_BACKEND_INIT_MODULE( relay )
00253 
00254 #endif /* SLAPD_RELAY == SLAPD_MOD_DYNAMIC */