2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static void decode_domain_info(SAM_DOMAIN_INFO
*a
)
27 printf("Domain Information\n");
28 printf("------------------\n");
30 unistr2_to_ascii(temp
, &a
->uni_dom_name
, sizeof(temp
)-1);
31 printf("\tDomain :%s\n", temp
);
32 printf("\tMin password len :%d\n", a
->min_pwd_len
);
33 printf("\tpassword history len:%d\n", a
->pwd_history_len
);
34 printf("\tcreation time :%s\n", http_timestring(nt_time_to_unix(&a
->creation_time
)));
37 static void decode_sam_group_info(SAM_GROUP_INFO
*a
)
40 printf("\nDomain Group Information\n");
41 printf("------------------------\n");
43 unistr2_to_ascii(temp
, &a
->uni_grp_name
, sizeof(temp
)-1);
44 printf("\tGroup name :%s\n", temp
);
45 unistr2_to_ascii(temp
, &a
->uni_grp_desc
, sizeof(temp
)-1);
46 printf("\tGroup description :%s\n", temp
);
47 printf("\trid :%d\n", a
->gid
.g_rid
);
48 printf("\tattribute :%d\n", a
->gid
.attr
);
51 static void decode_sam_account_info(SAM_ACCOUNT_INFO
*a
)
54 printf("\nUser Information\n");
55 printf("----------------\n");
57 unistr2_to_ascii(temp
, &a
->uni_acct_name
, sizeof(temp
)-1);
58 printf("\tUser name :%s\n", temp
);
59 printf("\tuser's rid :%d\n", a
->user_rid
);
60 printf("\tuser's primary gid :%d\n", a
->group_rid
);
61 unistr2_to_ascii(temp
, &a
->uni_full_name
, sizeof(temp
)-1);
62 printf("\tfull name :%s\n", temp
);
63 unistr2_to_ascii(temp
, &a
->uni_home_dir
, sizeof(temp
)-1);
64 printf("\thome directory :%s\n", temp
);
65 unistr2_to_ascii(temp
, &a
->uni_dir_drive
, sizeof(temp
)-1);
66 printf("\tdrive :%s\n", temp
);
67 unistr2_to_ascii(temp
, &a
->uni_logon_script
, sizeof(temp
)-1);
68 printf("\tlogon script :%s\n", temp
);
69 unistr2_to_ascii(temp
, &a
->uni_acct_desc
, sizeof(temp
)-1);
70 printf("\tdescription :%s\n", temp
);
71 unistr2_to_ascii(temp
, &a
->uni_workstations
, sizeof(temp
)-1);
72 printf("\tworkstations :%s\n", temp
);
75 static void decode_sam_grp_mem_info(SAM_GROUP_MEM_INFO
*a
)
78 printf("\nGroup members information\n");
79 printf("-------------------------\n");
80 printf("\tnum members :%d\n", a
->num_members
);
82 for (i
=0; i
<a
->num_members
; i
++) {
83 printf("\trid, attr:%d, %d\n", a
->rids
[i
], a
->attribs
[i
]);
87 static void decode_sam_alias_info(SAM_ALIAS_INFO
*a
)
90 printf("\nAlias Information\n");
91 printf("-----------------\n");
93 unistr2_to_ascii(temp
, &a
->uni_als_name
, sizeof(temp
)-1);
94 printf("\tname :%s\n", temp
);
95 unistr2_to_ascii(temp
, &a
->uni_als_desc
, sizeof(temp
)-1);
96 printf("\tdescription :%s\n", temp
);
97 printf("\trid :%d\n", a
->als_rid
);
100 static void decode_sam_als_mem_info(SAM_ALIAS_MEM_INFO
*a
)
104 printf("\nAlias members Information\n");
105 printf("-------------------------\n");
106 printf("\tnum members :%d\n", a
->num_members
);
107 printf("\tnum sids :%d\n", a
->num_sids
);
108 for (i
=0; i
<a
->num_sids
; i
++) {
109 printf("\tsid :%s\n", sid_to_string(temp
, &a
->sids
[i
].sid
));
115 static void decode_sam_dom_info(SAM_DELTA_DOM
*a
)
118 printf("\nDomain information\n");
119 printf("------------------\n");
121 unistr2_to_ascii(temp
, &a
->domain_name
, sizeof(temp
)-1);
122 printf("\tdomain name :%s\n", temp
);
123 printf("\tsid :%s\n", sid_to_string(temp
, &a
->domain_sid
.sid
));
126 static void decode_sam_unk0e_info(SAM_DELTA_UNK0E
*a
)
129 printf("\nTrust information\n");
130 printf("-----------------\n");
132 unistr2_to_ascii(temp
, &a
->domain
, sizeof(temp
)-1);
133 printf("\tdomain name :%s\n", temp
);
134 printf("\tsid :%s\n", sid_to_string(temp
, &a
->sid
.sid
));
135 display_sec_desc(a
->sec_desc
);
138 static void decode_sam_privs_info(SAM_DELTA_PRIVS
*a
)
142 printf("\nSID and privileges information\n");
143 printf("------------------------------\n");
144 printf("\tsid :%s\n", sid_to_string(temp
, &a
->sid
.sid
));
145 display_sec_desc(a
->sec_desc
);
146 printf("\tprivileges count :%d\n", a
->privlist_count
);
147 for (i
=0; i
<a
->privlist_count
; i
++) {
148 unistr2_to_ascii(temp
, &a
->uni_privslist
[i
], sizeof(temp
)-1);
149 printf("\tprivilege name :%s\n", temp
);
150 printf("\tattribute :%d\n", a
->attributes
[i
]);
154 static void decode_sam_unk12_info(SAM_DELTA_UNK12
*a
)
157 printf("\nTrusted information\n");
158 printf("-------------------\n");
160 unistr2_to_ascii(temp
, &a
->secret
, sizeof(temp
)-1);
161 printf("\tsecret name :%s\n", temp
);
162 display_sec_desc(a
->sec_desc
);
164 printf("\ttime 1 :%s\n", http_timestring(nt_time_to_unix(&a
->time1
)));
165 printf("\ttime 2 :%s\n", http_timestring(nt_time_to_unix(&a
->time2
)));
167 display_sec_desc(a
->sec_desc2
);
170 static void decode_sam_stamp(SAM_DELTA_STAMP
*a
)
172 printf("\nStamp information\n");
173 printf("-----------------\n");
174 printf("\tsequence number :%d\n", a
->seqnum
);
177 static void decode_sam_deltas(uint32 num_deltas
, SAM_DELTA_HDR
*hdr_deltas
, SAM_DELTA_CTR
*deltas
)
180 for (i
= 0; i
< num_deltas
; i
++) {
181 switch (hdr_deltas
[i
].type
) {
182 case SAM_DELTA_DOMAIN_INFO
: {
184 a
= &deltas
[i
].domain_info
;
185 decode_domain_info(a
);
188 case SAM_DELTA_GROUP_INFO
: {
190 a
= &deltas
[i
].group_info
;
191 decode_sam_group_info(a
);
194 case SAM_DELTA_ACCOUNT_INFO
: {
196 a
= &deltas
[i
].account_info
;
197 decode_sam_account_info(a
);
200 case SAM_DELTA_GROUP_MEM
: {
201 SAM_GROUP_MEM_INFO
*a
;
202 a
= &deltas
[i
].grp_mem_info
;
203 decode_sam_grp_mem_info(a
);
206 case SAM_DELTA_ALIAS_INFO
: {
208 a
= &deltas
[i
].alias_info
;
209 decode_sam_alias_info(a
);
212 case SAM_DELTA_ALIAS_MEM
: {
213 SAM_ALIAS_MEM_INFO
*a
;
214 a
= &deltas
[i
].als_mem_info
;
215 decode_sam_als_mem_info(a
);
218 case SAM_DELTA_DOM_INFO
: {
220 a
= &deltas
[i
].dom_info
;
221 decode_sam_dom_info(a
);
224 case SAM_DELTA_UNK0E_INFO
: {
226 a
= &deltas
[i
].unk0e_info
;
227 decode_sam_unk0e_info(a
);
230 case SAM_DELTA_PRIVS_INFO
: {
232 a
= &deltas
[i
].privs_info
;
233 decode_sam_privs_info(a
);
236 case SAM_DELTA_UNK12_INFO
: {
238 a
= &deltas
[i
].unk12_info
;
239 decode_sam_unk12_info(a
);
242 case SAM_DELTA_SAM_STAMP
: {
244 a
= &deltas
[i
].stamp
;
249 DEBUG(0,("unknown delta type: %d\n", hdr_deltas
[i
].type
));
255 /* Synchronise sam database */
257 static NTSTATUS
sam_sync(struct cli_state
*cli
, unsigned char trust_passwd
[16],
258 BOOL do_smbpasswd_output
, BOOL verbose
)
261 SAM_DELTA_HDR
*hdr_deltas_0
, *hdr_deltas_1
, *hdr_deltas_2
;
262 SAM_DELTA_CTR
*deltas_0
, *deltas_1
, *deltas_2
;
263 uint32 num_deltas_0
, num_deltas_1
, num_deltas_2
;
264 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 /* on first call the returnAuthenticator is empty */
287 memset(&ret_creds
, 0, sizeof(ret_creds
));
289 /* Do sam synchronisation on the SAM database*/
291 result
= cli_netlogon_sam_sync(cli
, mem_ctx
, &ret_creds
, 0, &num_deltas_0
, &hdr_deltas_0
, &deltas_0
);
293 if (!NT_STATUS_IS_OK(result
))
298 decode_sam_deltas(num_deltas_0
, hdr_deltas_0
, deltas_0
);
302 * we can't yet do several sam_sync in a raw, it's a credential problem
303 * we must chain the credentials
307 /* Do sam synchronisation on the LSA database */
309 result
= cli_netlogon_sam_sync(cli
, mem_ctx
, &ret_creds
, 2, &num_deltas_2
, &hdr_deltas_2
, &deltas_2
);
311 if (!NT_STATUS_IS_OK(result
))
316 decode_sam_deltas(num_deltas_2
, hdr_deltas_2
, deltas_2
);
319 /* Produce smbpasswd output - good for migrating from NT! */
321 if (do_smbpasswd_output
) {
324 for (i
= 0; i
< num_deltas_0
; i
++) {
326 fstring acct_name
, hex_nt_passwd
, hex_lm_passwd
;
327 uchar lm_passwd
[16], nt_passwd
[16];
329 /* Skip non-user accounts */
331 if (hdr_deltas_0
[i
].type
!= SAM_DELTA_ACCOUNT_INFO
)
334 a
= &deltas_0
[i
].account_info
;
336 unistr2_to_ascii(acct_name
, &a
->uni_acct_name
,
337 sizeof(acct_name
) - 1);
339 /* Decode hashes from password hash */
341 sam_pwd_hash(a
->user_rid
, a
->pass
.buf_lm_pwd
,
343 sam_pwd_hash(a
->user_rid
, a
->pass
.buf_nt_pwd
,
346 /* Encode as strings */
348 smbpasswd_sethexpwd(hex_lm_passwd
, lm_passwd
,
350 smbpasswd_sethexpwd(hex_nt_passwd
, nt_passwd
,
353 /* Display user info */
355 printf("%s:%d:%s:%s:%s:LCT-0\n", acct_name
,
356 a
->user_rid
, hex_lm_passwd
, hex_nt_passwd
,
357 smbpasswd_encode_acb_info(a
->acb_info
));
366 cli_nt_session_close(cli
);
367 talloc_destroy(mem_ctx
);
372 /* Replicate sam deltas */
374 static NTSTATUS
sam_repl(struct cli_state
*cli
, unsigned char trust_passwde
[16],
377 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
382 /* Print usage information */
384 static void usage(void)
386 printf("Usage: samsync [options]\n");
388 printf("\t-d debuglevel set the debuglevel\n");
389 printf("\t-h Print this help message.\n");
390 printf("\t-s configfile specify an alternative config file\n");
391 printf("\t-S synchronise sam database\n");
392 printf("\t-R replicate sam deltas\n");
393 printf("\t-U username username and password\n");
394 printf("\t-p produce smbpasswd output\n");
395 printf("\t-V verbose output\n");
399 /* Initialise client credentials for authenticated pipe access */
401 void init_rpcclient_creds(struct ntuser_creds
*creds
, char* username
,
402 char* domain
, char* password
)
406 if (lp_encrypted_passwords()) {
407 pwd_make_lm_nt_16(&creds
->pwd
, password
);
409 pwd_set_cleartext(&creds
->pwd
, password
);
412 fstrcpy(creds
->user_name
, username
);
413 fstrcpy(creds
->domain
, domain
);
416 creds
->pwd
.null_pwd
= True
;
420 /* Connect to primary domain controller */
422 static struct cli_state
*init_connection(struct cli_state
*cli
,
423 char *username
, char *domain
,
426 struct ntuser_creds creds
;
427 extern pstring global_myname
;
428 struct in_addr
*dest_ip
;
429 struct nmb_name calling
, called
;
433 /* Initialise cli_state information */
437 if (!cli_initialise(cli
)) {
441 init_rpcclient_creds(&creds
, username
, domain
, password
);
442 cli_init_creds(cli
, &creds
);
444 /* Look up name of PDC controller */
446 if (!get_dc_list(True
, lp_workgroup(), &dest_ip
, &count
)) {
447 DEBUG(0, ("Cannot find domain controller for domain %s\n",
452 if (!lookup_dc_name(global_myname
, lp_workgroup(), dest_ip
,
454 DEBUG(0, ("Could not lookup up PDC name for domain %s\n",
459 get_myname((*global_myname
)?NULL
:global_myname
);
460 strupper(global_myname
);
462 make_nmb_name(&called
, dns_to_netbios_name(dest_host
), 0x20);
463 make_nmb_name(&calling
, dns_to_netbios_name(global_myname
), 0);
465 /* Establish a SMB connection */
467 if (!cli_establish_connection(cli
, dest_host
, dest_ip
, &calling
,
468 &called
, "IPC$", "IPC", False
, True
)) {
477 int main(int argc
, char **argv
)
479 BOOL do_sam_sync
= False
, do_sam_repl
= False
;
480 struct cli_state cli
;
484 BOOL interactive
= False
, do_smbpasswd_output
= False
;
485 BOOL verbose
= False
;
486 uint32 low_serial
= 0;
487 unsigned char trust_passwd
[16];
488 fstring username
, domain
, password
;
495 ZERO_STRUCT(username
);
497 ZERO_STRUCT(password
);
499 /* Parse command line options */
501 while((opt
= getopt(argc
, argv
, "s:d:SR:hiU:W:pV")) != EOF
) {
504 pstrcpy(dyn_CONFIGFILE
, optarg
);
507 DEBUGLEVEL
= atoi(optarg
);
514 low_serial
= atoi(optarg
);
522 fstrcpy(username
,optarg
);
523 if ((lp
=strchr_m(username
,'%'))) {
525 fstrcpy(password
,lp
+1);
526 memset(strchr_m(optarg
, '%') + 1, 'X',
532 pstrcpy(domain
, optarg
);
535 do_smbpasswd_output
= True
;
554 /* Initialise samba */
556 slprintf(logfile
, sizeof(logfile
) - 1, "%s/log.%s", dyn_LOGFILEBASE
,
558 lp_set_logfile(logfile
);
560 setup_logging("samsync", interactive
);
565 if (!lp_load(dyn_CONFIGFILE
, True
, False
, False
)) {
566 fprintf(stderr
, "Can't load %s\n", dyn_CONFIGFILE
);
571 /* Check arguments make sense */
573 if (do_sam_sync
&& do_sam_repl
) {
574 fprintf(stderr
, "cannot specify both -S and -R\n");
579 if (!do_sam_sync
&& !do_sam_repl
) {
580 fprintf(stderr
, "must specify either -S or -R\n");
584 if (do_sam_repl
&& low_serial
== 0) {
585 fprintf(stderr
, "serial number must be positive\n");
589 /* BDC operations require the machine account password */
591 if (!secrets_init()) {
592 DEBUG(0, ("Unable to initialise secrets database\n"));
596 if (!secrets_fetch_trust_account_password(lp_workgroup(),
597 trust_passwd
, NULL
)) {
598 DEBUG(0, ("could not fetch trust account password\n"));
602 /* Perform sync or replication */
604 if (!init_connection(&cli
, username
, domain
, password
))
608 result
= sam_sync(&cli
, trust_passwd
, do_smbpasswd_output
, verbose
);
611 result
= sam_repl(&cli
, trust_passwd
, low_serial
);
613 if (!NT_STATUS_IS_OK(result
)) {
614 DEBUG(0, ("%s\n", get_nt_error_msg(result
)));