2 Unix SMB/CIFS implementation.
4 Copyright (C) David Mulder 2017
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 "param/param.h"
22 #include "param/loadparm.h"
23 #include "torture/smbtorture.h"
24 #include "lib/util/mkdir_p.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "auth/session.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "torture/gpo/proto.h"
31 struct torture_suite *gpo_apply_suite(TALLOC_CTX *ctx)
33 struct torture_suite *suite = torture_suite_create(ctx, "apply");
35 torture_suite_add_simple_test(suite, "gpo_param_from_gpo",
36 torture_gpo_system_access_policies);
38 suite->description = talloc_strdup(suite, "Group Policy apply tests");
43 static int exec_wait(const char **cmd)
49 execv(cmd[0], discard_const_p(char *, &(cmd[1])));
56 if (waitpid(pid, &ret, 0) < 0)
63 static int unix2nttime(char *sval)
65 return (strtoll(sval, NULL, 10) * -1 / 60 / 60 / 24 / 10000000);
68 #define GPODIR "addom.samba.example.com/Policies/"\
69 "{31B2F340-016D-11D2-945F-00C04FB984F9}/MACHINE/Microsoft/"\
71 #define GPOFILE "GptTmpl.inf"
72 #define GPTTMPL "[System Access]\n\
73 MinimumPasswordAge = %d\n\
74 MaximumPasswordAge = %d\n\
75 MinimumPasswordLength = %d\n\
76 PasswordComplexity = %d\n\
78 #define GPTINI "addom.samba.example.com/Policies/"\
79 "{31B2F340-016D-11D2-945F-00C04FB984F9}/GPT.INI"
81 bool torture_gpo_system_access_policies(struct torture_context *tctx)
84 const char *sysvol_path = NULL, *gpo_dir = NULL;
85 const char *gpo_file = NULL, *gpt_file = NULL;
86 struct ldb_context *samdb = NULL;
87 struct ldb_result *result;
88 const char *attrs[] = {
95 const struct ldb_val *val;
97 const char **gpo_update_cmd;
98 int minpwdcases[] = { 0, 1, 998 };
99 int maxpwdcases[] = { 0, 1, 999 };
100 int pwdlencases[] = { 0, 1, 14 };
101 int pwdpropcases[] = { 0, 1, 1 };
102 struct ldb_message *old_message = NULL;
104 sysvol_path = lpcfg_path(lpcfg_service(tctx->lp_ctx, "sysvol"),
105 lpcfg_default_service(tctx->lp_ctx), tctx);
106 torture_assert(tctx, sysvol_path, "Failed to fetch the sysvol path");
108 /* Ensure the sysvol path exists */
109 gpo_dir = talloc_asprintf(tctx, "%s/%s", sysvol_path, GPODIR);
110 mkdir_p(gpo_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
111 gpo_file = talloc_asprintf(tctx, "%s/%s", gpo_dir, GPOFILE);
113 /* Get the gpo update command */
114 gpo_update_cmd = lpcfg_gpo_update_command(tctx->lp_ctx);
115 torture_assert(tctx, gpo_update_cmd && gpo_update_cmd[0],
116 "Failed to fetch the gpo update command");
118 /* Open and read the samba db and store the initial password settings */
119 samdb = samdb_connect(tctx, tctx->ev, tctx->lp_ctx,
120 system_session(tctx->lp_ctx), 0);
121 torture_assert(tctx, samdb, "Failed to connect to the samdb");
123 ret = ldb_search(samdb, tctx, &result, ldb_get_default_basedn(samdb),
124 LDB_SCOPE_BASE, attrs, NULL);
125 torture_assert(tctx, ret == LDB_SUCCESS && result->count == 1,
126 "Searching the samdb failed");
128 old_message = result->msgs[0];
130 for (i = 0; i < 3; i++) {
131 /* Write out the sysvol */
132 if ( (fp = fopen(gpo_file, "w")) ) {
133 fputs(talloc_asprintf(tctx, GPTTMPL, minpwdcases[i],
134 maxpwdcases[i], pwdlencases[i],
135 pwdpropcases[i]), fp);
139 /* Update the version in the GPT.INI */
140 gpt_file = talloc_asprintf(tctx, "%s/%s", sysvol_path, GPTINI);
141 if ( (fp = fopen(gpt_file, "r")) ) {
143 while (fgets(line, 256, fp)) {
144 if (strncasecmp(line, "Version=", 8) == 0) {
151 if ( (fp = fopen(gpt_file, "w")) ) {
152 char *data = talloc_asprintf(tctx, "[General]\nVersion=%d\n",
158 /* Run the gpo update command */
159 ret = exec_wait(gpo_update_cmd);
160 torture_assert(tctx, ret == 0,
161 "Failed to execute the gpo update command");
163 ret = ldb_search(samdb, tctx, &result, ldb_get_default_basedn(samdb),
164 LDB_SCOPE_BASE, attrs, NULL);
165 torture_assert(tctx, ret == LDB_SUCCESS && result->count == 1,
166 "Searching the samdb failed");
169 val = ldb_msg_find_ldb_val(result->msgs[0], attrs[0]);
170 torture_assert(tctx, unix2nttime((char*)val->data) == minpwdcases[i],
171 "The minPwdAge was not applied");
174 val = ldb_msg_find_ldb_val(result->msgs[0], attrs[1]);
175 torture_assert(tctx, unix2nttime((char*)val->data) == maxpwdcases[i],
176 "The maxPwdAge was not applied");
179 val = ldb_msg_find_ldb_val(result->msgs[0], attrs[2]);
180 torture_assert(tctx, atoi((char*)val->data) == pwdlencases[i],
181 "The minPwdLength was not applied");
184 val = ldb_msg_find_ldb_val(result->msgs[0], attrs[3]);
185 torture_assert(tctx, atoi((char*)val->data) == pwdpropcases[i],
186 "The pwdProperties were not applied");
189 for (i = 0; i < old_message->num_elements; i++) {
190 old_message->elements[i].flags = LDB_FLAG_MOD_REPLACE;
193 ret = ldb_modify(samdb, old_message);
194 torture_assert(tctx, ret == 0, "Failed to reset password settings.");