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.
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
)
38 d_printf("Group mem %u: ", rid
);
39 for (i
=0;i
<g
->num_members
;i
++) {
40 d_printf("%u ", g
->rids
[i
]);
45 static const char *display_time(NTTIME
*nttime
)
47 static fstring string
;
52 int days
, hours
, mins
, secs
;
55 if (nttime
->high
==0 && nttime
->low
==0)
58 if (nttime
->high
==0x80000000 && nttime
->low
==0)
65 high
= high
* (~nttime
->high
);
68 low
= low
/(1000*1000*10);
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
);
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
)
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
));
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
);
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
);
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
)
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
);
161 case SAM_DELTA_GROUP_MEM
:
162 display_group_mem_info(hdr_delta
->target_rid
, &delta
->grp_mem_info
);
164 case SAM_DELTA_ALIAS_INFO
:
165 display_alias_info(hdr_delta
->target_rid
, &delta
->alias_info
);
167 case SAM_DELTA_ALIAS_MEM
:
168 display_alias_mem(hdr_delta
->target_rid
, &delta
->als_mem_info
);
170 case SAM_DELTA_DOMAIN_INFO
:
171 display_domain_info(&delta
->domain_info
);
173 case SAM_DELTA_GROUP_INFO
:
174 display_group_info(hdr_delta
->target_rid
, &delta
->group_info
);
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");
180 case SAM_DELTA_RENAME_USER
:
181 d_printf("SAM_DELTA_RENAME_USER not handled\n");
183 case SAM_DELTA_RENAME_ALIAS
:
184 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
186 case SAM_DELTA_POLICY_INFO
:
187 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
189 case SAM_DELTA_TRUST_DOMS
:
190 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
192 case SAM_DELTA_PRIVS_INFO
:
193 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
195 case SAM_DELTA_SECRET_INFO
:
196 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
198 case SAM_DELTA_DELETE_GROUP
:
199 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
201 case SAM_DELTA_DELETE_USER
:
202 d_printf("SAM_DELTA_DELETE_USER not handled\n");
204 case SAM_DELTA_MODIFIED_COUNT
:
205 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
208 d_printf("Unknown delta record type %d\n", hdr_delta
->type
);
213 static void dump_database(struct rpc_pipe_client
*pipe_hnd
, uint32 db_type
)
215 uint32 sync_context
= 0;
219 SAM_DELTA_HDR
*hdr_deltas
;
220 SAM_DELTA_CTR
*deltas
;
223 if (!(mem_ctx
= talloc_init("dump_database"))) {
228 case SAM_DATABASE_DOMAIN
:
229 d_printf("Dumping DOMAIN database\n");
231 case SAM_DATABASE_BUILTIN
:
232 d_printf("Dumping BUILTIN database\n");
234 case SAM_DATABASE_PRIVS
:
235 d_printf("Dumping PRIVS databases\n");
238 d_printf("Dumping unknown database type %u\n", db_type
);
243 result
= rpccli_netlogon_sam_sync(pipe_hnd
, mem_ctx
, db_type
,
245 &num_deltas
, &hdr_deltas
, &deltas
);
246 if (NT_STATUS_IS_ERR(result
))
249 for (i
= 0; i
< num_deltas
; i
++) {
250 display_sam_entry(&hdr_deltas
[i
], &deltas
[i
]);
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
,
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
,
277 NULL
, &sec_channel_type
)) {
278 DEBUG(0,("Could not fetch trust account password\n"));
282 nt_status
= rpccli_netlogon_setup_creds(pipe_hnd
,
290 if (!NT_STATUS_IS_OK(nt_status
)) {
291 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
296 dump_database(pipe_hnd
, SAM_DATABASE_DOMAIN
);
297 dump_database(pipe_hnd
, SAM_DATABASE_BUILTIN
);
298 dump_database(pipe_hnd
, SAM_DATABASE_PRIVS
);
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)) ||\
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
);
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
);
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
);
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
);
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
);
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
);
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
);
391 pdb_set_profile_path(account
, new_string
, PDB_CHANGED
);
394 if (delta
->hdr_parameters
.buffer
) {
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
);
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
);
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
);
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
);
463 /* no last set time, make it now */
464 pdb_set_pass_last_set_time(account
, time(NULL
), PDB_CHANGED
);
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
);
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
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
);
501 static NTSTATUS
fetch_account_info(uint32 rid
, SAM_ACCOUNT_INFO
*delta
)
503 NTSTATUS nt_ret
= NT_STATUS_UNSUCCESSFUL
;
506 struct samu
*sam_account
=NULL
;
511 struct passwd
*passwd
;
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());
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
;
537 all_string_sub(add_script
, "%u", 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
;
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",
563 return NT_STATUS_ACCESS_DENIED
;
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",
572 TALLOC_FREE(sam_account
);
573 return NT_STATUS_ACCESS_DENIED
;
577 group_sid
= *pdb_get_group_sid(sam_account
);
579 if (!pdb_getgrsid(&map
, group_sid
)) {
580 DEBUG(0, ("Primary group of %s has no mapping!\n",
581 pdb_get_username(sam_account
)));
583 if (map
.gid
!= passwd
->pw_gid
) {
584 if (!(grp
= getgrgid(map
.gid
))) {
585 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
586 (unsigned long)map
.gid
, pdb_get_username(sam_account
), sid_string_static(&group_sid
)));
588 smb_set_primary_group(grp
->gr_name
, pdb_get_username(sam_account
));
594 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
595 pdb_get_username(sam_account
)));
599 TALLOC_FREE(sam_account
);
603 static NTSTATUS
fetch_group_info(uint32 rid
, SAM_GROUP_INFO
*delta
)
607 struct group
*grp
= NULL
;
613 unistr2_to_ascii(name
, &delta
->uni_grp_name
, sizeof(name
)-1);
614 unistr2_to_ascii(comment
, &delta
->uni_grp_desc
, sizeof(comment
)-1);
616 /* add the group to the mapping table */
617 sid_copy(&group_sid
, get_global_sam_sid());
618 sid_append_rid(&group_sid
, rid
);
619 sid_to_string(sid_string
, &group_sid
);
621 if (pdb_getgrsid(&map
, group_sid
)) {
623 grp
= getgrgid(map
.gid
);
630 /* No group found from mapping, find it from its name. */
631 if ((grp
= getgrnam(name
)) == NULL
) {
633 /* No appropriate group found, create one */
635 d_printf("Creating unix group: '%s'\n", name
);
637 if (smb_create_group(name
, &gid
) != 0)
638 return NT_STATUS_ACCESS_DENIED
;
640 if ((grp
= getgrnam(name
)) == NULL
)
641 return NT_STATUS_ACCESS_DENIED
;
645 map
.gid
= grp
->gr_gid
;
647 map
.sid_name_use
= SID_NAME_DOM_GRP
;
648 fstrcpy(map
.nt_name
, name
);
649 if (delta
->hdr_grp_desc
.buffer
) {
650 fstrcpy(map
.comment
, comment
);
652 fstrcpy(map
.comment
, "");
656 pdb_add_group_mapping_entry(&map
);
658 pdb_update_group_mapping_entry(&map
);
663 static NTSTATUS
fetch_group_mem_info(uint32 rid
, SAM_GROUP_MEM_INFO
*delta
)
666 TALLOC_CTX
*t
= NULL
;
667 char **nt_members
= NULL
;
673 if (delta
->num_members
== 0) {
677 sid_copy(&group_sid
, get_global_sam_sid());
678 sid_append_rid(&group_sid
, rid
);
680 if (!get_domain_group_from_sid(group_sid
, &map
)) {
681 DEBUG(0, ("Could not find global group %d\n", rid
));
682 return NT_STATUS_NO_SUCH_GROUP
;
685 if (!(grp
= getgrgid(map
.gid
))) {
686 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map
.gid
));
687 return NT_STATUS_NO_SUCH_GROUP
;
690 d_printf("Group members of %s: ", grp
->gr_name
);
692 if (!(t
= talloc_init("fetch_group_mem_info"))) {
693 DEBUG(0, ("could not talloc_init\n"));
694 return NT_STATUS_NO_MEMORY
;
697 nt_members
= TALLOC_ZERO_ARRAY(t
, char *, delta
->num_members
);
699 for (i
=0; i
<delta
->num_members
; i
++) {
700 struct samu
*member
= NULL
;
703 if ( !(member
= samu_new(t
)) ) {
705 return NT_STATUS_NO_MEMORY
;
708 sid_copy(&member_sid
, get_global_sam_sid());
709 sid_append_rid(&member_sid
, delta
->rids
[i
]);
711 if (!pdb_getsampwsid(member
, &member_sid
)) {
712 DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
713 delta
->rids
[i
], sid_string_static(&member_sid
), grp
->gr_name
));
718 if (pdb_get_group_rid(member
) == rid
) {
719 d_printf("%s(primary),", pdb_get_username(member
));
724 d_printf("%s,", pdb_get_username(member
));
725 nt_members
[i
] = talloc_strdup(t
, pdb_get_username(member
));
731 unix_members
= grp
->gr_mem
;
733 while (*unix_members
) {
734 BOOL is_nt_member
= False
;
735 for (i
=0; i
<delta
->num_members
; i
++) {
736 if (nt_members
[i
] == NULL
) {
737 /* This was a primary group */
741 if (strcmp(*unix_members
, nt_members
[i
]) == 0) {
747 /* We look at a unix group member that is not
748 an nt group member. So, remove it. NT is
750 smb_delete_user_group(grp
->gr_name
, *unix_members
);
755 for (i
=0; i
<delta
->num_members
; i
++) {
756 BOOL is_unix_member
= False
;
758 if (nt_members
[i
] == NULL
) {
759 /* This was the primary group */
763 unix_members
= grp
->gr_mem
;
765 while (*unix_members
) {
766 if (strcmp(*unix_members
, nt_members
[i
]) == 0) {
767 is_unix_member
= True
;
773 if (!is_unix_member
) {
774 /* We look at a nt group member that is not a
775 unix group member currently. So, add the nt
777 smb_add_user_group(grp
->gr_name
, nt_members
[i
]);
785 static NTSTATUS
fetch_alias_info(uint32 rid
, SAM_ALIAS_INFO
*delta
,
790 struct group
*grp
= NULL
;
796 unistr2_to_ascii(name
, &delta
->uni_als_name
, sizeof(name
)-1);
797 unistr2_to_ascii(comment
, &delta
->uni_als_desc
, sizeof(comment
)-1);
799 /* Find out whether the group is already mapped */
800 sid_copy(&alias_sid
, &dom_sid
);
801 sid_append_rid(&alias_sid
, rid
);
802 sid_to_string(sid_string
, &alias_sid
);
804 if (pdb_getgrsid(&map
, alias_sid
)) {
805 grp
= getgrgid(map
.gid
);
812 /* No group found from mapping, find it from its name. */
813 if ((grp
= getgrnam(name
)) == NULL
) {
814 /* No appropriate group found, create one */
815 d_printf("Creating unix group: '%s'\n", name
);
816 if (smb_create_group(name
, &gid
) != 0)
817 return NT_STATUS_ACCESS_DENIED
;
818 if ((grp
= getgrgid(gid
)) == NULL
)
819 return NT_STATUS_ACCESS_DENIED
;
823 map
.gid
= grp
->gr_gid
;
826 if (sid_equal(&dom_sid
, &global_sid_Builtin
))
827 map
.sid_name_use
= SID_NAME_WKN_GRP
;
829 map
.sid_name_use
= SID_NAME_ALIAS
;
831 fstrcpy(map
.nt_name
, name
);
832 fstrcpy(map
.comment
, comment
);
835 pdb_add_group_mapping_entry(&map
);
837 pdb_update_group_mapping_entry(&map
);
842 static NTSTATUS
fetch_alias_mem(uint32 rid
, SAM_ALIAS_MEM_INFO
*delta
, DOM_SID dom_sid
)
847 static NTSTATUS
fetch_domain_info(uint32 rid
, SAM_DOMAIN_INFO
*delta
)
849 time_t u_max_age
, u_min_age
, u_logout
, u_lockoutreset
, u_lockouttime
;
850 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
853 u_max_age
= nt_time_to_unix_abs((NTTIME
*)&delta
->max_pwd_age
);
854 u_min_age
= nt_time_to_unix_abs((NTTIME
*)&delta
->min_pwd_age
);
855 u_logout
= nt_time_to_unix_abs((NTTIME
*)&delta
->force_logoff
);
856 u_lockoutreset
= nt_time_to_unix_abs((NTTIME
*)&delta
->account_lockout
.reset_count
);
857 u_lockouttime
= nt_time_to_unix_abs((NTTIME
*)&delta
->account_lockout
.lockout_duration
);
859 unistr2_to_ascii(domname
, &delta
->uni_dom_name
, sizeof(domname
) - 1);
861 /* we don't handle BUILTIN account policies */
862 if (!strequal(domname
, get_global_sam_name())) {
863 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname
);
868 if (!pdb_set_account_policy(AP_PASSWORD_HISTORY
, delta
->pwd_history_len
))
871 if (!pdb_set_account_policy(AP_MIN_PASSWORD_LEN
, delta
->min_pwd_len
))
874 if (!pdb_set_account_policy(AP_MAX_PASSWORD_AGE
, (uint32
)u_max_age
))
877 if (!pdb_set_account_policy(AP_MIN_PASSWORD_AGE
, (uint32
)u_min_age
))
880 if (!pdb_set_account_policy(AP_TIME_TO_LOGOUT
, (uint32
)u_logout
))
883 if (!pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, delta
->account_lockout
.bad_attempt_lockout
))
886 if (!pdb_set_account_policy(AP_RESET_COUNT_TIME
, (uint32
)u_lockoutreset
/60))
889 if (u_lockouttime
!= -1)
892 if (!pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION
, (uint32
)u_lockouttime
))
895 if (!pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS
, delta
->logon_chgpass
))
902 static void fetch_sam_entry(SAM_DELTA_HDR
*hdr_delta
, SAM_DELTA_CTR
*delta
,
905 switch(hdr_delta
->type
) {
906 case SAM_DELTA_ACCOUNT_INFO
:
907 fetch_account_info(hdr_delta
->target_rid
,
908 &delta
->account_info
);
910 case SAM_DELTA_GROUP_INFO
:
911 fetch_group_info(hdr_delta
->target_rid
,
914 case SAM_DELTA_GROUP_MEM
:
915 fetch_group_mem_info(hdr_delta
->target_rid
,
916 &delta
->grp_mem_info
);
918 case SAM_DELTA_ALIAS_INFO
:
919 fetch_alias_info(hdr_delta
->target_rid
,
920 &delta
->alias_info
, dom_sid
);
922 case SAM_DELTA_ALIAS_MEM
:
923 fetch_alias_mem(hdr_delta
->target_rid
,
924 &delta
->als_mem_info
, dom_sid
);
926 case SAM_DELTA_DOMAIN_INFO
:
927 fetch_domain_info(hdr_delta
->target_rid
,
928 &delta
->domain_info
);
930 /* The following types are recognised but not handled */
931 case SAM_DELTA_RENAME_GROUP
:
932 d_printf("SAM_DELTA_RENAME_GROUP not handled\n");
934 case SAM_DELTA_RENAME_USER
:
935 d_printf("SAM_DELTA_RENAME_USER not handled\n");
937 case SAM_DELTA_RENAME_ALIAS
:
938 d_printf("SAM_DELTA_RENAME_ALIAS not handled\n");
940 case SAM_DELTA_POLICY_INFO
:
941 d_printf("SAM_DELTA_POLICY_INFO not handled\n");
943 case SAM_DELTA_TRUST_DOMS
:
944 d_printf("SAM_DELTA_TRUST_DOMS not handled\n");
946 case SAM_DELTA_PRIVS_INFO
:
947 d_printf("SAM_DELTA_PRIVS_INFO not handled\n");
949 case SAM_DELTA_SECRET_INFO
:
950 d_printf("SAM_DELTA_SECRET_INFO not handled\n");
952 case SAM_DELTA_DELETE_GROUP
:
953 d_printf("SAM_DELTA_DELETE_GROUP not handled\n");
955 case SAM_DELTA_DELETE_USER
:
956 d_printf("SAM_DELTA_DELETE_USER not handled\n");
958 case SAM_DELTA_MODIFIED_COUNT
:
959 d_printf("SAM_DELTA_MODIFIED_COUNT not handled\n");
962 d_printf("Unknown delta record type %d\n", hdr_delta
->type
);
967 static NTSTATUS
fetch_database(struct rpc_pipe_client
*pipe_hnd
, uint32 db_type
, DOM_SID dom_sid
)
969 uint32 sync_context
= 0;
973 SAM_DELTA_HDR
*hdr_deltas
;
974 SAM_DELTA_CTR
*deltas
;
977 if (!(mem_ctx
= talloc_init("fetch_database")))
978 return NT_STATUS_NO_MEMORY
;
981 case SAM_DATABASE_DOMAIN
:
982 d_printf("Fetching DOMAIN database\n");
984 case SAM_DATABASE_BUILTIN
:
985 d_printf("Fetching BUILTIN database\n");
987 case SAM_DATABASE_PRIVS
:
988 d_printf("Fetching PRIVS databases\n");
991 d_printf("Fetching unknown database type %u\n", db_type
);
996 result
= rpccli_netlogon_sam_sync(pipe_hnd
, mem_ctx
,
997 db_type
, sync_context
,
999 &hdr_deltas
, &deltas
);
1001 if (NT_STATUS_IS_OK(result
) ||
1002 NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
1003 for (i
= 0; i
< num_deltas
; i
++) {
1004 fetch_sam_entry(&hdr_deltas
[i
], &deltas
[i
], dom_sid
);
1010 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1012 talloc_destroy(mem_ctx
);
1017 static NTSTATUS
populate_ldap_for_ldif(fstring sid
, const char *suffix
, const char
1018 *builtin_sid
, FILE *add_fd
)
1020 char *user_suffix
, *group_suffix
, *machine_suffix
, *idmap_suffix
;
1021 char *user_attr
=NULL
, *group_attr
=NULL
;
1025 /* Get the suffix attribute */
1026 suffix_attr
= sstring_sub(suffix
, '=', ',');
1027 if (suffix_attr
== NULL
) {
1028 len
= strlen(suffix
);
1029 suffix_attr
= (char*)SMB_MALLOC(len
+1);
1030 memcpy(suffix_attr
, suffix
, len
);
1031 suffix_attr
[len
] = '\0';
1034 /* Write the base */
1035 fprintf(add_fd
, "# %s\n", suffix
);
1036 fprintf(add_fd
, "dn: %s\n", suffix
);
1037 fprintf(add_fd
, "objectClass: dcObject\n");
1038 fprintf(add_fd
, "objectClass: organization\n");
1039 fprintf(add_fd
, "o: %s\n", suffix_attr
);
1040 fprintf(add_fd
, "dc: %s\n", suffix_attr
);
1041 fprintf(add_fd
, "\n");
1044 user_suffix
= lp_ldap_user_suffix();
1045 if (user_suffix
== NULL
) {
1046 return NT_STATUS_NO_MEMORY
;
1048 /* If it exists and is distinct from other containers,
1049 Write the Users entity */
1050 if (*user_suffix
&& strcmp(user_suffix
, suffix
)) {
1051 user_attr
= sstring_sub(lp_ldap_user_suffix(), '=', ',');
1052 fprintf(add_fd
, "# %s\n", user_suffix
);
1053 fprintf(add_fd
, "dn: %s\n", user_suffix
);
1054 fprintf(add_fd
, "objectClass: organizationalUnit\n");
1055 fprintf(add_fd
, "ou: %s\n", user_attr
);
1056 fprintf(add_fd
, "\n");
1061 group_suffix
= lp_ldap_group_suffix();
1062 if (group_suffix
== NULL
) {
1063 return NT_STATUS_NO_MEMORY
;
1065 /* If it exists and is distinct from other containers,
1066 Write the Groups entity */
1067 if (*group_suffix
&& strcmp(group_suffix
, suffix
)) {
1068 group_attr
= sstring_sub(lp_ldap_group_suffix(), '=', ',');
1069 fprintf(add_fd
, "# %s\n", group_suffix
);
1070 fprintf(add_fd
, "dn: %s\n", group_suffix
);
1071 fprintf(add_fd
, "objectClass: organizationalUnit\n");
1072 fprintf(add_fd
, "ou: %s\n", group_attr
);
1073 fprintf(add_fd
, "\n");
1077 /* If it exists and is distinct from other containers,
1078 Write the Computers entity */
1079 machine_suffix
= lp_ldap_machine_suffix();
1080 if (machine_suffix
== NULL
) {
1081 return NT_STATUS_NO_MEMORY
;
1083 if (*machine_suffix
&& strcmp(machine_suffix
, user_suffix
) &&
1084 strcmp(machine_suffix
, suffix
)) {
1085 char *machine_ou
= NULL
;
1086 fprintf(add_fd
, "# %s\n", machine_suffix
);
1087 fprintf(add_fd
, "dn: %s\n", machine_suffix
);
1088 fprintf(add_fd
, "objectClass: organizationalUnit\n");
1089 /* this isn't totally correct as it assumes that
1090 there _must_ be an ou. just fixing memleak now. jmcd */
1091 machine_ou
= sstring_sub(lp_ldap_machine_suffix(), '=', ',');
1092 fprintf(add_fd
, "ou: %s\n", machine_ou
);
1093 SAFE_FREE(machine_ou
);
1094 fprintf(add_fd
, "\n");
1098 /* If it exists and is distinct from other containers,
1099 Write the IdMap entity */
1100 idmap_suffix
= lp_ldap_idmap_suffix();
1101 if (idmap_suffix
== NULL
) {
1102 return NT_STATUS_NO_MEMORY
;
1104 if (*idmap_suffix
&&
1105 strcmp(idmap_suffix
, user_suffix
) &&
1106 strcmp(idmap_suffix
, suffix
)) {
1108 fprintf(add_fd
, "# %s\n", idmap_suffix
);
1109 fprintf(add_fd
, "dn: %s\n", idmap_suffix
);
1110 fprintf(add_fd
, "ObjectClass: organizationalUnit\n");
1111 s
= sstring_sub(lp_ldap_idmap_suffix(), '=', ',');
1112 fprintf(add_fd
, "ou: %s\n", s
);
1114 fprintf(add_fd
, "\n");
1118 /* Write the root entity */
1119 fprintf(add_fd
, "# root, %s, %s\n", user_attr
, suffix
);
1120 fprintf(add_fd
, "dn: uid=root,ou=%s,%s\n", user_attr
, suffix
);
1121 fprintf(add_fd
, "cn: root\n");
1122 fprintf(add_fd
, "sn: root\n");
1123 fprintf(add_fd
, "objectClass: inetOrgPerson\n");
1124 fprintf(add_fd
, "objectClass: sambaSAMAccount\n");
1125 fprintf(add_fd
, "objectClass: posixAccount\n");
1126 fprintf(add_fd
, "objectClass: shadowAccount\n");
1127 fprintf(add_fd
, "gidNumber: 0\n");
1128 fprintf(add_fd
, "uid: root\n");
1129 fprintf(add_fd
, "uidNumber: 0\n");
1130 fprintf(add_fd
, "homeDirectory: /home/root\n");
1131 fprintf(add_fd
, "sambaPwdLastSet: 0\n");
1132 fprintf(add_fd
, "sambaLogonTime: 0\n");
1133 fprintf(add_fd
, "sambaLogoffTime: 2147483647\n");
1134 fprintf(add_fd
, "sambaKickoffTime: 2147483647\n");
1135 fprintf(add_fd
, "sambaPwdCanChange: 0\n");
1136 fprintf(add_fd
, "sambaPwdMustChange: 2147483647\n");
1137 fprintf(add_fd
, "sambaHomePath: \\\\PDC-SRV\root\n");
1138 fprintf(add_fd
, "sambaHomeDrive: H:\n");
1139 fprintf(add_fd
, "sambaProfilePath: \\\\PDC-SRV\\profiles\\root\n");
1140 fprintf(add_fd
, "sambaprimaryGroupSID: %s-512\n", sid
);
1141 fprintf(add_fd
, "sambaLMPassword: XXX\n");
1142 fprintf(add_fd
, "sambaNTPassword: XXX\n");
1143 fprintf(add_fd
, "sambaAcctFlags: [U\n");
1144 fprintf(add_fd
, "sambaSID: %s-500\n", sid
);
1145 fprintf(add_fd
, "loginShell: /bin/false\n");
1146 fprintf(add_fd
, "\n");
1149 /* Write the domain entity */
1150 fprintf(add_fd
, "# %s, %s\n", lp_workgroup(), suffix
);
1151 fprintf(add_fd
, "dn: sambaDomainName=%s,%s\n", lp_workgroup(),
1153 fprintf(add_fd
, "objectClass: sambaDomain\n");
1154 fprintf(add_fd
, "objectClass: sambaUnixIdPool\n");
1155 fprintf(add_fd
, "sambaDomainName: %s\n", lp_workgroup());
1156 fprintf(add_fd
, "sambaSID: %s\n", sid
);
1157 fprintf(add_fd
, "uidNumber: %d\n", ++ldif_uid
);
1158 fprintf(add_fd
, "gidNumber: %d\n", ++ldif_gid
);
1159 fprintf(add_fd
, "\n");
1162 /* Write user nobody entity */
1163 fprintf(add_fd
, "# nobody, %s, %s\n", user_attr
, suffix
);
1164 fprintf(add_fd
, "dn: uid=nobody,ou=%s,%s\n", user_attr
, suffix
);
1165 fprintf(add_fd
, "cn: nobody\n");
1166 fprintf(add_fd
, "sn: nobody\n");
1167 fprintf(add_fd
, "objectClass: inetOrgPerson\n");
1168 fprintf(add_fd
, "objectClass: sambaSAMAccount\n");
1169 fprintf(add_fd
, "objectClass: posixAccount\n");
1170 fprintf(add_fd
, "objectClass: shadowAccount\n");
1171 fprintf(add_fd
, "gidNumber: 514\n");
1172 fprintf(add_fd
, "uid: nobody\n");
1173 fprintf(add_fd
, "uidNumber: 999\n");
1174 fprintf(add_fd
, "homeDirectory: /nobodyshomedir\n");
1175 fprintf(add_fd
, "sambaPwdLastSet: 0\n");
1176 fprintf(add_fd
, "sambaLogonTime: 0\n");
1177 fprintf(add_fd
, "sambaLogoffTime: 2147483647\n");
1178 fprintf(add_fd
, "sambaKickoffTime: 2147483647\n");
1179 fprintf(add_fd
, "sambaPwdCanChange: 0\n");
1180 fprintf(add_fd
, "sambaPwdMustChange: 2147483647\n");
1181 fprintf(add_fd
, "sambaHomePath: \\\\PDC-SMD3\\homes\\nobody\n");
1182 fprintf(add_fd
, "sambaHomeDrive: H:\n");
1183 fprintf(add_fd
, "sambaProfilePath: \\\\PDC-SMB3\\profiles\\nobody\n");
1184 fprintf(add_fd
, "sambaprimaryGroupSID: %s-514\n", sid
);
1185 fprintf(add_fd
, "sambaLMPassword: NOPASSWORDXXXXXXXXXXXXXXXXXXXXX\n");
1186 fprintf(add_fd
, "sambaNTPassword: NOPASSWORDXXXXXXXXXXXXXXXXXXXXX\n");
1187 fprintf(add_fd
, "sambaAcctFlags: [NU\n");
1188 fprintf(add_fd
, "sambaSID: %s-2998\n", sid
);
1189 fprintf(add_fd
, "loginShell: /bin/false\n");
1190 fprintf(add_fd
, "\n");
1193 /* Write the Domain Admins entity */
1194 fprintf(add_fd
, "# Domain Admins, %s, %s\n", group_attr
,
1196 fprintf(add_fd
, "dn: cn=Domain Admins,ou=%s,%s\n", group_attr
,
1198 fprintf(add_fd
, "objectClass: posixGroup\n");
1199 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1200 fprintf(add_fd
, "cn: Domain Admins\n");
1201 fprintf(add_fd
, "memberUid: Administrator\n");
1202 fprintf(add_fd
, "description: Netbios Domain Administrators\n");
1203 fprintf(add_fd
, "gidNumber: 512\n");
1204 fprintf(add_fd
, "sambaSID: %s-512\n", sid
);
1205 fprintf(add_fd
, "sambaGroupType: 2\n");
1206 fprintf(add_fd
, "displayName: Domain Admins\n");
1207 fprintf(add_fd
, "\n");
1210 /* Write the Domain Users entity */
1211 fprintf(add_fd
, "# Domain Users, %s, %s\n", group_attr
,
1213 fprintf(add_fd
, "dn: cn=Domain Users,ou=%s,%s\n", group_attr
,
1215 fprintf(add_fd
, "objectClass: posixGroup\n");
1216 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1217 fprintf(add_fd
, "cn: Domain Users\n");
1218 fprintf(add_fd
, "description: Netbios Domain Users\n");
1219 fprintf(add_fd
, "gidNumber: 513\n");
1220 fprintf(add_fd
, "sambaSID: %s-513\n", sid
);
1221 fprintf(add_fd
, "sambaGroupType: 2\n");
1222 fprintf(add_fd
, "displayName: Domain Users\n");
1223 fprintf(add_fd
, "\n");
1226 /* Write the Domain Guests entity */
1227 fprintf(add_fd
, "# Domain Guests, %s, %s\n", group_attr
,
1229 fprintf(add_fd
, "dn: cn=Domain Guests,ou=%s,%s\n", group_attr
,
1231 fprintf(add_fd
, "objectClass: posixGroup\n");
1232 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1233 fprintf(add_fd
, "cn: Domain Guests\n");
1234 fprintf(add_fd
, "description: Netbios Domain Guests\n");
1235 fprintf(add_fd
, "gidNumber: 514\n");
1236 fprintf(add_fd
, "sambaSID: %s-514\n", sid
);
1237 fprintf(add_fd
, "sambaGroupType: 2\n");
1238 fprintf(add_fd
, "displayName: Domain Guests\n");
1239 fprintf(add_fd
, "\n");
1242 /* Write the Domain Computers entity */
1243 fprintf(add_fd
, "# Domain Computers, %s, %s\n", group_attr
,
1245 fprintf(add_fd
, "dn: cn=Domain Computers,ou=%s,%s\n",
1246 group_attr
, suffix
);
1247 fprintf(add_fd
, "objectClass: posixGroup\n");
1248 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1249 fprintf(add_fd
, "gidNumber: 515\n");
1250 fprintf(add_fd
, "cn: Domain Computers\n");
1251 fprintf(add_fd
, "description: Netbios Domain Computers accounts\n");
1252 fprintf(add_fd
, "sambaSID: %s-515\n", sid
);
1253 fprintf(add_fd
, "sambaGroupType: 2\n");
1254 fprintf(add_fd
, "displayName: Domain Computers\n");
1255 fprintf(add_fd
, "\n");
1258 /* Write the Admininistrators Groups entity */
1259 fprintf(add_fd
, "# Administrators, %s, %s\n", group_attr
,
1261 fprintf(add_fd
, "dn: cn=Administrators,ou=%s,%s\n", group_attr
,
1263 fprintf(add_fd
, "objectClass: posixGroup\n");
1264 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1265 fprintf(add_fd
, "gidNumber: 544\n");
1266 fprintf(add_fd
, "cn: Administrators\n");
1267 fprintf(add_fd
, "description: Netbios Domain Members can fully administer the computer/sambaDomainName\n");
1268 fprintf(add_fd
, "sambaSID: %s-544\n", builtin_sid
);
1269 fprintf(add_fd
, "sambaGroupType: 5\n");
1270 fprintf(add_fd
, "displayName: Administrators\n");
1271 fprintf(add_fd
, "\n");
1273 /* Write the Print Operator entity */
1274 fprintf(add_fd
, "# Print Operators, %s, %s\n", group_attr
,
1276 fprintf(add_fd
, "dn: cn=Print Operators,ou=%s,%s\n",
1277 group_attr
, suffix
);
1278 fprintf(add_fd
, "objectClass: posixGroup\n");
1279 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1280 fprintf(add_fd
, "gidNumber: 550\n");
1281 fprintf(add_fd
, "cn: Print Operators\n");
1282 fprintf(add_fd
, "description: Netbios Domain Print Operators\n");
1283 fprintf(add_fd
, "sambaSID: %s-550\n", builtin_sid
);
1284 fprintf(add_fd
, "sambaGroupType: 5\n");
1285 fprintf(add_fd
, "displayName: Print Operators\n");
1286 fprintf(add_fd
, "\n");
1289 /* Write the Backup Operators entity */
1290 fprintf(add_fd
, "# Backup Operators, %s, %s\n", group_attr
,
1292 fprintf(add_fd
, "dn: cn=Backup Operators,ou=%s,%s\n",
1293 group_attr
, suffix
);
1294 fprintf(add_fd
, "objectClass: posixGroup\n");
1295 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1296 fprintf(add_fd
, "gidNumber: 551\n");
1297 fprintf(add_fd
, "cn: Backup Operators\n");
1298 fprintf(add_fd
, "description: Netbios Domain Members can bypass file security to back up files\n");
1299 fprintf(add_fd
, "sambaSID: %s-551\n", builtin_sid
);
1300 fprintf(add_fd
, "sambaGroupType: 5\n");
1301 fprintf(add_fd
, "displayName: Backup Operators\n");
1302 fprintf(add_fd
, "\n");
1305 /* Write the Replicators entity */
1306 fprintf(add_fd
, "# Replicators, %s, %s\n", group_attr
, suffix
);
1307 fprintf(add_fd
, "dn: cn=Replicators,ou=%s,%s\n", group_attr
,
1309 fprintf(add_fd
, "objectClass: posixGroup\n");
1310 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1311 fprintf(add_fd
, "gidNumber: 552\n");
1312 fprintf(add_fd
, "cn: Replicators\n");
1313 fprintf(add_fd
, "description: Netbios Domain Supports file replication in a sambaDomainName\n");
1314 fprintf(add_fd
, "sambaSID: %s-552\n", builtin_sid
);
1315 fprintf(add_fd
, "sambaGroupType: 5\n");
1316 fprintf(add_fd
, "displayName: Replicators\n");
1317 fprintf(add_fd
, "\n");
1320 /* Deallocate memory, and return */
1321 SAFE_FREE(suffix_attr
);
1322 SAFE_FREE(user_attr
);
1323 SAFE_FREE(group_attr
);
1324 return NT_STATUS_OK
;
1327 static NTSTATUS
map_populate_groups(GROUPMAP
*groupmap
, ACCOUNTMAP
*accountmap
, fstring sid
,
1328 const char *suffix
, const char *builtin_sid
)
1330 char *group_attr
= sstring_sub(lp_ldap_group_suffix(), '=', ',');
1332 /* Map the groups created by populate_ldap_for_ldif */
1333 groupmap
[0].rid
= 512;
1334 groupmap
[0].gidNumber
= 512;
1335 pstr_sprintf(groupmap
[0].sambaSID
, "%s-512", sid
);
1336 pstr_sprintf(groupmap
[0].group_dn
, "cn=Domain Admins,ou=%s,%s",
1337 group_attr
, suffix
);
1338 accountmap
[0].rid
= 512;
1339 pstr_sprintf(accountmap
[0].cn
, "%s", "Domain Admins");
1341 groupmap
[1].rid
= 513;
1342 groupmap
[1].gidNumber
= 513;
1343 pstr_sprintf(groupmap
[1].sambaSID
, "%s-513", sid
);
1344 pstr_sprintf(groupmap
[1].group_dn
, "cn=Domain Users,ou=%s,%s",
1345 group_attr
, suffix
);
1346 accountmap
[1].rid
= 513;
1347 pstr_sprintf(accountmap
[1].cn
, "%s", "Domain Users");
1349 groupmap
[2].rid
= 514;
1350 groupmap
[2].gidNumber
= 514;
1351 pstr_sprintf(groupmap
[2].sambaSID
, "%s-514", sid
);
1352 pstr_sprintf(groupmap
[2].group_dn
, "cn=Domain Guests,ou=%s,%s",
1353 group_attr
, suffix
);
1354 accountmap
[2].rid
= 514;
1355 pstr_sprintf(accountmap
[2].cn
, "%s", "Domain Guests");
1357 groupmap
[3].rid
= 515;
1358 groupmap
[3].gidNumber
= 515;
1359 pstr_sprintf(groupmap
[3].sambaSID
, "%s-515", sid
);
1360 pstr_sprintf(groupmap
[3].group_dn
, "cn=Domain Computers,ou=%s,%s",
1361 group_attr
, suffix
);
1362 accountmap
[3].rid
= 515;
1363 pstr_sprintf(accountmap
[3].cn
, "%s", "Domain Computers");
1365 groupmap
[4].rid
= 544;
1366 groupmap
[4].gidNumber
= 544;
1367 pstr_sprintf(groupmap
[4].sambaSID
, "%s-544", builtin_sid
);
1368 pstr_sprintf(groupmap
[4].group_dn
, "cn=Administrators,ou=%s,%s",
1369 group_attr
, suffix
);
1370 accountmap
[4].rid
= 515;
1371 pstr_sprintf(accountmap
[4].cn
, "%s", "Administrators");
1373 groupmap
[5].rid
= 550;
1374 groupmap
[5].gidNumber
= 550;
1375 pstr_sprintf(groupmap
[5].sambaSID
, "%s-550", builtin_sid
);
1376 pstr_sprintf(groupmap
[5].group_dn
, "cn=Print Operators,ou=%s,%s",
1377 group_attr
, suffix
);
1378 accountmap
[5].rid
= 550;
1379 pstr_sprintf(accountmap
[5].cn
, "%s", "Print Operators");
1381 groupmap
[6].rid
= 551;
1382 groupmap
[6].gidNumber
= 551;
1383 pstr_sprintf(groupmap
[6].sambaSID
, "%s-551", builtin_sid
);
1384 pstr_sprintf(groupmap
[6].group_dn
, "cn=Backup Operators,ou=%s,%s",
1385 group_attr
, suffix
);
1386 accountmap
[6].rid
= 551;
1387 pstr_sprintf(accountmap
[6].cn
, "%s", "Backup Operators");
1389 groupmap
[7].rid
= 552;
1390 groupmap
[7].gidNumber
= 552;
1391 pstr_sprintf(groupmap
[7].sambaSID
, "%s-552", builtin_sid
);
1392 pstr_sprintf(groupmap
[7].group_dn
, "cn=Replicators,ou=%s,%s",
1393 group_attr
, suffix
);
1394 accountmap
[7].rid
= 551;
1395 pstr_sprintf(accountmap
[7].cn
, "%s", "Replicators");
1396 SAFE_FREE(group_attr
);
1397 return NT_STATUS_OK
;
1400 static NTSTATUS
fetch_group_info_to_ldif(SAM_DELTA_CTR
*delta
, GROUPMAP
*groupmap
,
1401 FILE *add_fd
, fstring sid
, char *suffix
)
1404 uint32 grouptype
= 0, g_rid
= 0;
1405 char *group_attr
= sstring_sub(lp_ldap_group_suffix(), '=', ',');
1407 /* Get the group name */
1408 unistr2_to_ascii(groupname
,
1409 &(delta
->group_info
.uni_grp_name
),
1410 sizeof(groupname
)-1);
1412 /* Set up the group type (always 2 for group info) */
1415 /* These groups are entered by populate_ldap_for_ldif */
1416 if (strcmp(groupname
, "Domain Admins") == 0 ||
1417 strcmp(groupname
, "Domain Users") == 0 ||
1418 strcmp(groupname
, "Domain Guests") == 0 ||
1419 strcmp(groupname
, "Domain Computers") == 0 ||
1420 strcmp(groupname
, "Administrators") == 0 ||
1421 strcmp(groupname
, "Print Operators") == 0 ||
1422 strcmp(groupname
, "Backup Operators") == 0 ||
1423 strcmp(groupname
, "Replicators") == 0) {
1424 SAFE_FREE(group_attr
);
1425 return NT_STATUS_OK
;
1427 /* Increment the gid for the new group */
1431 /* Map the group rid, gid, and dn */
1432 g_rid
= delta
->group_info
.gid
.g_rid
;
1433 groupmap
->rid
= g_rid
;
1434 groupmap
->gidNumber
= ldif_gid
;
1435 pstr_sprintf(groupmap
->sambaSID
, "%s-%d", sid
, g_rid
);
1436 pstr_sprintf(groupmap
->group_dn
,
1437 "cn=%s,ou=%s,%s", groupname
, group_attr
, suffix
);
1439 /* Write the data to the temporary add ldif file */
1440 fprintf(add_fd
, "# %s, %s, %s\n", groupname
, group_attr
,
1442 fprintf(add_fd
, "dn: cn=%s,ou=%s,%s\n", groupname
, group_attr
,
1444 fprintf(add_fd
, "objectClass: posixGroup\n");
1445 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1446 fprintf(add_fd
, "cn: %s\n", groupname
);
1447 fprintf(add_fd
, "gidNumber: %d\n", ldif_gid
);
1448 fprintf(add_fd
, "sambaSID: %s\n", groupmap
->sambaSID
);
1449 fprintf(add_fd
, "sambaGroupType: %d\n", grouptype
);
1450 fprintf(add_fd
, "displayName: %s\n", groupname
);
1451 fprintf(add_fd
, "\n");
1454 SAFE_FREE(group_attr
);
1456 return NT_STATUS_OK
;
1459 static NTSTATUS
fetch_account_info_to_ldif(SAM_DELTA_CTR
*delta
, GROUPMAP
*groupmap
,
1460 ACCOUNTMAP
*accountmap
, FILE *add_fd
,
1461 fstring sid
, char *suffix
, int alloced
)
1463 fstring username
, logonscript
, homedrive
, homepath
= "", homedir
= "";
1464 fstring hex_nt_passwd
, hex_lm_passwd
;
1465 fstring description
, fullname
, sambaSID
;
1466 uchar lm_passwd
[16], nt_passwd
[16];
1467 char *flags
, *user_rdn
;
1468 const char* nopasswd
= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
1469 static uchar zero_buf
[16];
1470 uint32 rid
= 0, group_rid
= 0, gidNumber
= 0;
1474 /* Get the username */
1475 unistr2_to_ascii(username
,
1476 &(delta
->account_info
.uni_acct_name
),
1477 sizeof(username
)-1);
1480 rid
= delta
->account_info
.user_rid
;
1482 /* Map the rid and username for group member info later */
1483 accountmap
->rid
= rid
;
1484 pstr_sprintf(accountmap
->cn
, "%s", username
);
1486 /* Get the home directory */
1487 if (delta
->account_info
.acb_info
& ACB_NORMAL
) {
1488 unistr2_to_ascii(homedir
, &(delta
->account_info
.uni_home_dir
),
1491 pstr_sprintf(homedir
, "/home/%s", username
);
1493 pstr_sprintf(homedir
, "/nobodyshomedir");
1497 /* Get the logon script */
1498 unistr2_to_ascii(logonscript
, &(delta
->account_info
.uni_logon_script
),
1499 sizeof(logonscript
)-1);
1501 /* Get the home drive */
1502 unistr2_to_ascii(homedrive
, &(delta
->account_info
.uni_dir_drive
),
1503 sizeof(homedrive
)-1);
1505 /* Get the description */
1506 unistr2_to_ascii(description
, &(delta
->account_info
.uni_acct_desc
),
1507 sizeof(description
)-1);
1508 if (!*description
) {
1509 pstr_sprintf(description
, "System User");
1512 /* Get the display name */
1513 unistr2_to_ascii(fullname
, &(delta
->account_info
.uni_full_name
),
1514 sizeof(fullname
)-1);
1516 /* Get lm and nt password data */
1517 if (memcmp(delta
->account_info
.pass
.buf_lm_pwd
, zero_buf
, 16) != 0) {
1518 sam_pwd_hash(delta
->account_info
.user_rid
,
1519 delta
->account_info
.pass
.buf_lm_pwd
,
1521 pdb_sethexpwd(hex_lm_passwd
, lm_passwd
,
1522 delta
->account_info
.acb_info
);
1524 pdb_sethexpwd(hex_lm_passwd
, NULL
, 0);
1526 if (memcmp(delta
->account_info
.pass
.buf_nt_pwd
, zero_buf
, 16) != 0) {
1527 sam_pwd_hash(delta
->account_info
.user_rid
,
1528 delta
->account_info
.pass
.buf_nt_pwd
,
1530 pdb_sethexpwd(hex_nt_passwd
, nt_passwd
,
1531 delta
->account_info
.acb_info
);
1533 pdb_sethexpwd(hex_nt_passwd
, NULL
, 0);
1535 unix_time
= nt_time_to_unix(&(delta
->account_info
.pwd_last_set_time
));
1537 /* The nobody user is entered by populate_ldap_for_ldif */
1538 if (strcmp(username
, "nobody") == 0) {
1539 return NT_STATUS_OK
;
1541 /* Increment the uid for the new user */
1545 /* Set up group id and sambaSID for the user */
1546 group_rid
= delta
->account_info
.group_rid
;
1547 for (i
=0; i
<alloced
; i
++) {
1548 if (groupmap
[i
].rid
== group_rid
) break;
1551 DEBUG(1, ("Could not find rid %d in groupmap array\n",
1553 return NT_STATUS_UNSUCCESSFUL
;
1555 gidNumber
= groupmap
[i
].gidNumber
;
1556 pstr_sprintf(sambaSID
, groupmap
[i
].sambaSID
);
1558 /* Set up sambaAcctFlags */
1559 flags
= pdb_encode_acct_ctrl(delta
->account_info
.acb_info
,
1560 NEW_PW_FORMAT_SPACE_PADDED_LEN
);
1562 /* Add the user to the temporary add ldif file */
1563 /* this isn't quite right...we can't assume there's just OU=. jmcd */
1564 user_rdn
= sstring_sub(lp_ldap_user_suffix(), '=', ',');
1565 fprintf(add_fd
, "# %s, %s, %s\n", username
, user_rdn
, suffix
);
1566 fprintf(add_fd
, "dn: uid=%s,ou=%s,%s\n", username
, user_rdn
, suffix
);
1567 SAFE_FREE(user_rdn
);
1568 fprintf(add_fd
, "ObjectClass: top\n");
1569 fprintf(add_fd
, "objectClass: inetOrgPerson\n");
1570 fprintf(add_fd
, "objectClass: posixAccount\n");
1571 fprintf(add_fd
, "objectClass: shadowAccount\n");
1572 fprintf(add_fd
, "objectClass: sambaSamAccount\n");
1573 fprintf(add_fd
, "cn: %s\n", username
);
1574 fprintf(add_fd
, "sn: %s\n", username
);
1575 fprintf(add_fd
, "uid: %s\n", username
);
1576 fprintf(add_fd
, "uidNumber: %d\n", ldif_uid
);
1577 fprintf(add_fd
, "gidNumber: %d\n", gidNumber
);
1578 fprintf(add_fd
, "homeDirectory: %s\n", homedir
);
1580 fprintf(add_fd
, "SambaHomePath: %s\n", homepath
);
1582 fprintf(add_fd
, "SambaHomeDrive: %s\n", homedrive
);
1584 fprintf(add_fd
, "SambaLogonScript: %s\n", logonscript
);
1585 fprintf(add_fd
, "loginShell: %s\n",
1586 ((delta
->account_info
.acb_info
& ACB_NORMAL
) ?
1587 "/bin/bash" : "/bin/false"));
1588 fprintf(add_fd
, "gecos: System User\n");
1589 fprintf(add_fd
, "description: %s\n", description
);
1590 fprintf(add_fd
, "sambaSID: %s-%d\n", sid
, rid
);
1591 fprintf(add_fd
, "sambaPrimaryGroupSID: %s\n", sambaSID
);
1593 fprintf(add_fd
, "displayName: %s\n", fullname
);
1594 if (strcmp(nopasswd
, hex_lm_passwd
) != 0)
1595 fprintf(add_fd
, "sambaLMPassword: %s\n", hex_lm_passwd
);
1596 if (strcmp(nopasswd
, hex_nt_passwd
) != 0)
1597 fprintf(add_fd
, "sambaNTPassword: %s\n", hex_nt_passwd
);
1598 fprintf(add_fd
, "sambaPwdLastSet: %d\n", (int)unix_time
);
1599 fprintf(add_fd
, "sambaAcctFlags: %s\n", flags
);
1600 fprintf(add_fd
, "\n");
1604 return NT_STATUS_OK
;
1607 static NTSTATUS
fetch_alias_info_to_ldif(SAM_DELTA_CTR
*delta
, GROUPMAP
*groupmap
,
1608 FILE *add_fd
, fstring sid
, char *suffix
,
1611 fstring aliasname
, description
;
1612 uint32 grouptype
= 0, g_rid
= 0;
1613 char *group_attr
= sstring_sub(lp_ldap_group_suffix(), '=', ',');
1615 /* Get the alias name */
1616 unistr2_to_ascii(aliasname
, &(delta
->alias_info
.uni_als_name
),
1617 sizeof(aliasname
)-1);
1619 /* Get the alias description */
1620 unistr2_to_ascii(description
, &(delta
->alias_info
.uni_als_desc
),
1621 sizeof(description
)-1);
1623 /* Set up the group type */
1625 case SAM_DATABASE_DOMAIN
:
1628 case SAM_DATABASE_BUILTIN
:
1637 These groups are entered by populate_ldap_for_ldif
1638 Note that populate creates a group called Relicators,
1639 but NT returns a group called Replicator
1641 if (strcmp(aliasname
, "Domain Admins") == 0 ||
1642 strcmp(aliasname
, "Domain Users") == 0 ||
1643 strcmp(aliasname
, "Domain Guests") == 0 ||
1644 strcmp(aliasname
, "Domain Computers") == 0 ||
1645 strcmp(aliasname
, "Administrators") == 0 ||
1646 strcmp(aliasname
, "Print Operators") == 0 ||
1647 strcmp(aliasname
, "Backup Operators") == 0 ||
1648 strcmp(aliasname
, "Replicator") == 0) {
1649 SAFE_FREE(group_attr
);
1650 return NT_STATUS_OK
;
1652 /* Increment the gid for the new group */
1656 /* Map the group rid and gid */
1657 g_rid
= delta
->group_info
.gid
.g_rid
;
1658 groupmap
->gidNumber
= ldif_gid
;
1659 pstr_sprintf(groupmap
->sambaSID
, "%s-%d", sid
, g_rid
);
1661 /* Write the data to the temporary add ldif file */
1662 fprintf(add_fd
, "# %s, %s, %s\n", aliasname
, group_attr
,
1664 fprintf(add_fd
, "dn: cn=%s,ou=%s,%s\n", aliasname
, group_attr
,
1666 fprintf(add_fd
, "objectClass: posixGroup\n");
1667 fprintf(add_fd
, "objectClass: sambaGroupMapping\n");
1668 fprintf(add_fd
, "cn: %s\n", aliasname
);
1669 fprintf(add_fd
, "gidNumber: %d\n", ldif_gid
);
1670 fprintf(add_fd
, "sambaSID: %s\n", groupmap
->sambaSID
);
1671 fprintf(add_fd
, "sambaGroupType: %d\n", grouptype
);
1672 fprintf(add_fd
, "displayName: %s\n", aliasname
);
1673 fprintf(add_fd
, "description: %s\n", description
);
1674 fprintf(add_fd
, "\n");
1677 SAFE_FREE(group_attr
);
1679 return NT_STATUS_OK
;
1682 static NTSTATUS
fetch_groupmem_info_to_ldif(SAM_DELTA_CTR
*delta
, SAM_DELTA_HDR
*hdr_delta
,
1683 GROUPMAP
*groupmap
, ACCOUNTMAP
*accountmap
,
1684 FILE *mod_fd
, int alloced
)
1687 uint32 group_rid
= 0, rid
= 0;
1690 /* Get the dn for the group */
1691 if (delta
->grp_mem_info
.num_members
> 0) {
1692 group_rid
= hdr_delta
->target_rid
;
1693 for (j
=0; j
<alloced
; j
++) {
1694 if (groupmap
[j
].rid
== group_rid
) break;
1697 DEBUG(1, ("Could not find rid %d in groupmap array\n",
1699 return NT_STATUS_UNSUCCESSFUL
;
1701 pstr_sprintf(group_dn
, "%s", groupmap
[j
].group_dn
);
1702 fprintf(mod_fd
, "dn: %s\n", group_dn
);
1704 /* Get the cn for each member */
1705 for (i
=0; i
<delta
->grp_mem_info
.num_members
; i
++) {
1706 rid
= delta
->grp_mem_info
.rids
[i
];
1707 for (k
=0; k
<alloced
; k
++) {
1708 if (accountmap
[k
].rid
== rid
) break;
1711 DEBUG(1, ("Could not find rid %d in accountmap array\n", rid
));
1712 return NT_STATUS_UNSUCCESSFUL
;
1714 fprintf(mod_fd
, "memberUid: %s\n", accountmap
[k
].cn
);
1716 fprintf(mod_fd
, "\n");
1721 return NT_STATUS_OK
;
1724 static NTSTATUS
fetch_database_to_ldif(struct rpc_pipe_client
*pipe_hnd
,
1727 const char *user_file
)
1730 const char *builtin_sid
= "S-1-5-32";
1731 char *add_name
= NULL
, *mod_name
= NULL
;
1732 const char *add_template
= "/tmp/add.ldif.XXXXXX";
1733 const char *mod_template
= "/tmp/mod.ldif.XXXXXX";
1734 fstring sid
, domainname
;
1735 uint32 sync_context
= 0;
1736 NTSTATUS ret
= NT_STATUS_OK
, result
;
1738 TALLOC_CTX
*mem_ctx
;
1739 SAM_DELTA_HDR
*hdr_deltas
;
1740 SAM_DELTA_CTR
*deltas
;
1742 FILE *add_file
= NULL
, *mod_file
= NULL
, *ldif_file
= NULL
;
1743 int num_alloced
= 0, g_index
= 0, a_index
= 0;
1745 /* Set up array for mapping accounts to groups */
1746 /* Array element is the group rid */
1747 GROUPMAP
*groupmap
= NULL
;
1749 /* Set up array for mapping account rid's to cn's */
1750 /* Array element is the account rid */
1751 ACCOUNTMAP
*accountmap
= NULL
;
1753 if (!(mem_ctx
= talloc_init("fetch_database"))) {
1754 return NT_STATUS_NO_MEMORY
;
1757 /* Ensure we have an output file */
1759 ldif_file
= fopen(user_file
, "a");
1764 fprintf(stderr
, "Could not open %s\n", user_file
);
1765 DEBUG(1, ("Could not open %s\n", user_file
));
1766 ret
= NT_STATUS_UNSUCCESSFUL
;
1770 add_name
= talloc_strdup(mem_ctx
, add_template
);
1771 mod_name
= talloc_strdup(mem_ctx
, mod_template
);
1772 if (!add_name
|| !mod_name
) {
1773 ret
= NT_STATUS_NO_MEMORY
;
1777 /* Open the add and mod ldif files */
1778 if (!(add_file
= fdopen(smb_mkstemp(add_name
),"w"))) {
1779 DEBUG(1, ("Could not open %s\n", add_name
));
1780 ret
= NT_STATUS_UNSUCCESSFUL
;
1783 if (!(mod_file
= fdopen(smb_mkstemp(mod_name
),"w"))) {
1784 DEBUG(1, ("Could not open %s\n", mod_name
));
1785 ret
= NT_STATUS_UNSUCCESSFUL
;
1790 sid_to_string(sid
, &dom_sid
);
1792 /* Get the ldap suffix */
1793 suffix
= lp_ldap_suffix();
1794 if (suffix
== NULL
|| strcmp(suffix
, "") == 0) {
1795 DEBUG(0,("ldap suffix missing from smb.conf--exiting\n"));
1799 /* Get other smb.conf data */
1800 if (!(lp_workgroup()) || !*(lp_workgroup())) {
1801 DEBUG(0,("workgroup missing from smb.conf--exiting\n"));
1805 /* Allocate initial memory for groupmap and accountmap arrays */
1806 if (init_ldap
== 1) {
1807 groupmap
= SMB_MALLOC_ARRAY(GROUPMAP
, 8);
1808 accountmap
= SMB_MALLOC_ARRAY(ACCOUNTMAP
, 8);
1809 if (groupmap
== NULL
|| accountmap
== NULL
) {
1810 DEBUG(1,("GROUPMAP malloc failed\n"));
1811 ret
= NT_STATUS_NO_MEMORY
;
1815 /* Initialize the arrays */
1816 memset(groupmap
, 0, sizeof(GROUPMAP
)*8);
1817 memset(accountmap
, 0, sizeof(ACCOUNTMAP
)*8);
1819 /* Remember how many we malloced */
1822 /* Initial database population */
1823 populate_ldap_for_ldif(sid
, suffix
, builtin_sid
, add_file
);
1824 map_populate_groups(groupmap
, accountmap
, sid
, suffix
,
1827 /* Don't do this again */
1831 /* Announce what we are doing */
1833 case SAM_DATABASE_DOMAIN
:
1834 d_fprintf(stderr
, "Fetching DOMAIN database\n");
1836 case SAM_DATABASE_BUILTIN
:
1837 d_fprintf(stderr
, "Fetching BUILTIN database\n");
1839 case SAM_DATABASE_PRIVS
:
1840 d_fprintf(stderr
, "Fetching PRIVS databases\n");
1844 "Fetching unknown database type %u\n",
1850 result
= rpccli_netlogon_sam_sync(pipe_hnd
, mem_ctx
,
1851 db_type
, sync_context
,
1852 &num_deltas
, &hdr_deltas
,
1854 if (!NT_STATUS_IS_OK(result
) &&
1855 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
1857 goto done
; /* is this correct? jmcd */
1860 /* Re-allocate memory for groupmap and accountmap arrays */
1861 groupmap
= SMB_REALLOC_ARRAY(groupmap
, GROUPMAP
,
1862 num_deltas
+num_alloced
);
1863 accountmap
= SMB_REALLOC_ARRAY(accountmap
, ACCOUNTMAP
,
1864 num_deltas
+num_alloced
);
1865 if (groupmap
== NULL
|| accountmap
== NULL
) {
1866 DEBUG(1,("GROUPMAP malloc failed\n"));
1867 ret
= NT_STATUS_NO_MEMORY
;
1871 /* Initialize the new records */
1872 memset(&groupmap
[num_alloced
], 0,
1873 sizeof(GROUPMAP
)*num_deltas
);
1874 memset(&accountmap
[num_alloced
], 0,
1875 sizeof(ACCOUNTMAP
)*num_deltas
);
1877 /* Remember how many we alloced this time */
1878 num_alloced
+= num_deltas
;
1880 /* Loop through the deltas */
1881 for (k
=0; k
<num_deltas
; k
++) {
1882 switch(hdr_deltas
[k
].type
) {
1883 case SAM_DELTA_DOMAIN_INFO
:
1884 /* Is this case needed? */
1885 unistr2_to_ascii(domainname
,
1886 &deltas
[k
].domain_info
.uni_dom_name
,
1887 sizeof(domainname
)-1);
1890 case SAM_DELTA_GROUP_INFO
:
1891 fetch_group_info_to_ldif(
1892 &deltas
[k
], &groupmap
[g_index
],
1893 add_file
, sid
, suffix
);
1897 case SAM_DELTA_ACCOUNT_INFO
:
1898 fetch_account_info_to_ldif(
1899 &deltas
[k
], groupmap
,
1900 &accountmap
[a_index
], add_file
,
1901 sid
, suffix
, num_alloced
);
1905 case SAM_DELTA_ALIAS_INFO
:
1906 fetch_alias_info_to_ldif(
1907 &deltas
[k
], &groupmap
[g_index
],
1908 add_file
, sid
, suffix
, db_type
);
1912 case SAM_DELTA_GROUP_MEM
:
1913 fetch_groupmem_info_to_ldif(
1914 &deltas
[k
], &hdr_deltas
[k
],
1915 groupmap
, accountmap
,
1916 mod_file
, num_alloced
);
1919 case SAM_DELTA_ALIAS_MEM
:
1921 case SAM_DELTA_POLICY_INFO
:
1923 case SAM_DELTA_PRIVS_INFO
:
1925 case SAM_DELTA_TRUST_DOMS
:
1926 /* Implemented but broken */
1928 case SAM_DELTA_SECRET_INFO
:
1929 /* Implemented but broken */
1931 case SAM_DELTA_RENAME_GROUP
:
1932 /* Not yet implemented */
1934 case SAM_DELTA_RENAME_USER
:
1935 /* Not yet implemented */
1937 case SAM_DELTA_RENAME_ALIAS
:
1938 /* Not yet implemented */
1940 case SAM_DELTA_DELETE_GROUP
:
1941 /* Not yet implemented */
1943 case SAM_DELTA_DELETE_USER
:
1944 /* Not yet implemented */
1946 case SAM_DELTA_MODIFIED_COUNT
:
1950 } /* end of switch */
1951 } /* end of for loop */
1953 /* Increment sync_context */
1956 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1958 /* Write ldif data to the user's file */
1959 if (db_type
== SAM_DATABASE_DOMAIN
) {
1961 "# SAM_DATABASE_DOMAIN: ADD ENTITIES\n");
1963 "# =================================\n\n");
1965 } else if (db_type
== SAM_DATABASE_BUILTIN
) {
1967 "# SAM_DATABASE_BUILTIN: ADD ENTITIES\n");
1969 "# ==================================\n\n");
1972 fseek(add_file
, 0, SEEK_SET
);
1973 transfer_file(fileno(add_file
), fileno(ldif_file
), (size_t) -1);
1975 if (db_type
== SAM_DATABASE_DOMAIN
) {
1977 "# SAM_DATABASE_DOMAIN: MODIFY ENTITIES\n");
1979 "# ====================================\n\n");
1981 } else if (db_type
== SAM_DATABASE_BUILTIN
) {
1983 "# SAM_DATABASE_BUILTIN: MODIFY ENTITIES\n");
1985 "# =====================================\n\n");
1988 fseek(mod_file
, 0, SEEK_SET
);
1989 transfer_file(fileno(mod_file
), fileno(ldif_file
), (size_t) -1);
1993 /* Close and delete the ldif files */
1998 if ((add_name
!= NULL
) && strcmp(add_name
, add_template
) && (unlink(add_name
))) {
1999 DEBUG(1,("unlink(%s) failed, error was (%s)\n",
2000 add_name
, strerror(errno
)));
2007 if ((mod_name
!= NULL
) && strcmp(mod_name
, mod_template
) && (unlink(mod_name
))) {
2008 DEBUG(1,("unlink(%s) failed, error was (%s)\n",
2009 mod_name
, strerror(errno
)));
2012 if (ldif_file
&& (ldif_file
!= stdout
)) {
2016 /* Deallocate memory for the mapping arrays */
2017 SAFE_FREE(groupmap
);
2018 SAFE_FREE(accountmap
);
2021 talloc_destroy(mem_ctx
);
2026 * Basic usage function for 'net rpc vampire'
2027 * @param argc Standard main() style argc
2028 * @param argc Standard main() style argv. Initial components are already
2032 int rpc_vampire_usage(int argc
, const char **argv
)
2034 d_printf("net rpc vampire [ldif [<ldif-filename>] [options]\n"\
2035 "\t to pull accounts from a remote PDC where we are a BDC\n"\
2036 "\t\t no args puts accounts in local passdb from smb.conf\n"\
2037 "\t\t ldif - put accounts in ldif format (file defaults to /tmp/tmp.ldif\n");
2039 net_common_flags_usage(argc
, argv
);
2044 /* dump sam database via samsync rpc calls */
2045 NTSTATUS
rpc_vampire_internals(const DOM_SID
*domain_sid
,
2046 const char *domain_name
,
2047 struct cli_state
*cli
,
2048 struct rpc_pipe_client
*pipe_hnd
,
2049 TALLOC_CTX
*mem_ctx
,
2054 fstring my_dom_sid_str
;
2055 fstring rem_dom_sid_str
;
2057 if (!sid_equal(domain_sid
, get_global_sam_sid())) {
2058 d_printf("Cannot import users from %s at this time, "
2059 "as the current domain:\n\t%s: %s\nconflicts "
2060 "with the remote domain\n\t%s: %s\n"
2061 "Perhaps you need to set: \n\n\tsecurity=user\n\tworkgroup=%s\n\n in your smb.conf?\n",
2063 get_global_sam_name(), sid_to_string(my_dom_sid_str
,
2064 get_global_sam_sid()),
2065 domain_name
, sid_to_string(rem_dom_sid_str
, domain_sid
),
2067 return NT_STATUS_UNSUCCESSFUL
;
2070 if (argc
>= 1 && (strcmp(argv
[0], "ldif") == 0)) {
2071 result
= fetch_database_to_ldif(pipe_hnd
, SAM_DATABASE_DOMAIN
,
2072 *domain_sid
, argv
[1]);
2074 result
= fetch_database(pipe_hnd
, SAM_DATABASE_DOMAIN
, *domain_sid
);
2077 if (!NT_STATUS_IS_OK(result
)) {
2078 d_fprintf(stderr
, "Failed to fetch domain database: %s\n",
2080 if (NT_STATUS_EQUAL(result
, NT_STATUS_NOT_SUPPORTED
))
2081 d_fprintf(stderr
, "Perhaps %s is a Windows 2000 native "
2082 "mode domain?\n", domain_name
);
2086 if (argc
>= 1 && (strcmp(argv
[0], "ldif") == 0)) {
2087 result
= fetch_database_to_ldif(pipe_hnd
, SAM_DATABASE_BUILTIN
,
2088 global_sid_Builtin
, argv
[1]);
2090 result
= fetch_database(pipe_hnd
, SAM_DATABASE_BUILTIN
, global_sid_Builtin
);
2093 if (!NT_STATUS_IS_OK(result
)) {
2094 d_fprintf(stderr
, "Failed to fetch builtin database: %s\n",
2099 /* Currently we crash on PRIVS somewhere in unmarshalling */
2100 /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */