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];
59 static uchar zero_buf
[16];
61 /* Decode hashes from password hash (if they are not NULL) */
63 if (memcmp(a
->pass
.buf_lm_pwd
, zero_buf
, 16) != 0) {
64 sam_pwd_hash(a
->user_rid
, a
->pass
.buf_lm_pwd
, lm_passwd
, 0);
65 pdb_sethexpwd(hex_lm_passwd
, lm_passwd
, a
->acb_info
);
67 pdb_sethexpwd(hex_lm_passwd
, NULL
, 0);
70 if (memcmp(a
->pass
.buf_nt_pwd
, zero_buf
, 16) != 0) {
71 sam_pwd_hash(a
->user_rid
, a
->pass
.buf_nt_pwd
, nt_passwd
, 0);
72 pdb_sethexpwd(hex_nt_passwd
, nt_passwd
, a
->acb_info
);
74 pdb_sethexpwd(hex_nt_passwd
, NULL
, 0);
77 printf("%s:%d:%s:%s:%s:LCT-0\n", unistr2_static(&a
->uni_acct_name
),
78 a
->user_rid
, hex_lm_passwd
, hex_nt_passwd
,
79 pdb_encode_acct_ctrl(a
->acb_info
, NEW_PW_FORMAT_SPACE_PADDED_LEN
));
82 static void display_domain_info(SAM_DOMAIN_INFO
*a
)
84 d_printf("Domain name: %s\n", unistr2_static(&a
->uni_dom_name
));
87 static void display_group_info(uint32 rid
, SAM_GROUP_INFO
*a
)
89 d_printf("Group '%s' ", unistr2_static(&a
->uni_grp_name
));
90 d_printf("desc='%s', rid=%u\n", unistr2_static(&a
->uni_grp_desc
), rid
);
93 static void display_sam_entry(SAM_DELTA_HDR
*hdr_delta
, SAM_DELTA_CTR
*delta
)
95 switch (hdr_delta
->type
) {
96 case SAM_DELTA_ACCOUNT_INFO
:
97 display_account_info(hdr_delta
->target_rid
, &delta
->account_info
);
99 case SAM_DELTA_GROUP_MEM
:
100 display_group_mem_info(hdr_delta
->target_rid
, &delta
->grp_mem_info
);
102 case SAM_DELTA_ALIAS_INFO
:
103 display_alias_info(hdr_delta
->target_rid
, &delta
->alias_info
);
105 case SAM_DELTA_ALIAS_MEM
:
106 display_alias_mem(hdr_delta
->target_rid
, &delta
->als_mem_info
);
108 case SAM_DELTA_DOMAIN_INFO
:
109 display_domain_info(&delta
->domain_info
);
111 case SAM_DELTA_GROUP_INFO
:
112 display_group_info(hdr_delta
->target_rid
, &delta
->group_info
);
114 /* The following types are recognised but not handled */
115 case SAM_DELTA_RENAME_GROUP
:
116 d_printf("SAM_DELTA_RENAME_GROUP not handled\n");
118 case SAM_DELTA_RENAME_USER
:
119 d_printf("SAM_DELTA_RENAME_USER not handled\n");
121 case SAM_DELTA_RENAME_ALIAS
:
122 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
124 case SAM_DELTA_POLICY_INFO
:
125 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
127 case SAM_DELTA_TRUST_DOMS
:
128 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
130 case SAM_DELTA_PRIVS_INFO
:
131 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
133 case SAM_DELTA_SECRET_INFO
:
134 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
136 case SAM_DELTA_DELETE_GROUP
:
137 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
139 case SAM_DELTA_DELETE_USER
:
140 d_printf("SAM_DELTA_DELETE_USER not handled\n");
142 case SAM_DELTA_MODIFIED_COUNT
:
143 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
146 d_printf("Unknown delta record type %d\n", hdr_delta
->type
);
152 static void dump_database(struct cli_state
*cli
, unsigned db_type
, DOM_CRED
*ret_creds
)
154 unsigned sync_context
= 0;
158 SAM_DELTA_HDR
*hdr_deltas
;
159 SAM_DELTA_CTR
*deltas
;
162 if (!(mem_ctx
= talloc_init("dump_database"))) {
167 case SAM_DATABASE_DOMAIN
:
168 d_printf("Dumping DOMAIN database\n");
170 case SAM_DATABASE_BUILTIN
:
171 d_printf("Dumping BUILTIN database\n");
173 case SAM_DATABASE_PRIVS
:
174 d_printf("Dumping PRIVS databases\n");
177 d_printf("Dumping unknown database type %u\n", db_type
);
182 result
= cli_netlogon_sam_sync(cli
, mem_ctx
, ret_creds
, db_type
,
184 &num_deltas
, &hdr_deltas
, &deltas
);
185 if (NT_STATUS_IS_ERR(result
))
188 clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
), ret_creds
);
189 for (i
= 0; i
< num_deltas
; i
++) {
190 display_sam_entry(&hdr_deltas
[i
], &deltas
[i
]);
193 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
195 talloc_destroy(mem_ctx
);
198 /* dump sam database via samsync rpc calls */
199 NTSTATUS
rpc_samdump_internals(const DOM_SID
*domain_sid
,
200 const char *domain_name
,
201 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
202 int argc
, const char **argv
)
204 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
205 uchar trust_password
[16];
209 ZERO_STRUCT(ret_creds
);
211 fstrcpy(cli
->domain
, domain_name
);
213 if (!secrets_fetch_trust_account_password(domain_name
,
215 NULL
, &sec_channel
)) {
216 DEBUG(0,("Could not fetch trust account password\n"));
220 if (!NT_STATUS_IS_OK(nt_status
= cli_nt_establish_netlogon(cli
, sec_channel
,
222 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
226 dump_database(cli
, SAM_DATABASE_DOMAIN
, &ret_creds
);
227 dump_database(cli
, SAM_DATABASE_BUILTIN
, &ret_creds
);
228 dump_database(cli
, SAM_DATABASE_PRIVS
, &ret_creds
);
230 nt_status
= NT_STATUS_OK
;
233 cli_nt_session_close(cli
);
237 /* Convert a SAM_ACCOUNT_DELTA to a SAM_ACCOUNT. */
238 #define STRING_CHANGED (old_string && !new_string) ||\
239 (!old_string && new_string) ||\
240 (old_string && new_string && (strcmp(old_string, new_string) != 0))
243 sam_account_from_delta(SAM_ACCOUNT
*account
, SAM_ACCOUNT_INFO
*delta
)
245 const char *old_string
, *new_string
;
246 time_t unix_time
, stored_time
;
247 uchar lm_passwd
[16], nt_passwd
[16];
248 static uchar zero_buf
[16];
250 /* Username, fullname, home dir, dir drive, logon script, acct
251 desc, workstations, profile. */
253 if (delta
->hdr_acct_name
.buffer
) {
254 old_string
= pdb_get_nt_username(account
);
255 new_string
= unistr2_static(&delta
->uni_acct_name
);
257 if (STRING_CHANGED
) {
258 pdb_set_nt_username(account
, new_string
, PDB_CHANGED
);
262 /* Unix username is the same - for sanity */
263 old_string
= pdb_get_username( account
);
264 if (STRING_CHANGED
) {
265 pdb_set_username(account
, new_string
, PDB_CHANGED
);
269 if (delta
->hdr_full_name
.buffer
) {
270 old_string
= pdb_get_fullname(account
);
271 new_string
= unistr2_static(&delta
->uni_full_name
);
274 pdb_set_fullname(account
, new_string
, PDB_CHANGED
);
277 if (delta
->hdr_home_dir
.buffer
) {
278 old_string
= pdb_get_homedir(account
);
279 new_string
= unistr2_static(&delta
->uni_home_dir
);
282 pdb_set_homedir(account
, new_string
, PDB_CHANGED
);
285 if (delta
->hdr_dir_drive
.buffer
) {
286 old_string
= pdb_get_dir_drive(account
);
287 new_string
= unistr2_static(&delta
->uni_dir_drive
);
290 pdb_set_dir_drive(account
, new_string
, PDB_CHANGED
);
293 if (delta
->hdr_logon_script
.buffer
) {
294 old_string
= pdb_get_logon_script(account
);
295 new_string
= unistr2_static(&delta
->uni_logon_script
);
298 pdb_set_logon_script(account
, new_string
, PDB_CHANGED
);
301 if (delta
->hdr_acct_desc
.buffer
) {
302 old_string
= pdb_get_acct_desc(account
);
303 new_string
= unistr2_static(&delta
->uni_acct_desc
);
306 pdb_set_acct_desc(account
, new_string
, PDB_CHANGED
);
309 if (delta
->hdr_workstations
.buffer
) {
310 old_string
= pdb_get_workstations(account
);
311 new_string
= unistr2_static(&delta
->uni_workstations
);
314 pdb_set_workstations(account
, new_string
, PDB_CHANGED
);
317 if (delta
->hdr_profile
.buffer
) {
318 old_string
= pdb_get_profile_path(account
);
319 new_string
= unistr2_static(&delta
->uni_profile
);
322 pdb_set_profile_path(account
, new_string
, PDB_CHANGED
);
325 /* User and group sid */
326 if (pdb_get_user_rid(account
) != delta
->user_rid
)
327 pdb_set_user_sid_from_rid(account
, delta
->user_rid
, PDB_CHANGED
);
328 if (pdb_get_group_rid(account
) != delta
->group_rid
)
329 pdb_set_group_sid_from_rid(account
, delta
->group_rid
, PDB_CHANGED
);
331 /* Logon and password information */
332 if (!nt_time_is_zero(&delta
->logon_time
)) {
333 unix_time
= nt_time_to_unix(&delta
->logon_time
);
334 stored_time
= pdb_get_logon_time(account
);
335 if (stored_time
!= unix_time
)
336 pdb_set_logon_time(account
, unix_time
, PDB_CHANGED
);
339 if (!nt_time_is_zero(&delta
->logoff_time
)) {
340 unix_time
= nt_time_to_unix(&delta
->logoff_time
);
341 stored_time
= pdb_get_logoff_time(account
);
342 if (stored_time
!= unix_time
)
343 pdb_set_logoff_time(account
, unix_time
,PDB_CHANGED
);
346 if (pdb_get_logon_divs(account
) != delta
->logon_divs
)
347 pdb_set_logon_divs(account
, delta
->logon_divs
, PDB_CHANGED
);
349 /* TODO: logon hours */
350 /* TODO: bad password count */
351 /* TODO: logon count */
353 if (!nt_time_is_zero(&delta
->pwd_last_set_time
)) {
354 unix_time
= nt_time_to_unix(&delta
->pwd_last_set_time
);
355 stored_time
= pdb_get_pass_last_set_time(account
);
356 if (stored_time
!= unix_time
)
357 pdb_set_pass_last_set_time(account
, unix_time
, PDB_CHANGED
);
361 /* No kickoff time in the delta? */
362 if (!nt_time_is_zero(&delta
->kickoff_time
)) {
363 unix_time
= nt_time_to_unix(&delta
->kickoff_time
);
364 stored_time
= pdb_get_kickoff_time(account
);
365 if (stored_time
!= unix_time
)
366 pdb_set_kickoff_time(account
, unix_time
, PDB_CHANGED
);
370 /* Decode hashes from password hash
371 Note that win2000 may send us all zeros for the hashes if it doesn't
372 think this channel is secure enough - don't set the passwords at all
375 if (memcmp(delta
->pass
.buf_lm_pwd
, zero_buf
, 16) != 0) {
376 sam_pwd_hash(delta
->user_rid
, delta
->pass
.buf_lm_pwd
, lm_passwd
, 0);
377 pdb_set_lanman_passwd(account
, lm_passwd
, PDB_CHANGED
);
380 if (memcmp(delta
->pass
.buf_nt_pwd
, zero_buf
, 16) != 0) {
381 sam_pwd_hash(delta
->user_rid
, delta
->pass
.buf_nt_pwd
, nt_passwd
, 0);
382 pdb_set_nt_passwd(account
, nt_passwd
, PDB_CHANGED
);
385 /* TODO: account expiry time */
387 if (pdb_get_acct_ctrl(account
) != delta
->acb_info
)
388 pdb_set_acct_ctrl(account
, delta
->acb_info
, PDB_CHANGED
);
390 pdb_set_domain(account
, lp_workgroup(), PDB_CHANGED
);
395 static NTSTATUS
fetch_account_info(uint32 rid
, SAM_ACCOUNT_INFO
*delta
)
400 SAM_ACCOUNT
*sam_account
=NULL
;
405 struct passwd
*passwd
;
408 fstrcpy(account
, unistr2_static(&delta
->uni_acct_name
));
409 d_printf("Creating account: %s\n", account
);
411 if (!NT_STATUS_IS_OK(nt_ret
= pdb_init_sam(&sam_account
)))
414 if (!(passwd
= Get_Pwnam(account
))) {
415 /* Create appropriate user */
416 if (delta
->acb_info
& ACB_NORMAL
) {
417 pstrcpy(add_script
, lp_adduser_script());
418 } else if ( (delta
->acb_info
& ACB_WSTRUST
) ||
419 (delta
->acb_info
& ACB_SVRTRUST
) ||
420 (delta
->acb_info
& ACB_DOMTRUST
) ) {
421 pstrcpy(add_script
, lp_addmachine_script());
423 DEBUG(1, ("Unknown user type: %s\n",
424 pdb_encode_acct_ctrl(delta
->acb_info
, NEW_PW_FORMAT_SPACE_PADDED_LEN
)));
425 nt_ret
= NT_STATUS_UNSUCCESSFUL
;
430 all_string_sub(add_script
, "%u", account
,
432 add_ret
= smbrun(add_script
,NULL
);
433 DEBUG(1,("fetch_account: Running the command `%s' "
434 "gave %d\n", add_script
, add_ret
));
436 DEBUG(8,("fetch_account_info: no add user/machine script. Asking winbindd\n"));
438 /* don't need a RID allocated since the user already has a SID */
439 if ( !winbind_create_user( account
, NULL
) )
440 DEBUG(4,("fetch_account_info: winbind_create_user() failed\n"));
443 /* try and find the possible unix account again */
444 if ( !(passwd
= Get_Pwnam(account
)) ) {
445 d_printf("Could not create posix account info for '%s'\n", account
);
446 nt_ret
= NT_STATUS_NO_SUCH_USER
;
451 sid_copy(&user_sid
, get_global_sam_sid());
452 sid_append_rid(&user_sid
, delta
->user_rid
);
454 DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n", sid_to_string(sid_string
, &user_sid
), account
));
455 if (!pdb_getsampwsid(sam_account
, &user_sid
)) {
456 sam_account_from_delta(sam_account
, delta
);
457 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
458 sid_to_string(sid_string
, &user_sid
), pdb_get_username(sam_account
)));
459 if (!pdb_add_sam_account(sam_account
)) {
460 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
462 return NT_STATUS_ACCESS_DENIED
;
465 sam_account_from_delta(sam_account
, delta
);
466 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
467 sid_to_string(sid_string
, &user_sid
), pdb_get_username(sam_account
)));
468 if (!pdb_update_sam_account(sam_account
)) {
469 DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
471 pdb_free_sam(&sam_account
);
472 return NT_STATUS_ACCESS_DENIED
;
476 group_sid
= *pdb_get_group_sid(sam_account
);
478 if (!pdb_getgrsid(&map
, group_sid
)) {
479 DEBUG(0, ("Primary group of %s has no mapping!\n",
480 pdb_get_username(sam_account
)));
482 if (map
.gid
!= passwd
->pw_gid
) {
483 if (!(grp
= getgrgid(map
.gid
))) {
484 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
485 (unsigned long)map
.gid
, pdb_get_username(sam_account
), sid_string_static(&group_sid
)));
487 smb_set_primary_group(grp
->gr_name
, pdb_get_username(sam_account
));
493 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
494 pdb_get_username(sam_account
)));
498 pdb_free_sam(&sam_account
);
503 fetch_group_info(uint32 rid
, SAM_GROUP_INFO
*delta
)
507 struct group
*grp
= NULL
;
513 unistr2_to_ascii(name
, &delta
->uni_grp_name
, sizeof(name
)-1);
514 unistr2_to_ascii(comment
, &delta
->uni_grp_desc
, sizeof(comment
)-1);
516 /* add the group to the mapping table */
517 sid_copy(&group_sid
, get_global_sam_sid());
518 sid_append_rid(&group_sid
, rid
);
519 sid_to_string(sid_string
, &group_sid
);
521 if (pdb_getgrsid(&map
, group_sid
)) {
523 grp
= getgrgid(map
.gid
);
530 /* No group found from mapping, find it from its name. */
531 if ((grp
= getgrnam(name
)) == NULL
) {
533 /* No appropriate group found, create one */
535 d_printf("Creating unix group: '%s'\n", name
);
537 if (smb_create_group(name
, &gid
) != 0)
538 return NT_STATUS_ACCESS_DENIED
;
540 if ((grp
= getgrnam(name
)) == NULL
)
541 return NT_STATUS_ACCESS_DENIED
;
545 map
.gid
= grp
->gr_gid
;
547 map
.sid_name_use
= SID_NAME_DOM_GRP
;
548 fstrcpy(map
.nt_name
, name
);
549 if (delta
->hdr_grp_desc
.buffer
) {
550 fstrcpy(map
.comment
, comment
);
552 fstrcpy(map
.comment
, "");
556 pdb_add_group_mapping_entry(&map
);
558 pdb_update_group_mapping_entry(&map
);
564 fetch_group_mem_info(uint32 rid
, SAM_GROUP_MEM_INFO
*delta
)
567 TALLOC_CTX
*t
= NULL
;
568 char **nt_members
= NULL
;
574 if (delta
->num_members
== 0) {
578 sid_copy(&group_sid
, get_global_sam_sid());
579 sid_append_rid(&group_sid
, rid
);
581 if (!get_domain_group_from_sid(group_sid
, &map
)) {
582 DEBUG(0, ("Could not find global group %d\n", rid
));
583 return NT_STATUS_NO_SUCH_GROUP
;
586 if (!(grp
= getgrgid(map
.gid
))) {
587 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map
.gid
));
588 return NT_STATUS_NO_SUCH_GROUP
;
591 d_printf("Group members of %s: ", grp
->gr_name
);
593 if (!(t
= talloc_init("fetch_group_mem_info"))) {
594 DEBUG(0, ("could not talloc_init\n"));
595 return NT_STATUS_NO_MEMORY
;
598 nt_members
= talloc_zero(t
, sizeof(char *) * delta
->num_members
);
600 for (i
=0; i
<delta
->num_members
; i
++) {
602 SAM_ACCOUNT
*member
= NULL
;
605 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_sam_talloc(t
, &member
))) {
610 sid_copy(&member_sid
, get_global_sam_sid());
611 sid_append_rid(&member_sid
, delta
->rids
[i
]);
613 if (!pdb_getsampwsid(member
, &member_sid
)) {
614 DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
615 delta
->rids
[i
], sid_string_static(&member_sid
), grp
->gr_name
));
616 pdb_free_sam(&member
);
620 if (pdb_get_group_rid(member
) == rid
) {
621 d_printf("%s(primary),", pdb_get_username(member
));
622 pdb_free_sam(&member
);
626 d_printf("%s,", pdb_get_username(member
));
627 nt_members
[i
] = talloc_strdup(t
, pdb_get_username(member
));
628 pdb_free_sam(&member
);
633 unix_members
= grp
->gr_mem
;
635 while (*unix_members
) {
636 BOOL is_nt_member
= False
;
637 for (i
=0; i
<delta
->num_members
; i
++) {
638 if (nt_members
[i
] == NULL
) {
639 /* This was a primary group */
643 if (strcmp(*unix_members
, nt_members
[i
]) == 0) {
649 /* We look at a unix group member that is not
650 an nt group member. So, remove it. NT is
652 smb_delete_user_group(grp
->gr_name
, *unix_members
);
657 for (i
=0; i
<delta
->num_members
; i
++) {
658 BOOL is_unix_member
= False
;
660 if (nt_members
[i
] == NULL
) {
661 /* This was the primary group */
665 unix_members
= grp
->gr_mem
;
667 while (*unix_members
) {
668 if (strcmp(*unix_members
, nt_members
[i
]) == 0) {
669 is_unix_member
= True
;
675 if (!is_unix_member
) {
676 /* We look at a nt group member that is not a
677 unix group member currently. So, add the nt
679 smb_add_user_group(grp
->gr_name
, nt_members
[i
]);
687 static NTSTATUS
fetch_alias_info(uint32 rid
, SAM_ALIAS_INFO
*delta
,
692 struct group
*grp
= NULL
;
698 unistr2_to_ascii(name
, &delta
->uni_als_name
, sizeof(name
)-1);
699 unistr2_to_ascii(comment
, &delta
->uni_als_desc
, sizeof(comment
)-1);
701 /* Find out whether the group is already mapped */
702 sid_copy(&alias_sid
, &dom_sid
);
703 sid_append_rid(&alias_sid
, rid
);
704 sid_to_string(sid_string
, &alias_sid
);
706 if (pdb_getgrsid(&map
, alias_sid
)) {
707 grp
= getgrgid(map
.gid
);
714 /* No group found from mapping, find it from its name. */
715 if ((grp
= getgrnam(name
)) == NULL
) {
716 /* No appropriate group found, create one */
717 d_printf("Creating unix group: '%s'\n", name
);
718 if (smb_create_group(name
, &gid
) != 0)
719 return NT_STATUS_ACCESS_DENIED
;
720 if ((grp
= getgrgid(gid
)) == NULL
)
721 return NT_STATUS_ACCESS_DENIED
;
725 map
.gid
= grp
->gr_gid
;
728 if (sid_equal(&dom_sid
, &global_sid_Builtin
))
729 map
.sid_name_use
= SID_NAME_WKN_GRP
;
731 map
.sid_name_use
= SID_NAME_ALIAS
;
733 fstrcpy(map
.nt_name
, name
);
734 fstrcpy(map
.comment
, comment
);
737 pdb_add_group_mapping_entry(&map
);
739 pdb_update_group_mapping_entry(&map
);
745 fetch_alias_mem(uint32 rid
, SAM_ALIAS_MEM_INFO
*delta
, DOM_SID dom_sid
)
748 * commented out right now after talking to Volker. Can't
749 * do much with the membership but seemed a shame to waste
750 * somewhat working code. Needs testing because the membership
751 * that shows up surprises me. Also can't do much with groups
752 * in groups (e.g. Domain Admins being a member of Adminsitrators).
757 TALLOC_CTX
*t
= NULL
;
758 char **nt_members
= NULL
;
763 enum SID_NAME_USE sid_type
;
765 if (delta
->num_members
== 0) {
769 sid_copy(&group_sid
, &dom_sid
);
770 sid_append_rid(&group_sid
, rid
);
772 if (sid_equal(&dom_sid
, &global_sid_Builtin
)) {
773 sid_type
= SID_NAME_WKN_GRP
;
774 if (!get_builtin_group_from_sid(&group_sid
, &map
, False
)) {
775 DEBUG(0, ("Could not find builtin group %s\n", sid_string_static(&group_sid
)));
776 return NT_STATUS_NO_SUCH_GROUP
;
779 sid_type
= SID_NAME_ALIAS
;
780 if (!get_local_group_from_sid(&group_sid
, &map
, False
)) {
781 DEBUG(0, ("Could not find local group %s\n", sid_string_static(&group_sid
)));
782 return NT_STATUS_NO_SUCH_GROUP
;
786 if (!(grp
= getgrgid(map
.gid
))) {
787 DEBUG(0, ("Could not find unix group %d\n", map
.gid
));
788 return NT_STATUS_NO_SUCH_GROUP
;
791 d_printf("Group members of %s: ", grp
->gr_name
);
793 if (!(t
= talloc_init("fetch_group_mem_info"))) {
794 DEBUG(0, ("could not talloc_init\n"));
795 return NT_STATUS_NO_MEMORY
;
798 nt_members
= talloc_zero(t
, sizeof(char *) * delta
->num_members
);
800 for (i
=0; i
<delta
->num_members
; i
++) {
802 SAM_ACCOUNT
*member
= NULL
;
805 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_sam_talloc(t
, &member
))) {
810 sid_copy(&member_sid
, &delta
->sids
[i
].sid
);
812 if (!pdb_getsampwsid(member
, &member_sid
)) {
813 DEBUG(1, ("Found bogus group member: (member_sid=%s group=%s)\n",
814 sid_string_static(&member_sid
), grp
->gr_name
));
815 pdb_free_sam(&member
);
819 if (pdb_get_group_rid(member
) == rid
) {
820 d_printf("%s(primary),", pdb_get_username(member
));
821 pdb_free_sam(&member
);
825 d_printf("%s,", pdb_get_username(member
));
826 nt_members
[i
] = talloc_strdup(t
, pdb_get_username(member
));
827 pdb_free_sam(&member
);
832 unix_members
= grp
->gr_mem
;
834 while (*unix_members
) {
835 BOOL is_nt_member
= False
;
836 for (i
=0; i
<delta
->num_members
; i
++) {
837 if (nt_members
[i
] == NULL
) {
838 /* This was a primary group */
842 if (strcmp(*unix_members
, nt_members
[i
]) == 0) {
848 /* We look at a unix group member that is not
849 an nt group member. So, remove it. NT is
851 smb_delete_user_group(grp
->gr_name
, *unix_members
);
856 for (i
=0; i
<delta
->num_members
; i
++) {
857 BOOL is_unix_member
= False
;
859 if (nt_members
[i
] == NULL
) {
860 /* This was the primary group */
864 unix_members
= grp
->gr_mem
;
866 while (*unix_members
) {
867 if (strcmp(*unix_members
, nt_members
[i
]) == 0) {
868 is_unix_member
= True
;
874 if (!is_unix_member
) {
875 /* We look at a nt group member that is not a
876 unix group member currently. So, add the nt
878 smb_add_user_group(grp
->gr_name
, nt_members
[i
]);
884 #endif /* end of fetch_alias_mem() */
890 fetch_sam_entry(SAM_DELTA_HDR
*hdr_delta
, SAM_DELTA_CTR
*delta
,
893 switch(hdr_delta
->type
) {
894 case SAM_DELTA_ACCOUNT_INFO
:
895 fetch_account_info(hdr_delta
->target_rid
,
896 &delta
->account_info
);
898 case SAM_DELTA_GROUP_INFO
:
899 fetch_group_info(hdr_delta
->target_rid
,
902 case SAM_DELTA_GROUP_MEM
:
903 fetch_group_mem_info(hdr_delta
->target_rid
,
904 &delta
->grp_mem_info
);
906 case SAM_DELTA_ALIAS_INFO
:
907 fetch_alias_info(hdr_delta
->target_rid
,
908 &delta
->alias_info
, dom_sid
);
910 case SAM_DELTA_ALIAS_MEM
:
911 fetch_alias_mem(hdr_delta
->target_rid
,
912 &delta
->als_mem_info
, dom_sid
);
914 /* The following types are recognised but not handled */
915 case SAM_DELTA_DOMAIN_INFO
:
916 d_printf("SAM_DELTA_DOMAIN_INFO not handled\n");
918 case SAM_DELTA_RENAME_GROUP
:
919 d_printf("SAM_DELTA_RENAME_GROUP not handled\n");
921 case SAM_DELTA_RENAME_USER
:
922 d_printf("SAM_DELTA_RENAME_USER not handled\n");
924 case SAM_DELTA_RENAME_ALIAS
:
925 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
927 case SAM_DELTA_POLICY_INFO
:
928 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
930 case SAM_DELTA_TRUST_DOMS
:
931 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
933 case SAM_DELTA_PRIVS_INFO
:
934 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
936 case SAM_DELTA_SECRET_INFO
:
937 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
939 case SAM_DELTA_DELETE_GROUP
:
940 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
942 case SAM_DELTA_DELETE_USER
:
943 d_printf("SAM_DELTA_DELETE_USER not handled\n");
945 case SAM_DELTA_MODIFIED_COUNT
:
946 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
949 d_printf("Unknown delta record type %d\n", hdr_delta
->type
);
955 fetch_database(struct cli_state
*cli
, unsigned db_type
, DOM_CRED
*ret_creds
,
958 unsigned sync_context
= 0;
962 SAM_DELTA_HDR
*hdr_deltas
;
963 SAM_DELTA_CTR
*deltas
;
966 if (!(mem_ctx
= talloc_init("fetch_database")))
967 return NT_STATUS_NO_MEMORY
;
970 case SAM_DATABASE_DOMAIN
:
971 d_printf("Fetching DOMAIN database\n");
973 case SAM_DATABASE_BUILTIN
:
974 d_printf("Fetching BUILTIN database\n");
976 case SAM_DATABASE_PRIVS
:
977 d_printf("Fetching PRIVS databases\n");
980 d_printf("Fetching unknown database type %u\n", db_type
);
985 result
= cli_netlogon_sam_sync(cli
, mem_ctx
, ret_creds
,
986 db_type
, sync_context
,
988 &hdr_deltas
, &deltas
);
990 if (NT_STATUS_IS_OK(result
) ||
991 NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
993 clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
),
996 for (i
= 0; i
< num_deltas
; i
++) {
997 fetch_sam_entry(&hdr_deltas
[i
], &deltas
[i
], dom_sid
);
1003 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1005 talloc_destroy(mem_ctx
);
1010 /* dump sam database via samsync rpc calls */
1011 NTSTATUS
rpc_vampire_internals(const DOM_SID
*domain_sid
,
1012 const char *domain_name
,
1013 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1014 int argc
, const char **argv
)
1017 uchar trust_password
[16];
1019 fstring my_dom_sid_str
;
1020 fstring rem_dom_sid_str
;
1023 ZERO_STRUCT(ret_creds
);
1025 if (!sid_equal(domain_sid
, get_global_sam_sid())) {
1026 d_printf("Cannot import users from %s at this time, "
1027 "as the current domain:\n\t%s: %s\nconflicts "
1028 "with the remote domain\n\t%s: %s\n"
1029 "Perhaps you need to set: \n\n\tsecurity=user\n\tworkgroup=%s\n\n in your smb.conf?\n",
1031 get_global_sam_name(), sid_to_string(my_dom_sid_str
,
1032 get_global_sam_sid()),
1033 domain_name
, sid_to_string(rem_dom_sid_str
, domain_sid
),
1035 return NT_STATUS_UNSUCCESSFUL
;
1038 fstrcpy(cli
->domain
, domain_name
);
1040 if (!secrets_fetch_trust_account_password(domain_name
,
1041 trust_password
, NULL
,
1043 result
= NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1044 d_printf("Could not retrieve domain trust secret\n");
1048 result
= cli_nt_establish_netlogon(cli
, sec_channel
, trust_password
);
1050 if (!NT_STATUS_IS_OK(result
)) {
1051 d_printf("Failed to setup BDC creds\n");
1055 result
= fetch_database(cli
, SAM_DATABASE_DOMAIN
, &ret_creds
, *domain_sid
);
1057 if (!NT_STATUS_IS_OK(result
)) {
1058 d_printf("Failed to fetch domain database: %s\n",
1060 if (NT_STATUS_EQUAL(result
, NT_STATUS_NOT_SUPPORTED
))
1061 d_printf("Perhaps %s is a Windows 2000 native mode "
1062 "domain?\n", domain_name
);
1066 result
= fetch_database(cli
, SAM_DATABASE_BUILTIN
, &ret_creds
,
1067 global_sid_Builtin
);
1069 if (!NT_STATUS_IS_OK(result
)) {
1070 d_printf("Failed to fetch builtin database: %s\n",
1075 /* Currently we crash on PRIVS somewhere in unmarshalling */
1076 /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */