2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Volker Lendecke 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "../librpc/gen_ndr/netlogon.h"
22 #include "../libcli/security/security.h"
23 #include "rpc_client/util_netlogon.h"
25 #define COPY_LSA_STRING(mem_ctx, in, out, name) do { \
26 if (in->name.string) { \
27 out->name.string = talloc_strdup(mem_ctx, in->name.string); \
28 NT_STATUS_HAVE_NO_MEMORY(out->name.string); \
32 NTSTATUS copy_netr_SamBaseInfo(TALLOC_CTX *mem_ctx,
33 const struct netr_SamBaseInfo *in,
34 struct netr_SamBaseInfo *out)
36 /* first copy all, then realloc pointers */
39 COPY_LSA_STRING(mem_ctx, in, out, account_name);
40 COPY_LSA_STRING(mem_ctx, in, out, full_name);
41 COPY_LSA_STRING(mem_ctx, in, out, logon_script);
42 COPY_LSA_STRING(mem_ctx, in, out, profile_path);
43 COPY_LSA_STRING(mem_ctx, in, out, home_directory);
44 COPY_LSA_STRING(mem_ctx, in, out, home_drive);
46 if (in->groups.count) {
47 out->groups.rids = (struct samr_RidWithAttribute *)
48 talloc_memdup(mem_ctx, in->groups.rids,
49 (sizeof(struct samr_RidWithAttribute) *
51 NT_STATUS_HAVE_NO_MEMORY(out->groups.rids);
54 COPY_LSA_STRING(mem_ctx, in, out, logon_server);
55 COPY_LSA_STRING(mem_ctx, in, out, logon_domain);
58 out->domain_sid = dom_sid_dup(mem_ctx, in->domain_sid);
59 NT_STATUS_HAVE_NO_MEMORY(out->domain_sid);
65 NTSTATUS copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
66 const struct netr_SamInfo3 *in,
67 struct netr_SamInfo3 **pout)
69 struct netr_SamInfo3 *info3 = NULL;
73 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
75 status = NT_STATUS_NO_MEMORY;
79 status = copy_netr_SamBaseInfo(info3, &in->base, &info3->base);
80 if (!NT_STATUS_IS_OK(status)) {
85 info3->sidcount = in->sidcount;
86 info3->sids = talloc_array(info3, struct netr_SidAttr,
88 if (info3->sids == NULL) {
89 status = NT_STATUS_NO_MEMORY;
93 for (i = 0; i < in->sidcount; i++) {
94 info3->sids[i].sid = dom_sid_dup(info3->sids,
96 if (info3->sids[i].sid == NULL) {
97 status = NT_STATUS_NO_MEMORY;
100 info3->sids[i].attributes = in->sids[i].attributes;
107 status = NT_STATUS_OK;
113 NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
114 uint16_t validation_level,
115 union netr_Validation *validation,
116 struct netr_SamInfo3 **info3_p)
118 struct netr_SamInfo3 *info3 = NULL;
119 struct netr_SamInfo6 *info6 = NULL;
122 if (validation == NULL) {
123 return NT_STATUS_INVALID_PARAMETER;
126 switch (validation_level) {
128 if (validation->sam3 == NULL) {
129 return NT_STATUS_INVALID_PARAMETER;
132 status = copy_netr_SamInfo3(mem_ctx,
135 if (!NT_STATUS_IS_OK(status)) {
141 if (validation->sam6 == NULL) {
142 return NT_STATUS_INVALID_PARAMETER;
144 info6 = validation->sam6;
146 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
148 return NT_STATUS_NO_MEMORY;
151 status = copy_netr_SamBaseInfo(info3,
154 if (!NT_STATUS_IS_OK(status)) {
159 if (validation->sam6->sidcount > 0) {
162 info3->sidcount = info6->sidcount;
164 info3->sids = talloc_array(info3,
167 if (info3->sids == NULL) {
169 return NT_STATUS_NO_MEMORY;
172 for (i = 0; i < info3->sidcount; i++) {
173 info3->sids[i].sid = dom_sid_dup(
174 info3->sids, info6->sids[i].sid);
175 if (info3->sids[i].sid == NULL) {
177 return NT_STATUS_NO_MEMORY;
179 info3->sids[i].attributes =
180 info6->sids[i].attributes;
185 return NT_STATUS_BAD_VALIDATION_CLASS;
193 NTSTATUS copy_netr_SamInfo6(TALLOC_CTX *mem_ctx,
194 const struct netr_SamInfo6 *in,
195 struct netr_SamInfo6 **pout)
197 struct netr_SamInfo6 *info6 = NULL;
201 info6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
203 status = NT_STATUS_NO_MEMORY;
207 status = copy_netr_SamBaseInfo(info6, &in->base, &info6->base);
208 if (!NT_STATUS_IS_OK(status)) {
213 info6->sidcount = in->sidcount;
214 info6->sids = talloc_array(info6, struct netr_SidAttr,
216 if (info6->sids == NULL) {
217 status = NT_STATUS_NO_MEMORY;
221 for (i = 0; i < in->sidcount; i++) {
222 info6->sids[i].sid = dom_sid_dup(info6->sids,
224 if (info6->sids[i].sid == NULL) {
225 status = NT_STATUS_NO_MEMORY;
228 info6->sids[i].attributes = in->sids[i].attributes;
232 if (in->dns_domainname.string != NULL) {
233 info6->dns_domainname.string = talloc_strdup(info6,
234 in->dns_domainname.string);
235 if (info6->dns_domainname.string == NULL) {
236 status = NT_STATUS_NO_MEMORY;
241 if (in->principal_name.string != NULL) {
242 info6->principal_name.string = talloc_strdup(info6,
243 in->principal_name.string);
244 if (info6->principal_name.string == NULL) {
245 status = NT_STATUS_NO_MEMORY;
253 status = NT_STATUS_OK;
259 NTSTATUS map_validation_to_info6(TALLOC_CTX *mem_ctx,
260 uint16_t validation_level,
261 union netr_Validation *validation,
262 struct netr_SamInfo6 **info6_p)
264 struct netr_SamInfo3 *info3 = NULL;
265 struct netr_SamInfo6 *info6 = NULL;
268 if (validation == NULL) {
269 return NT_STATUS_INVALID_PARAMETER;
272 switch (validation_level) {
274 if (validation->sam3 == NULL) {
275 return NT_STATUS_INVALID_PARAMETER;
277 info3 = validation->sam3;
279 info6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
281 return NT_STATUS_NO_MEMORY;
284 status = copy_netr_SamBaseInfo(info6,
287 if (!NT_STATUS_IS_OK(status)) {
292 if (validation->sam3->sidcount > 0) {
295 info6->sidcount = info3->sidcount;
297 info6->sids = talloc_array(info6,
300 if (info6->sids == NULL) {
302 return NT_STATUS_NO_MEMORY;
305 for (i = 0; i < info6->sidcount; i++) {
306 info6->sids[i].sid = dom_sid_dup(
307 info6->sids, info3->sids[i].sid);
308 if (info6->sids[i].sid == NULL) {
310 return NT_STATUS_NO_MEMORY;
312 info6->sids[i].attributes =
313 info3->sids[i].attributes;
318 if (validation->sam6 == NULL) {
319 return NT_STATUS_INVALID_PARAMETER;
322 status = copy_netr_SamInfo6(mem_ctx,
325 if (!NT_STATUS_IS_OK(status)) {
331 return NT_STATUS_BAD_VALIDATION_CLASS;
339 NTSTATUS map_info3_to_validation(TALLOC_CTX *mem_ctx,
340 struct netr_SamInfo3 *info3,
341 uint16_t *_validation_level,
342 union netr_Validation **_validation)
344 union netr_Validation *validation = NULL;
347 validation = talloc_zero(mem_ctx, union netr_Validation);
348 if (validation == NULL) {
349 return NT_STATUS_NO_MEMORY;
352 status = copy_netr_SamInfo3(mem_ctx,
355 if (!NT_STATUS_IS_OK(status)) {
356 TALLOC_FREE(validation);
360 * _validation_level = 3;
361 *_validation = validation;
365 NTSTATUS map_info6_to_validation(TALLOC_CTX *mem_ctx,
366 const struct netr_SamInfo6 *info6,
367 uint16_t *_validation_level,
368 union netr_Validation **_validation)
370 union netr_Validation *validation = NULL;
373 validation = talloc_zero(mem_ctx, union netr_Validation);
374 if (validation == NULL) {
375 return NT_STATUS_NO_MEMORY;
378 status = copy_netr_SamInfo6(validation,
381 if (!NT_STATUS_IS_OK(status)) {
382 TALLOC_FREE(validation);
386 * _validation_level = 6;
387 *_validation = validation;
391 /****************************************************************
392 ****************************************************************/
394 NTSTATUS copy_netr_DsRGetDCNameInfo(TALLOC_CTX *mem_ctx,
395 const struct netr_DsRGetDCNameInfo *in,
396 struct netr_DsRGetDCNameInfo **pout)
398 struct netr_DsRGetDCNameInfo *r;
400 r = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
402 return NT_STATUS_NO_MEMORY;
405 r->dc_unc = talloc_strdup(r, in->dc_unc);
406 if (r->dc_unc == NULL) {
408 return NT_STATUS_NO_MEMORY;
410 r->dc_address = talloc_strdup(r, in->dc_address);
411 if (r->dc_address == NULL) {
413 return NT_STATUS_NO_MEMORY;
415 r->dc_address_type = in->dc_address_type;
416 r->domain_guid = in->domain_guid;
417 r->domain_name = talloc_strdup(r, in->domain_name);
418 if (r->domain_name == NULL) {
420 return NT_STATUS_NO_MEMORY;
422 /* forest could be empty */
423 if (in->forest_name != NULL) {
424 r->forest_name = talloc_strdup(r, in->forest_name);
425 if (r->forest_name == NULL) {
427 return NT_STATUS_NO_MEMORY;
430 r->dc_flags = in->dc_flags;
431 if (in->dc_site_name != NULL) {
432 r->dc_site_name = talloc_strdup(r, in->dc_site_name);
433 if (r->dc_site_name == NULL) {
435 return NT_STATUS_NO_MEMORY;
438 if (in->client_site_name != NULL) {
439 r->client_site_name = talloc_strdup(r, in->client_site_name);
440 if (r->client_site_name == NULL) {
442 return NT_STATUS_NO_MEMORY;