| 1 |
/* init.c - initialize relay backend */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2004-2024 The OpenLDAP Foundation. |
| 6 |
* Portions Copyright 2004 Pierangelo Masarati. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted only as authorized by the OpenLDAP |
| 11 |
* Public License. |
| 12 |
* |
| 13 |
* A copy of this license is available in the file LICENSE in the |
| 14 |
* top-level directory of the distribution or, alternatively, at |
| 15 |
* <http://www.OpenLDAP.org/license.html>. |
| 16 |
*/ |
| 17 |
/* ACKNOWLEDGEMENTS: |
| 18 |
* This work was initially developed by Pierangelo Masarati for inclusion |
| 19 |
* in OpenLDAP Software. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "portable.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <ac/string.h> |
| 26 |
|
| 27 |
#include "slap.h" |
| 28 |
#include "slap-config.h" |
| 29 |
#include "back-relay.h" |
| 30 |
|
| 31 |
static ConfigDriver relay_back_cf; |
| 32 |
|
| 33 |
static ConfigTable relaycfg[] = { |
| 34 |
{ "relay", "relay", 2, 2, 0, |
| 35 |
ARG_MAGIC|ARG_DN|ARG_QUOTE, |
| 36 |
relay_back_cf, "( OLcfgDbAt:5.1 " |
| 37 |
"NAME 'olcRelay' " |
| 38 |
"DESC 'Relay DN' " |
| 39 |
"EQUALITY distinguishedNameMatch " |
| 40 |
"SYNTAX OMsDN " |
| 41 |
"SINGLE-VALUE )", |
| 42 |
NULL, NULL }, |
| 43 |
{ NULL } |
| 44 |
}; |
| 45 |
|
| 46 |
static ConfigOCs relayocs[] = { |
| 47 |
{ "( OLcfgDbOc:5.1 " |
| 48 |
"NAME 'olcRelayConfig' " |
| 49 |
"DESC 'Relay backend configuration' " |
| 50 |
"SUP olcDatabaseConfig " |
| 51 |
"MAY ( olcRelay " |
| 52 |
") )", |
| 53 |
Cft_Database, relaycfg}, |
| 54 |
{ NULL, 0, NULL } |
| 55 |
}; |
| 56 |
|
| 57 |
static int |
| 58 |
relay_back_cf( ConfigArgs *c ) |
| 59 |
{ |
| 60 |
relay_back_info *ri = ( relay_back_info * )c->be->be_private; |
| 61 |
int rc = 0; |
| 62 |
|
| 63 |
if ( c->op == SLAP_CONFIG_EMIT ) { |
| 64 |
if ( ri != NULL && !BER_BVISNULL( &ri->ri_realsuffix ) ) { |
| 65 |
value_add_one( &c->rvalue_vals, &ri->ri_realsuffix ); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
return 1; |
| 69 |
|
| 70 |
} else if ( c->op == LDAP_MOD_DELETE ) { |
| 71 |
if ( !BER_BVISNULL( &ri->ri_realsuffix ) ) { |
| 72 |
ch_free( ri->ri_realsuffix.bv_val ); |
| 73 |
BER_BVZERO( &ri->ri_realsuffix ); |
| 74 |
ri->ri_bd = NULL; |
| 75 |
return 0; |
| 76 |
} |
| 77 |
return 1; |
| 78 |
|
| 79 |
} else { |
| 80 |
BackendDB *bd; |
| 81 |
|
| 82 |
assert( ri != NULL ); |
| 83 |
assert( BER_BVISNULL( &ri->ri_realsuffix ) ); |
| 84 |
|
| 85 |
if ( c->be->be_nsuffix == NULL ) { |
| 86 |
snprintf( c->cr_msg, sizeof( c->cr_msg), |
| 87 |
"\"relay\" directive " |
| 88 |
"must appear after \"suffix\"" ); |
| 89 |
Log( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR, |
| 90 |
"%s: %s.\n", c->log, c->cr_msg ); |
| 91 |
rc = 1; |
| 92 |
goto relay_done; |
| 93 |
} |
| 94 |
|
| 95 |
if ( !BER_BVISNULL( &c->be->be_nsuffix[ 1 ] ) ) { |
| 96 |
snprintf( c->cr_msg, sizeof( c->cr_msg), |
| 97 |
"relaying of multiple suffix " |
| 98 |
"database not supported" ); |
| 99 |
Log( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR, |
| 100 |
"%s: %s.\n", c->log, c->cr_msg ); |
| 101 |
rc = 1; |
| 102 |
goto relay_done; |
| 103 |
} |
| 104 |
|
| 105 |
bd = select_backend( &c->value_ndn, 1 ); |
| 106 |
if ( bd == NULL ) { |
| 107 |
snprintf( c->cr_msg, sizeof( c->cr_msg), |
| 108 |
"cannot find database " |
| 109 |
"of relay dn \"%s\" " |
| 110 |
"in \"olcRelay <dn>\"\n", |
| 111 |
c->value_dn.bv_val ); |
| 112 |
Log( LDAP_DEBUG_CONFIG, LDAP_LEVEL_ERR, |
| 113 |
"%s: %s.\n", c->log, c->cr_msg ); |
| 114 |
|
| 115 |
} else if ( bd->be_private == c->be->be_private ) { |
| 116 |
snprintf( c->cr_msg, sizeof( c->cr_msg), |
| 117 |
"relay dn \"%s\" would call self " |
| 118 |
"in \"relay <dn>\" line\n", |
| 119 |
c->value_dn.bv_val ); |
| 120 |
Log( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR, |
| 121 |
"%s: %s.\n", c->log, c->cr_msg ); |
| 122 |
rc = 1; |
| 123 |
goto relay_done; |
| 124 |
} |
| 125 |
|
| 126 |
ri->ri_realsuffix = c->value_ndn; |
| 127 |
BER_BVZERO( &c->value_ndn ); |
| 128 |
|
| 129 |
relay_done:; |
| 130 |
ch_free( c->value_dn.bv_val ); |
| 131 |
ch_free( c->value_ndn.bv_val ); |
| 132 |
} |
| 133 |
|
| 134 |
return rc; |
| 135 |
} |
| 136 |
|
| 137 |
int |
| 138 |
relay_back_initialize( BackendInfo *bi ) |
| 139 |
{ |
| 140 |
bi->bi_init = 0; |
| 141 |
bi->bi_open = 0; |
| 142 |
bi->bi_config = 0; |
| 143 |
bi->bi_close = 0; |
| 144 |
bi->bi_destroy = 0; |
| 145 |
|
| 146 |
bi->bi_db_init = relay_back_db_init; |
| 147 |
bi->bi_db_config = config_generic_wrapper; |
| 148 |
bi->bi_db_open = relay_back_db_open; |
| 149 |
#if 0 |
| 150 |
bi->bi_db_close = relay_back_db_close; |
| 151 |
#endif |
| 152 |
bi->bi_db_destroy = relay_back_db_destroy; |
| 153 |
|
| 154 |
bi->bi_op_bind = relay_back_op_bind; |
| 155 |
bi->bi_op_search = relay_back_op_search; |
| 156 |
bi->bi_op_compare = relay_back_op_compare; |
| 157 |
bi->bi_op_modify = relay_back_op_modify; |
| 158 |
bi->bi_op_modrdn = relay_back_op_modrdn; |
| 159 |
bi->bi_op_add = relay_back_op_add; |
| 160 |
bi->bi_op_delete = relay_back_op_delete; |
| 161 |
bi->bi_extended = relay_back_op_extended; |
| 162 |
bi->bi_entry_release_rw = relay_back_entry_release_rw; |
| 163 |
bi->bi_entry_get_rw = relay_back_entry_get_rw; |
| 164 |
bi->bi_operational = relay_back_operational; |
| 165 |
bi->bi_has_subordinates = relay_back_has_subordinates; |
| 166 |
|
| 167 |
bi->bi_cf_ocs = relayocs; |
| 168 |
|
| 169 |
return config_register_schema( relaycfg, relayocs ); |
| 170 |
} |
| 171 |
|
| 172 |
int |
| 173 |
relay_back_db_init( Backend *be, ConfigReply *cr) |
| 174 |
{ |
| 175 |
relay_back_info *ri; |
| 176 |
|
| 177 |
be->be_private = NULL; |
| 178 |
|
| 179 |
ri = (relay_back_info *) ch_calloc( 1, RELAY_INFO_SIZE ); |
| 180 |
if ( ri == NULL ) { |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
ri->ri_bd = NULL; |
| 185 |
BER_BVZERO( &ri->ri_realsuffix ); |
| 186 |
ri->ri_massage = 0; |
| 187 |
|
| 188 |
be->be_cf_ocs = be->bd_info->bi_cf_ocs; |
| 189 |
|
| 190 |
be->be_private = (void *)ri; |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int |
| 196 |
relay_back_db_open( Backend *be, ConfigReply *cr ) |
| 197 |
{ |
| 198 |
relay_back_info *ri = (relay_back_info *)be->be_private; |
| 199 |
|
| 200 |
assert( ri != NULL ); |
| 201 |
|
| 202 |
if ( !BER_BVISNULL( &ri->ri_realsuffix ) ) { |
| 203 |
ri->ri_bd = select_backend( &ri->ri_realsuffix, 1 ); |
| 204 |
|
| 205 |
/* must be there: it was during config! */ |
| 206 |
if ( ri->ri_bd == NULL ) { |
| 207 |
snprintf( cr->msg, sizeof( cr->msg), |
| 208 |
"cannot find database " |
| 209 |
"of relay dn \"%s\" " |
| 210 |
"in \"olcRelay <dn>\"\n", |
| 211 |
ri->ri_realsuffix.bv_val ); |
| 212 |
Log( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR, |
| 213 |
"relay_back_db_open: %s.\n", cr->msg ); |
| 214 |
|
| 215 |
return 1; |
| 216 |
} |
| 217 |
|
| 218 |
/* inherit controls */ |
| 219 |
AC_MEMCPY( be->bd_self->be_ctrls, ri->ri_bd->be_ctrls, sizeof( be->be_ctrls ) ); |
| 220 |
|
| 221 |
} else { |
| 222 |
/* inherit all? */ |
| 223 |
AC_MEMCPY( be->bd_self->be_ctrls, frontendDB->be_ctrls, sizeof( be->be_ctrls ) ); |
| 224 |
} |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
int |
| 230 |
relay_back_db_close( Backend *be, ConfigReply *cr ) |
| 231 |
{ |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
int |
| 236 |
relay_back_db_destroy( Backend *be, ConfigReply *cr) |
| 237 |
{ |
| 238 |
relay_back_info *ri = (relay_back_info *)be->be_private; |
| 239 |
|
| 240 |
if ( ri ) { |
| 241 |
if ( !BER_BVISNULL( &ri->ri_realsuffix ) ) { |
| 242 |
ch_free( ri->ri_realsuffix.bv_val ); |
| 243 |
} |
| 244 |
ch_free( ri ); |
| 245 |
} |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
#if SLAPD_RELAY == SLAPD_MOD_DYNAMIC |
| 251 |
|
| 252 |
/* conditionally define the init_module() function */ |
| 253 |
SLAP_BACKEND_INIT_MODULE( relay ) |
| 254 |
|
| 255 |
#endif /* SLAPD_RELAY == SLAPD_MOD_DYNAMIC */ |