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