trying to get HEAD building again. If you want the code
[Samba.git] / source / utils / net_rpc_samsync.c
blobe5e9a68b2eceece3e1752c17a7c913e129d95625
1 /*
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.
24 #include "includes.h"
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)
31 int i;
32 d_printf("Group mem %u: ", rid);
33 for (i=0;i<g->num_members;i++) {
34 d_printf("%u ", g->rids[i]);
36 d_printf("\n");
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)
47 int i;
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));
52 d_printf("\n");
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 smbpasswd_sethexpwd(hex_lm_passwd, lm_passwd, a->acb_info);
66 } else {
67 smbpasswd_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 smbpasswd_sethexpwd(hex_nt_passwd, nt_passwd, a->acb_info);
73 } else {
74 smbpasswd_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 smbpasswd_encode_acb_info(a->acb_info));
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);
98 break;
99 case SAM_DELTA_GROUP_MEM:
100 display_group_mem_info(hdr_delta->target_rid, &delta->grp_mem_info);
101 break;
102 case SAM_DELTA_ALIAS_INFO:
103 display_alias_info(hdr_delta->target_rid, &delta->alias_info);
104 break;
105 case SAM_DELTA_ALIAS_MEM:
106 display_alias_mem(hdr_delta->target_rid, &delta->als_mem_info);
107 break;
108 case SAM_DELTA_DOMAIN_INFO:
109 display_domain_info(&delta->domain_info);
110 break;
111 case SAM_DELTA_GROUP_INFO:
112 display_group_info(hdr_delta->target_rid, &delta->group_info);
113 break;
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");
117 break;
118 case SAM_DELTA_RENAME_USER:
119 d_printf("SAM_DELTA_RENAME_USER not handled\n");
120 break;
121 case SAM_DELTA_RENAME_ALIAS:
122 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
123 break;
124 case SAM_DELTA_POLICY_INFO:
125 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
126 break;
127 case SAM_DELTA_TRUST_DOMS:
128 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
129 break;
130 case SAM_DELTA_PRIVS_INFO:
131 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
132 break;
133 case SAM_DELTA_SECRET_INFO:
134 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
135 break;
136 case SAM_DELTA_DELETE_GROUP:
137 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
138 break;
139 case SAM_DELTA_DELETE_USER:
140 d_printf("SAM_DELTA_DELETE_USER not handled\n");
141 break;
142 case SAM_DELTA_MODIFIED_COUNT:
143 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
144 break;
145 default:
146 d_printf("Unknown delta record type %d\n", hdr_delta->type);
147 break;
152 static void dump_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds)
154 unsigned sync_context = 0;
155 NTSTATUS result;
156 int i;
157 TALLOC_CTX *mem_ctx;
158 SAM_DELTA_HDR *hdr_deltas;
159 SAM_DELTA_CTR *deltas;
160 uint32 num_deltas;
162 if (!(mem_ctx = talloc_init("dump_database"))) {
163 return;
166 switch( db_type ) {
167 case SAM_DATABASE_DOMAIN:
168 d_printf("Dumping DOMAIN database\n");
169 break;
170 case SAM_DATABASE_BUILTIN:
171 d_printf("Dumping BUILTIN database\n");
172 break;
173 case SAM_DATABASE_PRIVS:
174 d_printf("Dumping PRIVS databases\n");
175 break;
176 default:
177 d_printf("Dumping unknown database type %u\n", db_type );
178 break;
181 do {
182 result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds, db_type,
183 sync_context,
184 &num_deltas, &hdr_deltas, &deltas);
185 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), ret_creds);
186 for (i = 0; i < num_deltas; i++) {
187 display_sam_entry(&hdr_deltas[i], &deltas[i]);
189 sync_context += 1;
190 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
192 talloc_destroy(mem_ctx);
195 /* dump sam database via samsync rpc calls */
196 int rpc_samdump(int argc, const char **argv)
198 struct cli_state *cli = NULL;
199 uchar trust_password[16];
200 DOM_CRED ret_creds;
201 uint32 sec_channel;
203 ZERO_STRUCT(ret_creds);
205 /* Connect to remote machine */
206 if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
207 return 1;
210 fstrcpy(cli->domain, lp_workgroup());
212 if (!cli_nt_session_open(cli, PI_NETLOGON)) {
213 DEBUG(0,("Could not open connection to NETLOGON pipe\n"));
214 goto fail;
217 if (!secrets_fetch_trust_account_password(lp_workgroup(),
218 trust_password,
219 NULL, &sec_channel)) {
220 DEBUG(0,("Could not fetch trust account password\n"));
221 goto fail;
224 if (!NT_STATUS_IS_OK(cli_nt_establish_netlogon(cli, sec_channel,
225 trust_password))) {
226 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
227 goto fail;
230 dump_database(cli, SAM_DATABASE_DOMAIN, &ret_creds);
231 dump_database(cli, SAM_DATABASE_BUILTIN, &ret_creds);
232 dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds);
234 cli_nt_session_close(cli);
236 return 0;
238 fail:
239 if (cli) {
240 cli_nt_session_close(cli);
242 return -1;
245 /* Convert a SAM_ACCOUNT_DELTA to a SAM_ACCOUNT. */
246 #define STRING_CHANGED (old_string && !new_string) ||\
247 (!old_string && new_string) ||\
248 (old_string && new_string && (strcmp(old_string, new_string) != 0))
250 static NTSTATUS
251 sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta)
253 const char *old_string, *new_string;
254 time_t unix_time, stored_time;
255 uchar lm_passwd[16], nt_passwd[16];
256 static uchar zero_buf[16];
258 /* Username, fullname, home dir, dir drive, logon script, acct
259 desc, workstations, profile. */
261 if (delta->hdr_acct_name.buffer) {
262 old_string = pdb_get_nt_username(account);
263 new_string = unistr2_static(&delta->uni_acct_name);
265 if (STRING_CHANGED) {
266 pdb_set_nt_username(account, new_string, PDB_CHANGED);
270 /* Unix username is the same - for sanity */
271 old_string = pdb_get_username( account );
272 if (STRING_CHANGED) {
273 pdb_set_username(account, new_string, PDB_CHANGED);
277 if (delta->hdr_full_name.buffer) {
278 old_string = pdb_get_fullname(account);
279 new_string = unistr2_static(&delta->uni_full_name);
281 if (STRING_CHANGED)
282 pdb_set_fullname(account, new_string, PDB_CHANGED);
285 if (delta->hdr_home_dir.buffer) {
286 old_string = pdb_get_homedir(account);
287 new_string = unistr2_static(&delta->uni_home_dir);
289 if (STRING_CHANGED)
290 pdb_set_homedir(account, new_string, PDB_CHANGED);
293 if (delta->hdr_dir_drive.buffer) {
294 old_string = pdb_get_dir_drive(account);
295 new_string = unistr2_static(&delta->uni_dir_drive);
297 if (STRING_CHANGED)
298 pdb_set_dir_drive(account, new_string, PDB_CHANGED);
301 if (delta->hdr_logon_script.buffer) {
302 old_string = pdb_get_logon_script(account);
303 new_string = unistr2_static(&delta->uni_logon_script);
305 if (STRING_CHANGED)
306 pdb_set_logon_script(account, new_string, PDB_CHANGED);
309 if (delta->hdr_acct_desc.buffer) {
310 old_string = pdb_get_acct_desc(account);
311 new_string = unistr2_static(&delta->uni_acct_desc);
313 if (STRING_CHANGED)
314 pdb_set_acct_desc(account, new_string, PDB_CHANGED);
317 if (delta->hdr_workstations.buffer) {
318 old_string = pdb_get_workstations(account);
319 new_string = unistr2_static(&delta->uni_workstations);
321 if (STRING_CHANGED)
322 pdb_set_workstations(account, new_string, PDB_CHANGED);
325 if (delta->hdr_profile.buffer) {
326 old_string = pdb_get_profile_path(account);
327 new_string = unistr2_static(&delta->uni_profile);
329 if (STRING_CHANGED)
330 pdb_set_profile_path(account, new_string, PDB_CHANGED);
333 /* User and group sid */
334 if (pdb_get_user_rid(account) != delta->user_rid)
335 pdb_set_user_sid_from_rid(account, delta->user_rid, PDB_CHANGED);
336 if (pdb_get_group_rid(account) != delta->group_rid)
337 pdb_set_group_sid_from_rid(account, delta->group_rid, PDB_CHANGED);
339 /* Logon and password information */
340 if (!nt_time_is_zero(&delta->logon_time)) {
341 unix_time = nt_time_to_unix(&delta->logon_time);
342 stored_time = pdb_get_logon_time(account);
343 if (stored_time != unix_time)
344 pdb_set_logon_time(account, unix_time, PDB_CHANGED);
347 if (!nt_time_is_zero(&delta->logoff_time)) {
348 unix_time = nt_time_to_unix(&delta->logoff_time);
349 stored_time = pdb_get_logoff_time(account);
350 if (stored_time != unix_time)
351 pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
354 if (pdb_get_logon_divs(account) != delta->logon_divs)
355 pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED);
357 /* TODO: logon hours */
358 /* TODO: bad password count */
359 /* TODO: logon count */
361 if (!nt_time_is_zero(&delta->pwd_last_set_time)) {
362 unix_time = nt_time_to_unix(&delta->pwd_last_set_time);
363 stored_time = pdb_get_pass_last_set_time(account);
364 if (stored_time != unix_time)
365 pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
368 #if 0
369 /* No kickoff time in the delta? */
370 if (!nt_time_is_zero(&delta->kickoff_time)) {
371 unix_time = nt_time_to_unix(&delta->kickoff_time);
372 stored_time = pdb_get_kickoff_time(account);
373 if (stored_time != unix_time)
374 pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
376 #endif
378 /* Decode hashes from password hash
379 Note that win2000 may send us all zeros for the hashes if it doesn't
380 think this channel is secure enough - don't set the passwords at all
381 in that case
383 if (memcmp(delta->pass.buf_lm_pwd, zero_buf, 16) != 0) {
384 sam_pwd_hash(delta->user_rid, delta->pass.buf_lm_pwd, lm_passwd, 0);
385 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
388 if (memcmp(delta->pass.buf_nt_pwd, zero_buf, 16) != 0) {
389 sam_pwd_hash(delta->user_rid, delta->pass.buf_nt_pwd, nt_passwd, 0);
390 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
393 /* TODO: account expiry time */
395 if (pdb_get_acct_ctrl(account) != delta->acb_info)
396 pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED);
398 pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
400 return NT_STATUS_OK;
403 static NTSTATUS
404 fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta)
406 NTSTATUS nt_ret;
407 fstring account;
408 pstring add_script;
409 SAM_ACCOUNT *sam_account=NULL;
410 GROUP_MAP map;
411 struct group *grp;
412 DOM_SID user_sid;
413 DOM_SID group_sid;
414 struct passwd *passwd;
415 fstring sid_string;
417 fstrcpy(account, unistr2_static(&delta->uni_acct_name));
418 d_printf("Creating account: %s\n", account);
420 if (!NT_STATUS_IS_OK(nt_ret = pdb_init_sam(&sam_account)))
421 return nt_ret;
423 if (!(passwd = Get_Pwnam(account))) {
424 /* Create appropriate user */
425 if (delta->acb_info & ACB_NORMAL) {
426 pstrcpy(add_script, lp_adduser_script());
427 } else if ( (delta->acb_info & ACB_WSTRUST) ||
428 (delta->acb_info & ACB_SVRTRUST) ||
429 (delta->acb_info & ACB_DOMTRUST) ) {
430 pstrcpy(add_script, lp_addmachine_script());
431 } else {
432 DEBUG(1, ("Unknown user type: %s\n",
433 smbpasswd_encode_acb_info(delta->acb_info)));
435 if (*add_script) {
436 int add_ret;
437 all_string_sub(add_script, "%u", account,
438 sizeof(account));
439 add_ret = smbrun(add_script,NULL);
440 DEBUG(1,("fetch_account: Running the command `%s' "
441 "gave %d\n", add_script, add_ret));
443 else {
444 DEBUG(8,("fetch_account_info: no add user/machine script. Asking winbindd\n"));
446 /* don't need a RID allocated since the user already has a SID */
447 if ( !winbind_create_user( account, NULL ) )
448 DEBUG(4,("fetch_account_info: winbind_create_user() failed\n"));
451 /* try and find the possible unix account again */
452 if ( !(passwd = Get_Pwnam(account)) )
453 return NT_STATUS_NO_SUCH_USER;
457 sid_copy(&user_sid, get_global_sam_sid());
458 sid_append_rid(&user_sid, delta->user_rid);
460 DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n", sid_to_string(sid_string, &user_sid), account));
461 if (!pdb_getsampwsid(sam_account, &user_sid)) {
462 sam_account_from_delta(sam_account, delta);
463 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
464 sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account)));
465 if (!pdb_add_sam_account(sam_account)) {
466 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
467 account));
468 return NT_STATUS_ACCESS_DENIED;
470 } else {
471 sam_account_from_delta(sam_account, delta);
472 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
473 sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account)));
474 if (!pdb_update_sam_account(sam_account)) {
475 DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
476 account));
477 pdb_free_sam(&sam_account);
478 return NT_STATUS_ACCESS_DENIED;
482 group_sid = *pdb_get_group_sid(sam_account);
484 if (!pdb_getgrsid(&map, group_sid)) {
485 DEBUG(0, ("Primary group of %s has no mapping!\n",
486 pdb_get_username(sam_account)));
487 } else {
488 if (map.gid != passwd->pw_gid) {
489 if (!(grp = getgrgid(map.gid))) {
490 DEBUG(0, ("Could not find unix group %d for user %s (group SID=%s)\n",
491 map.gid, pdb_get_username(sam_account), sid_string_static(&group_sid)));
492 } else {
493 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
498 if ( !passwd ) {
499 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
500 pdb_get_username(sam_account)));
503 pdb_free_sam(&sam_account);
504 return nt_ret;
507 static NTSTATUS
508 fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta)
510 fstring name;
511 fstring comment;
512 struct group *grp = NULL;
513 DOM_SID group_sid;
514 fstring sid_string;
515 GROUP_MAP map;
516 BOOL insert = True;
518 unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1);
519 unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1);
521 /* add the group to the mapping table */
522 sid_copy(&group_sid, get_global_sam_sid());
523 sid_append_rid(&group_sid, rid);
524 sid_to_string(sid_string, &group_sid);
526 if (pdb_getgrsid(&map, group_sid)) {
527 if ( map.gid != -1 )
528 grp = getgrgid(map.gid);
529 insert = False;
532 if (grp == NULL) {
533 gid_t gid;
535 /* No group found from mapping, find it from its name. */
536 if ((grp = getgrnam(name)) == NULL) {
538 /* No appropriate group found, create one */
540 d_printf("Creating unix group: '%s'\n", name);
542 if (smb_create_group(name, &gid) != 0)
543 return NT_STATUS_ACCESS_DENIED;
545 if ((grp = getgrnam(name)) == NULL)
546 return NT_STATUS_ACCESS_DENIED;
550 map.gid = grp->gr_gid;
551 map.sid = group_sid;
552 map.sid_name_use = SID_NAME_DOM_GRP;
553 fstrcpy(map.nt_name, name);
554 fstrcpy(map.comment, comment);
556 if (insert)
557 pdb_add_group_mapping_entry(&map);
558 else
559 pdb_update_group_mapping_entry(&map);
561 return NT_STATUS_OK;
564 static NTSTATUS
565 fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta)
567 int i;
568 TALLOC_CTX *t = NULL;
569 char **nt_members = NULL;
570 char **unix_members;
571 DOM_SID group_sid;
572 GROUP_MAP map;
573 struct group *grp;
575 if (delta->num_members == 0) {
576 return NT_STATUS_OK;
579 sid_copy(&group_sid, get_global_sam_sid());
580 sid_append_rid(&group_sid, rid);
582 if (!get_domain_group_from_sid(group_sid, &map)) {
583 DEBUG(0, ("Could not find global group %d\n", rid));
584 return NT_STATUS_NO_SUCH_GROUP;
587 if (!(grp = getgrgid(map.gid))) {
588 DEBUG(0, ("Could not find unix group %d\n", map.gid));
589 return NT_STATUS_NO_SUCH_GROUP;
592 d_printf("Group members of %s: ", grp->gr_name);
594 if (!(t = talloc_init("fetch_group_mem_info"))) {
595 DEBUG(0, ("could not talloc_init\n"));
596 return NT_STATUS_NO_MEMORY;
599 nt_members = talloc_zero(t, sizeof(char *) * delta->num_members);
601 for (i=0; i<delta->num_members; i++) {
602 NTSTATUS nt_status;
603 SAM_ACCOUNT *member = NULL;
604 DOM_SID member_sid;
606 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) {
607 talloc_destroy(t);
608 return nt_status;
611 sid_copy(&member_sid, get_global_sam_sid());
612 sid_append_rid(&member_sid, delta->rids[i]);
614 if (!pdb_getsampwsid(member, &member_sid)) {
615 DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
616 delta->rids[i], sid_string_static(&member_sid), grp->gr_name));
617 pdb_free_sam(&member);
618 continue;
621 if (pdb_get_group_rid(member) == rid) {
622 d_printf("%s(primary),", pdb_get_username(member));
623 pdb_free_sam(&member);
624 continue;
627 d_printf("%s,", pdb_get_username(member));
628 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
629 pdb_free_sam(&member);
632 d_printf("\n");
634 unix_members = grp->gr_mem;
636 while (*unix_members) {
637 BOOL is_nt_member = False;
638 for (i=0; i<delta->num_members; i++) {
639 if (nt_members[i] == NULL) {
640 /* This was a primary group */
641 continue;
644 if (strcmp(*unix_members, nt_members[i]) == 0) {
645 is_nt_member = True;
646 break;
649 if (!is_nt_member) {
650 /* We look at a unix group member that is not
651 an nt group member. So, remove it. NT is
652 boss here. */
653 smb_delete_user_group(grp->gr_name, *unix_members);
655 unix_members += 1;
658 for (i=0; i<delta->num_members; i++) {
659 BOOL is_unix_member = False;
661 if (nt_members[i] == NULL) {
662 /* This was the primary group */
663 continue;
666 unix_members = grp->gr_mem;
668 while (*unix_members) {
669 if (strcmp(*unix_members, nt_members[i]) == 0) {
670 is_unix_member = True;
671 break;
673 unix_members += 1;
676 if (!is_unix_member) {
677 /* We look at a nt group member that is not a
678 unix group member currently. So, add the nt
679 group member. */
680 smb_add_user_group(grp->gr_name, nt_members[i]);
684 talloc_destroy(t);
685 return NT_STATUS_OK;
688 static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta,
689 DOM_SID dom_sid)
691 fstring name;
692 fstring comment;
693 struct group *grp = NULL;
694 DOM_SID alias_sid;
695 fstring sid_string;
696 GROUP_MAP map;
697 BOOL insert = True;
699 unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1);
700 unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1);
702 /* Find out whether the group is already mapped */
703 sid_copy(&alias_sid, &dom_sid);
704 sid_append_rid(&alias_sid, rid);
705 sid_to_string(sid_string, &alias_sid);
707 if (pdb_getgrsid(&map, alias_sid)) {
708 grp = getgrgid(map.gid);
709 insert = False;
712 if (grp == NULL) {
713 gid_t gid;
715 /* No group found from mapping, find it from its name. */
716 if ((grp = getgrnam(name)) == NULL) {
717 /* No appropriate group found, create one */
718 d_printf("Creating unix group: '%s'\n", name);
719 if (smb_create_group(name, &gid) != 0)
720 return NT_STATUS_ACCESS_DENIED;
721 if ((grp = getgrgid(gid)) == NULL)
722 return NT_STATUS_ACCESS_DENIED;
726 map.gid = grp->gr_gid;
727 map.sid = alias_sid;
729 if (sid_equal(&dom_sid, &global_sid_Builtin))
730 map.sid_name_use = SID_NAME_WKN_GRP;
731 else
732 map.sid_name_use = SID_NAME_ALIAS;
734 fstrcpy(map.nt_name, name);
735 fstrcpy(map.comment, comment);
737 if (insert)
738 pdb_add_group_mapping_entry(&map);
739 else
740 pdb_update_group_mapping_entry(&map);
742 return NT_STATUS_OK;
745 static NTSTATUS
746 fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid)
748 #if 0 /*
749 * commented out right now after talking to Volker. Can't
750 * do much with the membership but seemed a shame to waste
751 * somewhat working code. Needs testing because the membership
752 * that shows up surprises me. Also can't do much with groups
753 * in groups (e.g. Domain Admins being a member of Adminsitrators).
754 * --jerry
757 int i;
758 TALLOC_CTX *t = NULL;
759 char **nt_members = NULL;
760 char **unix_members;
761 DOM_SID group_sid;
762 GROUP_MAP map;
763 struct group *grp;
764 enum SID_NAME_USE sid_type;
766 if (delta->num_members == 0) {
767 return NT_STATUS_OK;
770 sid_copy(&group_sid, &dom_sid);
771 sid_append_rid(&group_sid, rid);
773 if (sid_equal(&dom_sid, &global_sid_Builtin)) {
774 sid_type = SID_NAME_WKN_GRP;
775 if (!get_builtin_group_from_sid(group_sid, &map, False)) {
776 DEBUG(0, ("Could not find builtin group %s\n", sid_string_static(&group_sid)));
777 return NT_STATUS_NO_SUCH_GROUP;
779 } else {
780 sid_type = SID_NAME_ALIAS;
781 if (!get_local_group_from_sid(group_sid, &map, False)) {
782 DEBUG(0, ("Could not find local group %s\n", sid_string_static(&group_sid)));
783 return NT_STATUS_NO_SUCH_GROUP;
787 if (!(grp = getgrgid(map.gid))) {
788 DEBUG(0, ("Could not find unix group %d\n", map.gid));
789 return NT_STATUS_NO_SUCH_GROUP;
792 d_printf("Group members of %s: ", grp->gr_name);
794 if (!(t = talloc_init("fetch_group_mem_info"))) {
795 DEBUG(0, ("could not talloc_init\n"));
796 return NT_STATUS_NO_MEMORY;
799 nt_members = talloc_zero(t, sizeof(char *) * delta->num_members);
801 for (i=0; i<delta->num_members; i++) {
802 NTSTATUS nt_status;
803 SAM_ACCOUNT *member = NULL;
804 DOM_SID member_sid;
806 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) {
807 talloc_destroy(t);
808 return nt_status;
811 sid_copy(&member_sid, &delta->sids[i].sid);
813 if (!pdb_getsampwsid(member, &member_sid)) {
814 DEBUG(1, ("Found bogus group member: (member_sid=%s group=%s)\n",
815 sid_string_static(&member_sid), grp->gr_name));
816 pdb_free_sam(&member);
817 continue;
820 if (pdb_get_group_rid(member) == rid) {
821 d_printf("%s(primary),", pdb_get_username(member));
822 pdb_free_sam(&member);
823 continue;
826 d_printf("%s,", pdb_get_username(member));
827 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
828 pdb_free_sam(&member);
831 d_printf("\n");
833 unix_members = grp->gr_mem;
835 while (*unix_members) {
836 BOOL is_nt_member = False;
837 for (i=0; i<delta->num_members; i++) {
838 if (nt_members[i] == NULL) {
839 /* This was a primary group */
840 continue;
843 if (strcmp(*unix_members, nt_members[i]) == 0) {
844 is_nt_member = True;
845 break;
848 if (!is_nt_member) {
849 /* We look at a unix group member that is not
850 an nt group member. So, remove it. NT is
851 boss here. */
852 smb_delete_user_group(grp->gr_name, *unix_members);
854 unix_members += 1;
857 for (i=0; i<delta->num_members; i++) {
858 BOOL is_unix_member = False;
860 if (nt_members[i] == NULL) {
861 /* This was the primary group */
862 continue;
865 unix_members = grp->gr_mem;
867 while (*unix_members) {
868 if (strcmp(*unix_members, nt_members[i]) == 0) {
869 is_unix_member = True;
870 break;
872 unix_members += 1;
875 if (!is_unix_member) {
876 /* We look at a nt group member that is not a
877 unix group member currently. So, add the nt
878 group member. */
879 smb_add_user_group(grp->gr_name, nt_members[i]);
883 talloc_destroy(t);
885 #endif /* end of fetch_alias_mem() */
887 return NT_STATUS_OK;
890 static void
891 fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta,
892 DOM_SID dom_sid)
894 switch(hdr_delta->type) {
895 case SAM_DELTA_ACCOUNT_INFO:
896 fetch_account_info(hdr_delta->target_rid,
897 &delta->account_info);
898 break;
899 case SAM_DELTA_GROUP_INFO:
900 fetch_group_info(hdr_delta->target_rid,
901 &delta->group_info);
902 break;
903 case SAM_DELTA_GROUP_MEM:
904 fetch_group_mem_info(hdr_delta->target_rid,
905 &delta->grp_mem_info);
906 break;
907 case SAM_DELTA_ALIAS_INFO:
908 fetch_alias_info(hdr_delta->target_rid,
909 &delta->alias_info, dom_sid);
910 break;
911 case SAM_DELTA_ALIAS_MEM:
912 fetch_alias_mem(hdr_delta->target_rid,
913 &delta->als_mem_info, dom_sid);
914 break;
915 case SAM_DELTA_DOMAIN_INFO:
916 d_printf("SAM_DELTA_DOMAIN_INFO not handled\n");
917 break;
918 default:
919 d_printf("Unknown delta record type %d\n", hdr_delta->type);
920 break;
924 static NTSTATUS
925 fetch_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds,
926 DOM_SID dom_sid)
928 unsigned sync_context = 0;
929 NTSTATUS result;
930 int i;
931 TALLOC_CTX *mem_ctx;
932 SAM_DELTA_HDR *hdr_deltas;
933 SAM_DELTA_CTR *deltas;
934 uint32 num_deltas;
936 if (!(mem_ctx = talloc_init("fetch_database")))
937 return NT_STATUS_NO_MEMORY;
939 switch( db_type ) {
940 case SAM_DATABASE_DOMAIN:
941 d_printf("Fetching DOMAIN database\n");
942 break;
943 case SAM_DATABASE_BUILTIN:
944 d_printf("Fetching BUILTIN database\n");
945 break;
946 case SAM_DATABASE_PRIVS:
947 d_printf("Fetching PRIVS databases\n");
948 break;
949 default:
950 d_printf("Fetching unknown database type %u\n", db_type );
951 break;
954 do {
955 result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds,
956 db_type, sync_context,
957 &num_deltas,
958 &hdr_deltas, &deltas);
960 if (NT_STATUS_IS_OK(result) ||
961 NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
963 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred),
964 ret_creds);
966 for (i = 0; i < num_deltas; i++) {
967 fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid);
969 } else
970 return result;
972 sync_context += 1;
973 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
975 talloc_destroy(mem_ctx);
977 return result;
980 /* dump sam database via samsync rpc calls */
981 int rpc_vampire(int argc, const char **argv)
983 NTSTATUS result;
984 struct cli_state *cli = NULL;
985 uchar trust_password[16];
986 DOM_CRED ret_creds;
987 DOM_SID dom_sid;
988 uint32 sec_channel;
990 ZERO_STRUCT(ret_creds);
992 /* Connect to remote machine */
993 if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS |
994 NET_FLAGS_PDC))) {
995 return 1;
998 if (!cli_nt_session_open(cli, PI_NETLOGON)) {
999 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
1000 goto fail;
1003 if (!secrets_fetch_trust_account_password(lp_workgroup(),
1004 trust_password, NULL,
1005 &sec_channel)) {
1006 d_printf("Could not retrieve domain trust secret\n");
1007 goto fail;
1010 result = cli_nt_establish_netlogon(cli, sec_channel, trust_password);
1012 if (!NT_STATUS_IS_OK(result)) {
1013 d_printf("Failed to setup BDC creds\n");
1014 goto fail;
1017 sid_copy( &dom_sid, get_global_sam_sid() );
1018 result = fetch_database(cli, SAM_DATABASE_DOMAIN, &ret_creds, dom_sid);
1020 if (!NT_STATUS_IS_OK(result)) {
1021 d_printf("Failed to fetch domain database: %s\n",
1022 nt_errstr(result));
1023 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED))
1024 d_printf("Perhaps %s is a Windows 2000 native mode "
1025 "domain?\n", lp_workgroup());
1026 goto fail;
1029 sid_copy(&dom_sid, &global_sid_Builtin);
1031 result = fetch_database(cli, SAM_DATABASE_BUILTIN, &ret_creds,
1032 dom_sid);
1034 if (!NT_STATUS_IS_OK(result)) {
1035 d_printf("Failed to fetch builtin database: %s\n",
1036 nt_errstr(result));
1037 goto fail;
1040 /* Currently we crash on PRIVS somewhere in unmarshalling */
1041 /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */
1043 cli_nt_session_close(cli);
1045 return 0;
1047 fail:
1048 if (cli)
1049 cli_nt_session_close(cli);
1051 return -1;