summaryrefslogtreecommitdiff
path: root/source3/rpc_client/util_netlogon.c
blob: 4108707143fe309b6b7b97d7192adc353806a587 (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
/*
   Unix SMB/CIFS implementation.
   Authentication utility functions
   Copyright (C) Volker Lendecke 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 "../librpc/gen_ndr/netlogon.h"
#include "../libcli/security/security.h"
#include "rpc_client/util_netlogon.h"

#define COPY_LSA_STRING(mem_ctx, in, out, name) do { \
	if (in->name.string) { \
		out->name.string = talloc_strdup(mem_ctx, in->name.string); \
		NT_STATUS_HAVE_NO_MEMORY(out->name.string); \
	} \
} while (0)

NTSTATUS copy_netr_SamBaseInfo(TALLOC_CTX *mem_ctx,
			       const struct netr_SamBaseInfo *in,
			       struct netr_SamBaseInfo *out)
{
	/* first copy all, then realloc pointers */
	*out = *in;

	COPY_LSA_STRING(mem_ctx, in, out, account_name);
	COPY_LSA_STRING(mem_ctx, in, out, full_name);
	COPY_LSA_STRING(mem_ctx, in, out, logon_script);
	COPY_LSA_STRING(mem_ctx, in, out, profile_path);
	COPY_LSA_STRING(mem_ctx, in, out, home_directory);
	COPY_LSA_STRING(mem_ctx, in, out, home_drive);

	if (in->groups.count) {
		out->groups.rids = (struct samr_RidWithAttribute *)
			talloc_memdup(mem_ctx, in->groups.rids,
				(sizeof(struct samr_RidWithAttribute) *
					in->groups.count));
		NT_STATUS_HAVE_NO_MEMORY(out->groups.rids);
	}

	COPY_LSA_STRING(mem_ctx, in, out, logon_server);
	COPY_LSA_STRING(mem_ctx, in, out, logon_domain);

	if (in->domain_sid) {
		out->domain_sid = dom_sid_dup(mem_ctx, in->domain_sid);
		NT_STATUS_HAVE_NO_MEMORY(out->domain_sid);
	}

	return NT_STATUS_OK;
}

NTSTATUS copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
			    const struct netr_SamInfo3 *in,
			    struct netr_SamInfo3 **pout)
{
	struct netr_SamInfo3 *info3 = NULL;
	unsigned int i;
	NTSTATUS status;

	info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
	if (info3 == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto out;
	}

	status = copy_netr_SamBaseInfo(info3, &in->base, &info3->base);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	if (in->sidcount) {
		info3->sidcount = in->sidcount;
		info3->sids = talloc_array(info3, struct netr_SidAttr,
					   in->sidcount);
		if (info3->sids == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}

		for (i = 0; i < in->sidcount; i++) {
			info3->sids[i].sid = dom_sid_dup(info3->sids,
							 in->sids[i].sid);
			if (info3->sids[i].sid == NULL) {
				status = NT_STATUS_NO_MEMORY;
				goto out;
			}
			info3->sids[i].attributes = in->sids[i].attributes;
		}
	}

	*pout = info3;
	info3 = NULL;

	status = NT_STATUS_OK;
out:
	TALLOC_FREE(info3);
	return status;
}

NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
				 uint16_t validation_level,
				 union netr_Validation *validation,
				 struct netr_SamInfo3 **info3_p)
{
	struct netr_SamInfo3 *info3 = NULL;
	struct netr_SamInfo6 *info6 = NULL;
	NTSTATUS status;

	if (validation == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	switch (validation_level) {
	case 3:
		if (validation->sam3 == NULL) {
			return NT_STATUS_INVALID_PARAMETER;
		}

		status = copy_netr_SamInfo3(mem_ctx,
					    validation->sam3,
					    &info3);
		if (!NT_STATUS_IS_OK(status)) {
			return status;
		}

		break;
	case 6:
		if (validation->sam6 == NULL) {
			return NT_STATUS_INVALID_PARAMETER;
		}
		info6 = validation->sam6;

		info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
		if (info3 == NULL) {
			return NT_STATUS_NO_MEMORY;
		}

		status = copy_netr_SamBaseInfo(info3,
					       &info6->base,
					       &info3->base);
		if (!NT_STATUS_IS_OK(status)) {
			TALLOC_FREE(info3);
			return status;
		}

		if (validation->sam6->sidcount > 0) {
			int i;

			info3->sidcount = info6->sidcount;

			info3->sids = talloc_array(info3,
						   struct netr_SidAttr,
						   info3->sidcount);
			if (info3->sids == NULL) {
				TALLOC_FREE(info3);
				return NT_STATUS_NO_MEMORY;
			}

			for (i = 0; i < info3->sidcount; i++) {
				info3->sids[i].sid = dom_sid_dup(
					info3->sids, info6->sids[i].sid);
				if (info3->sids[i].sid == NULL) {
					TALLOC_FREE(info3);
					return NT_STATUS_NO_MEMORY;
				}
				info3->sids[i].attributes =
					info6->sids[i].attributes;
			}
		}
		break;
	default:
		return NT_STATUS_BAD_VALIDATION_CLASS;
	}

	*info3_p = info3;

	return NT_STATUS_OK;
}

NTSTATUS copy_netr_SamInfo6(TALLOC_CTX *mem_ctx,
			    const struct netr_SamInfo6 *in,
			    struct netr_SamInfo6 **pout)
{
	struct netr_SamInfo6 *info6 = NULL;
	unsigned int i;
	NTSTATUS status;

	info6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
	if (info6 == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto out;
	}

	status = copy_netr_SamBaseInfo(info6, &in->base, &info6->base);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	if (in->sidcount) {
		info6->sidcount = in->sidcount;
		info6->sids = talloc_array(info6, struct netr_SidAttr,
					   in->sidcount);
		if (info6->sids == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}

		for (i = 0; i < in->sidcount; i++) {
			info6->sids[i].sid = dom_sid_dup(info6->sids,
							 in->sids[i].sid);
			if (info6->sids[i].sid == NULL) {
				status = NT_STATUS_NO_MEMORY;
				goto out;
			}
			info6->sids[i].attributes = in->sids[i].attributes;
		}
	}

	if (in->dns_domainname.string != NULL) {
		info6->dns_domainname.string = talloc_strdup(info6,
						in->dns_domainname.string);
		if (info6->dns_domainname.string == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}
	}

	if (in->principal_name.string != NULL) {
		info6->principal_name.string = talloc_strdup(info6,
						in->principal_name.string);
		if (info6->principal_name.string == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto out;
		}
	}

	*pout = info6;
	info6 = NULL;

	status = NT_STATUS_OK;
out:
	TALLOC_FREE(info6);
	return status;
}

NTSTATUS map_validation_to_info6(TALLOC_CTX *mem_ctx,
				 uint16_t validation_level,
				 union netr_Validation *validation,
				 struct netr_SamInfo6 **info6_p)
{
	struct netr_SamInfo3 *info3 = NULL;
	struct netr_SamInfo6 *info6 = NULL;
	NTSTATUS status;

	if (validation == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	switch (validation_level) {
	case 3:
		if (validation->sam3 == NULL) {
			return NT_STATUS_INVALID_PARAMETER;
		}
		info3 = validation->sam3;

		info6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
		if (info6 == NULL) {
			return NT_STATUS_NO_MEMORY;
		}

		status = copy_netr_SamBaseInfo(info6,
					       &info3->base,
					       &info6->base);
		if (!NT_STATUS_IS_OK(status)) {
			TALLOC_FREE(info6);
			return status;
		}

		if (validation->sam3->sidcount > 0) {
			int i;

			info6->sidcount = info3->sidcount;

			info6->sids = talloc_array(info6,
						   struct netr_SidAttr,
						   info6->sidcount);
			if (info6->sids == NULL) {
				TALLOC_FREE(info6);
				return NT_STATUS_NO_MEMORY;
			}

			for (i = 0; i < info6->sidcount; i++) {
				info6->sids[i].sid = dom_sid_dup(
					info6->sids, info3->sids[i].sid);
				if (info6->sids[i].sid == NULL) {
					TALLOC_FREE(info6);
					return NT_STATUS_NO_MEMORY;
				}
				info6->sids[i].attributes =
					info3->sids[i].attributes;
			}
		}
		break;
	case 6:
		if (validation->sam6 == NULL) {
			return NT_STATUS_INVALID_PARAMETER;
		}

		status = copy_netr_SamInfo6(mem_ctx,
					    validation->sam6,
					    &info6);
		if (!NT_STATUS_IS_OK(status)) {
			return status;
		}

		break;
	default:
		return NT_STATUS_BAD_VALIDATION_CLASS;
	}

	*info6_p = info6;

	return NT_STATUS_OK;
}

NTSTATUS map_info3_to_validation(TALLOC_CTX *mem_ctx,
				 struct netr_SamInfo3 *info3,
				 uint16_t *_validation_level,
				 union netr_Validation **_validation)
{
	union netr_Validation *validation = NULL;
	NTSTATUS status;

	validation = talloc_zero(mem_ctx, union netr_Validation);
	if (validation == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = copy_netr_SamInfo3(mem_ctx,
				    info3,
				    &validation->sam3);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(validation);
		return status;
	}

	* _validation_level = 3;
	*_validation = validation;
	return NT_STATUS_OK;
}

NTSTATUS map_info6_to_validation(TALLOC_CTX *mem_ctx,
				 const struct netr_SamInfo6 *info6,
				 uint16_t *_validation_level,
				 union netr_Validation **_validation)
{
	union netr_Validation *validation = NULL;
	NTSTATUS status;

	validation = talloc_zero(mem_ctx, union netr_Validation);
	if (validation == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = copy_netr_SamInfo6(mem_ctx,
				    info6,
				    &validation->sam6);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(validation);
		return status;
	}

	* _validation_level = 6;
	*_validation = validation;
	return NT_STATUS_OK;
}