summaryrefslogtreecommitdiffstats
path: root/source4/dsdb/schema/schema_info_attr.c
blob: 04b296459fe6d9a73dff4e2d197bec050c2275ad (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
/*
   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 <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;
}


/**
 * Verify the 'blob' is a valid schemaInfo blob
 */
bool dsdb_schema_info_blob_is_valid(const DATA_BLOB *blob)
{
	if (!blob || !blob->data) {
		return false;
	}

	/* schemaInfo blob must be 21 bytes long */
	if (blob->length != 21) {
		return false;
	}

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

	return true;
}

/**
 * Parse schemaInfo structure from a data_blob
 * (DATA_BLOB or ldb_val).
 * Suitable for parsing blobs that come from
 * DRS interface or 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;

	/* verify schemaInfo blob is valid */
	if (!dsdb_schema_info_blob_is_valid(blob)) {
		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,
	                                   &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_NOT_ENOUGH_MEMORY;
	}

	/* 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,
	                               &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;
}

/**
 * Compares schemaInfo signatures in dsdb_schema and prefixMap.
 * NOTE: At present function compares schemaInfo values
 * as string without taking into account schemaVersion field
 *
 * @return WERR_OK if schemaInfos are equal
 * 	   WERR_DS_DRA_SCHEMA_MISMATCH if schemaInfos are different
 */
WERROR dsdb_schema_info_cmp(const struct dsdb_schema *schema,
			    const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
{
	TALLOC_CTX *frame = NULL;
	DATA_BLOB blob = data_blob_null;
	struct dsdb_schema_info *schema_info = NULL;
	const struct drsuapi_DsReplicaOIDMapping *mapping = NULL;
	WERROR werr;

	/* we should have at least schemaInfo element */
	if (ctr->num_mappings < 1) {
		return WERR_INVALID_PARAMETER;
	}

	/* verify schemaInfo element is valid */
	mapping = &ctr->mappings[ctr->num_mappings - 1];
	if (mapping->id_prefix != 0) {
		return WERR_INVALID_PARAMETER;
	}

	blob = data_blob_const(mapping->oid.binary_oid, mapping->oid.length);
	if (!dsdb_schema_info_blob_is_valid(&blob)) {
		return WERR_INVALID_PARAMETER;
	}

	frame = talloc_stackframe();
	werr = dsdb_schema_info_from_blob(&blob, frame, &schema_info);
	if (!W_ERROR_IS_OK(werr)) {
		TALLOC_FREE(frame);
		return werr;
	}

	/*
	 * shouldn't really be possible is dsdb_schema_info_from_blob
	 * succeeded, this check is just to satisfy static checker
	 */
	if (schema_info == NULL) {
		TALLOC_FREE(frame);
		return WERR_INVALID_PARAMETER;
	}

	if (schema->schema_info->revision > schema_info->revision) {
		/*
		 * It's ok if our schema is newer than the remote one
		 */
		werr = WERR_OK;
	} else if (schema->schema_info->revision < schema_info->revision) {
		werr = WERR_DS_DRA_SCHEMA_MISMATCH;
	} else if (!GUID_equal(&schema->schema_info->invocation_id,
		   &schema_info->invocation_id))
	{
		werr = WERR_DS_DRA_SCHEMA_CONFLICT;
	} else {
		werr = WERR_OK;
	}

	TALLOC_FREE(frame);
	return werr;
}