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 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
8 Modified by Volker Lendecke 2002
9 Copyright (C) Jeremy Allison 2005.
10 Copyright (C) Guenther Deschner 2008.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "utils/net.h"
29 static void display_group_mem_info(uint32_t rid,
30 struct netr_DELTA_GROUP_MEMBER *r)
33 d_printf("Group mem %u: ", rid);
34 for (i=0; i< r->num_rids; i++) {
35 d_printf("%u ", r->rids[i]);
40 static void display_alias_info(uint32_t rid,
41 struct netr_DELTA_ALIAS *r)
43 d_printf("Alias '%s' ", r->alias_name.string);
44 d_printf("desc='%s' rid=%u\n", r->description.string, r->rid);
47 static void display_alias_mem(uint32_t rid,
48 struct netr_DELTA_ALIAS_MEMBER *r)
51 d_printf("Alias rid %u: ", rid);
52 for (i=0; i< r->sids.num_sids; i++) {
53 d_printf("%s ", sid_string_tos(r->sids.sids[i].sid));
58 static void display_account_info(uint32_t rid,
59 struct netr_DELTA_USER *r)
61 fstring hex_nt_passwd, hex_lm_passwd;
62 uchar lm_passwd[16], nt_passwd[16];
63 static uchar zero_buf[16];
65 /* Decode hashes from password hash (if they are not NULL) */
67 if (memcmp(r->lmpassword.hash, zero_buf, 16) != 0) {
68 sam_pwd_hash(r->rid, r->lmpassword.hash, lm_passwd, 0);
69 pdb_sethexpwd(hex_lm_passwd, lm_passwd, r->acct_flags);
71 pdb_sethexpwd(hex_lm_passwd, NULL, 0);
74 if (memcmp(r->ntpassword.hash, zero_buf, 16) != 0) {
75 sam_pwd_hash(r->rid, r->ntpassword.hash, nt_passwd, 0);
76 pdb_sethexpwd(hex_nt_passwd, nt_passwd, r->acct_flags);
78 pdb_sethexpwd(hex_nt_passwd, NULL, 0);
81 printf("%s:%d:%s:%s:%s:LCT-0\n",
82 r->account_name.string,
83 r->rid, hex_lm_passwd, hex_nt_passwd,
84 pdb_encode_acct_ctrl(r->acct_flags, NEW_PW_FORMAT_SPACE_PADDED_LEN));
87 static time_t uint64s_nt_time_to_unix_abs(const uint64 *src)
91 return nt_time_to_unix_abs(&nttime);
94 static NTSTATUS pull_netr_AcctLockStr(TALLOC_CTX *mem_ctx,
95 struct lsa_BinaryString *r,
96 struct netr_AcctLockStr **str_p)
98 struct netr_AcctLockStr *str;
99 enum ndr_err_code ndr_err;
102 if (!mem_ctx || !r || !str_p) {
103 return NT_STATUS_INVALID_PARAMETER;
108 str = TALLOC_ZERO_P(mem_ctx, struct netr_AcctLockStr);
110 return NT_STATUS_NO_MEMORY;
113 blob = data_blob_const(r->array, r->length);
115 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, str,
116 (ndr_pull_flags_fn_t)ndr_pull_netr_AcctLockStr);
117 data_blob_free(&blob);
119 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
120 return ndr_map_error2ntstatus(ndr_err);
128 static void display_domain_info(struct netr_DELTA_DOMAIN *r)
131 struct netr_AcctLockStr *lockstr = NULL;
133 TALLOC_CTX *mem_ctx = talloc_tos();
135 status = pull_netr_AcctLockStr(mem_ctx, &r->account_lockout,
137 if (!NT_STATUS_IS_OK(status)) {
138 d_printf("failed to pull account lockout string: %s\n",
142 u_logout = uint64s_nt_time_to_unix_abs((const uint64 *)&r->force_logoff_time);
144 d_printf("Domain name: %s\n", r->domain_name.string);
146 d_printf("Minimal Password Length: %d\n", r->min_password_length);
147 d_printf("Password History Length: %d\n", r->password_history_length);
149 d_printf("Force Logoff: %d\n", (int)u_logout);
151 d_printf("Max Password Age: %s\n", display_time(r->max_password_age));
152 d_printf("Min Password Age: %s\n", display_time(r->min_password_age));
155 d_printf("Lockout Time: %s\n", display_time((NTTIME)lockstr->lockout_duration));
156 d_printf("Lockout Reset Time: %s\n", display_time((NTTIME)lockstr->reset_count));
157 d_printf("Bad Attempt Lockout: %d\n", lockstr->bad_attempt_lockout);
160 d_printf("User must logon to change password: %d\n", r->logon_to_chgpass);
163 static void display_group_info(uint32_t rid, struct netr_DELTA_GROUP *r)
165 d_printf("Group '%s' ", r->group_name.string);
166 d_printf("desc='%s', rid=%u\n", r->description.string, rid);
169 static NTSTATUS display_sam_entry(TALLOC_CTX *mem_ctx,
170 enum netr_SamDatabaseID database_id,
171 struct netr_DELTA_ENUM *r,
173 struct samsync_context *ctx)
175 union netr_DELTA_UNION u = r->delta_union;
176 union netr_DELTA_ID_UNION id = r->delta_id_union;
178 switch (r->delta_type) {
179 case NETR_DELTA_DOMAIN:
180 display_domain_info(u.domain);
182 case NETR_DELTA_GROUP:
183 display_group_info(id.rid, u.group);
186 case NETR_DELTA_DELETE_GROUP:
187 printf("Delete Group: %d\n",
188 u.delete_account.unknown);
190 case NETR_DELTA_RENAME_GROUP:
191 printf("Rename Group: %s -> %s\n",
192 u.rename_group->OldName.string,
193 u.rename_group->NewName.string);
196 case NETR_DELTA_USER:
197 display_account_info(id.rid, u.user);
200 case NETR_DELTA_DELETE_USER:
201 printf("Delete User: %d\n",
204 case NETR_DELTA_RENAME_USER:
205 printf("Rename user: %s -> %s\n",
206 u.rename_user->OldName.string,
207 u.rename_user->NewName.string);
210 case NETR_DELTA_GROUP_MEMBER:
211 display_group_mem_info(id.rid, u.group_member);
213 case NETR_DELTA_ALIAS:
214 display_alias_info(id.rid, u.alias);
217 case NETR_DELTA_DELETE_ALIAS:
218 printf("Delete Alias: %d\n",
221 case NETR_DELTA_RENAME_ALIAS:
222 printf("Rename alias: %s -> %s\n",
223 u.rename_alias->OldName.string,
224 u.rename_alias->NewName.string);
227 case NETR_DELTA_ALIAS_MEMBER:
228 display_alias_mem(id.rid, u.alias_member);
231 case NETR_DELTA_POLICY:
234 case NETR_DELTA_TRUSTED_DOMAIN:
235 printf("Trusted Domain: %s\n",
236 u.trusted_domain->domain_name.string);
238 case NETR_DELTA_DELETE_TRUST:
239 printf("Delete Trust: %d\n",
240 u.delete_trust.unknown);
242 case NETR_DELTA_ACCOUNT:
245 case NETR_DELTA_DELETE_ACCOUNT:
246 printf("Delete Account: %d\n",
247 u.delete_account.unknown);
249 case NETR_DELTA_SECRET:
252 case NETR_DELTA_DELETE_SECRET:
253 printf("Delete Secret: %d\n",
254 u.delete_secret.unknown);
256 case NETR_DELTA_DELETE_GROUP2:
257 printf("Delete Group2: %s\n",
258 u.delete_group->account_name);
260 case NETR_DELTA_DELETE_USER2:
261 printf("Delete User2: %s\n",
262 u.delete_user->account_name);
264 case NETR_DELTA_MODIFY_COUNT:
265 printf("sam sequence update: 0x%016llx\n",
266 (unsigned long long) *u.modified_count);
269 /* The following types are recognised but not handled */
270 case NETR_DELTA_RENAME_GROUP:
271 d_printf("NETR_DELTA_RENAME_GROUP not handled\n");
273 case NETR_DELTA_RENAME_USER:
274 d_printf("NETR_DELTA_RENAME_USER not handled\n");
276 case NETR_DELTA_RENAME_ALIAS:
277 d_printf("NETR_DELTA_RENAME_ALIAS not handled\n");
279 case NETR_DELTA_POLICY:
280 d_printf("NETR_DELTA_POLICY not handled\n");
282 case NETR_DELTA_TRUSTED_DOMAIN:
283 d_printf("NETR_DELTA_TRUSTED_DOMAIN not handled\n");
285 case NETR_DELTA_ACCOUNT:
286 d_printf("NETR_DELTA_ACCOUNT not handled\n");
288 case NETR_DELTA_SECRET:
289 d_printf("NETR_DELTA_SECRET not handled\n");
291 case NETR_DELTA_DELETE_GROUP:
292 d_printf("NETR_DELTA_DELETE_GROUP not handled\n");
294 case NETR_DELTA_DELETE_USER:
295 d_printf("NETR_DELTA_DELETE_USER not handled\n");
297 case NETR_DELTA_MODIFY_COUNT:
298 d_printf("NETR_DELTA_MODIFY_COUNT not handled\n");
300 case NETR_DELTA_DELETE_ALIAS:
301 d_printf("NETR_DELTA_DELETE_ALIAS not handled\n");
303 case NETR_DELTA_DELETE_TRUST:
304 d_printf("NETR_DELTA_DELETE_TRUST not handled\n");
306 case NETR_DELTA_DELETE_ACCOUNT:
307 d_printf("NETR_DELTA_DELETE_ACCOUNT not handled\n");
309 case NETR_DELTA_DELETE_SECRET:
310 d_printf("NETR_DELTA_DELETE_SECRET not handled\n");
312 case NETR_DELTA_DELETE_GROUP2:
313 d_printf("NETR_DELTA_DELETE_GROUP2 not handled\n");
315 case NETR_DELTA_DELETE_USER2:
316 d_printf("NETR_DELTA_DELETE_USER2 not handled\n");
319 printf("unknown delta type 0x%02x\n",
327 static NTSTATUS display_sam_entries(TALLOC_CTX *mem_ctx,
328 enum netr_SamDatabaseID database_id,
329 struct netr_DELTA_ENUM_ARRAY *r,
331 struct samsync_context *ctx)
335 for (i = 0; i < r->num_deltas; i++) {
336 display_sam_entry(mem_ctx, database_id, &r->delta_enum[i], status, ctx);
342 /* dump sam database via samsync rpc calls */
343 NTSTATUS rpc_samdump_internals(struct net_context *c,
344 const DOM_SID *domain_sid,
345 const char *domain_name,
346 struct cli_state *cli,
347 struct rpc_pipe_client *pipe_hnd,
352 struct samsync_context *ctx = NULL;
355 status = samsync_init_context(mem_ctx,
357 NET_SAMSYNC_MODE_DUMP,
359 if (!NT_STATUS_IS_OK(status)) {
363 samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
364 display_sam_entries, ctx);
366 samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
367 display_sam_entries, ctx);
369 samsync_process_database(pipe_hnd, SAM_DATABASE_PRIVS,
370 display_sam_entries, ctx);
377 /* Convert a struct samu_DELTA to a struct samu. */
378 #define STRING_CHANGED (old_string && !new_string) ||\
379 (!old_string && new_string) ||\
380 (old_string && new_string && (strcmp(old_string, new_string) != 0))
382 #define STRING_CHANGED_NC(s1,s2) ((s1) && !(s2)) ||\
384 ((s1) && (s2) && (strcmp((s1), (s2)) != 0))
386 static NTSTATUS sam_account_from_delta(struct samu *account,
387 struct netr_DELTA_USER *r)
389 const char *old_string, *new_string;
390 time_t unix_time, stored_time;
391 uchar lm_passwd[16], nt_passwd[16];
392 static uchar zero_buf[16];
394 /* Username, fullname, home dir, dir drive, logon script, acct
395 desc, workstations, profile. */
397 if (r->account_name.string) {
398 old_string = pdb_get_nt_username(account);
399 new_string = r->account_name.string;
401 if (STRING_CHANGED) {
402 pdb_set_nt_username(account, new_string, PDB_CHANGED);
405 /* Unix username is the same - for sanity */
406 old_string = pdb_get_username( account );
407 if (STRING_CHANGED) {
408 pdb_set_username(account, new_string, PDB_CHANGED);
412 if (r->full_name.string) {
413 old_string = pdb_get_fullname(account);
414 new_string = r->full_name.string;
417 pdb_set_fullname(account, new_string, PDB_CHANGED);
420 if (r->home_directory.string) {
421 old_string = pdb_get_homedir(account);
422 new_string = r->home_directory.string;
425 pdb_set_homedir(account, new_string, PDB_CHANGED);
428 if (r->home_drive.string) {
429 old_string = pdb_get_dir_drive(account);
430 new_string = r->home_drive.string;
433 pdb_set_dir_drive(account, new_string, PDB_CHANGED);
436 if (r->logon_script.string) {
437 old_string = pdb_get_logon_script(account);
438 new_string = r->logon_script.string;
441 pdb_set_logon_script(account, new_string, PDB_CHANGED);
444 if (r->description.string) {
445 old_string = pdb_get_acct_desc(account);
446 new_string = r->description.string;
449 pdb_set_acct_desc(account, new_string, PDB_CHANGED);
452 if (r->workstations.string) {
453 old_string = pdb_get_workstations(account);
454 new_string = r->workstations.string;
457 pdb_set_workstations(account, new_string, PDB_CHANGED);
460 if (r->profile_path.string) {
461 old_string = pdb_get_profile_path(account);
462 new_string = r->profile_path.string;
465 pdb_set_profile_path(account, new_string, PDB_CHANGED);
468 if (r->parameters.string) {
471 old_string = pdb_get_munged_dial(account);
472 mung.length = r->parameters.length;
473 mung.data = (uint8 *) r->parameters.string;
474 newstr = (mung.length == 0) ? NULL :
475 base64_encode_data_blob(talloc_tos(), mung);
477 if (STRING_CHANGED_NC(old_string, newstr))
478 pdb_set_munged_dial(account, newstr, PDB_CHANGED);
482 /* User and group sid */
483 if (pdb_get_user_rid(account) != r->rid)
484 pdb_set_user_sid_from_rid(account, r->rid, PDB_CHANGED);
485 if (pdb_get_group_rid(account) != r->primary_gid)
486 pdb_set_group_sid_from_rid(account, r->primary_gid, PDB_CHANGED);
488 /* Logon and password information */
489 if (!nt_time_is_zero(&r->last_logon)) {
490 unix_time = nt_time_to_unix(r->last_logon);
491 stored_time = pdb_get_logon_time(account);
492 if (stored_time != unix_time)
493 pdb_set_logon_time(account, unix_time, PDB_CHANGED);
496 if (!nt_time_is_zero(&r->last_logoff)) {
497 unix_time = nt_time_to_unix(r->last_logoff);
498 stored_time = pdb_get_logoff_time(account);
499 if (stored_time != unix_time)
500 pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
504 if (pdb_get_logon_divs(account) != r->logon_hours.units_per_week)
505 pdb_set_logon_divs(account, r->logon_hours.units_per_week, PDB_CHANGED);
508 /* no idea what to do with this one - gd */
509 /* Max Logon Hours */
510 if (delta->unknown1 != pdb_get_unknown_6(account)) {
511 pdb_set_unknown_6(account, delta->unknown1, PDB_CHANGED);
514 /* Logon Hours Len */
515 if (r->logon_hours.units_per_week/8 != pdb_get_hours_len(account)) {
516 pdb_set_hours_len(account, r->logon_hours.units_per_week/8, PDB_CHANGED);
520 if (r->logon_hours.bits) {
521 char oldstr[44], newstr[44];
522 pdb_sethexhours(oldstr, pdb_get_hours(account));
523 pdb_sethexhours(newstr, r->logon_hours.bits);
524 if (!strequal(oldstr, newstr))
525 pdb_set_hours(account, r->logon_hours.bits, PDB_CHANGED);
528 if (pdb_get_bad_password_count(account) != r->bad_password_count)
529 pdb_set_bad_password_count(account, r->bad_password_count, PDB_CHANGED);
531 if (pdb_get_logon_count(account) != r->logon_count)
532 pdb_set_logon_count(account, r->logon_count, PDB_CHANGED);
534 if (!nt_time_is_zero(&r->last_password_change)) {
535 unix_time = nt_time_to_unix(r->last_password_change);
536 stored_time = pdb_get_pass_last_set_time(account);
537 if (stored_time != unix_time)
538 pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
540 /* no last set time, make it now */
541 pdb_set_pass_last_set_time(account, time(NULL), PDB_CHANGED);
544 if (!nt_time_is_zero(&r->acct_expiry)) {
545 unix_time = nt_time_to_unix(r->acct_expiry);
546 stored_time = pdb_get_kickoff_time(account);
547 if (stored_time != unix_time)
548 pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
551 /* Decode hashes from password hash
552 Note that win2000 may send us all zeros for the hashes if it doesn't
553 think this channel is secure enough - don't set the passwords at all
556 if (memcmp(r->ntpassword.hash, zero_buf, 16) != 0) {
557 sam_pwd_hash(r->rid, r->ntpassword.hash, lm_passwd, 0);
558 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
561 if (memcmp(r->lmpassword.hash, zero_buf, 16) != 0) {
562 sam_pwd_hash(r->rid, r->lmpassword.hash, nt_passwd, 0);
563 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
566 /* TODO: account expiry time */
568 pdb_set_acct_ctrl(account, r->acct_flags, PDB_CHANGED);
570 pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
575 static NTSTATUS fetch_account_info(uint32_t rid,
576 struct netr_DELTA_USER *r)
579 NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL;
581 char *add_script = NULL;
582 struct samu *sam_account=NULL;
587 struct passwd *passwd;
590 fstrcpy(account, r->account_name.string);
591 d_printf("Creating account: %s\n", account);
593 if ( !(sam_account = samu_new( NULL )) ) {
594 return NT_STATUS_NO_MEMORY;
597 if (!(passwd = Get_Pwnam_alloc(sam_account, account))) {
598 /* Create appropriate user */
599 if (r->acct_flags & ACB_NORMAL) {
600 add_script = talloc_strdup(sam_account,
601 lp_adduser_script());
602 } else if ( (r->acct_flags & ACB_WSTRUST) ||
603 (r->acct_flags & ACB_SVRTRUST) ||
604 (r->acct_flags & ACB_DOMTRUST) ) {
605 add_script = talloc_strdup(sam_account,
606 lp_addmachine_script());
608 DEBUG(1, ("Unknown user type: %s\n",
609 pdb_encode_acct_ctrl(r->acct_flags, NEW_PW_FORMAT_SPACE_PADDED_LEN)));
610 nt_ret = NT_STATUS_UNSUCCESSFUL;
614 nt_ret = NT_STATUS_NO_MEMORY;
619 add_script = talloc_all_string_sub(sam_account,
624 nt_ret = NT_STATUS_NO_MEMORY;
627 add_ret = smbrun(add_script,NULL);
628 DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' "
629 "gave %d\n", add_script, add_ret));
631 smb_nscd_flush_user_cache();
635 /* try and find the possible unix account again */
636 if ( !(passwd = Get_Pwnam_alloc(sam_account, account)) ) {
637 d_fprintf(stderr, "Could not create posix account info for '%s'\n", account);
638 nt_ret = NT_STATUS_NO_SUCH_USER;
643 sid_copy(&user_sid, get_global_sam_sid());
644 sid_append_rid(&user_sid, r->rid);
646 DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n",
647 sid_to_fstring(sid_string, &user_sid), account));
648 if (!pdb_getsampwsid(sam_account, &user_sid)) {
649 sam_account_from_delta(sam_account, r);
650 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
651 sid_to_fstring(sid_string, &user_sid),
652 pdb_get_username(sam_account)));
653 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) {
654 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
656 return NT_STATUS_ACCESS_DENIED;
659 sam_account_from_delta(sam_account, r);
660 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
661 sid_to_fstring(sid_string, &user_sid),
662 pdb_get_username(sam_account)));
663 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) {
664 DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
666 TALLOC_FREE(sam_account);
667 return NT_STATUS_ACCESS_DENIED;
671 if (pdb_get_group_sid(sam_account) == NULL) {
672 return NT_STATUS_UNSUCCESSFUL;
675 group_sid = *pdb_get_group_sid(sam_account);
677 if (!pdb_getgrsid(&map, group_sid)) {
678 DEBUG(0, ("Primary group of %s has no mapping!\n",
679 pdb_get_username(sam_account)));
681 if (map.gid != passwd->pw_gid) {
682 if (!(grp = getgrgid(map.gid))) {
683 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
684 (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_tos(&group_sid)));
686 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
692 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
693 pdb_get_username(sam_account)));
697 TALLOC_FREE(sam_account);
701 static NTSTATUS fetch_group_info(uint32_t rid,
702 struct netr_DELTA_GROUP *r)
706 struct group *grp = NULL;
712 fstrcpy(name, r->group_name.string);
713 fstrcpy(comment, r->description.string);
715 /* add the group to the mapping table */
716 sid_copy(&group_sid, get_global_sam_sid());
717 sid_append_rid(&group_sid, rid);
718 sid_to_fstring(sid_string, &group_sid);
720 if (pdb_getgrsid(&map, group_sid)) {
722 grp = getgrgid(map.gid);
729 /* No group found from mapping, find it from its name. */
730 if ((grp = getgrnam(name)) == NULL) {
732 /* No appropriate group found, create one */
734 d_printf("Creating unix group: '%s'\n", name);
736 if (smb_create_group(name, &gid) != 0)
737 return NT_STATUS_ACCESS_DENIED;
739 if ((grp = getgrnam(name)) == NULL)
740 return NT_STATUS_ACCESS_DENIED;
744 map.gid = grp->gr_gid;
746 map.sid_name_use = SID_NAME_DOM_GRP;
747 fstrcpy(map.nt_name, name);
748 if (r->description.string) {
749 fstrcpy(map.comment, comment);
751 fstrcpy(map.comment, "");
755 pdb_add_group_mapping_entry(&map);
757 pdb_update_group_mapping_entry(&map);
762 static NTSTATUS fetch_group_mem_info(uint32_t rid,
763 struct netr_DELTA_GROUP_MEMBER *r)
766 TALLOC_CTX *t = NULL;
767 char **nt_members = NULL;
773 if (r->num_rids == 0) {
777 sid_copy(&group_sid, get_global_sam_sid());
778 sid_append_rid(&group_sid, rid);
780 if (!get_domain_group_from_sid(group_sid, &map)) {
781 DEBUG(0, ("Could not find global group %d\n", rid));
782 return NT_STATUS_NO_SUCH_GROUP;
785 if (!(grp = getgrgid(map.gid))) {
786 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid));
787 return NT_STATUS_NO_SUCH_GROUP;
790 d_printf("Group members of %s: ", grp->gr_name);
792 if (!(t = talloc_init("fetch_group_mem_info"))) {
793 DEBUG(0, ("could not talloc_init\n"));
794 return NT_STATUS_NO_MEMORY;
798 if ((nt_members = TALLOC_ZERO_ARRAY(t, char *, r->num_rids)) == NULL) {
799 DEBUG(0, ("talloc failed\n"));
801 return NT_STATUS_NO_MEMORY;
807 for (i=0; i < r->num_rids; i++) {
808 struct samu *member = NULL;
811 if ( !(member = samu_new(t)) ) {
813 return NT_STATUS_NO_MEMORY;
816 sid_copy(&member_sid, get_global_sam_sid());
817 sid_append_rid(&member_sid, r->rids[i]);
819 if (!pdb_getsampwsid(member, &member_sid)) {
820 DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
821 r->rids[i], sid_string_tos(&member_sid), grp->gr_name));
826 if (pdb_get_group_rid(member) == rid) {
827 d_printf("%s(primary),", pdb_get_username(member));
832 d_printf("%s,", pdb_get_username(member));
833 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
839 unix_members = grp->gr_mem;
841 while (*unix_members) {
842 bool is_nt_member = false;
843 for (i=0; i < r->num_rids; i++) {
844 if (nt_members[i] == NULL) {
845 /* This was a primary group */
849 if (strcmp(*unix_members, nt_members[i]) == 0) {
855 /* We look at a unix group member that is not
856 an nt group member. So, remove it. NT is
858 smb_delete_user_group(grp->gr_name, *unix_members);
863 for (i=0; i < r->num_rids; i++) {
864 bool is_unix_member = false;
866 if (nt_members[i] == NULL) {
867 /* This was the primary group */
871 unix_members = grp->gr_mem;
873 while (*unix_members) {
874 if (strcmp(*unix_members, nt_members[i]) == 0) {
875 is_unix_member = true;
881 if (!is_unix_member) {
882 /* We look at a nt group member that is not a
883 unix group member currently. So, add the nt
885 smb_add_user_group(grp->gr_name, nt_members[i]);
893 static NTSTATUS fetch_alias_info(uint32_t rid,
894 struct netr_DELTA_ALIAS *r,
895 const DOM_SID *dom_sid)
899 struct group *grp = NULL;
905 fstrcpy(name, r->alias_name.string);
906 fstrcpy(comment, r->description.string);
908 /* Find out whether the group is already mapped */
909 sid_copy(&alias_sid, dom_sid);
910 sid_append_rid(&alias_sid, rid);
911 sid_to_fstring(sid_string, &alias_sid);
913 if (pdb_getgrsid(&map, alias_sid)) {
914 grp = getgrgid(map.gid);
921 /* No group found from mapping, find it from its name. */
922 if ((grp = getgrnam(name)) == NULL) {
923 /* No appropriate group found, create one */
924 d_printf("Creating unix group: '%s'\n", name);
925 if (smb_create_group(name, &gid) != 0)
926 return NT_STATUS_ACCESS_DENIED;
927 if ((grp = getgrgid(gid)) == NULL)
928 return NT_STATUS_ACCESS_DENIED;
932 map.gid = grp->gr_gid;
935 if (sid_equal(dom_sid, &global_sid_Builtin))
936 map.sid_name_use = SID_NAME_WKN_GRP;
938 map.sid_name_use = SID_NAME_ALIAS;
940 fstrcpy(map.nt_name, name);
941 fstrcpy(map.comment, comment);
944 pdb_add_group_mapping_entry(&map);
946 pdb_update_group_mapping_entry(&map);
951 static NTSTATUS fetch_alias_mem(uint32_t rid,
952 struct netr_DELTA_ALIAS_MEMBER *r,
953 const DOM_SID *dom_sid)
958 static NTSTATUS fetch_domain_info(uint32_t rid,
959 struct netr_DELTA_DOMAIN *r)
961 time_t u_max_age, u_min_age, u_logout;
962 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
964 struct netr_AcctLockStr *lockstr = NULL;
966 TALLOC_CTX *mem_ctx = talloc_tos();
968 status = pull_netr_AcctLockStr(mem_ctx, &r->account_lockout,
970 if (!NT_STATUS_IS_OK(status)) {
971 d_printf("failed to pull account lockout string: %s\n",
975 u_max_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->max_password_age);
976 u_min_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->min_password_age);
977 u_logout = uint64s_nt_time_to_unix_abs((uint64 *)&r->force_logoff_time);
979 domname = r->domain_name.string;
981 return NT_STATUS_NO_MEMORY;
984 /* we don't handle BUILTIN account policies */
985 if (!strequal(domname, get_global_sam_name())) {
986 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname);
991 if (!pdb_set_account_policy(AP_PASSWORD_HISTORY,
992 r->password_history_length))
995 if (!pdb_set_account_policy(AP_MIN_PASSWORD_LEN,
996 r->min_password_length))
999 if (!pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (uint32)u_max_age))
1002 if (!pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (uint32)u_min_age))
1005 if (!pdb_set_account_policy(AP_TIME_TO_LOGOUT, (uint32)u_logout))
1009 time_t u_lockoutreset, u_lockouttime;
1011 u_lockoutreset = uint64s_nt_time_to_unix_abs(&lockstr->reset_count);
1012 u_lockouttime = uint64s_nt_time_to_unix_abs((uint64_t *)&lockstr->lockout_duration);
1014 if (!pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT,
1015 lockstr->bad_attempt_lockout))
1018 if (!pdb_set_account_policy(AP_RESET_COUNT_TIME, (uint32_t)u_lockoutreset/60))
1021 if (u_lockouttime != -1)
1022 u_lockouttime /= 60;
1024 if (!pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (uint32_t)u_lockouttime))
1028 if (!pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
1029 r->logon_to_chgpass))
1032 return NT_STATUS_OK;
1035 static NTSTATUS fetch_sam_entry(TALLOC_CTX *mem_ctx,
1036 enum netr_SamDatabaseID database_id,
1037 struct netr_DELTA_ENUM *r,
1038 struct samsync_context *ctx)
1040 switch(r->delta_type) {
1041 case NETR_DELTA_USER:
1042 fetch_account_info(r->delta_id_union.rid,
1043 r->delta_union.user);
1045 case NETR_DELTA_GROUP:
1046 fetch_group_info(r->delta_id_union.rid,
1047 r->delta_union.group);
1049 case NETR_DELTA_GROUP_MEMBER:
1050 fetch_group_mem_info(r->delta_id_union.rid,
1051 r->delta_union.group_member);
1053 case NETR_DELTA_ALIAS:
1054 fetch_alias_info(r->delta_id_union.rid,
1055 r->delta_union.alias,
1058 case NETR_DELTA_ALIAS_MEMBER:
1059 fetch_alias_mem(r->delta_id_union.rid,
1060 r->delta_union.alias_member,
1063 case NETR_DELTA_DOMAIN:
1064 fetch_domain_info(r->delta_id_union.rid,
1065 r->delta_union.domain);
1067 /* The following types are recognised but not handled */
1068 case NETR_DELTA_RENAME_GROUP:
1069 d_printf("NETR_DELTA_RENAME_GROUP not handled\n");
1071 case NETR_DELTA_RENAME_USER:
1072 d_printf("NETR_DELTA_RENAME_USER not handled\n");
1074 case NETR_DELTA_RENAME_ALIAS:
1075 d_printf("NETR_DELTA_RENAME_ALIAS not handled\n");
1077 case NETR_DELTA_POLICY:
1078 d_printf("NETR_DELTA_POLICY not handled\n");
1080 case NETR_DELTA_TRUSTED_DOMAIN:
1081 d_printf("NETR_DELTA_TRUSTED_DOMAIN not handled\n");
1083 case NETR_DELTA_ACCOUNT:
1084 d_printf("NETR_DELTA_ACCOUNT not handled\n");
1086 case NETR_DELTA_SECRET:
1087 d_printf("NETR_DELTA_SECRET not handled\n");
1089 case NETR_DELTA_DELETE_GROUP:
1090 d_printf("NETR_DELTA_DELETE_GROUP not handled\n");
1092 case NETR_DELTA_DELETE_USER:
1093 d_printf("NETR_DELTA_DELETE_USER not handled\n");
1095 case NETR_DELTA_MODIFY_COUNT:
1096 d_printf("NETR_DELTA_MODIFY_COUNT not handled\n");
1098 case NETR_DELTA_DELETE_ALIAS:
1099 d_printf("NETR_DELTA_DELETE_ALIAS not handled\n");
1101 case NETR_DELTA_DELETE_TRUST:
1102 d_printf("NETR_DELTA_DELETE_TRUST not handled\n");
1104 case NETR_DELTA_DELETE_ACCOUNT:
1105 d_printf("NETR_DELTA_DELETE_ACCOUNT not handled\n");
1107 case NETR_DELTA_DELETE_SECRET:
1108 d_printf("NETR_DELTA_DELETE_SECRET not handled\n");
1110 case NETR_DELTA_DELETE_GROUP2:
1111 d_printf("NETR_DELTA_DELETE_GROUP2 not handled\n");
1113 case NETR_DELTA_DELETE_USER2:
1114 d_printf("NETR_DELTA_DELETE_USER2 not handled\n");
1117 d_printf("Unknown delta record type %d\n", r->delta_type);
1121 return NT_STATUS_OK;
1124 static NTSTATUS fetch_sam_entries(TALLOC_CTX *mem_ctx,
1125 enum netr_SamDatabaseID database_id,
1126 struct netr_DELTA_ENUM_ARRAY *r,
1128 struct samsync_context *ctx)
1132 for (i = 0; i < r->num_deltas; i++) {
1133 fetch_sam_entry(mem_ctx, database_id, &r->delta_enum[i], ctx);
1136 return NT_STATUS_OK;
1140 * Basic usage function for 'net rpc vampire'
1142 * @param c A net_context structure
1143 * @param argc Standard main() style argc
1144 * @param argc Standard main() style argv. Initial components are already
1148 int rpc_vampire_usage(struct net_context *c, int argc, const char **argv)
1150 d_printf("net rpc vampire [ldif [<ldif-filename>] [options]\n"
1151 "\t to pull accounts from a remote PDC where we are a BDC\n"
1152 "\t\t no args puts accounts in local passdb from smb.conf\n"
1153 "\t\t ldif - put accounts in ldif format (file defaults to "
1156 net_common_flags_usage(c, argc, argv);
1161 /* dump sam database via samsync rpc calls */
1162 NTSTATUS rpc_vampire_internals(struct net_context *c,
1163 const DOM_SID *domain_sid,
1164 const char *domain_name,
1165 struct cli_state *cli,
1166 struct rpc_pipe_client *pipe_hnd,
1167 TALLOC_CTX *mem_ctx,
1172 struct samsync_context *ctx = NULL;
1174 result = samsync_init_context(mem_ctx,
1176 NET_SAMSYNC_MODE_FETCH_PASSDB,
1178 if (!NT_STATUS_IS_OK(result)) {
1182 if (!sid_equal(domain_sid, get_global_sam_sid())) {
1183 d_printf("Cannot import users from %s at this time, "
1184 "as the current domain:\n\t%s: %s\nconflicts "
1185 "with the remote domain\n\t%s: %s\n"
1186 "Perhaps you need to set: \n\n\tsecurity=user\n\t"
1187 "workgroup=%s\n\n in your smb.conf?\n",
1189 get_global_sam_name(),
1190 sid_string_dbg(get_global_sam_sid()),
1192 sid_string_dbg(domain_sid),
1194 return NT_STATUS_UNSUCCESSFUL;
1198 result = samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
1199 fetch_sam_entries, ctx);
1200 if (!NT_STATUS_IS_OK(result)) {
1201 d_fprintf(stderr, "Failed to fetch domain database: %s\n",
1203 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED))
1204 d_fprintf(stderr, "Perhaps %s is a Windows 2000 "
1205 "native mode domain?\n", domain_name);
1210 ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
1211 ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
1212 result = samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
1213 fetch_sam_entries, ctx);
1214 if (!NT_STATUS_IS_OK(result)) {
1215 d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
1226 NTSTATUS rpc_vampire_ldif_internals(struct net_context *c,
1227 const DOM_SID *domain_sid,
1228 const char *domain_name,
1229 struct cli_state *cli,
1230 struct rpc_pipe_client *pipe_hnd,
1231 TALLOC_CTX *mem_ctx,
1236 struct samsync_context *ctx = NULL;
1238 status = samsync_init_context(mem_ctx,
1240 NET_SAMSYNC_MODE_FETCH_LDIF,
1242 if (!NT_STATUS_IS_OK(status)) {
1247 ctx->ldif_filename = argv[1];
1251 status = samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
1252 fetch_sam_entries_ldif, ctx);
1253 if (!NT_STATUS_IS_OK(status)) {
1254 d_fprintf(stderr, "Failed to fetch domain database: %s\n",
1256 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED))
1257 d_fprintf(stderr, "Perhaps %s is a Windows 2000 "
1258 "native mode domain?\n", domain_name);
1263 ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
1264 ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
1265 status = samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
1266 fetch_sam_entries_ldif, ctx);
1267 if (!NT_STATUS_IS_OK(status)) {
1268 d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
1279 int rpc_vampire_ldif(struct net_context *c, int argc, const char **argv)
1281 if (c->display_usage) {
1283 "net rpc vampire ldif\n"
1284 " Dump remote SAM database to LDIF file or stdout\n");
1288 return run_rpc_command(c, NULL, PI_NETLOGON, 0, rpc_vampire_ldif_internals,