r16418: Pull in more Klocwork fixes (up to r16415)
[Samba/gbeck.git] / source / utils / net_rpc_samsync.c
blob85b086a02ff2256bed57dd56149b8a15d03a669c
1 /*
2 Unix SMB/CIFS implementation.
3 dump the remote SAM using rpc samsync operations
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.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
27 #include "utils/net.h"
29 /* uid's and gid's for writing deltas to ldif */
30 static uint32 ldif_gid = 999;
31 static uint32 ldif_uid = 999;
32 /* Kkeep track of ldap initialization */
33 static int init_ldap = 1;
35 static void display_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *g)
37 int i;
38 d_printf("Group mem %u: ", rid);
39 for (i=0;i<g->num_members;i++) {
40 d_printf("%u ", g->rids[i]);
42 d_printf("\n");
45 static const char *display_time(NTTIME *nttime)
47 static fstring string;
49 float high;
50 float low;
51 int sec;
52 int days, hours, mins, secs;
53 int offset = 1;
55 if (nttime->high==0 && nttime->low==0)
56 return "Now";
58 if (nttime->high==0x80000000 && nttime->low==0)
59 return "Never";
61 high = 65536;
62 high = high/10000;
63 high = high*65536;
64 high = high/1000;
65 high = high * (~nttime->high);
67 low = ~nttime->low;
68 low = low/(1000*1000*10);
70 sec=high+low;
71 sec+=offset;
73 days=sec/(60*60*24);
74 hours=(sec - (days*60*60*24)) / (60*60);
75 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
76 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
78 fstr_sprintf(string, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs);
79 return (string);
83 static void display_alias_info(uint32 rid, SAM_ALIAS_INFO *a)
85 d_printf("Alias '%s' ", unistr2_static(&a->uni_als_name));
86 d_printf("desc='%s' rid=%u\n", unistr2_static(&a->uni_als_desc), a->als_rid);
89 static void display_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *a)
91 int i;
92 d_printf("Alias rid %u: ", rid);
93 for (i=0;i<a->num_members;i++) {
94 d_printf("%s ", sid_string_static(&a->sids[i].sid));
96 d_printf("\n");
99 static void display_account_info(uint32 rid, SAM_ACCOUNT_INFO *a)
101 fstring hex_nt_passwd, hex_lm_passwd;
102 uchar lm_passwd[16], nt_passwd[16];
103 static uchar zero_buf[16];
105 /* Decode hashes from password hash (if they are not NULL) */
107 if (memcmp(a->pass.buf_lm_pwd, zero_buf, 16) != 0) {
108 sam_pwd_hash(a->user_rid, a->pass.buf_lm_pwd, lm_passwd, 0);
109 pdb_sethexpwd(hex_lm_passwd, lm_passwd, a->acb_info);
110 } else {
111 pdb_sethexpwd(hex_lm_passwd, NULL, 0);
114 if (memcmp(a->pass.buf_nt_pwd, zero_buf, 16) != 0) {
115 sam_pwd_hash(a->user_rid, a->pass.buf_nt_pwd, nt_passwd, 0);
116 pdb_sethexpwd(hex_nt_passwd, nt_passwd, a->acb_info);
117 } else {
118 pdb_sethexpwd(hex_nt_passwd, NULL, 0);
121 printf("%s:%d:%s:%s:%s:LCT-0\n", unistr2_static(&a->uni_acct_name),
122 a->user_rid, hex_lm_passwd, hex_nt_passwd,
123 pdb_encode_acct_ctrl(a->acb_info, NEW_PW_FORMAT_SPACE_PADDED_LEN));
126 static void display_domain_info(SAM_DOMAIN_INFO *a)
128 time_t u_logout;
130 u_logout = nt_time_to_unix_abs((NTTIME *)&a->force_logoff);
132 d_printf("Domain name: %s\n", unistr2_static(&a->uni_dom_name));
134 d_printf("Minimal Password Length: %d\n", a->min_pwd_len);
135 d_printf("Password History Length: %d\n", a->pwd_history_len);
137 d_printf("Force Logoff: %d\n", (int)u_logout);
139 d_printf("Max Password Age: %s\n", display_time((NTTIME *)&a->max_pwd_age));
140 d_printf("Min Password Age: %s\n", display_time((NTTIME *)&a->min_pwd_age));
142 d_printf("Lockout Time: %s\n", display_time((NTTIME *)&a->account_lockout.lockout_duration));
143 d_printf("Lockout Reset Time: %s\n", display_time((NTTIME *)&a->account_lockout.reset_count));
145 d_printf("Bad Attempt Lockout: %d\n", a->account_lockout.bad_attempt_lockout);
146 d_printf("User must logon to change password: %d\n", a->logon_chgpass);
149 static void display_group_info(uint32 rid, SAM_GROUP_INFO *a)
151 d_printf("Group '%s' ", unistr2_static(&a->uni_grp_name));
152 d_printf("desc='%s', rid=%u\n", unistr2_static(&a->uni_grp_desc), rid);
155 static void display_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta)
157 switch (hdr_delta->type) {
158 case SAM_DELTA_ACCOUNT_INFO:
159 display_account_info(hdr_delta->target_rid, &delta->account_info);
160 break;
161 case SAM_DELTA_GROUP_MEM:
162 display_group_mem_info(hdr_delta->target_rid, &delta->grp_mem_info);
163 break;
164 case SAM_DELTA_ALIAS_INFO:
165 display_alias_info(hdr_delta->target_rid, &delta->alias_info);
166 break;
167 case SAM_DELTA_ALIAS_MEM:
168 display_alias_mem(hdr_delta->target_rid, &delta->als_mem_info);
169 break;
170 case SAM_DELTA_DOMAIN_INFO:
171 display_domain_info(&delta->domain_info);
172 break;
173 case SAM_DELTA_GROUP_INFO:
174 display_group_info(hdr_delta->target_rid, &delta->group_info);
175 break;
176 /* The following types are recognised but not handled */
177 case SAM_DELTA_RENAME_GROUP:
178 d_printf("SAM_DELTA_RENAME_GROUP not handled\n");
179 break;
180 case SAM_DELTA_RENAME_USER:
181 d_printf("SAM_DELTA_RENAME_USER not handled\n");
182 break;
183 case SAM_DELTA_RENAME_ALIAS:
184 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
185 break;
186 case SAM_DELTA_POLICY_INFO:
187 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
188 break;
189 case SAM_DELTA_TRUST_DOMS:
190 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
191 break;
192 case SAM_DELTA_PRIVS_INFO:
193 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
194 break;
195 case SAM_DELTA_SECRET_INFO:
196 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
197 break;
198 case SAM_DELTA_DELETE_GROUP:
199 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
200 break;
201 case SAM_DELTA_DELETE_USER:
202 d_printf("SAM_DELTA_DELETE_USER not handled\n");
203 break;
204 case SAM_DELTA_MODIFIED_COUNT:
205 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
206 break;
207 default:
208 d_printf("Unknown delta record type %d\n", hdr_delta->type);
209 break;
213 static void dump_database(struct rpc_pipe_client *pipe_hnd, uint32 db_type)
215 uint32 sync_context = 0;
216 NTSTATUS result;
217 int i;
218 TALLOC_CTX *mem_ctx;
219 SAM_DELTA_HDR *hdr_deltas;
220 SAM_DELTA_CTR *deltas;
221 uint32 num_deltas;
223 if (!(mem_ctx = talloc_init("dump_database"))) {
224 return;
227 switch( db_type ) {
228 case SAM_DATABASE_DOMAIN:
229 d_printf("Dumping DOMAIN database\n");
230 break;
231 case SAM_DATABASE_BUILTIN:
232 d_printf("Dumping BUILTIN database\n");
233 break;
234 case SAM_DATABASE_PRIVS:
235 d_printf("Dumping PRIVS databases\n");
236 break;
237 default:
238 d_printf("Dumping unknown database type %u\n", db_type );
239 break;
242 do {
243 result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx, db_type,
244 sync_context,
245 &num_deltas, &hdr_deltas, &deltas);
246 if (NT_STATUS_IS_ERR(result))
247 break;
249 for (i = 0; i < num_deltas; i++) {
250 display_sam_entry(&hdr_deltas[i], &deltas[i]);
252 sync_context += 1;
253 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
255 talloc_destroy(mem_ctx);
258 /* dump sam database via samsync rpc calls */
259 NTSTATUS rpc_samdump_internals(const DOM_SID *domain_sid,
260 const char *domain_name,
261 struct cli_state *cli,
262 struct rpc_pipe_client *pipe_hnd,
263 TALLOC_CTX *mem_ctx,
264 int argc,
265 const char **argv)
267 #if 0
268 /* net_rpc.c now always tries to create an schannel pipe.. */
270 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
271 uchar trust_password[16];
272 uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS;
273 uint32 sec_channel_type = 0;
275 if (!secrets_fetch_trust_account_password(domain_name,
276 trust_password,
277 NULL, &sec_channel_type)) {
278 DEBUG(0,("Could not fetch trust account password\n"));
279 goto fail;
282 nt_status = rpccli_netlogon_setup_creds(pipe_hnd,
283 cli->desthost,
284 domain_name,
285 global_myname(),
286 trust_password,
287 sec_channel_type,
288 &neg_flags);
290 if (!NT_STATUS_IS_OK(nt_status)) {
291 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
292 goto fail;
294 #endif
296 dump_database(pipe_hnd, SAM_DATABASE_DOMAIN);
297 dump_database(pipe_hnd, SAM_DATABASE_BUILTIN);
298 dump_database(pipe_hnd, SAM_DATABASE_PRIVS);
300 return NT_STATUS_OK;
303 /* Convert a struct samu_DELTA to a struct samu. */
304 #define STRING_CHANGED (old_string && !new_string) ||\
305 (!old_string && new_string) ||\
306 (old_string && new_string && (strcmp(old_string, new_string) != 0))
308 #define STRING_CHANGED_NC(s1,s2) ((s1) && !(s2)) ||\
309 (!(s1) && (s2)) ||\
310 ((s1) && (s2) && (strcmp((s1), (s2)) != 0))
312 static NTSTATUS sam_account_from_delta(struct samu *account, SAM_ACCOUNT_INFO *delta)
314 const char *old_string, *new_string;
315 time_t unix_time, stored_time;
316 uchar lm_passwd[16], nt_passwd[16];
317 static uchar zero_buf[16];
319 /* Username, fullname, home dir, dir drive, logon script, acct
320 desc, workstations, profile. */
322 if (delta->hdr_acct_name.buffer) {
323 old_string = pdb_get_nt_username(account);
324 new_string = unistr2_static(&delta->uni_acct_name);
326 if (STRING_CHANGED) {
327 pdb_set_nt_username(account, new_string, PDB_CHANGED);
331 /* Unix username is the same - for sanity */
332 old_string = pdb_get_username( account );
333 if (STRING_CHANGED) {
334 pdb_set_username(account, new_string, PDB_CHANGED);
338 if (delta->hdr_full_name.buffer) {
339 old_string = pdb_get_fullname(account);
340 new_string = unistr2_static(&delta->uni_full_name);
342 if (STRING_CHANGED)
343 pdb_set_fullname(account, new_string, PDB_CHANGED);
346 if (delta->hdr_home_dir.buffer) {
347 old_string = pdb_get_homedir(account);
348 new_string = unistr2_static(&delta->uni_home_dir);
350 if (STRING_CHANGED)
351 pdb_set_homedir(account, new_string, PDB_CHANGED);
354 if (delta->hdr_dir_drive.buffer) {
355 old_string = pdb_get_dir_drive(account);
356 new_string = unistr2_static(&delta->uni_dir_drive);
358 if (STRING_CHANGED)
359 pdb_set_dir_drive(account, new_string, PDB_CHANGED);
362 if (delta->hdr_logon_script.buffer) {
363 old_string = pdb_get_logon_script(account);
364 new_string = unistr2_static(&delta->uni_logon_script);
366 if (STRING_CHANGED)
367 pdb_set_logon_script(account, new_string, PDB_CHANGED);
370 if (delta->hdr_acct_desc.buffer) {
371 old_string = pdb_get_acct_desc(account);
372 new_string = unistr2_static(&delta->uni_acct_desc);
374 if (STRING_CHANGED)
375 pdb_set_acct_desc(account, new_string, PDB_CHANGED);
378 if (delta->hdr_workstations.buffer) {
379 old_string = pdb_get_workstations(account);
380 new_string = unistr2_static(&delta->uni_workstations);
382 if (STRING_CHANGED)
383 pdb_set_workstations(account, new_string, PDB_CHANGED);
386 if (delta->hdr_profile.buffer) {
387 old_string = pdb_get_profile_path(account);
388 new_string = unistr2_static(&delta->uni_profile);
390 if (STRING_CHANGED)
391 pdb_set_profile_path(account, new_string, PDB_CHANGED);
394 if (delta->hdr_parameters.buffer) {
395 DATA_BLOB mung;
396 char *newstr;
397 old_string = pdb_get_munged_dial(account);
398 mung.length = delta->hdr_parameters.uni_str_len;
399 mung.data = (uint8 *) delta->uni_parameters.buffer;
400 newstr = (mung.length == 0) ? NULL : base64_encode_data_blob(mung);
402 if (STRING_CHANGED_NC(old_string, newstr))
403 pdb_set_munged_dial(account, newstr, PDB_CHANGED);
404 SAFE_FREE(newstr);
407 /* User and group sid */
408 if (pdb_get_user_rid(account) != delta->user_rid)
409 pdb_set_user_sid_from_rid(account, delta->user_rid, PDB_CHANGED);
410 if (pdb_get_group_rid(account) != delta->group_rid)
411 pdb_set_group_sid_from_rid(account, delta->group_rid, PDB_CHANGED);
413 /* Logon and password information */
414 if (!nt_time_is_zero(&delta->logon_time)) {
415 unix_time = nt_time_to_unix(&delta->logon_time);
416 stored_time = pdb_get_logon_time(account);
417 if (stored_time != unix_time)
418 pdb_set_logon_time(account, unix_time, PDB_CHANGED);
421 if (!nt_time_is_zero(&delta->logoff_time)) {
422 unix_time = nt_time_to_unix(&delta->logoff_time);
423 stored_time = pdb_get_logoff_time(account);
424 if (stored_time != unix_time)
425 pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
428 /* Logon Divs */
429 if (pdb_get_logon_divs(account) != delta->logon_divs)
430 pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED);
432 /* Max Logon Hours */
433 if (delta->unknown1 != pdb_get_unknown_6(account)) {
434 pdb_set_unknown_6(account, delta->unknown1, PDB_CHANGED);
437 /* Logon Hours Len */
438 if (delta->buf_logon_hrs.buf_len != pdb_get_hours_len(account)) {
439 pdb_set_hours_len(account, delta->buf_logon_hrs.buf_len, PDB_CHANGED);
442 /* Logon Hours */
443 if (delta->buf_logon_hrs.buffer) {
444 pstring oldstr, newstr;
445 pdb_sethexhours(oldstr, pdb_get_hours(account));
446 pdb_sethexhours(newstr, delta->buf_logon_hrs.buffer);
447 if (!strequal(oldstr, newstr))
448 pdb_set_hours(account, (const uint8 *)delta->buf_logon_hrs.buffer, PDB_CHANGED);
451 if (pdb_get_bad_password_count(account) != delta->bad_pwd_count)
452 pdb_set_bad_password_count(account, delta->bad_pwd_count, PDB_CHANGED);
454 if (pdb_get_logon_count(account) != delta->logon_count)
455 pdb_set_logon_count(account, delta->logon_count, PDB_CHANGED);
457 if (!nt_time_is_zero(&delta->pwd_last_set_time)) {
458 unix_time = nt_time_to_unix(&delta->pwd_last_set_time);
459 stored_time = pdb_get_pass_last_set_time(account);
460 if (stored_time != unix_time)
461 pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
462 } else {
463 /* no last set time, make it now */
464 pdb_set_pass_last_set_time(account, time(NULL), PDB_CHANGED);
467 #if 0
468 /* No kickoff time in the delta? */
469 if (!nt_time_is_zero(&delta->kickoff_time)) {
470 unix_time = nt_time_to_unix(&delta->kickoff_time);
471 stored_time = pdb_get_kickoff_time(account);
472 if (stored_time != unix_time)
473 pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
475 #endif
477 /* Decode hashes from password hash
478 Note that win2000 may send us all zeros for the hashes if it doesn't
479 think this channel is secure enough - don't set the passwords at all
480 in that case
482 if (memcmp(delta->pass.buf_lm_pwd, zero_buf, 16) != 0) {
483 sam_pwd_hash(delta->user_rid, delta->pass.buf_lm_pwd, lm_passwd, 0);
484 pdb_set_lanman_passwd(account, lm_passwd, PDB_CHANGED);
487 if (memcmp(delta->pass.buf_nt_pwd, zero_buf, 16) != 0) {
488 sam_pwd_hash(delta->user_rid, delta->pass.buf_nt_pwd, nt_passwd, 0);
489 pdb_set_nt_passwd(account, nt_passwd, PDB_CHANGED);
492 /* TODO: account expiry time */
494 pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED);
496 pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
498 return NT_STATUS_OK;
501 static NTSTATUS fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta)
503 NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL;
504 fstring account;
505 pstring add_script;
506 struct samu *sam_account=NULL;
507 GROUP_MAP map;
508 struct group *grp;
509 DOM_SID user_sid;
510 DOM_SID group_sid;
511 struct passwd *passwd;
512 fstring sid_string;
514 fstrcpy(account, unistr2_static(&delta->uni_acct_name));
515 d_printf("Creating account: %s\n", account);
517 if ( !(sam_account = samu_new( NULL )) ) {
518 return NT_STATUS_NO_MEMORY;
521 if (!(passwd = Get_Pwnam(account))) {
522 /* Create appropriate user */
523 if (delta->acb_info & ACB_NORMAL) {
524 pstrcpy(add_script, lp_adduser_script());
525 } else if ( (delta->acb_info & ACB_WSTRUST) ||
526 (delta->acb_info & ACB_SVRTRUST) ||
527 (delta->acb_info & ACB_DOMTRUST) ) {
528 pstrcpy(add_script, lp_addmachine_script());
529 } else {
530 DEBUG(1, ("Unknown user type: %s\n",
531 pdb_encode_acct_ctrl(delta->acb_info, NEW_PW_FORMAT_SPACE_PADDED_LEN)));
532 nt_ret = NT_STATUS_UNSUCCESSFUL;
533 goto done;
535 if (*add_script) {
536 int add_ret;
537 all_string_sub(add_script, "%u", account,
538 sizeof(account));
539 add_ret = smbrun(add_script,NULL);
540 DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' "
541 "gave %d\n", add_script, add_ret));
544 /* try and find the possible unix account again */
545 if ( !(passwd = Get_Pwnam(account)) ) {
546 d_fprintf(stderr, "Could not create posix account info for '%s'\n", account);
547 nt_ret = NT_STATUS_NO_SUCH_USER;
548 goto done;
552 sid_copy(&user_sid, get_global_sam_sid());
553 sid_append_rid(&user_sid, delta->user_rid);
555 DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n", sid_to_string(sid_string, &user_sid), account));
556 if (!pdb_getsampwsid(sam_account, &user_sid)) {
557 sam_account_from_delta(sam_account, delta);
558 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
559 sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account)));
560 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) {
561 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
562 account));
563 return NT_STATUS_ACCESS_DENIED;
565 } else {
566 sam_account_from_delta(sam_account, delta);
567 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
568 sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account)));
569 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) {
570 DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
571 account));
572 TALLOC_FREE(sam_account);
573 return NT_STATUS_ACCESS_DENIED;
577 if (pdb_get_group_sid(sam_account) == NULL) {
578 return NT_STATUS_UNSUCCESSFUL;
581 group_sid = *pdb_get_group_sid(sam_account);
583 if (!pdb_getgrsid(&map, group_sid)) {
584 DEBUG(0, ("Primary group of %s has no mapping!\n",
585 pdb_get_username(sam_account)));
586 } else {
587 if (map.gid != passwd->pw_gid) {
588 if (!(grp = getgrgid(map.gid))) {
589 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
590 (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_static(&group_sid)));
591 } else {
592 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
597 if ( !passwd ) {
598 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
599 pdb_get_username(sam_account)));
602 done:
603 TALLOC_FREE(sam_account);
604 return nt_ret;
607 static NTSTATUS fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta)
609 fstring name;
610 fstring comment;
611 struct group *grp = NULL;
612 DOM_SID group_sid;
613 fstring sid_string;
614 GROUP_MAP map;
615 BOOL insert = True;
617 unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1);
618 unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1);
620 /* add the group to the mapping table */
621 sid_copy(&group_sid, get_global_sam_sid());
622 sid_append_rid(&group_sid, rid);
623 sid_to_string(sid_string, &group_sid);
625 if (pdb_getgrsid(&map, group_sid)) {
626 if ( map.gid != -1 )
627 grp = getgrgid(map.gid);
628 insert = False;
631 if (grp == NULL) {
632 gid_t gid;
634 /* No group found from mapping, find it from its name. */
635 if ((grp = getgrnam(name)) == NULL) {
637 /* No appropriate group found, create one */
639 d_printf("Creating unix group: '%s'\n", name);
641 if (smb_create_group(name, &gid) != 0)
642 return NT_STATUS_ACCESS_DENIED;
644 if ((grp = getgrnam(name)) == NULL)
645 return NT_STATUS_ACCESS_DENIED;
649 map.gid = grp->gr_gid;
650 map.sid = group_sid;
651 map.sid_name_use = SID_NAME_DOM_GRP;
652 fstrcpy(map.nt_name, name);
653 if (delta->hdr_grp_desc.buffer) {
654 fstrcpy(map.comment, comment);
655 } else {
656 fstrcpy(map.comment, "");
659 if (insert)
660 pdb_add_group_mapping_entry(&map);
661 else
662 pdb_update_group_mapping_entry(&map);
664 return NT_STATUS_OK;
667 static NTSTATUS fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta)
669 int i;
670 TALLOC_CTX *t = NULL;
671 char **nt_members = NULL;
672 char **unix_members;
673 DOM_SID group_sid;
674 GROUP_MAP map;
675 struct group *grp;
677 if (delta->num_members == 0) {
678 return NT_STATUS_OK;
681 sid_copy(&group_sid, get_global_sam_sid());
682 sid_append_rid(&group_sid, rid);
684 if (!get_domain_group_from_sid(group_sid, &map)) {
685 DEBUG(0, ("Could not find global group %d\n", rid));
686 return NT_STATUS_NO_SUCH_GROUP;
689 if (!(grp = getgrgid(map.gid))) {
690 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid));
691 return NT_STATUS_NO_SUCH_GROUP;
694 d_printf("Group members of %s: ", grp->gr_name);
696 if (!(t = talloc_init("fetch_group_mem_info"))) {
697 DEBUG(0, ("could not talloc_init\n"));
698 return NT_STATUS_NO_MEMORY;
701 if ((nt_members = TALLOC_ZERO_ARRAY(t, char *, delta->num_members)) == NULL) {
702 DEBUG(0, ("talloc failed\n"));
703 talloc_free(t);
704 return NT_STATUS_NO_MEMORY;
707 for (i=0; i<delta->num_members; i++) {
708 struct samu *member = NULL;
709 DOM_SID member_sid;
711 if ( !(member = samu_new(t)) ) {
712 talloc_destroy(t);
713 return NT_STATUS_NO_MEMORY;
716 sid_copy(&member_sid, get_global_sam_sid());
717 sid_append_rid(&member_sid, delta->rids[i]);
719 if (!pdb_getsampwsid(member, &member_sid)) {
720 DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
721 delta->rids[i], sid_string_static(&member_sid), grp->gr_name));
722 TALLOC_FREE(member);
723 continue;
726 if (pdb_get_group_rid(member) == rid) {
727 d_printf("%s(primary),", pdb_get_username(member));
728 TALLOC_FREE(member);
729 continue;
732 d_printf("%s,", pdb_get_username(member));
733 nt_members[i] = talloc_strdup(t, pdb_get_username(member));
734 TALLOC_FREE(member);
737 d_printf("\n");
739 unix_members = grp->gr_mem;
741 while (*unix_members) {
742 BOOL is_nt_member = False;
743 for (i=0; i<delta->num_members; i++) {
744 if (nt_members[i] == NULL) {
745 /* This was a primary group */
746 continue;
749 if (strcmp(*unix_members, nt_members[i]) == 0) {
750 is_nt_member = True;
751 break;
754 if (!is_nt_member) {
755 /* We look at a unix group member that is not
756 an nt group member. So, remove it. NT is
757 boss here. */
758 smb_delete_user_group(grp->gr_name, *unix_members);
760 unix_members += 1;
763 for (i=0; i<delta->num_members; i++) {
764 BOOL is_unix_member = False;
766 if (nt_members[i] == NULL) {
767 /* This was the primary group */
768 continue;
771 unix_members = grp->gr_mem;
773 while (*unix_members) {
774 if (strcmp(*unix_members, nt_members[i]) == 0) {
775 is_unix_member = True;
776 break;
778 unix_members += 1;
781 if (!is_unix_member) {
782 /* We look at a nt group member that is not a
783 unix group member currently. So, add the nt
784 group member. */
785 smb_add_user_group(grp->gr_name, nt_members[i]);
789 talloc_destroy(t);
790 return NT_STATUS_OK;
793 static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta,
794 DOM_SID dom_sid)
796 fstring name;
797 fstring comment;
798 struct group *grp = NULL;
799 DOM_SID alias_sid;
800 fstring sid_string;
801 GROUP_MAP map;
802 BOOL insert = True;
804 unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1);
805 unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1);
807 /* Find out whether the group is already mapped */
808 sid_copy(&alias_sid, &dom_sid);
809 sid_append_rid(&alias_sid, rid);
810 sid_to_string(sid_string, &alias_sid);
812 if (pdb_getgrsid(&map, alias_sid)) {
813 grp = getgrgid(map.gid);
814 insert = False;
817 if (grp == NULL) {
818 gid_t gid;
820 /* No group found from mapping, find it from its name. */
821 if ((grp = getgrnam(name)) == NULL) {
822 /* No appropriate group found, create one */
823 d_printf("Creating unix group: '%s'\n", name);
824 if (smb_create_group(name, &gid) != 0)
825 return NT_STATUS_ACCESS_DENIED;
826 if ((grp = getgrgid(gid)) == NULL)
827 return NT_STATUS_ACCESS_DENIED;
831 map.gid = grp->gr_gid;
832 map.sid = alias_sid;
834 if (sid_equal(&dom_sid, &global_sid_Builtin))
835 map.sid_name_use = SID_NAME_WKN_GRP;
836 else
837 map.sid_name_use = SID_NAME_ALIAS;
839 fstrcpy(map.nt_name, name);
840 fstrcpy(map.comment, comment);
842 if (insert)
843 pdb_add_group_mapping_entry(&map);
844 else
845 pdb_update_group_mapping_entry(&map);
847 return NT_STATUS_OK;
850 static NTSTATUS fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid)
852 return NT_STATUS_OK;
855 static NTSTATUS fetch_domain_info(uint32 rid, SAM_DOMAIN_INFO *delta)
857 time_t u_max_age, u_min_age, u_logout, u_lockoutreset, u_lockouttime;
858 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
859 pstring domname;
861 u_max_age = nt_time_to_unix_abs((NTTIME *)&delta->max_pwd_age);
862 u_min_age = nt_time_to_unix_abs((NTTIME *)&delta->min_pwd_age);
863 u_logout = nt_time_to_unix_abs((NTTIME *)&delta->force_logoff);
864 u_lockoutreset = nt_time_to_unix_abs((NTTIME *)&delta->account_lockout.reset_count);
865 u_lockouttime = nt_time_to_unix_abs((NTTIME *)&delta->account_lockout.lockout_duration);
867 unistr2_to_ascii(domname, &delta->uni_dom_name, sizeof(domname) - 1);
869 /* we don't handle BUILTIN account policies */
870 if (!strequal(domname, get_global_sam_name())) {
871 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname);
872 return NT_STATUS_OK;
876 if (!pdb_set_account_policy(AP_PASSWORD_HISTORY, delta->pwd_history_len))
877 return nt_status;
879 if (!pdb_set_account_policy(AP_MIN_PASSWORD_LEN, delta->min_pwd_len))
880 return nt_status;
882 if (!pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (uint32)u_max_age))
883 return nt_status;
885 if (!pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (uint32)u_min_age))
886 return nt_status;
888 if (!pdb_set_account_policy(AP_TIME_TO_LOGOUT, (uint32)u_logout))
889 return nt_status;
891 if (!pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, delta->account_lockout.bad_attempt_lockout))
892 return nt_status;
894 if (!pdb_set_account_policy(AP_RESET_COUNT_TIME, (uint32)u_lockoutreset/60))
895 return nt_status;
897 if (u_lockouttime != -1)
898 u_lockouttime /= 60;
900 if (!pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (uint32)u_lockouttime))
901 return nt_status;
903 if (!pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, delta->logon_chgpass))
904 return nt_status;
906 return NT_STATUS_OK;
910 static void fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta,
911 DOM_SID dom_sid)
913 switch(hdr_delta->type) {
914 case SAM_DELTA_ACCOUNT_INFO:
915 fetch_account_info(hdr_delta->target_rid,
916 &delta->account_info);
917 break;
918 case SAM_DELTA_GROUP_INFO:
919 fetch_group_info(hdr_delta->target_rid,
920 &delta->group_info);
921 break;
922 case SAM_DELTA_GROUP_MEM:
923 fetch_group_mem_info(hdr_delta->target_rid,
924 &delta->grp_mem_info);
925 break;
926 case SAM_DELTA_ALIAS_INFO:
927 fetch_alias_info(hdr_delta->target_rid,
928 &delta->alias_info, dom_sid);
929 break;
930 case SAM_DELTA_ALIAS_MEM:
931 fetch_alias_mem(hdr_delta->target_rid,
932 &delta->als_mem_info, dom_sid);
933 break;
934 case SAM_DELTA_DOMAIN_INFO:
935 fetch_domain_info(hdr_delta->target_rid,
936 &delta->domain_info);
937 break;
938 /* The following types are recognised but not handled */
939 case SAM_DELTA_RENAME_GROUP:
940 d_printf("SAM_DELTA_RENAME_GROUP not handled\n");
941 break;
942 case SAM_DELTA_RENAME_USER:
943 d_printf("SAM_DELTA_RENAME_USER not handled\n");
944 break;
945 case SAM_DELTA_RENAME_ALIAS:
946 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
947 break;
948 case SAM_DELTA_POLICY_INFO:
949 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
950 break;
951 case SAM_DELTA_TRUST_DOMS:
952 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
953 break;
954 case SAM_DELTA_PRIVS_INFO:
955 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
956 break;
957 case SAM_DELTA_SECRET_INFO:
958 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
959 break;
960 case SAM_DELTA_DELETE_GROUP:
961 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
962 break;
963 case SAM_DELTA_DELETE_USER:
964 d_printf("SAM_DELTA_DELETE_USER not handled\n");
965 break;
966 case SAM_DELTA_MODIFIED_COUNT:
967 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
968 break;
969 default:
970 d_printf("Unknown delta record type %d\n", hdr_delta->type);
971 break;
975 static NTSTATUS fetch_database(struct rpc_pipe_client *pipe_hnd, uint32 db_type, DOM_SID dom_sid)
977 uint32 sync_context = 0;
978 NTSTATUS result;
979 int i;
980 TALLOC_CTX *mem_ctx;
981 SAM_DELTA_HDR *hdr_deltas;
982 SAM_DELTA_CTR *deltas;
983 uint32 num_deltas;
985 if (!(mem_ctx = talloc_init("fetch_database")))
986 return NT_STATUS_NO_MEMORY;
988 switch( db_type ) {
989 case SAM_DATABASE_DOMAIN:
990 d_printf("Fetching DOMAIN database\n");
991 break;
992 case SAM_DATABASE_BUILTIN:
993 d_printf("Fetching BUILTIN database\n");
994 break;
995 case SAM_DATABASE_PRIVS:
996 d_printf("Fetching PRIVS databases\n");
997 break;
998 default:
999 d_printf("Fetching unknown database type %u\n", db_type );
1000 break;
1003 do {
1004 result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx,
1005 db_type, sync_context,
1006 &num_deltas,
1007 &hdr_deltas, &deltas);
1009 if (NT_STATUS_IS_OK(result) ||
1010 NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1011 for (i = 0; i < num_deltas; i++) {
1012 fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid);
1014 } else
1015 return result;
1017 sync_context += 1;
1018 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1020 talloc_destroy(mem_ctx);
1022 return result;
1025 static NTSTATUS populate_ldap_for_ldif(fstring sid, const char *suffix, const char
1026 *builtin_sid, FILE *add_fd)
1028 char *user_suffix, *group_suffix, *machine_suffix, *idmap_suffix;
1029 char *user_attr=NULL, *group_attr=NULL;
1030 char *suffix_attr;
1031 int len;
1033 /* Get the suffix attribute */
1034 suffix_attr = sstring_sub(suffix, '=', ',');
1035 if (suffix_attr == NULL) {
1036 len = strlen(suffix);
1037 suffix_attr = (char*)SMB_MALLOC(len+1);
1038 memcpy(suffix_attr, suffix, len);
1039 suffix_attr[len] = '\0';
1042 /* Write the base */
1043 fprintf(add_fd, "# %s\n", suffix);
1044 fprintf(add_fd, "dn: %s\n", suffix);
1045 fprintf(add_fd, "objectClass: dcObject\n");
1046 fprintf(add_fd, "objectClass: organization\n");
1047 fprintf(add_fd, "o: %s\n", suffix_attr);
1048 fprintf(add_fd, "dc: %s\n", suffix_attr);
1049 fprintf(add_fd, "\n");
1050 fflush(add_fd);
1052 user_suffix = lp_ldap_user_suffix();
1053 if (user_suffix == NULL) {
1054 SAFE_FREE(suffix_attr);
1055 return NT_STATUS_NO_MEMORY;
1057 /* If it exists and is distinct from other containers,
1058 Write the Users entity */
1059 if (*user_suffix && strcmp(user_suffix, suffix)) {
1060 user_attr = sstring_sub(lp_ldap_user_suffix(), '=', ',');
1061 fprintf(add_fd, "# %s\n", user_suffix);
1062 fprintf(add_fd, "dn: %s\n", user_suffix);
1063 fprintf(add_fd, "objectClass: organizationalUnit\n");
1064 fprintf(add_fd, "ou: %s\n", user_attr);
1065 fprintf(add_fd, "\n");
1066 fflush(add_fd);
1070 group_suffix = lp_ldap_group_suffix();
1071 if (group_suffix == NULL) {
1072 SAFE_FREE(suffix_attr);
1073 SAFE_FREE(user_attr);
1074 return NT_STATUS_NO_MEMORY;
1076 /* If it exists and is distinct from other containers,
1077 Write the Groups entity */
1078 if (*group_suffix && strcmp(group_suffix, suffix)) {
1079 group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1080 fprintf(add_fd, "# %s\n", group_suffix);
1081 fprintf(add_fd, "dn: %s\n", group_suffix);
1082 fprintf(add_fd, "objectClass: organizationalUnit\n");
1083 fprintf(add_fd, "ou: %s\n", group_attr);
1084 fprintf(add_fd, "\n");
1085 fflush(add_fd);
1088 /* If it exists and is distinct from other containers,
1089 Write the Computers entity */
1090 machine_suffix = lp_ldap_machine_suffix();
1091 if (machine_suffix == NULL) {
1092 SAFE_FREE(suffix_attr);
1093 SAFE_FREE(user_attr);
1094 SAFE_FREE(group_attr);
1095 return NT_STATUS_NO_MEMORY;
1097 if (*machine_suffix && strcmp(machine_suffix, user_suffix) &&
1098 strcmp(machine_suffix, suffix)) {
1099 char *machine_ou = NULL;
1100 fprintf(add_fd, "# %s\n", machine_suffix);
1101 fprintf(add_fd, "dn: %s\n", machine_suffix);
1102 fprintf(add_fd, "objectClass: organizationalUnit\n");
1103 /* this isn't totally correct as it assumes that
1104 there _must_ be an ou. just fixing memleak now. jmcd */
1105 machine_ou = sstring_sub(lp_ldap_machine_suffix(), '=', ',');
1106 fprintf(add_fd, "ou: %s\n", machine_ou);
1107 SAFE_FREE(machine_ou);
1108 fprintf(add_fd, "\n");
1109 fflush(add_fd);
1112 /* If it exists and is distinct from other containers,
1113 Write the IdMap entity */
1114 idmap_suffix = lp_ldap_idmap_suffix();
1115 if (idmap_suffix == NULL) {
1116 SAFE_FREE(suffix_attr);
1117 SAFE_FREE(user_attr);
1118 SAFE_FREE(group_attr);
1119 return NT_STATUS_NO_MEMORY;
1121 if (*idmap_suffix &&
1122 strcmp(idmap_suffix, user_suffix) &&
1123 strcmp(idmap_suffix, suffix)) {
1124 char *s;
1125 fprintf(add_fd, "# %s\n", idmap_suffix);
1126 fprintf(add_fd, "dn: %s\n", idmap_suffix);
1127 fprintf(add_fd, "ObjectClass: organizationalUnit\n");
1128 s = sstring_sub(lp_ldap_idmap_suffix(), '=', ',');
1129 fprintf(add_fd, "ou: %s\n", s);
1130 SAFE_FREE(s);
1131 fprintf(add_fd, "\n");
1132 fflush(add_fd);
1135 /* Write the root entity */
1136 fprintf(add_fd, "# root, %s, %s\n", user_attr, suffix);
1137 fprintf(add_fd, "dn: uid=root,ou=%s,%s\n", user_attr, suffix);
1138 fprintf(add_fd, "cn: root\n");
1139 fprintf(add_fd, "sn: root\n");
1140 fprintf(add_fd, "objectClass: inetOrgPerson\n");
1141 fprintf(add_fd, "objectClass: sambaSAMAccount\n");
1142 fprintf(add_fd, "objectClass: posixAccount\n");
1143 fprintf(add_fd, "objectClass: shadowAccount\n");
1144 fprintf(add_fd, "gidNumber: 0\n");
1145 fprintf(add_fd, "uid: root\n");
1146 fprintf(add_fd, "uidNumber: 0\n");
1147 fprintf(add_fd, "homeDirectory: /home/root\n");
1148 fprintf(add_fd, "sambaPwdLastSet: 0\n");
1149 fprintf(add_fd, "sambaLogonTime: 0\n");
1150 fprintf(add_fd, "sambaLogoffTime: 2147483647\n");
1151 fprintf(add_fd, "sambaKickoffTime: 2147483647\n");
1152 fprintf(add_fd, "sambaPwdCanChange: 0\n");
1153 fprintf(add_fd, "sambaPwdMustChange: 2147483647\n");
1154 fprintf(add_fd, "sambaHomePath: \\\\PDC-SRV\root\n");
1155 fprintf(add_fd, "sambaHomeDrive: H:\n");
1156 fprintf(add_fd, "sambaProfilePath: \\\\PDC-SRV\\profiles\\root\n");
1157 fprintf(add_fd, "sambaprimaryGroupSID: %s-512\n", sid);
1158 fprintf(add_fd, "sambaLMPassword: XXX\n");
1159 fprintf(add_fd, "sambaNTPassword: XXX\n");
1160 fprintf(add_fd, "sambaAcctFlags: [U\n");
1161 fprintf(add_fd, "sambaSID: %s-500\n", sid);
1162 fprintf(add_fd, "loginShell: /bin/false\n");
1163 fprintf(add_fd, "\n");
1164 fflush(add_fd);
1166 /* Write the domain entity */
1167 fprintf(add_fd, "# %s, %s\n", lp_workgroup(), suffix);
1168 fprintf(add_fd, "dn: sambaDomainName=%s,%s\n", lp_workgroup(),
1169 suffix);
1170 fprintf(add_fd, "objectClass: sambaDomain\n");
1171 fprintf(add_fd, "objectClass: sambaUnixIdPool\n");
1172 fprintf(add_fd, "sambaDomainName: %s\n", lp_workgroup());
1173 fprintf(add_fd, "sambaSID: %s\n", sid);
1174 fprintf(add_fd, "uidNumber: %d\n", ++ldif_uid);
1175 fprintf(add_fd, "gidNumber: %d\n", ++ldif_gid);
1176 fprintf(add_fd, "\n");
1177 fflush(add_fd);
1179 /* Write user nobody entity */
1180 fprintf(add_fd, "# nobody, %s, %s\n", user_attr, suffix);
1181 fprintf(add_fd, "dn: uid=nobody,ou=%s,%s\n", user_attr, suffix);
1182 fprintf(add_fd, "cn: nobody\n");
1183 fprintf(add_fd, "sn: nobody\n");
1184 fprintf(add_fd, "objectClass: inetOrgPerson\n");
1185 fprintf(add_fd, "objectClass: sambaSAMAccount\n");
1186 fprintf(add_fd, "objectClass: posixAccount\n");
1187 fprintf(add_fd, "objectClass: shadowAccount\n");
1188 fprintf(add_fd, "gidNumber: 514\n");
1189 fprintf(add_fd, "uid: nobody\n");
1190 fprintf(add_fd, "uidNumber: 999\n");
1191 fprintf(add_fd, "homeDirectory: /nobodyshomedir\n");
1192 fprintf(add_fd, "sambaPwdLastSet: 0\n");
1193 fprintf(add_fd, "sambaLogonTime: 0\n");
1194 fprintf(add_fd, "sambaLogoffTime: 2147483647\n");
1195 fprintf(add_fd, "sambaKickoffTime: 2147483647\n");
1196 fprintf(add_fd, "sambaPwdCanChange: 0\n");
1197 fprintf(add_fd, "sambaPwdMustChange: 2147483647\n");
1198 fprintf(add_fd, "sambaHomePath: \\\\PDC-SMD3\\homes\\nobody\n");
1199 fprintf(add_fd, "sambaHomeDrive: H:\n");
1200 fprintf(add_fd, "sambaProfilePath: \\\\PDC-SMB3\\profiles\\nobody\n");
1201 fprintf(add_fd, "sambaprimaryGroupSID: %s-514\n", sid);
1202 fprintf(add_fd, "sambaLMPassword: NOPASSWORDXXXXXXXXXXXXXXXXXXXXX\n");
1203 fprintf(add_fd, "sambaNTPassword: NOPASSWORDXXXXXXXXXXXXXXXXXXXXX\n");
1204 fprintf(add_fd, "sambaAcctFlags: [NU\n");
1205 fprintf(add_fd, "sambaSID: %s-2998\n", sid);
1206 fprintf(add_fd, "loginShell: /bin/false\n");
1207 fprintf(add_fd, "\n");
1208 fflush(add_fd);
1210 /* Write the Domain Admins entity */
1211 fprintf(add_fd, "# Domain Admins, %s, %s\n", group_attr,
1212 suffix);
1213 fprintf(add_fd, "dn: cn=Domain Admins,ou=%s,%s\n", group_attr,
1214 suffix);
1215 fprintf(add_fd, "objectClass: posixGroup\n");
1216 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1217 fprintf(add_fd, "cn: Domain Admins\n");
1218 fprintf(add_fd, "memberUid: Administrator\n");
1219 fprintf(add_fd, "description: Netbios Domain Administrators\n");
1220 fprintf(add_fd, "gidNumber: 512\n");
1221 fprintf(add_fd, "sambaSID: %s-512\n", sid);
1222 fprintf(add_fd, "sambaGroupType: 2\n");
1223 fprintf(add_fd, "displayName: Domain Admins\n");
1224 fprintf(add_fd, "\n");
1225 fflush(add_fd);
1227 /* Write the Domain Users entity */
1228 fprintf(add_fd, "# Domain Users, %s, %s\n", group_attr,
1229 suffix);
1230 fprintf(add_fd, "dn: cn=Domain Users,ou=%s,%s\n", group_attr,
1231 suffix);
1232 fprintf(add_fd, "objectClass: posixGroup\n");
1233 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1234 fprintf(add_fd, "cn: Domain Users\n");
1235 fprintf(add_fd, "description: Netbios Domain Users\n");
1236 fprintf(add_fd, "gidNumber: 513\n");
1237 fprintf(add_fd, "sambaSID: %s-513\n", sid);
1238 fprintf(add_fd, "sambaGroupType: 2\n");
1239 fprintf(add_fd, "displayName: Domain Users\n");
1240 fprintf(add_fd, "\n");
1241 fflush(add_fd);
1243 /* Write the Domain Guests entity */
1244 fprintf(add_fd, "# Domain Guests, %s, %s\n", group_attr,
1245 suffix);
1246 fprintf(add_fd, "dn: cn=Domain Guests,ou=%s,%s\n", group_attr,
1247 suffix);
1248 fprintf(add_fd, "objectClass: posixGroup\n");
1249 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1250 fprintf(add_fd, "cn: Domain Guests\n");
1251 fprintf(add_fd, "description: Netbios Domain Guests\n");
1252 fprintf(add_fd, "gidNumber: 514\n");
1253 fprintf(add_fd, "sambaSID: %s-514\n", sid);
1254 fprintf(add_fd, "sambaGroupType: 2\n");
1255 fprintf(add_fd, "displayName: Domain Guests\n");
1256 fprintf(add_fd, "\n");
1257 fflush(add_fd);
1259 /* Write the Domain Computers entity */
1260 fprintf(add_fd, "# Domain Computers, %s, %s\n", group_attr,
1261 suffix);
1262 fprintf(add_fd, "dn: cn=Domain Computers,ou=%s,%s\n",
1263 group_attr, suffix);
1264 fprintf(add_fd, "objectClass: posixGroup\n");
1265 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1266 fprintf(add_fd, "gidNumber: 515\n");
1267 fprintf(add_fd, "cn: Domain Computers\n");
1268 fprintf(add_fd, "description: Netbios Domain Computers accounts\n");
1269 fprintf(add_fd, "sambaSID: %s-515\n", sid);
1270 fprintf(add_fd, "sambaGroupType: 2\n");
1271 fprintf(add_fd, "displayName: Domain Computers\n");
1272 fprintf(add_fd, "\n");
1273 fflush(add_fd);
1275 /* Write the Admininistrators Groups entity */
1276 fprintf(add_fd, "# Administrators, %s, %s\n", group_attr,
1277 suffix);
1278 fprintf(add_fd, "dn: cn=Administrators,ou=%s,%s\n", group_attr,
1279 suffix);
1280 fprintf(add_fd, "objectClass: posixGroup\n");
1281 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1282 fprintf(add_fd, "gidNumber: 544\n");
1283 fprintf(add_fd, "cn: Administrators\n");
1284 fprintf(add_fd, "description: Netbios Domain Members can fully administer the computer/sambaDomainName\n");
1285 fprintf(add_fd, "sambaSID: %s-544\n", builtin_sid);
1286 fprintf(add_fd, "sambaGroupType: 5\n");
1287 fprintf(add_fd, "displayName: Administrators\n");
1288 fprintf(add_fd, "\n");
1290 /* Write the Print Operator entity */
1291 fprintf(add_fd, "# Print Operators, %s, %s\n", group_attr,
1292 suffix);
1293 fprintf(add_fd, "dn: cn=Print Operators,ou=%s,%s\n",
1294 group_attr, suffix);
1295 fprintf(add_fd, "objectClass: posixGroup\n");
1296 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1297 fprintf(add_fd, "gidNumber: 550\n");
1298 fprintf(add_fd, "cn: Print Operators\n");
1299 fprintf(add_fd, "description: Netbios Domain Print Operators\n");
1300 fprintf(add_fd, "sambaSID: %s-550\n", builtin_sid);
1301 fprintf(add_fd, "sambaGroupType: 5\n");
1302 fprintf(add_fd, "displayName: Print Operators\n");
1303 fprintf(add_fd, "\n");
1304 fflush(add_fd);
1306 /* Write the Backup Operators entity */
1307 fprintf(add_fd, "# Backup Operators, %s, %s\n", group_attr,
1308 suffix);
1309 fprintf(add_fd, "dn: cn=Backup Operators,ou=%s,%s\n",
1310 group_attr, suffix);
1311 fprintf(add_fd, "objectClass: posixGroup\n");
1312 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1313 fprintf(add_fd, "gidNumber: 551\n");
1314 fprintf(add_fd, "cn: Backup Operators\n");
1315 fprintf(add_fd, "description: Netbios Domain Members can bypass file security to back up files\n");
1316 fprintf(add_fd, "sambaSID: %s-551\n", builtin_sid);
1317 fprintf(add_fd, "sambaGroupType: 5\n");
1318 fprintf(add_fd, "displayName: Backup Operators\n");
1319 fprintf(add_fd, "\n");
1320 fflush(add_fd);
1322 /* Write the Replicators entity */
1323 fprintf(add_fd, "# Replicators, %s, %s\n", group_attr, suffix);
1324 fprintf(add_fd, "dn: cn=Replicators,ou=%s,%s\n", group_attr,
1325 suffix);
1326 fprintf(add_fd, "objectClass: posixGroup\n");
1327 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1328 fprintf(add_fd, "gidNumber: 552\n");
1329 fprintf(add_fd, "cn: Replicators\n");
1330 fprintf(add_fd, "description: Netbios Domain Supports file replication in a sambaDomainName\n");
1331 fprintf(add_fd, "sambaSID: %s-552\n", builtin_sid);
1332 fprintf(add_fd, "sambaGroupType: 5\n");
1333 fprintf(add_fd, "displayName: Replicators\n");
1334 fprintf(add_fd, "\n");
1335 fflush(add_fd);
1337 /* Deallocate memory, and return */
1338 SAFE_FREE(suffix_attr);
1339 SAFE_FREE(user_attr);
1340 SAFE_FREE(group_attr);
1341 return NT_STATUS_OK;
1344 static NTSTATUS map_populate_groups(GROUPMAP *groupmap, ACCOUNTMAP *accountmap, fstring sid,
1345 const char *suffix, const char *builtin_sid)
1347 char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1349 /* Map the groups created by populate_ldap_for_ldif */
1350 groupmap[0].rid = 512;
1351 groupmap[0].gidNumber = 512;
1352 pstr_sprintf(groupmap[0].sambaSID, "%s-512", sid);
1353 pstr_sprintf(groupmap[0].group_dn, "cn=Domain Admins,ou=%s,%s",
1354 group_attr, suffix);
1355 accountmap[0].rid = 512;
1356 pstr_sprintf(accountmap[0].cn, "%s", "Domain Admins");
1358 groupmap[1].rid = 513;
1359 groupmap[1].gidNumber = 513;
1360 pstr_sprintf(groupmap[1].sambaSID, "%s-513", sid);
1361 pstr_sprintf(groupmap[1].group_dn, "cn=Domain Users,ou=%s,%s",
1362 group_attr, suffix);
1363 accountmap[1].rid = 513;
1364 pstr_sprintf(accountmap[1].cn, "%s", "Domain Users");
1366 groupmap[2].rid = 514;
1367 groupmap[2].gidNumber = 514;
1368 pstr_sprintf(groupmap[2].sambaSID, "%s-514", sid);
1369 pstr_sprintf(groupmap[2].group_dn, "cn=Domain Guests,ou=%s,%s",
1370 group_attr, suffix);
1371 accountmap[2].rid = 514;
1372 pstr_sprintf(accountmap[2].cn, "%s", "Domain Guests");
1374 groupmap[3].rid = 515;
1375 groupmap[3].gidNumber = 515;
1376 pstr_sprintf(groupmap[3].sambaSID, "%s-515", sid);
1377 pstr_sprintf(groupmap[3].group_dn, "cn=Domain Computers,ou=%s,%s",
1378 group_attr, suffix);
1379 accountmap[3].rid = 515;
1380 pstr_sprintf(accountmap[3].cn, "%s", "Domain Computers");
1382 groupmap[4].rid = 544;
1383 groupmap[4].gidNumber = 544;
1384 pstr_sprintf(groupmap[4].sambaSID, "%s-544", builtin_sid);
1385 pstr_sprintf(groupmap[4].group_dn, "cn=Administrators,ou=%s,%s",
1386 group_attr, suffix);
1387 accountmap[4].rid = 515;
1388 pstr_sprintf(accountmap[4].cn, "%s", "Administrators");
1390 groupmap[5].rid = 550;
1391 groupmap[5].gidNumber = 550;
1392 pstr_sprintf(groupmap[5].sambaSID, "%s-550", builtin_sid);
1393 pstr_sprintf(groupmap[5].group_dn, "cn=Print Operators,ou=%s,%s",
1394 group_attr, suffix);
1395 accountmap[5].rid = 550;
1396 pstr_sprintf(accountmap[5].cn, "%s", "Print Operators");
1398 groupmap[6].rid = 551;
1399 groupmap[6].gidNumber = 551;
1400 pstr_sprintf(groupmap[6].sambaSID, "%s-551", builtin_sid);
1401 pstr_sprintf(groupmap[6].group_dn, "cn=Backup Operators,ou=%s,%s",
1402 group_attr, suffix);
1403 accountmap[6].rid = 551;
1404 pstr_sprintf(accountmap[6].cn, "%s", "Backup Operators");
1406 groupmap[7].rid = 552;
1407 groupmap[7].gidNumber = 552;
1408 pstr_sprintf(groupmap[7].sambaSID, "%s-552", builtin_sid);
1409 pstr_sprintf(groupmap[7].group_dn, "cn=Replicators,ou=%s,%s",
1410 group_attr, suffix);
1411 accountmap[7].rid = 551;
1412 pstr_sprintf(accountmap[7].cn, "%s", "Replicators");
1413 SAFE_FREE(group_attr);
1414 return NT_STATUS_OK;
1417 static NTSTATUS fetch_group_info_to_ldif(SAM_DELTA_CTR *delta, GROUPMAP *groupmap,
1418 FILE *add_fd, fstring sid, char *suffix)
1420 fstring groupname;
1421 uint32 grouptype = 0, g_rid = 0;
1422 char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1424 /* Get the group name */
1425 unistr2_to_ascii(groupname,
1426 &(delta->group_info.uni_grp_name),
1427 sizeof(groupname)-1);
1429 /* Set up the group type (always 2 for group info) */
1430 grouptype = 2;
1432 /* These groups are entered by populate_ldap_for_ldif */
1433 if (strcmp(groupname, "Domain Admins") == 0 ||
1434 strcmp(groupname, "Domain Users") == 0 ||
1435 strcmp(groupname, "Domain Guests") == 0 ||
1436 strcmp(groupname, "Domain Computers") == 0 ||
1437 strcmp(groupname, "Administrators") == 0 ||
1438 strcmp(groupname, "Print Operators") == 0 ||
1439 strcmp(groupname, "Backup Operators") == 0 ||
1440 strcmp(groupname, "Replicators") == 0) {
1441 SAFE_FREE(group_attr);
1442 return NT_STATUS_OK;
1443 } else {
1444 /* Increment the gid for the new group */
1445 ldif_gid++;
1448 /* Map the group rid, gid, and dn */
1449 g_rid = delta->group_info.gid.g_rid;
1450 groupmap->rid = g_rid;
1451 groupmap->gidNumber = ldif_gid;
1452 pstr_sprintf(groupmap->sambaSID, "%s-%d", sid, g_rid);
1453 pstr_sprintf(groupmap->group_dn,
1454 "cn=%s,ou=%s,%s", groupname, group_attr, suffix);
1456 /* Write the data to the temporary add ldif file */
1457 fprintf(add_fd, "# %s, %s, %s\n", groupname, group_attr,
1458 suffix);
1459 fprintf(add_fd, "dn: cn=%s,ou=%s,%s\n", groupname, group_attr,
1460 suffix);
1461 fprintf(add_fd, "objectClass: posixGroup\n");
1462 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1463 fprintf(add_fd, "cn: %s\n", groupname);
1464 fprintf(add_fd, "gidNumber: %d\n", ldif_gid);
1465 fprintf(add_fd, "sambaSID: %s\n", groupmap->sambaSID);
1466 fprintf(add_fd, "sambaGroupType: %d\n", grouptype);
1467 fprintf(add_fd, "displayName: %s\n", groupname);
1468 fprintf(add_fd, "\n");
1469 fflush(add_fd);
1471 SAFE_FREE(group_attr);
1472 /* Return */
1473 return NT_STATUS_OK;
1476 static NTSTATUS fetch_account_info_to_ldif(SAM_DELTA_CTR *delta, GROUPMAP *groupmap,
1477 ACCOUNTMAP *accountmap, FILE *add_fd,
1478 fstring sid, char *suffix, int alloced)
1480 fstring username, logonscript, homedrive, homepath = "", homedir = "";
1481 fstring hex_nt_passwd, hex_lm_passwd;
1482 fstring description, fullname, sambaSID;
1483 uchar lm_passwd[16], nt_passwd[16];
1484 char *flags, *user_rdn;
1485 const char* nopasswd = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
1486 static uchar zero_buf[16];
1487 uint32 rid = 0, group_rid = 0, gidNumber = 0;
1488 time_t unix_time;
1489 int i;
1491 /* Get the username */
1492 unistr2_to_ascii(username,
1493 &(delta->account_info.uni_acct_name),
1494 sizeof(username)-1);
1496 /* Get the rid */
1497 rid = delta->account_info.user_rid;
1499 /* Map the rid and username for group member info later */
1500 accountmap->rid = rid;
1501 pstr_sprintf(accountmap->cn, "%s", username);
1503 /* Get the home directory */
1504 if (delta->account_info.acb_info & ACB_NORMAL) {
1505 unistr2_to_ascii(homedir, &(delta->account_info.uni_home_dir),
1506 sizeof(homedir)-1);
1507 if (!*homedir) {
1508 pstr_sprintf(homedir, "/home/%s", username);
1509 } else {
1510 pstr_sprintf(homedir, "/nobodyshomedir");
1514 /* Get the logon script */
1515 unistr2_to_ascii(logonscript, &(delta->account_info.uni_logon_script),
1516 sizeof(logonscript)-1);
1518 /* Get the home drive */
1519 unistr2_to_ascii(homedrive, &(delta->account_info.uni_dir_drive),
1520 sizeof(homedrive)-1);
1522 /* Get the description */
1523 unistr2_to_ascii(description, &(delta->account_info.uni_acct_desc),
1524 sizeof(description)-1);
1525 if (!*description) {
1526 pstr_sprintf(description, "System User");
1529 /* Get the display name */
1530 unistr2_to_ascii(fullname, &(delta->account_info.uni_full_name),
1531 sizeof(fullname)-1);
1533 /* Get lm and nt password data */
1534 if (memcmp(delta->account_info.pass.buf_lm_pwd, zero_buf, 16) != 0) {
1535 sam_pwd_hash(delta->account_info.user_rid,
1536 delta->account_info.pass.buf_lm_pwd,
1537 lm_passwd, 0);
1538 pdb_sethexpwd(hex_lm_passwd, lm_passwd,
1539 delta->account_info.acb_info);
1540 } else {
1541 pdb_sethexpwd(hex_lm_passwd, NULL, 0);
1543 if (memcmp(delta->account_info.pass.buf_nt_pwd, zero_buf, 16) != 0) {
1544 sam_pwd_hash(delta->account_info.user_rid,
1545 delta->account_info.pass.buf_nt_pwd,
1546 nt_passwd, 0);
1547 pdb_sethexpwd(hex_nt_passwd, nt_passwd,
1548 delta->account_info.acb_info);
1549 } else {
1550 pdb_sethexpwd(hex_nt_passwd, NULL, 0);
1552 unix_time = nt_time_to_unix(&(delta->account_info.pwd_last_set_time));
1554 /* The nobody user is entered by populate_ldap_for_ldif */
1555 if (strcmp(username, "nobody") == 0) {
1556 return NT_STATUS_OK;
1557 } else {
1558 /* Increment the uid for the new user */
1559 ldif_uid++;
1562 /* Set up group id and sambaSID for the user */
1563 group_rid = delta->account_info.group_rid;
1564 for (i=0; i<alloced; i++) {
1565 if (groupmap[i].rid == group_rid) break;
1567 if (i == alloced){
1568 DEBUG(1, ("Could not find rid %d in groupmap array\n",
1569 group_rid));
1570 return NT_STATUS_UNSUCCESSFUL;
1572 gidNumber = groupmap[i].gidNumber;
1573 pstr_sprintf(sambaSID, groupmap[i].sambaSID);
1575 /* Set up sambaAcctFlags */
1576 flags = pdb_encode_acct_ctrl(delta->account_info.acb_info,
1577 NEW_PW_FORMAT_SPACE_PADDED_LEN);
1579 /* Add the user to the temporary add ldif file */
1580 /* this isn't quite right...we can't assume there's just OU=. jmcd */
1581 user_rdn = sstring_sub(lp_ldap_user_suffix(), '=', ',');
1582 fprintf(add_fd, "# %s, %s, %s\n", username, user_rdn, suffix);
1583 fprintf(add_fd, "dn: uid=%s,ou=%s,%s\n", username, user_rdn, suffix);
1584 SAFE_FREE(user_rdn);
1585 fprintf(add_fd, "ObjectClass: top\n");
1586 fprintf(add_fd, "objectClass: inetOrgPerson\n");
1587 fprintf(add_fd, "objectClass: posixAccount\n");
1588 fprintf(add_fd, "objectClass: shadowAccount\n");
1589 fprintf(add_fd, "objectClass: sambaSamAccount\n");
1590 fprintf(add_fd, "cn: %s\n", username);
1591 fprintf(add_fd, "sn: %s\n", username);
1592 fprintf(add_fd, "uid: %s\n", username);
1593 fprintf(add_fd, "uidNumber: %d\n", ldif_uid);
1594 fprintf(add_fd, "gidNumber: %d\n", gidNumber);
1595 fprintf(add_fd, "homeDirectory: %s\n", homedir);
1596 if (*homepath)
1597 fprintf(add_fd, "SambaHomePath: %s\n", homepath);
1598 if (*homedrive)
1599 fprintf(add_fd, "SambaHomeDrive: %s\n", homedrive);
1600 if (*logonscript)
1601 fprintf(add_fd, "SambaLogonScript: %s\n", logonscript);
1602 fprintf(add_fd, "loginShell: %s\n",
1603 ((delta->account_info.acb_info & ACB_NORMAL) ?
1604 "/bin/bash" : "/bin/false"));
1605 fprintf(add_fd, "gecos: System User\n");
1606 fprintf(add_fd, "description: %s\n", description);
1607 fprintf(add_fd, "sambaSID: %s-%d\n", sid, rid);
1608 fprintf(add_fd, "sambaPrimaryGroupSID: %s\n", sambaSID);
1609 if(*fullname)
1610 fprintf(add_fd, "displayName: %s\n", fullname);
1611 if (strcmp(nopasswd, hex_lm_passwd) != 0)
1612 fprintf(add_fd, "sambaLMPassword: %s\n", hex_lm_passwd);
1613 if (strcmp(nopasswd, hex_nt_passwd) != 0)
1614 fprintf(add_fd, "sambaNTPassword: %s\n", hex_nt_passwd);
1615 fprintf(add_fd, "sambaPwdLastSet: %d\n", (int)unix_time);
1616 fprintf(add_fd, "sambaAcctFlags: %s\n", flags);
1617 fprintf(add_fd, "\n");
1618 fflush(add_fd);
1620 /* Return */
1621 return NT_STATUS_OK;
1624 static NTSTATUS fetch_alias_info_to_ldif(SAM_DELTA_CTR *delta, GROUPMAP *groupmap,
1625 FILE *add_fd, fstring sid, char *suffix,
1626 unsigned db_type)
1628 fstring aliasname, description;
1629 uint32 grouptype = 0, g_rid = 0;
1630 char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ',');
1632 /* Get the alias name */
1633 unistr2_to_ascii(aliasname, &(delta->alias_info.uni_als_name),
1634 sizeof(aliasname)-1);
1636 /* Get the alias description */
1637 unistr2_to_ascii(description, &(delta->alias_info.uni_als_desc),
1638 sizeof(description)-1);
1640 /* Set up the group type */
1641 switch (db_type) {
1642 case SAM_DATABASE_DOMAIN:
1643 grouptype = 4;
1644 break;
1645 case SAM_DATABASE_BUILTIN:
1646 grouptype = 5;
1647 break;
1648 default:
1649 grouptype = 4;
1650 break;
1654 These groups are entered by populate_ldap_for_ldif
1655 Note that populate creates a group called Relicators,
1656 but NT returns a group called Replicator
1658 if (strcmp(aliasname, "Domain Admins") == 0 ||
1659 strcmp(aliasname, "Domain Users") == 0 ||
1660 strcmp(aliasname, "Domain Guests") == 0 ||
1661 strcmp(aliasname, "Domain Computers") == 0 ||
1662 strcmp(aliasname, "Administrators") == 0 ||
1663 strcmp(aliasname, "Print Operators") == 0 ||
1664 strcmp(aliasname, "Backup Operators") == 0 ||
1665 strcmp(aliasname, "Replicator") == 0) {
1666 SAFE_FREE(group_attr);
1667 return NT_STATUS_OK;
1668 } else {
1669 /* Increment the gid for the new group */
1670 ldif_gid++;
1673 /* Map the group rid and gid */
1674 g_rid = delta->group_info.gid.g_rid;
1675 groupmap->gidNumber = ldif_gid;
1676 pstr_sprintf(groupmap->sambaSID, "%s-%d", sid, g_rid);
1678 /* Write the data to the temporary add ldif file */
1679 fprintf(add_fd, "# %s, %s, %s\n", aliasname, group_attr,
1680 suffix);
1681 fprintf(add_fd, "dn: cn=%s,ou=%s,%s\n", aliasname, group_attr,
1682 suffix);
1683 fprintf(add_fd, "objectClass: posixGroup\n");
1684 fprintf(add_fd, "objectClass: sambaGroupMapping\n");
1685 fprintf(add_fd, "cn: %s\n", aliasname);
1686 fprintf(add_fd, "gidNumber: %d\n", ldif_gid);
1687 fprintf(add_fd, "sambaSID: %s\n", groupmap->sambaSID);
1688 fprintf(add_fd, "sambaGroupType: %d\n", grouptype);
1689 fprintf(add_fd, "displayName: %s\n", aliasname);
1690 fprintf(add_fd, "description: %s\n", description);
1691 fprintf(add_fd, "\n");
1692 fflush(add_fd);
1694 SAFE_FREE(group_attr);
1695 /* Return */
1696 return NT_STATUS_OK;
1699 static NTSTATUS fetch_groupmem_info_to_ldif(SAM_DELTA_CTR *delta, SAM_DELTA_HDR *hdr_delta,
1700 GROUPMAP *groupmap, ACCOUNTMAP *accountmap,
1701 FILE *mod_fd, int alloced)
1703 fstring group_dn;
1704 uint32 group_rid = 0, rid = 0;
1705 int i, j, k;
1707 /* Get the dn for the group */
1708 if (delta->grp_mem_info.num_members > 0) {
1709 group_rid = hdr_delta->target_rid;
1710 for (j=0; j<alloced; j++) {
1711 if (groupmap[j].rid == group_rid) break;
1713 if (j == alloced){
1714 DEBUG(1, ("Could not find rid %d in groupmap array\n",
1715 group_rid));
1716 return NT_STATUS_UNSUCCESSFUL;
1718 pstr_sprintf(group_dn, "%s", groupmap[j].group_dn);
1719 fprintf(mod_fd, "dn: %s\n", group_dn);
1721 /* Get the cn for each member */
1722 for (i=0; i<delta->grp_mem_info.num_members; i++) {
1723 rid = delta->grp_mem_info.rids[i];
1724 for (k=0; k<alloced; k++) {
1725 if (accountmap[k].rid == rid) break;
1727 if (k == alloced){
1728 DEBUG(1, ("Could not find rid %d in accountmap array\n", rid));
1729 return NT_STATUS_UNSUCCESSFUL;
1731 fprintf(mod_fd, "memberUid: %s\n", accountmap[k].cn);
1733 fprintf(mod_fd, "\n");
1735 fflush(mod_fd);
1737 /* Return */
1738 return NT_STATUS_OK;
1741 static NTSTATUS fetch_database_to_ldif(struct rpc_pipe_client *pipe_hnd,
1742 uint32 db_type,
1743 DOM_SID dom_sid,
1744 const char *user_file)
1746 char *suffix;
1747 const char *builtin_sid = "S-1-5-32";
1748 char *add_name = NULL, *mod_name = NULL;
1749 const char *add_template = "/tmp/add.ldif.XXXXXX";
1750 const char *mod_template = "/tmp/mod.ldif.XXXXXX";
1751 fstring sid, domainname;
1752 uint32 sync_context = 0;
1753 NTSTATUS ret = NT_STATUS_OK, result;
1754 int k;
1755 TALLOC_CTX *mem_ctx;
1756 SAM_DELTA_HDR *hdr_deltas;
1757 SAM_DELTA_CTR *deltas;
1758 uint32 num_deltas;
1759 FILE *add_file = NULL, *mod_file = NULL, *ldif_file = NULL;
1760 int num_alloced = 0, g_index = 0, a_index = 0;
1762 /* Set up array for mapping accounts to groups */
1763 /* Array element is the group rid */
1764 GROUPMAP *groupmap = NULL;
1766 /* Set up array for mapping account rid's to cn's */
1767 /* Array element is the account rid */
1768 ACCOUNTMAP *accountmap = NULL;
1770 if (!(mem_ctx = talloc_init("fetch_database"))) {
1771 return NT_STATUS_NO_MEMORY;
1774 /* Ensure we have an output file */
1775 if (user_file)
1776 ldif_file = fopen(user_file, "a");
1777 else
1778 ldif_file = stdout;
1780 if (!ldif_file) {
1781 fprintf(stderr, "Could not open %s\n", user_file);
1782 DEBUG(1, ("Could not open %s\n", user_file));
1783 ret = NT_STATUS_UNSUCCESSFUL;
1784 goto done;
1787 add_name = talloc_strdup(mem_ctx, add_template);
1788 mod_name = talloc_strdup(mem_ctx, mod_template);
1789 if (!add_name || !mod_name) {
1790 ret = NT_STATUS_NO_MEMORY;
1791 goto done;
1794 /* Open the add and mod ldif files */
1795 if (!(add_file = fdopen(smb_mkstemp(add_name),"w"))) {
1796 DEBUG(1, ("Could not open %s\n", add_name));
1797 ret = NT_STATUS_UNSUCCESSFUL;
1798 goto done;
1800 if (!(mod_file = fdopen(smb_mkstemp(mod_name),"w"))) {
1801 DEBUG(1, ("Could not open %s\n", mod_name));
1802 ret = NT_STATUS_UNSUCCESSFUL;
1803 goto done;
1806 /* Get the sid */
1807 sid_to_string(sid, &dom_sid);
1809 /* Get the ldap suffix */
1810 suffix = lp_ldap_suffix();
1811 if (suffix == NULL || strcmp(suffix, "") == 0) {
1812 DEBUG(0,("ldap suffix missing from smb.conf--exiting\n"));
1813 exit(1);
1816 /* Get other smb.conf data */
1817 if (!(lp_workgroup()) || !*(lp_workgroup())) {
1818 DEBUG(0,("workgroup missing from smb.conf--exiting\n"));
1819 exit(1);
1822 /* Allocate initial memory for groupmap and accountmap arrays */
1823 if (init_ldap == 1) {
1824 groupmap = SMB_MALLOC_ARRAY(GROUPMAP, 8);
1825 accountmap = SMB_MALLOC_ARRAY(ACCOUNTMAP, 8);
1826 if (groupmap == NULL || accountmap == NULL) {
1827 DEBUG(1,("GROUPMAP malloc failed\n"));
1828 ret = NT_STATUS_NO_MEMORY;
1829 goto done;
1832 /* Initialize the arrays */
1833 memset(groupmap, 0, sizeof(GROUPMAP)*8);
1834 memset(accountmap, 0, sizeof(ACCOUNTMAP)*8);
1836 /* Remember how many we malloced */
1837 num_alloced = 8;
1839 /* Initial database population */
1840 populate_ldap_for_ldif(sid, suffix, builtin_sid, add_file);
1841 map_populate_groups(groupmap, accountmap, sid, suffix,
1842 builtin_sid);
1844 /* Don't do this again */
1845 init_ldap = 0;
1848 /* Announce what we are doing */
1849 switch( db_type ) {
1850 case SAM_DATABASE_DOMAIN:
1851 d_fprintf(stderr, "Fetching DOMAIN database\n");
1852 break;
1853 case SAM_DATABASE_BUILTIN:
1854 d_fprintf(stderr, "Fetching BUILTIN database\n");
1855 break;
1856 case SAM_DATABASE_PRIVS:
1857 d_fprintf(stderr, "Fetching PRIVS databases\n");
1858 break;
1859 default:
1860 d_fprintf(stderr,
1861 "Fetching unknown database type %u\n",
1862 db_type );
1863 break;
1866 do {
1867 result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx,
1868 db_type, sync_context,
1869 &num_deltas, &hdr_deltas,
1870 &deltas);
1871 if (!NT_STATUS_IS_OK(result) &&
1872 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1873 ret = NT_STATUS_OK;
1874 goto done; /* is this correct? jmcd */
1877 /* Re-allocate memory for groupmap and accountmap arrays */
1878 groupmap = SMB_REALLOC_ARRAY(groupmap, GROUPMAP,
1879 num_deltas+num_alloced);
1880 accountmap = SMB_REALLOC_ARRAY(accountmap, ACCOUNTMAP,
1881 num_deltas+num_alloced);
1882 if (groupmap == NULL || accountmap == NULL) {
1883 DEBUG(1,("GROUPMAP malloc failed\n"));
1884 ret = NT_STATUS_NO_MEMORY;
1885 goto done;
1888 /* Initialize the new records */
1889 memset(&groupmap[num_alloced], 0,
1890 sizeof(GROUPMAP)*num_deltas);
1891 memset(&accountmap[num_alloced], 0,
1892 sizeof(ACCOUNTMAP)*num_deltas);
1894 /* Remember how many we alloced this time */
1895 num_alloced += num_deltas;
1897 /* Loop through the deltas */
1898 for (k=0; k<num_deltas; k++) {
1899 switch(hdr_deltas[k].type) {
1900 case SAM_DELTA_DOMAIN_INFO:
1901 /* Is this case needed? */
1902 unistr2_to_ascii(domainname,
1903 &deltas[k].domain_info.uni_dom_name,
1904 sizeof(domainname)-1);
1905 break;
1907 case SAM_DELTA_GROUP_INFO:
1908 fetch_group_info_to_ldif(
1909 &deltas[k], &groupmap[g_index],
1910 add_file, sid, suffix);
1911 g_index++;
1912 break;
1914 case SAM_DELTA_ACCOUNT_INFO:
1915 fetch_account_info_to_ldif(
1916 &deltas[k], groupmap,
1917 &accountmap[a_index], add_file,
1918 sid, suffix, num_alloced);
1919 a_index++;
1920 break;
1922 case SAM_DELTA_ALIAS_INFO:
1923 fetch_alias_info_to_ldif(
1924 &deltas[k], &groupmap[g_index],
1925 add_file, sid, suffix, db_type);
1926 g_index++;
1927 break;
1929 case SAM_DELTA_GROUP_MEM:
1930 fetch_groupmem_info_to_ldif(
1931 &deltas[k], &hdr_deltas[k],
1932 groupmap, accountmap,
1933 mod_file, num_alloced);
1934 break;
1936 case SAM_DELTA_ALIAS_MEM:
1937 break;
1938 case SAM_DELTA_POLICY_INFO:
1939 break;
1940 case SAM_DELTA_PRIVS_INFO:
1941 break;
1942 case SAM_DELTA_TRUST_DOMS:
1943 /* Implemented but broken */
1944 break;
1945 case SAM_DELTA_SECRET_INFO:
1946 /* Implemented but broken */
1947 break;
1948 case SAM_DELTA_RENAME_GROUP:
1949 /* Not yet implemented */
1950 break;
1951 case SAM_DELTA_RENAME_USER:
1952 /* Not yet implemented */
1953 break;
1954 case SAM_DELTA_RENAME_ALIAS:
1955 /* Not yet implemented */
1956 break;
1957 case SAM_DELTA_DELETE_GROUP:
1958 /* Not yet implemented */
1959 break;
1960 case SAM_DELTA_DELETE_USER:
1961 /* Not yet implemented */
1962 break;
1963 case SAM_DELTA_MODIFIED_COUNT:
1964 break;
1965 default:
1966 break;
1967 } /* end of switch */
1968 } /* end of for loop */
1970 /* Increment sync_context */
1971 sync_context += 1;
1973 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1975 /* Write ldif data to the user's file */
1976 if (db_type == SAM_DATABASE_DOMAIN) {
1977 fprintf(ldif_file,
1978 "# SAM_DATABASE_DOMAIN: ADD ENTITIES\n");
1979 fprintf(ldif_file,
1980 "# =================================\n\n");
1981 fflush(ldif_file);
1982 } else if (db_type == SAM_DATABASE_BUILTIN) {
1983 fprintf(ldif_file,
1984 "# SAM_DATABASE_BUILTIN: ADD ENTITIES\n");
1985 fprintf(ldif_file,
1986 "# ==================================\n\n");
1987 fflush(ldif_file);
1989 fseek(add_file, 0, SEEK_SET);
1990 transfer_file(fileno(add_file), fileno(ldif_file), (size_t) -1);
1992 if (db_type == SAM_DATABASE_DOMAIN) {
1993 fprintf(ldif_file,
1994 "# SAM_DATABASE_DOMAIN: MODIFY ENTITIES\n");
1995 fprintf(ldif_file,
1996 "# ====================================\n\n");
1997 fflush(ldif_file);
1998 } else if (db_type == SAM_DATABASE_BUILTIN) {
1999 fprintf(ldif_file,
2000 "# SAM_DATABASE_BUILTIN: MODIFY ENTITIES\n");
2001 fprintf(ldif_file,
2002 "# =====================================\n\n");
2003 fflush(ldif_file);
2005 fseek(mod_file, 0, SEEK_SET);
2006 transfer_file(fileno(mod_file), fileno(ldif_file), (size_t) -1);
2009 done:
2010 /* Close and delete the ldif files */
2011 if (add_file) {
2012 fclose(add_file);
2015 if ((add_name != NULL) && strcmp(add_name, add_template) && (unlink(add_name))) {
2016 DEBUG(1,("unlink(%s) failed, error was (%s)\n",
2017 add_name, strerror(errno)));
2020 if (mod_file) {
2021 fclose(mod_file);
2024 if ((mod_name != NULL) && strcmp(mod_name, mod_template) && (unlink(mod_name))) {
2025 DEBUG(1,("unlink(%s) failed, error was (%s)\n",
2026 mod_name, strerror(errno)));
2029 if (ldif_file && (ldif_file != stdout)) {
2030 fclose(ldif_file);
2033 /* Deallocate memory for the mapping arrays */
2034 SAFE_FREE(groupmap);
2035 SAFE_FREE(accountmap);
2037 /* Return */
2038 talloc_destroy(mem_ctx);
2039 return ret;
2042 /**
2043 * Basic usage function for 'net rpc vampire'
2044 * @param argc Standard main() style argc
2045 * @param argc Standard main() style argv. Initial components are already
2046 * stripped
2049 int rpc_vampire_usage(int argc, const char **argv)
2051 d_printf("net rpc vampire [ldif [<ldif-filename>] [options]\n"\
2052 "\t to pull accounts from a remote PDC where we are a BDC\n"\
2053 "\t\t no args puts accounts in local passdb from smb.conf\n"\
2054 "\t\t ldif - put accounts in ldif format (file defaults to /tmp/tmp.ldif\n");
2056 net_common_flags_usage(argc, argv);
2057 return -1;
2061 /* dump sam database via samsync rpc calls */
2062 NTSTATUS rpc_vampire_internals(const DOM_SID *domain_sid,
2063 const char *domain_name,
2064 struct cli_state *cli,
2065 struct rpc_pipe_client *pipe_hnd,
2066 TALLOC_CTX *mem_ctx,
2067 int argc,
2068 const char **argv)
2070 NTSTATUS result;
2071 fstring my_dom_sid_str;
2072 fstring rem_dom_sid_str;
2074 if (!sid_equal(domain_sid, get_global_sam_sid())) {
2075 d_printf("Cannot import users from %s at this time, "
2076 "as the current domain:\n\t%s: %s\nconflicts "
2077 "with the remote domain\n\t%s: %s\n"
2078 "Perhaps you need to set: \n\n\tsecurity=user\n\tworkgroup=%s\n\n in your smb.conf?\n",
2079 domain_name,
2080 get_global_sam_name(), sid_to_string(my_dom_sid_str,
2081 get_global_sam_sid()),
2082 domain_name, sid_to_string(rem_dom_sid_str, domain_sid),
2083 domain_name);
2084 return NT_STATUS_UNSUCCESSFUL;
2087 if (argc >= 1 && (strcmp(argv[0], "ldif") == 0)) {
2088 result = fetch_database_to_ldif(pipe_hnd, SAM_DATABASE_DOMAIN,
2089 *domain_sid, argv[1]);
2090 } else {
2091 result = fetch_database(pipe_hnd, SAM_DATABASE_DOMAIN, *domain_sid);
2094 if (!NT_STATUS_IS_OK(result)) {
2095 d_fprintf(stderr, "Failed to fetch domain database: %s\n",
2096 nt_errstr(result));
2097 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED))
2098 d_fprintf(stderr, "Perhaps %s is a Windows 2000 native "
2099 "mode domain?\n", domain_name);
2100 goto fail;
2103 if (argc >= 1 && (strcmp(argv[0], "ldif") == 0)) {
2104 result = fetch_database_to_ldif(pipe_hnd, SAM_DATABASE_BUILTIN,
2105 global_sid_Builtin, argv[1]);
2106 } else {
2107 result = fetch_database(pipe_hnd, SAM_DATABASE_BUILTIN, global_sid_Builtin);
2110 if (!NT_STATUS_IS_OK(result)) {
2111 d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
2112 nt_errstr(result));
2113 goto fail;
2116 /* Currently we crash on PRIVS somewhere in unmarshalling */
2117 /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */
2119 fail:
2120 return result;