adbd982ab4421e974df863dc99c9916096085946
[metze/samba/wip.git] / source3 / torture / pdbtest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    passdb testing utility
4
5    Copyright (C) Wilco Baan Hofman 2006
6    Copyright (C) Jelmer Vernooij 2006
7    Copyright (C) Andrew Bartlett 2012
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #include "includes.h"
25 #include "popt_common.h"
26 #include "passdb.h"
27
28 #include "../librpc/gen_ndr/drsblobs.h"
29 #include "../librpc/gen_ndr/ndr_drsblobs.h"
30 #include "../libcli/security/dom_sid.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "../auth/common_auth.h"
33 #include "lib/tsocket/tsocket.h"
34 #include "include/auth.h"
35 #include "nsswitch/libwbclient/wbclient.h"
36 #include "auth/auth_sam_reply.h"
37
38 #define TRUST_DOM "trustdom"
39 #define TRUST_PWD "trustpwd1232"
40 #define TRUST_SID "S-1-5-21-1111111111-2222222222-3333333333"
41
42 static bool samu_correct(struct samu *s1, struct samu *s2)
43 {
44         bool ret = True;
45         uint32_t s1_len, s2_len;
46         const char *s1_buf, *s2_buf;
47         const uint8_t *d1_buf, *d2_buf;
48         const struct dom_sid *s1_sid, *s2_sid;
49
50         /* Check Unix username */
51         s1_buf = pdb_get_username(s1);
52         s2_buf = pdb_get_username(s2);
53         if (s2_buf == NULL && s1_buf != NULL) {
54                 DEBUG(0, ("Username is not set\n"));
55                 ret = False;
56         } else if (s1_buf == NULL) {
57                 /* Do nothing */
58         } else if (strcmp(s1_buf,s2_buf)) {
59                 DEBUG(0, ("Username not written correctly, want %s, got \"%s\"\n",
60                                         pdb_get_username(s1),
61                                         pdb_get_username(s2)));
62                 ret = False;
63         }
64
65         /* Check NT username */
66         s1_buf = pdb_get_nt_username(s1);
67         s2_buf = pdb_get_nt_username(s2);
68         if (s2_buf == NULL && s1_buf != NULL) {
69                 DEBUG(0, ("NT Username is not set\n"));
70                 ret = False;
71         } else if (s1_buf == NULL) {
72                 /* Do nothing */
73         } else if (strcmp(s1_buf, s2_buf)) {
74                 DEBUG(0, ("NT Username not written correctly, want \"%s\", got \"%s\"\n",
75                                         pdb_get_nt_username(s1),
76                                         pdb_get_nt_username(s2)));
77                 ret = False;
78         }
79
80         /* Check acct ctrl */
81         if (pdb_get_acct_ctrl(s1) != pdb_get_acct_ctrl(s2)) {
82                 DEBUG(0, ("Acct ctrl field not written correctly, want %d (0x%X), got %d (0x%X)\n",
83                                         pdb_get_acct_ctrl(s1),
84                                         pdb_get_acct_ctrl(s1),
85                                         pdb_get_acct_ctrl(s2),
86                                         pdb_get_acct_ctrl(s2)));
87                 ret = False;
88         }
89
90         /* Check NT password */
91         d1_buf = pdb_get_nt_passwd(s1);
92         d2_buf = pdb_get_nt_passwd(s2);
93         if (d2_buf == NULL && d1_buf != NULL) {
94                 DEBUG(0, ("NT password is not set\n"));
95                 ret = False;
96         } else if (d1_buf == NULL) {
97                 /* Do nothing */
98         } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
99                 DEBUG(0, ("NT password not written correctly\n"));
100                 ret = False;
101         }
102
103         /* Check lanman password */
104         d1_buf = pdb_get_lanman_passwd(s1);
105         d2_buf = pdb_get_lanman_passwd(s2);
106         if (d2_buf == NULL && d1_buf != NULL) {
107                 DEBUG(0, ("Lanman password is not set\n"));
108         } else if (d1_buf == NULL) {
109                 /* Do nothing */
110         } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
111                 DEBUG(0, ("Lanman password not written correctly\n"));
112                 ret = False;
113         }
114
115         /* Check password history */
116         d1_buf = pdb_get_pw_history(s1, &s1_len);
117         d2_buf = pdb_get_pw_history(s2, &s2_len);
118         if (d2_buf == NULL && d1_buf != NULL) {
119                 DEBUG(0, ("Password history is not set\n"));
120         } else if (d1_buf == NULL) {
121                 /* Do nothing */
122         } else if (s1_len != s2_len) {
123                 DEBUG(0, ("Password history not written correctly, lengths differ, want %d, got %d\n",
124                                         s1_len, s2_len));
125                 ret = False;
126         } else if (strncmp(s1_buf, s2_buf, s1_len)) {
127                 DEBUG(0, ("Password history not written correctly\n"));
128                 ret = False;
129         }
130
131         /* Check logon time */
132         if (pdb_get_logon_time(s1) != pdb_get_logon_time(s2)) {
133                 DEBUG(0, ("Logon time is not written correctly\n"));
134                 ret = False;
135         }
136
137         /* Check logoff time */
138         if (pdb_get_logoff_time(s1) != pdb_get_logoff_time(s2)) {
139                 DEBUG(0, ("Logoff time is not written correctly: %s vs %s \n",
140                           http_timestring(talloc_tos(), pdb_get_logoff_time(s1)),
141                           http_timestring(talloc_tos(), pdb_get_logoff_time(s2))));
142                 ret = False;
143         }
144
145         /* Check kickoff time */
146         if (pdb_get_kickoff_time(s1) != pdb_get_kickoff_time(s2)) {
147                 DEBUG(0, ("Kickoff time is not written correctly: %s vs %s \n",
148                           http_timestring(talloc_tos(), pdb_get_kickoff_time(s1)),
149                           http_timestring(talloc_tos(), pdb_get_kickoff_time(s2))));
150                 ret = False;
151         }
152
153         /* Check bad password time */
154         if (pdb_get_bad_password_time(s1) != pdb_get_bad_password_time(s2)) {
155                 DEBUG(0, ("Bad password time is not written correctly\n"));
156                 ret = False;
157         }
158
159         /* Check password last set time */
160         if (pdb_get_pass_last_set_time(s1) != pdb_get_pass_last_set_time(s2)) {
161                 DEBUG(0, ("Password last set time is not written correctly: %s vs %s \n",
162                           http_timestring(talloc_tos(), pdb_get_pass_last_set_time(s1)),
163                           http_timestring(talloc_tos(), pdb_get_pass_last_set_time(s2))));
164                 ret = False;
165         }
166
167         /* Check password can change time */
168         if (pdb_get_pass_can_change_time(s1) != pdb_get_pass_can_change_time(s2)) {
169                 DEBUG(0, ("Password can change time is not written correctly %s vs %s \n",
170                           http_timestring(talloc_tos(), pdb_get_pass_can_change_time(s1)),
171                           http_timestring(talloc_tos(), pdb_get_pass_can_change_time(s2))));
172                 ret = False;
173         }
174
175         /* Check password must change time */
176         if (pdb_get_pass_must_change_time(s1) != pdb_get_pass_must_change_time(s2)) {
177                 DEBUG(0, ("Password must change time is not written correctly\n"));
178                 ret = False;
179         }
180
181         /* Check logon divs */
182         if (pdb_get_logon_divs(s1) != pdb_get_logon_divs(s2)) {
183                 DEBUG(0, ("Logon divs not written correctly\n"));
184                 ret = False;
185         }
186
187         /* Check logon hours */
188         if (pdb_get_hours_len(s1) != pdb_get_hours_len(s2)) {
189                 DEBUG(0, ("Logon hours length not written correctly\n"));
190                 ret = False;
191         } else if (pdb_get_hours_len(s1) != 0) {
192                 d1_buf = pdb_get_hours(s1);
193                 d2_buf = pdb_get_hours(s2);
194                 if (d2_buf == NULL && d2_buf != NULL) {
195                         DEBUG(0, ("Logon hours is not set\n"));
196                         ret = False;
197                 } else if (d1_buf == NULL) {
198                         /* Do nothing */
199                 } else if (memcmp(d1_buf, d2_buf, MAX_HOURS_LEN)) {
200                         DEBUG(0, ("Logon hours is not written correctly\n"));
201                         ret = False;
202                 }
203         }
204
205         /* Check profile path */
206         s1_buf = pdb_get_profile_path(s1);
207         s2_buf = pdb_get_profile_path(s2);
208         if (s2_buf == NULL && s1_buf != NULL) {
209                 DEBUG(0, ("Profile path is not set\n"));
210                 ret = False;
211         } else if (s1_buf == NULL) {
212                 /* Do nothing */
213         } else if (strcmp(s1_buf, s2_buf)) {
214                 DEBUG(0, ("Profile path is not written correctly\n"));
215                 ret = False;
216         }
217
218         /* Check home dir */
219         s1_buf = pdb_get_homedir(s1);
220         s2_buf = pdb_get_homedir(s2);
221         if (s2_buf == NULL && s1_buf != NULL) {
222                 DEBUG(0, ("Home dir is not set\n"));
223                 ret = False;
224         } else if (s1_buf == NULL) {
225                 /* Do nothing */
226         } else if (strcmp(s1_buf, s2_buf)) {
227                 DEBUG(0, ("Home dir is not written correctly\n"));
228                 ret = False;
229         }
230
231         /* Check logon script */
232         s1_buf = pdb_get_logon_script(s1);
233         s2_buf = pdb_get_logon_script(s2);
234         if (s2_buf == NULL && s1_buf != NULL) {
235                 DEBUG(0, ("Logon script not set\n"));
236                 ret = False;
237         } else if (s1_buf == NULL) {
238                 /* Do nothing */
239         } else if (strcmp(s1_buf, s2_buf)) {
240                 DEBUG(0, ("Logon script is not written correctly\n"));
241                 ret = False;
242         }
243
244         /* Check user and group sids */
245         s1_sid = pdb_get_user_sid(s1);
246         s2_sid = pdb_get_user_sid(s2);
247         if (s2_sid == NULL && s1_sid != NULL) {
248                 DEBUG(0, ("USER SID not set\n"));
249                 ret = False;
250         } else if (s1_sid == NULL) {
251                 /* Do nothing */
252         } else if (!dom_sid_equal(s1_sid, s2_sid)) {
253                 DEBUG(0, ("USER SID is not written correctly\n"));
254                 ret = False;
255         }
256
257         return ret;     
258 }
259
260 static bool test_auth(TALLOC_CTX *mem_ctx, struct samu *pdb_entry)
261 {
262         struct auth_usersupplied_info *user_info;
263         struct auth_context *auth_context;
264         static const uint8_t challenge_8[8] = {1, 2, 3, 4, 5, 6, 7, 8};
265         DATA_BLOB challenge = data_blob_const(challenge_8, sizeof(challenge_8));
266         struct tsocket_address *remote_address;
267         struct tsocket_address *local_address;
268         unsigned char local_nt_response[24];
269         DATA_BLOB nt_resp = data_blob_const(local_nt_response, sizeof(local_nt_response));
270         unsigned char local_nt_session_key[16];
271         struct netr_SamInfo3 *info3_sam, *info3_auth;
272         struct auth_serversupplied_info *server_info;
273         struct wbcAuthUserParams params = { .flags = 0 };
274         struct wbcAuthUserInfo *info = NULL;
275         struct wbcAuthErrorInfo *err = NULL;
276         wbcErr wbc_status;
277         struct netr_SamInfo6 *info6_wbc = NULL;
278         NTSTATUS status;
279         bool ok;
280         uint8_t authoritative = 0;
281
282         SMBOWFencrypt(pdb_get_nt_passwd(pdb_entry), challenge_8,
283                       local_nt_response);
284         SMBsesskeygen_ntv1(pdb_get_nt_passwd(pdb_entry), local_nt_session_key);
285
286         if (tsocket_address_inet_from_strings(NULL, "ip", NULL, 0, &remote_address) != 0) {
287                 return False;
288         }
289
290         if (tsocket_address_inet_from_strings(NULL, "ip", NULL, 0, &local_address) != 0) {
291                 return False;
292         }
293
294         status = make_user_info(mem_ctx,
295                                 &user_info, pdb_get_username(pdb_entry), pdb_get_username(pdb_entry),
296                                 pdb_get_domain(pdb_entry), pdb_get_domain(pdb_entry), lp_netbios_name(),
297                                 remote_address,local_address, "pdbtest",
298                                 NULL, &nt_resp, NULL, NULL, NULL,
299                                 AUTH_PASSWORD_RESPONSE);
300         if (!NT_STATUS_IS_OK(status)) {
301                 DEBUG(0, ("Failed to test authentication with check_sam_security_info3: %s\n", nt_errstr(status)));
302                 return False;
303         }
304
305         status = check_sam_security_info3(&challenge, NULL, user_info, &info3_sam);
306         if (!NT_STATUS_IS_OK(status)) {
307                 DEBUG(0, ("Failed to test authentication with check_sam_security_info3: %s\n", nt_errstr(status)));
308                 return False;
309         }
310
311         if (memcmp(info3_sam->base.key.key, local_nt_session_key, 16) != 0) {
312                 DEBUG(0, ("Returned NT session key is incorrect\n"));
313                 return False;
314         }
315
316         status = make_auth3_context_for_ntlm(NULL, &auth_context);
317
318         if (!NT_STATUS_IS_OK(status)) {
319                 DEBUG(0, ("Failed to test authentication with check_sam_security_info3: %s\n", nt_errstr(status)));
320                 return False;
321         }
322
323         ok = auth3_context_set_challenge(
324                 auth_context, challenge.data, "fixed");
325         if (!ok) {
326                 DBG_ERR("auth3_context_set_challenge failed\n");
327                 return false;
328         }
329
330         status = auth_check_ntlm_password(mem_ctx,
331                                           auth_context,
332                                           user_info,
333                                           &server_info,
334                                           &authoritative);
335
336         if (!NT_STATUS_IS_OK(status)) {
337                 DEBUG(0, ("Failed to test authentication with auth module: "
338                           "%s authoritative[%u].\n",
339                           nt_errstr(status), authoritative));
340                 return False;
341         }
342         
343         info3_auth = talloc_zero(mem_ctx, struct netr_SamInfo3);
344         if (info3_auth == NULL) {
345                 return False;
346         }
347
348         status = serverinfo_to_SamInfo3(server_info, info3_auth);
349         if (!NT_STATUS_IS_OK(status)) {
350                 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
351                           nt_errstr(status)));
352                 return False;
353         }
354
355         if (memcmp(info3_auth->base.key.key, local_nt_session_key, 16) != 0) {
356                 DEBUG(0, ("Returned NT session key is incorrect\n"));
357                 return False;
358         }
359
360         if (!dom_sid_equal(info3_sam->base.domain_sid, info3_auth->base.domain_sid)) {
361                 DEBUG(0, ("domain_sid in SAM info3 %s does not match domain_sid in AUTH info3 %s\n", 
362                           dom_sid_string(NULL, info3_sam->base.domain_sid),
363                           dom_sid_string(NULL, info3_auth->base.domain_sid)));
364                 return False;
365         }
366         
367         /* TODO: 
368          * Compre more details from the two info3 structures,
369          * then test that an expired/disabled/pwdmustchange account
370          * returns the correct errors
371          */
372
373         params.parameter_control = user_info->logon_parameters;
374         params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
375                                     WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
376         params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
377
378         params.account_name     = user_info->client.account_name;
379         params.domain_name      = user_info->client.domain_name;
380         params.workstation_name = user_info->workstation_name;
381
382         memcpy(params.password.response.challenge,
383                challenge.data,
384                sizeof(params.password.response.challenge));
385
386         params.password.response.lm_length =
387                 user_info->password.response.lanman.length;
388         params.password.response.nt_length =
389                 user_info->password.response.nt.length;
390
391         params.password.response.lm_data =
392                 user_info->password.response.lanman.data;
393         params.password.response.nt_data =
394                 user_info->password.response.nt.data;
395
396         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
397         if (wbc_status != WBC_ERR_WINBIND_NOT_AVAILABLE) {
398                 if (wbc_status == WBC_ERR_AUTH_ERROR) {
399                         if (err) {
400                                 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
401                                       err->nt_string, err->nt_status, err->display_string));
402                                 status = NT_STATUS(err->nt_status);
403                                 wbcFreeMemory(err);
404                         } else {
405                                 status = NT_STATUS_LOGON_FAILURE;
406                         }
407                         if (!NT_STATUS_IS_OK(status)) {
408                                 return false;
409                         }
410                 } else if (!WBC_ERROR_IS_OK(wbc_status)) {
411                         DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
412                                 wbc_status, wbcErrorString(wbc_status)));
413                         if (err) {
414                                 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
415                                       err->nt_string, err->nt_status, err->display_string));
416                         }
417                         return false;
418                 }
419                 info6_wbc = wbcAuthUserInfo_to_netr_SamInfo6(mem_ctx, info);
420                 wbcFreeMemory(info);
421                 if (!info6_wbc) {
422                         DEBUG(1, ("wbcAuthUserInfo_to_netr_SamInfo6 failed\n"));
423                         return false;
424                 }
425
426                 if (memcmp(info6_wbc->base.key.key, local_nt_session_key, 16) != 0) {
427                         DEBUG(0, ("Returned NT session key is incorrect\n"));
428                         return false;
429                 }
430
431                 if (!dom_sid_equal(info3_sam->base.domain_sid, info6_wbc->base.domain_sid)) {
432                         DEBUG(0, ("domain_sid in SAM info3 %s does not match domain_sid in AUTH info3 %s\n",
433                                   dom_sid_string(NULL, info3_sam->base.domain_sid),
434                                   dom_sid_string(NULL, info6_wbc->base.domain_sid)));
435                         return false;
436                 }
437         }
438
439         return True;
440 }
441
442 static bool test_trusted_domains(TALLOC_CTX *ctx,
443                                  struct pdb_methods *pdb,
444                                  bool *error)
445 {
446         NTSTATUS rv;
447         /* test trustdom calls */
448         struct pdb_trusted_domain *td;
449         struct pdb_trusted_domain *new_td;
450         struct trustAuthInOutBlob taiob;
451         struct AuthenticationInformation aia;
452         enum ndr_err_code ndr_err;
453
454         td = talloc_zero(ctx ,struct pdb_trusted_domain);
455         if (!td) {
456                 fprintf(stderr, "talloc failed\n");
457                 return false;
458         }
459
460         td->domain_name = talloc_strdup(td, TRUST_DOM);
461         td->netbios_name = talloc_strdup(td, TRUST_DOM);
462         if (!td->domain_name || !td->netbios_name) {
463                 fprintf(stderr, "talloc failed\n");
464                 return false;
465         }
466
467         td->trust_auth_incoming = data_blob_null;
468
469         ZERO_STRUCT(taiob);
470         ZERO_STRUCT(aia);
471         taiob.count = 1;
472         taiob.current.count = 1;
473         taiob.current.array = &aia;
474         unix_to_nt_time(&aia.LastUpdateTime, time(NULL));
475         aia.AuthType = TRUST_AUTH_TYPE_CLEAR;
476         aia.AuthInfo.clear.password = (uint8_t *) talloc_strdup(ctx, TRUST_PWD);
477         aia.AuthInfo.clear.size = strlen(TRUST_PWD);
478
479         taiob.previous.count = 0;
480         taiob.previous.array = NULL;
481
482         ndr_err = ndr_push_struct_blob(&td->trust_auth_outgoing,
483                                         td, &taiob,
484                         (ndr_push_flags_fn_t) ndr_push_trustAuthInOutBlob);
485         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
486                 fprintf(stderr, "ndr_push_struct_blob failed.\n");
487                 return false;
488         }
489
490         td->trust_direction = LSA_TRUST_DIRECTION_OUTBOUND;
491         td->trust_type = LSA_TRUST_TYPE_DOWNLEVEL;
492         td->trust_attributes = 0;
493         td->trust_forest_trust_info = data_blob_null;
494
495         rv = pdb->set_trusted_domain(pdb, TRUST_DOM, td);
496         if (!NT_STATUS_IS_OK(rv)) {
497                 fprintf(stderr, "Error in set_trusted_domain %s\n",
498                                 get_friendly_nt_error_msg(rv));
499                 *error = true;
500         }
501
502         rv = pdb->get_trusted_domain(pdb, ctx, TRUST_DOM, &new_td);
503         if (!NT_STATUS_IS_OK(rv)) {
504                 fprintf(stderr, "Error in get_trusted_domain %s\n",
505                                 get_friendly_nt_error_msg(rv));
506                 *error = true;
507         }
508
509         if (!strequal(td->domain_name, new_td->domain_name) ||
510             !strequal(td->netbios_name, new_td->netbios_name) ||
511             !dom_sid_equal(&td->security_identifier,
512                            &new_td->security_identifier) ||
513             td->trust_direction != new_td->trust_direction ||
514             td->trust_type != new_td->trust_type ||
515             td->trust_attributes != new_td->trust_attributes ||
516             td->trust_auth_incoming.length != new_td->trust_auth_incoming.length ||
517             td->trust_forest_trust_info.length != new_td->trust_forest_trust_info.length ||
518             data_blob_cmp(&td->trust_auth_outgoing, &new_td->trust_auth_outgoing) != 0) {
519                 fprintf(stderr, "Old and new trusdet domain data do not match\n");
520                 *error = true;
521         }
522
523         return true;
524 }
525
526
527 int main(int argc, const char **argv)
528 {
529         TALLOC_CTX *ctx;
530         struct samu *out = NULL;
531         struct samu *in = NULL;
532         NTSTATUS rv;
533         int i;
534         struct timeval tv;
535         bool error = False;
536         struct passwd *pwd;
537         uint8_t *buf;
538         uint32_t expire, min_age, history;
539         struct pdb_methods *pdb;
540         poptContext pc;
541         static const char *backend = NULL;
542         static const char *unix_user = "nobody";
543         struct poptOption long_options[] = {
544                 {"username", 'u', POPT_ARG_STRING, &unix_user, 0, "Unix user to use for testing", "USERNAME" },
545                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "Backend to use if not default", "BACKEND[:SETTINGS]" },
546                 POPT_AUTOHELP
547                 POPT_COMMON_SAMBA
548                 POPT_TABLEEND
549         };
550
551         ctx = talloc_stackframe();
552
553         smb_init_locale();
554
555         pc = poptGetContext("pdbtest", argc, argv, long_options, 0);
556
557         poptSetOtherOptionHelp(pc, "backend[:settings] username");
558
559         while(poptGetNextOpt(pc) != -1);
560
561         poptFreeContext(pc);
562
563         /* Load configuration */
564         lp_load_global(get_dyn_CONFIGFILE());
565         setup_logging("pdbtest", DEBUG_STDOUT);
566         init_names();
567
568         if (backend == NULL) {
569                 backend = lp_passdb_backend();
570         }
571
572         rv = make_pdb_method_name(&pdb, backend);
573         if (NT_STATUS_IS_ERR(rv)) {
574                 fprintf(stderr, "Error initializing '%s': %s\n", backend, get_friendly_nt_error_msg(rv));
575                 exit(1);
576         }
577
578         if (!(out = samu_new(ctx))) {
579                 fprintf(stderr, "Can't create samu structure.\n");
580                 exit(1);
581         }
582
583         if ((pwd = Get_Pwnam_alloc(ctx, unix_user)) == NULL) {
584                 fprintf(stderr, "Error getting user information for %s\n", unix_user);
585                 exit(1);
586         }
587
588         samu_set_unix(out, pwd);
589
590         pdb_set_profile_path(out, "\\\\torture\\profile", PDB_SET);
591         pdb_set_homedir(out, "\\\\torture\\home", PDB_SET);
592         pdb_set_logon_script(out, "torture_script.cmd", PDB_SET);
593
594         pdb_set_acct_ctrl(out, ACB_NORMAL, PDB_SET);
595
596         pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &history);
597         if (history * PW_HISTORY_ENTRY_LEN < NT_HASH_LEN) {
598                 buf = (uint8_t *)TALLOC(ctx, NT_HASH_LEN);
599         } else {
600                 buf = (uint8_t *)TALLOC(ctx, history * PW_HISTORY_ENTRY_LEN);
601         }
602
603         /* Generate some random hashes */
604         GetTimeOfDay(&tv);
605         srand(tv.tv_usec);
606         for (i = 0; i < NT_HASH_LEN; i++) {
607                 buf[i] = (uint8_t) rand();
608         }
609         pdb_set_nt_passwd(out, buf, PDB_SET);
610         for (i = 0; i < LM_HASH_LEN; i++) {
611                 buf[i] = (uint8_t) rand();
612         }
613         pdb_set_lanman_passwd(out, buf, PDB_SET);
614         for (i = 0; i < history * PW_HISTORY_ENTRY_LEN; i++) {
615                 buf[i] = (uint8_t) rand();
616         }
617         pdb_set_pw_history(out, buf, history, PDB_SET);
618
619         pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &expire);
620         pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &min_age);
621         pdb_set_pass_last_set_time(out, time(NULL), PDB_SET);
622
623         if (min_age == (uint32_t)-1) {
624                 pdb_set_pass_can_change_time(out, 0, PDB_SET);
625         } else {
626                 pdb_set_pass_can_change_time(out, time(NULL)+min_age, PDB_SET);
627         }
628
629         pdb_set_logon_time(out, time(NULL)-3600, PDB_SET);
630
631         pdb_set_logoff_time(out, time(NULL), PDB_SET);
632
633         pdb_set_kickoff_time(out, time(NULL)+3600, PDB_SET);
634
635         /* Create account */
636         if (!NT_STATUS_IS_OK(rv = pdb->add_sam_account(pdb, out))) {
637                 fprintf(stderr, "Error in add_sam_account: %s\n", 
638                                 get_friendly_nt_error_msg(rv));
639                 exit(1);
640         }
641
642         if (!(in = samu_new(ctx))) {
643                 fprintf(stderr, "Can't create samu structure.\n");
644                 exit(1);
645         }
646
647         /* Get account information through getsampwnam() */
648         rv = pdb->getsampwnam(pdb, in, out->username);
649         if (NT_STATUS_IS_ERR(rv)) {
650                 fprintf(stderr, "Error getting sampw of added user %s: %s\n",
651                         out->username, nt_errstr(rv));
652                 if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
653                         fprintf(stderr, "Error in delete_sam_account %s\n", 
654                                         get_friendly_nt_error_msg(rv));
655                 }
656                 TALLOC_FREE(ctx);
657                 exit(1);
658         }
659
660         /* Verify integrity */
661         if (samu_correct(out, in)) {
662                 printf("User info written correctly\n");
663         } else {
664                 printf("User info NOT written correctly\n");
665                 error = True;
666         }
667
668         if (test_auth(ctx, out)) {
669                 printf("Authentication module test passed\n");
670         } else {
671                 printf("Authentication module test failed!\n");
672                 error = True;
673         }
674                         
675
676         /* Delete account */
677         if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
678                 fprintf(stderr, "Error in delete_sam_account %s\n", 
679                                         get_friendly_nt_error_msg(rv));
680         }
681
682         if (pdb_capabilities() & PDB_CAP_TRUSTED_DOMAINS_EX) {
683                 if (!test_trusted_domains(ctx, pdb, &error)) {
684                         fprintf(stderr, "failed testing trusted domains.\n");
685                         exit(1);
686                 }
687         }
688
689         TALLOC_FREE(ctx);
690
691         if (error) {
692                 return 1;
693         }
694         return 0;
695 }