r1874: add dcerpc_errstr() function to display a fault code by name,
[abartlet/samba.git/.git] / source4 / utils / net_rpc_samsync.c
1 /* 
2    Unix SMB/CIFS implementation.
3    dump the remote SAM using rpc samsync operations
4
5    Copyright (C) Andrew Tridgell 2002
6    Copyright (C) Tim Potter 2001,2002
7    Modified by Volker Lendecke 2002
8
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "../utils/net.h"
26
27 extern DOM_SID global_sid_Builtin; 
28
29 static void display_group_mem_info(uint32_t rid, SAM_GROUP_MEM_INFO *g)
30 {
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]);
35         }
36         d_printf("\n");
37 }
38
39 static void display_alias_info(uint32_t rid, SAM_ALIAS_INFO *a)
40 {
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);
43 }
44
45 static void display_alias_mem(uint32_t rid, SAM_ALIAS_MEM_INFO *a)
46 {
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));
51         }
52         d_printf("\n");
53 }
54
55 static void display_account_info(uint32_t rid, SAM_ACCOUNT_INFO *a)
56 {
57         fstring hex_nt_passwd, hex_lm_passwd;
58         uint8_t lm_passwd[16], nt_passwd[16];
59         static uint8_t zero_buf[16];
60
61         /* Decode hashes from password hash (if they are not NULL) */
62         
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);
68         }
69
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);
75         }
76         
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));
80 }
81
82 static void display_domain_info(SAM_DOMAIN_INFO *a)
83 {
84         d_printf("Domain name: %s\n", unistr2_static(&a->uni_dom_name));
85 }
86
87 static void display_group_info(uint32_t rid, SAM_GROUP_INFO *a)
88 {
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);
91 }
92
93 static void display_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta)
94 {
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         default:
115                 d_printf("Unknown delta record type %d\n", hdr_delta->type);
116                 break;
117         }
118 }
119
120
121 static void dump_database(struct smbcli_state *cli, uint_t db_type, DOM_CRED *ret_creds)
122 {
123         uint_t sync_context = 0;
124         NTSTATUS result;
125         int i;
126         TALLOC_CTX *mem_ctx;
127         SAM_DELTA_HDR *hdr_deltas;
128         SAM_DELTA_CTR *deltas;
129         uint32_t num_deltas;
130
131         if (!(mem_ctx = talloc_init("dump_database"))) {
132                 return;
133         }
134
135         d_printf("Dumping database %u\n", db_type);
136
137         do {
138                 result = smbcli_netlogon_sam_sync(cli, mem_ctx, ret_creds, db_type,
139                                                sync_context,
140                                                &num_deltas, &hdr_deltas, &deltas);
141                 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), ret_creds);
142                 for (i = 0; i < num_deltas; i++) {
143                         display_sam_entry(&hdr_deltas[i], &deltas[i]);
144                 }
145                 sync_context += 1;
146         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
147
148         talloc_destroy(mem_ctx);
149 }
150
151 /* dump sam database via samsync rpc calls */
152 int rpc_samdump(int argc, const char **argv)
153 {
154         NTSTATUS result;
155         struct smbcli_state *cli = NULL;
156         uint8_t trust_password[16];
157         DOM_CRED ret_creds;
158         uint32_t neg_flags = 0x000001ff;
159
160
161         ZERO_STRUCT(ret_creds);
162
163         /* Connect to remote machine */
164         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
165                 return 1;
166         }
167
168         if (!smbcli_nt_session_open(cli, PI_NETLOGON)) {
169                 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
170                 goto fail;
171         }
172
173         if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_password, NULL)) {
174                 d_printf("Could not retrieve domain trust secret\n");
175                 goto fail;
176         }
177         
178         result = smbcli_nt_setup_creds(cli, SEC_CHAN_BDC,  trust_password, &neg_flags, 2);
179         if (!NT_STATUS_IS_OK(result)) {
180                 d_printf("Failed to setup BDC creds\n");
181                 goto fail;
182         }
183
184         dump_database(cli, SAM_DATABASE_DOMAIN, &ret_creds);
185         dump_database(cli, SAM_DATABASE_BUILTIN, &ret_creds);
186         dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds);
187
188         smbcli_nt_session_close(cli);
189         
190         return 0;
191
192 fail:
193         if (cli) {
194                 smbcli_nt_session_close(cli);
195         }
196         return -1;
197 }
198
199 /* Convert a SAM_ACCOUNT_DELTA to a SAM_ACCOUNT. */
200
201 static NTSTATUS
202 sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta)
203 {
204         fstring s;
205         uint8_t lm_passwd[16], nt_passwd[16];
206         static uint8_t zero_buf[16];
207
208         /* Username, fullname, home dir, dir drive, logon script, acct
209            desc, workstations, profile. */
210
211         unistr2_to_ascii(s, &delta->uni_acct_name, sizeof(s) - 1);
212         pdb_set_nt_username(account, s, PDB_CHANGED);
213
214         /* Unix username is the same - for sainity */
215         pdb_set_username(account, s, PDB_CHANGED);
216
217         unistr2_to_ascii(s, &delta->uni_full_name, sizeof(s) - 1);
218         pdb_set_fullname(account, s, PDB_CHANGED);
219
220         unistr2_to_ascii(s, &delta->uni_home_dir, sizeof(s) - 1);
221         pdb_set_homedir(account, s, PDB_CHANGED);
222
223         unistr2_to_ascii(s, &delta->uni_dir_drive, sizeof(s) - 1);
224         pdb_set_dir_drive(account, s, PDB_CHANGED);
225
226         unistr2_to_ascii(s, &delta->uni_logon_script, sizeof(s) - 1);
227         pdb_set_logon_script(account, s, PDB_CHANGED);
228
229         unistr2_to_ascii(s, &delta->uni_acct_desc, sizeof(s) - 1);
230         pdb_set_acct_desc(account, s, PDB_CHANGED);
231
232         unistr2_to_ascii(s, &delta->uni_workstations, sizeof(s) - 1);
233         pdb_set_workstations(account, s, PDB_CHANGED);
234
235         unistr2_to_ascii(s, &delta->uni_profile, sizeof(s) - 1);
236         pdb_set_profile_path(account, s, PDB_CHANGED);
237
238         /* User and group sid */
239
240         pdb_set_user_sid_from_rid(account, delta->user_rid, PDB_CHANGED);
241         pdb_set_group_sid_from_rid(account, delta->group_rid, PDB_CHANGED);
242
243         /* Logon and password information */
244
245         pdb_set_logon_time(account, nt_time_to_unix(&delta->logon_time), PDB_CHANGED);
246         pdb_set_logoff_time(account, nt_time_to_unix(&delta->logoff_time),
247                             PDB_CHANGED);
248         pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED);
249
250         /* TODO: logon hours */
251         /* TODO: bad password count */
252         /* TODO: logon count */
253
254         pdb_set_pass_last_set_time(
255                 account, nt_time_to_unix(&delta->pwd_last_set_time), PDB_CHANGED);
256
257         pdb_set_kickoff_time(account, get_time_t_max(), PDB_CHANGED);
258
259         /* Decode hashes from password hash 
260            Note that win2000 may send us all zeros for the hashes if it doesn't 
261            think this channel is secure enough - don't set the passwords at all
262            in that case
263          */
264         if (memcmp(delta->pass.buf_lm_pwd, zero_buf, 16) != 0) {
265                 sam_pwd_hash(delta->user_rid, delta->pass.buf_lm_pwd, lm_passwd, 0);
266                 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
267         }
268
269         if (memcmp(delta->pass.buf_nt_pwd, zero_buf, 16) != 0) {
270                 sam_pwd_hash(delta->user_rid, delta->pass.buf_nt_pwd, nt_passwd, 0);
271                 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
272         }
273
274         /* TODO: account expiry time */
275
276         pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED);
277         return NT_STATUS_OK;
278 }
279
280 static NTSTATUS
281 fetch_account_info(uint32_t rid, SAM_ACCOUNT_INFO *delta)
282 {
283         NTSTATUS nt_ret;
284         fstring account;
285         pstring add_script;
286         SAM_ACCOUNT *sam_account=NULL;
287         GROUP_MAP map;
288         struct group *grp;
289         DOM_SID sid;
290         BOOL try_add = False;
291
292         fstrcpy(account, unistr2_static(&delta->uni_acct_name));
293         d_printf("Creating account: %s\n", account);
294
295         if (!NT_STATUS_IS_OK(nt_ret = pdb_init_sam(&sam_account)))
296                 return nt_ret;
297
298         if (!pdb_getsampwnam(sam_account, account)) {
299                 /* Create appropriate user */
300                 if (delta->acb_info & ACB_NORMAL) {
301                         pstrcpy(add_script, lp_adduser_script());
302                 } else if ( (delta->acb_info & ACB_WSTRUST) ||
303                             (delta->acb_info & ACB_SVRTRUST) ) {
304                         pstrcpy(add_script, lp_addmachine_script());
305                 } else {
306                         DEBUG(1, ("Unknown user type: %s\n",
307                                   smbpasswd_encode_acb_info(delta->acb_info)));
308                         pdb_free_sam(&sam_account);
309                         return NT_STATUS_NO_SUCH_USER;
310                 }
311                 if (*add_script) {
312                         int add_ret;
313                         all_string_sub(add_script, "%u", account,
314                                        sizeof(account));
315                         add_ret = smbrun(add_script,NULL);
316                         DEBUG(1,("fetch_account: Running the command `%s' "
317                                  "gave %d\n", add_script, add_ret));
318                 }
319
320                 try_add = True;
321         }
322
323         sam_account_from_delta(sam_account, delta);
324
325         if (try_add) { 
326                 if (!pdb_add_sam_account(sam_account)) {
327                         DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
328                                   account));
329                 }
330         } else {
331                 if (!pdb_update_sam_account(sam_account)) {
332                         DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
333                                   account));
334                 }
335         }
336
337         sid = *pdb_get_group_sid(sam_account);
338
339         if (!pdb_getgrsid(&map, sid, False)) {
340                 DEBUG(0, ("Primary group of %s has no mapping!\n",
341                           pdb_get_username(sam_account)));
342                 pdb_free_sam(&sam_account);
343                 return NT_STATUS_NO_SUCH_GROUP;
344         }
345
346         if (!(grp = getgrgid(map.gid))) {
347                 DEBUG(0, ("Could not find unix group %d for user %s (group SID=%s)\n", 
348                           map.gid, pdb_get_username(sam_account), sid_string_static(&sid)));
349                 pdb_free_sam(&sam_account);
350                 return NT_STATUS_NO_SUCH_GROUP;
351         }
352
353         smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
354
355         pdb_free_sam(&sam_account);
356         return NT_STATUS_OK;
357 }
358
359 static NTSTATUS
360 fetch_group_info(uint32_t rid, SAM_GROUP_INFO *delta)
361 {
362         fstring name;
363         fstring comment;
364         struct group *grp = NULL;
365         DOM_SID group_sid;
366         fstring sid_string;
367         GROUP_MAP map;
368         BOOL insert = True;
369
370         unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1);
371         unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1);
372
373         /* add the group to the mapping table */
374         sid_copy(&group_sid, get_global_sam_sid());
375         sid_append_rid(&group_sid, rid);
376         sid_to_string(sid_string, &group_sid);
377
378         if (pdb_getgrsid(&map, group_sid, False)) {
379                 grp = getgrgid(map.gid);
380                 insert = False;
381         }
382
383         if (grp == NULL)
384         {
385                 gid_t gid;
386
387                 /* No group found from mapping, find it from its name. */
388                 if ((grp = getgrnam(name)) == NULL) {
389                                 /* No appropriate group found, create one */
390                         d_printf("Creating unix group: '%s'\n", name);
391                         if (smb_create_group(name, &gid) != 0)
392                                 return NT_STATUS_ACCESS_DENIED;
393                         if ((grp = getgrgid(gid)) == NULL)
394                                 return NT_STATUS_ACCESS_DENIED;
395                 }
396         }
397
398         map.gid = grp->gr_gid;
399         map.sid = group_sid;
400         map.sid_name_use = SID_NAME_DOM_GRP;
401         fstrcpy(map.nt_name, name);
402         fstrcpy(map.comment, comment);
403
404         map.priv_set.count = 0;
405         map.priv_set.set = NULL;
406
407         if (insert)
408                 pdb_add_group_mapping_entry(&map);
409         else
410                 pdb_update_group_mapping_entry(&map);
411
412         return NT_STATUS_OK;
413 }
414
415 static NTSTATUS
416 fetch_group_mem_info(uint32_t rid, SAM_GROUP_MEM_INFO *delta)
417 {
418         int i;
419         TALLOC_CTX *t = NULL;
420         char **nt_members = NULL;
421         char **unix_members;
422         DOM_SID group_sid;
423         GROUP_MAP map;
424         struct group *grp;
425
426         if (delta->num_members == 0) {
427                 return NT_STATUS_OK;
428         }
429
430         sid_copy(&group_sid, get_global_sam_sid());
431         sid_append_rid(&group_sid, rid);
432
433         if (!get_domain_group_from_sid(group_sid, &map, False)) {
434                 DEBUG(0, ("Could not find global group %d\n", rid));
435                 return NT_STATUS_NO_SUCH_GROUP;
436         }
437
438         if (!(grp = getgrgid(map.gid))) {
439                 DEBUG(0, ("Could not find unix group %d\n", map.gid));
440                 return NT_STATUS_NO_SUCH_GROUP;
441         }
442
443         d_printf("Group members of %s: ", grp->gr_name);
444
445         if (!(t = talloc_init("fetch_group_mem_info"))) {
446                 DEBUG(0, ("could not talloc_init\n"));
447                 return NT_STATUS_NO_MEMORY;
448         }
449
450         nt_members = talloc_zero(t, sizeof(char *) * delta->num_members);
451
452         for (i=0; i<delta->num_members; i++) {
453                 NTSTATUS nt_status;
454                 SAM_ACCOUNT *member = NULL;
455                 DOM_SID member_sid;
456
457                 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) {
458                         talloc_destroy(t);
459                         return nt_status;
460                 }
461
462                 sid_copy(&member_sid, get_global_sam_sid());
463                 sid_append_rid(&member_sid, delta->rids[i]);
464
465                 if (!pdb_getsampwsid(member, &member_sid)) {
466                         DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
467                                   delta->rids[i], sid_string_static(&member_sid), grp->gr_name));
468                         pdb_free_sam(&member);
469                         continue;
470                 }
471
472                 if (pdb_get_group_rid(member) == rid) {
473                         d_printf("%s(primary),", pdb_get_username(member));
474                         pdb_free_sam(&member);
475                         continue;
476                 }
477                 
478                 d_printf("%s,", pdb_get_username(member));
479                 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
480                 pdb_free_sam(&member);
481         }
482
483         d_printf("\n");
484
485         unix_members = grp->gr_mem;
486
487         while (*unix_members) {
488                 BOOL is_nt_member = False;
489                 for (i=0; i<delta->num_members; i++) {
490                         if (nt_members[i] == NULL) {
491                                 /* This was a primary group */
492                                 continue;
493                         }
494
495                         if (strcmp(*unix_members, nt_members[i]) == 0) {
496                                 is_nt_member = True;
497                                 break;
498                         }
499                 }
500                 if (!is_nt_member) {
501                         /* We look at a unix group member that is not
502                            an nt group member. So, remove it. NT is
503                            boss here. */
504                         smb_delete_user_group(grp->gr_name, *unix_members);
505                 }
506                 unix_members += 1;
507         }
508
509         for (i=0; i<delta->num_members; i++) {
510                 BOOL is_unix_member = False;
511
512                 if (nt_members[i] == NULL) {
513                         /* This was the primary group */
514                         continue;
515                 }
516
517                 unix_members = grp->gr_mem;
518
519                 while (*unix_members) {
520                         if (strcmp(*unix_members, nt_members[i]) == 0) {
521                                 is_unix_member = True;
522                                 break;
523                         }
524                         unix_members += 1;
525                 }
526
527                 if (!is_unix_member) {
528                         /* We look at a nt group member that is not a
529                            unix group member currently. So, add the nt
530                            group member. */
531                         smb_add_user_group(grp->gr_name, nt_members[i]);
532                 }
533         }
534         
535         talloc_destroy(t);
536         return NT_STATUS_OK;
537 }
538
539 static NTSTATUS fetch_alias_info(uint32_t rid, SAM_ALIAS_INFO *delta,
540                                  DOM_SID dom_sid)
541 {
542         fstring name;
543         fstring comment;
544         struct group *grp = NULL;
545         DOM_SID alias_sid;
546         fstring sid_string;
547         GROUP_MAP map;
548         BOOL insert = True;
549
550         unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1);
551         unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1);
552
553         /* Find out whether the group is already mapped */
554         sid_copy(&alias_sid, &dom_sid);
555         sid_append_rid(&alias_sid, rid);
556         sid_to_string(sid_string, &alias_sid);
557
558         if (pdb_getgrsid(&map, alias_sid, False)) {
559                 grp = getgrgid(map.gid);
560                 insert = False;
561         }
562
563         if (grp == NULL) {
564                 gid_t gid;
565
566                 /* No group found from mapping, find it from its name. */
567                 if ((grp = getgrnam(name)) == NULL) {
568                                 /* No appropriate group found, create one */
569                         d_printf("Creating unix group: '%s'\n", name);
570                         if (smb_create_group(name, &gid) != 0)
571                                 return NT_STATUS_ACCESS_DENIED;
572                         if ((grp = getgrgid(gid)) == NULL)
573                                 return NT_STATUS_ACCESS_DENIED;
574                 }
575         }
576
577         map.gid = grp->gr_gid;
578         map.sid = alias_sid;
579
580         if (sid_equal(&dom_sid, &global_sid_Builtin))
581                 map.sid_name_use = SID_NAME_WKN_GRP;
582         else
583                 map.sid_name_use = SID_NAME_ALIAS;
584
585         fstrcpy(map.nt_name, name);
586         fstrcpy(map.comment, comment);
587
588         map.priv_set.count = 0;
589         map.priv_set.set = NULL;
590
591         if (insert)
592                 pdb_add_group_mapping_entry(&map);
593         else
594                 pdb_update_group_mapping_entry(&map);
595
596         return NT_STATUS_OK;
597 }
598
599 static NTSTATUS
600 fetch_alias_mem(uint32_t rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid)
601 {
602         
603         return NT_STATUS_OK;
604 }
605
606 static void
607 fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta,
608                 DOM_SID dom_sid)
609 {
610         switch(hdr_delta->type) {
611         case SAM_DELTA_ACCOUNT_INFO:
612                 fetch_account_info(hdr_delta->target_rid,
613                                    &delta->account_info);
614                 break;
615         case SAM_DELTA_GROUP_INFO:
616                 fetch_group_info(hdr_delta->target_rid,
617                                  &delta->group_info);
618                 break;
619         case SAM_DELTA_GROUP_MEM:
620                 fetch_group_mem_info(hdr_delta->target_rid,
621                                      &delta->grp_mem_info);
622                 break;
623         case SAM_DELTA_ALIAS_INFO:
624                 fetch_alias_info(hdr_delta->target_rid,
625                                  &delta->alias_info, dom_sid);
626                 break;
627         case SAM_DELTA_ALIAS_MEM:
628                 fetch_alias_mem(hdr_delta->target_rid,
629                                 &delta->als_mem_info, dom_sid);
630                 break;
631         default:
632                 d_printf("Unknown delta record type %d\n", hdr_delta->type);
633                 break;
634         }
635 }
636
637 static void
638 fetch_database(struct smbcli_state *cli, uint_t db_type, DOM_CRED *ret_creds,
639                DOM_SID dom_sid)
640 {
641         uint_t sync_context = 0;
642         NTSTATUS result;
643         int i;
644         TALLOC_CTX *mem_ctx;
645         SAM_DELTA_HDR *hdr_deltas;
646         SAM_DELTA_CTR *deltas;
647         uint32_t num_deltas;
648
649         if (!(mem_ctx = talloc_init("fetch_database"))) {
650                 return;
651         }
652
653         d_printf("Fetching database %u\n", db_type);
654
655         do {
656                 result = smbcli_netlogon_sam_sync(cli, mem_ctx, ret_creds,
657                                                db_type, sync_context,
658                                                &num_deltas,
659                                                &hdr_deltas, &deltas);
660                 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred),
661                                      ret_creds);
662                 for (i = 0; i < num_deltas; i++) {
663                         fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid);
664                 }
665                 sync_context += 1;
666         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
667
668         talloc_destroy(mem_ctx);
669 }
670
671 /* dump sam database via samsync rpc calls */
672 int rpc_vampire(int argc, const char **argv)
673 {
674         NTSTATUS result;
675         struct smbcli_state *cli = NULL;
676         uint8_t trust_password[16];
677         DOM_CRED ret_creds;
678         uint32_t neg_flags = 0x000001ff;
679         DOM_SID dom_sid;
680
681         ZERO_STRUCT(ret_creds);
682
683         /* Connect to remote machine */
684         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS |
685                                             NET_FLAGS_PDC))) {
686                 return 1;
687         }
688
689         if (!smbcli_nt_session_open(cli, PI_NETLOGON)) {
690                 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
691                 goto fail;
692         }
693
694         if (!secrets_fetch_trust_account_password(lp_workgroup(),
695                                                   trust_password, NULL)) {
696                 d_printf("Could not retrieve domain trust secret\n");
697                 goto fail;
698         }
699         
700         result = smbcli_nt_setup_creds(cli, SEC_CHAN_BDC,  trust_password,
701                                     &neg_flags, 2);
702         if (!NT_STATUS_IS_OK(result)) {
703                 d_printf("Failed to setup BDC creds\n");
704                 goto fail;
705         }
706
707         dom_sid = *get_global_sam_sid();
708         fetch_database(cli, SAM_DATABASE_DOMAIN, &ret_creds, dom_sid);
709
710         sid_copy(&dom_sid, &global_sid_Builtin);
711         fetch_database(cli, SAM_DATABASE_BUILTIN, &ret_creds, dom_sid);
712
713         /* Currently we crash on PRIVS somewhere in unmarshalling */
714         /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */
715
716         smbcli_nt_session_close(cli);
717         
718         return 0;
719
720 fail:
721         if (cli) {
722                 smbcli_nt_session_close(cli);
723         }
724         return -1;
725 }