summaryrefslogtreecommitdiff
path: root/source4/dsdb/samdb/ldb_modules/schema_syntax.c
blob: 02c42bbf8f4052e4da67361ee709419a644561fa (plain)
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
/* 
   ldb database library

   Copyright (C) Simo Sorce  2004-2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 *  Name: ldb
 *
 *  Component: ldb schema module
 *
 *  Description: add schema syntax functionality
 *
 *  Author: Simo Sorce
 *
 *  License: GNU GPL v2 or Later
 */

#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_errors.h"
#include "schema_syntax.h"

static int schema_validate_boolean(struct ldb_val *val, int min, int max)
{

	if ((strncmp("TRUE", (const char *)val->data, val->length) != 0) &&
	    (strncmp("FALSE", (const char *)val->data, val->length) != 0)) {
		return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	}

	return LDB_SUCCESS;
}

static int schema_validate_integer(struct ldb_val *val, int min, int max)
{
	int value;
	char *endptr;

	errno = 0;
	value = strtol((const char *)val->data, &endptr, 0);
	if (errno) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if (endptr[0] != '\0') return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if ((min > INT_MIN) && (value < min)) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if ((max < INT_MAX) && (value > max)) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;

	return LDB_SUCCESS;
}

static int schema_validate_binary_blob(struct ldb_val *val, int min, int max)
{
	/* is there anythign we should check in a binary blob ? */
	return LDB_SUCCESS;
}

static int schema_validate_sid(struct ldb_val *val, int min, int max)
{
	/* TODO: validate binary form of objectSid */
	return LDB_SUCCESS;	
}

static int schema_validate_oid(struct ldb_val *val, int min, int max)
{
	if (strspn((const char *)val->data, "0123456789.") != val->length)
		return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;

	return LDB_SUCCESS;
}

static int schema_validate_numeric_string(struct ldb_val *val, int min, int max)
{
	if (strspn((const char *)val->data, "0123456789") != val->length)
		return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;

	return LDB_SUCCESS;
}

static int schema_validate_printable_string(struct ldb_val *val, int min, int max)
{
	/* TODO: find out what constitutes the printable character set */
	return LDB_SUCCESS;
}

static int schema_validate_teletext_string(struct ldb_val *val, int min, int max)
{
	/* TODO: find out what constitutes the teletext character set */
	return LDB_SUCCESS;
}

static int schema_validate_ia5_string(struct ldb_val *val, int min, int max)
{
	/* TODO: find out what constitutes the IA5 character set */
	return LDB_SUCCESS;
}

static int schema_validate_utc_time(struct ldb_val *val, int min, int max)
{
	/* TODO: validate syntax of UTC Time string */
	return LDB_SUCCESS;
}

static int schema_validate_generalized_time(struct ldb_val *val, int min, int max)
{
	/* TODO: validate syntax of Generalized Time string */
	return LDB_SUCCESS;
}

/* NOTE: not a single attribute has this syntax in the basic w2k3 schema */
static int schema_validate_sensitive_string(struct ldb_val *val, int min, int max)
{
	/* TODO: find out what constitutes a "case sensitive string" */
	return LDB_SUCCESS;
}

static int schema_validate_unicode_string(struct ldb_val *val, int min, int max)
{
	/* TODO: validate utf8 string */
	return LDB_SUCCESS;
}

static int schema_validate_large_integer(struct ldb_val *val, int min, int max)
{
	/* TODO: validate large integer/interval */
	return LDB_SUCCESS;
}

static int schema_validate_object_sd(struct ldb_val *val, int min, int max)
{
	/* TODO: validate object Security Descriptor */
	return LDB_SUCCESS;
}

static int schema_validate_dn(struct ldb_val *val, int min, int max)
{
	TALLOC_CTX *memctx;
	struct ldb_dn *dn;
	int ret = LDB_SUCCESS;

	memctx = talloc_new(NULL);
	if (!memctx) return LDB_ERR_OPERATIONS_ERROR;

	dn = ldb_dn_explode(memctx, (const char *)val->data);
	if (!dn) {
		ret = LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	}

	talloc_free(memctx);
	return ret;
}

static int schema_validate_binary_plus_dn(struct ldb_val *val, int min, int max)
{
	int ret = LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	TALLOC_CTX *memctx;
	struct ldb_dn *dn;
	char *str, *p;
	char *endptr;
	int num;
       
	memctx = talloc_new(NULL);
	if (!memctx) return LDB_ERR_OPERATIONS_ERROR;

	str = talloc_strdup(memctx, (const char *)val->data);
	if (!str) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		goto done;
	}
	if (strncasecmp(str, "B:", 2) != 0) {
		goto done;
	}

	/* point at the number of chars in the string */
	str = strchr(&str[2], ':');
	if (!str) {
		goto done;
	}
	str++;

	errno = 0;
	num = strtol(str, &endptr, 0);
	if (errno) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if (endptr[0] != ':') return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if ((min > INT_MIN) && (num < min)) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if ((max < INT_MAX) && (num > max)) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;

	/* point at the string */
	str = strchr(str, ':');
	if (!str) {
		goto done;
	}
	str++;

	/* terminate the string */
	p = strchr(str, ':');
	if (!p) {
		goto done;
	}
	*p = '\0';

	if (strlen(str) != 2*num) {
		goto done;
	}

	str = p + 1;

	dn = ldb_dn_explode(memctx, str);
	if (dn) {
		ret = LDB_SUCCESS;
	}

done:
	talloc_free(memctx);
	return ret;
}

static int schema_validate_x400_or_name(struct ldb_val *val, int min, int max)
{
	/* TODO: find out what is the syntax of an X400 OR NAME */
	return LDB_SUCCESS;
}

static int schema_validate_presentation_address(struct ldb_val *val, int min, int max)
{
	/* TODO: find out what is the syntax of a presentation address */
	return LDB_SUCCESS;
}

static int schema_validate_x400_access_point(struct ldb_val *val, int min, int max)
{
	/* TODO: find out what is the syntax of an X400 Access Point */
	return LDB_SUCCESS;
}

/* NOTE: seem there isn't a single attribute defined like this in the base w2k3 schema */
static int schema_validate_string_plus_dn(struct ldb_val *val, int min, int max)
{
	int ret = LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	TALLOC_CTX *memctx;
	struct ldb_dn *dn;
	char *str, *p;
	char *endptr;
	int num;
       
	memctx = talloc_new(NULL);
	if (!memctx) return LDB_ERR_OPERATIONS_ERROR;

	str = talloc_strdup(memctx, (const char *)val->data);
	if (!str) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		goto done;
	}
	if (strncasecmp(str, "S:", 2) != 0) {
		goto done;
	}

	/* point at the number of chars in the string */
	str = strchr(&str[2], ':');
	if (!str) {
		goto done;
	}
	str++;

	errno = 0;
	num = strtol(str, &endptr, 0);
	if (errno) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if (endptr[0] != ':') return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if ((min > INT_MIN) && (num < min)) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	if ((max < INT_MAX) && (num > max)) return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;

	/* point at the string */
	str = strchr(str, ':');
	if (!str) {
		goto done;
	}
	str++;

	/* terminate the string */
	p = strchr(str, ':');
	if (!p) {
		goto done;
	}
	*p = '\0';

	if (strlen(str) != num) {
		goto done;
	}

	str = p + 1;

	dn = ldb_dn_explode(memctx, str);
	if (dn) {
		ret = LDB_SUCCESS;
	}

done:
	talloc_free(memctx);
	return ret;
}

struct schema_syntax_validator {
	enum schema_internal_syntax type;
	int (*validate)(struct ldb_val *, int, int);
};

struct schema_syntax_validator schema_syntax_validators[] = {
	{ SCHEMA_AS_BOOLEAN, schema_validate_boolean },
	{ SCHEMA_AS_INTEGER, schema_validate_integer },
	{ SCHEMA_AS_OCTET_STRING, schema_validate_binary_blob },
	{ SCHEMA_AS_SID, schema_validate_sid },
	{ SCHEMA_AS_OID, schema_validate_oid },
	{ SCHEMA_AS_ENUMERATION, schema_validate_integer },
	{ SCHEMA_AS_NUMERIC_STRING, schema_validate_numeric_string },
	{ SCHEMA_AS_PRINTABLE_STRING, schema_validate_printable_string },
	{ SCHEMA_AS_CASE_IGNORE_STRING, schema_validate_teletext_string },
	{ SCHEMA_AS_IA5_STRING, schema_validate_ia5_string },
	{ SCHEMA_AS_UTC_TIME, schema_validate_utc_time },
	{ SCHEMA_AS_GENERALIZED_TIME, schema_validate_generalized_time },
	{ SCHEMA_AS_CASE_SENSITIVE_STRING, schema_validate_sensitive_string },
	{ SCHEMA_AS_DIRECTORY_STRING, schema_validate_unicode_string },
	{ SCHEMA_AS_LARGE_INTEGER, schema_validate_large_integer },
	{ SCHEMA_AS_OBJECT_SECURITY_DESCRIPTOR, schema_validate_object_sd },
	{ SCHEMA_AS_DN, schema_validate_dn },
	{ SCHEMA_AS_DN_BINARY, schema_validate_binary_plus_dn },
	{ SCHEMA_AS_OR_NAME, schema_validate_x400_or_name },
	{ SCHEMA_AS_REPLICA_LINK, schema_validate_binary_blob },
	{ SCHEMA_AS_PRESENTATION_ADDRESS, schema_validate_presentation_address }, /* see rfc1278 ? */
	{ SCHEMA_AS_ACCESS_POINT, schema_validate_x400_access_point },
	{ SCHEMA_AS_DN_STRING, schema_validate_string_plus_dn },
	{ -1, NULL }
};

int schema_validate(struct ldb_message_element *el,
		    enum schema_internal_syntax type,
		    bool single, int min, int max)
{
	struct schema_syntax_validator *v;
	int i, ret;

	if (single && (el->num_values > 1)) {
		return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
	}

	for (i = 0; schema_syntax_validators[i].type != 0; i++) {
		if (schema_syntax_validators[i].type == type)
			break;
	}
	if (schema_syntax_validators[i].type == 0) {
		return LDB_ERR_OPERATIONS_ERROR;
	}
	v = &schema_syntax_validators[i];
	
	for (i = 0; i < el->num_values; i++) {
		ret = v->validate(&el->values[i], min, max);
	}

	return LDB_SUCCESS;
}