net_vampire: move ldif code out of net_rpc_samsync.c
[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    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
8    Modified by Volker Lendecke 2002
9    Copyright (C) Jeremy Allison 2005.
10    Copyright (C) Guenther Deschner 2008.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "utils/net.h"
28
29 static void display_group_mem_info(uint32_t rid,
30                                    struct netr_DELTA_GROUP_MEMBER *r)
31 {
32         int i;
33         d_printf("Group mem %u: ", rid);
34         for (i=0; i< r->num_rids; i++) {
35                 d_printf("%u ", r->rids[i]);
36         }
37         d_printf("\n");
38 }
39
40 static void display_alias_info(uint32_t rid,
41                                struct netr_DELTA_ALIAS *r)
42 {
43         d_printf("Alias '%s' ", r->alias_name.string);
44         d_printf("desc='%s' rid=%u\n", r->description.string, r->rid);
45 }
46
47 static void display_alias_mem(uint32_t rid,
48                               struct netr_DELTA_ALIAS_MEMBER *r)
49 {
50         int i;
51         d_printf("Alias rid %u: ", rid);
52         for (i=0; i< r->sids.num_sids; i++) {
53                 d_printf("%s ", sid_string_tos(r->sids.sids[i].sid));
54         }
55         d_printf("\n");
56 }
57
58 static void display_account_info(uint32_t rid,
59                                  struct netr_DELTA_USER *r)
60 {
61         fstring hex_nt_passwd, hex_lm_passwd;
62         uchar lm_passwd[16], nt_passwd[16];
63         static uchar zero_buf[16];
64
65         /* Decode hashes from password hash (if they are not NULL) */
66
67         if (memcmp(r->lmpassword.hash, zero_buf, 16) != 0) {
68                 sam_pwd_hash(r->rid, r->lmpassword.hash, lm_passwd, 0);
69                 pdb_sethexpwd(hex_lm_passwd, lm_passwd, r->acct_flags);
70         } else {
71                 pdb_sethexpwd(hex_lm_passwd, NULL, 0);
72         }
73
74         if (memcmp(r->ntpassword.hash, zero_buf, 16) != 0) {
75                 sam_pwd_hash(r->rid, r->ntpassword.hash, nt_passwd, 0);
76                 pdb_sethexpwd(hex_nt_passwd, nt_passwd, r->acct_flags);
77         } else {
78                 pdb_sethexpwd(hex_nt_passwd, NULL, 0);
79         }
80
81         printf("%s:%d:%s:%s:%s:LCT-0\n",
82                 r->account_name.string,
83                 r->rid, hex_lm_passwd, hex_nt_passwd,
84                 pdb_encode_acct_ctrl(r->acct_flags, NEW_PW_FORMAT_SPACE_PADDED_LEN));
85 }
86
87 static time_t uint64s_nt_time_to_unix_abs(const uint64 *src)
88 {
89         NTTIME nttime;
90         nttime = *src;
91         return nt_time_to_unix_abs(&nttime);
92 }
93
94 static NTSTATUS pull_netr_AcctLockStr(TALLOC_CTX *mem_ctx,
95                                       struct lsa_BinaryString *r,
96                                       struct netr_AcctLockStr **str_p)
97 {
98         struct netr_AcctLockStr *str;
99         enum ndr_err_code ndr_err;
100         DATA_BLOB blob;
101
102         if (!mem_ctx || !r || !str_p) {
103                 return NT_STATUS_INVALID_PARAMETER;
104         }
105
106         *str_p = NULL;
107
108         str = TALLOC_ZERO_P(mem_ctx, struct netr_AcctLockStr);
109         if (!str) {
110                 return NT_STATUS_NO_MEMORY;
111         }
112
113         blob = data_blob_const(r->array, r->length);
114
115         ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, str,
116                        (ndr_pull_flags_fn_t)ndr_pull_netr_AcctLockStr);
117         data_blob_free(&blob);
118
119         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
120                 return ndr_map_error2ntstatus(ndr_err);
121         }
122
123         *str_p = str;
124
125         return NT_STATUS_OK;
126 }
127
128 static void display_domain_info(struct netr_DELTA_DOMAIN *r)
129 {
130         time_t u_logout;
131         struct netr_AcctLockStr *lockstr = NULL;
132         NTSTATUS status;
133         TALLOC_CTX *mem_ctx = talloc_tos();
134
135         status = pull_netr_AcctLockStr(mem_ctx, &r->account_lockout,
136                                        &lockstr);
137         if (!NT_STATUS_IS_OK(status)) {
138                 d_printf("failed to pull account lockout string: %s\n",
139                         nt_errstr(status));
140         }
141
142         u_logout = uint64s_nt_time_to_unix_abs((const uint64 *)&r->force_logoff_time);
143
144         d_printf("Domain name: %s\n", r->domain_name.string);
145
146         d_printf("Minimal Password Length: %d\n", r->min_password_length);
147         d_printf("Password History Length: %d\n", r->password_history_length);
148
149         d_printf("Force Logoff: %d\n", (int)u_logout);
150
151         d_printf("Max Password Age: %s\n", display_time(r->max_password_age));
152         d_printf("Min Password Age: %s\n", display_time(r->min_password_age));
153
154         if (lockstr) {
155                 d_printf("Lockout Time: %s\n", display_time((NTTIME)lockstr->lockout_duration));
156                 d_printf("Lockout Reset Time: %s\n", display_time((NTTIME)lockstr->reset_count));
157                 d_printf("Bad Attempt Lockout: %d\n", lockstr->bad_attempt_lockout);
158         }
159
160         d_printf("User must logon to change password: %d\n", r->logon_to_chgpass);
161 }
162
163 static void display_group_info(uint32_t rid, struct netr_DELTA_GROUP *r)
164 {
165         d_printf("Group '%s' ", r->group_name.string);
166         d_printf("desc='%s', rid=%u\n", r->description.string, rid);
167 }
168
169 static NTSTATUS display_sam_entry(TALLOC_CTX *mem_ctx,
170                                   enum netr_SamDatabaseID database_id,
171                                   struct netr_DELTA_ENUM *r,
172                                   NTSTATUS status,
173                                   struct samsync_context *ctx)
174 {
175         union netr_DELTA_UNION u = r->delta_union;
176         union netr_DELTA_ID_UNION id = r->delta_id_union;
177
178         switch (r->delta_type) {
179         case NETR_DELTA_DOMAIN:
180                 display_domain_info(u.domain);
181                 break;
182         case NETR_DELTA_GROUP:
183                 display_group_info(id.rid, u.group);
184                 break;
185 #if 0
186         case NETR_DELTA_DELETE_GROUP:
187                 printf("Delete Group: %d\n",
188                         u.delete_account.unknown);
189                 break;
190         case NETR_DELTA_RENAME_GROUP:
191                 printf("Rename Group: %s -> %s\n",
192                         u.rename_group->OldName.string,
193                         u.rename_group->NewName.string);
194                 break;
195 #endif
196         case NETR_DELTA_USER:
197                 display_account_info(id.rid, u.user);
198                 break;
199 #if 0
200         case NETR_DELTA_DELETE_USER:
201                 printf("Delete User: %d\n",
202                         id.rid);
203                 break;
204         case NETR_DELTA_RENAME_USER:
205                 printf("Rename user: %s -> %s\n",
206                         u.rename_user->OldName.string,
207                         u.rename_user->NewName.string);
208                 break;
209 #endif
210         case NETR_DELTA_GROUP_MEMBER:
211                 display_group_mem_info(id.rid, u.group_member);
212                 break;
213         case NETR_DELTA_ALIAS:
214                 display_alias_info(id.rid, u.alias);
215                 break;
216 #if 0
217         case NETR_DELTA_DELETE_ALIAS:
218                 printf("Delete Alias: %d\n",
219                         id.rid);
220                 break;
221         case NETR_DELTA_RENAME_ALIAS:
222                 printf("Rename alias: %s -> %s\n",
223                         u.rename_alias->OldName.string,
224                         u.rename_alias->NewName.string);
225                 break;
226 #endif
227         case NETR_DELTA_ALIAS_MEMBER:
228                 display_alias_mem(id.rid, u.alias_member);
229                 break;
230 #if 0
231         case NETR_DELTA_POLICY:
232                 printf("Policy\n");
233                 break;
234         case NETR_DELTA_TRUSTED_DOMAIN:
235                 printf("Trusted Domain: %s\n",
236                         u.trusted_domain->domain_name.string);
237                 break;
238         case NETR_DELTA_DELETE_TRUST:
239                 printf("Delete Trust: %d\n",
240                         u.delete_trust.unknown);
241                 break;
242         case NETR_DELTA_ACCOUNT:
243                 printf("Account\n");
244                 break;
245         case NETR_DELTA_DELETE_ACCOUNT:
246                 printf("Delete Account: %d\n",
247                         u.delete_account.unknown);
248                 break;
249         case NETR_DELTA_SECRET:
250                 printf("Secret\n");
251                 break;
252         case NETR_DELTA_DELETE_SECRET:
253                 printf("Delete Secret: %d\n",
254                         u.delete_secret.unknown);
255                 break;
256         case NETR_DELTA_DELETE_GROUP2:
257                 printf("Delete Group2: %s\n",
258                         u.delete_group->account_name);
259                 break;
260         case NETR_DELTA_DELETE_USER2:
261                 printf("Delete User2: %s\n",
262                         u.delete_user->account_name);
263                 break;
264         case NETR_DELTA_MODIFY_COUNT:
265                 printf("sam sequence update: 0x%016llx\n",
266                         (unsigned long long) *u.modified_count);
267                 break;
268 #endif
269         /* The following types are recognised but not handled */
270         case NETR_DELTA_RENAME_GROUP:
271                 d_printf("NETR_DELTA_RENAME_GROUP not handled\n");
272                 break;
273         case NETR_DELTA_RENAME_USER:
274                 d_printf("NETR_DELTA_RENAME_USER not handled\n");
275                 break;
276         case NETR_DELTA_RENAME_ALIAS:
277                 d_printf("NETR_DELTA_RENAME_ALIAS not handled\n");
278                 break;
279         case NETR_DELTA_POLICY:
280                 d_printf("NETR_DELTA_POLICY not handled\n");
281                 break;
282         case NETR_DELTA_TRUSTED_DOMAIN:
283                 d_printf("NETR_DELTA_TRUSTED_DOMAIN not handled\n");
284                 break;
285         case NETR_DELTA_ACCOUNT:
286                 d_printf("NETR_DELTA_ACCOUNT not handled\n");
287                 break;
288         case NETR_DELTA_SECRET:
289                 d_printf("NETR_DELTA_SECRET not handled\n");
290                 break;
291         case NETR_DELTA_DELETE_GROUP:
292                 d_printf("NETR_DELTA_DELETE_GROUP not handled\n");
293                 break;
294         case NETR_DELTA_DELETE_USER:
295                 d_printf("NETR_DELTA_DELETE_USER not handled\n");
296                 break;
297         case NETR_DELTA_MODIFY_COUNT:
298                 d_printf("NETR_DELTA_MODIFY_COUNT not handled\n");
299                 break;
300         case NETR_DELTA_DELETE_ALIAS:
301                 d_printf("NETR_DELTA_DELETE_ALIAS not handled\n");
302                 break;
303         case NETR_DELTA_DELETE_TRUST:
304                 d_printf("NETR_DELTA_DELETE_TRUST not handled\n");
305                 break;
306         case NETR_DELTA_DELETE_ACCOUNT:
307                 d_printf("NETR_DELTA_DELETE_ACCOUNT not handled\n");
308                 break;
309         case NETR_DELTA_DELETE_SECRET:
310                 d_printf("NETR_DELTA_DELETE_SECRET not handled\n");
311                 break;
312         case NETR_DELTA_DELETE_GROUP2:
313                 d_printf("NETR_DELTA_DELETE_GROUP2 not handled\n");
314                 break;
315         case NETR_DELTA_DELETE_USER2:
316                 d_printf("NETR_DELTA_DELETE_USER2 not handled\n");
317                 break;
318         default:
319                 printf("unknown delta type 0x%02x\n",
320                         r->delta_type);
321                 break;
322         }
323
324         return NT_STATUS_OK;
325 }
326
327 static NTSTATUS display_sam_entries(TALLOC_CTX *mem_ctx,
328                                     enum netr_SamDatabaseID database_id,
329                                     struct netr_DELTA_ENUM_ARRAY *r,
330                                     NTSTATUS status,
331                                     struct samsync_context *ctx)
332 {
333         int i;
334
335         for (i = 0; i < r->num_deltas; i++) {
336                 display_sam_entry(mem_ctx, database_id, &r->delta_enum[i], status, ctx);
337         }
338
339         return NT_STATUS_OK;
340 }
341
342 /* dump sam database via samsync rpc calls */
343 NTSTATUS rpc_samdump_internals(struct net_context *c,
344                                 const DOM_SID *domain_sid,
345                                 const char *domain_name,
346                                 struct cli_state *cli,
347                                 struct rpc_pipe_client *pipe_hnd,
348                                 TALLOC_CTX *mem_ctx,
349                                 int argc,
350                                 const char **argv)
351 {
352         struct samsync_context *ctx = NULL;
353         NTSTATUS status;
354
355         status = samsync_init_context(mem_ctx,
356                                       domain_sid,
357                                       NET_SAMSYNC_MODE_DUMP,
358                                       &ctx);
359         if (!NT_STATUS_IS_OK(status)) {
360                 return status;
361         }
362
363         samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
364                                  display_sam_entries, ctx);
365
366         samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
367                                  display_sam_entries, ctx);
368
369         samsync_process_database(pipe_hnd, SAM_DATABASE_PRIVS,
370                                  display_sam_entries, ctx);
371
372         TALLOC_FREE(ctx);
373
374         return NT_STATUS_OK;
375 }
376
377 /* Convert a struct samu_DELTA to a struct samu. */
378 #define STRING_CHANGED (old_string && !new_string) ||\
379                     (!old_string && new_string) ||\
380                 (old_string && new_string && (strcmp(old_string, new_string) != 0))
381
382 #define STRING_CHANGED_NC(s1,s2) ((s1) && !(s2)) ||\
383                     (!(s1) && (s2)) ||\
384                 ((s1) && (s2) && (strcmp((s1), (s2)) != 0))
385
386 static NTSTATUS sam_account_from_delta(struct samu *account,
387                                        struct netr_DELTA_USER *r)
388 {
389         const char *old_string, *new_string;
390         time_t unix_time, stored_time;
391         uchar lm_passwd[16], nt_passwd[16];
392         static uchar zero_buf[16];
393
394         /* Username, fullname, home dir, dir drive, logon script, acct
395            desc, workstations, profile. */
396
397         if (r->account_name.string) {
398                 old_string = pdb_get_nt_username(account);
399                 new_string = r->account_name.string;
400
401                 if (STRING_CHANGED) {
402                         pdb_set_nt_username(account, new_string, PDB_CHANGED);
403                 }
404
405                 /* Unix username is the same - for sanity */
406                 old_string = pdb_get_username( account );
407                 if (STRING_CHANGED) {
408                         pdb_set_username(account, new_string, PDB_CHANGED);
409                 }
410         }
411
412         if (r->full_name.string) {
413                 old_string = pdb_get_fullname(account);
414                 new_string = r->full_name.string;
415
416                 if (STRING_CHANGED)
417                         pdb_set_fullname(account, new_string, PDB_CHANGED);
418         }
419
420         if (r->home_directory.string) {
421                 old_string = pdb_get_homedir(account);
422                 new_string = r->home_directory.string;
423
424                 if (STRING_CHANGED)
425                         pdb_set_homedir(account, new_string, PDB_CHANGED);
426         }
427
428         if (r->home_drive.string) {
429                 old_string = pdb_get_dir_drive(account);
430                 new_string = r->home_drive.string;
431
432                 if (STRING_CHANGED)
433                         pdb_set_dir_drive(account, new_string, PDB_CHANGED);
434         }
435
436         if (r->logon_script.string) {
437                 old_string = pdb_get_logon_script(account);
438                 new_string = r->logon_script.string;
439
440                 if (STRING_CHANGED)
441                         pdb_set_logon_script(account, new_string, PDB_CHANGED);
442         }
443
444         if (r->description.string) {
445                 old_string = pdb_get_acct_desc(account);
446                 new_string = r->description.string;
447
448                 if (STRING_CHANGED)
449                         pdb_set_acct_desc(account, new_string, PDB_CHANGED);
450         }
451
452         if (r->workstations.string) {
453                 old_string = pdb_get_workstations(account);
454                 new_string = r->workstations.string;
455
456                 if (STRING_CHANGED)
457                         pdb_set_workstations(account, new_string, PDB_CHANGED);
458         }
459
460         if (r->profile_path.string) {
461                 old_string = pdb_get_profile_path(account);
462                 new_string = r->profile_path.string;
463
464                 if (STRING_CHANGED)
465                         pdb_set_profile_path(account, new_string, PDB_CHANGED);
466         }
467
468         if (r->parameters.string) {
469                 DATA_BLOB mung;
470                 char *newstr;
471                 old_string = pdb_get_munged_dial(account);
472                 mung.length = r->parameters.length;
473                 mung.data = (uint8 *) r->parameters.string;
474                 newstr = (mung.length == 0) ? NULL :
475                         base64_encode_data_blob(talloc_tos(), mung);
476
477                 if (STRING_CHANGED_NC(old_string, newstr))
478                         pdb_set_munged_dial(account, newstr, PDB_CHANGED);
479                 TALLOC_FREE(newstr);
480         }
481
482         /* User and group sid */
483         if (pdb_get_user_rid(account) != r->rid)
484                 pdb_set_user_sid_from_rid(account, r->rid, PDB_CHANGED);
485         if (pdb_get_group_rid(account) != r->primary_gid)
486                 pdb_set_group_sid_from_rid(account, r->primary_gid, PDB_CHANGED);
487
488         /* Logon and password information */
489         if (!nt_time_is_zero(&r->last_logon)) {
490                 unix_time = nt_time_to_unix(r->last_logon);
491                 stored_time = pdb_get_logon_time(account);
492                 if (stored_time != unix_time)
493                         pdb_set_logon_time(account, unix_time, PDB_CHANGED);
494         }
495
496         if (!nt_time_is_zero(&r->last_logoff)) {
497                 unix_time = nt_time_to_unix(r->last_logoff);
498                 stored_time = pdb_get_logoff_time(account);
499                 if (stored_time != unix_time)
500                         pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
501         }
502
503         /* Logon Divs */
504         if (pdb_get_logon_divs(account) != r->logon_hours.units_per_week)
505                 pdb_set_logon_divs(account, r->logon_hours.units_per_week, PDB_CHANGED);
506
507 #if 0
508         /* no idea what to do with this one - gd */
509         /* Max Logon Hours */
510         if (delta->unknown1 != pdb_get_unknown_6(account)) {
511                 pdb_set_unknown_6(account, delta->unknown1, PDB_CHANGED);
512         }
513 #endif
514         /* Logon Hours Len */
515         if (r->logon_hours.units_per_week/8 != pdb_get_hours_len(account)) {
516                 pdb_set_hours_len(account, r->logon_hours.units_per_week/8, PDB_CHANGED);
517         }
518
519         /* Logon Hours */
520         if (r->logon_hours.bits) {
521                 char oldstr[44], newstr[44];
522                 pdb_sethexhours(oldstr, pdb_get_hours(account));
523                 pdb_sethexhours(newstr, r->logon_hours.bits);
524                 if (!strequal(oldstr, newstr))
525                         pdb_set_hours(account, r->logon_hours.bits, PDB_CHANGED);
526         }
527
528         if (pdb_get_bad_password_count(account) != r->bad_password_count)
529                 pdb_set_bad_password_count(account, r->bad_password_count, PDB_CHANGED);
530
531         if (pdb_get_logon_count(account) != r->logon_count)
532                 pdb_set_logon_count(account, r->logon_count, PDB_CHANGED);
533
534         if (!nt_time_is_zero(&r->last_password_change)) {
535                 unix_time = nt_time_to_unix(r->last_password_change);
536                 stored_time = pdb_get_pass_last_set_time(account);
537                 if (stored_time != unix_time)
538                         pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
539         } else {
540                 /* no last set time, make it now */
541                 pdb_set_pass_last_set_time(account, time(NULL), PDB_CHANGED);
542         }
543
544         if (!nt_time_is_zero(&r->acct_expiry)) {
545                 unix_time = nt_time_to_unix(r->acct_expiry);
546                 stored_time = pdb_get_kickoff_time(account);
547                 if (stored_time != unix_time)
548                         pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
549         }
550
551         /* Decode hashes from password hash
552            Note that win2000 may send us all zeros for the hashes if it doesn't
553            think this channel is secure enough - don't set the passwords at all
554            in that case
555         */
556         if (memcmp(r->ntpassword.hash, zero_buf, 16) != 0) {
557                 sam_pwd_hash(r->rid, r->ntpassword.hash, lm_passwd, 0);
558                 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
559         }
560
561         if (memcmp(r->lmpassword.hash, zero_buf, 16) != 0) {
562                 sam_pwd_hash(r->rid, r->lmpassword.hash, nt_passwd, 0);
563                 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
564         }
565
566         /* TODO: account expiry time */
567
568         pdb_set_acct_ctrl(account, r->acct_flags, PDB_CHANGED);
569
570         pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
571
572         return NT_STATUS_OK;
573 }
574
575 static NTSTATUS fetch_account_info(uint32_t rid,
576                                    struct netr_DELTA_USER *r)
577 {
578
579         NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL;
580         fstring account;
581         char *add_script = NULL;
582         struct samu *sam_account=NULL;
583         GROUP_MAP map;
584         struct group *grp;
585         DOM_SID user_sid;
586         DOM_SID group_sid;
587         struct passwd *passwd;
588         fstring sid_string;
589
590         fstrcpy(account, r->account_name.string);
591         d_printf("Creating account: %s\n", account);
592
593         if ( !(sam_account = samu_new( NULL )) ) {
594                 return NT_STATUS_NO_MEMORY;
595         }
596
597         if (!(passwd = Get_Pwnam_alloc(sam_account, account))) {
598                 /* Create appropriate user */
599                 if (r->acct_flags & ACB_NORMAL) {
600                         add_script = talloc_strdup(sam_account,
601                                         lp_adduser_script());
602                 } else if ( (r->acct_flags & ACB_WSTRUST) ||
603                             (r->acct_flags & ACB_SVRTRUST) ||
604                             (r->acct_flags & ACB_DOMTRUST) ) {
605                         add_script = talloc_strdup(sam_account,
606                                         lp_addmachine_script());
607                 } else {
608                         DEBUG(1, ("Unknown user type: %s\n",
609                                   pdb_encode_acct_ctrl(r->acct_flags, NEW_PW_FORMAT_SPACE_PADDED_LEN)));
610                         nt_ret = NT_STATUS_UNSUCCESSFUL;
611                         goto done;
612                 }
613                 if (!add_script) {
614                         nt_ret = NT_STATUS_NO_MEMORY;
615                         goto done;
616                 }
617                 if (*add_script) {
618                         int add_ret;
619                         add_script = talloc_all_string_sub(sam_account,
620                                         add_script,
621                                         "%u",
622                                         account);
623                         if (!add_script) {
624                                 nt_ret = NT_STATUS_NO_MEMORY;
625                                 goto done;
626                         }
627                         add_ret = smbrun(add_script,NULL);
628                         DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' "
629                                  "gave %d\n", add_script, add_ret));
630                         if (add_ret == 0) {
631                                 smb_nscd_flush_user_cache();
632                         }
633                 }
634
635                 /* try and find the possible unix account again */
636                 if ( !(passwd = Get_Pwnam_alloc(sam_account, account)) ) {
637                         d_fprintf(stderr, "Could not create posix account info for '%s'\n", account);
638                         nt_ret = NT_STATUS_NO_SUCH_USER;
639                         goto done;
640                 }
641         }
642
643         sid_copy(&user_sid, get_global_sam_sid());
644         sid_append_rid(&user_sid, r->rid);
645
646         DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n",
647                   sid_to_fstring(sid_string, &user_sid), account));
648         if (!pdb_getsampwsid(sam_account, &user_sid)) {
649                 sam_account_from_delta(sam_account, r);
650                 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
651                           sid_to_fstring(sid_string, &user_sid),
652                           pdb_get_username(sam_account)));
653                 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) {
654                         DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
655                                   account));
656                         return NT_STATUS_ACCESS_DENIED;
657                 }
658         } else {
659                 sam_account_from_delta(sam_account, r);
660                 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
661                           sid_to_fstring(sid_string, &user_sid),
662                           pdb_get_username(sam_account)));
663                 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) {
664                         DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
665                                   account));
666                         TALLOC_FREE(sam_account);
667                         return NT_STATUS_ACCESS_DENIED;
668                 }
669         }
670
671         if (pdb_get_group_sid(sam_account) == NULL) {
672                 return NT_STATUS_UNSUCCESSFUL;
673         }
674
675         group_sid = *pdb_get_group_sid(sam_account);
676
677         if (!pdb_getgrsid(&map, group_sid)) {
678                 DEBUG(0, ("Primary group of %s has no mapping!\n",
679                           pdb_get_username(sam_account)));
680         } else {
681                 if (map.gid != passwd->pw_gid) {
682                         if (!(grp = getgrgid(map.gid))) {
683                                 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
684                                           (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_tos(&group_sid)));
685                         } else {
686                                 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
687                         }
688                 }
689         }
690
691         if ( !passwd ) {
692                 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
693                         pdb_get_username(sam_account)));
694         }
695
696  done:
697         TALLOC_FREE(sam_account);
698         return nt_ret;
699 }
700
701 static NTSTATUS fetch_group_info(uint32_t rid,
702                                  struct netr_DELTA_GROUP *r)
703 {
704         fstring name;
705         fstring comment;
706         struct group *grp = NULL;
707         DOM_SID group_sid;
708         fstring sid_string;
709         GROUP_MAP map;
710         bool insert = true;
711
712         fstrcpy(name, r->group_name.string);
713         fstrcpy(comment, r->description.string);
714
715         /* add the group to the mapping table */
716         sid_copy(&group_sid, get_global_sam_sid());
717         sid_append_rid(&group_sid, rid);
718         sid_to_fstring(sid_string, &group_sid);
719
720         if (pdb_getgrsid(&map, group_sid)) {
721                 if ( map.gid != -1 )
722                         grp = getgrgid(map.gid);
723                 insert = false;
724         }
725
726         if (grp == NULL) {
727                 gid_t gid;
728
729                 /* No group found from mapping, find it from its name. */
730                 if ((grp = getgrnam(name)) == NULL) {
731
732                         /* No appropriate group found, create one */
733
734                         d_printf("Creating unix group: '%s'\n", name);
735
736                         if (smb_create_group(name, &gid) != 0)
737                                 return NT_STATUS_ACCESS_DENIED;
738
739                         if ((grp = getgrnam(name)) == NULL)
740                                 return NT_STATUS_ACCESS_DENIED;
741                 }
742         }
743
744         map.gid = grp->gr_gid;
745         map.sid = group_sid;
746         map.sid_name_use = SID_NAME_DOM_GRP;
747         fstrcpy(map.nt_name, name);
748         if (r->description.string) {
749                 fstrcpy(map.comment, comment);
750         } else {
751                 fstrcpy(map.comment, "");
752         }
753
754         if (insert)
755                 pdb_add_group_mapping_entry(&map);
756         else
757                 pdb_update_group_mapping_entry(&map);
758
759         return NT_STATUS_OK;
760 }
761
762 static NTSTATUS fetch_group_mem_info(uint32_t rid,
763                                      struct netr_DELTA_GROUP_MEMBER *r)
764 {
765         int i;
766         TALLOC_CTX *t = NULL;
767         char **nt_members = NULL;
768         char **unix_members;
769         DOM_SID group_sid;
770         GROUP_MAP map;
771         struct group *grp;
772
773         if (r->num_rids == 0) {
774                 return NT_STATUS_OK;
775         }
776
777         sid_copy(&group_sid, get_global_sam_sid());
778         sid_append_rid(&group_sid, rid);
779
780         if (!get_domain_group_from_sid(group_sid, &map)) {
781                 DEBUG(0, ("Could not find global group %d\n", rid));
782                 return NT_STATUS_NO_SUCH_GROUP;
783         }
784
785         if (!(grp = getgrgid(map.gid))) {
786                 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid));
787                 return NT_STATUS_NO_SUCH_GROUP;
788         }
789
790         d_printf("Group members of %s: ", grp->gr_name);
791
792         if (!(t = talloc_init("fetch_group_mem_info"))) {
793                 DEBUG(0, ("could not talloc_init\n"));
794                 return NT_STATUS_NO_MEMORY;
795         }
796
797         if (r->num_rids) {
798                 if ((nt_members = TALLOC_ZERO_ARRAY(t, char *, r->num_rids)) == NULL) {
799                         DEBUG(0, ("talloc failed\n"));
800                         talloc_free(t);
801                         return NT_STATUS_NO_MEMORY;
802                 }
803         } else {
804                 nt_members = NULL;
805         }
806
807         for (i=0; i < r->num_rids; i++) {
808                 struct samu *member = NULL;
809                 DOM_SID member_sid;
810
811                 if ( !(member = samu_new(t)) ) {
812                         talloc_destroy(t);
813                         return NT_STATUS_NO_MEMORY;
814                 }
815
816                 sid_copy(&member_sid, get_global_sam_sid());
817                 sid_append_rid(&member_sid, r->rids[i]);
818
819                 if (!pdb_getsampwsid(member, &member_sid)) {
820                         DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
821                                   r->rids[i], sid_string_tos(&member_sid), grp->gr_name));
822                         TALLOC_FREE(member);
823                         continue;
824                 }
825
826                 if (pdb_get_group_rid(member) == rid) {
827                         d_printf("%s(primary),", pdb_get_username(member));
828                         TALLOC_FREE(member);
829                         continue;
830                 }
831
832                 d_printf("%s,", pdb_get_username(member));
833                 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
834                 TALLOC_FREE(member);
835         }
836
837         d_printf("\n");
838
839         unix_members = grp->gr_mem;
840
841         while (*unix_members) {
842                 bool is_nt_member = false;
843                 for (i=0; i < r->num_rids; i++) {
844                         if (nt_members[i] == NULL) {
845                                 /* This was a primary group */
846                                 continue;
847                         }
848
849                         if (strcmp(*unix_members, nt_members[i]) == 0) {
850                                 is_nt_member = true;
851                                 break;
852                         }
853                 }
854                 if (!is_nt_member) {
855                         /* We look at a unix group member that is not
856                            an nt group member. So, remove it. NT is
857                            boss here. */
858                         smb_delete_user_group(grp->gr_name, *unix_members);
859                 }
860                 unix_members += 1;
861         }
862
863         for (i=0; i < r->num_rids; i++) {
864                 bool is_unix_member = false;
865
866                 if (nt_members[i] == NULL) {
867                         /* This was the primary group */
868                         continue;
869                 }
870
871                 unix_members = grp->gr_mem;
872
873                 while (*unix_members) {
874                         if (strcmp(*unix_members, nt_members[i]) == 0) {
875                                 is_unix_member = true;
876                                 break;
877                         }
878                         unix_members += 1;
879                 }
880
881                 if (!is_unix_member) {
882                         /* We look at a nt group member that is not a
883                            unix group member currently. So, add the nt
884                            group member. */
885                         smb_add_user_group(grp->gr_name, nt_members[i]);
886                 }
887         }
888
889         talloc_destroy(t);
890         return NT_STATUS_OK;
891 }
892
893 static NTSTATUS fetch_alias_info(uint32_t rid,
894                                  struct netr_DELTA_ALIAS *r,
895                                  const DOM_SID *dom_sid)
896 {
897         fstring name;
898         fstring comment;
899         struct group *grp = NULL;
900         DOM_SID alias_sid;
901         fstring sid_string;
902         GROUP_MAP map;
903         bool insert = true;
904
905         fstrcpy(name, r->alias_name.string);
906         fstrcpy(comment, r->description.string);
907
908         /* Find out whether the group is already mapped */
909         sid_copy(&alias_sid, dom_sid);
910         sid_append_rid(&alias_sid, rid);
911         sid_to_fstring(sid_string, &alias_sid);
912
913         if (pdb_getgrsid(&map, alias_sid)) {
914                 grp = getgrgid(map.gid);
915                 insert = false;
916         }
917
918         if (grp == NULL) {
919                 gid_t gid;
920
921                 /* No group found from mapping, find it from its name. */
922                 if ((grp = getgrnam(name)) == NULL) {
923                         /* No appropriate group found, create one */
924                         d_printf("Creating unix group: '%s'\n", name);
925                         if (smb_create_group(name, &gid) != 0)
926                                 return NT_STATUS_ACCESS_DENIED;
927                         if ((grp = getgrgid(gid)) == NULL)
928                                 return NT_STATUS_ACCESS_DENIED;
929                 }
930         }
931
932         map.gid = grp->gr_gid;
933         map.sid = alias_sid;
934
935         if (sid_equal(dom_sid, &global_sid_Builtin))
936                 map.sid_name_use = SID_NAME_WKN_GRP;
937         else
938                 map.sid_name_use = SID_NAME_ALIAS;
939
940         fstrcpy(map.nt_name, name);
941         fstrcpy(map.comment, comment);
942
943         if (insert)
944                 pdb_add_group_mapping_entry(&map);
945         else
946                 pdb_update_group_mapping_entry(&map);
947
948         return NT_STATUS_OK;
949 }
950
951 static NTSTATUS fetch_alias_mem(uint32_t rid,
952                                 struct netr_DELTA_ALIAS_MEMBER *r,
953                                 const DOM_SID *dom_sid)
954 {
955         return NT_STATUS_OK;
956 }
957
958 static NTSTATUS fetch_domain_info(uint32_t rid,
959                                   struct netr_DELTA_DOMAIN *r)
960 {
961         time_t u_max_age, u_min_age, u_logout;
962         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
963         const char *domname;
964         struct netr_AcctLockStr *lockstr = NULL;
965         NTSTATUS status;
966         TALLOC_CTX *mem_ctx = talloc_tos();
967
968         status = pull_netr_AcctLockStr(mem_ctx, &r->account_lockout,
969                                        &lockstr);
970         if (!NT_STATUS_IS_OK(status)) {
971                 d_printf("failed to pull account lockout string: %s\n",
972                         nt_errstr(status));
973         }
974
975         u_max_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->max_password_age);
976         u_min_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->min_password_age);
977         u_logout = uint64s_nt_time_to_unix_abs((uint64 *)&r->force_logoff_time);
978
979         domname = r->domain_name.string;
980         if (!domname) {
981                 return NT_STATUS_NO_MEMORY;
982         }
983
984         /* we don't handle BUILTIN account policies */
985         if (!strequal(domname, get_global_sam_name())) {
986                 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname);
987                 return NT_STATUS_OK;
988         }
989
990
991         if (!pdb_set_account_policy(AP_PASSWORD_HISTORY,
992                                     r->password_history_length))
993                 return nt_status;
994
995         if (!pdb_set_account_policy(AP_MIN_PASSWORD_LEN,
996                                     r->min_password_length))
997                 return nt_status;
998
999         if (!pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (uint32)u_max_age))
1000                 return nt_status;
1001
1002         if (!pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (uint32)u_min_age))
1003                 return nt_status;
1004
1005         if (!pdb_set_account_policy(AP_TIME_TO_LOGOUT, (uint32)u_logout))
1006                 return nt_status;
1007
1008         if (lockstr) {
1009                 time_t u_lockoutreset, u_lockouttime;
1010
1011                 u_lockoutreset = uint64s_nt_time_to_unix_abs(&lockstr->reset_count);
1012                 u_lockouttime = uint64s_nt_time_to_unix_abs((uint64_t *)&lockstr->lockout_duration);
1013
1014                 if (!pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT,
1015                                             lockstr->bad_attempt_lockout))
1016                         return nt_status;
1017
1018                 if (!pdb_set_account_policy(AP_RESET_COUNT_TIME, (uint32_t)u_lockoutreset/60))
1019                         return nt_status;
1020
1021                 if (u_lockouttime != -1)
1022                         u_lockouttime /= 60;
1023
1024                 if (!pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (uint32_t)u_lockouttime))
1025                         return nt_status;
1026         }
1027
1028         if (!pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
1029                                     r->logon_to_chgpass))
1030                 return nt_status;
1031
1032         return NT_STATUS_OK;
1033 }
1034
1035 static NTSTATUS fetch_sam_entry(TALLOC_CTX *mem_ctx,
1036                                 enum netr_SamDatabaseID database_id,
1037                                 struct netr_DELTA_ENUM *r,
1038                                 struct samsync_context *ctx)
1039 {
1040         switch(r->delta_type) {
1041         case NETR_DELTA_USER:
1042                 fetch_account_info(r->delta_id_union.rid,
1043                                    r->delta_union.user);
1044                 break;
1045         case NETR_DELTA_GROUP:
1046                 fetch_group_info(r->delta_id_union.rid,
1047                                  r->delta_union.group);
1048                 break;
1049         case NETR_DELTA_GROUP_MEMBER:
1050                 fetch_group_mem_info(r->delta_id_union.rid,
1051                                      r->delta_union.group_member);
1052                 break;
1053         case NETR_DELTA_ALIAS:
1054                 fetch_alias_info(r->delta_id_union.rid,
1055                                  r->delta_union.alias,
1056                                  ctx->domain_sid);
1057                 break;
1058         case NETR_DELTA_ALIAS_MEMBER:
1059                 fetch_alias_mem(r->delta_id_union.rid,
1060                                 r->delta_union.alias_member,
1061                                 ctx->domain_sid);
1062                 break;
1063         case NETR_DELTA_DOMAIN:
1064                 fetch_domain_info(r->delta_id_union.rid,
1065                                   r->delta_union.domain);
1066                 break;
1067         /* The following types are recognised but not handled */
1068         case NETR_DELTA_RENAME_GROUP:
1069                 d_printf("NETR_DELTA_RENAME_GROUP not handled\n");
1070                 break;
1071         case NETR_DELTA_RENAME_USER:
1072                 d_printf("NETR_DELTA_RENAME_USER not handled\n");
1073                 break;
1074         case NETR_DELTA_RENAME_ALIAS:
1075                 d_printf("NETR_DELTA_RENAME_ALIAS not handled\n");
1076                 break;
1077         case NETR_DELTA_POLICY:
1078                 d_printf("NETR_DELTA_POLICY not handled\n");
1079                 break;
1080         case NETR_DELTA_TRUSTED_DOMAIN:
1081                 d_printf("NETR_DELTA_TRUSTED_DOMAIN not handled\n");
1082                 break;
1083         case NETR_DELTA_ACCOUNT:
1084                 d_printf("NETR_DELTA_ACCOUNT not handled\n");
1085                 break;
1086         case NETR_DELTA_SECRET:
1087                 d_printf("NETR_DELTA_SECRET not handled\n");
1088                 break;
1089         case NETR_DELTA_DELETE_GROUP:
1090                 d_printf("NETR_DELTA_DELETE_GROUP not handled\n");
1091                 break;
1092         case NETR_DELTA_DELETE_USER:
1093                 d_printf("NETR_DELTA_DELETE_USER not handled\n");
1094                 break;
1095         case NETR_DELTA_MODIFY_COUNT:
1096                 d_printf("NETR_DELTA_MODIFY_COUNT not handled\n");
1097                 break;
1098         case NETR_DELTA_DELETE_ALIAS:
1099                 d_printf("NETR_DELTA_DELETE_ALIAS not handled\n");
1100                 break;
1101         case NETR_DELTA_DELETE_TRUST:
1102                 d_printf("NETR_DELTA_DELETE_TRUST not handled\n");
1103                 break;
1104         case NETR_DELTA_DELETE_ACCOUNT:
1105                 d_printf("NETR_DELTA_DELETE_ACCOUNT not handled\n");
1106                 break;
1107         case NETR_DELTA_DELETE_SECRET:
1108                 d_printf("NETR_DELTA_DELETE_SECRET not handled\n");
1109                 break;
1110         case NETR_DELTA_DELETE_GROUP2:
1111                 d_printf("NETR_DELTA_DELETE_GROUP2 not handled\n");
1112                 break;
1113         case NETR_DELTA_DELETE_USER2:
1114                 d_printf("NETR_DELTA_DELETE_USER2 not handled\n");
1115                 break;
1116         default:
1117                 d_printf("Unknown delta record type %d\n", r->delta_type);
1118                 break;
1119         }
1120
1121         return NT_STATUS_OK;
1122 }
1123
1124 static NTSTATUS fetch_sam_entries(TALLOC_CTX *mem_ctx,
1125                                   enum netr_SamDatabaseID database_id,
1126                                   struct netr_DELTA_ENUM_ARRAY *r,
1127                                   NTSTATUS status,
1128                                   struct samsync_context *ctx)
1129 {
1130         int i;
1131
1132         for (i = 0; i < r->num_deltas; i++) {
1133                 fetch_sam_entry(mem_ctx, database_id, &r->delta_enum[i], ctx);
1134         }
1135
1136         return NT_STATUS_OK;
1137 }
1138
1139 /**
1140  * Basic usage function for 'net rpc vampire'
1141  *
1142  * @param c     A net_context structure
1143  * @param argc  Standard main() style argc
1144  * @param argc  Standard main() style argv.  Initial components are already
1145  *              stripped
1146  **/
1147
1148 int rpc_vampire_usage(struct net_context *c, int argc, const char **argv)
1149 {
1150         d_printf("net rpc vampire [ldif [<ldif-filename>] [options]\n"
1151                  "\t to pull accounts from a remote PDC where we are a BDC\n"
1152                  "\t\t no args puts accounts in local passdb from smb.conf\n"
1153                  "\t\t ldif - put accounts in ldif format (file defaults to "
1154                  "/tmp/tmp.ldif\n");
1155
1156         net_common_flags_usage(c, argc, argv);
1157         return -1;
1158 }
1159
1160
1161 /* dump sam database via samsync rpc calls */
1162 NTSTATUS rpc_vampire_internals(struct net_context *c,
1163                                 const DOM_SID *domain_sid,
1164                                 const char *domain_name,
1165                                 struct cli_state *cli,
1166                                 struct rpc_pipe_client *pipe_hnd,
1167                                 TALLOC_CTX *mem_ctx,
1168                                 int argc,
1169                                 const char **argv)
1170 {
1171         NTSTATUS result;
1172         struct samsync_context *ctx = NULL;
1173
1174         result = samsync_init_context(mem_ctx,
1175                                       domain_sid,
1176                                       NET_SAMSYNC_MODE_FETCH_PASSDB,
1177                                       &ctx);
1178         if (!NT_STATUS_IS_OK(result)) {
1179                 return result;
1180         }
1181
1182         if (!sid_equal(domain_sid, get_global_sam_sid())) {
1183                 d_printf("Cannot import users from %s at this time, "
1184                          "as the current domain:\n\t%s: %s\nconflicts "
1185                          "with the remote domain\n\t%s: %s\n"
1186                          "Perhaps you need to set: \n\n\tsecurity=user\n\t"
1187                          "workgroup=%s\n\n in your smb.conf?\n",
1188                          domain_name,
1189                          get_global_sam_name(),
1190                          sid_string_dbg(get_global_sam_sid()),
1191                          domain_name,
1192                          sid_string_dbg(domain_sid),
1193                          domain_name);
1194                 return NT_STATUS_UNSUCCESSFUL;
1195         }
1196
1197         /* fetch domain */
1198         result = samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
1199                                           fetch_sam_entries, ctx);
1200         if (!NT_STATUS_IS_OK(result)) {
1201                 d_fprintf(stderr, "Failed to fetch domain database: %s\n",
1202                           nt_errstr(result));
1203                 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED))
1204                         d_fprintf(stderr, "Perhaps %s is a Windows 2000 "
1205                                   "native mode domain?\n", domain_name);
1206                 goto fail;
1207         }
1208
1209         /* fetch builtin */
1210         ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
1211         ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
1212         result = samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
1213                                           fetch_sam_entries, ctx);
1214         if (!NT_STATUS_IS_OK(result)) {
1215                 d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
1216                           nt_errstr(result));
1217                 goto fail;
1218         }
1219
1220         TALLOC_FREE(ctx);
1221
1222  fail:
1223         return result;
1224 }
1225
1226 NTSTATUS rpc_vampire_ldif_internals(struct net_context *c,
1227                                     const DOM_SID *domain_sid,
1228                                     const char *domain_name,
1229                                     struct cli_state *cli,
1230                                     struct rpc_pipe_client *pipe_hnd,
1231                                     TALLOC_CTX *mem_ctx,
1232                                     int argc,
1233                                     const char **argv)
1234 {
1235         NTSTATUS status;
1236         struct samsync_context *ctx = NULL;
1237
1238         status = samsync_init_context(mem_ctx,
1239                                       domain_sid,
1240                                       NET_SAMSYNC_MODE_FETCH_LDIF,
1241                                       &ctx);
1242         if (!NT_STATUS_IS_OK(status)) {
1243                 return status;
1244         }
1245
1246         if (argc >= 1) {
1247                 ctx->ldif_filename = argv[1];
1248         }
1249
1250         /* fetch domain */
1251         status = samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
1252                                           fetch_sam_entries_ldif, ctx);
1253         if (!NT_STATUS_IS_OK(status)) {
1254                 d_fprintf(stderr, "Failed to fetch domain database: %s\n",
1255                           nt_errstr(status));
1256                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED))
1257                         d_fprintf(stderr, "Perhaps %s is a Windows 2000 "
1258                                   "native mode domain?\n", domain_name);
1259                 goto fail;
1260         }
1261
1262         /* fetch builtin */
1263         ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
1264         ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
1265         status = samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
1266                                           fetch_sam_entries_ldif, ctx);
1267         if (!NT_STATUS_IS_OK(status)) {
1268                 d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
1269                           nt_errstr(status));
1270                 goto fail;
1271         }
1272
1273         TALLOC_FREE(ctx);
1274
1275  fail:
1276         return status;
1277 }
1278
1279 int rpc_vampire_ldif(struct net_context *c, int argc, const char **argv)
1280 {
1281         if (c->display_usage) {
1282                 d_printf("Usage\n"
1283                          "net rpc vampire ldif\n"
1284                          "    Dump remote SAM database to LDIF file or stdout\n");
1285                 return 0;
1286         }
1287
1288         return run_rpc_command(c, NULL, PI_NETLOGON, 0, rpc_vampire_ldif_internals,
1289                                argc, argv);
1290 }