| 1 |
/* $OpenLDAP$ */ |
| 2 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 3 |
* |
| 4 |
* Copyright 1999-2024 The OpenLDAP Foundation. |
| 5 |
* Portions Copyright 1999 Dmitry Kovalev. |
| 6 |
* Portions Copyright 2002 Pierangelo Masarati. |
| 7 |
* Portions Copyright 2004 Mark Adamson. |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or without |
| 11 |
* modification, are permitted only as authorized by the OpenLDAP |
| 12 |
* Public License. |
| 13 |
* |
| 14 |
* A copy of this license is available in the file LICENSE in the |
| 15 |
* top-level directory of the distribution or, alternatively, at |
| 16 |
* <http://www.OpenLDAP.org/license.html>. |
| 17 |
*/ |
| 18 |
/* ACKNOWLEDGEMENTS: |
| 19 |
* This work was initially developed by Dmitry Kovalev for inclusion |
| 20 |
* by OpenLDAP Software. Additional significant contributors include |
| 21 |
* Pierangelo Masarati. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "portable.h" |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include "ac/string.h" |
| 28 |
#include <sys/types.h> |
| 29 |
|
| 30 |
#include "slap.h" |
| 31 |
#include "slap-config.h" |
| 32 |
#include "ldif.h" |
| 33 |
#include "lutil.h" |
| 34 |
#include "proto-sql.h" |
| 35 |
|
| 36 |
static int |
| 37 |
create_baseObject( |
| 38 |
BackendDB *be, |
| 39 |
const char *fname, |
| 40 |
int lineno ); |
| 41 |
|
| 42 |
static int |
| 43 |
read_baseObject( |
| 44 |
BackendDB *be, |
| 45 |
const char *fname ); |
| 46 |
|
| 47 |
static ConfigDriver sql_cf_gen; |
| 48 |
|
| 49 |
enum { |
| 50 |
BSQL_CONCAT_PATT = 1, |
| 51 |
BSQL_CREATE_NEEDS_SEL, |
| 52 |
BSQL_UPPER_NEEDS_CAST, |
| 53 |
BSQL_HAS_LDAPINFO_DN_RU, |
| 54 |
BSQL_FAIL_IF_NO_MAPPING, |
| 55 |
BSQL_ALLOW_ORPHANS, |
| 56 |
BSQL_BASE_OBJECT, |
| 57 |
BSQL_LAYER, |
| 58 |
BSQL_SUBTREE_SHORTCUT, |
| 59 |
BSQL_FETCH_ALL_ATTRS, |
| 60 |
BSQL_FETCH_ATTRS, |
| 61 |
BSQL_CHECK_SCHEMA, |
| 62 |
BSQL_ALIASING_KEYWORD, |
| 63 |
BSQL_AUTOCOMMIT |
| 64 |
}; |
| 65 |
|
| 66 |
static ConfigTable sqlcfg[] = { |
| 67 |
{ "dbhost", "hostname", 2, 2, 0, ARG_STRING|ARG_OFFSET, |
| 68 |
(void *)offsetof(struct backsql_info, sql_dbhost), |
| 69 |
"( OLcfgDbAt:6.1 NAME 'olcDbHost' " |
| 70 |
"DESC 'Hostname of SQL server' " |
| 71 |
"EQUALITY caseExactMatch " |
| 72 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 73 |
{ "dbname", "name", 2, 2, 0, ARG_STRING|ARG_OFFSET, |
| 74 |
(void *)offsetof(struct backsql_info, sql_dbname), |
| 75 |
"( OLcfgDbAt:6.2 NAME 'olcDbName' " |
| 76 |
"DESC 'Name of SQL database' " |
| 77 |
"EQUALITY caseExactMatch " |
| 78 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 79 |
{ "dbuser", "username", 2, 2, 0, ARG_STRING|ARG_OFFSET, |
| 80 |
(void *)offsetof(struct backsql_info, sql_dbuser), |
| 81 |
"( OLcfgDbAt:6.3 NAME 'olcDbUser' " |
| 82 |
"DESC 'Username for SQL session' " |
| 83 |
"EQUALITY caseExactMatch " |
| 84 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 85 |
{ "dbpasswd", "password", 2, 2, 0, ARG_STRING|ARG_OFFSET, |
| 86 |
(void *)offsetof(struct backsql_info, sql_dbpasswd), |
| 87 |
"( OLcfgDbAt:6.4 NAME 'olcDbPass' " |
| 88 |
"DESC 'Password for SQL session' " |
| 89 |
"EQUALITY caseExactMatch " |
| 90 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 91 |
{ "concat_pattern", "pattern", 2, 2, 0, |
| 92 |
ARG_STRING|ARG_MAGIC|BSQL_CONCAT_PATT, (void *)sql_cf_gen, |
| 93 |
"( OLcfgDbAt:6.20 NAME 'olcSqlConcatPattern' " |
| 94 |
"DESC 'Pattern used to concatenate strings' " |
| 95 |
"EQUALITY caseExactMatch " |
| 96 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 97 |
{ "subtree_cond", "SQL expression", 2, 0, 0, ARG_BERVAL|ARG_QUOTE|ARG_OFFSET, |
| 98 |
(void *)offsetof(struct backsql_info, sql_subtree_cond), |
| 99 |
"( OLcfgDbAt:6.21 NAME 'olcSqlSubtreeCond' " |
| 100 |
"DESC 'Where-clause template for a subtree search condition' " |
| 101 |
"EQUALITY caseExactMatch " |
| 102 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 103 |
{ "children_cond", "SQL expression", 2, 0, 0, ARG_BERVAL|ARG_QUOTE|ARG_OFFSET, |
| 104 |
(void *)offsetof(struct backsql_info, sql_children_cond), |
| 105 |
"( OLcfgDbAt:6.22 NAME 'olcSqlChildrenCond' " |
| 106 |
"DESC 'Where-clause template for a children search condition' " |
| 107 |
"EQUALITY caseExactMatch " |
| 108 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 109 |
{ "dn_match_cond", "SQL expression", 2, 0, 0, ARG_BERVAL|ARG_QUOTE|ARG_OFFSET, |
| 110 |
(void *)offsetof(struct backsql_info, sql_dn_match_cond), |
| 111 |
"( OLcfgDbAt:6.23 NAME 'olcSqlDnMatchCond' " |
| 112 |
"DESC 'Where-clause template for a DN match search condition' " |
| 113 |
"EQUALITY caseExactMatch " |
| 114 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 115 |
{ "oc_query", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET, |
| 116 |
(void *)offsetof(struct backsql_info, sql_oc_query), |
| 117 |
"( OLcfgDbAt:6.24 NAME 'olcSqlOcQuery' " |
| 118 |
"DESC 'Query used to collect objectClass mapping data' " |
| 119 |
"EQUALITY caseExactMatch " |
| 120 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 121 |
{ "at_query", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET, |
| 122 |
(void *)offsetof(struct backsql_info, sql_at_query), |
| 123 |
"( OLcfgDbAt:6.25 NAME 'olcSqlAtQuery' " |
| 124 |
"DESC 'Query used to collect attributeType mapping data' " |
| 125 |
"EQUALITY caseExactMatch " |
| 126 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 127 |
{ "insentry_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET, |
| 128 |
(void *)offsetof(struct backsql_info, sql_insentry_stmt), |
| 129 |
"( OLcfgDbAt:6.26 NAME 'olcSqlInsEntryStmt' " |
| 130 |
"DESC 'Statement used to insert a new entry' " |
| 131 |
"EQUALITY caseExactMatch " |
| 132 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 133 |
{ "create_needs_select", "yes|no", 2, 2, 0, |
| 134 |
ARG_ON_OFF|ARG_MAGIC|BSQL_CREATE_NEEDS_SEL, (void *)sql_cf_gen, |
| 135 |
"( OLcfgDbAt:6.27 NAME 'olcSqlCreateNeedsSelect' " |
| 136 |
"DESC 'Whether entry creation needs a subsequent select' " |
| 137 |
"EQUALITY booleanMatch " |
| 138 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 139 |
{ "upper_func", "SQL function name", 2, 2, 0, ARG_BERVAL|ARG_OFFSET, |
| 140 |
(void *)offsetof(struct backsql_info, sql_upper_func), |
| 141 |
"( OLcfgDbAt:6.28 NAME 'olcSqlUpperFunc' " |
| 142 |
"DESC 'Function that converts a value to uppercase' " |
| 143 |
"EQUALITY caseExactMatch " |
| 144 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 145 |
{ "upper_needs_cast", "yes|no", 2, 2, 0, |
| 146 |
ARG_ON_OFF|ARG_MAGIC|BSQL_UPPER_NEEDS_CAST, (void *)sql_cf_gen, |
| 147 |
"( OLcfgDbAt:6.29 NAME 'olcSqlUpperNeedsCast' " |
| 148 |
"DESC 'Whether olcSqlUpperFunc needs an explicit cast' " |
| 149 |
"EQUALITY booleanMatch " |
| 150 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 151 |
{ "strcast_func", "SQL function name", 2, 2, 0, ARG_BERVAL|ARG_OFFSET, |
| 152 |
(void *)offsetof(struct backsql_info, sql_strcast_func), |
| 153 |
"( OLcfgDbAt:6.30 NAME 'olcSqlStrcastFunc' " |
| 154 |
"DESC 'Function that converts a value to a string' " |
| 155 |
"EQUALITY caseExactMatch " |
| 156 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 157 |
{ "delentry_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET, |
| 158 |
(void *)offsetof(struct backsql_info, sql_delentry_stmt), |
| 159 |
"( OLcfgDbAt:6.31 NAME 'olcSqlDelEntryStmt' " |
| 160 |
"DESC 'Statement used to delete an existing entry' " |
| 161 |
"EQUALITY caseExactMatch " |
| 162 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 163 |
{ "renentry_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET, |
| 164 |
(void *)offsetof(struct backsql_info, sql_renentry_stmt), |
| 165 |
"( OLcfgDbAt:6.32 NAME 'olcSqlRenEntryStmt' " |
| 166 |
"DESC 'Statement used to rename an entry' " |
| 167 |
"EQUALITY caseExactMatch " |
| 168 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 169 |
{ "delobjclasses_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET, |
| 170 |
(void *)offsetof(struct backsql_info, sql_delobjclasses_stmt), |
| 171 |
"( OLcfgDbAt:6.33 NAME 'olcSqlDelObjclassesStmt' " |
| 172 |
"DESC 'Statement used to delete the ID of an entry' " |
| 173 |
"EQUALITY caseExactMatch " |
| 174 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 175 |
{ "has_ldapinfo_dn_ru", "yes|no", 2, 2, 0, |
| 176 |
ARG_ON_OFF|ARG_MAGIC|BSQL_HAS_LDAPINFO_DN_RU, (void *)sql_cf_gen, |
| 177 |
"( OLcfgDbAt:6.34 NAME 'olcSqlHasLDAPinfoDnRu' " |
| 178 |
"DESC 'Whether the dn_ru column is present' " |
| 179 |
"EQUALITY booleanMatch " |
| 180 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 181 |
{ "fail_if_no_mapping", "yes|no", 2, 2, 0, |
| 182 |
ARG_ON_OFF|ARG_MAGIC|BSQL_FAIL_IF_NO_MAPPING, (void *)sql_cf_gen, |
| 183 |
"( OLcfgDbAt:6.35 NAME 'olcSqlFailIfNoMapping' " |
| 184 |
"DESC 'Whether to fail on unknown attribute mappings' " |
| 185 |
"EQUALITY booleanMatch " |
| 186 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 187 |
{ "allow_orphans", "yes|no", 2, 2, 0, |
| 188 |
ARG_ON_OFF|ARG_MAGIC|BSQL_ALLOW_ORPHANS, (void *)sql_cf_gen, |
| 189 |
"( OLcfgDbAt:6.36 NAME 'olcSqlAllowOrphans' " |
| 190 |
"DESC 'Whether to allow adding entries with no parent' " |
| 191 |
"EQUALITY booleanMatch " |
| 192 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 193 |
{ "baseobject", "[file]", 1, 2, 0, |
| 194 |
ARG_STRING|ARG_MAGIC|BSQL_BASE_OBJECT, (void *)sql_cf_gen, |
| 195 |
"( OLcfgDbAt:6.37 NAME 'olcSqlBaseObject' " |
| 196 |
"DESC 'Manage an in-memory baseObject entry' " |
| 197 |
"EQUALITY caseExactMatch " |
| 198 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 199 |
{ "sqllayer", "name", 2, 0, 0, |
| 200 |
ARG_MAGIC|BSQL_LAYER, (void *)sql_cf_gen, |
| 201 |
"( OLcfgDbAt:6.38 NAME 'olcSqlLayer' " |
| 202 |
"DESC 'Helper used to map DNs between LDAP and SQL' " |
| 203 |
"EQUALITY caseExactMatch " |
| 204 |
"SYNTAX OMsDirectoryString )", NULL, NULL }, |
| 205 |
{ "use_subtree_shortcut", "yes|no", 2, 2, 0, |
| 206 |
ARG_ON_OFF|ARG_MAGIC|BSQL_SUBTREE_SHORTCUT, (void *)sql_cf_gen, |
| 207 |
"( OLcfgDbAt:6.39 NAME 'olcSqlUseSubtreeShortcut' " |
| 208 |
"DESC 'Collect all entries when searchBase is DB suffix' " |
| 209 |
"EQUALITY booleanMatch " |
| 210 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 211 |
{ "fetch_all_attrs", "yes|no", 2, 2, 0, |
| 212 |
ARG_ON_OFF|ARG_MAGIC|BSQL_FETCH_ALL_ATTRS, (void *)sql_cf_gen, |
| 213 |
"( OLcfgDbAt:6.40 NAME 'olcSqlFetchAllAttrs' " |
| 214 |
"DESC 'Require all attributes to always be loaded' " |
| 215 |
"EQUALITY booleanMatch " |
| 216 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 217 |
{ "fetch_attrs", "attrlist", 2, 0, 0, |
| 218 |
ARG_MAGIC|BSQL_FETCH_ATTRS, (void *)sql_cf_gen, |
| 219 |
"( OLcfgDbAt:6.41 NAME 'olcSqlFetchAttrs' " |
| 220 |
"DESC 'Set of attributes to always fetch' " |
| 221 |
"EQUALITY caseIgnoreMatch " |
| 222 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 223 |
{ "check_schema", "yes|no", 2, 2, 0, |
| 224 |
ARG_ON_OFF|ARG_MAGIC|BSQL_CHECK_SCHEMA, (void *)sql_cf_gen, |
| 225 |
"( OLcfgDbAt:6.42 NAME 'olcSqlCheckSchema' " |
| 226 |
"DESC 'Check schema after modifications' " |
| 227 |
"EQUALITY booleanMatch " |
| 228 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 229 |
{ "aliasing_keyword", "string", 2, 2, 0, |
| 230 |
ARG_STRING|ARG_MAGIC|BSQL_ALIASING_KEYWORD, (void *)sql_cf_gen, |
| 231 |
"( OLcfgDbAt:6.43 NAME 'olcSqlAliasingKeyword' " |
| 232 |
"DESC 'The aliasing keyword' " |
| 233 |
"EQUALITY caseExactMatch " |
| 234 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 235 |
{ "aliasing_quote", "string", 2, 2, 0, ARG_BERVAL|ARG_OFFSET, |
| 236 |
(void *)offsetof(struct backsql_info, sql_aliasing_quote), |
| 237 |
"( OLcfgDbAt:6.44 NAME 'olcSqlAliasingQuote' " |
| 238 |
"DESC 'Quoting char of the aliasing keyword' " |
| 239 |
"EQUALITY caseIgnoreMatch " |
| 240 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 241 |
{ "autocommit", "yes|no", 2, 2, 0, |
| 242 |
ARG_ON_OFF|ARG_MAGIC|BSQL_AUTOCOMMIT, (void *)sql_cf_gen, |
| 243 |
"( OLcfgDbAt:6.45 NAME 'olcSqlAutocommit' " |
| 244 |
"EQUALITY booleanMatch " |
| 245 |
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, |
| 246 |
{ "id_query", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET, |
| 247 |
(void *)offsetof(struct backsql_info, sql_id_query), |
| 248 |
"( OLcfgDbAt:6.46 NAME 'olcSqlIdQuery' " |
| 249 |
"DESC 'Query used to collect entryID mapping data' " |
| 250 |
"EQUALITY caseExactMatch " |
| 251 |
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL }, |
| 252 |
{ NULL, NULL, 0, 0, 0, ARG_IGNORED, |
| 253 |
NULL, NULL, NULL, NULL } |
| 254 |
}; |
| 255 |
|
| 256 |
static ConfigOCs sqlocs[] = { |
| 257 |
{ |
| 258 |
"( OLcfgDbOc:6.1 " |
| 259 |
"NAME 'olcSqlConfig' " |
| 260 |
"DESC 'SQL backend configuration' " |
| 261 |
"SUP olcDatabaseConfig " |
| 262 |
"MUST olcDbName " |
| 263 |
"MAY ( olcDbHost $ olcDbUser $ olcDbPass $ olcSqlConcatPattern $ " |
| 264 |
"olcSqlSubtreeCond $ olcsqlChildrenCond $ olcSqlDnMatchCond $ " |
| 265 |
"olcSqlOcQuery $ olcSqlAtQuery $ olcSqlInsEntryStmt $ " |
| 266 |
"olcSqlCreateNeedsSelect $ olcSqlUpperFunc $ olcSqlUpperNeedsCast $ " |
| 267 |
"olcSqlStrCastFunc $ olcSqlDelEntryStmt $ olcSqlRenEntryStmt $ " |
| 268 |
"olcSqlDelObjClassesStmt $ olcSqlHasLDAPInfoDnRu $ " |
| 269 |
"olcSqlFailIfNoMapping $ olcSqlAllowOrphans $ olcSqlBaseObject $ " |
| 270 |
"olcSqlLayer $ olcSqlUseSubtreeShortcut $ olcSqlFetchAllAttrs $ " |
| 271 |
"olcSqlFetchAttrs $ olcSqlCheckSchema $ olcSqlAliasingKeyword $ " |
| 272 |
"olcSqlAliasingQuote $ olcSqlAutocommit $ olcSqlIdQuery ) )", |
| 273 |
Cft_Database, sqlcfg }, |
| 274 |
{ NULL, Cft_Abstract, NULL } |
| 275 |
}; |
| 276 |
|
| 277 |
static int |
| 278 |
sql_cf_gen( ConfigArgs *c ) |
| 279 |
{ |
| 280 |
backsql_info *bi = (backsql_info *)c->be->be_private; |
| 281 |
int rc = 0; |
| 282 |
|
| 283 |
if ( c->op == SLAP_CONFIG_EMIT ) { |
| 284 |
switch( c->type ) { |
| 285 |
case BSQL_CONCAT_PATT: |
| 286 |
if ( bi->sql_concat_patt ) { |
| 287 |
c->value_string = ch_strdup( bi->sql_concat_patt ); |
| 288 |
} else { |
| 289 |
rc = 1; |
| 290 |
} |
| 291 |
break; |
| 292 |
case BSQL_CREATE_NEEDS_SEL: |
| 293 |
if ( bi->sql_flags & BSQLF_CREATE_NEEDS_SELECT ) |
| 294 |
c->value_int = 1; |
| 295 |
break; |
| 296 |
case BSQL_UPPER_NEEDS_CAST: |
| 297 |
if ( bi->sql_flags & BSQLF_UPPER_NEEDS_CAST ) |
| 298 |
c->value_int = 1; |
| 299 |
break; |
| 300 |
case BSQL_HAS_LDAPINFO_DN_RU: |
| 301 |
if ( !(bi->sql_flags & BSQLF_DONTCHECK_LDAPINFO_DN_RU) ) |
| 302 |
return 1; |
| 303 |
if ( bi->sql_flags & BSQLF_HAS_LDAPINFO_DN_RU ) |
| 304 |
c->value_int = 1; |
| 305 |
break; |
| 306 |
case BSQL_FAIL_IF_NO_MAPPING: |
| 307 |
if ( bi->sql_flags & BSQLF_FAIL_IF_NO_MAPPING ) |
| 308 |
c->value_int = 1; |
| 309 |
break; |
| 310 |
case BSQL_ALLOW_ORPHANS: |
| 311 |
if ( bi->sql_flags & BSQLF_ALLOW_ORPHANS ) |
| 312 |
c->value_int = 1; |
| 313 |
break; |
| 314 |
case BSQL_SUBTREE_SHORTCUT: |
| 315 |
if ( bi->sql_flags & BSQLF_USE_SUBTREE_SHORTCUT ) |
| 316 |
c->value_int = 1; |
| 317 |
break; |
| 318 |
case BSQL_FETCH_ALL_ATTRS: |
| 319 |
if ( bi->sql_flags & BSQLF_FETCH_ALL_ATTRS ) |
| 320 |
c->value_int = 1; |
| 321 |
break; |
| 322 |
case BSQL_CHECK_SCHEMA: |
| 323 |
if ( bi->sql_flags & BSQLF_CHECK_SCHEMA ) |
| 324 |
c->value_int = 1; |
| 325 |
break; |
| 326 |
case BSQL_AUTOCOMMIT: |
| 327 |
if ( bi->sql_flags & BSQLF_AUTOCOMMIT_ON ) |
| 328 |
c->value_int = 1; |
| 329 |
break; |
| 330 |
case BSQL_BASE_OBJECT: |
| 331 |
if ( bi->sql_base_ob_file ) { |
| 332 |
c->value_string = ch_strdup( bi->sql_base_ob_file ); |
| 333 |
} else if ( bi->sql_baseObject ) { |
| 334 |
c->value_string = ch_strdup( "TRUE" ); |
| 335 |
} else { |
| 336 |
rc = 1; |
| 337 |
} |
| 338 |
break; |
| 339 |
case BSQL_LAYER: |
| 340 |
if ( bi->sql_api ) { |
| 341 |
backsql_api *ba; |
| 342 |
struct berval bv; |
| 343 |
char *ptr; |
| 344 |
int i; |
| 345 |
for ( ba = bi->sql_api; ba; ba = ba->ba_next ) { |
| 346 |
bv.bv_len = strlen( ba->ba_name ); |
| 347 |
if ( ba->ba_argc ) { |
| 348 |
for ( i = 0; i<ba->ba_argc; i++ ) |
| 349 |
bv.bv_len += strlen( ba->ba_argv[i] ) + 3; |
| 350 |
} |
| 351 |
bv.bv_val = ch_malloc( bv.bv_len + 1 ); |
| 352 |
ptr = lutil_strcopy( bv.bv_val, ba->ba_name ); |
| 353 |
if ( ba->ba_argc ) { |
| 354 |
for ( i = 0; i<ba->ba_argc; i++ ) { |
| 355 |
*ptr++ = ' '; |
| 356 |
*ptr++ = '"'; |
| 357 |
ptr = lutil_strcopy( ptr, ba->ba_argv[i] ); |
| 358 |
*ptr++ = '"'; |
| 359 |
} |
| 360 |
} |
| 361 |
ber_bvarray_add( &c->rvalue_vals, &bv ); |
| 362 |
} |
| 363 |
} else { |
| 364 |
rc = 1; |
| 365 |
} |
| 366 |
break; |
| 367 |
case BSQL_ALIASING_KEYWORD: |
| 368 |
if ( !BER_BVISNULL( &bi->sql_aliasing )) { |
| 369 |
struct berval bv; |
| 370 |
bv = bi->sql_aliasing; |
| 371 |
bv.bv_len--; |
| 372 |
value_add_one( &c->rvalue_vals, &bv ); |
| 373 |
} else { |
| 374 |
rc = 1; |
| 375 |
} |
| 376 |
break; |
| 377 |
case BSQL_FETCH_ATTRS: |
| 378 |
if ( bi->sql_anlist || |
| 379 |
( bi->sql_flags & (BSQLF_FETCH_ALL_USERATTRS| |
| 380 |
BSQLF_FETCH_ALL_OPATTRS))) |
| 381 |
{ |
| 382 |
char buf[BUFSIZ*2], *ptr; |
| 383 |
struct berval bv; |
| 384 |
# define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr)) |
| 385 |
ptr = buf; |
| 386 |
if ( bi->sql_anlist ) { |
| 387 |
ptr = anlist_unparse( bi->sql_anlist, ptr, WHATSLEFT ); |
| 388 |
if ( ptr == NULL ) |
| 389 |
return 1; |
| 390 |
} |
| 391 |
if ( bi->sql_flags & BSQLF_FETCH_ALL_USERATTRS ) { |
| 392 |
if ( WHATSLEFT <= STRLENOF( ",*" )) return 1; |
| 393 |
if ( ptr != buf ) *ptr++ = ','; |
| 394 |
*ptr++ = '*'; |
| 395 |
} |
| 396 |
if ( bi->sql_flags & BSQLF_FETCH_ALL_OPATTRS ) { |
| 397 |
if ( WHATSLEFT <= STRLENOF( ",+" )) return 1; |
| 398 |
if ( ptr != buf ) *ptr++ = ','; |
| 399 |
*ptr++ = '+'; |
| 400 |
} |
| 401 |
bv.bv_val = buf; |
| 402 |
bv.bv_len = ptr - buf; |
| 403 |
value_add_one( &c->rvalue_vals, &bv ); |
| 404 |
} |
| 405 |
break; |
| 406 |
} |
| 407 |
return rc; |
| 408 |
} else if ( c->op == LDAP_MOD_DELETE ) { /* FIXME */ |
| 409 |
return -1; |
| 410 |
} |
| 411 |
|
| 412 |
switch( c->type ) { |
| 413 |
case BSQL_CONCAT_PATT: |
| 414 |
if ( backsql_split_pattern( c->argv[ 1 ], &bi->sql_concat_func, 2 ) ) { |
| 415 |
snprintf( c->cr_msg, sizeof( c->cr_msg ), |
| 416 |
"%s: unable to parse pattern \"%s\"", |
| 417 |
c->log, c->argv[ 1 ] ); |
| 418 |
Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg ); |
| 419 |
return -1; |
| 420 |
} |
| 421 |
bi->sql_concat_patt = c->value_string; |
| 422 |
break; |
| 423 |
case BSQL_CREATE_NEEDS_SEL: |
| 424 |
if ( c->value_int ) |
| 425 |
bi->sql_flags |= BSQLF_CREATE_NEEDS_SELECT; |
| 426 |
else |
| 427 |
bi->sql_flags &= ~BSQLF_CREATE_NEEDS_SELECT; |
| 428 |
break; |
| 429 |
case BSQL_UPPER_NEEDS_CAST: |
| 430 |
if ( c->value_int ) |
| 431 |
bi->sql_flags |= BSQLF_UPPER_NEEDS_CAST; |
| 432 |
else |
| 433 |
bi->sql_flags &= ~BSQLF_UPPER_NEEDS_CAST; |
| 434 |
break; |
| 435 |
case BSQL_HAS_LDAPINFO_DN_RU: |
| 436 |
bi->sql_flags |= BSQLF_DONTCHECK_LDAPINFO_DN_RU; |
| 437 |
if ( c->value_int ) |
| 438 |
bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU; |
| 439 |
else |
| 440 |
bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU; |
| 441 |
break; |
| 442 |
case BSQL_FAIL_IF_NO_MAPPING: |
| 443 |
if ( c->value_int ) |
| 444 |
bi->sql_flags |= BSQLF_FAIL_IF_NO_MAPPING; |
| 445 |
else |
| 446 |
bi->sql_flags &= ~BSQLF_FAIL_IF_NO_MAPPING; |
| 447 |
break; |
| 448 |
case BSQL_ALLOW_ORPHANS: |
| 449 |
if ( c->value_int ) |
| 450 |
bi->sql_flags |= BSQLF_ALLOW_ORPHANS; |
| 451 |
else |
| 452 |
bi->sql_flags &= ~BSQLF_ALLOW_ORPHANS; |
| 453 |
break; |
| 454 |
case BSQL_SUBTREE_SHORTCUT: |
| 455 |
if ( c->value_int ) |
| 456 |
bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT; |
| 457 |
else |
| 458 |
bi->sql_flags &= ~BSQLF_USE_SUBTREE_SHORTCUT; |
| 459 |
break; |
| 460 |
case BSQL_FETCH_ALL_ATTRS: |
| 461 |
if ( c->value_int ) |
| 462 |
bi->sql_flags |= BSQLF_FETCH_ALL_ATTRS; |
| 463 |
else |
| 464 |
bi->sql_flags &= ~BSQLF_FETCH_ALL_ATTRS; |
| 465 |
break; |
| 466 |
case BSQL_CHECK_SCHEMA: |
| 467 |
if ( c->value_int ) |
| 468 |
bi->sql_flags |= BSQLF_CHECK_SCHEMA; |
| 469 |
else |
| 470 |
bi->sql_flags &= ~BSQLF_CHECK_SCHEMA; |
| 471 |
break; |
| 472 |
case BSQL_AUTOCOMMIT: |
| 473 |
if ( c->value_int ) |
| 474 |
bi->sql_flags |= BSQLF_AUTOCOMMIT_ON; |
| 475 |
else |
| 476 |
bi->sql_flags &= ~BSQLF_AUTOCOMMIT_ON; |
| 477 |
break; |
| 478 |
case BSQL_BASE_OBJECT: |
| 479 |
if ( c->be->be_nsuffix == NULL ) { |
| 480 |
snprintf( c->cr_msg, sizeof( c->cr_msg ), |
| 481 |
"%s: suffix must be set", c->log ); |
| 482 |
Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg ); |
| 483 |
rc = ARG_BAD_CONF; |
| 484 |
break; |
| 485 |
} |
| 486 |
if ( bi->sql_baseObject ) { |
| 487 |
Debug( LDAP_DEBUG_CONFIG, |
| 488 |
"%s: " |
| 489 |
"\"baseObject\" already provided (will be overwritten)\n", |
| 490 |
c->log ); |
| 491 |
entry_free( bi->sql_baseObject ); |
| 492 |
} |
| 493 |
if ( c->argc == 2 && !strcmp( c->argv[1], "TRUE" )) |
| 494 |
c->argc = 1; |
| 495 |
switch( c->argc ) { |
| 496 |
case 1: |
| 497 |
return create_baseObject( c->be, c->fname, c->lineno ); |
| 498 |
|
| 499 |
case 2: |
| 500 |
rc = read_baseObject( c->be, c->argv[ 1 ] ); |
| 501 |
if ( rc == 0 ) { |
| 502 |
ch_free( bi->sql_base_ob_file ); |
| 503 |
bi->sql_base_ob_file = c->value_string; |
| 504 |
} |
| 505 |
return rc; |
| 506 |
|
| 507 |
default: |
| 508 |
snprintf( c->cr_msg, sizeof( c->cr_msg ), |
| 509 |
"%s: trailing values in directive", c->log ); |
| 510 |
Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg ); |
| 511 |
return 1; |
| 512 |
} |
| 513 |
break; |
| 514 |
case BSQL_LAYER: |
| 515 |
if ( backsql_api_config( bi, c->argv[ 1 ], c->argc - 2, &c->argv[ 2 ] ) ) |
| 516 |
{ |
| 517 |
snprintf( c->cr_msg, sizeof( c->cr_msg ), |
| 518 |
"%s: unable to load sql layer", c->log ); |
| 519 |
Debug( LDAP_DEBUG_ANY, "%s \"%s\"\n", |
| 520 |
c->cr_msg, c->argv[1] ); |
| 521 |
return 1; |
| 522 |
} |
| 523 |
break; |
| 524 |
case BSQL_ALIASING_KEYWORD: |
| 525 |
if ( ! BER_BVISNULL( &bi->sql_aliasing ) ) { |
| 526 |
ch_free( bi->sql_aliasing.bv_val ); |
| 527 |
} |
| 528 |
|
| 529 |
ber_str2bv( c->argv[ 1 ], strlen( c->argv[ 1 ] ) + 1, 1, |
| 530 |
&bi->sql_aliasing ); |
| 531 |
/* add a trailing space... */ |
| 532 |
bi->sql_aliasing.bv_val[ bi->sql_aliasing.bv_len - 1] = ' '; |
| 533 |
break; |
| 534 |
case BSQL_FETCH_ATTRS: { |
| 535 |
char *str, *s, *next; |
| 536 |
const char *delimstr = ","; |
| 537 |
|
| 538 |
str = ch_strdup( c->argv[ 1 ] ); |
| 539 |
for ( s = ldap_pvt_strtok( str, delimstr, &next ); |
| 540 |
s != NULL; |
| 541 |
s = ldap_pvt_strtok( NULL, delimstr, &next ) ) |
| 542 |
{ |
| 543 |
if ( strlen( s ) == 1 ) { |
| 544 |
if ( *s == '*' ) { |
| 545 |
bi->sql_flags |= BSQLF_FETCH_ALL_USERATTRS; |
| 546 |
c->argv[ 1 ][ s - str ] = ','; |
| 547 |
|
| 548 |
} else if ( *s == '+' ) { |
| 549 |
bi->sql_flags |= BSQLF_FETCH_ALL_OPATTRS; |
| 550 |
c->argv[ 1 ][ s - str ] = ','; |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
ch_free( str ); |
| 555 |
bi->sql_anlist = str2anlist( bi->sql_anlist, c->argv[ 1 ], delimstr ); |
| 556 |
if ( bi->sql_anlist == NULL ) { |
| 557 |
return -1; |
| 558 |
} |
| 559 |
} |
| 560 |
break; |
| 561 |
} |
| 562 |
return rc; |
| 563 |
} |
| 564 |
|
| 565 |
/* |
| 566 |
* Read the entries specified in fname and merge the attributes |
| 567 |
* to the user defined baseObject entry. Note that if we find any errors |
| 568 |
* what so ever, we will discard the entire entries, print an |
| 569 |
* error message and return. |
| 570 |
*/ |
| 571 |
static int |
| 572 |
read_baseObject( |
| 573 |
BackendDB *be, |
| 574 |
const char *fname ) |
| 575 |
{ |
| 576 |
backsql_info *bi = (backsql_info *)be->be_private; |
| 577 |
LDIFFP *fp; |
| 578 |
int rc = 0, lmax = 0, ldifrc; |
| 579 |
unsigned long lineno = 0; |
| 580 |
char *buf = NULL; |
| 581 |
|
| 582 |
assert( fname != NULL ); |
| 583 |
|
| 584 |
fp = ldif_open( fname, "r" ); |
| 585 |
if ( fp == NULL ) { |
| 586 |
Debug( LDAP_DEBUG_ANY, |
| 587 |
"could not open back-sql baseObject " |
| 588 |
"attr file \"%s\" - absolute path?\n", |
| 589 |
fname ); |
| 590 |
perror( fname ); |
| 591 |
return LDAP_OTHER; |
| 592 |
} |
| 593 |
|
| 594 |
bi->sql_baseObject = entry_alloc(); |
| 595 |
if ( bi->sql_baseObject == NULL ) { |
| 596 |
Debug( LDAP_DEBUG_ANY, |
| 597 |
"read_baseObject_file: entry_alloc failed" ); |
| 598 |
ldif_close( fp ); |
| 599 |
return LDAP_NO_MEMORY; |
| 600 |
} |
| 601 |
bi->sql_baseObject->e_name = be->be_suffix[0]; |
| 602 |
bi->sql_baseObject->e_nname = be->be_nsuffix[0]; |
| 603 |
bi->sql_baseObject->e_attrs = NULL; |
| 604 |
|
| 605 |
while (( ldifrc = ldif_read_record( fp, &lineno, &buf, &lmax )) > 0 ) { |
| 606 |
Entry *e = str2entry( buf ); |
| 607 |
Attribute *a; |
| 608 |
|
| 609 |
if( e == NULL ) { |
| 610 |
fprintf( stderr, "back-sql baseObject: " |
| 611 |
"could not parse entry (line=%lu)\n", |
| 612 |
lineno ); |
| 613 |
rc = LDAP_OTHER; |
| 614 |
break; |
| 615 |
} |
| 616 |
|
| 617 |
/* make sure the DN is the database's suffix */ |
| 618 |
if ( !be_issuffix( be, &e->e_nname ) ) { |
| 619 |
fprintf( stderr, |
| 620 |
"back-sql: invalid baseObject - " |
| 621 |
"dn=\"%s\" (line=%lu)\n", |
| 622 |
e->e_name.bv_val, lineno ); |
| 623 |
entry_free( e ); |
| 624 |
rc = LDAP_OTHER; |
| 625 |
break; |
| 626 |
} |
| 627 |
|
| 628 |
/* |
| 629 |
* we found a valid entry, so walk thru all the attributes in the |
| 630 |
* entry, and add each attribute type and description to baseObject |
| 631 |
*/ |
| 632 |
for ( a = e->e_attrs; a != NULL; a = a->a_next ) { |
| 633 |
if ( attr_merge( bi->sql_baseObject, a->a_desc, |
| 634 |
a->a_vals, |
| 635 |
( a->a_nvals == a->a_vals ) ? |
| 636 |
NULL : a->a_nvals ) ) |
| 637 |
{ |
| 638 |
rc = LDAP_OTHER; |
| 639 |
break; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
entry_free( e ); |
| 644 |
if ( rc ) { |
| 645 |
break; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
if ( ldifrc < 0 ) |
| 650 |
rc = LDAP_OTHER; |
| 651 |
|
| 652 |
if ( rc ) { |
| 653 |
entry_free( bi->sql_baseObject ); |
| 654 |
bi->sql_baseObject = NULL; |
| 655 |
} |
| 656 |
|
| 657 |
ch_free( buf ); |
| 658 |
|
| 659 |
ldif_close( fp ); |
| 660 |
|
| 661 |
Debug( LDAP_DEBUG_CONFIG, "back-sql baseObject file \"%s\" read.\n", |
| 662 |
fname ); |
| 663 |
|
| 664 |
return rc; |
| 665 |
} |
| 666 |
|
| 667 |
static int |
| 668 |
create_baseObject( |
| 669 |
BackendDB *be, |
| 670 |
const char *fname, |
| 671 |
int lineno ) |
| 672 |
{ |
| 673 |
backsql_info *bi = (backsql_info *)be->be_private; |
| 674 |
LDAPRDN rdn; |
| 675 |
char *p; |
| 676 |
int rc, iAVA; |
| 677 |
char buf[1024]; |
| 678 |
|
| 679 |
snprintf( buf, sizeof(buf), |
| 680 |
"dn: %s\n" |
| 681 |
"objectClass: extensibleObject\n" |
| 682 |
"description: builtin baseObject for back-sql\n" |
| 683 |
"description: all entries mapped " |
| 684 |
"in table \"ldap_entries\" " |
| 685 |
"must have " |
| 686 |
"\"" BACKSQL_BASEOBJECT_IDSTR "\" " |
| 687 |
"in the \"parent\" column", |
| 688 |
be->be_suffix[0].bv_val ); |
| 689 |
|
| 690 |
bi->sql_baseObject = str2entry( buf ); |
| 691 |
if ( bi->sql_baseObject == NULL ) { |
| 692 |
Debug( LDAP_DEBUG_TRACE, |
| 693 |
"<==backsql_db_config (%s line %d): " |
| 694 |
"unable to parse baseObject entry\n", |
| 695 |
fname, lineno ); |
| 696 |
return 1; |
| 697 |
} |
| 698 |
|
| 699 |
if ( BER_BVISEMPTY( &be->be_suffix[ 0 ] ) ) { |
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
rc = ldap_bv2rdn( &be->be_suffix[ 0 ], &rdn, (char **)&p, |
| 704 |
LDAP_DN_FORMAT_LDAP ); |
| 705 |
if ( rc != LDAP_SUCCESS ) { |
| 706 |
Debug(LDAP_DEBUG_TRACE, |
| 707 |
"<==backsql_db_config (%s line %d): unable to extract RDN " "from baseObject DN \"%s\" (%d: %s)\n", |
| 708 |
fname, lineno, be->be_suffix[0].bv_val, rc, |
| 709 |
ldap_err2string(rc) ); |
| 710 |
return 1; |
| 711 |
} |
| 712 |
|
| 713 |
for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) { |
| 714 |
LDAPAVA *ava = rdn[ iAVA ]; |
| 715 |
AttributeDescription *ad = NULL; |
| 716 |
slap_syntax_transform_func *transf = NULL; |
| 717 |
struct berval bv = BER_BVNULL; |
| 718 |
const char *text = NULL; |
| 719 |
|
| 720 |
assert( ava != NULL ); |
| 721 |
|
| 722 |
rc = slap_bv2ad( &ava->la_attr, &ad, &text ); |
| 723 |
if ( rc != LDAP_SUCCESS ) { |
| 724 |
Debug(LDAP_DEBUG_TRACE, |
| 725 |
"<==backsql_db_config (%s line %d): AttributeDescription of naming " "attribute #%d from baseObject " "DN \"%s\": %d: %s\n", |
| 726 |
fname, lineno, iAVA, be->be_suffix[0].bv_val, |
| 727 |
rc, ldap_err2string(rc) ); |
| 728 |
return 1; |
| 729 |
} |
| 730 |
|
| 731 |
transf = ad->ad_type->sat_syntax->ssyn_pretty; |
| 732 |
if ( transf ) { |
| 733 |
/* |
| 734 |
* transform value by pretty function |
| 735 |
* if value is empty, use empty_bv |
| 736 |
*/ |
| 737 |
rc = ( *transf )( ad->ad_type->sat_syntax, |
| 738 |
ava->la_value.bv_len |
| 739 |
? &ava->la_value |
| 740 |
: (struct berval *) &slap_empty_bv, |
| 741 |
&bv, NULL ); |
| 742 |
|
| 743 |
if ( rc != LDAP_SUCCESS ) { |
| 744 |
Debug(LDAP_DEBUG_TRACE, |
| 745 |
"<==backsql_db_config (%s line %d): " "prettying of attribute #%d " "from baseObject " "DN \"%s\" failed: %d: %s\n", |
| 746 |
fname, lineno, iAVA, |
| 747 |
be->be_suffix[0].bv_val, rc, |
| 748 |
ldap_err2string(rc) ); |
| 749 |
return 1; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
if ( !BER_BVISNULL( &bv ) ) { |
| 754 |
if ( ava->la_flags & LDAP_AVA_FREE_VALUE ) { |
| 755 |
ber_memfree( ava->la_value.bv_val ); |
| 756 |
} |
| 757 |
ava->la_value = bv; |
| 758 |
ava->la_flags |= LDAP_AVA_FREE_VALUE; |
| 759 |
} |
| 760 |
|
| 761 |
attr_merge_normalize_one( bi->sql_baseObject, |
| 762 |
ad, &ava->la_value, NULL ); |
| 763 |
} |
| 764 |
|
| 765 |
ldap_rdnfree( rdn ); |
| 766 |
|
| 767 |
return 0; |
| 768 |
} |
| 769 |
|
| 770 |
int backsql_init_cf( BackendInfo *bi ) |
| 771 |
{ |
| 772 |
int rc; |
| 773 |
|
| 774 |
bi->bi_cf_ocs = sqlocs; |
| 775 |
rc = config_register_schema( sqlcfg, sqlocs ); |
| 776 |
if ( rc ) return rc; |
| 777 |
return 0; |
| 778 |
} |