1d168347add44b5d751051dc4cdea9773f52feff
[nivanova/samba-autobuild/.git] / source4 / torture / gpo / apply.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) David Mulder 2017
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
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"
29 #include <unistd.h>
30
31 struct torture_suite *gpo_apply_suite(TALLOC_CTX *ctx)
32 {
33         struct torture_suite *suite = torture_suite_create(ctx, "apply");
34
35         torture_suite_add_simple_test(suite, "gpo_param_from_gpo",
36                                       torture_gpo_system_access_policies);
37
38         suite->description = talloc_strdup(suite, "Group Policy apply tests");
39
40         return suite;
41 }
42
43 static int exec_wait(const char **cmd)
44 {
45         int ret;
46         pid_t pid = fork();
47         switch (pid) {
48                 case 0:
49                         execv(cmd[0], discard_const_p(char *, &(cmd[1])));
50                         ret = -1;
51                         break;
52                 case -1:
53                         ret = errno;
54                         break;
55                 default:
56                         if (waitpid(pid, &ret, 0) < 0)
57                                 ret = errno;
58                         break;
59         }
60         return ret;
61 }
62
63 static int unix2nttime(char *sval)
64 {
65         return (strtoll(sval, NULL, 10) * -1 / 60 / 60 / 24 / 10000000);
66 }
67
68 #define GPODIR "addom.samba.example.com/Policies/"\
69                "{31B2F340-016D-11D2-945F-00C04FB984F9}/MACHINE/Microsoft/"\
70                "Windows NT/SecEdit"
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\
77 "
78 #define GPTINI "addom.samba.example.com/Policies/"\
79                "{31B2F340-016D-11D2-945F-00C04FB984F9}/GPT.INI"
80
81 bool torture_gpo_system_access_policies(struct torture_context *tctx)
82 {
83         int ret, vers = 0, i;
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[] = {
89                 "minPwdAge",
90                 "maxPwdAge",
91                 "minPwdLength",
92                 "pwdProperties",
93                 NULL
94         };
95         const struct ldb_val *val;
96         FILE *fp = NULL;
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;
103
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");
107
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);
112
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");
117
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");
122
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");
127
128         old_message = result->msgs[0];
129
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);
136                         fclose(fp);
137                 }
138
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")) ) {
142                         char line[256];
143                         while (fgets(line, 256, fp)) {
144                                 if (strncasecmp(line, "Version=", 8) == 0) {
145                                         vers = atoi(line+8);
146                                         break;
147                                 }
148                         }
149                         fclose(fp);
150                 }
151                 if ( (fp = fopen(gpt_file, "w")) ) {
152                         char *data = talloc_asprintf(tctx, "[General]\nVersion=%d\n",
153                                                      ++vers);
154                         fputs(data, fp);
155                         fclose(fp);
156                 }
157
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");
162
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");
167
168                 /* minPwdAge */
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");
172
173                 /* maxPwdAge */
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");
177
178                 /* minPwdLength */
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");
182
183                 /* pwdProperties */
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");
187         }
188
189         for (i = 0; i < old_message->num_elements; i++) {
190                 old_message->elements[i].flags = LDB_FLAG_MOD_REPLACE;
191         }
192
193         ret = ldb_modify(samdb, old_message);
194         torture_assert(tctx, ret == 0, "Failed to reset password settings.");
195
196         return true;
197 }