2 Unix SMB/CIFS implementation.
3 dump the remote SAM using rpc samsync operations
5 Copyright (C) Andrew Tridgell 2002
6 Copyright (C) Tim Potter 2001,2002
7 Modified by Volker Lendecke 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "../utils/net.h"
27 extern DOM_SID global_sid_Builtin;
29 static void display_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *g)
32 d_printf("Group mem %u: ", rid);
33 for (i=0;i<g->num_members;i++) {
34 d_printf("%u ", g->rids[i]);
39 static void display_alias_info(uint32 rid, SAM_ALIAS_INFO *a)
41 d_printf("Alias '%s' ", unistr2_static(&a->uni_als_name));
42 d_printf("desc='%s' rid=%u\n", unistr2_static(&a->uni_als_desc), a->als_rid);
45 static void display_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *a)
48 d_printf("Alias rid %u: ", rid);
49 for (i=0;i<a->num_members;i++) {
50 d_printf("%s ", sid_string_static(&a->sids[i].sid));
55 static void display_account_info(uint32 rid, SAM_ACCOUNT_INFO *a)
57 fstring hex_nt_passwd, hex_lm_passwd;
58 uchar lm_passwd[16], nt_passwd[16];
60 /* Decode hashes from password hash */
61 sam_pwd_hash(a->user_rid, a->pass.buf_lm_pwd, lm_passwd, 0);
62 sam_pwd_hash(a->user_rid, a->pass.buf_nt_pwd, nt_passwd, 0);
64 /* Encode as strings */
65 smbpasswd_sethexpwd(hex_lm_passwd, lm_passwd, a->acb_info);
66 smbpasswd_sethexpwd(hex_nt_passwd, nt_passwd, a->acb_info);
68 printf("%s:%d:%s:%s:%s:LCT-0\n", unistr2_static(&a->uni_acct_name),
69 a->user_rid, hex_lm_passwd, hex_nt_passwd,
70 smbpasswd_encode_acb_info(a->acb_info));
73 static void display_domain_info(SAM_DOMAIN_INFO *a)
75 d_printf("Domain name: %s\n", unistr2_static(&a->uni_dom_name));
78 static void display_group_info(uint32 rid, SAM_GROUP_INFO *a)
80 d_printf("Group '%s' ", unistr2_static(&a->uni_grp_name));
81 d_printf("desc='%s', rid=%u\n", unistr2_static(&a->uni_grp_desc), rid);
84 static void display_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta)
86 switch (hdr_delta->type) {
87 case SAM_DELTA_ACCOUNT_INFO:
88 display_account_info(hdr_delta->target_rid, &delta->account_info);
90 case SAM_DELTA_GROUP_MEM:
91 display_group_mem_info(hdr_delta->target_rid, &delta->grp_mem_info);
93 case SAM_DELTA_ALIAS_INFO:
94 display_alias_info(hdr_delta->target_rid, &delta->alias_info);
96 case SAM_DELTA_ALIAS_MEM:
97 display_alias_mem(hdr_delta->target_rid, &delta->als_mem_info);
99 case SAM_DELTA_DOMAIN_INFO:
100 display_domain_info(&delta->domain_info);
102 case SAM_DELTA_GROUP_INFO:
103 display_group_info(hdr_delta->target_rid, &delta->group_info);
106 d_printf("Unknown delta record type %d\n", hdr_delta->type);
112 static void dump_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds)
114 unsigned sync_context = 0;
118 SAM_DELTA_HDR *hdr_deltas;
119 SAM_DELTA_CTR *deltas;
122 if (!(mem_ctx = talloc_init())) {
126 d_printf("Dumping database %u\n", db_type);
129 result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds, db_type,
131 &num_deltas, &hdr_deltas, &deltas);
132 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), ret_creds);
133 for (i = 0; i < num_deltas; i++) {
134 display_sam_entry(&hdr_deltas[i], &deltas[i]);
137 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
139 talloc_destroy(mem_ctx);
142 /* dump sam database via samsync rpc calls */
143 int rpc_samdump(int argc, const char **argv)
146 struct cli_state *cli = NULL;
147 uchar trust_password[16];
149 uint32 neg_flags = 0x000001ff;
152 ZERO_STRUCT(ret_creds);
154 /* Connect to remote machine */
155 if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
159 if (!cli_nt_session_open(cli, PI_NETLOGON)) {
160 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
164 if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_password, NULL)) {
165 d_printf("Could not retrieve domain trust secret");
169 result = cli_nt_setup_creds(cli, SEC_CHAN_BDC, trust_password, &neg_flags, 2);
170 if (!NT_STATUS_IS_OK(result)) {
171 d_printf("Failed to setup BDC creds\n");
175 dump_database(cli, SAM_DATABASE_DOMAIN, &ret_creds);
176 dump_database(cli, SAM_DATABASE_BUILTIN, &ret_creds);
177 dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds);
179 cli_nt_session_close(cli);
185 cli_nt_session_close(cli);
190 /* Convert a SAM_ACCOUNT_DELTA to a SAM_ACCOUNT. */
193 sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta)
196 uchar lm_passwd[16], nt_passwd[16];
198 /* Username, fullname, home dir, dir drive, logon script, acct
199 desc, workstations, profile. */
201 unistr2_to_ascii(s, &delta->uni_acct_name, sizeof(s) - 1);
202 pdb_set_nt_username(account, s, PDB_CHANGED);
204 /* Unix username is the same - for sainity */
205 pdb_set_username(account, s, PDB_CHANGED);
207 unistr2_to_ascii(s, &delta->uni_full_name, sizeof(s) - 1);
208 pdb_set_fullname(account, s, PDB_CHANGED);
210 unistr2_to_ascii(s, &delta->uni_home_dir, sizeof(s) - 1);
211 pdb_set_homedir(account, s, PDB_CHANGED);
213 unistr2_to_ascii(s, &delta->uni_dir_drive, sizeof(s) - 1);
214 pdb_set_dir_drive(account, s, PDB_CHANGED);
216 unistr2_to_ascii(s, &delta->uni_logon_script, sizeof(s) - 1);
217 pdb_set_logon_script(account, s, PDB_CHANGED);
219 unistr2_to_ascii(s, &delta->uni_acct_desc, sizeof(s) - 1);
220 pdb_set_acct_desc(account, s, PDB_CHANGED);
222 unistr2_to_ascii(s, &delta->uni_workstations, sizeof(s) - 1);
223 pdb_set_workstations(account, s, PDB_CHANGED);
225 unistr2_to_ascii(s, &delta->uni_profile, sizeof(s) - 1);
226 pdb_set_profile_path(account, s, PDB_CHANGED);
228 /* User and group sid */
230 pdb_set_user_sid_from_rid(account, delta->user_rid, PDB_CHANGED);
231 pdb_set_group_sid_from_rid(account, delta->group_rid, PDB_CHANGED);
233 /* Logon and password information */
235 pdb_set_logon_time(account, nt_time_to_unix(&delta->logon_time), PDB_CHANGED);
236 pdb_set_logoff_time(account, nt_time_to_unix(&delta->logoff_time),
238 pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED);
240 /* TODO: logon hours */
241 /* TODO: bad password count */
242 /* TODO: logon count */
244 pdb_set_pass_last_set_time(
245 account, nt_time_to_unix(&delta->pwd_last_set_time), PDB_CHANGED);
247 pdb_set_kickoff_time(account, get_time_t_max(), PDB_CHANGED);
249 /* Decode hashes from password hash */
250 sam_pwd_hash(delta->user_rid, delta->pass.buf_lm_pwd, lm_passwd, 0);
251 sam_pwd_hash(delta->user_rid, delta->pass.buf_nt_pwd, nt_passwd, 0);
252 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
253 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
255 /* TODO: account expiry time */
257 pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED);
262 fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta)
267 SAM_ACCOUNT *sam_account=NULL;
271 fstrcpy(account, unistr2_static(&delta->uni_acct_name));
272 d_printf("Creating account: %s\n", account);
274 if (!NT_STATUS_IS_OK(nt_ret = pdb_init_sam(&sam_account)))
277 if (!pdb_getsampwnam(sam_account, account)) {
280 pdb_free_sam(&sam_account);
282 /* Create appropriate user */
283 if (delta->acb_info & ACB_NORMAL) {
284 pstrcpy(add_script, lp_adduser_script());
285 } else if ( (delta->acb_info & ACB_WSTRUST) ||
286 (delta->acb_info & ACB_SVRTRUST) ) {
287 pstrcpy(add_script, lp_addmachine_script());
289 DEBUG(1, ("Unknown user type: %s\n",
290 smbpasswd_encode_acb_info(delta->acb_info)));
291 pdb_free_sam(&sam_account);
292 return NT_STATUS_NO_SUCH_USER;
296 all_string_sub(add_script, "%u", account,
298 add_ret = smbrun(add_script,NULL);
299 DEBUG(1,("fetch_account: Running the command `%s' "
300 "gave %d\n", add_script, add_ret));
302 pw = getpwnam_alloc(account);
304 nt_ret = pdb_init_sam_pw(&sam_account, pw);
306 if (!NT_STATUS_IS_OK(nt_ret)) {
308 pdb_free_sam(&sam_account);
313 DEBUG(3, ("Could not create account %s\n", account));
314 pdb_free_sam(&sam_account);
315 return NT_STATUS_NO_SUCH_USER;
319 sam_account_from_delta(sam_account, delta);
321 if (!pdb_add_sam_account(sam_account)) {
322 DEBUG(1, ("SAM Account for %s already existed, updating\n",
324 pdb_update_sam_account(sam_account);
327 if (!pdb_getgrsid(&map, *pdb_get_group_sid(sam_account), False)) {
328 DEBUG(0, ("Primary group of %s has no mapping!\n",
329 pdb_get_username(sam_account)));
330 pdb_free_sam(&sam_account);
331 return NT_STATUS_NO_SUCH_GROUP;
334 if (!(grp = getgrgid(map.gid))) {
335 DEBUG(0, ("Could not find unix group %d\n", map.gid));
336 pdb_free_sam(&sam_account);
337 return NT_STATUS_NO_SUCH_GROUP;
340 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
342 pdb_free_sam(&sam_account);
347 fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta)
351 struct group *grp = NULL;
357 unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1);
358 unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1);
360 /* add the group to the mapping table */
361 sid_copy(&group_sid, get_global_sam_sid());
362 sid_append_rid(&group_sid, rid);
363 sid_to_string(sid_string, &group_sid);
365 if (pdb_getgrsid(&map, group_sid, False)) {
366 grp = getgrgid(map.gid);
374 /* No group found from mapping, find it from its name. */
375 if ((grp = getgrnam(name)) == NULL) {
376 /* No appropriate group found, create one */
377 d_printf("Creating unix group: '%s'\n", name);
378 if (smb_create_group(name, &gid) != 0)
379 return NT_STATUS_ACCESS_DENIED;
380 if ((grp = getgrgid(gid)) == NULL)
381 return NT_STATUS_ACCESS_DENIED;
385 map.gid = grp->gr_gid;
387 map.sid_name_use = SID_NAME_DOM_GRP;
388 fstrcpy(map.nt_name, name);
389 fstrcpy(map.comment, comment);
391 map.priv_set.count = 0;
392 map.priv_set.set = NULL;
395 pdb_add_group_mapping_entry(&map);
397 pdb_update_group_mapping_entry(&map);
403 fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta)
406 TALLOC_CTX *t = NULL;
407 char **nt_members = NULL;
413 if (delta->num_members == 0) {
417 sid_copy(&group_sid, get_global_sam_sid());
418 sid_append_rid(&group_sid, rid);
420 if (!get_domain_group_from_sid(group_sid, &map, False)) {
421 DEBUG(0, ("Could not find global group %d\n", rid));
422 return NT_STATUS_NO_SUCH_GROUP;
425 if (!(grp = getgrgid(map.gid))) {
426 DEBUG(0, ("Could not find unix group %d\n", map.gid));
427 return NT_STATUS_NO_SUCH_GROUP;
430 d_printf("Group members of %s: ", grp->gr_name);
432 if (!(t = talloc_init())) {
433 DEBUG(0, ("could not talloc_init\n"));
434 return NT_STATUS_NO_MEMORY;
437 nt_members = talloc_zero(t, sizeof(char *) * delta->num_members);
439 for (i=0; i<delta->num_members; i++) {
441 SAM_ACCOUNT *member = NULL;
444 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) {
449 sid_copy(&member_sid, get_global_sam_sid());
450 sid_append_rid(&member_sid, delta->rids[i]);
452 if (!pdb_getsampwsid(member, &member_sid)) {
453 DEBUG(1, ("Found bogus group member: %d\n",
455 pdb_free_sam(&member);
459 if (pdb_get_group_rid(member) == rid) {
460 d_printf("%s(primary),", pdb_get_username(member));
461 pdb_free_sam(&member);
465 d_printf("%s,", pdb_get_username(member));
466 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
467 pdb_free_sam(&member);
472 unix_members = grp->gr_mem;
474 while (*unix_members) {
475 BOOL is_nt_member = False;
476 for (i=0; i<delta->num_members; i++) {
477 if (nt_members[i] == NULL) {
478 /* This was a primary group */
482 if (strcmp(*unix_members, nt_members[i]) == 0) {
488 /* We look at a unix group member that is not
489 an nt group member. So, remove it. NT is
491 smb_delete_user_group(grp->gr_name, *unix_members);
496 for (i=0; i<delta->num_members; i++) {
497 BOOL is_unix_member = False;
499 if (nt_members[i] == NULL) {
500 /* This was the primary group */
504 unix_members = grp->gr_mem;
506 while (*unix_members) {
507 if (strcmp(*unix_members, nt_members[i]) == 0) {
508 is_unix_member = True;
514 if (!is_unix_member) {
515 /* We look at a nt group member that is not a
516 unix group member currently. So, add the nt
518 smb_add_user_group(grp->gr_name, nt_members[i]);
526 static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta,
531 struct group *grp = NULL;
537 unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1);
538 unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1);
540 /* Find out whether the group is already mapped */
541 sid_copy(&alias_sid, &dom_sid);
542 sid_append_rid(&alias_sid, rid);
543 sid_to_string(sid_string, &alias_sid);
545 if (pdb_getgrsid(&map, alias_sid, False)) {
546 grp = getgrgid(map.gid);
553 /* No group found from mapping, find it from its name. */
554 if ((grp = getgrnam(name)) == NULL) {
555 /* No appropriate group found, create one */
556 d_printf("Creating unix group: '%s'\n", name);
557 if (smb_create_group(name, &gid) != 0)
558 return NT_STATUS_ACCESS_DENIED;
559 if ((grp = getgrgid(gid)) == NULL)
560 return NT_STATUS_ACCESS_DENIED;
564 map.gid = grp->gr_gid;
567 if (sid_equal(&dom_sid, &global_sid_Builtin))
568 map.sid_name_use = SID_NAME_WKN_GRP;
570 map.sid_name_use = SID_NAME_ALIAS;
572 fstrcpy(map.nt_name, name);
573 fstrcpy(map.comment, comment);
575 map.priv_set.count = 0;
576 map.priv_set.set = NULL;
579 pdb_add_group_mapping_entry(&map);
581 pdb_update_group_mapping_entry(&map);
587 fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid)
594 fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta,
597 switch(hdr_delta->type) {
598 case SAM_DELTA_ACCOUNT_INFO:
599 fetch_account_info(hdr_delta->target_rid,
600 &delta->account_info);
602 case SAM_DELTA_GROUP_INFO:
603 fetch_group_info(hdr_delta->target_rid,
606 case SAM_DELTA_GROUP_MEM:
607 fetch_group_mem_info(hdr_delta->target_rid,
608 &delta->grp_mem_info);
610 case SAM_DELTA_ALIAS_INFO:
611 fetch_alias_info(hdr_delta->target_rid,
612 &delta->alias_info, dom_sid);
614 case SAM_DELTA_ALIAS_MEM:
615 fetch_alias_mem(hdr_delta->target_rid,
616 &delta->als_mem_info, dom_sid);
619 d_printf("Unknown delta record type %d\n", hdr_delta->type);
625 fetch_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds,
628 unsigned sync_context = 0;
632 SAM_DELTA_HDR *hdr_deltas;
633 SAM_DELTA_CTR *deltas;
636 if (!(mem_ctx = talloc_init())) {
640 d_printf("Fetching database %u\n", db_type);
643 result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds,
644 db_type, sync_context,
646 &hdr_deltas, &deltas);
647 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred),
649 for (i = 0; i < num_deltas; i++) {
650 fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid);
653 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
655 talloc_destroy(mem_ctx);
658 /* dump sam database via samsync rpc calls */
659 int rpc_vampire(int argc, const char **argv)
662 struct cli_state *cli = NULL;
663 uchar trust_password[16];
665 uint32 neg_flags = 0x000001ff;
668 ZERO_STRUCT(ret_creds);
670 /* Connect to remote machine */
671 if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS |
676 if (!cli_nt_session_open(cli, PI_NETLOGON)) {
677 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
681 if (!secrets_fetch_trust_account_password(lp_workgroup(),
682 trust_password, NULL)) {
683 d_printf("Could not retrieve domain trust secret");
687 result = cli_nt_setup_creds(cli, SEC_CHAN_BDC, trust_password,
689 if (!NT_STATUS_IS_OK(result)) {
690 d_printf("Failed to setup BDC creds\n");
694 dom_sid = *get_global_sam_sid();
695 fetch_database(cli, SAM_DATABASE_DOMAIN, &ret_creds, dom_sid);
697 sid_copy(&dom_sid, &global_sid_Builtin);
698 fetch_database(cli, SAM_DATABASE_BUILTIN, &ret_creds, dom_sid);
700 /* Currently we crash on PRIVS somewhere in unmarshalling */
701 /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */
703 cli_nt_session_close(cli);
709 cli_nt_session_close(cli);