samr.c

来自「samba最新软件」· C语言 代码 · 共 2,122 行 · 第 1/5 页

C
2,122
字号
/*    Unix SMB/CIFS implementation.   test suite for samr rpc operations   Copyright (C) Andrew Tridgell 2003   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003      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 "torture/torture.h"#include "system/time.h"#include "librpc/gen_ndr/lsa.h"#include "librpc/gen_ndr/ndr_samr_c.h"#include "lib/crypto/crypto.h"#include "libcli/auth/libcli_auth.h"#include "libcli/security/security.h"#include "torture/rpc/rpc.h"#include "param/param.h"#define TEST_ACCOUNT_NAME "samrtorturetest"#define TEST_ALIASNAME "samrtorturetestalias"#define TEST_GROUPNAME "samrtorturetestgroup"#define TEST_MACHINENAME "samrtestmach$"#define TEST_DOMAINNAME "samrtestdom$"enum torture_samr_choice {	TORTURE_SAMR_PASSWORDS,	TORTURE_SAMR_USER_ATTRIBUTES,	TORTURE_SAMR_OTHER};static bool test_QueryUserInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 			       struct policy_handle *handle);static bool test_QueryUserInfo2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 				struct policy_handle *handle);static bool test_QueryAliasInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,			       struct policy_handle *handle);static bool test_ChangePassword(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 				const char *acct_name, 				struct policy_handle *domain_handle, char **password);static void init_lsa_String(struct lsa_String *string, const char *s){	string->string = s;}bool test_samr_handle_Close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 				   struct policy_handle *handle){	NTSTATUS status;	struct samr_Close r;	r.in.handle = handle;	r.out.handle = handle;	status = dcerpc_samr_Close(p, mem_ctx, &r);	if (!NT_STATUS_IS_OK(status)) {		printf("Close handle failed - %s\n", nt_errstr(status));		return false;	}	return true;}static bool test_Shutdown(struct dcerpc_pipe *p, struct torture_context *tctx,		       struct policy_handle *handle){	NTSTATUS status;	struct samr_Shutdown r;	if (!torture_setting_bool(tctx, "dangerous", false)) {		printf("samr_Shutdown disabled - enable dangerous tests to use\n");		return true;	}	r.in.connect_handle = handle;	printf("testing samr_Shutdown\n");	status = dcerpc_samr_Shutdown(p, tctx, &r);	if (!NT_STATUS_IS_OK(status)) {		printf("samr_Shutdown failed - %s\n", nt_errstr(status));		return false;	}	return true;}static bool test_SetDsrmPassword(struct dcerpc_pipe *p, struct torture_context *tctx,				 struct policy_handle *handle){	NTSTATUS status;	struct samr_SetDsrmPassword r;	struct lsa_String string;	struct samr_Password hash;	if (!torture_setting_bool(tctx, "dangerous", false)) {		printf("samr_SetDsrmPassword disabled - enable dangerous tests to use\n");		return true;	}	E_md4hash("TeSTDSRM123", hash.hash);	init_lsa_String(&string, "Administrator");	r.in.name = &string;	r.in.unknown = 0;	r.in.hash = &hash;	printf("testing samr_SetDsrmPassword\n");	status = dcerpc_samr_SetDsrmPassword(p, tctx, &r);	if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {		printf("samr_SetDsrmPassword failed - %s\n", nt_errstr(status));		return false;	}	return true;}static bool test_QuerySecurity(struct dcerpc_pipe *p, 			       struct torture_context *tctx, 			       struct policy_handle *handle){	NTSTATUS status;	struct samr_QuerySecurity r;	struct samr_SetSecurity s;	r.in.handle = handle;	r.in.sec_info = 7;	status = dcerpc_samr_QuerySecurity(p, tctx, &r);	if (!NT_STATUS_IS_OK(status)) {		printf("QuerySecurity failed - %s\n", nt_errstr(status));		return false;	}	if (r.out.sdbuf == NULL) {		return false;	}	s.in.handle = handle;	s.in.sec_info = 7;	s.in.sdbuf = r.out.sdbuf;	if (torture_setting_bool(tctx, "samba4", false)) {		printf("skipping SetSecurity test against Samba4\n");		return true;	}	status = dcerpc_samr_SetSecurity(p, tctx, &s);	if (!NT_STATUS_IS_OK(status)) {		printf("SetSecurity failed - %s\n", nt_errstr(status));		return false;	}	status = dcerpc_samr_QuerySecurity(p, tctx, &r);	if (!NT_STATUS_IS_OK(status)) {		printf("QuerySecurity failed - %s\n", nt_errstr(status));		return false;	}	return true;}static bool test_SetUserInfo(struct dcerpc_pipe *p, struct torture_context *tctx, 			     struct policy_handle *handle, uint32_t base_acct_flags,			     const char *base_account_name){	NTSTATUS status;	struct samr_SetUserInfo s;	struct samr_SetUserInfo2 s2;	struct samr_QueryUserInfo q;	struct samr_QueryUserInfo q0;	union samr_UserInfo u;	bool ret = true;	const char *test_account_name;	uint32_t user_extra_flags = 0;	if (base_acct_flags == ACB_NORMAL) {		/* When created, accounts are expired by default */		user_extra_flags = ACB_PW_EXPIRED;	}	s.in.user_handle = handle;	s.in.info = &u;	s2.in.user_handle = handle;	s2.in.info = &u;	q.in.user_handle = handle;	q.out.info = &u;	q0 = q;#define TESTCALL(call, r) \		status = dcerpc_samr_ ##call(p, tctx, &r); \		if (!NT_STATUS_IS_OK(status)) { \			printf(#call " level %u failed - %s (%s)\n", \			       r.in.level, nt_errstr(status), __location__); \			ret = false; \			break; \		}#define STRING_EQUAL(s1, s2, field) \		if ((s1 && !s2) || (s2 && !s1) || strcmp(s1, s2)) { \			printf("Failed to set %s to '%s' (%s)\n", \			       #field, s2, __location__); \			ret = false; \			break; \		}#define INT_EQUAL(i1, i2, field) \		if (i1 != i2) { \			printf("Failed to set %s to 0x%llx - got 0x%llx (%s)\n", \			       #field, (unsigned long long)i2, (unsigned long long)i1, __location__); \			ret = false; \			break; \		}#define TEST_USERINFO_STRING(lvl1, field1, lvl2, field2, value, fpval) do { \		printf("field test %d/%s vs %d/%s\n", lvl1, #field1, lvl2, #field2); \		q.in.level = lvl1; \		TESTCALL(QueryUserInfo, q) \		s.in.level = lvl1; \		s2.in.level = lvl1; \		u = *q.out.info; \		if (lvl1 == 21) { \			ZERO_STRUCT(u.info21); \			u.info21.fields_present = fpval; \		} \		init_lsa_String(&u.info ## lvl1.field1, value); \		TESTCALL(SetUserInfo, s) \		TESTCALL(SetUserInfo2, s2) \		init_lsa_String(&u.info ## lvl1.field1, ""); \		TESTCALL(QueryUserInfo, q); \		u = *q.out.info; \		STRING_EQUAL(u.info ## lvl1.field1.string, value, field1); \		q.in.level = lvl2; \		TESTCALL(QueryUserInfo, q) \		u = *q.out.info; \		STRING_EQUAL(u.info ## lvl2.field2.string, value, field2); \	} while (0)#define TEST_USERINFO_INT_EXP(lvl1, field1, lvl2, field2, value, exp_value, fpval) do { \		printf("field test %d/%s vs %d/%s\n", lvl1, #field1, lvl2, #field2); \		q.in.level = lvl1; \		TESTCALL(QueryUserInfo, q) \		s.in.level = lvl1; \		s2.in.level = lvl1; \		u = *q.out.info; \		if (lvl1 == 21) { \			uint8_t *bits = u.info21.logon_hours.bits; \			ZERO_STRUCT(u.info21); \			if (fpval == SAMR_FIELD_LOGON_HOURS) { \				u.info21.logon_hours.units_per_week = 168; \				u.info21.logon_hours.bits = bits; \			} \			u.info21.fields_present = fpval; \		} \		u.info ## lvl1.field1 = value; \		TESTCALL(SetUserInfo, s) \		TESTCALL(SetUserInfo2, s2) \		u.info ## lvl1.field1 = 0; \		TESTCALL(QueryUserInfo, q); \		u = *q.out.info; \		INT_EQUAL(u.info ## lvl1.field1, exp_value, field1); \		q.in.level = lvl2; \		TESTCALL(QueryUserInfo, q) \		u = *q.out.info; \		INT_EQUAL(u.info ## lvl2.field2, exp_value, field1); \	} while (0)#define TEST_USERINFO_INT(lvl1, field1, lvl2, field2, value, fpval) do { \        TEST_USERINFO_INT_EXP(lvl1, field1, lvl2, field2, value, value, fpval); \        } while (0)	q0.in.level = 12;	do { TESTCALL(QueryUserInfo, q0) } while (0);	TEST_USERINFO_STRING(2, comment,  1, comment, "xx2-1 comment", 0);	TEST_USERINFO_STRING(2, comment, 21, comment, "xx2-21 comment", 0);	TEST_USERINFO_STRING(21, comment, 21, comment, "xx21-21 comment", 			   SAMR_FIELD_COMMENT);	test_account_name = talloc_asprintf(tctx, "%sxx7-1", base_account_name);	TEST_USERINFO_STRING(7, account_name,  1, account_name, base_account_name, 0);	test_account_name = talloc_asprintf(tctx, "%sxx7-3", base_account_name);	TEST_USERINFO_STRING(7, account_name,  3, account_name, base_account_name, 0);	test_account_name = talloc_asprintf(tctx, "%sxx7-5", base_account_name);	TEST_USERINFO_STRING(7, account_name,  5, account_name, base_account_name, 0);	test_account_name = talloc_asprintf(tctx, "%sxx7-6", base_account_name);	TEST_USERINFO_STRING(7, account_name,  6, account_name, base_account_name, 0);	test_account_name = talloc_asprintf(tctx, "%sxx7-7", base_account_name);	TEST_USERINFO_STRING(7, account_name,  7, account_name, base_account_name, 0);	test_account_name = talloc_asprintf(tctx, "%sxx7-21", base_account_name);	TEST_USERINFO_STRING(7, account_name, 21, account_name, base_account_name, 0);	test_account_name = base_account_name;	TEST_USERINFO_STRING(21, account_name, 21, account_name, base_account_name, 			   SAMR_FIELD_ACCOUNT_NAME);	TEST_USERINFO_STRING(6, full_name,  1, full_name, "xx6-1 full_name", 0);	TEST_USERINFO_STRING(6, full_name,  3, full_name, "xx6-3 full_name", 0);	TEST_USERINFO_STRING(6, full_name,  5, full_name, "xx6-5 full_name", 0);	TEST_USERINFO_STRING(6, full_name,  6, full_name, "xx6-6 full_name", 0);	TEST_USERINFO_STRING(6, full_name,  8, full_name, "xx6-8 full_name", 0);	TEST_USERINFO_STRING(6, full_name, 21, full_name, "xx6-21 full_name", 0);	TEST_USERINFO_STRING(8, full_name, 21, full_name, "xx8-21 full_name", 0);	TEST_USERINFO_STRING(21, full_name, 21, full_name, "xx21-21 full_name", 			   SAMR_FIELD_FULL_NAME);	TEST_USERINFO_STRING(6, full_name,  1, full_name, "", 0);	TEST_USERINFO_STRING(6, full_name,  3, full_name, "", 0);	TEST_USERINFO_STRING(6, full_name,  5, full_name, "", 0);	TEST_USERINFO_STRING(6, full_name,  6, full_name, "", 0);	TEST_USERINFO_STRING(6, full_name,  8, full_name, "", 0);	TEST_USERINFO_STRING(6, full_name, 21, full_name, "", 0);	TEST_USERINFO_STRING(8, full_name, 21, full_name, "", 0);	TEST_USERINFO_STRING(21, full_name, 21, full_name, "", 			   SAMR_FIELD_FULL_NAME);	TEST_USERINFO_STRING(11, logon_script, 3, logon_script, "xx11-3 logon_script", 0);	TEST_USERINFO_STRING(11, logon_script, 5, logon_script, "xx11-5 logon_script", 0);	TEST_USERINFO_STRING(11, logon_script, 21, logon_script, "xx11-21 logon_script", 0);	TEST_USERINFO_STRING(21, logon_script, 21, logon_script, "xx21-21 logon_script", 			   SAMR_FIELD_LOGON_SCRIPT);	TEST_USERINFO_STRING(12, profile_path,  3, profile_path, "xx12-3 profile_path", 0);	TEST_USERINFO_STRING(12, profile_path,  5, profile_path, "xx12-5 profile_path", 0);	TEST_USERINFO_STRING(12, profile_path, 21, profile_path, "xx12-21 profile_path", 0);	TEST_USERINFO_STRING(21, profile_path, 21, profile_path, "xx21-21 profile_path", 			   SAMR_FIELD_PROFILE_PATH);	TEST_USERINFO_STRING(10, home_directory, 3, home_directory, "xx10-3 home_directory", 0);	TEST_USERINFO_STRING(10, home_directory, 5, home_directory, "xx10-5 home_directory", 0);	TEST_USERINFO_STRING(10, home_directory, 21, home_directory, "xx10-21 home_directory", 0);	TEST_USERINFO_STRING(21, home_directory, 21, home_directory, "xx21-21 home_directory",			     SAMR_FIELD_HOME_DIRECTORY);	TEST_USERINFO_STRING(21, home_directory, 10, home_directory, "xx21-10 home_directory",			     SAMR_FIELD_HOME_DIRECTORY);	TEST_USERINFO_STRING(10, home_drive, 3, home_drive, "xx10-3 home_drive", 0);	TEST_USERINFO_STRING(10, home_drive, 5, home_drive, "xx10-5 home_drive", 0);	TEST_USERINFO_STRING(10, home_drive, 21, home_drive, "xx10-21 home_drive", 0);	TEST_USERINFO_STRING(21, home_drive, 21, home_drive, "xx21-21 home_drive",			     SAMR_FIELD_HOME_DRIVE);	TEST_USERINFO_STRING(21, home_drive, 10, home_drive, "xx21-10 home_drive",			     SAMR_FIELD_HOME_DRIVE);		TEST_USERINFO_STRING(13, description,  1, description, "xx13-1 description", 0);	TEST_USERINFO_STRING(13, description,  5, description, "xx13-5 description", 0);	TEST_USERINFO_STRING(13, description, 21, description, "xx13-21 description", 0);	TEST_USERINFO_STRING(21, description, 21, description, "xx21-21 description", 			   SAMR_FIELD_DESCRIPTION);	TEST_USERINFO_STRING(14, workstations,  3, workstations, "14workstation3", 0);	TEST_USERINFO_STRING(14, workstations,  5, workstations, "14workstation4", 0);	TEST_USERINFO_STRING(14, workstations, 21, workstations, "14workstation21", 0);	TEST_USERINFO_STRING(21, workstations, 21, workstations, "21workstation21", 			   SAMR_FIELD_WORKSTATIONS);	TEST_USERINFO_STRING(21, workstations, 3, workstations, "21workstation3", 			   SAMR_FIELD_WORKSTATIONS);	TEST_USERINFO_STRING(21, workstations, 5, workstations, "21workstation5", 			   SAMR_FIELD_WORKSTATIONS);	TEST_USERINFO_STRING(21, workstations, 14, workstations, "21workstation14", 			   SAMR_FIELD_WORKSTATIONS);	TEST_USERINFO_STRING(20, parameters, 21, parameters, "xx20-21 parameters", 0);	TEST_USERINFO_STRING(21, parameters, 21, parameters, "xx21-21 parameters", 			   SAMR_FIELD_PARAMETERS);	TEST_USERINFO_STRING(21, parameters, 20, parameters, "xx21-20 parameters", 			   SAMR_FIELD_PARAMETERS);	TEST_USERINFO_INT(2, country_code, 2, country_code, __LINE__, 0);	TEST_USERINFO_INT(2, country_code, 21, country_code, __LINE__, 0);	TEST_USERINFO_INT(21, country_code, 21, country_code, __LINE__, 			  SAMR_FIELD_COUNTRY_CODE);	TEST_USERINFO_INT(21, country_code, 2, country_code, __LINE__, 			  SAMR_FIELD_COUNTRY_CODE);	TEST_USERINFO_INT(2, code_page, 21, code_page, __LINE__, 0);	TEST_USERINFO_INT(21, code_page, 21, code_page, __LINE__, 			  SAMR_FIELD_CODE_PAGE);	TEST_USERINFO_INT(21, code_page, 2, code_page, __LINE__, 			  SAMR_FIELD_CODE_PAGE);	TEST_USERINFO_INT(17, acct_expiry, 21, acct_expiry, __LINE__, 0);	TEST_USERINFO_INT(17, acct_expiry, 5, acct_expiry, __LINE__, 0);	TEST_USERINFO_INT(21, acct_expiry, 21, acct_expiry, __LINE__, 			  SAMR_FIELD_ACCT_EXPIRY);	TEST_USERINFO_INT(21, acct_expiry, 5, acct_expiry, __LINE__, 			  SAMR_FIELD_ACCT_EXPIRY);	TEST_USERINFO_INT(21, acct_expiry, 17, acct_expiry, __LINE__, 			  SAMR_FIELD_ACCT_EXPIRY);	TEST_USERINFO_INT(4, logon_hours.bits[3],  3, logon_hours.bits[3], 1, 0);	TEST_USERINFO_INT(4, logon_hours.bits[3],  5, logon_hours.bits[3], 2, 0);	TEST_USERINFO_INT(4, logon_hours.bits[3], 21, logon_hours.bits[3], 3, 0);	TEST_USERINFO_INT(21, logon_hours.bits[3], 21, logon_hours.bits[3], 4, 			  SAMR_FIELD_LOGON_HOURS);	TEST_USERINFO_INT_EXP(16, acct_flags, 5, acct_flags, 			      (base_acct_flags  | ACB_DISABLED | ACB_HOMDIRREQ), 			      (base_acct_flags  | ACB_DISABLED | ACB_HOMDIRREQ | user_extra_flags), 			      0);	TEST_USERINFO_INT_EXP(16, acct_flags, 5, acct_flags, 			      (base_acct_flags  | ACB_DISABLED), 			      (base_acct_flags  | ACB_DISABLED | user_extra_flags), 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?