summaryrefslogtreecommitdiff
path: root/source4/dsdb/schema/schema_info_attr.c
blob: ac22eb9b3f2905608662b53033e7d2b9f0cdd1e8 (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
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
/*
   Unix SMB/CIFS implementation.

   SCHEMA::schemaInfo implementation

   Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "dsdb/common/util.h"
#include "dsdb/samdb/samdb.h"
#include "dsdb/samdb/ldb_modules/util.h"
#include "lib/ldb/include/ldb_module.h"
#include "librpc/gen_ndr/ndr_drsuapi.h"
#include "librpc/gen_ndr/ndr_drsblobs.h"
#include "param/param.h"


/**
 * Creates and initializes new dsdb_schema_info value.
 * Initial schemaInfo values is with:
 *   revision = 0
 *   invocationId = GUID_ZERO
 */
WERROR dsdb_schema_info_new(TALLOC_CTX *mem_ctx, struct dsdb_schema_info **_schema_info)
{
	struct dsdb_schema_info *schema_info;

	schema_info = talloc_zero(mem_ctx, struct dsdb_schema_info);
	W_ERROR_HAVE_NO_MEMORY(schema_info);

	*_schema_info = schema_info;

	return WERR_OK;
}

/**
 * Creates and initializes new dsdb_schema_info blob value.
 * Initial schemaInfo values is with:
 *   revision = 0
 *   invocationId = GUID_ZERO
 */
WERROR dsdb_schema_info_blob_new(TALLOC_CTX *mem_ctx, DATA_BLOB *_schema_info_blob)
{
	DATA_BLOB blob;

	blob = data_blob_talloc_zero(mem_ctx, 21);
	W_ERROR_HAVE_NO_MEMORY(blob.data);

	/* Set the schemaInfo marker to 0xFF */
	blob.data[0] = 0xFF;

	*_schema_info_blob = blob;

	return WERR_OK;
}


/**
 * Parse schemaInfo structure from a data_blob
 * (DATA_BLOB or ldb_val).
 * Suitable for parsing blobs that comes from
 * DRS interface of from LDB database
 */
WERROR dsdb_schema_info_from_blob(const DATA_BLOB *blob,
				  TALLOC_CTX *mem_ctx, struct dsdb_schema_info **_schema_info)
{
	TALLOC_CTX *temp_ctx;
	enum ndr_err_code ndr_err;
	struct dsdb_schema_info *schema_info;
	struct schemaInfoBlob schema_info_blob;

	if (!blob || !blob->data) {
		return WERR_INVALID_PARAMETER;
	}

	if (blob->length != 21) {
		return WERR_INVALID_PARAMETER;
	}

	/* schemaInfo blob should start with 0xFF */
	if (blob->data[0] != 0xFF) {
		return WERR_INVALID_PARAMETER;
	}

	temp_ctx = talloc_new(mem_ctx);
	W_ERROR_HAVE_NO_MEMORY(temp_ctx);

	ndr_err = ndr_pull_struct_blob_all(blob, temp_ctx,
	                                   lp_iconv_convenience(NULL), &schema_info_blob,
	                                   (ndr_pull_flags_fn_t)ndr_pull_schemaInfoBlob);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
		talloc_free(temp_ctx);
		return ntstatus_to_werror(nt_status);
	}

	schema_info = talloc(mem_ctx, struct dsdb_schema_info);
	if (!schema_info) {
		talloc_free(temp_ctx);
		return WERR_NOMEM;
	}

	/* note that we accept revision numbers of zero now - w2k8r2
	   sends a revision of zero on initial vampire */
	schema_info->revision      = schema_info_blob.revision;
	schema_info->invocation_id = schema_info_blob.invocation_id;
	*_schema_info = schema_info;

	talloc_free(temp_ctx);
	return WERR_OK;
}

/**
 * Creates a blob from schemaInfo structure
 * Suitable for packing schemaInfo into a blob
 * which is to be used in DRS interface of LDB database
 */
WERROR dsdb_blob_from_schema_info(const struct dsdb_schema_info *schema_info,
				  TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
{
	enum ndr_err_code ndr_err;
	struct schemaInfoBlob schema_info_blob;

	schema_info_blob.marker		= 0xFF;
	schema_info_blob.revision	= schema_info->revision;
	schema_info_blob.invocation_id  = schema_info->invocation_id;

	ndr_err = ndr_push_struct_blob(blob, mem_ctx,
	                               lp_iconv_convenience(NULL), &schema_info_blob,
	                               (ndr_push_flags_fn_t)ndr_push_schemaInfoBlob);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
		return ntstatus_to_werror(nt_status);
	}

	return WERR_OK;
}


/**
 * Reads schema_info structure from schemaInfo
 * attribute on SCHEMA partition
 *
 * @param dsdb_flags 	DSDB_FLAG_... flag of 0
 */
WERROR dsdb_module_schema_info_blob_read(struct ldb_module *ldb_module,
					 uint32_t dsdb_flags,
					 TALLOC_CTX *mem_ctx, DATA_BLOB *schema_info_blob)
{
	int ldb_err;
	const struct ldb_val *blob_val;
	struct ldb_dn *schema_dn;
	struct ldb_result *schema_res = NULL;
	static const char *schema_attrs[] = {
		"schemaInfo",
		NULL
	};

	schema_dn = ldb_get_schema_basedn(ldb_module_get_ctx(ldb_module));
	if (!schema_dn) {
		DEBUG(0,("dsdb_module_schema_info_blob_read: no schema dn present!\n"));
		return WERR_INTERNAL_DB_CORRUPTION;
	}

	ldb_err = dsdb_module_search(ldb_module, mem_ctx, &schema_res, schema_dn,
	                             LDB_SCOPE_BASE, schema_attrs, dsdb_flags, NULL);
	if (ldb_err == LDB_ERR_NO_SUCH_OBJECT) {
		DEBUG(0,("dsdb_module_schema_info_blob_read: Schema DN not found!\n"));
		talloc_free(schema_res);
		return WERR_INTERNAL_DB_CORRUPTION;
	} else if (ldb_err != LDB_SUCCESS) {
		DEBUG(0,("dsdb_module_schema_info_blob_read: failed to find schemaInfo attribute\n"));
		talloc_free(schema_res);
		return WERR_INTERNAL_DB_CORRUPTION;
	}

	blob_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
	if (!blob_val) {
		DEBUG(0,("dsdb_module_schema_info_blob_read: no schemaInfo attribute found\n"));
		talloc_free(schema_res);
		return WERR_DS_NO_ATTRIBUTE_OR_VALUE;
	}

	/* transfer .data ownership to mem_ctx */
	schema_info_blob->length = blob_val->length;
	schema_info_blob->data = talloc_steal(mem_ctx, blob_val->data);

	talloc_free(schema_res);

	return WERR_OK;
}

/**
 * Prepares ldb_msg to be used for updating schemaInfo value in DB
 */
static WERROR _dsdb_schema_info_write_prepare(struct ldb_context *ldb,
					      DATA_BLOB *schema_info_blob,
					      TALLOC_CTX *mem_ctx,
					      struct ldb_message **_msg)
{
	int ldb_err;
	struct ldb_message *msg;
	struct ldb_dn *schema_dn;
	struct ldb_message_element *return_el;

	schema_dn = ldb_get_schema_basedn(ldb);
	if (!schema_dn) {
		DEBUG(0,("_dsdb_schema_info_write_prepare: no schema dn present\n"));
		return WERR_INTERNAL_DB_CORRUPTION;
	}

	/* prepare ldb_msg to update schemaInfo */
	msg = ldb_msg_new(mem_ctx);
	W_ERROR_HAVE_NO_MEMORY(msg);

	msg->dn = schema_dn;
	ldb_err = ldb_msg_add_value(msg, "schemaInfo", schema_info_blob, &return_el);
	if (ldb_err != 0) {
		DEBUG(0,("_dsdb_schema_info_write_prepare: ldb_msg_add_value failed - %s\n",
			 ldb_strerror(ldb_err)));
		talloc_free(msg);
		return WERR_INTERNAL_ERROR;
	}

	/* mark schemaInfo element for replacement */
	return_el->flags = LDB_FLAG_MOD_REPLACE;

	*_msg = msg;

	return WERR_OK;
}

/**
 * Writes schema_info structure into schemaInfo
 * attribute on SCHEMA partition
 *
 * @param dsdb_flags 	DSDB_FLAG_... flag of 0
 */
WERROR dsdb_module_schema_info_blob_write(struct ldb_module *ldb_module,
					  uint32_t dsdb_flags,
					  DATA_BLOB *schema_info_blob)
{
	int ldb_err;
	WERROR werr;
	struct ldb_message *msg;
	TALLOC_CTX *temp_ctx;

	temp_ctx = talloc_new(ldb_module);
	W_ERROR_HAVE_NO_MEMORY(temp_ctx);

	/* write serialized schemaInfo into LDB */
	werr = _dsdb_schema_info_write_prepare(ldb_module_get_ctx(ldb_module),
	                                       schema_info_blob,
	                                       temp_ctx, &msg);
	if (!W_ERROR_IS_OK(werr)) {
		talloc_free(temp_ctx);
		return werr;
	}


	ldb_err = dsdb_module_modify(ldb_module, msg, dsdb_flags);

	talloc_free(temp_ctx);

	if (ldb_err != 0) {
		DEBUG(0,("dsdb_module_schema_info_blob_write: dsdb_replace failed: %s (%s)\n",
			 ldb_strerror(ldb_err),
			 ldb_errstring(ldb_module_get_ctx(ldb_module))));
		return WERR_INTERNAL_DB_ERROR;
	}

	return WERR_OK;
}


/**
 * Reads schema_info structure from schemaInfo
 * attribute on SCHEMA partition
 */
static WERROR dsdb_module_schema_info_read(struct ldb_module *ldb_module,
					   uint32_t dsdb_flags,
					   TALLOC_CTX *mem_ctx,
					   struct dsdb_schema_info **_schema_info)
{
	WERROR werr;
	DATA_BLOB ndr_blob;
	TALLOC_CTX *temp_ctx;

	temp_ctx = talloc_new(mem_ctx);
	W_ERROR_HAVE_NO_MEMORY(temp_ctx);

	/* read serialized schemaInfo from LDB  */
	werr = dsdb_module_schema_info_blob_read(ldb_module, dsdb_flags, temp_ctx, &ndr_blob);
	if (!W_ERROR_IS_OK(werr)) {
		talloc_free(temp_ctx);
		return werr;
	}

	/* convert NDR blob to dsdb_schema_info object */
	werr = dsdb_schema_info_from_blob(&ndr_blob,
					  mem_ctx,
					  _schema_info);
	talloc_free(temp_ctx);

	return werr;
}

/**
 * Writes schema_info structure into schemaInfo
 * attribute on SCHEMA partition
 *
 * @param dsdb_flags 	DSDB_FLAG_... flag of 0
 */
static WERROR dsdb_module_schema_info_write(struct ldb_module *ldb_module,
					    uint32_t dsdb_flags,
					    const struct dsdb_schema_info *schema_info)
{
	WERROR werr;
	DATA_BLOB ndr_blob;
	TALLOC_CTX *temp_ctx;

	temp_ctx = talloc_new(ldb_module);
	W_ERROR_HAVE_NO_MEMORY(temp_ctx);

	/* convert schema_info to a blob */
	werr = dsdb_blob_from_schema_info(schema_info, temp_ctx, &ndr_blob);
	if (!W_ERROR_IS_OK(werr)) {
		talloc_free(temp_ctx);
		return werr;
	}

	/* write serialized schemaInfo into LDB */
	werr = dsdb_module_schema_info_blob_write(ldb_module, dsdb_flags, &ndr_blob);

	talloc_free(temp_ctx);

	return werr;
}


/**
 * Increments schemaInfo revision and save it to DB
 * setting our invocationID in the process
 * NOTE: this function should be called in a transaction
 * much in the same way prefixMap update function is called
 *
 * @param ldb_module 	current module
 * @param schema 	schema cache
 * @param dsdb_flags 	DSDB_FLAG_... flag of 0
 */
WERROR dsdb_module_schema_info_update(struct ldb_module *ldb_module,
				      struct dsdb_schema *schema,
				      int dsdb_flags)
{
	WERROR werr;
	const struct GUID *invocation_id;
	DATA_BLOB ndr_blob;
	struct dsdb_schema_info *schema_info;
	const char *schema_info_str;

	TALLOC_CTX *temp_ctx = talloc_new(schema);
	W_ERROR_HAVE_NO_MEMORY(temp_ctx);

	invocation_id = samdb_ntds_invocation_id(ldb_module_get_ctx(ldb_module));
	if (!invocation_id) {
		return WERR_INTERNAL_DB_CORRUPTION;
	}

	/* read serialized schemaInfo from LDB  */
	werr = dsdb_module_schema_info_read(ldb_module, dsdb_flags, temp_ctx, &schema_info);
	if (W_ERROR_EQUAL(werr, WERR_DS_NO_ATTRIBUTE_OR_VALUE)) {
		/* make default value in case
		 * we have no schemaInfo value yet */
		werr = dsdb_schema_info_new(temp_ctx, &schema_info);
	}
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(0,("dsdb_module_schema_info_update: failed to reload schemaInfo - %s\n",
			 win_errstr(werr)));
		talloc_free(temp_ctx);
		return werr;
	}

	/* update schemaInfo */
	schema_info->revision++;
	schema_info->invocation_id = *invocation_id;

	werr = dsdb_module_schema_info_write(ldb_module, dsdb_flags, schema_info);
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(0,("dsdb_module_schema_info_update: failed to save schemaInfo - %s\n",
			 win_errstr(werr)));
		talloc_free(temp_ctx);
		return werr;
	}

	/* finally, update schema_info in the cache */
	werr = dsdb_blob_from_schema_info(schema_info, temp_ctx, &ndr_blob);
	W_ERROR_NOT_OK_RETURN(werr);

	schema_info_str = hex_encode_talloc(schema, ndr_blob.data, ndr_blob.length);
	W_ERROR_HAVE_NO_MEMORY(schema_info_str);

	talloc_unlink(schema, discard_const(schema->schema_info));
	schema->schema_info = schema_info_str;

	talloc_free(temp_ctx);
	return WERR_OK;
}