s3: Fix "ISO C90 forbids mixed declarations and code"
[vlendec/samba-autobuild/.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
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.
12
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.
17
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/>.
20 */
21
22
23 #include "includes.h"
24 #include "popt_common.h"
25 #include "passdb.h"
26
27 #include "../librpc/gen_ndr/drsblobs.h"
28 #include "../librpc/gen_ndr/ndr_drsblobs.h"
29 #include "../libcli/security/dom_sid.h"
30
31 #define TRUST_DOM "trustdom"
32 #define TRUST_PWD "trustpwd1232"
33 #define TRUST_SID "S-1-5-21-1111111111-2222222222-3333333333"
34
35 static bool samu_correct(struct samu *s1, struct samu *s2)
36 {
37         bool ret = True;
38         uint32 s1_len, s2_len;
39         const char *s1_buf, *s2_buf;
40         const uint8 *d1_buf, *d2_buf;
41
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;
55         }
56
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;
70         }
71
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;
80         }
81
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;
93         }
94
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;
105         }
106
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;
121         }
122
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;
127         }
128
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;
133         }
134
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;
139         }
140
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;
145         }
146
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;
151         }
152
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;
157         }
158
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;
163         }
164
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;
169         }
170
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;
186                 }
187         }
188
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;
200         }
201
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;
213         }
214
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;
226         }
227
228         /* TODO Check user and group sids */
229
230         return ret;     
231 }
232
233
234 int main(int argc, char **argv)
235 {
236         TALLOC_CTX *ctx;
237         struct samu *out = NULL;
238         struct samu *in = NULL;
239         NTSTATUS rv;
240         int i;
241         struct timeval tv;
242         bool error = False;
243         struct passwd *pwd;
244         uint8 *buf;
245         uint32 expire, min_age, history;
246         struct pdb_methods *pdb;
247         poptContext pc;
248         static const char *backend = NULL;
249         static const char *unix_user = "nobody";
250         struct poptOption long_options[] = {
251                 {"username", 'u', POPT_ARG_STRING, &unix_user, 0, "Unix user to use for testing", "USERNAME" },
252                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "Backend to use if not default", "BACKEND[:SETTINGS]" },
253                 POPT_AUTOHELP
254                 POPT_COMMON_SAMBA
255                 POPT_TABLEEND
256         };
257
258         /* test trustdom calls */
259         struct pdb_trusted_domain *td;
260         struct pdb_trusted_domain *new_td;
261         struct trustAuthInOutBlob taiob;
262         struct AuthenticationInformation aia;
263         enum ndr_err_code ndr_err;
264
265         load_case_tables();
266
267         pc = poptGetContext("pdbtest", argc, (const char **) argv,
268                             long_options, 0);
269
270         poptSetOtherOptionHelp(pc, "backend[:settings] username");
271
272         while(poptGetNextOpt(pc) != -1);
273
274         poptFreeContext(pc);
275
276         /* Load configuration */
277         lp_load_global(get_dyn_CONFIGFILE());
278         setup_logging("pdbtest", DEBUG_STDOUT);
279
280         if (backend == NULL) {
281                 backend = lp_passdb_backend();
282         }
283
284         rv = make_pdb_method_name(&pdb, backend);
285         if (NT_STATUS_IS_ERR(rv)) {
286                 fprintf(stderr, "Error initializing '%s': %s\n", backend, get_friendly_nt_error_msg(rv));
287                 exit(1);
288         }
289
290         ctx = talloc_init("PDBTEST");
291
292         if (!(out = samu_new(ctx))) {
293                 fprintf(stderr, "Can't create samu structure.\n");
294                 exit(1);
295         }
296
297         if ((pwd = Get_Pwnam_alloc(ctx, unix_user)) == NULL) {
298                 fprintf(stderr, "Error getting user information for %s\n", unix_user);
299                 exit(1);
300         }
301
302         samu_set_unix(out, pwd);
303
304         pdb_set_profile_path(out, "\\\\torture\\profile", PDB_SET);
305         pdb_set_homedir(out, "\\\\torture\\home", PDB_SET);
306         pdb_set_logon_script(out, "torture_script.cmd", PDB_SET);
307
308         pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &history);
309         if (history * PW_HISTORY_ENTRY_LEN < NT_HASH_LEN) {
310                 buf = (uint8 *)TALLOC(ctx, NT_HASH_LEN);
311         } else {
312                 buf = (uint8 *)TALLOC(ctx, history * PW_HISTORY_ENTRY_LEN);
313         }
314
315         /* Generate some random hashes */
316         GetTimeOfDay(&tv);
317         srand(tv.tv_usec);
318         for (i = 0; i < NT_HASH_LEN; i++) {
319                 buf[i] = (uint8) rand();
320         }
321         pdb_set_nt_passwd(out, buf, PDB_SET);
322         for (i = 0; i < LM_HASH_LEN; i++) {
323                 buf[i] = (uint8) rand();
324         }
325         pdb_set_lanman_passwd(out, buf, PDB_SET);
326         for (i = 0; i < history * PW_HISTORY_ENTRY_LEN; i++) {
327                 buf[i] = (uint8) rand();
328         }
329         pdb_set_pw_history(out, buf, history, PDB_SET);
330
331         pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &expire);
332         pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &min_age);
333         pdb_set_pass_last_set_time(out, time(NULL), PDB_SET);
334
335         if (expire == 0 || expire == (uint32)-1) {
336                 pdb_set_pass_must_change_time(out, get_time_t_max(), PDB_SET);
337         } else {
338                 pdb_set_pass_must_change_time(out, time(NULL)+expire, PDB_SET);
339         }
340
341         if (min_age == (uint32)-1) {
342                 pdb_set_pass_can_change_time(out, 0, PDB_SET);
343         } else {
344                 pdb_set_pass_can_change_time(out, time(NULL)+min_age, PDB_SET);
345         }
346
347         /* Create account */
348         if (!NT_STATUS_IS_OK(rv = pdb->add_sam_account(pdb, out))) {
349                 fprintf(stderr, "Error in add_sam_account: %s\n", 
350                                 get_friendly_nt_error_msg(rv));
351                 exit(1);
352         }
353
354         if (!(in = samu_new(ctx))) {
355                 fprintf(stderr, "Can't create samu structure.\n");
356                 exit(1);
357         }
358
359         /* Get account information through getsampwnam() */
360         if (NT_STATUS_IS_ERR(pdb->getsampwnam(pdb, in, out->username))) {
361                 fprintf(stderr, "Error getting sampw of added user %s.\n",
362                                 out->username);
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));
366                 }
367                 TALLOC_FREE(ctx);
368         }
369
370         /* Verify integrity */
371         if (samu_correct(out, in)) {
372                 printf("User info written correctly\n");
373         } else {
374                 printf("User info NOT written correctly\n");
375                 error = True;
376         }
377
378         /* Delete account */
379         if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
380                 fprintf(stderr, "Error in delete_sam_account %s\n", 
381                                         get_friendly_nt_error_msg(rv));
382         }
383
384         td = talloc_zero(ctx ,struct pdb_trusted_domain);
385         if (!td) {
386                 fprintf(stderr, "talloc failed\n");
387                 exit(1);
388         }
389
390         td->domain_name = talloc_strdup(td, TRUST_DOM);
391         td->netbios_name = talloc_strdup(td, TRUST_DOM);
392         if (!td->domain_name || !td->netbios_name) {
393                 fprintf(stderr, "talloc failed\n");
394                 exit(1);
395         }
396
397         td->trust_auth_incoming = data_blob_null;
398
399         ZERO_STRUCT(taiob);
400         ZERO_STRUCT(aia);
401         taiob.count = 1;
402         taiob.current.count = 1;
403         taiob.current.array = &aia;
404         unix_to_nt_time(&aia.LastUpdateTime, time(NULL));
405         aia.AuthType = TRUST_AUTH_TYPE_CLEAR;
406         aia.AuthInfo.clear.password = (uint8_t *) talloc_strdup(ctx, TRUST_PWD);
407         aia.AuthInfo.clear.size = strlen(TRUST_PWD);
408
409         taiob.previous.count = 0;
410         taiob.previous.array = NULL;
411
412         ndr_err = ndr_push_struct_blob(&td->trust_auth_outgoing,
413                                         td, &taiob,
414                         (ndr_push_flags_fn_t) ndr_push_trustAuthInOutBlob);
415         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
416                 fprintf(stderr, "ndr_push_struct_blob failed.\n");
417                 exit(1);
418         }
419
420         td->trust_direction = LSA_TRUST_DIRECTION_OUTBOUND;
421         td->trust_type = LSA_TRUST_TYPE_DOWNLEVEL;
422         td->trust_attributes = 0;
423         td->trust_forest_trust_info = data_blob_null;
424
425         rv = pdb->set_trusted_domain(pdb, TRUST_DOM, td);
426         if (!NT_STATUS_IS_OK(rv)) {
427                 fprintf(stderr, "Error in set_trusted_domain %s\n",
428                                 get_friendly_nt_error_msg(rv));
429                 error = True;
430         }
431
432         rv = pdb->get_trusted_domain(pdb, ctx, TRUST_DOM, &new_td);
433         if (!NT_STATUS_IS_OK(rv)) {
434                 fprintf(stderr, "Error in set_trusted_domain %s\n",
435                                 get_friendly_nt_error_msg(rv));
436                 error = True;
437         }
438
439         if (!strequal(td->domain_name, new_td->domain_name) ||
440             !strequal(td->netbios_name, new_td->netbios_name) ||
441             !sid_equal(&td->security_identifier, &new_td->security_identifier) ||
442             td->trust_direction != new_td->trust_direction ||
443             td->trust_type != new_td->trust_type ||
444             td->trust_attributes != new_td->trust_attributes ||
445             td->trust_auth_incoming.length != new_td->trust_auth_incoming.length ||
446             td->trust_forest_trust_info.length != new_td->trust_forest_trust_info.length ||
447             data_blob_cmp(&td->trust_auth_outgoing, &new_td->trust_auth_outgoing) != 0) {
448                 fprintf(stderr, "Old and new trusdet domain data do not match\n");
449                 error = True;
450         }
451
452         TALLOC_FREE(ctx);
453
454         if (error) {
455                 return 1;
456         }
457         return 0;
458 }