s4-dns: Remove sync dns_server_process_query
[Samba.git] / source3 / torture / pdbtest.c
blob1257eff9f6e5941d862d2c7f53a774384a719b66
1 /*
2 Unix SMB/CIFS implementation.
3 passdb testing utility
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "popt_common.h"
25 #include "passdb.h"
27 #include "../librpc/gen_ndr/drsblobs.h"
28 #include "../librpc/gen_ndr/ndr_drsblobs.h"
29 #include "../libcli/security/dom_sid.h"
31 #define TRUST_DOM "trustdom"
32 #define TRUST_PWD "trustpwd1232"
33 #define TRUST_SID "S-1-5-21-1111111111-2222222222-3333333333"
35 static bool samu_correct(struct samu *s1, struct samu *s2)
37 bool ret = True;
38 uint32 s1_len, s2_len;
39 const char *s1_buf, *s2_buf;
40 const uint8 *d1_buf, *d2_buf;
42 /* Check Unix username */
43 s1_buf = pdb_get_username(s1);
44 s2_buf = pdb_get_username(s2);
45 if (s2_buf == NULL && s1_buf != NULL) {
46 DEBUG(0, ("Username is not set\n"));
47 ret = False;
48 } else if (s1_buf == NULL) {
49 /* Do nothing */
50 } else if (strcmp(s1_buf,s2_buf)) {
51 DEBUG(0, ("Username not written correctly, want %s, got \"%s\"\n",
52 pdb_get_username(s1),
53 pdb_get_username(s2)));
54 ret = False;
57 /* Check NT username */
58 s1_buf = pdb_get_nt_username(s1);
59 s2_buf = pdb_get_nt_username(s2);
60 if (s2_buf == NULL && s1_buf != NULL) {
61 DEBUG(0, ("NT Username is not set\n"));
62 ret = False;
63 } else if (s1_buf == NULL) {
64 /* Do nothing */
65 } else if (strcmp(s1_buf, s2_buf)) {
66 DEBUG(0, ("NT Username not written correctly, want \"%s\", got \"%s\"\n",
67 pdb_get_nt_username(s1),
68 pdb_get_nt_username(s2)));
69 ret = False;
72 /* Check acct ctrl */
73 if (pdb_get_acct_ctrl(s1) != pdb_get_acct_ctrl(s2)) {
74 DEBUG(0, ("Acct ctrl field not written correctly, want %d (0x%X), got %d (0x%X)\n",
75 pdb_get_acct_ctrl(s1),
76 pdb_get_acct_ctrl(s1),
77 pdb_get_acct_ctrl(s2),
78 pdb_get_acct_ctrl(s2)));
79 ret = False;
82 /* Check NT password */
83 d1_buf = pdb_get_nt_passwd(s1);
84 d2_buf = pdb_get_nt_passwd(s2);
85 if (d2_buf == NULL && d1_buf != NULL) {
86 DEBUG(0, ("NT password is not set\n"));
87 ret = False;
88 } else if (d1_buf == NULL) {
89 /* Do nothing */
90 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
91 DEBUG(0, ("NT password not written correctly\n"));
92 ret = False;
95 /* Check lanman password */
96 d1_buf = pdb_get_lanman_passwd(s1);
97 d2_buf = pdb_get_lanman_passwd(s2);
98 if (d2_buf == NULL && d1_buf != NULL) {
99 DEBUG(0, ("Lanman password is not set\n"));
100 } else if (d1_buf == NULL) {
101 /* Do nothing */
102 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
103 DEBUG(0, ("Lanman password not written correctly\n"));
104 ret = False;
107 /* Check password history */
108 d1_buf = pdb_get_pw_history(s1, &s1_len);
109 d2_buf = pdb_get_pw_history(s2, &s2_len);
110 if (d2_buf == NULL && d1_buf != NULL) {
111 DEBUG(0, ("Password history is not set\n"));
112 } else if (d1_buf == NULL) {
113 /* Do nothing */
114 } else if (s1_len != s1_len) {
115 DEBUG(0, ("Password history not written correctly, lengths differ, want %d, got %d\n",
116 s1_len, s2_len));
117 ret = False;
118 } else if (strncmp(s1_buf, s2_buf, s1_len)) {
119 DEBUG(0, ("Password history not written correctly\n"));
120 ret = False;
123 /* Check logon time */
124 if (pdb_get_logon_time(s1) != pdb_get_logon_time(s2)) {
125 DEBUG(0, ("Logon time is not written correctly\n"));
126 ret = False;
129 /* Check logoff time */
130 if (pdb_get_logoff_time(s1) != pdb_get_logoff_time(s2)) {
131 DEBUG(0, ("Logoff time is not written correctly\n"));
132 ret = False;
135 /* Check kickoff time */
136 if (pdb_get_kickoff_time(s1) != pdb_get_logoff_time(s2)) {
137 DEBUG(0, ("Kickoff time is not written correctly\n"));
138 ret = False;
141 /* Check bad password time */
142 if (pdb_get_bad_password_time(s1) != pdb_get_bad_password_time(s2)) {
143 DEBUG(0, ("Bad password time is not written correctly\n"));
144 ret = False;
147 /* Check password last set time */
148 if (pdb_get_pass_last_set_time(s1) != pdb_get_pass_last_set_time(s2)) {
149 DEBUG(0, ("Password last set time is not written correctly\n"));
150 ret = False;
153 /* Check password can change time */
154 if (pdb_get_pass_can_change_time(s1) != pdb_get_pass_can_change_time(s2)) {
155 DEBUG(0, ("Password can change time is not written correctly\n"));
156 ret = False;
159 /* Check password must change time */
160 if (pdb_get_pass_must_change_time(s1) != pdb_get_pass_must_change_time(s2)) {
161 DEBUG(0, ("Password must change time is not written correctly\n"));
162 ret = False;
165 /* Check logon divs */
166 if (pdb_get_logon_divs(s1) != pdb_get_logon_divs(s2)) {
167 DEBUG(0, ("Logon divs not written correctly\n"));
168 ret = False;
171 /* Check logon hours */
172 if (pdb_get_hours_len(s1) != pdb_get_hours_len(s2)) {
173 DEBUG(0, ("Logon hours length not written correctly\n"));
174 ret = False;
175 } else if (pdb_get_hours_len(s1) != 0) {
176 d1_buf = pdb_get_hours(s1);
177 d2_buf = pdb_get_hours(s2);
178 if (d2_buf == NULL && d2_buf != NULL) {
179 DEBUG(0, ("Logon hours is not set\n"));
180 ret = False;
181 } else if (d1_buf == NULL) {
182 /* Do nothing */
183 } else if (memcmp(d1_buf, d2_buf, MAX_HOURS_LEN)) {
184 DEBUG(0, ("Logon hours is not written correctly\n"));
185 ret = False;
189 /* Check profile path */
190 s1_buf = pdb_get_profile_path(s1);
191 s2_buf = pdb_get_profile_path(s2);
192 if (s2_buf == NULL && s1_buf != NULL) {
193 DEBUG(0, ("Profile path is not set\n"));
194 ret = False;
195 } else if (s1_buf == NULL) {
196 /* Do nothing */
197 } else if (strcmp(s1_buf, s2_buf)) {
198 DEBUG(0, ("Profile path is not written correctly\n"));
199 ret = False;
202 /* Check home dir */
203 s1_buf = pdb_get_homedir(s1);
204 s2_buf = pdb_get_homedir(s2);
205 if (s2_buf == NULL && s1_buf != NULL) {
206 DEBUG(0, ("Home dir is not set\n"));
207 ret = False;
208 } else if (s1_buf == NULL) {
209 /* Do nothing */
210 } else if (strcmp(s1_buf, s2_buf)) {
211 DEBUG(0, ("Home dir is not written correctly\n"));
212 ret = False;
215 /* Check logon script */
216 s1_buf = pdb_get_logon_script(s1);
217 s2_buf = pdb_get_logon_script(s2);
218 if (s2_buf == NULL && s1_buf != NULL) {
219 DEBUG(0, ("Logon script not set\n"));
220 ret = False;
221 } else if (s1_buf == NULL) {
222 /* Do nothing */
223 } else if (strcmp(s1_buf, s2_buf)) {
224 DEBUG(0, ("Logon script is not written correctly\n"));
225 ret = False;
228 /* TODO Check user and group sids */
230 return ret;
233 static bool test_trusted_domains(TALLOC_CTX *ctx,
234 struct pdb_methods *pdb,
235 bool *error)
237 NTSTATUS rv;
238 /* test trustdom calls */
239 struct pdb_trusted_domain *td;
240 struct pdb_trusted_domain *new_td;
241 struct trustAuthInOutBlob taiob;
242 struct AuthenticationInformation aia;
243 enum ndr_err_code ndr_err;
245 td = talloc_zero(ctx ,struct pdb_trusted_domain);
246 if (!td) {
247 fprintf(stderr, "talloc failed\n");
248 return false;
251 td->domain_name = talloc_strdup(td, TRUST_DOM);
252 td->netbios_name = talloc_strdup(td, TRUST_DOM);
253 if (!td->domain_name || !td->netbios_name) {
254 fprintf(stderr, "talloc failed\n");
255 return false;
258 td->trust_auth_incoming = data_blob_null;
260 ZERO_STRUCT(taiob);
261 ZERO_STRUCT(aia);
262 taiob.count = 1;
263 taiob.current.count = 1;
264 taiob.current.array = &aia;
265 unix_to_nt_time(&aia.LastUpdateTime, time(NULL));
266 aia.AuthType = TRUST_AUTH_TYPE_CLEAR;
267 aia.AuthInfo.clear.password = (uint8_t *) talloc_strdup(ctx, TRUST_PWD);
268 aia.AuthInfo.clear.size = strlen(TRUST_PWD);
270 taiob.previous.count = 0;
271 taiob.previous.array = NULL;
273 ndr_err = ndr_push_struct_blob(&td->trust_auth_outgoing,
274 td, &taiob,
275 (ndr_push_flags_fn_t) ndr_push_trustAuthInOutBlob);
276 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
277 fprintf(stderr, "ndr_push_struct_blob failed.\n");
278 return false;
281 td->trust_direction = LSA_TRUST_DIRECTION_OUTBOUND;
282 td->trust_type = LSA_TRUST_TYPE_DOWNLEVEL;
283 td->trust_attributes = 0;
284 td->trust_forest_trust_info = data_blob_null;
286 rv = pdb->set_trusted_domain(pdb, TRUST_DOM, td);
287 if (!NT_STATUS_IS_OK(rv)) {
288 fprintf(stderr, "Error in set_trusted_domain %s\n",
289 get_friendly_nt_error_msg(rv));
290 *error = true;
293 rv = pdb->get_trusted_domain(pdb, ctx, TRUST_DOM, &new_td);
294 if (!NT_STATUS_IS_OK(rv)) {
295 fprintf(stderr, "Error in set_trusted_domain %s\n",
296 get_friendly_nt_error_msg(rv));
297 *error = true;
300 if (!strequal(td->domain_name, new_td->domain_name) ||
301 !strequal(td->netbios_name, new_td->netbios_name) ||
302 !dom_sid_equal(&td->security_identifier,
303 &new_td->security_identifier) ||
304 td->trust_direction != new_td->trust_direction ||
305 td->trust_type != new_td->trust_type ||
306 td->trust_attributes != new_td->trust_attributes ||
307 td->trust_auth_incoming.length != new_td->trust_auth_incoming.length ||
308 td->trust_forest_trust_info.length != new_td->trust_forest_trust_info.length ||
309 data_blob_cmp(&td->trust_auth_outgoing, &new_td->trust_auth_outgoing) != 0) {
310 fprintf(stderr, "Old and new trusdet domain data do not match\n");
311 *error = true;
314 return true;
318 int main(int argc, char **argv)
320 TALLOC_CTX *ctx;
321 struct samu *out = NULL;
322 struct samu *in = NULL;
323 NTSTATUS rv;
324 int i;
325 struct timeval tv;
326 bool error = False;
327 struct passwd *pwd;
328 uint8 *buf;
329 uint32 expire, min_age, history;
330 struct pdb_methods *pdb;
331 poptContext pc;
332 static const char *backend = NULL;
333 static const char *unix_user = "nobody";
334 struct poptOption long_options[] = {
335 {"username", 'u', POPT_ARG_STRING, &unix_user, 0, "Unix user to use for testing", "USERNAME" },
336 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "Backend to use if not default", "BACKEND[:SETTINGS]" },
337 POPT_AUTOHELP
338 POPT_COMMON_SAMBA
339 POPT_TABLEEND
342 load_case_tables();
344 pc = poptGetContext("pdbtest", argc, (const char **) argv,
345 long_options, 0);
347 poptSetOtherOptionHelp(pc, "backend[:settings] username");
349 while(poptGetNextOpt(pc) != -1);
351 poptFreeContext(pc);
353 /* Load configuration */
354 lp_load_global(get_dyn_CONFIGFILE());
355 setup_logging("pdbtest", DEBUG_STDOUT);
357 if (backend == NULL) {
358 backend = lp_passdb_backend();
361 rv = make_pdb_method_name(&pdb, backend);
362 if (NT_STATUS_IS_ERR(rv)) {
363 fprintf(stderr, "Error initializing '%s': %s\n", backend, get_friendly_nt_error_msg(rv));
364 exit(1);
367 ctx = talloc_init("PDBTEST");
369 if (!(out = samu_new(ctx))) {
370 fprintf(stderr, "Can't create samu structure.\n");
371 exit(1);
374 if ((pwd = Get_Pwnam_alloc(ctx, unix_user)) == NULL) {
375 fprintf(stderr, "Error getting user information for %s\n", unix_user);
376 exit(1);
379 samu_set_unix(out, pwd);
381 pdb_set_profile_path(out, "\\\\torture\\profile", PDB_SET);
382 pdb_set_homedir(out, "\\\\torture\\home", PDB_SET);
383 pdb_set_logon_script(out, "torture_script.cmd", PDB_SET);
385 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &history);
386 if (history * PW_HISTORY_ENTRY_LEN < NT_HASH_LEN) {
387 buf = (uint8 *)TALLOC(ctx, NT_HASH_LEN);
388 } else {
389 buf = (uint8 *)TALLOC(ctx, history * PW_HISTORY_ENTRY_LEN);
392 /* Generate some random hashes */
393 GetTimeOfDay(&tv);
394 srand(tv.tv_usec);
395 for (i = 0; i < NT_HASH_LEN; i++) {
396 buf[i] = (uint8) rand();
398 pdb_set_nt_passwd(out, buf, PDB_SET);
399 for (i = 0; i < LM_HASH_LEN; i++) {
400 buf[i] = (uint8) rand();
402 pdb_set_lanman_passwd(out, buf, PDB_SET);
403 for (i = 0; i < history * PW_HISTORY_ENTRY_LEN; i++) {
404 buf[i] = (uint8) rand();
406 pdb_set_pw_history(out, buf, history, PDB_SET);
408 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &expire);
409 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &min_age);
410 pdb_set_pass_last_set_time(out, time(NULL), PDB_SET);
412 if (min_age == (uint32)-1) {
413 pdb_set_pass_can_change_time(out, 0, PDB_SET);
414 } else {
415 pdb_set_pass_can_change_time(out, time(NULL)+min_age, PDB_SET);
418 /* Create account */
419 if (!NT_STATUS_IS_OK(rv = pdb->add_sam_account(pdb, out))) {
420 fprintf(stderr, "Error in add_sam_account: %s\n",
421 get_friendly_nt_error_msg(rv));
422 exit(1);
425 if (!(in = samu_new(ctx))) {
426 fprintf(stderr, "Can't create samu structure.\n");
427 exit(1);
430 /* Get account information through getsampwnam() */
431 if (NT_STATUS_IS_ERR(pdb->getsampwnam(pdb, in, out->username))) {
432 fprintf(stderr, "Error getting sampw of added user %s.\n",
433 out->username);
434 if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
435 fprintf(stderr, "Error in delete_sam_account %s\n",
436 get_friendly_nt_error_msg(rv));
438 TALLOC_FREE(ctx);
441 /* Verify integrity */
442 if (samu_correct(out, in)) {
443 printf("User info written correctly\n");
444 } else {
445 printf("User info NOT written correctly\n");
446 error = True;
449 /* Delete account */
450 if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
451 fprintf(stderr, "Error in delete_sam_account %s\n",
452 get_friendly_nt_error_msg(rv));
455 if (pdb_capabilities() & PDB_CAP_TRUSTED_DOMAINS_EX) {
456 if (!test_trusted_domains(ctx, pdb, &error)) {
457 fprintf(stderr, "failed testing trusted domains.\n");
458 exit(1);
462 TALLOC_FREE(ctx);
464 if (error) {
465 return 1;
467 return 0;