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 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/>.
24 #include "popt_common.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
)
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"));
48 } else if (s1_buf
== NULL
) {
50 } else if (strcmp(s1_buf
,s2_buf
)) {
51 DEBUG(0, ("Username not written correctly, want %s, got \"%s\"\n",
53 pdb_get_username(s2
)));
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"));
63 } else if (s1_buf
== NULL
) {
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
)));
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
)));
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"));
88 } else if (d1_buf
== NULL
) {
90 } else if (memcmp(d1_buf
, d2_buf
, NT_HASH_LEN
)) {
91 DEBUG(0, ("NT password not written correctly\n"));
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
) {
102 } else if (memcmp(d1_buf
, d2_buf
, NT_HASH_LEN
)) {
103 DEBUG(0, ("Lanman password not written correctly\n"));
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
) {
114 } else if (s1_len
!= s1_len
) {
115 DEBUG(0, ("Password history not written correctly, lengths differ, want %d, got %d\n",
118 } else if (strncmp(s1_buf
, s2_buf
, s1_len
)) {
119 DEBUG(0, ("Password history not written correctly\n"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
181 } else if (d1_buf
== NULL
) {
183 } else if (memcmp(d1_buf
, d2_buf
, MAX_HOURS_LEN
)) {
184 DEBUG(0, ("Logon hours is not written correctly\n"));
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"));
195 } else if (s1_buf
== NULL
) {
197 } else if (strcmp(s1_buf
, s2_buf
)) {
198 DEBUG(0, ("Profile path is not written correctly\n"));
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"));
208 } else if (s1_buf
== NULL
) {
210 } else if (strcmp(s1_buf
, s2_buf
)) {
211 DEBUG(0, ("Home dir is not written correctly\n"));
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"));
221 } else if (s1_buf
== NULL
) {
223 } else if (strcmp(s1_buf
, s2_buf
)) {
224 DEBUG(0, ("Logon script is not written correctly\n"));
228 /* TODO Check user and group sids */
234 int main(int argc
, char **argv
)
237 struct samu
*out
= NULL
;
238 struct samu
*in
= NULL
;
245 uint32 expire
, min_age
, history
;
246 struct pdb_methods
*pdb
;
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]" },
260 pc
= poptGetContext("pdbtest", argc
, (const char **) argv
,
263 poptSetOtherOptionHelp(pc
, "backend[:settings] username");
265 while(poptGetNextOpt(pc
) != -1);
269 /* Load configuration */
270 lp_load(get_dyn_CONFIGFILE(), False
, False
, True
, True
);
271 setup_logging("pdbtest", DEBUG_STDOUT
);
273 if (backend
== NULL
) {
274 backend
= lp_passdb_backend();
277 rv
= make_pdb_method_name(&pdb
, backend
);
278 if (NT_STATUS_IS_ERR(rv
)) {
279 fprintf(stderr
, "Error initializing '%s': %s\n", backend
, get_friendly_nt_error_msg(rv
));
283 ctx
= talloc_init("PDBTEST");
285 if (!(out
= samu_new(ctx
))) {
286 fprintf(stderr
, "Can't create samu structure.\n");
290 if ((pwd
= Get_Pwnam_alloc(ctx
, unix_user
)) == NULL
) {
291 fprintf(stderr
, "Error getting user information for %s\n", unix_user
);
295 samu_set_unix(out
, pwd
);
297 pdb_set_profile_path(out
, "\\\\torture\\profile", PDB_SET
);
298 pdb_set_homedir(out
, "\\\\torture\\home", PDB_SET
);
299 pdb_set_logon_script(out
, "torture_script.cmd", PDB_SET
);
301 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &history
);
302 if (history
* PW_HISTORY_ENTRY_LEN
< NT_HASH_LEN
) {
303 buf
= (uint8
*)TALLOC(ctx
, NT_HASH_LEN
);
305 buf
= (uint8
*)TALLOC(ctx
, history
* PW_HISTORY_ENTRY_LEN
);
308 /* Generate some random hashes */
311 for (i
= 0; i
< NT_HASH_LEN
; i
++) {
312 buf
[i
] = (uint8
) rand();
314 pdb_set_nt_passwd(out
, buf
, PDB_SET
);
315 for (i
= 0; i
< LM_HASH_LEN
; i
++) {
316 buf
[i
] = (uint8
) rand();
318 pdb_set_lanman_passwd(out
, buf
, PDB_SET
);
319 for (i
= 0; i
< history
* PW_HISTORY_ENTRY_LEN
; i
++) {
320 buf
[i
] = (uint8
) rand();
322 pdb_set_pw_history(out
, buf
, history
, PDB_SET
);
324 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE
, &expire
);
325 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE
, &min_age
);
326 pdb_set_pass_last_set_time(out
, time(NULL
), PDB_SET
);
328 if (expire
== 0 || expire
== (uint32
)-1) {
329 pdb_set_pass_must_change_time(out
, get_time_t_max(), PDB_SET
);
331 pdb_set_pass_must_change_time(out
, time(NULL
)+expire
, PDB_SET
);
334 if (min_age
== (uint32
)-1) {
335 pdb_set_pass_can_change_time(out
, 0, PDB_SET
);
337 pdb_set_pass_can_change_time(out
, time(NULL
)+min_age
, PDB_SET
);
341 if (!NT_STATUS_IS_OK(rv
= pdb
->add_sam_account(pdb
, out
))) {
342 fprintf(stderr
, "Error in add_sam_account: %s\n",
343 get_friendly_nt_error_msg(rv
));
347 if (!(in
= samu_new(ctx
))) {
348 fprintf(stderr
, "Can't create samu structure.\n");
352 /* Get account information through getsampwnam() */
353 if (NT_STATUS_IS_ERR(pdb
->getsampwnam(pdb
, in
, out
->username
))) {
354 fprintf(stderr
, "Error getting sampw of added user %s.\n",
356 if (!NT_STATUS_IS_OK(rv
= pdb
->delete_sam_account(pdb
, out
))) {
357 fprintf(stderr
, "Error in delete_sam_account %s\n",
358 get_friendly_nt_error_msg(rv
));
363 /* Verify integrity */
364 if (samu_correct(out
, in
)) {
365 printf("User info written correctly\n");
367 printf("User info NOT written correctly\n");
372 if (!NT_STATUS_IS_OK(rv
= pdb
->delete_sam_account(pdb
, out
))) {
373 fprintf(stderr
, "Error in delete_sam_account %s\n",
374 get_friendly_nt_error_msg(rv
));
377 /* test trustdom calls */
378 struct pdb_trusted_domain
*td
;
379 struct pdb_trusted_domain
*new_td
;
380 struct trustAuthInOutBlob taiob
;
381 struct AuthenticationInformation aia
;
382 enum ndr_err_code ndr_err
;
384 td
= talloc_zero(ctx
,struct pdb_trusted_domain
);
386 fprintf(stderr
, "talloc failed\n");
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");
397 td
->trust_auth_incoming
= data_blob_null
;
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
);
409 taiob
.previous
.count
= 0;
410 taiob
.previous
.array
= NULL
;
412 ndr_err
= ndr_push_struct_blob(&td
->trust_auth_outgoing
,
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");
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
;
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
));
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
));
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");