2 Unix SMB/Netbios implementation.
6 Copyright (C) Tim Potter 2001
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.
25 static void decode_domain_info(SAM_DOMAIN_INFO *a)
28 printf("Domain Information\n");
29 printf("------------------\n");
31 unistr2_to_ascii(temp, &a->uni_dom_name, sizeof(temp)-1);
32 printf("\tDomain :%s\n", temp);
33 printf("\tMin password len :%d\n", a->min_pwd_len);
34 printf("\tpassword history len:%d\n", a->pwd_history_len);
35 printf("\tcreation time :%s\n", http_timestring(nt_time_to_unix(&a->creation_time)));
38 static void decode_sam_group_info(SAM_GROUP_INFO *a)
41 printf("\nDomain Group Information\n");
42 printf("------------------------\n");
44 unistr2_to_ascii(temp, &a->uni_grp_name, sizeof(temp)-1);
45 printf("\tGroup name :%s\n", temp);
46 unistr2_to_ascii(temp, &a->uni_grp_desc, sizeof(temp)-1);
47 printf("\tGroup description :%s\n", temp);
48 printf("\trid :%d\n", a->gid.g_rid);
49 printf("\tattribute :%d\n", a->gid.attr);
52 static void decode_sam_account_info(SAM_ACCOUNT_INFO *a)
55 printf("\nUser Information\n");
56 printf("----------------\n");
58 unistr2_to_ascii(temp, &a->uni_acct_name, sizeof(temp)-1);
59 printf("\tUser name :%s\n", temp);
60 printf("\tuser's rid :%d\n", a->user_rid);
61 printf("\tuser's primary gid :%d\n", a->group_rid);
62 unistr2_to_ascii(temp, &a->uni_full_name, sizeof(temp)-1);
63 printf("\tfull name :%s\n", temp);
64 unistr2_to_ascii(temp, &a->uni_home_dir, sizeof(temp)-1);
65 printf("\thome directory :%s\n", temp);
66 unistr2_to_ascii(temp, &a->uni_dir_drive, sizeof(temp)-1);
67 printf("\tdrive :%s\n", temp);
68 unistr2_to_ascii(temp, &a->uni_logon_script, sizeof(temp)-1);
69 printf("\tlogon script :%s\n", temp);
70 unistr2_to_ascii(temp, &a->uni_acct_desc, sizeof(temp)-1);
71 printf("\tdescription :%s\n", temp);
72 unistr2_to_ascii(temp, &a->uni_workstations, sizeof(temp)-1);
73 printf("\tworkstations :%s\n", temp);
76 static void decode_sam_grp_mem_info(SAM_GROUP_MEM_INFO *a)
79 printf("\nGroup members information\n");
80 printf("-------------------------\n");
81 printf("\tnum members :%d\n", a->num_members);
83 for (i=0; i<a->num_members; i++) {
84 printf("\trid, attr:%d, %d\n", a->rids[i], a->attribs[i]);
88 static void decode_sam_alias_info(SAM_ALIAS_INFO *a)
91 printf("\nAlias Information\n");
92 printf("-----------------\n");
94 unistr2_to_ascii(temp, &a->uni_als_name, sizeof(temp)-1);
95 printf("\tname :%s\n", temp);
96 unistr2_to_ascii(temp, &a->uni_als_desc, sizeof(temp)-1);
97 printf("\tdescription :%s\n", temp);
98 printf("\trid :%d\n", a->als_rid);
101 static void decode_sam_als_mem_info(SAM_ALIAS_MEM_INFO *a)
105 printf("\nAlias members Information\n");
106 printf("-------------------------\n");
107 printf("\tnum members :%d\n", a->num_members);
108 printf("\tnum sids :%d\n", a->num_sids);
109 for (i=0; i<a->num_sids; i++) {
110 printf("\tsid :%s\n", sid_to_string(temp, &a->sids[i].sid));
116 static void decode_sam_dom_info(SAM_DELTA_DOM *a)
119 printf("\nDomain information\n");
120 printf("------------------\n");
122 unistr2_to_ascii(temp, &a->domain_name, sizeof(temp)-1);
123 printf("\tdomain name :%s\n", temp);
124 printf("\tsid :%s\n", sid_to_string(temp, &a->domain_sid.sid));
127 static void decode_sam_unk0e_info(SAM_DELTA_UNK0E *a)
130 printf("\nTrust information\n");
131 printf("-----------------\n");
133 unistr2_to_ascii(temp, &a->domain, sizeof(temp)-1);
134 printf("\tdomain name :%s\n", temp);
135 printf("\tsid :%s\n", sid_to_string(temp, &a->sid.sid));
136 display_sec_desc(a->sec_desc);
139 static void decode_sam_privs_info(SAM_DELTA_PRIVS *a)
143 printf("\nSID and privileges information\n");
144 printf("------------------------------\n");
145 printf("\tsid :%s\n", sid_to_string(temp, &a->sid.sid));
146 display_sec_desc(a->sec_desc);
147 printf("\tprivileges count :%d\n", a->privlist_count);
148 for (i=0; i<a->privlist_count; i++) {
149 unistr2_to_ascii(temp, &a->uni_privslist[i], sizeof(temp)-1);
150 printf("\tprivilege name :%s\n", temp);
151 printf("\tattribute :%d\n", a->attributes[i]);
155 static void decode_sam_unk12_info(SAM_DELTA_UNK12 *a)
158 printf("\nTrusted information\n");
159 printf("-------------------\n");
161 unistr2_to_ascii(temp, &a->secret, sizeof(temp)-1);
162 printf("\tsecret name :%s\n", temp);
163 display_sec_desc(a->sec_desc);
165 printf("\ttime 1 :%s\n", http_timestring(nt_time_to_unix(&a->time1)));
166 printf("\ttime 2 :%s\n", http_timestring(nt_time_to_unix(&a->time2)));
168 display_sec_desc(a->sec_desc2);
171 static void decode_sam_stamp(SAM_DELTA_STAMP *a)
173 printf("\nStamp information\n");
174 printf("-----------------\n");
175 printf("\tsequence number :%d\n", a->seqnum);
178 static void decode_sam_deltas(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas, SAM_DELTA_CTR *deltas)
181 for (i = 0; i < num_deltas; i++) {
182 switch (hdr_deltas[i].type) {
183 case SAM_DELTA_DOMAIN_INFO: {
185 a = &deltas[i].domain_info;
186 decode_domain_info(a);
189 case SAM_DELTA_GROUP_INFO: {
191 a = &deltas[i].group_info;
192 decode_sam_group_info(a);
195 case SAM_DELTA_ACCOUNT_INFO: {
197 a = &deltas[i].account_info;
198 decode_sam_account_info(a);
201 case SAM_DELTA_GROUP_MEM: {
202 SAM_GROUP_MEM_INFO *a;
203 a = &deltas[i].grp_mem_info;
204 decode_sam_grp_mem_info(a);
207 case SAM_DELTA_ALIAS_INFO: {
209 a = &deltas[i].alias_info;
210 decode_sam_alias_info(a);
213 case SAM_DELTA_ALIAS_MEM: {
214 SAM_ALIAS_MEM_INFO *a;
215 a = &deltas[i].als_mem_info;
216 decode_sam_als_mem_info(a);
219 case SAM_DELTA_DOM_INFO: {
221 a = &deltas[i].dom_info;
222 decode_sam_dom_info(a);
225 case SAM_DELTA_UNK0E_INFO: {
227 a = &deltas[i].unk0e_info;
228 decode_sam_unk0e_info(a);
231 case SAM_DELTA_PRIVS_INFO: {
233 a = &deltas[i].privs_info;
234 decode_sam_privs_info(a);
237 case SAM_DELTA_UNK12_INFO: {
239 a = &deltas[i].unk12_info;
240 decode_sam_unk12_info(a);
243 case SAM_DELTA_SAM_STAMP: {
245 a = &deltas[i].stamp;
250 DEBUG(0,("unknown delta type: %d\n", hdr_deltas[i].type));
256 /* Synchronise sam database */
258 static NTSTATUS sam_sync(struct cli_state *cli, unsigned char trust_passwd[16],
259 BOOL do_smbpasswd_output, BOOL verbose)
262 SAM_DELTA_HDR *hdr_deltas_0, *hdr_deltas_1, *hdr_deltas_2;
263 SAM_DELTA_CTR *deltas_0, *deltas_1, *deltas_2;
264 uint32 num_deltas_0, num_deltas_1, num_deltas_2;
265 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
269 if (!(mem_ctx = talloc_init())) {
270 DEBUG(0,("talloc_init failed\n"));
274 if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
275 DEBUG(0, ("Could not initialize netlogon pipe!\n"));
279 /* Request a challenge */
281 if (!NT_STATUS_IS_OK(new_cli_nt_setup_creds(cli, trust_passwd))) {
282 DEBUG(0, ("Error initialising session creds\n"));
286 /* Do sam synchronisation on the SAM database*/
288 result = cli_netlogon_sam_sync(cli, mem_ctx, 0, &num_deltas_0, &hdr_deltas_0, &deltas_0);
290 if (!NT_STATUS_IS_OK(result))
295 decode_sam_deltas(num_deltas_0, hdr_deltas_0, deltas_0);
299 * we can't yet do several sam_sync in a raw, it's a credential problem
300 * we must chain the credentials
305 /* Do sam synchronisation on the LSA database */
307 result = cli_netlogon_sam_sync(cli, mem_ctx, 2, &num_deltas_2, &hdr_deltas_2, &deltas_2);
309 if (!NT_STATUS_IS_OK(result))
314 decode_sam_deltas(num_deltas_2, hdr_deltas_2, deltas_2);
317 /* Produce smbpasswd output - good for migrating from NT! */
319 if (do_smbpasswd_output) {
322 for (i = 0; i < num_deltas_0; i++) {
324 fstring acct_name, hex_nt_passwd, hex_lm_passwd;
325 uchar lm_passwd[16], nt_passwd[16];
327 /* Skip non-user accounts */
329 if (hdr_deltas_0[i].type != SAM_DELTA_ACCOUNT_INFO)
332 a = &deltas_0[i].account_info;
334 unistr2_to_ascii(acct_name, &a->uni_acct_name,
335 sizeof(acct_name) - 1);
337 /* Decode hashes from password hash */
339 sam_pwd_hash(a->user_rid, a->pass.buf_lm_pwd,
341 sam_pwd_hash(a->user_rid, a->pass.buf_nt_pwd,
344 /* Encode as strings */
346 smbpasswd_sethexpwd(hex_lm_passwd, lm_passwd,
348 smbpasswd_sethexpwd(hex_nt_passwd, nt_passwd,
351 /* Display user info */
353 printf("%s:%d:%s:%s:%s:LCT-0\n", acct_name,
354 a->user_rid, hex_lm_passwd, hex_nt_passwd,
355 smbpasswd_encode_acb_info(a->acb_info));
364 cli_nt_session_close(cli);
365 talloc_destroy(mem_ctx);
370 /* Replicate sam deltas */
372 static NTSTATUS sam_repl(struct cli_state *cli, unsigned char trust_passwde[16],
375 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
380 /* Print usage information */
382 static void usage(void)
384 printf("Usage: samsync [options]\n");
386 printf("\t-d debuglevel set the debuglevel\n");
387 printf("\t-h Print this help message.\n");
388 printf("\t-s configfile specify an alternative config file\n");
389 printf("\t-S synchronise sam database\n");
390 printf("\t-R replicate sam deltas\n");
391 printf("\t-U username username and password\n");
392 printf("\t-p produce smbpasswd output\n");
393 printf("\t-V verbose output\n");
397 /* Initialise client credentials for authenticated pipe access */
399 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
400 char* domain, char* password)
404 if (lp_encrypted_passwords()) {
405 pwd_make_lm_nt_16(&creds->pwd, password);
407 pwd_set_cleartext(&creds->pwd, password);
410 fstrcpy(creds->user_name, username);
411 fstrcpy(creds->domain, domain);
414 creds->pwd.null_pwd = True;
418 /* Connect to primary domain controller */
420 static struct cli_state *init_connection(struct cli_state *cli,
421 char *username, char *domain,
424 struct ntuser_creds creds;
425 extern pstring global_myname;
426 struct in_addr *dest_ip;
427 struct nmb_name calling, called;
431 /* Initialise cli_state information */
435 if (!cli_initialise(cli)) {
439 init_rpcclient_creds(&creds, username, domain, password);
440 cli_init_creds(cli, &creds);
442 /* Look up name of PDC controller */
444 if (!get_dc_list(True, lp_workgroup(), &dest_ip, &count)) {
445 DEBUG(0, ("Cannot find domain controller for domain %s\n",
450 if (!lookup_dc_name(global_myname, lp_workgroup(), dest_ip,
452 DEBUG(0, ("Could not lookup up PDC name for domain %s\n",
457 get_myname((*global_myname)?NULL:global_myname);
458 strupper(global_myname);
460 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
461 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
463 /* Establish a SMB connection */
465 if (!cli_establish_connection(cli, dest_host, dest_ip, &calling,
466 &called, "IPC$", "IPC", False, True)) {
475 int main(int argc, char **argv)
477 BOOL do_sam_sync = False, do_sam_repl = False;
478 struct cli_state cli;
481 extern pstring debugf;
482 BOOL interactive = False, do_smbpasswd_output = False;
483 BOOL verbose = False;
484 uint32 low_serial = 0;
485 unsigned char trust_passwd[16];
486 fstring username, domain, password;
493 ZERO_STRUCT(username);
495 ZERO_STRUCT(password);
497 /* Parse command line options */
499 while((opt = getopt(argc, argv, "s:d:SR:hiU:W:pV")) != EOF) {
502 pstrcpy(dyn_CONFIGFILE, optarg);
505 DEBUGLEVEL = atoi(optarg);
512 low_serial = atoi(optarg);
520 fstrcpy(username,optarg);
521 if ((lp=strchr_m(username,'%'))) {
523 fstrcpy(password,lp+1);
524 memset(strchr_m(optarg, '%') + 1, 'X',
530 pstrcpy(domain, optarg);
533 do_smbpasswd_output = True;
552 /* Initialise samba */
554 slprintf(debugf, sizeof(debugf) - 1, "%s/log.%s", dyn_LOGFILEBASE,
557 setup_logging("samsync", interactive);
562 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
563 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
568 /* Check arguments make sense */
570 if (do_sam_sync && do_sam_repl) {
571 fprintf(stderr, "cannot specify both -S and -R\n");
576 if (!do_sam_sync && !do_sam_repl) {
577 fprintf(stderr, "must specify either -S or -R\n");
581 if (do_sam_repl && low_serial == 0) {
582 fprintf(stderr, "serial number must be positive\n");
586 /* BDC operations require the machine account password */
588 if (!secrets_init()) {
589 DEBUG(0, ("Unable to initialise secrets database\n"));
593 if (!secrets_fetch_trust_account_password(lp_workgroup(),
594 trust_passwd, NULL)) {
595 DEBUG(0, ("could not fetch trust account password\n"));
599 /* Perform sync or replication */
601 if (!init_connection(&cli, username, domain, password))
605 result = sam_sync(&cli, trust_passwd, do_smbpasswd_output, verbose);
608 result = sam_repl(&cli, trust_passwd, low_serial);
610 if (!NT_STATUS_IS_OK(result)) {
611 DEBUG(0, ("%s\n", get_nt_error_msg(result)));