[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / torture / pdbtest.c
blobb51e36e3550d0e0ec327a5dce87ced3e408c7561
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 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.
24 #include "includes.h"
26 static BOOL samu_correct(struct samu *s1, struct samu *s2)
28 BOOL ret = True;
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"));
38 ret = False;
39 } else if (s1_buf == NULL) {
40 /* Do nothing */
41 } else if (strcmp(s1_buf,s2_buf)) {
42 DEBUG(0, ("Username not written correctly, want %s, got \"%s\"\n",
43 pdb_get_username(s1),
44 pdb_get_username(s2)));
45 ret = False;
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"));
53 ret = False;
54 } else if (s1_buf == NULL) {
55 /* Do nothing */
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)));
60 ret = False;
63 /* Check acct ctrl */
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)));
70 ret = False;
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"));
78 ret = False;
79 } else if (d1_buf == NULL) {
80 /* Do nothing */
81 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
82 DEBUG(0, ("NT password not written correctly\n"));
83 ret = False;
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) {
92 /* Do nothing */
93 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
94 DEBUG(0, ("Lanman password not written correctly\n"));
95 ret = False;
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) {
104 /* Do nothing */
105 } else if (s1_len != s1_len) {
106 DEBUG(0, ("Password history not written correctly, lengths differ, want %d, got %d\n",
107 s1_len, s2_len));
108 ret = False;
109 } else if (strncmp(s1_buf, s2_buf, s1_len)) {
110 DEBUG(0, ("Password history not written correctly\n"));
111 ret = False;
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"));
117 ret = False;
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"));
123 ret = False;
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"));
129 ret = False;
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"));
135 ret = False;
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"));
141 ret = False;
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"));
147 ret = False;
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"));
153 ret = False;
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"));
159 ret = False;
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"));
165 ret = False;
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"));
171 ret = False;
172 } else if (d1_buf == NULL) {
173 /* Do nothing */
174 } else if (memcmp(d1_buf, d2_buf, MAX_HOURS_LEN)) {
175 DEBUG(0, ("Logon hours is not written correctly\n"));
176 ret = False;
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"));
185 ret = False;
186 } else if (s1_buf == NULL) {
187 /* Do nothing */
188 } else if (strcmp(s1_buf, s2_buf)) {
189 DEBUG(0, ("Profile path is not written correctly\n"));
190 ret = False;
193 /* Check home dir */
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"));
198 ret = False;
199 } else if (s1_buf == NULL) {
200 /* Do nothing */
201 } else if (strcmp(s1_buf, s2_buf)) {
202 DEBUG(0, ("Home dir is not written correctly\n"));
203 ret = False;
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"));
211 ret = False;
212 } else if (s1_buf == NULL) {
213 /* Do nothing */
214 } else if (strcmp(s1_buf, s2_buf)) {
215 DEBUG(0, ("Logon script is not written correctly\n"));
216 ret = False;
219 /* TODO Check user and group sids */
221 return ret;
225 int main(int argc, char **argv)
227 TALLOC_CTX *ctx;
228 struct samu *out = NULL;
229 struct samu *in = NULL;
230 NTSTATUS rv;
231 int i;
232 struct timeval tv;
233 BOOL error = False;
234 struct passwd *pwd;
235 uint8 *buf;
236 uint32 expire, min_age, history;
237 struct pdb_methods *pdb;
238 poptContext pc;
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]" },
244 POPT_AUTOHELP
245 POPT_COMMON_SAMBA
246 POPT_TABLEEND
249 load_case_tables();
251 pc = poptGetContext("vfstest", argc, (const char **) argv,
252 long_options, 0);
254 poptSetOtherOptionHelp(pc, "backend[:settings] username");
256 while(poptGetNextOpt(pc) != -1);
258 poptFreeContext(pc);
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));
271 exit(1);
274 ctx = talloc_init("PDBTEST");
276 if (!(out = samu_new(ctx))) {
277 fprintf(stderr, "Can't create samu structure.\n");
278 exit(1);
281 if ((pwd = getpwnam_alloc(ctx, unix_user)) == NULL) {
282 fprintf(stderr, "Error getting user information for %s\n", unix_user);
283 exit(1);
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);
295 } else {
296 buf = (uint8 *)TALLOC(ctx, history * PW_HISTORY_ENTRY_LEN);
299 /* Generate some random hashes */
300 GetTimeOfDay(&tv);
301 srand(tv.tv_usec);
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);
321 } else {
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);
327 } else {
328 pdb_set_pass_can_change_time(out, time(NULL)+min_age, PDB_SET);
331 /* Create account */
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));
335 exit(1);
338 if (!(in = samu_new(ctx))) {
339 fprintf(stderr, "Can't create samu structure.\n");
340 exit(1);
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",
346 out->username);
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));
351 TALLOC_FREE(ctx);
354 /* Verify integrity */
355 if (samu_correct(out, in)) {
356 printf("User info written correctly\n");
357 } else {
358 printf("User info NOT written correctly\n");
359 error = True;
362 /* Delete account */
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");
372 error = True;
373 break;
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");
377 error = True;
378 continue;
380 if (!samu_correct(out, in)) {
381 printf("Record gotten through getsampwnam() differs from same record through getsampwent()\n");
384 pdb->endsampwent(pdb);
386 TALLOC_FREE(ctx);
388 if (error) {
389 return 1;
391 return 0;