2 Unix SMB/CIFS implementation.
5 Copyright (C) Wilco Baan Hofman 2006
6 Copyright (C) Jelmer Vernooij 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 static BOOL samu_correct(struct samu *s1, struct samu *s2)
29 uint32 s1_len, s2_len;
30 const char *s1_buf, *s2_buf;
31 const uint8 *d1_buf, *d2_buf;
33 /* Check Unix username */
34 s1_buf = pdb_get_username(s1);
35 s2_buf = pdb_get_username(s2);
36 if (s2_buf == NULL && s1_buf != NULL) {
37 DEBUG(0, ("Username is not set\n"));
39 } else if (s1_buf == NULL) {
41 } else if (strcmp(s1_buf,s2_buf)) {
42 DEBUG(0, ("Username not written correctly, want %s, got \"%s\"\n",
44 pdb_get_username(s2)));
48 /* Check NT username */
49 s1_buf = pdb_get_nt_username(s1);
50 s2_buf = pdb_get_nt_username(s2);
51 if (s2_buf == NULL && s1_buf != NULL) {
52 DEBUG(0, ("NT Username is not set\n"));
54 } else if (s1_buf == NULL) {
56 } else if (strcmp(s1_buf, s2_buf)) {
57 DEBUG(0, ("NT Username not written correctly, want \"%s\", got \"%s\"\n",
58 pdb_get_nt_username(s1),
59 pdb_get_nt_username(s2)));
64 if (pdb_get_acct_ctrl(s1) != pdb_get_acct_ctrl(s2)) {
65 DEBUG(0, ("Acct ctrl field not written correctly, want %d (0x%X), got %d (0x%X)\n",
66 pdb_get_acct_ctrl(s1),
67 pdb_get_acct_ctrl(s1),
68 pdb_get_acct_ctrl(s2),
69 pdb_get_acct_ctrl(s2)));
73 /* Check NT password */
74 d1_buf = pdb_get_nt_passwd(s1);
75 d2_buf = pdb_get_nt_passwd(s2);
76 if (d2_buf == NULL && d1_buf != NULL) {
77 DEBUG(0, ("NT password is not set\n"));
79 } else if (d1_buf == NULL) {
81 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
82 DEBUG(0, ("NT password not written correctly\n"));
86 /* Check lanman password */
87 d1_buf = pdb_get_lanman_passwd(s1);
88 d2_buf = pdb_get_lanman_passwd(s2);
89 if (d2_buf == NULL && d1_buf != NULL) {
90 DEBUG(0, ("Lanman password is not set\n"));
91 } else if (d1_buf == NULL) {
93 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
94 DEBUG(0, ("Lanman password not written correctly\n"));
98 /* Check password history */
99 d1_buf = pdb_get_pw_history(s1, &s1_len);
100 d2_buf = pdb_get_pw_history(s2, &s2_len);
101 if (d2_buf == NULL && d1_buf != NULL) {
102 DEBUG(0, ("Password history is not set\n"));
103 } else if (d1_buf == NULL) {
105 } else if (s1_len != s1_len) {
106 DEBUG(0, ("Password history not written correctly, lengths differ, want %d, got %d\n",
109 } else if (strncmp(s1_buf, s2_buf, s1_len)) {
110 DEBUG(0, ("Password history not written correctly\n"));
114 /* Check logon time */
115 if (pdb_get_logon_time(s1) != pdb_get_logon_time(s2)) {
116 DEBUG(0, ("Logon time is not written correctly\n"));
120 /* Check logoff time */
121 if (pdb_get_logoff_time(s1) != pdb_get_logoff_time(s2)) {
122 DEBUG(0, ("Logoff time is not written correctly\n"));
126 /* Check kickoff time */
127 if (pdb_get_kickoff_time(s1) != pdb_get_logoff_time(s2)) {
128 DEBUG(0, ("Kickoff time is not written correctly\n"));
132 /* Check bad password time */
133 if (pdb_get_bad_password_time(s1) != pdb_get_bad_password_time(s2)) {
134 DEBUG(0, ("Bad password time is not written correctly\n"));
138 /* Check password last set time */
139 if (pdb_get_pass_last_set_time(s1) != pdb_get_pass_last_set_time(s2)) {
140 DEBUG(0, ("Password last set time is not written correctly\n"));
144 /* Check password can change time */
145 if (pdb_get_pass_can_change_time(s1) != pdb_get_pass_can_change_time(s2)) {
146 DEBUG(0, ("Password can change time is not written correctly\n"));
150 /* Check password must change time */
151 if (pdb_get_pass_must_change_time(s1) != pdb_get_pass_must_change_time(s2)) {
152 DEBUG(0, ("Password must change time is not written correctly\n"));
156 /* Check logon divs */
157 if (pdb_get_logon_divs(s1) != pdb_get_logon_divs(s2)) {
158 DEBUG(0, ("Logon divs not written correctly\n"));
162 /* Check logon hours */
163 if (pdb_get_hours_len(s1) != pdb_get_hours_len(s2)) {
164 DEBUG(0, ("Logon hours length not written correctly\n"));
166 } else if (pdb_get_hours_len(s1) != 0) {
167 d1_buf = pdb_get_hours(s1);
168 d2_buf = pdb_get_hours(s2);
169 if (d2_buf == NULL && d2_buf != NULL) {
170 DEBUG(0, ("Logon hours is not set\n"));
172 } else if (d1_buf == NULL) {
174 } else if (memcmp(d1_buf, d2_buf, MAX_HOURS_LEN)) {
175 DEBUG(0, ("Logon hours is not written correctly\n"));
180 /* Check profile path */
181 s1_buf = pdb_get_profile_path(s1);
182 s2_buf = pdb_get_profile_path(s2);
183 if (s2_buf == NULL && s1_buf != NULL) {
184 DEBUG(0, ("Profile path is not set\n"));
186 } else if (s1_buf == NULL) {
188 } else if (strcmp(s1_buf, s2_buf)) {
189 DEBUG(0, ("Profile path is not written correctly\n"));
194 s1_buf = pdb_get_homedir(s1);
195 s2_buf = pdb_get_homedir(s2);
196 if (s2_buf == NULL && s1_buf != NULL) {
197 DEBUG(0, ("Home dir is not set\n"));
199 } else if (s1_buf == NULL) {
201 } else if (strcmp(s1_buf, s2_buf)) {
202 DEBUG(0, ("Home dir is not written correctly\n"));
206 /* Check logon script */
207 s1_buf = pdb_get_logon_script(s1);
208 s2_buf = pdb_get_logon_script(s2);
209 if (s2_buf == NULL && s1_buf != NULL) {
210 DEBUG(0, ("Logon script not set\n"));
212 } else if (s1_buf == NULL) {
214 } else if (strcmp(s1_buf, s2_buf)) {
215 DEBUG(0, ("Logon script is not written correctly\n"));
219 /* TODO Check user and group sids */
225 int main(int argc, char **argv)
228 struct samu *out = NULL;
229 struct samu *in = NULL;
236 uint32 expire, min_age, history;
237 struct pdb_methods *pdb;
239 static const char *backend = NULL;
240 static const char *unix_user = "nobody";
241 struct poptOption long_options[] = {
242 {"username", 'u', POPT_ARG_STRING, &unix_user, 0, "Unix user to use for testing", "USERNAME" },
243 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "Backend to use if not default", "BACKEND[:SETTINGS]" },
251 pc = poptGetContext("vfstest", argc, (const char **) argv,
254 poptSetOtherOptionHelp(pc, "backend[:settings] username");
256 while(poptGetNextOpt(pc) != -1);
260 /* Load configuration */
261 lp_load(dyn_CONFIGFILE, False, False, True, True);
262 setup_logging("pdbtest", True);
264 if (backend == NULL) {
265 backend = lp_passdb_backend();
268 rv = make_pdb_method_name(&pdb, backend);
269 if (NT_STATUS_IS_ERR(rv)) {
270 fprintf(stderr, "Error initializing '%s': %s\n", backend, get_friendly_nt_error_msg(rv));
274 ctx = talloc_init("PDBTEST");
276 if (!(out = samu_new(ctx))) {
277 fprintf(stderr, "Can't create samu structure.\n");
281 if ((pwd = getpwnam_alloc(ctx, unix_user)) == NULL) {
282 fprintf(stderr, "Error getting user information for %s\n", unix_user);
286 samu_set_unix(out, pwd);
288 pdb_set_profile_path(out, "\\\\torture\\profile", PDB_SET);
289 pdb_set_homedir(out, "\\\\torture\\home", PDB_SET);
290 pdb_set_logon_script(out, "torture_script.cmd", PDB_SET);
292 pdb_get_account_policy(AP_PASSWORD_HISTORY, &history);
293 if (history * PW_HISTORY_ENTRY_LEN < NT_HASH_LEN) {
294 buf = (uint8 *)TALLOC(ctx, NT_HASH_LEN);
296 buf = (uint8 *)TALLOC(ctx, history * PW_HISTORY_ENTRY_LEN);
299 /* Generate some random hashes */
302 for (i = 0; i < NT_HASH_LEN; i++) {
303 buf[i] = (uint8) rand();
305 pdb_set_nt_passwd(out, buf, PDB_SET);
306 for (i = 0; i < LM_HASH_LEN; i++) {
307 buf[i] = (uint8) rand();
309 pdb_set_lanman_passwd(out, buf, PDB_SET);
310 for (i = 0; i < history * PW_HISTORY_ENTRY_LEN; i++) {
311 buf[i] = (uint8) rand();
313 pdb_set_pw_history(out, buf, history, PDB_SET);
315 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &expire);
316 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &min_age);
317 pdb_set_pass_last_set_time(out, time(NULL), PDB_SET);
319 if (expire == 0 || expire == (uint32)-1) {
320 pdb_set_pass_must_change_time(out, get_time_t_max(), PDB_SET);
322 pdb_set_pass_must_change_time(out, time(NULL)+expire, PDB_SET);
325 if (min_age == (uint32)-1) {
326 pdb_set_pass_can_change_time(out, 0, PDB_SET);
328 pdb_set_pass_can_change_time(out, time(NULL)+min_age, PDB_SET);
332 if (!NT_STATUS_IS_OK(rv = pdb->add_sam_account(pdb, out))) {
333 fprintf(stderr, "Error in add_sam_account: %s\n",
334 get_friendly_nt_error_msg(rv));
338 if (!(in = samu_new(ctx))) {
339 fprintf(stderr, "Can't create samu structure.\n");
343 /* Get account information through getsampwnam() */
344 if (NT_STATUS_IS_ERR(pdb->getsampwnam(pdb, in, out->username))) {
345 fprintf(stderr, "Error getting sampw of added user %s.\n",
347 if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
348 fprintf(stderr, "Error in delete_sam_account %s\n",
349 get_friendly_nt_error_msg(rv));
354 /* Verify integrity */
355 if (samu_correct(out, in)) {
356 printf("User info written correctly\n");
358 printf("User info NOT written correctly\n");
363 if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
364 fprintf(stderr, "Error in delete_sam_account %s\n",
365 get_friendly_nt_error_msg(rv));
368 pdb->setsampwent(pdb, False, 0);
369 while (NT_STATUS_IS_OK(pdb->getsampwent(pdb, out))) {
370 if (pdb_get_username(out) == NULL) {
371 fprintf(stderr, "Got bad username through getsampwent()\n");
375 if (NT_STATUS_IS_ERR(pdb->getsampwnam(pdb, in, pdb_get_username(out)))) {
376 fprintf(stderr, "Error getting samu through getsampwnam() of an account we got through getsampwent!\n");
380 if (!samu_correct(out, in)) {
381 printf("Record gotten through getsampwnam() differs from same record through getsampwent()\n");
384 pdb->endsampwent(pdb);