1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
/* init.cpp - initialize ndb backend */
/* $OpenLDAP: pkg/ldap/servers/slapd/back-ndb/init.cpp,v 1.4.2.4 2010/04/13 20:23:35 kurt Exp $ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2008-2010 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by Howard Chu for inclusion
* in OpenLDAP Software. This work was sponsored by MySQL.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/string.h>
#include <ac/unistd.h>
#include <ac/stdlib.h>
#include <ac/errno.h>
#include <sys/stat.h>
#include "back-ndb.h"
#include <lutil.h>
#include "config.h"
extern "C" {
static BI_db_init ndb_db_init;
static BI_db_close ndb_db_close;
static BI_db_open ndb_db_open;
static BI_db_destroy ndb_db_destroy;
}
static struct berval ndb_optable = BER_BVC("OL_opattrs");
static struct berval ndb_opattrs[] = {
BER_BVC("structuralObjectClass"),
BER_BVC("entryUUID"),
BER_BVC("creatorsName"),
BER_BVC("createTimestamp"),
BER_BVC("entryCSN"),
BER_BVC("modifiersName"),
BER_BVC("modifyTimestamp"),
BER_BVNULL
};
static int ndb_oplens[] = {
0, /* structuralOC, default */
36, /* entryUUID */
0, /* creatorsName, default */
26, /* createTimestamp */
40, /* entryCSN */
0, /* modifiersName, default */
26, /* modifyTimestamp */
-1
};
static Uint32 ndb_lastrow[1];
NdbInterpretedCode *ndb_lastrow_code;
static int
ndb_db_init( BackendDB *be, ConfigReply *cr )
{
struct ndb_info *ni;
int rc = 0;
Debug( LDAP_DEBUG_TRACE,
LDAP_XSTRING(ndb_db_init) ": Initializing ndb database\n",
0, 0, 0 );
/* allocate backend-database-specific stuff */
ni = (struct ndb_info *) ch_calloc( 1, sizeof(struct ndb_info) );
be->be_private = ni;
be->be_cf_ocs = be->bd_info->bi_cf_ocs;
ni->ni_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
ldap_pvt_thread_rdwr_init( &ni->ni_ai_rwlock );
ldap_pvt_thread_rdwr_init( &ni->ni_oc_rwlock );
ldap_pvt_thread_mutex_init( &ni->ni_conn_mutex );
#ifdef DO_MONITORING
rc = ndb_monitor_db_init( be );
#endif
return rc;
}
static int
ndb_db_close( BackendDB *be, ConfigReply *cr );
static int
ndb_db_open( BackendDB *be, ConfigReply *cr )
{
struct ndb_info *ni = (struct ndb_info *) be->be_private;
char sqlbuf[BUFSIZ], *ptr;
int rc, i;
if ( be->be_suffix == NULL ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: need suffix" );
Debug( LDAP_DEBUG_ANY, "%s\n",
cr->msg, 0, 0 );
return -1;
}
Debug( LDAP_DEBUG_ARGS,
LDAP_XSTRING(ndb_db_open) ": \"%s\"\n",
be->be_suffix[0].bv_val, 0, 0 );
if ( ni->ni_nconns < 1 )
ni->ni_nconns = 1;
ni->ni_cluster = (Ndb_cluster_connection **)ch_calloc( ni->ni_nconns, sizeof( Ndb_cluster_connection *));
for ( i=0; i<ni->ni_nconns; i++ ) {
ni->ni_cluster[i] = new Ndb_cluster_connection( ni->ni_connectstr );
rc = ni->ni_cluster[i]->connect( 20, 5, 1 );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: ni_cluster[%d]->connect failed (%d)",
i, rc );
goto fail;
}
}
for ( i=0; i<ni->ni_nconns; i++ ) {
rc = ni->ni_cluster[i]->wait_until_ready( 30, 30 );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: ni_cluster[%d]->wait failed (%d)",
i, rc );
goto fail;
}
}
mysql_init( &ni->ni_sql );
if ( !mysql_real_connect( &ni->ni_sql, ni->ni_hostname, ni->ni_username, ni->ni_password,
"", ni->ni_port, ni->ni_socket, ni->ni_clflag )) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: mysql_real_connect failed, %s (%d)",
mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
rc = -1;
goto fail;
}
sprintf( sqlbuf, "CREATE DATABASE IF NOT EXISTS %s", ni->ni_dbname );
rc = mysql_query( &ni->ni_sql, sqlbuf );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: CREATE DATABASE %s failed, %s (%d)",
ni->ni_dbname, mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
goto fail;
}
sprintf( sqlbuf, "USE %s", ni->ni_dbname );
rc = mysql_query( &ni->ni_sql, sqlbuf );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: USE DATABASE %s failed, %s (%d)",
ni->ni_dbname, mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
goto fail;
}
ptr = sqlbuf;
ptr += sprintf( ptr, "CREATE TABLE IF NOT EXISTS " DN2ID_TABLE " ("
"eid bigint unsigned NOT NULL, "
"object_classes VARCHAR(1024) NOT NULL, "
"a0 VARCHAR(128) NOT NULL DEFAULT '', "
"a1 VARCHAR(128) NOT NULL DEFAULT '', "
"a2 VARCHAR(128) NOT NULL DEFAULT '', "
"a3 VARCHAR(128) NOT NULL DEFAULT '', "
"a4 VARCHAR(128) NOT NULL DEFAULT '', "
"a5 VARCHAR(128) NOT NULL DEFAULT '', "
"a6 VARCHAR(128) NOT NULL DEFAULT '', "
"a7 VARCHAR(128) NOT NULL DEFAULT '', "
"a8 VARCHAR(128) NOT NULL DEFAULT '', "
"a9 VARCHAR(128) NOT NULL DEFAULT '', "
"a10 VARCHAR(128) NOT NULL DEFAULT '', "
"a11 VARCHAR(128) NOT NULL DEFAULT '', "
"a12 VARCHAR(128) NOT NULL DEFAULT '', "
"a13 VARCHAR(128) NOT NULL DEFAULT '', "
"a14 VARCHAR(128) NOT NULL DEFAULT '', "
"a15 VARCHAR(128) NOT NULL DEFAULT '', "
"PRIMARY KEY (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15), "
"UNIQUE KEY eid (eid) USING HASH" );
/* Create index columns */
if ( ni->ni_attridxs ) {
ListNode *ln;
int newcol = 0;
*ptr++ = ',';
*ptr++ = ' ';
for ( ln = ni->ni_attridxs; ln; ln=ln->ln_next ) {
NdbAttrInfo *ai = (NdbAttrInfo *)ln->ln_data;
ptr += sprintf( ptr, "`%s` VARCHAR(%d), ",
ai->na_name.bv_val, ai->na_len );
}
ptr = lutil_strcopy(ptr, "KEY " INDEX_NAME " (" );
for ( ln = ni->ni_attridxs; ln; ln=ln->ln_next ) {
NdbAttrInfo *ai = (NdbAttrInfo *)ln->ln_data;
if ( newcol ) *ptr++ = ',';
*ptr++ = '`';
ptr = lutil_strcopy( ptr, ai->na_name.bv_val );
*ptr++ = '`';
ai->na_ixcol = newcol + 18;
newcol++;
}
*ptr++ = ')';
}
strcpy( ptr, ") ENGINE=ndb" );
rc = mysql_query( &ni->ni_sql, sqlbuf );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: CREATE TABLE " DN2ID_TABLE " failed, %s (%d)",
mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
goto fail;
}
rc = mysql_query( &ni->ni_sql, "CREATE TABLE IF NOT EXISTS " NEXTID_TABLE " ("
"a bigint unsigned AUTO_INCREMENT PRIMARY KEY ) ENGINE=ndb" );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: CREATE TABLE " NEXTID_TABLE " failed, %s (%d)",
mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
goto fail;
}
{
NdbOcInfo *oci;
rc = ndb_aset_get( ni, &ndb_optable, ndb_opattrs, &oci );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: ndb_aset_get( %s ) failed (%d)",
ndb_optable.bv_val, rc );
goto fail;
}
for ( i=0; ndb_oplens[i] >= 0; i++ ) {
if ( ndb_oplens[i] )
oci->no_attrs[i]->na_len = ndb_oplens[i];
}
rc = ndb_aset_create( ni, oci );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: ndb_aset_create( %s ) failed (%d)",
ndb_optable.bv_val, rc );
goto fail;
}
ni->ni_opattrs = oci;
}
/* Create attribute sets */
{
ListNode *ln;
for ( ln = ni->ni_attrsets; ln; ln=ln->ln_next ) {
NdbOcInfo *oci = (NdbOcInfo *)ln->ln_data;
rc = ndb_aset_create( ni, oci );
if ( rc ) {
snprintf( cr->msg, sizeof( cr->msg ),
"ndb_db_open: ndb_aset_create( %s ) failed (%d)",
oci->no_name.bv_val, rc );
goto fail;
}
}
}
/* Initialize any currently used objectClasses */
{
Ndb *ndb;
const NdbDictionary::Dictionary *myDict;
ndb = new Ndb( ni->ni_cluster[0], ni->ni_dbname );
ndb->init(1024);
myDict = ndb->getDictionary();
ndb_oc_read( ni, myDict );
delete ndb;
}
#ifdef DO_MONITORING
/* monitor setup */
rc = ndb_monitor_db_open( be );
if ( rc != 0 ) {
goto fail;
}
#endif
return 0;
fail:
Debug( LDAP_DEBUG_ANY, "%s\n",
cr->msg, 0, 0 );
ndb_db_close( be, NULL );
return rc;
}
static int
ndb_db_close( BackendDB *be, ConfigReply *cr )
{
int i;
struct ndb_info *ni = (struct ndb_info *) be->be_private;
mysql_close( &ni->ni_sql );
if ( ni->ni_cluster ) {
for ( i=0; i<ni->ni_nconns; i++ ) {
if ( ni->ni_cluster[i] ) {
delete ni->ni_cluster[i];
ni->ni_cluster[i] = NULL;
}
}
ch_free( ni->ni_cluster );
ni->ni_cluster = NULL;
}
#ifdef DO_MONITORING
/* monitor handling */
(void)ndb_monitor_db_close( be );
#endif
return 0;
}
static int
ndb_db_destroy( BackendDB *be, ConfigReply *cr )
{
struct ndb_info *ni = (struct ndb_info *) be->be_private;
#ifdef DO_MONITORING
/* monitor handling */
(void)ndb_monitor_db_destroy( be );
#endif
ldap_pvt_thread_mutex_destroy( &ni->ni_conn_mutex );
ldap_pvt_thread_rdwr_destroy( &ni->ni_ai_rwlock );
ldap_pvt_thread_rdwr_destroy( &ni->ni_oc_rwlock );
ch_free( ni );
be->be_private = NULL;
return 0;
}
extern "C" int
ndb_back_initialize(
BackendInfo *bi )
{
static char *controls[] = {
LDAP_CONTROL_ASSERT,
LDAP_CONTROL_MANAGEDSAIT,
LDAP_CONTROL_NOOP,
LDAP_CONTROL_PAGEDRESULTS,
LDAP_CONTROL_PRE_READ,
LDAP_CONTROL_POST_READ,
LDAP_CONTROL_SUBENTRIES,
LDAP_CONTROL_X_PERMISSIVE_MODIFY,
#ifdef LDAP_X_TXN
LDAP_CONTROL_X_TXN_SPEC,
#endif
NULL
};
int rc = 0;
/* initialize the underlying database system */
Debug( LDAP_DEBUG_TRACE,
LDAP_XSTRING(ndb_back_initialize) ": initialize ndb backend\n", 0, 0, 0 );
ndb_init();
ndb_lastrow_code = new NdbInterpretedCode( NULL, ndb_lastrow, 1 );
ndb_lastrow_code->interpret_exit_last_row();
ndb_lastrow_code->finalise();
bi->bi_flags |=
SLAP_BFLAG_INCREMENT |
SLAP_BFLAG_SUBENTRIES |
SLAP_BFLAG_ALIASES |
SLAP_BFLAG_REFERRALS;
bi->bi_controls = controls;
bi->bi_open = 0;
bi->bi_close = 0;
bi->bi_config = 0;
bi->bi_destroy = 0;
bi->bi_db_init = ndb_db_init;
bi->bi_db_config = config_generic_wrapper;
bi->bi_db_open = ndb_db_open;
bi->bi_db_close = ndb_db_close;
bi->bi_db_destroy = ndb_db_destroy;
bi->bi_op_add = ndb_back_add;
bi->bi_op_bind = ndb_back_bind;
bi->bi_op_compare = ndb_back_compare;
bi->bi_op_delete = ndb_back_delete;
bi->bi_op_modify = ndb_back_modify;
bi->bi_op_modrdn = ndb_back_modrdn;
bi->bi_op_search = ndb_back_search;
bi->bi_op_unbind = 0;
#if 0
bi->bi_extended = ndb_extended;
bi->bi_chk_referrals = ndb_referrals;
#endif
bi->bi_operational = ndb_operational;
bi->bi_has_subordinates = ndb_has_subordinates;
bi->bi_entry_release_rw = 0;
bi->bi_entry_get_rw = ndb_entry_get;
/*
* hooks for slap tools
*/
bi->bi_tool_entry_open = ndb_tool_entry_open;
bi->bi_tool_entry_close = ndb_tool_entry_close;
bi->bi_tool_entry_first = ndb_tool_entry_first;
bi->bi_tool_entry_next = ndb_tool_entry_next;
bi->bi_tool_entry_get = ndb_tool_entry_get;
bi->bi_tool_entry_put = ndb_tool_entry_put;
#if 0
bi->bi_tool_entry_reindex = ndb_tool_entry_reindex;
bi->bi_tool_sync = 0;
bi->bi_tool_dn2id_get = ndb_tool_dn2id_get;
bi->bi_tool_entry_modify = ndb_tool_entry_modify;
#endif
bi->bi_connection_init = 0;
bi->bi_connection_destroy = 0;
rc = ndb_back_init_cf( bi );
return rc;
}
#if SLAPD_NDB == SLAPD_MOD_DYNAMIC
/* conditionally define the init_module() function */
extern "C" { int init_module( int argc, char *argv[] ); }
SLAP_BACKEND_INIT_MODULE( ndb )
#endif /* SLAPD_NDB == SLAPD_MOD_DYNAMIC */
|