2 Unix SMB/CIFS implementation.
4 test suite for netlogon rpc operations
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Tim Potter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "librpc/gen_ndr/ndr_netlogon.h"
27 #include "auth/auth.h"
28 #include "dlinklist.h"
29 #include "lib/crypto/crypto.h"
30 #include "system/time.h"
32 #define TEST_MACHINE_NAME "samsynctest"
33 #define TEST_MACHINE_NAME2 "samsynctest2"
36 try a netlogon SamLogon
38 static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
39 struct creds_CredentialState *creds,
40 const char *domain, const char *account_name,
41 const char *workstation,
42 struct samr_Password *lm_hash,
43 struct samr_Password *nt_hash,
44 struct netr_SamInfo3 **info3)
47 struct netr_LogonSamLogon r;
48 struct netr_Authenticator auth, auth2;
49 struct netr_NetworkInfo ninfo;
51 ninfo.identity_info.domain_name.string = domain;
52 ninfo.identity_info.parameter_control = 0;
53 ninfo.identity_info.logon_id_low = 0;
54 ninfo.identity_info.logon_id_high = 0;
55 ninfo.identity_info.account_name.string = account_name;
56 ninfo.identity_info.workstation.string = workstation;
57 generate_random_buffer(ninfo.challenge,
58 sizeof(ninfo.challenge));
61 ninfo.nt.data = talloc(mem_ctx, 24);
62 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
70 ninfo.lm.data = talloc(mem_ctx, 24);
71 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
77 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
78 r.in.workstation = workstation;
79 r.in.credential = &auth;
80 r.in.return_authenticator = &auth2;
82 r.in.logon.network = &ninfo;
85 creds_client_authenticator(creds, &auth);
87 r.in.validation_level = 3;
89 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
91 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
92 printf("Credential chaining failed\n");
96 *info3 = r.out.validation.sam3;
102 struct samsync_state {
103 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
105 char *domain_name[2];
106 struct samsync_secret *secrets;
107 struct samsync_trusted_domain *trusted_domains;
108 struct creds_CredentialState *creds;
109 struct creds_CredentialState *creds_netlogon_wksta;
110 struct policy_handle *connect_handle;
111 struct policy_handle *domain_handle[2];
112 struct dom_sid *sid[2];
113 struct dcerpc_pipe *p;
114 struct dcerpc_pipe *p_netlogon_wksta;
115 struct dcerpc_pipe *p_samr;
116 struct dcerpc_pipe *p_lsa;
117 struct policy_handle *lsa_handle;
120 struct samsync_secret {
121 struct samsync_secret *prev, *next;
126 struct samsync_trusted_domain {
127 struct samsync_trusted_domain *prev, *next;
132 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
133 struct samsync_state *samsync_state,
135 struct dom_sid **sid)
137 struct samr_String name;
138 struct samr_OpenDomain o;
139 struct samr_LookupDomain l;
140 struct policy_handle *domain_handle = talloc_p(mem_ctx, struct policy_handle);
143 name.string = domain;
144 l.in.connect_handle = samsync_state->connect_handle;
147 nt_status = dcerpc_samr_LookupDomain(samsync_state->p_samr, mem_ctx, &l);
148 if (!NT_STATUS_IS_OK(nt_status)) {
149 printf("LookupDomain failed - %s\n", nt_errstr(nt_status));
153 o.in.connect_handle = samsync_state->connect_handle;
154 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
155 o.in.sid = l.out.sid;
156 o.out.domain_handle = domain_handle;
162 nt_status = dcerpc_samr_OpenDomain(samsync_state->p_samr, mem_ctx, &o);
163 if (!NT_STATUS_IS_OK(nt_status)) {
164 printf("OpenDomain failed - %s\n", nt_errstr(nt_status));
168 return domain_handle;
171 static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
172 struct samsync_state *samsync_state,
173 struct policy_handle *handle)
175 struct samr_QuerySecurity r;
178 r.in.handle = handle;
181 status = dcerpc_samr_QuerySecurity(samsync_state->p_samr, mem_ctx, &r);
182 if (!NT_STATUS_IS_OK(status)) {
183 printf("SAMR QuerySecurity failed - %s\n", nt_errstr(status));
190 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
191 struct samsync_state *samsync_state,
192 struct policy_handle *handle)
194 struct lsa_QuerySecurity r;
197 r.in.handle = handle;
200 status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
201 if (!NT_STATUS_IS_OK(status)) {
202 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
209 #define TEST_UINT64_EQUAL(i1, i2) do {\
211 printf("uint64 mismatch: " #i1 ": 0x%08x%08x (%lld) != " #i2 ": 0x%08x%08x (%lld)\n", \
212 (uint32_t)(i1 >> 32), (uint32_t)(i1 & 0xFFFFFFFF), i1, \
213 (uint32_t)(i2 >> 32), (uint32_t)(i2 & 0xFFFFFFFF), i2);\
217 #define TEST_INT_EQUAL(i1, i2) do {\
219 printf("integer mismatch: " #i1 ":%d != " #i2 ": %d\n", \
224 #define TEST_TIME_EQUAL(t1, t2) do {\
226 printf("NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
227 nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
232 #define TEST_STRING_EQUAL(s1, s2) do {\
233 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
234 && strcmp_safe(s1.string, s2.string) != 0) {\
235 printf("string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
236 s1.string, s2.string);\
241 #define TEST_SID_EQUAL(s1, s2) do {\
242 if (!dom_sid_equal(s1, s2)) {\
243 printf("dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
244 dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
249 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
250 * get back the SACL part of the SD when we ask over SAMR */
252 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
253 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
255 if (!sdbuf || !sdbuf->sd) { \
256 printf("Could not obtain security descriptor to match " #sd1 "\n");\
259 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
260 ~SEC_DESC_SACL_PRESENT)) {\
261 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
262 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
263 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
269 static BOOL samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
270 int database_id, struct netr_DELTA_ENUM *delta)
272 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
273 struct dom_sid *dom_sid;
274 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
275 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
280 samsync_state->seq_num[database_id] =
281 domain->sequence_num;
282 switch (database_id) {
283 case SAM_DATABASE_DOMAIN:
285 case SAM_DATABASE_BUILTIN:
286 if (StrCaseCmp("BUILTIN", domain->domain_name.string) != 0) {
287 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
290 case SAM_DATABASE_PRIVS:
291 printf("DOMAIN entry on privs DB!\n");
296 if (!samsync_state->domain_name[database_id]) {
297 samsync_state->domain_name[database_id] =
298 talloc_reference(samsync_state, domain->domain_name.string);
300 if (StrCaseCmp(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
301 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
302 domain->domain_name.string);
307 if (!samsync_state->domain_handle[database_id]) {
308 samsync_state->domain_handle[database_id]
309 = samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
312 if (samsync_state->domain_handle[database_id]) {
313 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
316 printf("\tsequence_nums[%d/%s]=%llu\n",
317 database_id, domain->domain_name.string,
318 samsync_state->seq_num[database_id]);
320 for (i=0;i<ARRAY_SIZE(levels);i++) {
321 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
322 q[levels[i]].in.level = levels[i];
324 nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
326 if (!NT_STATUS_IS_OK(nt_status)) {
327 printf("QueryDomainInfo level %u failed - %s\n",
328 q[levels[i]].in.level, nt_errstr(nt_status));
333 TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
335 TEST_STRING_EQUAL(q[2].out.info->info2.comment, domain->comment);
336 TEST_STRING_EQUAL(q[4].out.info->info4.comment, domain->comment);
337 TEST_TIME_EQUAL(q[2].out.info->info2.force_logoff_time, domain->force_logoff_time);
338 TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
340 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
341 TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
342 TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
343 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
345 TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num,
346 domain->sequence_num);
347 TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time,
348 domain->domain_create_time);
349 TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time,
350 domain->domain_create_time);
352 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
357 static BOOL samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
358 int database_id, struct netr_DELTA_ENUM *delta)
360 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
362 samsync_state->seq_num[database_id] =
363 policy->sequence_num;
365 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
366 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
367 talloc_reference(samsync_state, policy->primary_domain_name.string);
369 if (StrCaseCmp(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
370 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
371 policy->primary_domain_name.string);
376 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
377 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
378 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
382 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
384 samsync_state->seq_num[database_id]);
388 static BOOL samsync_handle_user(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
389 int database_id, struct netr_DELTA_ENUM *delta)
391 uint32 rid = delta->delta_id_union.rid;
392 struct netr_DELTA_USER *user = delta->delta_union.user;
393 struct netr_SamInfo3 *info3;
394 struct samr_Password lm_hash;
395 struct samr_Password nt_hash;
396 struct samr_Password *lm_hash_p = NULL;
397 struct samr_Password *nt_hash_p = NULL;
398 const char *domain = samsync_state->domain_name[database_id];
399 const char *username = user->account_name.string;
403 struct samr_OpenUser r;
404 struct samr_QueryUserInfo q;
405 struct policy_handle user_handle;
407 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
408 printf("SamSync needs domain information before the users\n");
412 r.in.domain_handle = samsync_state->domain_handle[database_id];
413 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
415 r.out.user_handle = &user_handle;
417 nt_status = dcerpc_samr_OpenUser(samsync_state->p_samr, mem_ctx, &r);
418 if (!NT_STATUS_IS_OK(nt_status)) {
419 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
423 q.in.user_handle = &user_handle;
426 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
428 nt_status = dcerpc_samr_QueryUserInfo(samsync_state->p_samr, mem_ctx, &q);
429 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &user_handle)) {
433 if (!NT_STATUS_IS_OK(nt_status)) {
434 printf("QueryUserInfo level %u failed - %s\n",
435 q.in.level, nt_errstr(nt_status));
439 TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
440 TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
441 TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
442 TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
443 TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
444 TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
445 TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
446 TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
447 TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
449 TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
450 TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
453 TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week,
454 user->logon_hours.units_per_week);
456 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits,
457 q.out.info->info21.logon_hours.units_per_week/8) != 0) {
458 printf("Logon hours mismatch\n");
463 TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
464 user->bad_password_count);
465 TEST_INT_EQUAL(q.out.info->info21.logon_count,
468 TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
469 user->last_password_change);
470 TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
473 TEST_INT_EQUAL(q.out.info->info21.acct_flags, user->acct_flags);
474 TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
475 TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
476 TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
478 TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
479 TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
481 TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
482 TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
484 TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
486 if (user->lm_password_present) {
487 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
488 lm_hash_p = &lm_hash;
490 if (user->nt_password_present) {
491 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
492 nt_hash_p = &nt_hash;
495 if (user->user_private_info.SensitiveData) {
497 struct netr_USER_KEYS keys;
498 data.data = user->user_private_info.SensitiveData;
499 data.length = user->user_private_info.DataLength;
500 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
502 printf("Sensitive Data for %s:\n", username);
503 dump_data(0, data.data, data.length);
505 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
506 if (NT_STATUS_IS_OK(nt_status)) {
507 if (keys.keys.keys2.lmpassword.length == 16) {
508 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
509 lm_hash_p = &lm_hash;
511 if (keys.keys.keys2.ntpassword.length == 16) {
512 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
513 nt_hash_p = &nt_hash;
519 nt_status = test_SamLogon(samsync_state->p, mem_ctx, samsync_state->creds,
527 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
528 if (user->acct_flags & ACB_DISABLED) {
531 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
532 if (user->acct_flags & ACB_WSTRUST) {
535 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
536 if (user->acct_flags & ACB_SVRTRUST) {
539 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
540 if (user->acct_flags & ACB_DOMTRUST) {
543 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
544 if (user->acct_flags & ACB_DOMTRUST) {
547 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
548 if (user->acct_flags & ACB_AUTOLOCK) {
551 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
552 if (!lm_hash_p && !nt_hash_p) {
555 } else if (NT_STATUS_IS_OK(nt_status)) {
556 TEST_INT_EQUAL(user->rid, info3->base.rid);
557 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
558 /* this is 0x0 from NT4 sp6 */
559 if (info3->base.acct_flags) {
560 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
562 /* this is NULL from NT4 sp6 */
563 if (info3->base.account_name.string) {
564 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
566 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
567 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
568 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
569 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
570 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
571 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
574 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
575 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
576 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
578 /* Does the concept of a logoff time ever really
579 * exist? (not in any sensible way, according to the
580 * doco I read -- abartlet) */
582 /* This copes with the two different versions of 0 I see */
583 /* with NT4 sp6 we have the || case */
584 if (!((user->last_logoff == 0)
585 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
586 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
590 printf("Could not validate password for user %s\\%s: %s\n",
591 domain, username, nt_errstr(nt_status));
597 static BOOL samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
598 int database_id, struct netr_DELTA_ENUM *delta)
600 uint32 rid = delta->delta_id_union.rid;
601 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
605 struct samr_OpenAlias r;
606 struct samr_QueryAliasInfo q;
607 struct policy_handle alias_handle;
609 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
610 printf("SamSync needs domain information before the users\n");
614 r.in.domain_handle = samsync_state->domain_handle[database_id];
615 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
617 r.out.alias_handle = &alias_handle;
619 nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
620 if (!NT_STATUS_IS_OK(nt_status)) {
621 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
625 q.in.alias_handle = &alias_handle;
628 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
630 nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
631 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
635 if (!NT_STATUS_IS_OK(nt_status)) {
636 printf("QueryAliasInfo level %u failed - %s\n",
637 q.in.level, nt_errstr(nt_status));
641 TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
642 TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
646 static BOOL samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
647 int database_id, struct netr_DELTA_ENUM *delta)
649 uint32 rid = delta->delta_id_union.rid;
650 struct netr_DELTA_GROUP *group = delta->delta_union.group;
654 struct samr_OpenGroup r;
655 struct samr_QueryGroupInfo q;
656 struct policy_handle group_handle;
658 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
659 printf("SamSync needs domain information before the users\n");
663 r.in.domain_handle = samsync_state->domain_handle[database_id];
664 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
666 r.out.group_handle = &group_handle;
668 nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
669 if (!NT_STATUS_IS_OK(nt_status)) {
670 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
674 q.in.group_handle = &group_handle;
677 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
679 nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
680 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
684 if (!NT_STATUS_IS_OK(nt_status)) {
685 printf("QueryGroupInfo level %u failed - %s\n",
686 q.in.level, nt_errstr(nt_status));
690 TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
691 TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
692 TEST_STRING_EQUAL(q.out.info->all.description, group->description);
696 static BOOL samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
697 int database_id, struct netr_DELTA_ENUM *delta)
699 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
700 const char *name = delta->delta_id_union.name;
701 struct samsync_secret *new = talloc_p(samsync_state, struct samsync_secret);
702 struct lsa_QuerySecret q;
703 struct lsa_OpenSecret o;
704 struct policy_handle sec_handle;
705 struct lsa_DATA_BUF_PTR bufp1;
708 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
711 creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data,
712 secret->current_cipher.maxlen);
714 creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data,
715 secret->old_cipher.maxlen);
717 new->name = talloc_reference(new, name);
718 new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
720 DLIST_ADD(samsync_state->secrets, new);
722 o.in.handle = samsync_state->lsa_handle;
723 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
724 o.in.name.string = name;
725 o.out.sec_handle = &sec_handle;
727 status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
728 if (!NT_STATUS_IS_OK(status)) {
729 printf("OpenSecret failed - %s\n", nt_errstr(status));
733 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
735 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
736 if (!NT_STATUS_IS_OK(status)) {
737 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
742 ZERO_STRUCT(new_mtime);
744 /* fetch the secret back again */
745 q.in.handle = &sec_handle;
746 q.in.new_val = &bufp1;
747 q.in.new_mtime = &new_mtime;
749 q.in.old_mtime = NULL;
753 status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
754 if (!NT_STATUS_IS_OK(status)) {
755 printf("QuerySecret failed - %s\n", nt_errstr(status));
759 if (q.out.new_val->buf == NULL) {
760 /* probably just not available due to ACLs */
762 lsa_blob1.data = q.out.new_val->buf->data;
763 lsa_blob1.length = q.out.new_val->buf->length;
765 lsa_blob_out = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key);
767 if (new->secret.length != lsa_blob_out.length) {
768 printf("Returned secret %s doesn't match: %d != %d\n",
769 new->name, new->secret.length, lsa_blob_out.length);
773 if (memcmp(lsa_blob_out.data,
774 new->secret.data, new->secret.length) != 0) {
775 printf("Returned secret %s doesn't match: \n",
777 DEBUG(1, ("SamSync Secret:\n"));
778 dump_data(1, new->secret.data, new->secret.length);
779 DEBUG(1, ("LSA Secret:\n"));
780 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
788 static BOOL samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
789 int database_id, struct netr_DELTA_ENUM *delta)
793 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
794 struct dom_sid *dom_sid = delta->delta_id_union.sid;
796 struct samsync_trusted_domain *new = talloc_p(samsync_state, struct samsync_trusted_domain);
797 struct lsa_OpenTrustedDomain t;
798 struct policy_handle trustdom_handle;
799 struct lsa_QueryTrustedDomainInfo q;
800 union lsa_TrustedDomainInfo *info[9];
801 int levels [] = {1, 3, 8};
804 new->name = talloc_reference(new, trusted_domain->domain_name.string);
805 new->sid = talloc_reference(new, dom_sid);
807 t.in.handle = samsync_state->lsa_handle;
808 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
810 t.out.trustdom_handle = &trustdom_handle;
812 status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
813 if (!NT_STATUS_IS_OK(status)) {
814 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
818 for (i=0; i< ARRAY_SIZE(levels); i++) {
819 q.in.trustdom_handle = &trustdom_handle;
820 q.in.level = levels[i];
821 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
822 if (!NT_STATUS_IS_OK(status)) {
823 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
824 info[levels[i]] = NULL;
827 printf("QueryInfoTrustedDomain level %d failed - %s\n",
828 levels[i], nt_errstr(status));
831 info[levels[i]] = q.out.info;
835 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
836 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
838 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
839 TEST_INT_EQUAL(info[3]->flags.flags, trusted_domain->flags);
840 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
842 DLIST_ADD(samsync_state->trusted_domains, new);
847 static BOOL samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
848 int database_id, struct netr_DELTA_ENUM *delta)
852 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
853 struct dom_sid *dom_sid = delta->delta_id_union.sid;
855 struct lsa_OpenAccount a;
856 struct policy_handle acct_handle;
857 struct lsa_EnumPrivsAccount e;
858 struct lsa_LookupPrivName r;
862 BOOL *found_priv_in_lsa;
864 a.in.handle = samsync_state->lsa_handle;
865 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
867 a.out.acct_handle = &acct_handle;
869 status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
870 if (!NT_STATUS_IS_OK(status)) {
871 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
875 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
877 found_priv_in_lsa = talloc_zero_array_p(mem_ctx, BOOL, account->privilege_entries);
879 e.in.handle = &acct_handle;
881 status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
882 if (!NT_STATUS_IS_OK(status)) {
883 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
888 return account->privilege_entries != 0;
891 if ((account->privilege_entries && !e.out.privs)) {
895 TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
897 for (i=0;i< e.out.privs->count; i++) {
898 r.in.handle = samsync_state->lsa_handle;
899 r.in.luid = &e.out.privs->set[i].luid;
901 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
902 if (!NT_STATUS_IS_OK(status)) {
903 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
908 printf("\nLookupPrivName failed to return a name\n");
911 for (j=0;j<account->privilege_entries; j++) {
912 if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
913 found_priv_in_lsa[j] = True;
918 for (j=0;j<account->privilege_entries; j++) {
919 if (!found_priv_in_lsa[j]) {
920 printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string,
921 dom_sid_string(mem_ctx, dom_sid));
929 try a netlogon DatabaseSync
931 static BOOL test_DatabaseSync(struct samsync_state *samsync_state,
935 struct netr_DatabaseSync r;
936 const uint32_t database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
939 struct samsync_trusted_domain *t;
940 struct samsync_secret *s;
942 const char *domain, *username;
944 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
945 r.in.computername = TEST_MACHINE_NAME;
946 r.in.preferredmaximumlength = (uint32_t)-1;
947 ZERO_STRUCT(r.in.return_authenticator);
949 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
950 r.in.sync_context = 0;
951 r.in.database_id = database_ids[i];
953 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
956 creds_client_authenticator(samsync_state->creds, &r.in.credential);
958 status = dcerpc_netr_DatabaseSync(samsync_state->p, mem_ctx, &r);
959 if (!NT_STATUS_IS_OK(status) &&
960 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
961 printf("DatabaseSync - %s\n", nt_errstr(status));
966 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
967 printf("Credential chaining failed\n");
970 r.in.sync_context = r.out.sync_context;
972 for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
973 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
974 case NETR_DELTA_DOMAIN:
975 ret &= samsync_handle_domain(mem_ctx, samsync_state,
976 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
978 case NETR_DELTA_GROUP:
979 ret &= samsync_handle_group(mem_ctx, samsync_state,
980 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
982 case NETR_DELTA_USER:
983 ret &= samsync_handle_user(mem_ctx, samsync_state,
984 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
986 case NETR_DELTA_ALIAS:
987 ret &= samsync_handle_alias(mem_ctx, samsync_state,
988 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
990 case NETR_DELTA_POLICY:
991 ret &= samsync_handle_policy(mem_ctx, samsync_state,
992 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
994 case NETR_DELTA_TRUSTED_DOMAIN:
995 ret &= samsync_handle_trusted_domain(mem_ctx, samsync_state,
996 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
998 case NETR_DELTA_ACCOUNT:
999 ret &= samsync_handle_account(mem_ctx, samsync_state,
1000 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1002 case NETR_DELTA_SECRET:
1003 ret &= samsync_handle_secret(mem_ctx, samsync_state,
1004 r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1008 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1012 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1014 printf("Never got a DOMAIN object in samsync!\n");
1018 username = talloc_asprintf(mem_ctx, "%s$", domain);
1019 for (t=samsync_state->trusted_domains; t; t=t->next) {
1020 char *secret_name = talloc_asprintf(mem_ctx, "G$$%s", t->name);
1021 for (s=samsync_state->secrets; s; s=s->next) {
1022 if (StrCaseCmp(s->name, secret_name) == 0) {
1024 struct samr_Password nt_hash;
1025 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1027 printf("Checking password for %s\\%s\n", t->name, username);
1028 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
1035 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1036 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1037 t->name, nt_errstr(nt_status));
1041 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1042 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1043 t->name, nt_errstr(nt_status));
1049 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
1057 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1058 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1059 t->name, nt_errstr(nt_status));
1073 try a netlogon DatabaseDeltas
1075 static BOOL test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1078 struct netr_DatabaseDeltas r;
1079 const uint32_t database_ids[] = {0, 1, 2};
1083 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1084 r.in.computername = TEST_MACHINE_NAME;
1085 r.in.preferredmaximumlength = (uint32_t)-1;
1086 ZERO_STRUCT(r.in.return_authenticator);
1088 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1089 r.in.database_id = database_ids[i];
1090 r.in.sequence_num = samsync_state->seq_num[i];
1092 if (r.in.sequence_num == 0) continue;
1094 r.in.sequence_num -= 1;
1097 printf("Testing DatabaseDeltas of id %d at %llu\n",
1098 r.in.database_id, r.in.sequence_num);
1101 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1103 status = dcerpc_netr_DatabaseDeltas(samsync_state->p, mem_ctx, &r);
1104 if (!NT_STATUS_IS_OK(status) &&
1105 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1106 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1111 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1112 printf("Credential chaining failed\n");
1115 r.in.sequence_num++;
1116 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1124 try a netlogon DatabaseSync2
1126 static BOOL test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1127 struct creds_CredentialState *creds)
1130 struct netr_DatabaseSync2 r;
1131 const uint32_t database_ids[] = {0, 1, 2};
1135 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1136 r.in.computername = TEST_MACHINE_NAME;
1137 r.in.preferredmaximumlength = (uint32_t)-1;
1138 ZERO_STRUCT(r.in.return_authenticator);
1140 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1141 r.in.sync_context = 0;
1142 r.in.database_id = database_ids[i];
1143 r.in.restart_state = 0;
1145 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1148 creds_client_authenticator(creds, &r.in.credential);
1150 status = dcerpc_netr_DatabaseSync2(p, mem_ctx, &r);
1151 if (!NT_STATUS_IS_OK(status) &&
1152 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1153 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1158 if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1159 printf("Credential chaining failed\n");
1162 r.in.sync_context = r.out.sync_context;
1163 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1171 BOOL torture_rpc_samsync(void)
1174 TALLOC_CTX *mem_ctx;
1176 struct test_join *join_ctx;
1177 struct test_join *join_ctx2;
1178 const char *machine_password;
1179 const char *machine_password2;
1180 const char *binding = lp_parm_string(-1, "torture", "binding");
1181 struct dcerpc_binding b;
1182 struct dcerpc_binding b_netlogon_wksta;
1183 struct samr_Connect c;
1184 struct samr_SetDomainInfo s;
1185 struct policy_handle *domain_policy;
1187 struct lsa_ObjectAttribute attr;
1188 struct lsa_QosInfo qos;
1189 struct lsa_OpenPolicy2 r;
1191 struct samsync_state *samsync_state;
1193 mem_ctx = talloc_init("torture_rpc_netlogon");
1195 join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST,
1198 printf("Failed to join as BDC\n");
1202 join_ctx2 = torture_join_domain(TEST_MACHINE_NAME2, lp_workgroup(), ACB_WSTRUST,
1203 &machine_password2);
1205 printf("Failed to join as member\n");
1209 samsync_state = talloc_zero_p(mem_ctx, struct samsync_state);
1211 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1212 samsync_state->connect_handle = talloc_zero_p(samsync_state, struct policy_handle);
1213 samsync_state->lsa_handle = talloc_zero_p(samsync_state, struct policy_handle);
1214 c.in.system_name = NULL;
1215 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1216 c.out.connect_handle = samsync_state->connect_handle;
1218 status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1219 if (!NT_STATUS_IS_OK(status)) {
1220 printf("samr_Connect failed\n");
1225 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(), NULL);
1226 if (!domain_policy) {
1227 printf("samrsync_open_domain failed\n");
1232 s.in.domain_handle = domain_policy;
1234 s.in.info = talloc_p(mem_ctx, union samr_DomainInfo);
1236 s.in.info->info4.comment.string
1237 = talloc_asprintf(mem_ctx,
1238 "Tortured by Samba4: %s",
1239 timestring(mem_ctx, time(NULL)));
1240 status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1242 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1247 if (!NT_STATUS_IS_OK(status)) {
1248 printf("SetDomainInfo level %u failed - %s\n",
1249 s.in.level, nt_errstr(status));
1255 status = torture_rpc_connection(&samsync_state->p_lsa,
1258 DCERPC_LSARPC_VERSION);
1260 if (!NT_STATUS_IS_OK(status)) {
1266 qos.impersonation_level = 2;
1267 qos.context_mode = 1;
1268 qos.effective_only = 0;
1271 attr.root_dir = NULL;
1272 attr.object_name = NULL;
1273 attr.attributes = 0;
1274 attr.sec_desc = NULL;
1275 attr.sec_qos = &qos;
1277 r.in.system_name = "\\";
1279 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1280 r.out.handle = samsync_state->lsa_handle;
1282 status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1283 if (!NT_STATUS_IS_OK(status)) {
1284 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1289 status = dcerpc_parse_binding(mem_ctx, binding, &b);
1290 if (!NT_STATUS_IS_OK(status)) {
1291 printf("Bad binding string %s\n", binding);
1296 b.flags &= ~DCERPC_AUTH_OPTIONS;
1297 b.flags |= DCERPC_SCHANNEL_BDC | DCERPC_SIGN;
1299 status = dcerpc_pipe_connect_b(&samsync_state->p, &b,
1300 DCERPC_NETLOGON_UUID,
1301 DCERPC_NETLOGON_VERSION,
1306 if (!NT_STATUS_IS_OK(status)) {
1311 status = dcerpc_schannel_creds(samsync_state->p->security_state.generic_state, mem_ctx, &samsync_state->creds);
1312 if (!NT_STATUS_IS_OK(status)) {
1318 status = dcerpc_parse_binding(mem_ctx, binding, &b_netlogon_wksta);
1319 if (!NT_STATUS_IS_OK(status)) {
1320 printf("Bad binding string %s\n", binding);
1325 b_netlogon_wksta.flags &= ~DCERPC_AUTH_OPTIONS;
1326 b_netlogon_wksta.flags |= DCERPC_SCHANNEL_WORKSTATION | DCERPC_SIGN;
1328 status = dcerpc_pipe_connect_b(&samsync_state->p_netlogon_wksta, &b_netlogon_wksta,
1329 DCERPC_NETLOGON_UUID,
1330 DCERPC_NETLOGON_VERSION,
1335 if (!NT_STATUS_IS_OK(status)) {
1340 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->security_state.generic_state, mem_ctx, &samsync_state->creds_netlogon_wksta);
1341 if (!NT_STATUS_IS_OK(status)) {
1345 if (!test_DatabaseSync(samsync_state, mem_ctx)) {
1349 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1353 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1357 torture_rpc_close(samsync_state->p);
1359 torture_leave_domain(join_ctx);
1360 torture_leave_domain(join_ctx2);
1362 talloc_destroy(mem_ctx);