This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[nivanova/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         static uchar 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 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                 /* 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;
148         }
149 }
150
151
152 static void dump_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds)
153 {
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;
161
162         if (!(mem_ctx = talloc_init("dump_database"))) {
163                 return;
164         }
165
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;
179         }
180
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]);
188                 }
189                 sync_context += 1;
190         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
191
192         talloc_destroy(mem_ctx);
193 }
194
195 /* dump sam database via samsync rpc calls */
196 int rpc_samdump(int argc, const char **argv)
197 {
198         NTSTATUS result;
199         struct cli_state *cli = NULL;
200         uchar trust_password[16];
201         DOM_CRED ret_creds;
202         uint32 neg_flags = 0x000001ff;
203
204
205         ZERO_STRUCT(ret_creds);
206
207         /* Connect to remote machine */
208         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
209                 return 1;
210         }
211
212         if (!cli_nt_session_open(cli, PI_NETLOGON)) {
213                 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
214                 goto fail;
215         }
216
217         if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_password, NULL)) {
218                 d_printf("Could not retrieve domain trust secret\n");
219                 goto fail;
220         }
221         
222         result = cli_nt_setup_creds(cli, SEC_CHAN_BDC,  trust_password, &neg_flags, 2);
223         if (!NT_STATUS_IS_OK(result)) {
224                 d_printf("Failed to setup BDC creds\n");
225                 goto fail;
226         }
227
228         dump_database(cli, SAM_DATABASE_DOMAIN, &ret_creds);
229         dump_database(cli, SAM_DATABASE_BUILTIN, &ret_creds);
230         dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds);
231
232         cli_nt_session_close(cli);
233         
234         return 0;
235
236 fail:
237         if (cli) {
238                 cli_nt_session_close(cli);
239         }
240         return -1;
241 }
242
243 /* Convert a SAM_ACCOUNT_DELTA to a SAM_ACCOUNT. */
244 #define STRING_CHANGED (old_string && !new_string) ||\
245                     (!old_string && new_string) ||\
246                 (old_string && new_string && (strcmp(old_string, new_string) != 0))
247
248 static NTSTATUS
249 sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta)
250 {
251         const char *old_string, *new_string;
252         time_t unix_time, stored_time;
253         uchar lm_passwd[16], nt_passwd[16];
254         static uchar zero_buf[16];
255
256         /* Username, fullname, home dir, dir drive, logon script, acct
257            desc, workstations, profile. */
258
259         if (delta->hdr_acct_name.buffer) {
260                 old_string = pdb_get_nt_username(account);
261                 new_string = unistr2_static(&delta->uni_acct_name);
262
263                 if (STRING_CHANGED) {
264                         pdb_set_nt_username(account, new_string, PDB_CHANGED);
265               
266                 }
267          
268                 /* Unix username is the same - for sanity */
269                 old_string = pdb_get_username( account );
270                 if (STRING_CHANGED) {
271                         pdb_set_username(account, new_string, PDB_CHANGED);
272                 }
273         }
274
275         if (delta->hdr_full_name.buffer) {
276                 old_string = pdb_get_fullname(account);
277                 new_string = unistr2_static(&delta->uni_full_name);
278
279                 if (STRING_CHANGED)
280                         pdb_set_fullname(account, new_string, PDB_CHANGED);
281         }
282
283         if (delta->hdr_home_dir.buffer) {
284                 old_string = pdb_get_homedir(account);
285                 new_string = unistr2_static(&delta->uni_home_dir);
286
287                 if (STRING_CHANGED)
288                         pdb_set_homedir(account, new_string, PDB_CHANGED);
289         }
290
291         if (delta->hdr_dir_drive.buffer) {
292                 old_string = pdb_get_dir_drive(account);
293                 new_string = unistr2_static(&delta->uni_dir_drive);
294
295                 if (STRING_CHANGED)
296                         pdb_set_dir_drive(account, new_string, PDB_CHANGED);
297         }
298
299         if (delta->hdr_logon_script.buffer) {
300                 old_string = pdb_get_logon_script(account);
301                 new_string = unistr2_static(&delta->uni_logon_script);
302
303                 if (STRING_CHANGED)
304                         pdb_set_logon_script(account, new_string, PDB_CHANGED);
305         }
306
307         if (delta->hdr_acct_desc.buffer) {
308                 old_string = pdb_get_acct_desc(account);
309                 new_string = unistr2_static(&delta->uni_acct_desc);
310
311                 if (STRING_CHANGED)
312                         pdb_set_acct_desc(account, new_string, PDB_CHANGED);
313         }
314
315         if (delta->hdr_workstations.buffer) {
316                 old_string = pdb_get_workstations(account);
317                 new_string = unistr2_static(&delta->uni_workstations);
318
319                 if (STRING_CHANGED)
320                         pdb_set_workstations(account, new_string, PDB_CHANGED);
321         }
322
323         if (delta->hdr_profile.buffer) {
324                 old_string = pdb_get_profile_path(account);
325                 new_string = unistr2_static(&delta->uni_profile);
326
327                 if (STRING_CHANGED)
328                         pdb_set_profile_path(account, new_string, PDB_CHANGED);
329         }
330
331         /* User and group sid */
332         if (pdb_get_user_rid(account) != delta->user_rid)
333                 pdb_set_user_sid_from_rid(account, delta->user_rid, PDB_CHANGED);
334         if (pdb_get_group_rid(account) != delta->group_rid)
335                 pdb_set_group_sid_from_rid(account, delta->group_rid, PDB_CHANGED);
336
337         /* Logon and password information */
338         if (!nt_time_is_zero(&delta->logon_time)) {
339                 unix_time = nt_time_to_unix(&delta->logon_time);
340                 stored_time = pdb_get_logon_time(account);
341                 if (stored_time != unix_time)
342                         pdb_set_logon_time(account, unix_time, PDB_CHANGED);
343         }
344
345         if (!nt_time_is_zero(&delta->logoff_time)) {
346                 unix_time = nt_time_to_unix(&delta->logoff_time);
347                 stored_time = pdb_get_logoff_time(account);
348                 if (stored_time != unix_time)
349                         pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
350         }
351
352         if (pdb_get_logon_divs(account) != delta->logon_divs)
353                 pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED);
354
355         /* TODO: logon hours */
356         /* TODO: bad password count */
357         /* TODO: logon count */
358
359         if (!nt_time_is_zero(&delta->pwd_last_set_time)) {
360                 unix_time = nt_time_to_unix(&delta->pwd_last_set_time);
361                 stored_time = pdb_get_pass_last_set_time(account);
362                 if (stored_time != unix_time)
363                         pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
364         }
365
366 #if 0
367         /* No kickoff time in the delta? */
368         if (!nt_time_is_zero(&delta->kickoff_time)) {
369                 unix_time = nt_time_to_unix(&delta->kickoff_time);
370                 stored_time = pdb_get_kickoff_time(account);
371                 if (stored_time != unix_time)
372                         pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
373         }
374 #endif
375
376         /* Decode hashes from password hash 
377            Note that win2000 may send us all zeros for the hashes if it doesn't 
378            think this channel is secure enough - don't set the passwords at all
379            in that case
380         */
381         if (memcmp(delta->pass.buf_lm_pwd, zero_buf, 16) != 0) {
382                 sam_pwd_hash(delta->user_rid, delta->pass.buf_lm_pwd, lm_passwd, 0);
383                 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
384         }
385
386         if (memcmp(delta->pass.buf_nt_pwd, zero_buf, 16) != 0) {
387                 sam_pwd_hash(delta->user_rid, delta->pass.buf_nt_pwd, nt_passwd, 0);
388                 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
389         }
390
391         /* TODO: account expiry time */
392
393         if (pdb_get_acct_ctrl(account) != delta->acb_info)
394                 pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED);
395
396         pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
397
398         return NT_STATUS_OK;
399 }
400
401 static NTSTATUS
402 fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta)
403 {
404         NTSTATUS nt_ret;
405         fstring account;
406         pstring add_script;
407         SAM_ACCOUNT *sam_account=NULL;
408         GROUP_MAP map;
409         struct group *grp;
410         DOM_SID sid;
411         BOOL try_add = False;
412
413         fstrcpy(account, unistr2_static(&delta->uni_acct_name));
414         d_printf("Creating account: %s\n", account);
415
416         if (!NT_STATUS_IS_OK(nt_ret = pdb_init_sam(&sam_account)))
417                 return nt_ret;
418
419         if (!pdb_getsampwnam(sam_account, account)) {
420                 /* Create appropriate user */
421                 if (delta->acb_info & ACB_NORMAL) {
422                         pstrcpy(add_script, lp_adduser_script());
423                 } else if ( (delta->acb_info & ACB_WSTRUST) ||
424                             (delta->acb_info & ACB_SVRTRUST) ||
425                             (delta->acb_info & ACB_DOMTRUST) ) {
426                         pstrcpy(add_script, lp_addmachine_script());
427                 } else {
428                         DEBUG(1, ("Unknown user type: %s\n",
429                                   smbpasswd_encode_acb_info(delta->acb_info)));
430                         pdb_free_sam(&sam_account);
431                         return NT_STATUS_NO_SUCH_USER;
432                 }
433                 if (*add_script) {
434                         int add_ret;
435                         all_string_sub(add_script, "%u", account,
436                                        sizeof(account));
437                         add_ret = smbrun(add_script,NULL);
438                         DEBUG(1,("fetch_account: Running the command `%s' "
439                                  "gave %d\n", add_script, add_ret));
440                 }
441
442                 try_add = True;
443         }
444
445         sam_account_from_delta(sam_account, delta);
446
447         if (try_add) { 
448                 if (!pdb_add_sam_account(sam_account)) {
449                         DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
450                                   account));
451                 }
452         } else {
453                 if (!pdb_update_sam_account(sam_account)) {
454                         DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
455                                   account));
456                 }
457         }
458
459         sid = *pdb_get_group_sid(sam_account);
460
461         if (!pdb_getgrsid(&map, sid, False)) {
462                 DEBUG(0, ("Primary group of %s has no mapping!\n",
463                           pdb_get_username(sam_account)));
464                 pdb_free_sam(&sam_account);
465                 return NT_STATUS_NO_SUCH_GROUP;
466         }
467
468         if (!(grp = getgrgid(map.gid))) {
469                 DEBUG(0, ("Could not find unix group %d for user %s (group SID=%s)\n", 
470                           map.gid, pdb_get_username(sam_account), sid_string_static(&sid)));
471                 pdb_free_sam(&sam_account);
472                 return NT_STATUS_NO_SUCH_GROUP;
473         }
474
475         smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
476
477         pdb_free_sam(&sam_account);
478         return NT_STATUS_OK;
479 }
480
481 static NTSTATUS
482 fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta)
483 {
484         fstring name;
485         fstring comment;
486         struct group *grp = NULL;
487         DOM_SID group_sid;
488         fstring sid_string;
489         GROUP_MAP map;
490         BOOL insert = True;
491
492         unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1);
493         unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1);
494
495         /* add the group to the mapping table */
496         sid_copy(&group_sid, get_global_sam_sid());
497         sid_append_rid(&group_sid, rid);
498         sid_to_string(sid_string, &group_sid);
499
500         if (pdb_getgrsid(&map, group_sid, False)) {
501                 grp = getgrgid(map.gid);
502                 insert = False;
503         }
504
505         if (grp == NULL)
506         {
507                 gid_t gid;
508
509                 /* No group found from mapping, find it from its name. */
510                 if ((grp = getgrnam(name)) == NULL) {
511                         /* No appropriate group found, create one */
512                         d_printf("Creating unix group: '%s'\n", name);
513                         if (smb_create_group(name, &gid) != 0)
514                                 return NT_STATUS_ACCESS_DENIED;
515                         if ((grp = getgrgid(gid)) == NULL)
516                                 return NT_STATUS_ACCESS_DENIED;
517                 }
518         }
519
520         map.gid = grp->gr_gid;
521         map.sid = group_sid;
522         map.sid_name_use = SID_NAME_DOM_GRP;
523         fstrcpy(map.nt_name, name);
524         fstrcpy(map.comment, comment);
525
526         map.priv_set.count = 0;
527         map.priv_set.set = NULL;
528
529         if (insert)
530                 pdb_add_group_mapping_entry(&map);
531         else
532                 pdb_update_group_mapping_entry(&map);
533
534         return NT_STATUS_OK;
535 }
536
537 static NTSTATUS
538 fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta)
539 {
540         int i;
541         TALLOC_CTX *t = NULL;
542         char **nt_members = NULL;
543         char **unix_members;
544         DOM_SID group_sid;
545         GROUP_MAP map;
546         struct group *grp;
547
548         if (delta->num_members == 0) {
549                 return NT_STATUS_OK;
550         }
551
552         sid_copy(&group_sid, get_global_sam_sid());
553         sid_append_rid(&group_sid, rid);
554
555         if (!get_domain_group_from_sid(group_sid, &map, False)) {
556                 DEBUG(0, ("Could not find global group %d\n", rid));
557                 return NT_STATUS_NO_SUCH_GROUP;
558         }
559
560         if (!(grp = getgrgid(map.gid))) {
561                 DEBUG(0, ("Could not find unix group %d\n", map.gid));
562                 return NT_STATUS_NO_SUCH_GROUP;
563         }
564
565         d_printf("Group members of %s: ", grp->gr_name);
566
567         if (!(t = talloc_init("fetch_group_mem_info"))) {
568                 DEBUG(0, ("could not talloc_init\n"));
569                 return NT_STATUS_NO_MEMORY;
570         }
571
572         nt_members = talloc_zero(t, sizeof(char *) * delta->num_members);
573
574         for (i=0; i<delta->num_members; i++) {
575                 NTSTATUS nt_status;
576                 SAM_ACCOUNT *member = NULL;
577                 DOM_SID member_sid;
578
579                 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) {
580                         talloc_destroy(t);
581                         return nt_status;
582                 }
583
584                 sid_copy(&member_sid, get_global_sam_sid());
585                 sid_append_rid(&member_sid, delta->rids[i]);
586
587                 if (!pdb_getsampwsid(member, &member_sid)) {
588                         DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
589                                   delta->rids[i], sid_string_static(&member_sid), grp->gr_name));
590                         pdb_free_sam(&member);
591                         continue;
592                 }
593
594                 if (pdb_get_group_rid(member) == rid) {
595                         d_printf("%s(primary),", pdb_get_username(member));
596                         pdb_free_sam(&member);
597                         continue;
598                 }
599                 
600                 d_printf("%s,", pdb_get_username(member));
601                 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
602                 pdb_free_sam(&member);
603         }
604
605         d_printf("\n");
606
607         unix_members = grp->gr_mem;
608
609         while (*unix_members) {
610                 BOOL is_nt_member = False;
611                 for (i=0; i<delta->num_members; i++) {
612                         if (nt_members[i] == NULL) {
613                                 /* This was a primary group */
614                                 continue;
615                         }
616
617                         if (strcmp(*unix_members, nt_members[i]) == 0) {
618                                 is_nt_member = True;
619                                 break;
620                         }
621                 }
622                 if (!is_nt_member) {
623                         /* We look at a unix group member that is not
624                            an nt group member. So, remove it. NT is
625                            boss here. */
626                         smb_delete_user_group(grp->gr_name, *unix_members);
627                 }
628                 unix_members += 1;
629         }
630
631         for (i=0; i<delta->num_members; i++) {
632                 BOOL is_unix_member = False;
633
634                 if (nt_members[i] == NULL) {
635                         /* This was the primary group */
636                         continue;
637                 }
638
639                 unix_members = grp->gr_mem;
640
641                 while (*unix_members) {
642                         if (strcmp(*unix_members, nt_members[i]) == 0) {
643                                 is_unix_member = True;
644                                 break;
645                         }
646                         unix_members += 1;
647                 }
648
649                 if (!is_unix_member) {
650                         /* We look at a nt group member that is not a
651                            unix group member currently. So, add the nt
652                            group member. */
653                         smb_add_user_group(grp->gr_name, nt_members[i]);
654                 }
655         }
656         
657         talloc_destroy(t);
658         return NT_STATUS_OK;
659 }
660
661 static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta,
662                                  DOM_SID dom_sid)
663 {
664         fstring name;
665         fstring comment;
666         struct group *grp = NULL;
667         DOM_SID alias_sid;
668         fstring sid_string;
669         GROUP_MAP map;
670         BOOL insert = True;
671
672         unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1);
673         unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1);
674
675         /* Find out whether the group is already mapped */
676         sid_copy(&alias_sid, &dom_sid);
677         sid_append_rid(&alias_sid, rid);
678         sid_to_string(sid_string, &alias_sid);
679
680         if (pdb_getgrsid(&map, alias_sid, False)) {
681                 grp = getgrgid(map.gid);
682                 insert = False;
683         }
684
685         if (grp == NULL) {
686                 gid_t gid;
687
688                 /* No group found from mapping, find it from its name. */
689                 if ((grp = getgrnam(name)) == NULL) {
690                         /* No appropriate group found, create one */
691                         d_printf("Creating unix group: '%s'\n", name);
692                         if (smb_create_group(name, &gid) != 0)
693                                 return NT_STATUS_ACCESS_DENIED;
694                         if ((grp = getgrgid(gid)) == NULL)
695                                 return NT_STATUS_ACCESS_DENIED;
696                 }
697         }
698
699         map.gid = grp->gr_gid;
700         map.sid = alias_sid;
701
702         if (sid_equal(&dom_sid, &global_sid_Builtin))
703                 map.sid_name_use = SID_NAME_WKN_GRP;
704         else
705                 map.sid_name_use = SID_NAME_ALIAS;
706
707         fstrcpy(map.nt_name, name);
708         fstrcpy(map.comment, comment);
709
710         map.priv_set.count = 0;
711         map.priv_set.set = NULL;
712
713         if (insert)
714                 pdb_add_group_mapping_entry(&map);
715         else
716                 pdb_update_group_mapping_entry(&map);
717
718         return NT_STATUS_OK;
719 }
720
721 static NTSTATUS
722 fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid)
723 {
724         
725         return NT_STATUS_OK;
726 }
727
728 static void
729 fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta,
730                 DOM_SID dom_sid)
731 {
732         switch(hdr_delta->type) {
733         case SAM_DELTA_ACCOUNT_INFO:
734                 fetch_account_info(hdr_delta->target_rid,
735                                    &delta->account_info);
736                 break;
737         case SAM_DELTA_GROUP_INFO:
738                 fetch_group_info(hdr_delta->target_rid,
739                                  &delta->group_info);
740                 break;
741         case SAM_DELTA_GROUP_MEM:
742                 fetch_group_mem_info(hdr_delta->target_rid,
743                                      &delta->grp_mem_info);
744                 break;
745         case SAM_DELTA_ALIAS_INFO:
746                 fetch_alias_info(hdr_delta->target_rid,
747                                  &delta->alias_info, dom_sid);
748                 break;
749         case SAM_DELTA_ALIAS_MEM:
750                 fetch_alias_mem(hdr_delta->target_rid,
751                                 &delta->als_mem_info, dom_sid);
752                 break;
753         case SAM_DELTA_DOMAIN_INFO:
754                 d_printf("SAMBA_DELTA_DOMAIN_INFO not handled\n");
755                 break;
756         default:
757                 d_printf("Unknown delta record type %d\n", hdr_delta->type);
758                 break;
759         }
760 }
761
762 static void
763 fetch_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds,
764                DOM_SID dom_sid)
765 {
766         unsigned sync_context = 0;
767         NTSTATUS result;
768         int i;
769         TALLOC_CTX *mem_ctx;
770         SAM_DELTA_HDR *hdr_deltas;
771         SAM_DELTA_CTR *deltas;
772         uint32 num_deltas;
773
774         if (!(mem_ctx = talloc_init("fetch_database"))) {
775                 return;
776         }
777
778         switch( db_type ) {
779         case SAM_DATABASE_DOMAIN:
780                 d_printf("Fetching DOMAIN database\n");
781                 break;
782         case SAM_DATABASE_BUILTIN:
783                 d_printf("Fetching BUILTIN database\n");
784                 break;
785         case SAM_DATABASE_PRIVS:
786                 d_printf("Fetching PRIVS databases\n");
787                 break;
788         default:
789                 d_printf("Fetching unknown database type %u\n", db_type );
790                 break;
791         }
792
793         do {
794                 result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds,
795                                                db_type, sync_context,
796                                                &num_deltas,
797                                                &hdr_deltas, &deltas);
798                 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred),
799                                      ret_creds);
800                 for (i = 0; i < num_deltas; i++) {
801                         fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid);
802                 }
803                 sync_context += 1;
804         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
805
806         talloc_destroy(mem_ctx);
807 }
808
809 /* dump sam database via samsync rpc calls */
810 int rpc_vampire(int argc, const char **argv)
811 {
812         NTSTATUS result;
813         struct cli_state *cli = NULL;
814         uchar trust_password[16];
815         DOM_CRED ret_creds;
816         uint32 neg_flags = 0x000001ff;
817         DOM_SID dom_sid;
818
819         ZERO_STRUCT(ret_creds);
820
821         /* Connect to remote machine */
822         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS |
823                                             NET_FLAGS_PDC))) {
824                 return 1;
825         }
826
827         if (!cli_nt_session_open(cli, PI_NETLOGON)) {
828                 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
829                 goto fail;
830         }
831
832         if (!secrets_fetch_trust_account_password(lp_workgroup(),
833                                                   trust_password, NULL)) {
834                 d_printf("Could not retrieve domain trust secret\n");
835                 goto fail;
836         }
837         
838         result = cli_nt_setup_creds(cli, SEC_CHAN_BDC,  trust_password,
839                                     &neg_flags, 2);
840         if (!NT_STATUS_IS_OK(result)) {
841                 d_printf("Failed to setup BDC creds\n");
842                 goto fail;
843         }
844
845         dom_sid = *get_global_sam_sid();
846         fetch_database(cli, SAM_DATABASE_DOMAIN, &ret_creds, dom_sid);
847
848         sid_copy(&dom_sid, &global_sid_Builtin);
849         fetch_database(cli, SAM_DATABASE_BUILTIN, &ret_creds, dom_sid);
850
851         /* Currently we crash on PRIVS somewhere in unmarshalling */
852         /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */
853
854         cli_nt_session_close(cli);
855         
856         return 0;
857
858 fail:
859         if (cli) {
860                 cli_nt_session_close(cli);
861         }
862         return -1;
863 }