Ok, this patch removes the privilege stuff we had in, unused, for some time.
[amitay/samba.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         struct cli_state *cli = NULL;
199         uchar trust_password[16];
200         DOM_CRED ret_creds;
201         uint32 sec_channel;
202
203         ZERO_STRUCT(ret_creds);
204
205         /* Connect to remote machine */
206         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
207                 return 1;
208         }
209
210         fstrcpy(cli->domain, lp_workgroup());
211
212         if (!cli_nt_session_open(cli, PI_NETLOGON)) {
213                 DEBUG(0,("Could not open connection to NETLOGON pipe\n"));
214                 goto fail;
215         }
216
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;
222         }
223
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;
228         }
229
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);
233
234         cli_nt_session_close(cli);
235         
236         return 0;
237
238 fail:
239         if (cli) {
240                 cli_nt_session_close(cli);
241         }
242         return -1;
243 }
244
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))
249
250 static NTSTATUS
251 sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta)
252 {
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];
257
258         /* Username, fullname, home dir, dir drive, logon script, acct
259            desc, workstations, profile. */
260
261         if (delta->hdr_acct_name.buffer) {
262                 old_string = pdb_get_nt_username(account);
263                 new_string = unistr2_static(&delta->uni_acct_name);
264
265                 if (STRING_CHANGED) {
266                         pdb_set_nt_username(account, new_string, PDB_CHANGED);
267               
268                 }
269          
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);
274                 }
275         }
276
277         if (delta->hdr_full_name.buffer) {
278                 old_string = pdb_get_fullname(account);
279                 new_string = unistr2_static(&delta->uni_full_name);
280
281                 if (STRING_CHANGED)
282                         pdb_set_fullname(account, new_string, PDB_CHANGED);
283         }
284
285         if (delta->hdr_home_dir.buffer) {
286                 old_string = pdb_get_homedir(account);
287                 new_string = unistr2_static(&delta->uni_home_dir);
288
289                 if (STRING_CHANGED)
290                         pdb_set_homedir(account, new_string, PDB_CHANGED);
291         }
292
293         if (delta->hdr_dir_drive.buffer) {
294                 old_string = pdb_get_dir_drive(account);
295                 new_string = unistr2_static(&delta->uni_dir_drive);
296
297                 if (STRING_CHANGED)
298                         pdb_set_dir_drive(account, new_string, PDB_CHANGED);
299         }
300
301         if (delta->hdr_logon_script.buffer) {
302                 old_string = pdb_get_logon_script(account);
303                 new_string = unistr2_static(&delta->uni_logon_script);
304
305                 if (STRING_CHANGED)
306                         pdb_set_logon_script(account, new_string, PDB_CHANGED);
307         }
308
309         if (delta->hdr_acct_desc.buffer) {
310                 old_string = pdb_get_acct_desc(account);
311                 new_string = unistr2_static(&delta->uni_acct_desc);
312
313                 if (STRING_CHANGED)
314                         pdb_set_acct_desc(account, new_string, PDB_CHANGED);
315         }
316
317         if (delta->hdr_workstations.buffer) {
318                 old_string = pdb_get_workstations(account);
319                 new_string = unistr2_static(&delta->uni_workstations);
320
321                 if (STRING_CHANGED)
322                         pdb_set_workstations(account, new_string, PDB_CHANGED);
323         }
324
325         if (delta->hdr_profile.buffer) {
326                 old_string = pdb_get_profile_path(account);
327                 new_string = unistr2_static(&delta->uni_profile);
328
329                 if (STRING_CHANGED)
330                         pdb_set_profile_path(account, new_string, PDB_CHANGED);
331         }
332
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);
338
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);
345         }
346
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);
352         }
353
354         if (pdb_get_logon_divs(account) != delta->logon_divs)
355                 pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED);
356
357         /* TODO: logon hours */
358         /* TODO: bad password count */
359         /* TODO: logon count */
360
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);
366         }
367
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);
375         }
376 #endif
377
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
382         */
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);
386         }
387
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);
391         }
392
393         /* TODO: account expiry time */
394
395         if (pdb_get_acct_ctrl(account) != delta->acb_info)
396                 pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED);
397
398         pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
399
400         return NT_STATUS_OK;
401 }
402
403 static NTSTATUS
404 fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta)
405 {
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 sid;
413         struct passwd *passwd;
414         unid_t id;
415         int u_type;
416
417         fstrcpy(account, unistr2_static(&delta->uni_acct_name));
418         d_printf("Creating account: %s\n", account);
419
420         if (!NT_STATUS_IS_OK(nt_ret = pdb_init_sam(&sam_account)))
421                 return nt_ret;
422
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)));
434                 }
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));
442
443                         /* try and find the possible unix account again */
444                         passwd = Get_Pwnam(account);
445                 }
446         }
447
448         sam_account_from_delta(sam_account, delta);
449         if (!pdb_add_sam_account(sam_account)) {
450                 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
451                           account));
452                 if (!pdb_update_sam_account(sam_account)) {
453                         DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
454                                   account));
455                         pdb_free_sam(&sam_account);
456                         return NT_STATUS_OK;
457 /*                      return NT_STATUS_ACCESS_DENIED; */
458                 }
459         }
460
461         sid = *pdb_get_group_sid(sam_account);
462
463         if (!pdb_getgrsid(&map, sid)) {
464                 DEBUG(0, ("Primary group of %s has no mapping!\n",
465                           pdb_get_username(sam_account)));
466                 pdb_free_sam(&sam_account);
467                 return NT_STATUS_NO_SUCH_GROUP;
468         }
469         
470         if (!passwd) {
471                 /* if no unix user, changing the mapping won't help */
472                 pdb_free_sam(&sam_account);
473                 return NT_STATUS_OK;
474         }
475                 
476         if (map.gid != passwd->pw_gid) {
477                 if (!(grp = getgrgid(map.gid))) {
478                         DEBUG(0, ("Could not find unix group %d for user %s (group SID=%s)\n", 
479                                   map.gid, pdb_get_username(sam_account), sid_string_static(&sid)));
480                         pdb_free_sam(&sam_account);
481                         return NT_STATUS_NO_SUCH_GROUP;
482                 }
483                 
484                 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
485         }
486         
487         nt_ret = idmap_get_id_from_sid(&id, &u_type, pdb_get_user_sid(sam_account));
488         if (!NT_STATUS_IS_OK(nt_ret)) {
489                 pdb_free_sam(&sam_account);
490                 return nt_ret;
491         }
492
493         if ((u_type != ID_USERID) || (id.uid != passwd->pw_uid)) {
494                 id.uid = passwd->pw_uid;
495                 nt_ret = idmap_set_mapping(pdb_get_user_sid(sam_account), id, ID_USERID);
496         }
497
498         pdb_free_sam(&sam_account);
499         return nt_ret;
500 }
501
502 static NTSTATUS
503 fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta)
504 {
505         fstring name;
506         fstring comment;
507         struct group *grp = NULL;
508         DOM_SID group_sid;
509         fstring sid_string;
510         GROUP_MAP map;
511         BOOL insert = True;
512
513         unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1);
514         unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1);
515
516         /* add the group to the mapping table */
517         sid_copy(&group_sid, get_global_sam_sid());
518         sid_append_rid(&group_sid, rid);
519         sid_to_string(sid_string, &group_sid);
520
521         if (pdb_getgrsid(&map, group_sid)) {
522                 grp = getgrgid(map.gid);
523                 insert = False;
524         }
525
526         if (grp == NULL)
527         {
528                 gid_t gid;
529
530                 /* No group found from mapping, find it from its name. */
531                 if ((grp = getgrnam(name)) == NULL) {
532                         /* No appropriate group found, create one */
533                         d_printf("Creating unix group: '%s'\n", name);
534                         if (smb_create_group(name, &gid) != 0)
535                                 return NT_STATUS_ACCESS_DENIED;
536                         if ((grp = getgrgid(gid)) == NULL)
537                                 return NT_STATUS_ACCESS_DENIED;
538                 }
539         }
540
541         map.gid = grp->gr_gid;
542         map.sid = group_sid;
543         map.sid_name_use = SID_NAME_DOM_GRP;
544         fstrcpy(map.nt_name, name);
545         fstrcpy(map.comment, comment);
546
547         if (insert)
548                 pdb_add_group_mapping_entry(&map);
549         else
550                 pdb_update_group_mapping_entry(&map);
551
552         return NT_STATUS_OK;
553 }
554
555 static NTSTATUS
556 fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta)
557 {
558         int i;
559         TALLOC_CTX *t = NULL;
560         char **nt_members = NULL;
561         char **unix_members;
562         DOM_SID group_sid;
563         GROUP_MAP map;
564         struct group *grp;
565
566         if (delta->num_members == 0) {
567                 return NT_STATUS_OK;
568         }
569
570         sid_copy(&group_sid, get_global_sam_sid());
571         sid_append_rid(&group_sid, rid);
572
573         if (!get_domain_group_from_sid(group_sid, &map)) {
574                 DEBUG(0, ("Could not find global group %d\n", rid));
575                 return NT_STATUS_NO_SUCH_GROUP;
576         }
577
578         if (!(grp = getgrgid(map.gid))) {
579                 DEBUG(0, ("Could not find unix group %d\n", map.gid));
580                 return NT_STATUS_NO_SUCH_GROUP;
581         }
582
583         d_printf("Group members of %s: ", grp->gr_name);
584
585         if (!(t = talloc_init("fetch_group_mem_info"))) {
586                 DEBUG(0, ("could not talloc_init\n"));
587                 return NT_STATUS_NO_MEMORY;
588         }
589
590         nt_members = talloc_zero(t, sizeof(char *) * delta->num_members);
591
592         for (i=0; i<delta->num_members; i++) {
593                 NTSTATUS nt_status;
594                 SAM_ACCOUNT *member = NULL;
595                 DOM_SID member_sid;
596
597                 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) {
598                         talloc_destroy(t);
599                         return nt_status;
600                 }
601
602                 sid_copy(&member_sid, get_global_sam_sid());
603                 sid_append_rid(&member_sid, delta->rids[i]);
604
605                 if (!pdb_getsampwsid(member, &member_sid)) {
606                         DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
607                                   delta->rids[i], sid_string_static(&member_sid), grp->gr_name));
608                         pdb_free_sam(&member);
609                         continue;
610                 }
611
612                 if (pdb_get_group_rid(member) == rid) {
613                         d_printf("%s(primary),", pdb_get_username(member));
614                         pdb_free_sam(&member);
615                         continue;
616                 }
617                 
618                 d_printf("%s,", pdb_get_username(member));
619                 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
620                 pdb_free_sam(&member);
621         }
622
623         d_printf("\n");
624
625         unix_members = grp->gr_mem;
626
627         while (*unix_members) {
628                 BOOL is_nt_member = False;
629                 for (i=0; i<delta->num_members; i++) {
630                         if (nt_members[i] == NULL) {
631                                 /* This was a primary group */
632                                 continue;
633                         }
634
635                         if (strcmp(*unix_members, nt_members[i]) == 0) {
636                                 is_nt_member = True;
637                                 break;
638                         }
639                 }
640                 if (!is_nt_member) {
641                         /* We look at a unix group member that is not
642                            an nt group member. So, remove it. NT is
643                            boss here. */
644                         smb_delete_user_group(grp->gr_name, *unix_members);
645                 }
646                 unix_members += 1;
647         }
648
649         for (i=0; i<delta->num_members; i++) {
650                 BOOL is_unix_member = False;
651
652                 if (nt_members[i] == NULL) {
653                         /* This was the primary group */
654                         continue;
655                 }
656
657                 unix_members = grp->gr_mem;
658
659                 while (*unix_members) {
660                         if (strcmp(*unix_members, nt_members[i]) == 0) {
661                                 is_unix_member = True;
662                                 break;
663                         }
664                         unix_members += 1;
665                 }
666
667                 if (!is_unix_member) {
668                         /* We look at a nt group member that is not a
669                            unix group member currently. So, add the nt
670                            group member. */
671                         smb_add_user_group(grp->gr_name, nt_members[i]);
672                 }
673         }
674         
675         talloc_destroy(t);
676         return NT_STATUS_OK;
677 }
678
679 static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta,
680                                  DOM_SID dom_sid)
681 {
682         fstring name;
683         fstring comment;
684         struct group *grp = NULL;
685         DOM_SID alias_sid;
686         fstring sid_string;
687         GROUP_MAP map;
688         BOOL insert = True;
689
690         unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1);
691         unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1);
692
693         /* Find out whether the group is already mapped */
694         sid_copy(&alias_sid, &dom_sid);
695         sid_append_rid(&alias_sid, rid);
696         sid_to_string(sid_string, &alias_sid);
697
698         if (pdb_getgrsid(&map, alias_sid)) {
699                 grp = getgrgid(map.gid);
700                 insert = False;
701         }
702
703         if (grp == NULL) {
704                 gid_t gid;
705
706                 /* No group found from mapping, find it from its name. */
707                 if ((grp = getgrnam(name)) == NULL) {
708                         /* No appropriate group found, create one */
709                         d_printf("Creating unix group: '%s'\n", name);
710                         if (smb_create_group(name, &gid) != 0)
711                                 return NT_STATUS_ACCESS_DENIED;
712                         if ((grp = getgrgid(gid)) == NULL)
713                                 return NT_STATUS_ACCESS_DENIED;
714                 }
715         }
716
717         map.gid = grp->gr_gid;
718         map.sid = alias_sid;
719
720         if (sid_equal(&dom_sid, &global_sid_Builtin))
721                 map.sid_name_use = SID_NAME_WKN_GRP;
722         else
723                 map.sid_name_use = SID_NAME_ALIAS;
724
725         fstrcpy(map.nt_name, name);
726         fstrcpy(map.comment, comment);
727
728         if (insert)
729                 pdb_add_group_mapping_entry(&map);
730         else
731                 pdb_update_group_mapping_entry(&map);
732
733         return NT_STATUS_OK;
734 }
735
736 static NTSTATUS
737 fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid)
738 {
739 #if 0   /* 
740          * commented out right now after talking to Volker.  Can't
741          * do much with the membership but seemed a shame to waste
742          * somewhat working code.  Needs testing because the membership
743          * that shows up surprises me.  Also can't do much with groups
744          * in groups (e.g. Domain Admins being a member of Adminsitrators).
745          * --jerry
746          */
747         
748         int i;
749         TALLOC_CTX *t = NULL;
750         char **nt_members = NULL;
751         char **unix_members;
752         DOM_SID group_sid;
753         GROUP_MAP map;
754         struct group *grp;
755         enum SID_NAME_USE sid_type;
756
757         if (delta->num_members == 0) {
758                 return NT_STATUS_OK;
759         }
760
761         sid_copy(&group_sid, &dom_sid);
762         sid_append_rid(&group_sid, rid);
763
764         if (sid_equal(&dom_sid, &global_sid_Builtin)) {
765                 sid_type = SID_NAME_WKN_GRP;
766                 if (!get_builtin_group_from_sid(group_sid, &map, False)) {
767                         DEBUG(0, ("Could not find builtin group %s\n", sid_string_static(&group_sid)));
768                         return NT_STATUS_NO_SUCH_GROUP;
769                 }
770         } else {
771                 sid_type = SID_NAME_ALIAS;
772                 if (!get_local_group_from_sid(group_sid, &map, False)) {
773                         DEBUG(0, ("Could not find local group %s\n", sid_string_static(&group_sid)));
774                         return NT_STATUS_NO_SUCH_GROUP;
775                 }
776         }       
777
778         if (!(grp = getgrgid(map.gid))) {
779                 DEBUG(0, ("Could not find unix group %d\n", map.gid));
780                 return NT_STATUS_NO_SUCH_GROUP;
781         }
782
783         d_printf("Group members of %s: ", grp->gr_name);
784
785         if (!(t = talloc_init("fetch_group_mem_info"))) {
786                 DEBUG(0, ("could not talloc_init\n"));
787                 return NT_STATUS_NO_MEMORY;
788         }
789
790         nt_members = talloc_zero(t, sizeof(char *) * delta->num_members);
791
792         for (i=0; i<delta->num_members; i++) {
793                 NTSTATUS nt_status;
794                 SAM_ACCOUNT *member = NULL;
795                 DOM_SID member_sid;
796
797                 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) {
798                         talloc_destroy(t);
799                         return nt_status;
800                 }
801
802                 sid_copy(&member_sid, &delta->sids[i].sid);
803
804                 if (!pdb_getsampwsid(member, &member_sid)) {
805                         DEBUG(1, ("Found bogus group member: (member_sid=%s group=%s)\n",
806                                   sid_string_static(&member_sid), grp->gr_name));
807                         pdb_free_sam(&member);
808                         continue;
809                 }
810
811                 if (pdb_get_group_rid(member) == rid) {
812                         d_printf("%s(primary),", pdb_get_username(member));
813                         pdb_free_sam(&member);
814                         continue;
815                 }
816                 
817                 d_printf("%s,", pdb_get_username(member));
818                 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
819                 pdb_free_sam(&member);
820         }
821
822         d_printf("\n");
823
824         unix_members = grp->gr_mem;
825
826         while (*unix_members) {
827                 BOOL is_nt_member = False;
828                 for (i=0; i<delta->num_members; i++) {
829                         if (nt_members[i] == NULL) {
830                                 /* This was a primary group */
831                                 continue;
832                         }
833
834                         if (strcmp(*unix_members, nt_members[i]) == 0) {
835                                 is_nt_member = True;
836                                 break;
837                         }
838                 }
839                 if (!is_nt_member) {
840                         /* We look at a unix group member that is not
841                            an nt group member. So, remove it. NT is
842                            boss here. */
843                         smb_delete_user_group(grp->gr_name, *unix_members);
844                 }
845                 unix_members += 1;
846         }
847
848         for (i=0; i<delta->num_members; i++) {
849                 BOOL is_unix_member = False;
850
851                 if (nt_members[i] == NULL) {
852                         /* This was the primary group */
853                         continue;
854                 }
855
856                 unix_members = grp->gr_mem;
857
858                 while (*unix_members) {
859                         if (strcmp(*unix_members, nt_members[i]) == 0) {
860                                 is_unix_member = True;
861                                 break;
862                         }
863                         unix_members += 1;
864                 }
865
866                 if (!is_unix_member) {
867                         /* We look at a nt group member that is not a
868                            unix group member currently. So, add the nt
869                            group member. */
870                         smb_add_user_group(grp->gr_name, nt_members[i]);
871                 }
872         }
873         
874         talloc_destroy(t);
875
876 #endif  /* end of fetch_alias_mem() */
877
878         return NT_STATUS_OK;
879 }
880
881 static void
882 fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta,
883                 DOM_SID dom_sid)
884 {
885         switch(hdr_delta->type) {
886         case SAM_DELTA_ACCOUNT_INFO:
887                 fetch_account_info(hdr_delta->target_rid,
888                                    &delta->account_info);
889                 break;
890         case SAM_DELTA_GROUP_INFO:
891                 fetch_group_info(hdr_delta->target_rid,
892                                  &delta->group_info);
893                 break;
894         case SAM_DELTA_GROUP_MEM:
895                 fetch_group_mem_info(hdr_delta->target_rid,
896                                      &delta->grp_mem_info);
897                 break;
898         case SAM_DELTA_ALIAS_INFO:
899                 fetch_alias_info(hdr_delta->target_rid,
900                                  &delta->alias_info, dom_sid);
901                 break;
902         case SAM_DELTA_ALIAS_MEM:
903                 fetch_alias_mem(hdr_delta->target_rid,
904                                 &delta->als_mem_info, dom_sid);
905                 break;
906         case SAM_DELTA_DOMAIN_INFO:
907                 d_printf("SAMBA_DELTA_DOMAIN_INFO not handled\n");
908                 break;
909         default:
910                 d_printf("Unknown delta record type %d\n", hdr_delta->type);
911                 break;
912         }
913 }
914
915 static NTSTATUS
916 fetch_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds,
917                DOM_SID dom_sid)
918 {
919         unsigned sync_context = 0;
920         NTSTATUS result;
921         int i;
922         TALLOC_CTX *mem_ctx;
923         SAM_DELTA_HDR *hdr_deltas;
924         SAM_DELTA_CTR *deltas;
925         uint32 num_deltas;
926
927         if (!(mem_ctx = talloc_init("fetch_database")))
928                 return NT_STATUS_NO_MEMORY;
929
930         switch( db_type ) {
931         case SAM_DATABASE_DOMAIN:
932                 d_printf("Fetching DOMAIN database\n");
933                 break;
934         case SAM_DATABASE_BUILTIN:
935                 d_printf("Fetching BUILTIN database\n");
936                 break;
937         case SAM_DATABASE_PRIVS:
938                 d_printf("Fetching PRIVS databases\n");
939                 break;
940         default:
941                 d_printf("Fetching unknown database type %u\n", db_type );
942                 break;
943         }
944
945         do {
946                 result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds,
947                                                db_type, sync_context,
948                                                &num_deltas,
949                                                &hdr_deltas, &deltas);
950
951                 if (NT_STATUS_IS_OK(result) ||
952                     NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
953
954                         clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred),
955                                              ret_creds);
956
957                         for (i = 0; i < num_deltas; i++) {
958                                 fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid);
959                         }
960                 } else
961                         return result;
962
963                 sync_context += 1;
964         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
965
966         talloc_destroy(mem_ctx);
967
968         return result;
969 }
970
971 /* dump sam database via samsync rpc calls */
972 int rpc_vampire(int argc, const char **argv)
973 {
974         NTSTATUS result;
975         struct cli_state *cli = NULL;
976         uchar trust_password[16];
977         DOM_CRED ret_creds;
978         DOM_SID dom_sid;
979         uint32 sec_channel;
980
981         ZERO_STRUCT(ret_creds);
982
983         /* Connect to remote machine */
984         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS |
985                                             NET_FLAGS_PDC))) {
986                 return 1;
987         }
988
989         if (!cli_nt_session_open(cli, PI_NETLOGON)) {
990                 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
991                 goto fail;
992         }
993
994         if (!secrets_fetch_trust_account_password(lp_workgroup(),
995                                                   trust_password, NULL,
996                                                   &sec_channel)) {
997                 d_printf("Could not retrieve domain trust secret\n");
998                 goto fail;
999         }
1000         
1001         result = cli_nt_establish_netlogon(cli, sec_channel,  trust_password);
1002
1003         if (!NT_STATUS_IS_OK(result)) {
1004                 d_printf("Failed to setup BDC creds\n");
1005                 goto fail;
1006         }
1007
1008         dom_sid = *get_global_sam_sid();
1009         result = fetch_database(cli, SAM_DATABASE_DOMAIN, &ret_creds, dom_sid);
1010
1011         if (!NT_STATUS_IS_OK(result)) {
1012                 d_printf("Failed to fetch domain database: %s\n",
1013                          nt_errstr(result));
1014                 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED))
1015                         d_printf("Perhaps %s is a Windows 2000 native mode "
1016                                  "domain?\n", lp_workgroup());
1017                 goto fail;
1018         }
1019
1020         sid_copy(&dom_sid, &global_sid_Builtin);
1021
1022         result = fetch_database(cli, SAM_DATABASE_BUILTIN, &ret_creds, 
1023                                 dom_sid);
1024
1025         if (!NT_STATUS_IS_OK(result)) {
1026                 d_printf("Failed to fetch builtin database: %s\n",
1027                          nt_errstr(result));
1028                 goto fail;
1029         }       
1030
1031         /* Currently we crash on PRIVS somewhere in unmarshalling */
1032         /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */
1033
1034         cli_nt_session_close(cli);
1035         
1036         return 0;
1037
1038 fail:
1039         if (cli)
1040                 cli_nt_session_close(cli);
1041
1042         return -1;
1043 }