config.c 23.2 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
3
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1999-2015 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Portions Copyright 1999 Dmitry Kovalev.
6
 * Portions Copyright 2002 Pierangelo Masarati.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
 * Portions Copyright 2004 Mark Adamson.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9
10
11
12
13
14
15
16
17
18
19
 * 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 Dmitry Kovalev for inclusion
20
21
 * by OpenLDAP Software.  Additional significant contributors include
 * Pierangelo Masarati.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
22
23
24
25
26
 */

#include "portable.h"

#include <stdio.h>
27
#include "ac/string.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
28
#include <sys/types.h>
29

Dmitry Kovalev's avatar
Dmitry Kovalev committed
30
#include "slap.h"
31
#include "config.h"
32
#include "ldif.h"
33
#include "lutil.h"
34
#include "proto-sql.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
35

36
37
38
39
40
41
42
43
44
45
46
static int
create_baseObject(
	BackendDB	*be,
	const char	*fname,
	int		lineno );

static int
read_baseObject(
	BackendDB	*be,
	const char	*fname );

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
static ConfigDriver sql_cf_gen;

enum {
	BSQL_CONCAT_PATT = 1,
	BSQL_CREATE_NEEDS_SEL,
	BSQL_UPPER_NEEDS_CAST,
	BSQL_HAS_LDAPINFO_DN_RU,
	BSQL_FAIL_IF_NO_MAPPING,
	BSQL_ALLOW_ORPHANS,
	BSQL_BASE_OBJECT,
	BSQL_LAYER,
	BSQL_SUBTREE_SHORTCUT,
	BSQL_FETCH_ALL_ATTRS,
	BSQL_FETCH_ATTRS,
	BSQL_CHECK_SCHEMA,
	BSQL_ALIASING_KEYWORD,
	BSQL_AUTOCOMMIT
};

static ConfigTable sqlcfg[] = {
	{ "dbhost", "hostname", 2, 2, 0, ARG_STRING|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_dbhost),
		"( OLcfgDbAt:6.1 NAME 'olcDbHost' "
			"DESC 'Hostname of SQL server' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "dbname", "name", 2, 2, 0, ARG_STRING|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_dbname),
		"( OLcfgDbAt:6.2 NAME 'olcDbName' "
			"DESC 'Name of SQL database' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "dbuser", "username", 2, 2, 0, ARG_STRING|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_dbuser),
		"( OLcfgDbAt:6.3 NAME 'olcDbUser' "
			"DESC 'Username for SQL session' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "dbpasswd", "password", 2, 2, 0, ARG_STRING|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_dbpasswd),
		"( OLcfgDbAt:6.4 NAME 'olcDbPass' "
			"DESC 'Password for SQL session' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "concat_pattern", "pattern", 2, 2, 0,
		ARG_STRING|ARG_MAGIC|BSQL_CONCAT_PATT, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.20 NAME 'olcSqlConcatPattern' "
			"DESC 'Pattern used to concatenate strings' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
92
	{ "subtree_cond", "SQL expression", 2, 0, 0, ARG_BERVAL|ARG_QUOTE|ARG_OFFSET,
93
94
95
96
		(void *)offsetof(struct backsql_info, sql_subtree_cond),
		"( OLcfgDbAt:6.21 NAME 'olcSqlSubtreeCond' "
			"DESC 'Where-clause template for a subtree search condition' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
97
	{ "children_cond", "SQL expression", 2, 0, 0, ARG_BERVAL|ARG_QUOTE|ARG_OFFSET,
98
99
100
101
		(void *)offsetof(struct backsql_info, sql_children_cond),
		"( OLcfgDbAt:6.22 NAME 'olcSqlChildrenCond' "
			"DESC 'Where-clause template for a children search condition' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
102
	{ "dn_match_cond", "SQL expression", 2, 0, 0, ARG_BERVAL|ARG_QUOTE|ARG_OFFSET,
103
104
105
106
		(void *)offsetof(struct backsql_info, sql_dn_match_cond),
		"( OLcfgDbAt:6.23 NAME 'olcSqlDnMatchCond' "
			"DESC 'Where-clause template for a DN match search condition' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
107
	{ "oc_query", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET,
108
109
110
111
		(void *)offsetof(struct backsql_info, sql_oc_query),
		"( OLcfgDbAt:6.24 NAME 'olcSqlOcQuery' "
			"DESC 'Query used to collect objectClass mapping data' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
112
	{ "at_query", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET,
113
114
115
116
		(void *)offsetof(struct backsql_info, sql_at_query),
		"( OLcfgDbAt:6.25 NAME 'olcSqlAtQuery' "
			"DESC 'Query used to collect attributeType mapping data' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
117
	{ "insentry_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET,
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
		(void *)offsetof(struct backsql_info, sql_insentry_stmt),
		"( OLcfgDbAt:6.26 NAME 'olcSqlInsEntryStmt' "
			"DESC 'Statement used to insert a new entry' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "create_needs_select", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_CREATE_NEEDS_SEL, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.27 NAME 'olcSqlCreateNeedsSelect' "
			"DESC 'Whether entry creation needs a subsequent select' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "upper_func", "SQL function name", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_upper_func),
		"( OLcfgDbAt:6.28 NAME 'olcSqlUpperFunc' "
			"DESC 'Function that converts a value to uppercase' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "upper_needs_cast", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_UPPER_NEEDS_CAST, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.29 NAME 'olcSqlUpperNeedsCast' "
			"DESC 'Whether olcSqlUpperFunc needs an explicit cast' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "strcast_func", "SQL function name", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_strcast_func),
		"( OLcfgDbAt:6.30 NAME 'olcSqlStrcastFunc' "
			"DESC 'Function that converts a value to a string' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
142
	{ "delentry_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET,
143
144
145
146
		(void *)offsetof(struct backsql_info, sql_delentry_stmt),
		"( OLcfgDbAt:6.31 NAME 'olcSqlDelEntryStmt' "
			"DESC 'Statement used to delete an existing entry' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
147
	{ "renentry_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET,
148
149
150
151
		(void *)offsetof(struct backsql_info, sql_renentry_stmt),
		"( OLcfgDbAt:6.32 NAME 'olcSqlRenEntryStmt' "
			"DESC 'Statement used to rename an entry' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
152
	{ "delobjclasses_stmt", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET,
153
154
		(void *)offsetof(struct backsql_info, sql_delobjclasses_stmt),
		"( OLcfgDbAt:6.33 NAME 'olcSqlDelObjclassesStmt' "
Pierangelo Masarati's avatar
Pierangelo Masarati committed
155
			"DESC 'Statement used to delete the ID of an entry' "
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
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "has_ldapinfo_dn_ru", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_HAS_LDAPINFO_DN_RU, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.34 NAME 'olcSqlHasLDAPinfoDnRu' "
			"DESC 'Whether the dn_ru column is present' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "fail_if_no_mapping", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_FAIL_IF_NO_MAPPING, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.35 NAME 'olcSqlFailIfNoMapping' "
			"DESC 'Whether to fail on unknown attribute mappings' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "allow_orphans", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_ALLOW_ORPHANS, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.36 NAME 'olcSqlAllowOrphans' "
			"DESC 'Whether to allow adding entries with no parent' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "baseobject", "[file]", 1, 2, 0,
		ARG_STRING|ARG_MAGIC|BSQL_BASE_OBJECT, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.37 NAME 'olcSqlBaseObject' "
			"DESC 'Manage an in-memory baseObject entry' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "sqllayer", "name", 2, 0, 0,
		ARG_MAGIC|BSQL_LAYER, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.38 NAME 'olcSqlLayer' "
			"DESC 'Helper used to map DNs between LDAP and SQL' "
			"SYNTAX OMsDirectoryString )", NULL, NULL },
	{ "use_subtree_shortcut", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_SUBTREE_SHORTCUT, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.39 NAME 'olcSqlUseSubtreeShortcut' "
			"DESC 'Collect all entries when searchBase is DB suffix' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "fetch_all_attrs", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_FETCH_ALL_ATTRS, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.40 NAME 'olcSqlFetchAllAttrs' "
			"DESC 'Require all attributes to always be loaded' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "fetch_attrs", "attrlist", 2, 0, 0,
		ARG_MAGIC|BSQL_FETCH_ATTRS, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.41 NAME 'olcSqlFetchAttrs' "
			"DESC 'Set of attributes to always fetch' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "check_schema", "yes|no", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|BSQL_CHECK_SCHEMA, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.42 NAME 'olcSqlCheckSchema' "
			"DESC 'Check schema after modifications' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
	{ "aliasing_keyword", "string", 2, 2, 0,
		ARG_STRING|ARG_MAGIC|BSQL_ALIASING_KEYWORD, (void *)sql_cf_gen,
		"( OLcfgDbAt:6.43 NAME 'olcSqlAliasingKeyword' "
			"DESC 'The aliasing keyword' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "aliasing_quote", "string", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_aliasing_quote),
		"( OLcfgDbAt:6.44 NAME 'olcSqlAliasingQuote' "
			"DESC 'Quoting char of the aliasing keyword' "
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
	{ "autocommit", "yes|no", 2, 2, 0,
Howard Chu's avatar
Howard Chu committed
213
		ARG_ON_OFF|ARG_MAGIC|BSQL_AUTOCOMMIT, (void *)sql_cf_gen,
214
215
		"( OLcfgDbAt:6.45 NAME 'olcSqlAutocommit' "
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
216
217
218
	{ "id_query", "SQL expression", 2, 0, 0, ARG_STRING|ARG_QUOTE|ARG_OFFSET,
		(void *)offsetof(struct backsql_info, sql_id_query),
		"( OLcfgDbAt:6.46 NAME 'olcSqlIdQuery' "
Howard Chu's avatar
Howard Chu committed
219
			"DESC 'Query used to collect entryID mapping data' "
220
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
221
222
223
224
225
	{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
		NULL, NULL, NULL, NULL }
};

static ConfigOCs sqlocs[] = {
226
	{
227
228
229
230
231
232
233
234
235
		"( OLcfgDbOc:6.1 "
		"NAME 'olcSqlConfig' "
		"DESC 'SQL backend configuration' "
		"SUP olcDatabaseConfig "
		"MUST olcDbName "
		"MAY ( olcDbHost $ olcDbUser $ olcDbPass $ olcSqlConcatPattern $ "
		"olcSqlSubtreeCond $ olcsqlChildrenCond $ olcSqlDnMatchCond $ "
		"olcSqlOcQuery $ olcSqlAtQuery $ olcSqlInsEntryStmt $ "
		"olcSqlCreateNeedsSelect $ olcSqlUpperFunc $ olcSqlUpperNeedsCast $ "
Pierangelo Masarati's avatar
Pierangelo Masarati committed
236
		"olcSqlStrCastFunc $ olcSqlDelEntryStmt $ olcSqlRenEntryStmt $ "
237
238
239
240
		"olcSqlDelObjClassesStmt $ olcSqlHasLDAPInfoDnRu $ "
		"olcSqlFailIfNoMapping $ olcSqlAllowOrphans $ olcSqlBaseObject $ "
		"olcSqlLayer $ olcSqlUseSubtreeShortcut $ olcSqlFetchAllAttrs $ "
		"olcSqlFetchAttrs $ olcSqlCheckSchema $ olcSqlAliasingKeyword $ "
Howard Chu's avatar
Howard Chu committed
241
		"olcSqlAliasingQuote $ olcSqlAutocommit $ olcSqlIdQuery ) )",
242
243
244
			Cft_Database, sqlcfg },
	{ NULL, Cft_Abstract, NULL }
};
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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
static int
sql_cf_gen( ConfigArgs *c )
{
	backsql_info 	*bi = (backsql_info *)c->be->be_private;
	int rc = 0;

	if ( c->op == SLAP_CONFIG_EMIT ) {
		switch( c->type ) {
		case BSQL_CONCAT_PATT:
			if ( bi->sql_concat_patt ) {
				c->value_string = ch_strdup( bi->sql_concat_patt );
			} else {
				rc = 1;
			}
			break;
		case BSQL_CREATE_NEEDS_SEL:
			if ( bi->sql_flags & BSQLF_CREATE_NEEDS_SELECT )
				c->value_int = 1;
			break;
		case BSQL_UPPER_NEEDS_CAST:
			if ( bi->sql_flags & BSQLF_UPPER_NEEDS_CAST )
				c->value_int = 1;
			break;
		case BSQL_HAS_LDAPINFO_DN_RU:
			if ( !(bi->sql_flags & BSQLF_DONTCHECK_LDAPINFO_DN_RU) )
				return 1;
			if ( bi->sql_flags & BSQLF_HAS_LDAPINFO_DN_RU )
				c->value_int = 1;
			break;
		case BSQL_FAIL_IF_NO_MAPPING:
			if ( bi->sql_flags & BSQLF_FAIL_IF_NO_MAPPING )
				c->value_int = 1;
			break;
		case BSQL_ALLOW_ORPHANS:
			if ( bi->sql_flags & BSQLF_ALLOW_ORPHANS )
				c->value_int = 1;
			break;
		case BSQL_SUBTREE_SHORTCUT:
			if ( bi->sql_flags & BSQLF_USE_SUBTREE_SHORTCUT )
				c->value_int = 1;
			break;
		case BSQL_FETCH_ALL_ATTRS:
			if ( bi->sql_flags & BSQLF_FETCH_ALL_ATTRS )
				c->value_int = 1;
			break;
		case BSQL_CHECK_SCHEMA:
			if ( bi->sql_flags & BSQLF_CHECK_SCHEMA )
				c->value_int = 1;
			break;
		case BSQL_AUTOCOMMIT:
			if ( bi->sql_flags & BSQLF_AUTOCOMMIT_ON )
				c->value_int = 1;
			break;
		case BSQL_BASE_OBJECT:
			if ( bi->sql_base_ob_file ) {
				c->value_string = ch_strdup( bi->sql_base_ob_file );
			} else if ( bi->sql_baseObject ) {
				c->value_string = ch_strdup( "TRUE" );
			} else {
				rc = 1;
			}
			break;
		case BSQL_LAYER:
			if ( bi->sql_api ) {
				backsql_api *ba;
				struct berval bv;
				char *ptr;
				int i;
				for ( ba = bi->sql_api; ba; ba = ba->ba_next ) {
					bv.bv_len = strlen( ba->ba_name );
					if ( ba->ba_argc ) {
						for ( i = 0; i<ba->ba_argc; i++ )
							bv.bv_len += strlen( ba->ba_argv[i] ) + 3;
					}
					bv.bv_val = ch_malloc( bv.bv_len + 1 );
					ptr = lutil_strcopy( bv.bv_val, ba->ba_name );
					if ( ba->ba_argc ) {
						for ( i = 0; i<ba->ba_argc; i++ ) {
							*ptr++ = ' ';
							*ptr++ = '"';
							ptr = lutil_strcopy( ptr, ba->ba_argv[i] );
							*ptr++ = '"';
						}
					}
					ber_bvarray_add( &c->rvalue_vals, &bv );
				}
			} else {
				rc = 1;
			}
			break;
		case BSQL_ALIASING_KEYWORD:
			if ( !BER_BVISNULL( &bi->sql_aliasing )) {
				struct berval bv;
				bv = bi->sql_aliasing;
				bv.bv_len--;
				value_add_one( &c->rvalue_vals, &bv );
			} else {
				rc = 1;
			}
			break;
		case BSQL_FETCH_ATTRS:
			if ( bi->sql_anlist ||
				( bi->sql_flags & (BSQLF_FETCH_ALL_USERATTRS|
								   BSQLF_FETCH_ALL_OPATTRS)))
			{
				char buf[BUFSIZ*2], *ptr;
				struct berval bv;
#   define WHATSLEFT    ((ber_len_t) (&buf[sizeof( buf )] - ptr))
				ptr = buf;
				if ( bi->sql_anlist ) {
					ptr = anlist_unparse( bi->sql_anlist, ptr, WHATSLEFT );
					if ( ptr == NULL )
						return 1;
				}
				if ( bi->sql_flags & BSQLF_FETCH_ALL_USERATTRS ) {
					if ( WHATSLEFT <= STRLENOF( ",*" )) return 1;
					if ( ptr != buf ) *ptr++ = ',';
					*ptr++ = '*';
				}
				if ( bi->sql_flags & BSQLF_FETCH_ALL_OPATTRS ) {
					if ( WHATSLEFT <= STRLENOF( ",+" )) return 1;
					if ( ptr != buf ) *ptr++ = ',';
					*ptr++ = '+';
				}
				bv.bv_val = buf;
				bv.bv_len = ptr - buf;
				value_add_one( &c->rvalue_vals, &bv );
			}
			break;
375
		}
376
377
378
379
		return rc;
	} else if ( c->op == LDAP_MOD_DELETE ) {	/* FIXME */
		return -1;
	}
380

381
382
383
384
385
386
387
388
389
390
391
392
393
	switch( c->type ) {
	case BSQL_CONCAT_PATT:
		if ( backsql_split_pattern( c->argv[ 1 ], &bi->sql_concat_func, 2 ) ) {
			snprintf( c->cr_msg, sizeof( c->cr_msg ),
				"%s: unable to parse pattern \"%s\"",
				c->log, c->argv[ 1 ] );
			Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
			return -1;
		}
		bi->sql_concat_patt = c->value_string;
		break;
	case BSQL_CREATE_NEEDS_SEL:
		if ( c->value_int )
394
			bi->sql_flags |= BSQLF_CREATE_NEEDS_SELECT;
395
		else
396
			bi->sql_flags &= ~BSQLF_CREATE_NEEDS_SELECT;
397
398
399
		break;
	case BSQL_UPPER_NEEDS_CAST:
		if ( c->value_int )
400
			bi->sql_flags |= BSQLF_UPPER_NEEDS_CAST;
401
		else
402
			bi->sql_flags &= ~BSQLF_UPPER_NEEDS_CAST;
403
404
405
406
		break;
	case BSQL_HAS_LDAPINFO_DN_RU:
		bi->sql_flags |= BSQLF_DONTCHECK_LDAPINFO_DN_RU;
		if ( c->value_int )
407
			bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
408
		else
409
			bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
410
411
412
		break;
	case BSQL_FAIL_IF_NO_MAPPING:
		if ( c->value_int )
413
			bi->sql_flags |= BSQLF_FAIL_IF_NO_MAPPING;
414
		else
415
			bi->sql_flags &= ~BSQLF_FAIL_IF_NO_MAPPING;
416
417
418
		break;
	case BSQL_ALLOW_ORPHANS:
		if ( c->value_int )
419
			bi->sql_flags |= BSQLF_ALLOW_ORPHANS;
420
		else
421
			bi->sql_flags &= ~BSQLF_ALLOW_ORPHANS;
422
423
424
425
426
427
428
429
430
431
432
		break;
	case BSQL_SUBTREE_SHORTCUT:
		if ( c->value_int )
			bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
		else
			bi->sql_flags &= ~BSQLF_USE_SUBTREE_SHORTCUT;
		break;
	case BSQL_FETCH_ALL_ATTRS:
		if ( c->value_int )
			bi->sql_flags |= BSQLF_FETCH_ALL_ATTRS;
		else
Howard Chu's avatar
Howard Chu committed
433
			bi->sql_flags &= ~BSQLF_FETCH_ALL_ATTRS;
434
435
436
437
438
		break;
	case BSQL_CHECK_SCHEMA:
		if ( c->value_int )
			bi->sql_flags |= BSQLF_CHECK_SCHEMA;
		else
Howard Chu's avatar
Howard Chu committed
439
			bi->sql_flags &= ~BSQLF_CHECK_SCHEMA;
440
441
442
443
444
		break;
	case BSQL_AUTOCOMMIT:
		if ( c->value_int )
			bi->sql_flags |= BSQLF_AUTOCOMMIT_ON;
		else
Howard Chu's avatar
Howard Chu committed
445
			bi->sql_flags &= ~BSQLF_AUTOCOMMIT_ON;
446
447
448
449
450
451
452
453
		break;
	case BSQL_BASE_OBJECT:
		if ( c->be->be_nsuffix == NULL ) {
			snprintf( c->cr_msg, sizeof( c->cr_msg ),
				"%s: suffix must be set", c->log );
			Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
			rc = ARG_BAD_CONF;
			break;
454
455
		}
		if ( bi->sql_baseObject ) {
456
457
			Debug( LDAP_DEBUG_CONFIG,
				"%s: "
458
				"\"baseObject\" already provided (will be overwritten)\n",
459
				c->log, 0, 0 );
460
461
			entry_free( bi->sql_baseObject );
		}
462
463
464
		if ( c->argc == 2 && !strcmp( c->argv[1], "TRUE" ))
			c->argc = 1;
		switch( c->argc ) {
465
		case 1:
466
			return create_baseObject( c->be, c->fname, c->lineno );
467
468

		case 2:
469
470
471
472
473
474
			rc = read_baseObject( c->be, c->argv[ 1 ] );
			if ( rc == 0 ) {
				ch_free( bi->sql_base_ob_file );
				bi->sql_base_ob_file = c->value_string;
			}
			return rc;
475
476

		default:
477
478
479
			snprintf( c->cr_msg, sizeof( c->cr_msg ),
				"%s: trailing values in directive", c->log );
			Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
480
481
			return 1;
		}
482
483
484
		break;
	case BSQL_LAYER:
		if ( backsql_api_config( bi, c->argv[ 1 ], c->argc - 2, &c->argv[ 2 ] ) )
485
		{
486
487
488
489
			snprintf( c->cr_msg, sizeof( c->cr_msg ),
				"%s: unable to load sql layer", c->log );
			Debug( LDAP_DEBUG_ANY, "%s \"%s\"\n",
				c->cr_msg, c->argv[1], 0 );
490
491
			return 1;
		}
492
493
494
495
		break;
	case BSQL_ALIASING_KEYWORD:
		if ( ! BER_BVISNULL( &bi->sql_aliasing ) ) {
			ch_free( bi->sql_aliasing.bv_val );
496
497
		}

498
499
500
501
502
503
		ber_str2bv( c->argv[ 1 ], strlen( c->argv[ 1 ] ) + 1, 1,
			&bi->sql_aliasing );
		/* add a trailing space... */
		bi->sql_aliasing.bv_val[ bi->sql_aliasing.bv_len - 1] = ' ';
		break;
	case BSQL_FETCH_ATTRS: {
504
505
		char		*str, *s, *next;
		const char	*delimstr = ",";
506

507
		str = ch_strdup( c->argv[ 1 ] );
508
509
510
511
512
513
514
		for ( s = ldap_pvt_strtok( str, delimstr, &next );
				s != NULL;
				s = ldap_pvt_strtok( NULL, delimstr, &next ) )
		{
			if ( strlen( s ) == 1 ) {
				if ( *s == '*' ) {
					bi->sql_flags |= BSQLF_FETCH_ALL_USERATTRS;
515
					c->argv[ 1 ][ s - str ] = ',';
516
517
518

				} else if ( *s == '+' ) {
					bi->sql_flags |= BSQLF_FETCH_ALL_OPATTRS;
519
					c->argv[ 1 ][ s - str ] = ',';
520
521
522
523
				}
			}
		}
		ch_free( str );
524
		bi->sql_anlist = str2anlist( bi->sql_anlist, c->argv[ 1 ], delimstr );
525
526
527
		if ( bi->sql_anlist == NULL ) {
			return -1;
		}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
528
		}
529
		break;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
530
	}
531
	return rc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
532
}
Dmitry Kovalev's avatar
Dmitry Kovalev committed
533

534
535
536
537
538
539
540
541
542
543
544
545
/*
 * Read the entries specified in fname and merge the attributes
 * to the user defined baseObject entry. Note that if we find any errors
 * what so ever, we will discard the entire entries, print an
 * error message and return.
 */
static int
read_baseObject( 
	BackendDB	*be,
	const char	*fname )
{
	backsql_info 	*bi = (backsql_info *)be->be_private;
546
	LDIFFP		*fp;
547
548
	int		rc = 0, lmax = 0, ldifrc;
	unsigned long	lineno = 0;
549
550
	char		*buf = NULL;

551
	assert( fname != NULL );
552

553
	fp = ldif_open( fname, "r" );
554
555
	if ( fp == NULL ) {
		Debug( LDAP_DEBUG_ANY,
556
557
			"could not open back-sql baseObject "
			"attr file \"%s\" - absolute path?\n",
558
559
560
561
562
			fname, 0, 0 );
		perror( fname );
		return LDAP_OTHER;
	}

563
	bi->sql_baseObject = entry_alloc();
564
565
	if ( bi->sql_baseObject == NULL ) {
		Debug( LDAP_DEBUG_ANY,
566
			"read_baseObject_file: entry_alloc failed", 0, 0, 0 );
567
		ldif_close( fp );
568
569
570
571
572
573
		return LDAP_NO_MEMORY;
	}
	bi->sql_baseObject->e_name = be->be_suffix[0];
	bi->sql_baseObject->e_nname = be->be_nsuffix[0];
	bi->sql_baseObject->e_attrs = NULL;

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
574
	while (( ldifrc = ldif_read_record( fp, &lineno, &buf, &lmax )) > 0 ) {
575
576
577
578
		Entry		*e = str2entry( buf );
		Attribute	*a;

		if( e == NULL ) {
579
			fprintf( stderr, "back-sql baseObject: "
580
					"could not parse entry (line=%lu)\n",
581
					lineno );
582
583
584
585
586
587
588
			rc = LDAP_OTHER;
			break;
		}

		/* make sure the DN is the database's suffix */
		if ( !be_issuffix( be, &e->e_nname ) ) {
			fprintf( stderr,
589
				"back-sql: invalid baseObject - "
590
				"dn=\"%s\" (line=%lu)\n",
591
				e->e_name.bv_val, lineno );
592
			entry_free( e );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
593
			rc = LDAP_OTHER;
594
595
596
597
598
599
600
601
			break;
		}

		/*
		 * we found a valid entry, so walk thru all the attributes in the
		 * entry, and add each attribute type and description to baseObject
		 */
		for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
602
603
604
605
			if ( attr_merge( bi->sql_baseObject, a->a_desc,
						a->a_vals,
						( a->a_nvals == a->a_vals ) ?
						NULL : a->a_nvals ) )
606
607
608
609
610
611
612
613
614
615
616
617
			{
				rc = LDAP_OTHER;
				break;
			}
		}

		entry_free( e );
		if ( rc ) {
			break;
		}
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
618
619
620
	if ( ldifrc < 0 )
		rc = LDAP_OTHER;

621
622
623
624
625
626
627
	if ( rc ) {
		entry_free( bi->sql_baseObject );
		bi->sql_baseObject = NULL;
	}

	ch_free( buf );

628
	ldif_close( fp );
629

630
631
	Debug( LDAP_DEBUG_CONFIG, "back-sql baseObject file \"%s\" read.\n",
			fname, 0, 0 );
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651

	return rc;
}

static int
create_baseObject(
	BackendDB	*be,
	const char	*fname,
	int		lineno )
{
	backsql_info 	*bi = (backsql_info *)be->be_private;
	LDAPRDN		rdn;
	char		*p;
	int		rc, iAVA;
	char		buf[1024];

	snprintf( buf, sizeof(buf),
			"dn: %s\n"
			"objectClass: extensibleObject\n"
			"description: builtin baseObject for back-sql\n"
652
			"description: all entries mapped "
653
654
655
656
				"in table \"ldap_entries\" "
				"must have "
				"\"" BACKSQL_BASEOBJECT_IDSTR "\" "
				"in the \"parent\" column",
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
			be->be_suffix[0].bv_val );

	bi->sql_baseObject = str2entry( buf );
	if ( bi->sql_baseObject == NULL ) {
		Debug( LDAP_DEBUG_TRACE,
			"<==backsql_db_config (%s line %d): "
			"unable to parse baseObject entry\n",
			fname, lineno, 0 );
		return 1;
	}

	if ( BER_BVISEMPTY( &be->be_suffix[ 0 ] ) ) {
		return 0;
	}

672
673
	rc = ldap_bv2rdn( &be->be_suffix[ 0 ], &rdn, (char **)&p,
			LDAP_DN_FORMAT_LDAP );
674
675
	if ( rc != LDAP_SUCCESS ) {
		snprintf( buf, sizeof(buf),
676
677
678
679
			"unable to extract RDN "
			"from baseObject DN \"%s\" (%d: %s)",
			be->be_suffix[ 0 ].bv_val,
			rc, ldap_err2string( rc ) );
680
681
682
683
684
685
686
687
688
689
690
691
692
		Debug( LDAP_DEBUG_TRACE,
			"<==backsql_db_config (%s line %d): %s\n",
			fname, lineno, buf );
		return 1;
	}

	for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
		LDAPAVA				*ava = rdn[ iAVA ];
		AttributeDescription		*ad = NULL;
		slap_syntax_transform_func	*transf = NULL;
		struct berval			bv = BER_BVNULL;
		const char			*text = NULL;

693
		assert( ava != NULL );
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722

		rc = slap_bv2ad( &ava->la_attr, &ad, &text );
		if ( rc != LDAP_SUCCESS ) {
			snprintf( buf, sizeof(buf),
				"AttributeDescription of naming "
				"attribute #%d from baseObject "
				"DN \"%s\": %d: %s",
				iAVA, be->be_suffix[ 0 ].bv_val,
				rc, ldap_err2string( rc ) );
			Debug( LDAP_DEBUG_TRACE,
				"<==backsql_db_config (%s line %d): %s\n",
				fname, lineno, buf );
			return 1;
		}
		
		transf = ad->ad_type->sat_syntax->ssyn_pretty;
		if ( transf ) {
			/*
	 		 * transform value by pretty function
			 *	if value is empty, use empty_bv
			 */
			rc = ( *transf )( ad->ad_type->sat_syntax,
				ava->la_value.bv_len
					? &ava->la_value
					: (struct berval *) &slap_empty_bv,
				&bv, NULL );
	
			if ( rc != LDAP_SUCCESS ) {
				snprintf( buf, sizeof(buf),
723
724
					"prettying of attribute #%d "
					"from baseObject "
725
726
727
728
					"DN \"%s\" failed: %d: %s",
					iAVA, be->be_suffix[ 0 ].bv_val,
					rc, ldap_err2string( rc ) );
				Debug( LDAP_DEBUG_TRACE,
729
730
					"<==backsql_db_config (%s line %d): "
					"%s\n",
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
					fname, lineno, buf );
				return 1;
			}
		}

		if ( !BER_BVISNULL( &bv ) ) {
			if ( ava->la_flags & LDAP_AVA_FREE_VALUE ) {
				ber_memfree( ava->la_value.bv_val );
			}
			ava->la_value = bv;
			ava->la_flags |= LDAP_AVA_FREE_VALUE;
		}

		attr_merge_normalize_one( bi->sql_baseObject,
				ad, &ava->la_value, NULL );
	}

	ldap_rdnfree( rdn );

	return 0;
}

753
754
755
756
757
758
759
760
761
int backsql_init_cf( BackendInfo *bi )
{
	int rc;

	bi->bi_cf_ocs = sqlocs;
	rc = config_register_schema( sqlcfg, sqlocs );
	if ( rc ) return rc;
	return 0;
}