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.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "utils/net.h"
29 /* uid's and gid's for writing deltas to ldif */
30 static uint32 ldif_gid = 999;
31 static uint32 ldif_uid = 999;
32 /* Kkeep track of ldap initialization */
33 static int init_ldap = 1;
35 static void display_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *g)
38 d_printf("Group mem %u: ", rid);
39 for (i=0;i<g->num_members;i++) {
40 d_printf("%u ", g->rids[i]);
45 static const char *display_time(NTTIME *nttime)
47 static fstring string;
52 int days, hours, mins, secs;
55 if (nttime->high==0 && nttime->low==0)
58 if (nttime->high==0x80000000 && nttime->low==0)
65 high = high * (~nttime->high);
68 low = low/(1000*1000*10);
74 hours=(sec - (days*60*60*24)) / (60*60);
75 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
76 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
78 fstr_sprintf(string, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs);
83 static void display_alias_info(uint32 rid, SAM_ALIAS_INFO *a)
85 d_printf("Alias '%s' ", unistr2_static(&a->uni_als_name));
86 d_printf("desc='%s' rid=%u\n", unistr2_static(&a->uni_als_desc), a->als_rid);
89 static void display_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *a)
92 d_printf("Alias rid %u: ", rid);
93 for (i=0;i<a->num_members;i++) {
94 d_printf("%s ", sid_string_static(&a->sids[i].sid));
99 static void display_account_info(uint32 rid, SAM_ACCOUNT_INFO *a)
101 fstring hex_nt_passwd, hex_lm_passwd;
102 uchar lm_passwd[16], nt_passwd[16];
103 static uchar zero_buf[16];
105 /* Decode hashes from password hash (if they are not NULL) */
107 if (memcmp(a->pass.buf_lm_pwd, zero_buf, 16) != 0) {
108 sam_pwd_hash(a->user_rid, a->pass.buf_lm_pwd, lm_passwd, 0);
109 pdb_sethexpwd(hex_lm_passwd, lm_passwd, a->acb_info);
111 pdb_sethexpwd(hex_lm_passwd, NULL, 0);
114 if (memcmp(a->pass.buf_nt_pwd, zero_buf, 16) != 0) {
115 sam_pwd_hash(a->user_rid, a->pass.buf_nt_pwd, nt_passwd, 0);
116 pdb_sethexpwd(hex_nt_passwd, nt_passwd, a->acb_info);
118 pdb_sethexpwd(hex_nt_passwd, NULL, 0);
121 printf("%s:%d:%s:%s:%s:LCT-0\n", unistr2_static(&a->uni_acct_name),
122 a->user_rid, hex_lm_passwd, hex_nt_passwd,
123 pdb_encode_acct_ctrl(a->acb_info, NEW_PW_FORMAT_SPACE_PADDED_LEN));
126 static void display_domain_info(SAM_DOMAIN_INFO *a)
130 u_logout = nt_time_to_unix_abs((NTTIME *)&a->force_logoff);
132 d_printf("Domain name: %s\n", unistr2_static(&a->uni_dom_name));
134 d_printf("Minimal Password Length: %d\n", a->min_pwd_len);
135 d_printf("Password History Length: %d\n", a->pwd_history_len);
137 d_printf("Force Logoff: %d\n", (int)u_logout);
139 d_printf("Max Password Age: %s\n", display_time((NTTIME *)&a->max_pwd_age));
140 d_printf("Min Password Age: %s\n", display_time((NTTIME *)&a->min_pwd_age));
142 d_printf("Lockout Time: %s\n", display_time((NTTIME *)&a->account_lockout.lockout_duration));
143 d_printf("Lockout Reset Time: %s\n", display_time((NTTIME *)&a->account_lockout.reset_count));
145 d_printf("Bad Attempt Lockout: %d\n", a->account_lockout.bad_attempt_lockout);
146 d_printf("User must logon to change password: %d\n", a->logon_chgpass);
149 static void display_group_info(uint32 rid, SAM_GROUP_INFO *a)
151 d_printf("Group '%s' ", unistr2_static(&a->uni_grp_name));
152 d_printf("desc='%s', rid=%u\n", unistr2_static(&a->uni_grp_desc), rid);
155 static void display_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta)
157 switch (hdr_delta->type) {
158 case SAM_DELTA_ACCOUNT_INFO:
159 display_account_info(hdr_delta->target_rid, &delta->account_info);
161 case SAM_DELTA_GROUP_MEM:
162 display_group_mem_info(hdr_delta->target_rid, &delta->grp_mem_info);
164 case SAM_DELTA_ALIAS_INFO:
165 display_alias_info(hdr_delta->target_rid, &delta->alias_info);
167 case SAM_DELTA_ALIAS_MEM:
168 display_alias_mem(hdr_delta->target_rid, &delta->als_mem_info);
170 case SAM_DELTA_DOMAIN_INFO:
171 display_domain_info(&delta->domain_info);
173 case SAM_DELTA_GROUP_INFO:
174 display_group_info(hdr_delta->target_rid, &delta->group_info);
176 /* The following types are recognised but not handled */
177 case SAM_DELTA_RENAME_GROUP:
178 d_printf("SAM_DELTA_RENAME_GROUP not handled\n");
180 case SAM_DELTA_RENAME_USER:
181 d_printf("SAM_DELTA_RENAME_USER not handled\n");
183 case SAM_DELTA_RENAME_ALIAS:
184 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
186 case SAM_DELTA_POLICY_INFO:
187 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
189 case SAM_DELTA_TRUST_DOMS:
190 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
192 case SAM_DELTA_PRIVS_INFO:
193 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
195 case SAM_DELTA_SECRET_INFO:
196 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
198 case SAM_DELTA_DELETE_GROUP:
199 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
201 case SAM_DELTA_DELETE_USER:
202 d_printf("SAM_DELTA_DELETE_USER not handled\n");
204 case SAM_DELTA_MODIFIED_COUNT:
205 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
208 d_printf("Unknown delta record type %d\n", hdr_delta->type);
213 static void dump_database(struct rpc_pipe_client *pipe_hnd, uint32 db_type)
215 uint32 sync_context = 0;
219 SAM_DELTA_HDR *hdr_deltas;
220 SAM_DELTA_CTR *deltas;
223 if (!(mem_ctx = talloc_init("dump_database"))) {
228 case SAM_DATABASE_DOMAIN:
229 d_printf("Dumping DOMAIN database\n");
231 case SAM_DATABASE_BUILTIN:
232 d_printf("Dumping BUILTIN database\n");
234 case SAM_DATABASE_PRIVS:
235 d_printf("Dumping PRIVS databases\n");
238 d_printf("Dumping unknown database type %u\n", db_type );
243 result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx, db_type,
245 &num_deltas, &hdr_deltas, &deltas);
246 if (NT_STATUS_IS_ERR(result))
249 for (i = 0; i < num_deltas; i++) {
250 display_sam_entry(&hdr_deltas[i], &deltas[i]);
253 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
255 talloc_destroy(mem_ctx);
258 /* dump sam database via samsync rpc calls */
259 NTSTATUS rpc_samdump_internals(const DOM_SID *domain_sid,
260 const char *domain_name,
261 struct cli_state *cli,
262 struct rpc_pipe_client *pipe_hnd,
268 /* net_rpc.c now always tries to create an schannel pipe.. */
270 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
271 uchar trust_password[16];
272 uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS;
273 uint32 sec_channel_type = 0;
275 if (!secrets_fetch_trust_account_password(domain_name,
277 NULL, &sec_channel_type)) {
278 DEBUG(0,("Could not fetch trust account password\n"));
282 nt_status = rpccli_netlogon_setup_creds(pipe_hnd,
290 if (!NT_STATUS_IS_OK(nt_status)) {
291 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
296 dump_database(pipe_hnd, SAM_DATABASE_DOMAIN);
297 dump_database(pipe_hnd, SAM_DATABASE_BUILTIN);
298 dump_database(pipe_hnd, SAM_DATABASE_PRIVS);
303 /* Convert a struct samu_DELTA to a struct samu. */
304 #define STRING_CHANGED (old_string && !new_string) ||\
305 (!old_string && new_string) ||\
306 (old_string && new_string && (strcmp(old_string, new_string) != 0))
308 static NTSTATUS sam_account_from_delta(struct samu *account, SAM_ACCOUNT_INFO *delta)
310 const char *old_string, *new_string;
311 time_t unix_time, stored_time;
312 uchar lm_passwd[16], nt_passwd[16];
313 static uchar zero_buf[16];
315 /* Username, fullname, home dir, dir drive, logon script, acct
316 desc, workstations, profile. */
318 if (delta->hdr_acct_name.buffer) {
319 old_string = pdb_get_nt_username(account);
320 new_string = unistr2_static(&delta->uni_acct_name);
322 if (STRING_CHANGED) {
323 pdb_set_nt_username(account, new_string, PDB_CHANGED);
327 /* Unix username is the same - for sanity */
328 old_string = pdb_get_username( account );
329 if (STRING_CHANGED) {
330 pdb_set_username(account, new_string, PDB_CHANGED);
334 if (delta->hdr_full_name.buffer) {
335 old_string = pdb_get_fullname(account);
336 new_string = unistr2_static(&delta->uni_full_name);
339 pdb_set_fullname(account, new_string, PDB_CHANGED);
342 if (delta->hdr_home_dir.buffer) {
343 old_string = pdb_get_homedir(account);
344 new_string = unistr2_static(&delta->uni_home_dir);
347 pdb_set_homedir(account, new_string, PDB_CHANGED);
350 if (delta->hdr_dir_drive.buffer) {
351 old_string = pdb_get_dir_drive(account);
352 new_string = unistr2_static(&delta->uni_dir_drive);
355 pdb_set_dir_drive(account, new_string, PDB_CHANGED);
358 if (delta->hdr_logon_script.buffer) {
359 old_string = pdb_get_logon_script(account);
360 new_string = unistr2_static(&delta->uni_logon_script);
363 pdb_set_logon_script(account, new_string, PDB_CHANGED);
366 if (delta->hdr_acct_desc.buffer) {
367 old_string = pdb_get_acct_desc(account);
368 new_string = unistr2_static(&delta->uni_acct_desc);
371 pdb_set_acct_desc(account, new_string, PDB_CHANGED);
374 if (delta->hdr_workstations.buffer) {
375 old_string = pdb_get_workstations(account);
376 new_string = unistr2_static(&delta->uni_workstations);
379 pdb_set_workstations(account, new_string, PDB_CHANGED);
382 if (delta->hdr_profile.buffer) {
383 old_string = pdb_get_profile_path(account);
384 new_string = unistr2_static(&delta->uni_profile);
387 pdb_set_profile_path(account, new_string, PDB_CHANGED);
390 if (delta->hdr_parameters.buffer) {
392 old_string = pdb_get_munged_dial(account);
393 mung.length = delta->hdr_parameters.uni_str_len;
394 mung.data = (uint8 *) delta->uni_parameters.buffer;
395 new_string = (mung.length == 0) ? NULL : base64_encode_data_blob(mung);
398 pdb_set_munged_dial(account, new_string, PDB_CHANGED);
401 /* User and group sid */
402 if (pdb_get_user_rid(account) != delta->user_rid)
403 pdb_set_user_sid_from_rid(account, delta->user_rid, PDB_CHANGED);
404 if (pdb_get_group_rid(account) != delta->group_rid)
405 pdb_set_group_sid_from_rid(account, delta->group_rid, PDB_CHANGED);
407 /* Logon and password information */
408 if (!nt_time_is_zero(&delta->logon_time)) {
409 unix_time = nt_time_to_unix(&delta->logon_time);
410 stored_time = pdb_get_logon_time(account);
411 if (stored_time != unix_time)
412 pdb_set_logon_time(account, unix_time, PDB_CHANGED);
415 if (!nt_time_is_zero(&delta->logoff_time)) {
416 unix_time = nt_time_to_unix(&delta->logoff_time);
417 stored_time = pdb_get_logoff_time(account);
418 if (stored_time != unix_time)
419 pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
423 if (pdb_get_logon_divs(account) != delta->logon_divs)
424 pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED);
426 /* Max Logon Hours */
427 if (delta->unknown1 != pdb_get_unknown_6(account)) {
428 pdb_set_unknown_6(account, delta->unknown1, PDB_CHANGED);
431 /* Logon Hours Len */
432 if (delta->buf_logon_hrs.buf_len != pdb_get_hours_len(account)) {
433 pdb_set_hours_len(account, delta->buf_logon_hrs.buf_len, PDB_CHANGED);
437 if (delta->buf_logon_hrs.buffer) {
438 pstring oldstr, newstr;
439 pdb_sethexhours(oldstr, pdb_get_hours(account));
440 pdb_sethexhours(newstr, delta->buf_logon_hrs.buffer);
441 if (!strequal(oldstr, newstr))
442 pdb_set_hours(account, (const uint8 *)delta->buf_logon_hrs.buffer, PDB_CHANGED);
445 if (pdb_get_bad_password_count(account) != delta->bad_pwd_count)
446 pdb_set_bad_password_count(account, delta->bad_pwd_count, PDB_CHANGED);
448 if (pdb_get_logon_count(account) != delta->logon_count)
449 pdb_set_logon_count(account, delta->logon_count, PDB_CHANGED);
451 if (!nt_time_is_zero(&delta->pwd_last_set_time)) {
452 unix_time = nt_time_to_unix(&delta->pwd_last_set_time);
453 stored_time = pdb_get_pass_last_set_time(account);
454 if (stored_time != unix_time)
455 pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
457 /* no last set time, make it now */
458 pdb_set_pass_last_set_time(account, time(NULL), PDB_CHANGED);
462 /* No kickoff time in the delta? */
463 if (!nt_time_is_zero(&delta->kickoff_time)) {
464 unix_time = nt_time_to_unix(&delta->kickoff_time);
465 stored_time = pdb_get_kickoff_time(account);
466 if (stored_time != unix_time)
467 pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
471 /* Decode hashes from password hash
472 Note that win2000 may send us all zeros for the hashes if it doesn't
473 think this channel is secure enough - don't set the passwords at all
476 if (memcmp(delta->pass.buf_lm_pwd, zero_buf, 16) != 0) {
477 sam_pwd_hash(delta->user_rid, delta->pass.buf_lm_pwd, lm_passwd, 0);
478 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
481 if (memcmp(delta->pass.buf_nt_pwd, zero_buf, 16) != 0) {
482 sam_pwd_hash(delta->user_rid, delta->pass.buf_nt_pwd, nt_passwd, 0);
483 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
486 /* TODO: account expiry time */
488 pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED);
490 pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
495 static NTSTATUS fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta)
497 NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL;
500 struct samu *sam_account=NULL;
505 struct passwd *passwd;
508 fstrcpy(account, unistr2_static(&delta->uni_acct_name));
509 d_printf("Creating account: %s\n", account);
511 if ( !(sam_account = samu_new( NULL )) ) {
512 return NT_STATUS_NO_MEMORY;
515 if (!(passwd = Get_Pwnam(account))) {
516 /* Create appropriate user */
517 if (delta->acb_info & ACB_NORMAL) {
518 pstrcpy(add_script, lp_adduser_script());
519 } else if ( (delta->acb_info & ACB_WSTRUST) ||
520 (delta->acb_info & ACB_SVRTRUST) ||
521 (delta->acb_info & ACB_DOMTRUST) ) {
522 pstrcpy(add_script, lp_addmachine_script());
524 DEBUG(1, ("Unknown user type: %s\n",
525 pdb_encode_acct_ctrl(delta->acb_info, NEW_PW_FORMAT_SPACE_PADDED_LEN)));
526 nt_ret = NT_STATUS_UNSUCCESSFUL;
531 all_string_sub(add_script, "%u", account,
533 add_ret = smbrun(add_script,NULL);
534 DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' "
535 "gave %d\n", add_script, add_ret));
538 /* try and find the possible unix account again */
539 if ( !(passwd = Get_Pwnam(account)) ) {
540 d_fprintf(stderr, "Could not create posix account info for '%s'\n", account);
541 nt_ret = NT_STATUS_NO_SUCH_USER;
546 sid_copy(&user_sid, get_global_sam_sid());
547 sid_append_rid(&user_sid, delta->user_rid);
549 DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n", sid_to_string(sid_string, &user_sid), account));
550 if (!pdb_getsampwsid(sam_account, &user_sid)) {
551 sam_account_from_delta(sam_account, delta);
552 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
553 sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account)));
554 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) {
555 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
557 return NT_STATUS_ACCESS_DENIED;
560 sam_account_from_delta(sam_account, delta);
561 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
562 sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account)));
563 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) {
564 DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
566 TALLOC_FREE(sam_account);
567 return NT_STATUS_ACCESS_DENIED;
571 group_sid = *pdb_get_group_sid(sam_account);
573 if (!pdb_getgrsid(&map, group_sid)) {
574 DEBUG(0, ("Primary group of %s has no mapping!\n",
575 pdb_get_username(sam_account)));
577 if (map.gid != passwd->pw_gid) {
578 if (!(grp = getgrgid(map.gid))) {
579 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
580 (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_static(&group_sid)));
582 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
588 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
589 pdb_get_username(sam_account)));
593 TALLOC_FREE(sam_account);
597 static NTSTATUS fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta)
601 struct group *grp = NULL;
607 unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1);
608 unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1);
610 /* add the group to the mapping table */
611 sid_copy(&group_sid, get_global_sam_sid());
612 sid_append_rid(&group_sid, rid);
613 sid_to_string(sid_string, &group_sid);
615 if (pdb_getgrsid(&map, group_sid)) {
617 grp = getgrgid(map.gid);
624 /* No group found from mapping, find it from its name. */
625 if ((grp = getgrnam(name)) == NULL) {
627 /* No appropriate group found, create one */
629 d_printf("Creating unix group: '%s'\n", name);
631 if (smb_create_group(name, &gid) != 0)
632 return NT_STATUS_ACCESS_DENIED;
634 if ((grp = getgrnam(name)) == NULL)
635 return NT_STATUS_ACCESS_DENIED;
639 map.gid = grp->gr_gid;
641 map.sid_name_use = SID_NAME_DOM_GRP;
642 fstrcpy(map.nt_name, name);
643 if (delta->hdr_grp_desc.buffer) {
644 fstrcpy(map.comment, comment);
646 fstrcpy(map.comment, "");
650 pdb_add_group_mapping_entry(&map);
652 pdb_update_group_mapping_entry(&map);
657 static NTSTATUS fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta)
660 TALLOC_CTX *t = NULL;
661 char **nt_members = NULL;
667 if (delta->num_members == 0) {
671 sid_copy(&group_sid, get_global_sam_sid());
672 sid_append_rid(&group_sid, rid);
674 if (!get_domain_group_from_sid(group_sid, &map)) {
675 DEBUG(0, ("Could not find global group %d\n", rid));
676 return NT_STATUS_NO_SUCH_GROUP;
679 if (!(grp = getgrgid(map.gid))) {
680 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid));
681 return NT_STATUS_NO_SUCH_GROUP;
684 d_printf("Group members of %s: ", grp->gr_name);
686 if (!(t = talloc_init("fetch_group_mem_info"))) {
687 DEBUG(0, ("could not talloc_init\n"));
688 return NT_STATUS_NO_MEMORY;
691 nt_members = TALLOC_ZERO_ARRAY(t, char *, delta->num_members);
693 for (i=0; i<delta->num_members; i++) {
694 struct samu *member = NULL;
697 if ( !(member = samu_new(t)) ) {
699 return NT_STATUS_NO_MEMORY;
702 sid_copy(&member_sid, get_global_sam_sid());
703 sid_append_rid(&member_sid, delta->rids[i]);
705 if (!pdb_getsampwsid(member, &member_sid)) {
706 DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
707 delta->rids[i], sid_string_static(&member_sid), grp->gr_name));
712 if (pdb_get_group_rid(member) == rid) {
713 d_printf("%s(primary),", pdb_get_username(member));
718 d_printf("%s,", pdb_get_username(member));
719 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
725 unix_members = grp->gr_mem;
727 while (*unix_members) {
728 BOOL is_nt_member = False;
729 for (i=0; i<delta->num_members; i++) {
730 if (nt_members[i] == NULL) {
731 /* This was a primary group */
735 if (strcmp(*unix_members, nt_members[i]) == 0) {
741 /* We look at a unix group member that is not
742 an nt group member. So, remove it. NT is
744 smb_delete_user_group(grp->gr_name, *unix_members);
749 for (i=0; i<delta->num_members; i++) {
750 BOOL is_unix_member = False;
752 if (nt_members[i] == NULL) {
753 /* This was the primary group */
757 unix_members = grp->gr_mem;
759 while (*unix_members) {
760 if (strcmp(*unix_members, nt_members[i]) == 0) {
761 is_unix_member = True;
767 if (!is_unix_member) {
768 /* We look at a nt group member that is not a
769 unix group member currently. So, add the nt
771 smb_add_user_group(grp->gr_name, nt_members[i]);
779 static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta,
784 struct group *grp = NULL;
790 unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1);
791 unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1);
793 /* Find out whether the group is already mapped */
794 sid_copy(&alias_sid, &dom_sid);
795 sid_append_rid(&alias_sid, rid);
796 sid_to_string(sid_string, &alias_sid);
798 if (pdb_getgrsid(&map, alias_sid)) {
799 grp = getgrgid(map.gid);
806 /* No group found from mapping, find it from its name. */
807 if ((grp = getgrnam(name)) == NULL) {
808 /* No appropriate group found, create one */
809 d_printf("Creating unix group: '%s'\n", name);
810 if (smb_create_group(name, &gid) != 0)
811 return NT_STATUS_ACCESS_DENIED;
812 if ((grp = getgrgid(gid)) == NULL)
813 return NT_STATUS_ACCESS_DENIED;
817 map.gid = grp->gr_gid;
820 if (sid_equal(&dom_sid, &global_sid_Builtin))
821 map.sid_name_use = SID_NAME_WKN_GRP;
823 map.sid_name_use = SID_NAME_ALIAS;
825 fstrcpy(map.nt_name, name);
826 fstrcpy(map.comment, comment);
829 pdb_add_group_mapping_entry(&map);
831 pdb_update_group_mapping_entry(&map);
836 static NTSTATUS fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid)
841 static NTSTATUS fetch_domain_info(uint32 rid, SAM_DOMAIN_INFO *delta)
843 time_t u_max_age, u_min_age, u_logout, u_lockoutreset, u_lockouttime;
844 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
847 u_max_age = nt_time_to_unix_abs((NTTIME *)&delta->max_pwd_age);
848 u_min_age = nt_time_to_unix_abs((NTTIME *)&delta->min_pwd_age);
849 u_logout = nt_time_to_unix_abs((NTTIME *)&delta->force_logoff);
850 u_lockoutreset = nt_time_to_unix_abs((NTTIME *)&delta->account_lockout.reset_count);
851 u_lockouttime = nt_time_to_unix_abs((NTTIME *)&delta->account_lockout.lockout_duration);
853 unistr2_to_ascii(domname, &delta->uni_dom_name, sizeof(domname) - 1);
855 /* we don't handle BUILTIN account policies */
856 if (!strequal(domname, get_global_sam_name())) {
857 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname);
862 if (!pdb_set_account_policy(AP_PASSWORD_HISTORY, delta->pwd_history_len))
865 if (!pdb_set_account_policy(AP_MIN_PASSWORD_LEN, delta->min_pwd_len))
868 if (!pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (uint32)u_max_age))
871 if (!pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (uint32)u_min_age))
874 if (!pdb_set_account_policy(AP_TIME_TO_LOGOUT, (uint32)u_logout))
877 if (!pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, delta->account_lockout.bad_attempt_lockout))
880 if (!pdb_set_account_policy(AP_RESET_COUNT_TIME, (uint32)u_lockoutreset/60))
883 if (u_lockouttime != -1)
886 if (!pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (uint32)u_lockouttime))
889 if (!pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, delta->logon_chgpass))
896 static void fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta,
899 switch(hdr_delta->type) {
900 case SAM_DELTA_ACCOUNT_INFO:
901 fetch_account_info(hdr_delta->target_rid,
902 &delta->account_info);
904 case SAM_DELTA_GROUP_INFO:
905 fetch_group_info(hdr_delta->target_rid,
908 case SAM_DELTA_GROUP_MEM:
909 fetch_group_mem_info(hdr_delta->target_rid,
910 &delta->grp_mem_info);
912 case SAM_DELTA_ALIAS_INFO:
913 fetch_alias_info(hdr_delta->target_rid,
914 &delta->alias_info, dom_sid);
916 case SAM_DELTA_ALIAS_MEM:
917 fetch_alias_mem(hdr_delta->target_rid,
918 &delta->als_mem_info, dom_sid);
920 case SAM_DELTA_DOMAIN_INFO:
921 fetch_domain_info(hdr_delta->target_rid,
922 &delta->domain_info);
924 /* The following types are recognised but not handled */
925 case SAM_DELTA_RENAME_GROUP:
926 d_printf("SAM_DELTA_RENAME_GROUP not handled\n");
928 case SAM_DELTA_RENAME_USER:
929 d_printf("SAM_DELTA_RENAME_USER not handled\n");
931 case SAM_DELTA_RENAME_ALIAS:
932 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
934 case SAM_DELTA_POLICY_INFO:
935 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
937 case SAM_DELTA_TRUST_DOMS:
938 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
940 case SAM_DELTA_PRIVS_INFO:
941 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
943 case SAM_DELTA_SECRET_INFO:
944 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
946 case SAM_DELTA_DELETE_GROUP:
947 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
949 case SAM_DELTA_DELETE_USER:
950 d_printf("SAM_DELTA_DELETE_USER not handled\n");
952 case SAM_DELTA_MODIFIED_COUNT:
953 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
956 d_printf("Unknown delta record type %d\n", hdr_delta->type);
961 static NTSTATUS fetch_database(struct rpc_pipe_client *pipe_hnd, uint32 db_type, DOM_SID dom_sid)
963 uint32 sync_context = 0;
967 SAM_DELTA_HDR *hdr_deltas;
968 SAM_DELTA_CTR *deltas;
971 if (!(mem_ctx = talloc_init("fetch_database")))
972 return NT_STATUS_NO_MEMORY;
975 case SAM_DATABASE_DOMAIN:
976 d_printf("Fetching DOMAIN database\n");
978 case SAM_DATABASE_BUILTIN:
979 d_printf("Fetching BUILTIN database\n");
981 case SAM_DATABASE_PRIVS:
982 d_printf("Fetching PRIVS databases\n");
985 d_printf("Fetching unknown database type %u\n", db_type );
990 result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx,
991 db_type, sync_context,
993 &hdr_deltas, &deltas);
995 if (NT_STATUS_IS_OK(result) ||
996 NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
997 for (i = 0; i < num_deltas; i++) {
998 fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid);
1004 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1006 talloc_destroy(mem_ctx);
1011 static NTSTATUS populate_ldap_for_ldif(fstring sid, const char *suffix, const char
1012 *builtin_sid, FILE *add_fd)
1014 char *user_suffix, *group_suffix, *machine_suffix, *idmap_suffix;
1015 char *user_attr=NULL, *group_attr=NULL;
1019 /* Get the suffix attribute */
1020 suffix_attr = sstring_sub(suffix, '=', ',');
1021 if (suffix_attr == NULL) {
1022 len = strlen(suffix);
1023 suffix_attr = (char*)SMB_MALLOC(len+1);
1024 memcpy(suffix_attr, suffix, len);
1025 suffix_attr[len] = '\0';
1028 /* Write the base */
1029 fprintf(add_fd, "# %s\n", suffix);
1030 fprintf(add_fd, "dn: %s\n", suffix);
1031 fprintf(add_fd, "objectClass: dcObject\n");
1032 fprintf(add_fd, "objectClass: organization\n");
1033 fprintf(add_fd, "o: %s\n", suffix_attr);
1034 fprintf(add_fd, "dc: %s\n", suffix_attr);
1035 fprintf(add_fd, "\n");
1038 user_suffix = lp_ldap_user_suffix();
1039 /* If it exists and is distinct from other containers,
1040 Write the Users entity */
1041 if (user_suffix && *user_suffix &&
1042 strcmp(user_suffix, suffix)) {
1043 user_attr = sstring_sub(lp_ldap_user_suffix(), '=', ',');
1044 fprintf(add_fd, "# %s\n", user_suffix);
1045 fprintf(add_fd, "dn: %s\n", user_suffix);
1046 fprintf(add_fd, "objectClass: organizationalUnit\n");
1047 fprintf(add_fd, "ou: %s\n", user_attr);
1048 fprintf(add_fd, "\n");
1053 group_suffix = lp_ldap_group_suffix();
1054 /* If it exists and is distinct from other containers,
1055 Write the Groups entity */
1056 if (group_suffix && *group_suffix &&
1057 strcmp(group_suffix, suffix)) {
1058 group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1059 fprintf(add_fd, "# %s\n", group_suffix);
1060 fprintf(add_fd, "dn: %s\n", group_suffix);
1061 fprintf(add_fd, "objectClass: organizationalUnit\n");
1062 fprintf(add_fd, "ou: %s\n", group_attr);
1063 fprintf(add_fd, "\n");
1067 /* If it exists and is distinct from other containers,
1068 Write the Computers entity */
1069 machine_suffix = lp_ldap_machine_suffix();
1070 if (machine_suffix && *machine_suffix &&
1071 strcmp(machine_suffix, user_suffix) &&
1072 strcmp(machine_suffix, suffix)) {
1073 fprintf(add_fd, "# %s\n", lp_ldap_machine_suffix());
1074 fprintf(add_fd, "dn: %s\n", lp_ldap_machine_suffix());
1075 fprintf(add_fd, "objectClass: organizationalUnit\n");
1076 fprintf(add_fd, "ou: %s\n",
1077 sstring_sub(lp_ldap_machine_suffix(), '=', ','));
1078 fprintf(add_fd, "\n");
1082 /* If it exists and is distinct from other containers,
1083 Write the IdMap entity */
1084 idmap_suffix = lp_ldap_idmap_suffix();
1085 if (idmap_suffix && *idmap_suffix &&
1086 strcmp(idmap_suffix, user_suffix) &&
1087 strcmp(idmap_suffix, suffix)) {
1088 fprintf(add_fd, "# %s\n", idmap_suffix);
1089 fprintf(add_fd, "dn: %s\n", idmap_suffix);
1090 fprintf(add_fd, "ObjectClass: organizationalUnit\n");
1091 fprintf(add_fd, "ou: %s\n",
1092 sstring_sub(lp_ldap_idmap_suffix(), '=', ','));
1093 fprintf(add_fd, "\n");
1097 /* Write the root entity */
1098 fprintf(add_fd, "# root, %s, %s\n", user_attr, suffix);
1099 fprintf(add_fd, "dn: uid=root,ou=%s,%s\n", user_attr, suffix);
1100 fprintf(add_fd, "cn: root\n");
1101 fprintf(add_fd, "sn: root\n");
1102 fprintf(add_fd, "objectClass: inetOrgPerson\n");
1103 fprintf(add_fd, "objectClass: sambaSAMAccount\n");
1104 fprintf(add_fd, "objectClass: posixAccount\n");
1105 fprintf(add_fd, "objectClass: shadowAccount\n");
1106 fprintf(add_fd, "gidNumber: 0\n");
1107 fprintf(add_fd, "uid: root\n");
1108 fprintf(add_fd, "uidNumber: 0\n");
1109 fprintf(add_fd, "homeDirectory: /home/root\n");
1110 fprintf(add_fd, "sambaPwdLastSet: 0\n");
1111 fprintf(add_fd, "sambaLogonTime: 0\n");
1112 fprintf(add_fd, "sambaLogoffTime: 2147483647\n");
1113 fprintf(add_fd, "sambaKickoffTime: 2147483647\n");
1114 fprintf(add_fd, "sambaPwdCanChange: 0\n");
1115 fprintf(add_fd, "sambaPwdMustChange: 2147483647\n");
1116 fprintf(add_fd, "sambaHomePath: \\\\PDC-SRV\root\n");
1117 fprintf(add_fd, "sambaHomeDrive: H:\n");
1118 fprintf(add_fd, "sambaProfilePath: \\\\PDC-SRV\\profiles\\root\n");
1119 fprintf(add_fd, "sambaprimaryGroupSID: %s-512\n", sid);
1120 fprintf(add_fd, "sambaLMPassword: XXX\n");
1121 fprintf(add_fd, "sambaNTPassword: XXX\n");
1122 fprintf(add_fd, "sambaAcctFlags: [U\n");
1123 fprintf(add_fd, "sambaSID: %s-500\n", sid);
1124 fprintf(add_fd, "loginShell: /bin/false\n");
1125 fprintf(add_fd, "\n");
1128 /* Write the domain entity */
1129 fprintf(add_fd, "# %s, %s\n", lp_workgroup(), suffix);
1130 fprintf(add_fd, "dn: sambaDomainName=%s,%s\n", lp_workgroup(),
1132 fprintf(add_fd, "objectClass: sambaDomain\n");
1133 fprintf(add_fd, "objectClass: sambaUnixIdPool\n");
1134 fprintf(add_fd, "sambaDomainName: %s\n", lp_workgroup());
1135 fprintf(add_fd, "sambaSID: %s\n", sid);
1136 fprintf(add_fd, "uidNumber: %d\n", ++ldif_uid);
1137 fprintf(add_fd, "gidNumber: %d\n", ++ldif_gid);
1138 fprintf(add_fd, "\n");
1141 /* Write user nobody entity */
1142 fprintf(add_fd, "# nobody, %s, %s\n", user_attr, suffix);
1143 fprintf(add_fd, "dn: uid=nobody,ou=%s,%s\n", user_attr, suffix);
1144 fprintf(add_fd, "cn: nobody\n");
1145 fprintf(add_fd, "sn: nobody\n");
1146 fprintf(add_fd, "objectClass: inetOrgPerson\n");
1147 fprintf(add_fd, "objectClass: sambaSAMAccount\n");
1148 fprintf(add_fd, "objectClass: posixAccount\n");
1149 fprintf(add_fd, "objectClass: shadowAccount\n");
1150 fprintf(add_fd, "gidNumber: 514\n");
1151 fprintf(add_fd, "uid: nobody\n");
1152 fprintf(add_fd, "uidNumber: 999\n");
1153 fprintf(add_fd, "homeDirectory: /dev/null\n");
1154 fprintf(add_fd, "sambaPwdLastSet: 0\n");
1155 fprintf(add_fd, "sambaLogonTime: 0\n");
1156 fprintf(add_fd, "sambaLogoffTime: 2147483647\n");
1157 fprintf(add_fd, "sambaKickoffTime: 2147483647\n");
1158 fprintf(add_fd, "sambaPwdCanChange: 0\n");
1159 fprintf(add_fd, "sambaPwdMustChange: 2147483647\n");
1160 fprintf(add_fd, "sambaHomePath: \\\\PDC-SMD3\\homes\\nobody\n");
1161 fprintf(add_fd, "sambaHomeDrive: H:\n");
1162 fprintf(add_fd, "sambaProfilePath: \\\\PDC-SMB3\\profiles\\nobody\n");
1163 fprintf(add_fd, "sambaprimaryGroupSID: %s-514\n", sid);
1164 fprintf(add_fd, "sambaLMPassword: NOPASSWORDXXXXXXXXXXXXXXXXXXXXX\n");
1165 fprintf(add_fd, "sambaNTPassword: NOPASSWORDXXXXXXXXXXXXXXXXXXXXX\n");
1166 fprintf(add_fd, "sambaAcctFlags: [NU\n");
1167 fprintf(add_fd, "sambaSID: %s-2998\n", sid);
1168 fprintf(add_fd, "loginShell: /bin/false\n");
1169 fprintf(add_fd, "\n");
1172 /* Write the Domain Admins entity */
1173 fprintf(add_fd, "# Domain Admins, %s, %s\n", group_attr,
1175 fprintf(add_fd, "dn: cn=Domain Admins,ou=%s,%s\n", group_attr,
1177 fprintf(add_fd, "objectClass: posixGroup\n");
1178 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1179 fprintf(add_fd, "cn: Domain Admins\n");
1180 fprintf(add_fd, "memberUid: Administrator\n");
1181 fprintf(add_fd, "description: Netbios Domain Administrators\n");
1182 fprintf(add_fd, "gidNumber: 512\n");
1183 fprintf(add_fd, "sambaSID: %s-512\n", sid);
1184 fprintf(add_fd, "sambaGroupType: 2\n");
1185 fprintf(add_fd, "displayName: Domain Admins\n");
1186 fprintf(add_fd, "\n");
1189 /* Write the Domain Users entity */
1190 fprintf(add_fd, "# Domain Users, %s, %s\n", group_attr,
1192 fprintf(add_fd, "dn: cn=Domain Users,ou=%s,%s\n", group_attr,
1194 fprintf(add_fd, "objectClass: posixGroup\n");
1195 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1196 fprintf(add_fd, "cn: Domain Users\n");
1197 fprintf(add_fd, "description: Netbios Domain Users\n");
1198 fprintf(add_fd, "gidNumber: 513\n");
1199 fprintf(add_fd, "sambaSID: %s-513\n", sid);
1200 fprintf(add_fd, "sambaGroupType: 2\n");
1201 fprintf(add_fd, "displayName: Domain Users\n");
1202 fprintf(add_fd, "\n");
1205 /* Write the Domain Guests entity */
1206 fprintf(add_fd, "# Domain Guests, %s, %s\n", group_attr,
1208 fprintf(add_fd, "dn: cn=Domain Guests,ou=%s,%s\n", group_attr,
1210 fprintf(add_fd, "objectClass: posixGroup\n");
1211 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1212 fprintf(add_fd, "cn: Domain Guests\n");
1213 fprintf(add_fd, "description: Netbios Domain Guests\n");
1214 fprintf(add_fd, "gidNumber: 514\n");
1215 fprintf(add_fd, "sambaSID: %s-514\n", sid);
1216 fprintf(add_fd, "sambaGroupType: 2\n");
1217 fprintf(add_fd, "displayName: Domain Guests\n");
1218 fprintf(add_fd, "\n");
1221 /* Write the Domain Computers entity */
1222 fprintf(add_fd, "# Domain Computers, %s, %s\n", group_attr,
1224 fprintf(add_fd, "dn: cn=Domain Computers,ou=%s,%s\n",
1225 group_attr, suffix);
1226 fprintf(add_fd, "objectClass: posixGroup\n");
1227 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1228 fprintf(add_fd, "gidNumber: 515\n");
1229 fprintf(add_fd, "cn: Domain Computers\n");
1230 fprintf(add_fd, "description: Netbios Domain Computers accounts\n");
1231 fprintf(add_fd, "sambaSID: %s-515\n", sid);
1232 fprintf(add_fd, "sambaGroupType: 2\n");
1233 fprintf(add_fd, "displayName: Domain Computers\n");
1234 fprintf(add_fd, "\n");
1237 /* Write the Admininistrators Groups entity */
1238 fprintf(add_fd, "# Administrators, %s, %s\n", group_attr,
1240 fprintf(add_fd, "dn: cn=Administrators,ou=%s,%s\n", group_attr,
1242 fprintf(add_fd, "objectClass: posixGroup\n");
1243 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1244 fprintf(add_fd, "gidNumber: 544\n");
1245 fprintf(add_fd, "cn: Administrators\n");
1246 fprintf(add_fd, "description: Netbios Domain Members can fully administer the computer/sambaDomainName\n");
1247 fprintf(add_fd, "sambaSID: %s-544\n", builtin_sid);
1248 fprintf(add_fd, "sambaGroupType: 5\n");
1249 fprintf(add_fd, "displayName: Administrators\n");
1250 fprintf(add_fd, "\n");
1252 /* Write the Print Operator entity */
1253 fprintf(add_fd, "# Print Operators, %s, %s\n", group_attr,
1255 fprintf(add_fd, "dn: cn=Print Operators,ou=%s,%s\n",
1256 group_attr, suffix);
1257 fprintf(add_fd, "objectClass: posixGroup\n");
1258 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1259 fprintf(add_fd, "gidNumber: 550\n");
1260 fprintf(add_fd, "cn: Print Operators\n");
1261 fprintf(add_fd, "description: Netbios Domain Print Operators\n");
1262 fprintf(add_fd, "sambaSID: %s-550\n", builtin_sid);
1263 fprintf(add_fd, "sambaGroupType: 5\n");
1264 fprintf(add_fd, "displayName: Print Operators\n");
1265 fprintf(add_fd, "\n");
1268 /* Write the Backup Operators entity */
1269 fprintf(add_fd, "# Backup Operators, %s, %s\n", group_attr,
1271 fprintf(add_fd, "dn: cn=Backup Operators,ou=%s,%s\n",
1272 group_attr, suffix);
1273 fprintf(add_fd, "objectClass: posixGroup\n");
1274 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1275 fprintf(add_fd, "gidNumber: 551\n");
1276 fprintf(add_fd, "cn: Backup Operators\n");
1277 fprintf(add_fd, "description: Netbios Domain Members can bypass file security to back up files\n");
1278 fprintf(add_fd, "sambaSID: %s-551\n", builtin_sid);
1279 fprintf(add_fd, "sambaGroupType: 5\n");
1280 fprintf(add_fd, "displayName: Backup Operators\n");
1281 fprintf(add_fd, "\n");
1284 /* Write the Replicators entity */
1285 fprintf(add_fd, "# Replicators, %s, %s\n", group_attr, suffix);
1286 fprintf(add_fd, "dn: cn=Replicators,ou=%s,%s\n", group_attr,
1288 fprintf(add_fd, "objectClass: posixGroup\n");
1289 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1290 fprintf(add_fd, "gidNumber: 552\n");
1291 fprintf(add_fd, "cn: Replicators\n");
1292 fprintf(add_fd, "description: Netbios Domain Supports file replication in a sambaDomainName\n");
1293 fprintf(add_fd, "sambaSID: %s-552\n", builtin_sid);
1294 fprintf(add_fd, "sambaGroupType: 5\n");
1295 fprintf(add_fd, "displayName: Replicators\n");
1296 fprintf(add_fd, "\n");
1299 /* Deallocate memory, and return */
1300 if (suffix_attr != NULL) SAFE_FREE(suffix_attr);
1301 return NT_STATUS_OK;
1304 static NTSTATUS map_populate_groups(GROUPMAP *groupmap, ACCOUNTMAP *accountmap, fstring sid,
1305 const char *suffix, const char *builtin_sid)
1307 char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1309 /* Map the groups created by populate_ldap_for_ldif */
1310 groupmap[0].rid = 512;
1311 groupmap[0].gidNumber = 512;
1312 pstr_sprintf(groupmap[0].sambaSID, "%s-512", sid);
1313 pstr_sprintf(groupmap[0].group_dn, "cn=Domain Admins,ou=%s,%s",
1314 group_attr, suffix);
1315 accountmap[0].rid = 512;
1316 pstr_sprintf(accountmap[0].cn, "%s", "Domain Admins");
1318 groupmap[1].rid = 513;
1319 groupmap[1].gidNumber = 513;
1320 pstr_sprintf(groupmap[1].sambaSID, "%s-513", sid);
1321 pstr_sprintf(groupmap[1].group_dn, "cn=Domain Users,ou=%s,%s",
1322 group_attr, suffix);
1323 accountmap[1].rid = 513;
1324 pstr_sprintf(accountmap[1].cn, "%s", "Domain Users");
1326 groupmap[2].rid = 514;
1327 groupmap[2].gidNumber = 514;
1328 pstr_sprintf(groupmap[2].sambaSID, "%s-514", sid);
1329 pstr_sprintf(groupmap[2].group_dn, "cn=Domain Guests,ou=%s,%s",
1330 group_attr, suffix);
1331 accountmap[2].rid = 514;
1332 pstr_sprintf(accountmap[2].cn, "%s", "Domain Guests");
1334 groupmap[3].rid = 515;
1335 groupmap[3].gidNumber = 515;
1336 pstr_sprintf(groupmap[3].sambaSID, "%s-515", sid);
1337 pstr_sprintf(groupmap[3].group_dn, "cn=Domain Computers,ou=%s,%s",
1338 group_attr, suffix);
1339 accountmap[3].rid = 515;
1340 pstr_sprintf(accountmap[3].cn, "%s", "Domain Computers");
1342 groupmap[4].rid = 544;
1343 groupmap[4].gidNumber = 544;
1344 pstr_sprintf(groupmap[4].sambaSID, "%s-544", builtin_sid);
1345 pstr_sprintf(groupmap[4].group_dn, "cn=Administrators,ou=%s,%s",
1346 group_attr, suffix);
1347 accountmap[4].rid = 515;
1348 pstr_sprintf(accountmap[4].cn, "%s", "Administrators");
1350 groupmap[5].rid = 550;
1351 groupmap[5].gidNumber = 550;
1352 pstr_sprintf(groupmap[5].sambaSID, "%s-550", builtin_sid);
1353 pstr_sprintf(groupmap[5].group_dn, "cn=Print Operators,ou=%s,%s",
1354 group_attr, suffix);
1355 accountmap[5].rid = 550;
1356 pstr_sprintf(accountmap[5].cn, "%s", "Print Operators");
1358 groupmap[6].rid = 551;
1359 groupmap[6].gidNumber = 551;
1360 pstr_sprintf(groupmap[6].sambaSID, "%s-551", builtin_sid);
1361 pstr_sprintf(groupmap[6].group_dn, "cn=Backup Operators,ou=%s,%s",
1362 group_attr, suffix);
1363 accountmap[6].rid = 551;
1364 pstr_sprintf(accountmap[6].cn, "%s", "Backup Operators");
1366 groupmap[7].rid = 552;
1367 groupmap[7].gidNumber = 552;
1368 pstr_sprintf(groupmap[7].sambaSID, "%s-552", builtin_sid);
1369 pstr_sprintf(groupmap[7].group_dn, "cn=Replicators,ou=%s,%s",
1370 group_attr, suffix);
1371 accountmap[7].rid = 551;
1372 pstr_sprintf(accountmap[7].cn, "%s", "Replicators");
1373 return NT_STATUS_OK;
1376 static NTSTATUS fetch_group_info_to_ldif(SAM_DELTA_CTR *delta, GROUPMAP *groupmap,
1377 FILE *add_fd, fstring sid, char *suffix)
1380 uint32 grouptype = 0, g_rid = 0;
1381 char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1383 /* Get the group name */
1384 unistr2_to_ascii(groupname,
1385 &(delta->group_info.uni_grp_name),
1386 sizeof(groupname)-1);
1388 /* Set up the group type (always 2 for group info) */
1391 /* These groups are entered by populate_ldap_for_ldif */
1392 if (strcmp(groupname, "Domain Admins") == 0 ||
1393 strcmp(groupname, "Domain Users") == 0 ||
1394 strcmp(groupname, "Domain Guests") == 0 ||
1395 strcmp(groupname, "Domain Computers") == 0 ||
1396 strcmp(groupname, "Administrators") == 0 ||
1397 strcmp(groupname, "Print Operators") == 0 ||
1398 strcmp(groupname, "Backup Operators") == 0 ||
1399 strcmp(groupname, "Replicators") == 0) {
1400 return NT_STATUS_OK;
1402 /* Increment the gid for the new group */
1406 /* Map the group rid, gid, and dn */
1407 g_rid = delta->group_info.gid.g_rid;
1408 groupmap->rid = g_rid;
1409 groupmap->gidNumber = ldif_gid;
1410 pstr_sprintf(groupmap->sambaSID, "%s-%d", sid, g_rid);
1411 pstr_sprintf(groupmap->group_dn,
1412 "cn=%s,ou=%s,%s", groupname, group_attr, suffix);
1414 /* Write the data to the temporary add ldif file */
1415 fprintf(add_fd, "# %s, %s, %s\n", groupname, group_attr,
1417 fprintf(add_fd, "dn: cn=%s,ou=%s,%s\n", groupname, group_attr,
1419 fprintf(add_fd, "objectClass: posixGroup\n");
1420 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1421 fprintf(add_fd, "cn: %s\n", groupname);
1422 fprintf(add_fd, "gidNumber: %d\n", ldif_gid);
1423 fprintf(add_fd, "sambaSID: %s\n", groupmap->sambaSID);
1424 fprintf(add_fd, "sambaGroupType: %d\n", grouptype);
1425 fprintf(add_fd, "displayName: %s\n", groupname);
1426 fprintf(add_fd, "\n");
1430 return NT_STATUS_OK;
1433 static NTSTATUS fetch_account_info_to_ldif(SAM_DELTA_CTR *delta, GROUPMAP *groupmap,
1434 ACCOUNTMAP *accountmap, FILE *add_fd,
1435 fstring sid, char *suffix, int alloced)
1437 fstring username, logonscript, homedrive, homepath = "", homedir = "";
1438 fstring hex_nt_passwd, hex_lm_passwd;
1439 fstring description, fullname, sambaSID;
1440 uchar lm_passwd[16], nt_passwd[16];
1442 const char* nopasswd = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
1443 static uchar zero_buf[16];
1444 uint32 rid = 0, group_rid = 0, gidNumber = 0;
1448 /* Get the username */
1449 unistr2_to_ascii(username,
1450 &(delta->account_info.uni_acct_name),
1451 sizeof(username)-1);
1454 rid = delta->account_info.user_rid;
1456 /* Map the rid and username for group member info later */
1457 accountmap->rid = rid;
1458 pstr_sprintf(accountmap->cn, "%s", username);
1460 /* Get the home directory */
1461 if (delta->account_info.acb_info & ACB_NORMAL) {
1462 unistr2_to_ascii(homedir, &(delta->account_info.uni_home_dir),
1465 pstr_sprintf(homedir, "/home/%s", username);
1467 pstr_sprintf(homedir, "/dev/null");
1471 /* Get the logon script */
1472 unistr2_to_ascii(logonscript, &(delta->account_info.uni_logon_script),
1473 sizeof(logonscript)-1);
1475 /* Get the home drive */
1476 unistr2_to_ascii(homedrive, &(delta->account_info.uni_dir_drive),
1477 sizeof(homedrive)-1);
1479 /* Get the description */
1480 unistr2_to_ascii(description, &(delta->account_info.uni_acct_desc),
1481 sizeof(description)-1);
1482 if (!*description) {
1483 pstr_sprintf(description, "System User");
1486 /* Get the display name */
1487 unistr2_to_ascii(fullname, &(delta->account_info.uni_full_name),
1488 sizeof(fullname)-1);
1490 /* Get lm and nt password data */
1491 if (memcmp(delta->account_info.pass.buf_lm_pwd, zero_buf, 16) != 0) {
1492 sam_pwd_hash(delta->account_info.user_rid,
1493 delta->account_info.pass.buf_lm_pwd,
1495 pdb_sethexpwd(hex_lm_passwd, lm_passwd,
1496 delta->account_info.acb_info);
1498 pdb_sethexpwd(hex_lm_passwd, NULL, 0);
1500 if (memcmp(delta->account_info.pass.buf_nt_pwd, zero_buf, 16) != 0) {
1501 sam_pwd_hash(delta->account_info.user_rid,
1502 delta->account_info.pass.buf_nt_pwd,
1504 pdb_sethexpwd(hex_nt_passwd, nt_passwd,
1505 delta->account_info.acb_info);
1507 pdb_sethexpwd(hex_nt_passwd, NULL, 0);
1509 unix_time = nt_time_to_unix(&(delta->account_info.pwd_last_set_time));
1511 /* The nobody user is entered by populate_ldap_for_ldif */
1512 if (strcmp(username, "nobody") == 0) {
1513 return NT_STATUS_OK;
1515 /* Increment the uid for the new user */
1519 /* Set up group id and sambaSID for the user */
1520 group_rid = delta->account_info.group_rid;
1521 for (i=0; i<alloced; i++) {
1522 if (groupmap[i].rid == group_rid) break;
1525 DEBUG(1, ("Could not find rid %d in groupmap array\n",
1527 return NT_STATUS_UNSUCCESSFUL;
1529 gidNumber = groupmap[i].gidNumber;
1530 pstr_sprintf(sambaSID, groupmap[i].sambaSID);
1532 /* Set up sambaAcctFlags */
1533 flags = pdb_encode_acct_ctrl(delta->account_info.acb_info,
1534 NEW_PW_FORMAT_SPACE_PADDED_LEN);
1536 /* Add the user to the temporary add ldif file */
1537 fprintf(add_fd, "# %s, %s, %s\n", username,
1538 sstring_sub(lp_ldap_user_suffix(), '=', ','), suffix);
1539 fprintf(add_fd, "dn: uid=%s,ou=%s,%s\n", username,
1540 sstring_sub(lp_ldap_user_suffix(), '=', ','), suffix);
1541 fprintf(add_fd, "ObjectClass: top\n");
1542 fprintf(add_fd, "objectClass: inetOrgPerson\n");
1543 fprintf(add_fd, "objectClass: posixAccount\n");
1544 fprintf(add_fd, "objectClass: shadowAccount\n");
1545 fprintf(add_fd, "objectClass: sambaSamAccount\n");
1546 fprintf(add_fd, "cn: %s\n", username);
1547 fprintf(add_fd, "sn: %s\n", username);
1548 fprintf(add_fd, "uid: %s\n", username);
1549 fprintf(add_fd, "uidNumber: %d\n", ldif_uid);
1550 fprintf(add_fd, "gidNumber: %d\n", gidNumber);
1551 fprintf(add_fd, "homeDirectory: %s\n", homedir);
1553 fprintf(add_fd, "SambaHomePath: %s\n", homepath);
1555 fprintf(add_fd, "SambaHomeDrive: %s\n", homedrive);
1557 fprintf(add_fd, "SambaLogonScript: %s\n", logonscript);
1558 fprintf(add_fd, "loginShell: %s\n",
1559 ((delta->account_info.acb_info & ACB_NORMAL) ?
1560 "/bin/bash" : "/bin/false"));
1561 fprintf(add_fd, "gecos: System User\n");
1562 fprintf(add_fd, "description: %s\n", description);
1563 fprintf(add_fd, "sambaSID: %s-%d\n", sid, rid);
1564 fprintf(add_fd, "sambaPrimaryGroupSID: %s\n", sambaSID);
1566 fprintf(add_fd, "displayName: %s\n", fullname);
1567 if (strcmp(nopasswd, hex_lm_passwd) != 0)
1568 fprintf(add_fd, "sambaLMPassword: %s\n", hex_lm_passwd);
1569 if (strcmp(nopasswd, hex_nt_passwd) != 0)
1570 fprintf(add_fd, "sambaNTPassword: %s\n", hex_nt_passwd);
1571 fprintf(add_fd, "sambaPwdLastSet: %d\n", (int)unix_time);
1572 fprintf(add_fd, "sambaAcctFlags: %s\n", flags);
1573 fprintf(add_fd, "\n");
1577 return NT_STATUS_OK;
1580 static NTSTATUS fetch_alias_info_to_ldif(SAM_DELTA_CTR *delta, GROUPMAP *groupmap,
1581 FILE *add_fd, fstring sid, char *suffix,
1584 fstring aliasname, description;
1585 uint32 grouptype = 0, g_rid = 0;
1586 char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1588 /* Get the alias name */
1589 unistr2_to_ascii(aliasname, &(delta->alias_info.uni_als_name),
1590 sizeof(aliasname)-1);
1592 /* Get the alias description */
1593 unistr2_to_ascii(description, &(delta->alias_info.uni_als_desc),
1594 sizeof(description)-1);
1596 /* Set up the group type */
1598 case SAM_DATABASE_DOMAIN:
1601 case SAM_DATABASE_BUILTIN:
1610 These groups are entered by populate_ldap_for_ldif
1611 Note that populate creates a group called Relicators,
1612 but NT returns a group called Replicator
1614 if (strcmp(aliasname, "Domain Admins") == 0 ||
1615 strcmp(aliasname, "Domain Users") == 0 ||
1616 strcmp(aliasname, "Domain Guests") == 0 ||
1617 strcmp(aliasname, "Domain Computers") == 0 ||
1618 strcmp(aliasname, "Administrators") == 0 ||
1619 strcmp(aliasname, "Print Operators") == 0 ||
1620 strcmp(aliasname, "Backup Operators") == 0 ||
1621 strcmp(aliasname, "Replicator") == 0) {
1622 return NT_STATUS_OK;
1624 /* Increment the gid for the new group */
1628 /* Map the group rid and gid */
1629 g_rid = delta->group_info.gid.g_rid;
1630 groupmap->gidNumber = ldif_gid;
1631 pstr_sprintf(groupmap->sambaSID, "%s-%d", sid, g_rid);
1633 /* Write the data to the temporary add ldif file */
1634 fprintf(add_fd, "# %s, %s, %s\n", aliasname, group_attr,
1636 fprintf(add_fd, "dn: cn=%s,ou=%s,%s\n", aliasname, group_attr,
1638 fprintf(add_fd, "objectClass: posixGroup\n");
1639 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1640 fprintf(add_fd, "cn: %s\n", aliasname);
1641 fprintf(add_fd, "gidNumber: %d\n", ldif_gid);
1642 fprintf(add_fd, "sambaSID: %s\n", groupmap->sambaSID);
1643 fprintf(add_fd, "sambaGroupType: %d\n", grouptype);
1644 fprintf(add_fd, "displayName: %s\n", aliasname);
1645 fprintf(add_fd, "description: %s\n", description);
1646 fprintf(add_fd, "\n");
1650 return NT_STATUS_OK;
1653 static NTSTATUS fetch_groupmem_info_to_ldif(SAM_DELTA_CTR *delta, SAM_DELTA_HDR *hdr_delta,
1654 GROUPMAP *groupmap, ACCOUNTMAP *accountmap,
1655 FILE *mod_fd, int alloced)
1658 uint32 group_rid = 0, rid = 0;
1661 /* Get the dn for the group */
1662 if (delta->grp_mem_info.num_members > 0) {
1663 group_rid = hdr_delta->target_rid;
1664 for (j=0; j<alloced; j++) {
1665 if (groupmap[j].rid == group_rid) break;
1668 DEBUG(1, ("Could not find rid %d in groupmap array\n",
1670 return NT_STATUS_UNSUCCESSFUL;
1672 pstr_sprintf(group_dn, "%s", groupmap[j].group_dn);
1673 fprintf(mod_fd, "dn: %s\n", group_dn);
1675 /* Get the cn for each member */
1676 for (i=0; i<delta->grp_mem_info.num_members; i++) {
1677 rid = delta->grp_mem_info.rids[i];
1678 for (k=0; k<alloced; k++) {
1679 if (accountmap[k].rid == rid) break;
1682 DEBUG(1, ("Could not find rid %d in accountmap array\n", rid));
1683 return NT_STATUS_UNSUCCESSFUL;
1685 fprintf(mod_fd, "memberUid: %s\n", accountmap[k].cn);
1687 fprintf(mod_fd, "\n");
1692 return NT_STATUS_OK;
1695 static NTSTATUS fetch_database_to_ldif(struct rpc_pipe_client *pipe_hnd,
1698 const char *user_file)
1701 const char *builtin_sid = "S-1-5-32";
1703 fstring sid, domainname;
1704 uint32 sync_context = 0;
1707 TALLOC_CTX *mem_ctx;
1708 SAM_DELTA_HDR *hdr_deltas;
1709 SAM_DELTA_CTR *deltas;
1711 const char *add_ldif = "/tmp/add.ldif", *mod_ldif = "/tmp/mod.ldif";
1712 FILE *add_fd, *mod_fd, *ldif_fd;
1714 int num_alloced = 0, g_index = 0, a_index = 0, sys_cmd_result;
1716 /* Set up array for mapping accounts to groups */
1717 /* Array element is the group rid */
1718 GROUPMAP *groupmap = NULL;
1720 /* Set up array for mapping account rid's to cn's */
1721 /* Array element is the account rid */
1722 ACCOUNTMAP *accountmap = NULL;
1724 if (!(mem_ctx = talloc_init("fetch_database"))) {
1725 return NT_STATUS_NO_MEMORY;
1728 /* Ensure we have an output file */
1730 ldif_file = talloc_strdup(mem_ctx, user_file);
1732 ldif_file = talloc_strdup(mem_ctx, "/tmp/tmp.ldif");
1734 if (ldif_file == NULL)
1735 return NT_STATUS_NO_MEMORY;
1737 /* Open the add and mod ldif files */
1738 add_fd = fopen(add_ldif, "a");
1739 mod_fd = fopen(mod_ldif, "a");
1740 if (add_fd == NULL || mod_fd == NULL) {
1741 DEBUG(1, ("Could not open %s\n", add_ldif));
1742 return NT_STATUS_UNSUCCESSFUL;
1745 /* Open the user's ldif file */
1746 ldif_fd = fopen(ldif_file, "a");
1747 if (ldif_fd == NULL) {
1748 DEBUG(1, ("Could not open %s\n", ldif_file));
1749 return NT_STATUS_UNSUCCESSFUL;
1753 sid_to_string(sid, &dom_sid);
1755 /* Get the ldap suffix */
1756 suffix = lp_ldap_suffix();
1757 if (suffix == NULL || strcmp(suffix, "") == 0) {
1758 DEBUG(0,("ldap suffix missing from smb.conf--exiting\n"));
1762 /* Get other smb.conf data */
1763 if (!(lp_workgroup()) || !*(lp_workgroup())) {
1764 DEBUG(0,("workgroup missing from smb.conf--exiting\n"));
1768 /* Allocate initial memory for groupmap and accountmap arrays */
1769 if (init_ldap == 1) {
1770 groupmap = SMB_MALLOC_ARRAY(GROUPMAP, 8);
1771 accountmap = SMB_MALLOC_ARRAY(ACCOUNTMAP, 8);
1772 if (groupmap == NULL || accountmap == NULL) {
1773 DEBUG(1,("GROUPMAP malloc failed\n"));
1774 return NT_STATUS_NO_MEMORY;
1777 /* Initialize the arrays */
1778 memset(groupmap, 0, sizeof(GROUPMAP)*8);
1779 memset(accountmap, 0, sizeof(ACCOUNTMAP)*8);
1781 /* Remember how many we malloced */
1784 /* Initial database population */
1785 populate_ldap_for_ldif(sid, suffix, builtin_sid, add_fd);
1786 map_populate_groups(groupmap, accountmap, sid, suffix,
1789 /* Don't do this again */
1793 /* Announce what we are doing */
1795 case SAM_DATABASE_DOMAIN:
1796 d_printf("Fetching DOMAIN database\n");
1798 case SAM_DATABASE_BUILTIN:
1799 d_printf("Fetching BUILTIN database\n");
1801 case SAM_DATABASE_PRIVS:
1802 d_printf("Fetching PRIVS databases\n");
1805 d_printf("Fetching unknown database type %u\n", db_type );
1810 result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx,
1811 db_type, sync_context,
1812 &num_deltas, &hdr_deltas,
1814 if (!NT_STATUS_IS_OK(result) &&
1815 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1816 return NT_STATUS_OK;
1819 /* Re-allocate memory for groupmap and accountmap arrays */
1820 groupmap = SMB_REALLOC_ARRAY(groupmap, GROUPMAP,
1821 num_deltas+num_alloced);
1822 accountmap = SMB_REALLOC_ARRAY(accountmap, ACCOUNTMAP,
1823 num_deltas+num_alloced);
1824 if (groupmap == NULL || accountmap == NULL) {
1825 DEBUG(1,("GROUPMAP malloc failed\n"));
1826 SAFE_FREE(groupmap);
1827 SAFE_FREE(accountmap);
1828 return NT_STATUS_NO_MEMORY;
1831 /* Initialize the new records */
1832 memset(&groupmap[num_alloced], 0,
1833 sizeof(GROUPMAP)*num_deltas);
1834 memset(&accountmap[num_alloced], 0,
1835 sizeof(ACCOUNTMAP)*num_deltas);
1837 /* Remember how many we alloced this time */
1838 num_alloced += num_deltas;
1840 /* Loop through the deltas */
1841 for (k=0; k<num_deltas; k++) {
1842 switch(hdr_deltas[k].type) {
1843 case SAM_DELTA_DOMAIN_INFO:
1844 /* Is this case needed? */
1845 unistr2_to_ascii(domainname,
1846 &deltas[k].domain_info.uni_dom_name,
1847 sizeof(domainname)-1);
1850 case SAM_DELTA_GROUP_INFO:
1851 fetch_group_info_to_ldif(
1852 &deltas[k], &groupmap[g_index],
1853 add_fd, sid, suffix);
1857 case SAM_DELTA_ACCOUNT_INFO:
1858 fetch_account_info_to_ldif(
1859 &deltas[k], groupmap,
1860 &accountmap[a_index], add_fd,
1861 sid, suffix, num_alloced);
1865 case SAM_DELTA_ALIAS_INFO:
1866 fetch_alias_info_to_ldif(
1867 &deltas[k], &groupmap[g_index],
1868 add_fd, sid, suffix, db_type);
1872 case SAM_DELTA_GROUP_MEM:
1873 fetch_groupmem_info_to_ldif(
1874 &deltas[k], &hdr_deltas[k],
1875 groupmap, accountmap,
1876 mod_fd, num_alloced);
1879 case SAM_DELTA_ALIAS_MEM:
1881 case SAM_DELTA_POLICY_INFO:
1883 case SAM_DELTA_PRIVS_INFO:
1885 case SAM_DELTA_TRUST_DOMS:
1886 /* Implemented but broken */
1888 case SAM_DELTA_SECRET_INFO:
1889 /* Implemented but broken */
1891 case SAM_DELTA_RENAME_GROUP:
1892 /* Not yet implemented */
1894 case SAM_DELTA_RENAME_USER:
1895 /* Not yet implemented */
1897 case SAM_DELTA_RENAME_ALIAS:
1898 /* Not yet implemented */
1900 case SAM_DELTA_DELETE_GROUP:
1901 /* Not yet implemented */
1903 case SAM_DELTA_DELETE_USER:
1904 /* Not yet implemented */
1906 case SAM_DELTA_MODIFIED_COUNT:
1910 } /* end of switch */
1911 } /* end of for loop */
1913 /* Increment sync_context */
1916 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1918 /* Close the ldif files */
1922 /* Write ldif data to the user's file */
1923 if (db_type == SAM_DATABASE_DOMAIN) {
1925 "# SAM_DATABASE_DOMAIN: ADD ENTITIES\n");
1927 "# =================================\n\n");
1929 } else if (db_type == SAM_DATABASE_BUILTIN) {
1931 "# SAM_DATABASE_BUILTIN: ADD ENTITIES\n");
1933 "# ==================================\n\n");
1936 pstr_sprintf(sys_cmd, "cat %s >> %s", add_ldif, ldif_file);
1937 sys_cmd_result = system(sys_cmd);
1938 if (sys_cmd_result) {
1939 d_fprintf(stderr, "%s failed. Error was (%s)\n",
1940 sys_cmd, strerror(errno));
1941 return NT_STATUS_UNSUCCESSFUL;
1943 if (db_type == SAM_DATABASE_DOMAIN) {
1945 "# SAM_DATABASE_DOMAIN: MODIFY ENTITIES\n");
1947 "# ====================================\n\n");
1949 } else if (db_type == SAM_DATABASE_BUILTIN) {
1951 "# SAM_DATABASE_BUILTIN: MODIFY ENTITIES\n");
1953 "# =====================================\n\n");
1956 pstr_sprintf(sys_cmd, "cat %s >> %s", mod_ldif, ldif_file);
1957 sys_cmd_result = system(sys_cmd);
1958 if (sys_cmd_result) {
1959 d_fprintf(stderr, "%s failed. Error was (%s)\n",
1960 sys_cmd, strerror(errno));
1961 return NT_STATUS_UNSUCCESSFUL;
1964 /* Delete the temporary ldif files */
1965 pstr_sprintf(sys_cmd, "rm -f %s %s", add_ldif, mod_ldif);
1966 sys_cmd_result = system(sys_cmd);
1967 if (sys_cmd_result) {
1968 d_fprintf(stderr, "%s failed. Error was (%s)\n",
1969 sys_cmd, strerror(errno));
1970 return NT_STATUS_UNSUCCESSFUL;
1973 /* Close the ldif file */
1976 /* Deallocate memory for the mapping arrays */
1977 SAFE_FREE(groupmap);
1978 SAFE_FREE(accountmap);
1981 talloc_destroy(mem_ctx);
1982 return NT_STATUS_OK;
1986 * Basic usage function for 'net rpc vampire'
1987 * @param argc Standard main() style argc
1988 * @param argc Standard main() style argv. Initial components are already
1992 int rpc_vampire_usage(int argc, const char **argv)
1994 d_printf("net rpc vampire [ldif [<ldif-filename>] [options]\n"\
1995 "\t to pull accounts from a remote PDC where we are a BDC\n"\
1996 "\t\t no args puts accounts in local passdb from smb.conf\n"\
1997 "\t\t ldif - put accounts in ldif format (file defaults to /tmp/tmp.ldif\n");
1999 net_common_flags_usage(argc, argv);
2004 /* dump sam database via samsync rpc calls */
2005 NTSTATUS rpc_vampire_internals(const DOM_SID *domain_sid,
2006 const char *domain_name,
2007 struct cli_state *cli,
2008 struct rpc_pipe_client *pipe_hnd,
2009 TALLOC_CTX *mem_ctx,
2014 fstring my_dom_sid_str;
2015 fstring rem_dom_sid_str;
2017 if (!sid_equal(domain_sid, get_global_sam_sid())) {
2018 d_printf("Cannot import users from %s at this time, "
2019 "as the current domain:\n\t%s: %s\nconflicts "
2020 "with the remote domain\n\t%s: %s\n"
2021 "Perhaps you need to set: \n\n\tsecurity=user\n\tworkgroup=%s\n\n in your smb.conf?\n",
2023 get_global_sam_name(), sid_to_string(my_dom_sid_str,
2024 get_global_sam_sid()),
2025 domain_name, sid_to_string(rem_dom_sid_str, domain_sid),
2027 return NT_STATUS_UNSUCCESSFUL;
2030 if (argc >= 1 && (strcmp(argv[0], "ldif") == 0)) {
2031 result = fetch_database_to_ldif(pipe_hnd, SAM_DATABASE_DOMAIN,
2032 *domain_sid, argv[1]);
2034 result = fetch_database(pipe_hnd, SAM_DATABASE_DOMAIN, *domain_sid);
2037 if (!NT_STATUS_IS_OK(result)) {
2038 d_fprintf(stderr, "Failed to fetch domain database: %s\n",
2040 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED))
2041 d_fprintf(stderr, "Perhaps %s is a Windows 2000 native "
2042 "mode domain?\n", domain_name);
2046 if (argc >= 1 && (strcmp(argv[0], "ldif") == 0)) {
2047 result = fetch_database_to_ldif(pipe_hnd, SAM_DATABASE_BUILTIN,
2048 global_sid_Builtin, argv[1]);
2050 result = fetch_database(pipe_hnd, SAM_DATABASE_BUILTIN, global_sid_Builtin);
2053 if (!NT_STATUS_IS_OK(result)) {
2054 d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
2059 /* Currently we crash on PRIVS somewhere in unmarshalling */
2060 /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */