X-Git-Url: http://git.samba.org/?p=samba.git;a=blobdiff_plain;f=source3%2Futils%2Fnet_rpc_samsync.c;h=d0fcfe3aeb3c6e3e517eb03d3c2268f70df1ec21;hp=ed69f8a326b15850fdc67df1f1f18d707cabbde9;hb=805caafd44cbc5fff49711b1a15fb64cc99f3ad3;hpb=4093bf7ff8c8861cf7b941945ede53a8ec5bb6c8 diff --git a/source3/utils/net_rpc_samsync.c b/source3/utils/net_rpc_samsync.c index ed69f8a326b..d0fcfe3aeb3 100644 --- a/source3/utils/net_rpc_samsync.c +++ b/source3/utils/net_rpc_samsync.c @@ -4,11 +4,13 @@ Copyright (C) Andrew Tridgell 2002 Copyright (C) Tim Potter 2001,2002 + Copyright (C) Jim McDonough 2005 Modified by Volker Lendecke 2002 + Copyright (C) Jeremy Allison 2005. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -17,14 +19,17 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -#include "../utils/net.h" +#include "utils/net.h" -extern DOM_SID global_sid_Builtin; +/* uid's and gid's for writing deltas to ldif */ +static uint32 ldif_gid = 999; +static uint32 ldif_uid = 999; +/* Keep track of ldap initialization */ +static int init_ldap = 1; static void display_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *g) { @@ -47,7 +52,7 @@ static void display_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *a) int i; d_printf("Alias rid %u: ", rid); for (i=0;inum_members;i++) { - d_printf("%s ", sid_string_static(&a->sids[i].sid)); + d_printf("%s ", sid_string_tos(&a->sids[i].sid)); } d_printf("\n"); } @@ -62,26 +67,51 @@ static void display_account_info(uint32 rid, SAM_ACCOUNT_INFO *a) if (memcmp(a->pass.buf_lm_pwd, zero_buf, 16) != 0) { sam_pwd_hash(a->user_rid, a->pass.buf_lm_pwd, lm_passwd, 0); - smbpasswd_sethexpwd(hex_lm_passwd, lm_passwd, a->acb_info); + pdb_sethexpwd(hex_lm_passwd, lm_passwd, a->acb_info); } else { - smbpasswd_sethexpwd(hex_lm_passwd, NULL, 0); + pdb_sethexpwd(hex_lm_passwd, NULL, 0); } if (memcmp(a->pass.buf_nt_pwd, zero_buf, 16) != 0) { sam_pwd_hash(a->user_rid, a->pass.buf_nt_pwd, nt_passwd, 0); - smbpasswd_sethexpwd(hex_nt_passwd, nt_passwd, a->acb_info); + pdb_sethexpwd(hex_nt_passwd, nt_passwd, a->acb_info); } else { - smbpasswd_sethexpwd(hex_nt_passwd, NULL, 0); + pdb_sethexpwd(hex_nt_passwd, NULL, 0); } printf("%s:%d:%s:%s:%s:LCT-0\n", unistr2_static(&a->uni_acct_name), a->user_rid, hex_lm_passwd, hex_nt_passwd, - smbpasswd_encode_acb_info(a->acb_info)); + pdb_encode_acct_ctrl(a->acb_info, NEW_PW_FORMAT_SPACE_PADDED_LEN)); +} + +static time_t uint64s_nt_time_to_unix_abs(const uint64 *src) +{ + NTTIME nttime; + nttime = *src; + return nt_time_to_unix_abs(&nttime); } static void display_domain_info(SAM_DOMAIN_INFO *a) { + time_t u_logout; + + u_logout = uint64s_nt_time_to_unix_abs(&a->force_logoff); + d_printf("Domain name: %s\n", unistr2_static(&a->uni_dom_name)); + + d_printf("Minimal Password Length: %d\n", a->min_pwd_len); + d_printf("Password History Length: %d\n", a->pwd_history_len); + + d_printf("Force Logoff: %d\n", (int)u_logout); + + d_printf("Max Password Age: %s\n", display_time(a->max_pwd_age)); + d_printf("Min Password Age: %s\n", display_time(a->min_pwd_age)); + + d_printf("Lockout Time: %s\n", display_time(a->account_lockout.lockout_duration)); + d_printf("Lockout Reset Time: %s\n", display_time(a->account_lockout.reset_count)); + + d_printf("Bad Attempt Lockout: %d\n", a->account_lockout.bad_attempt_lockout); + d_printf("User must logon to change password: %d\n", a->logon_chgpass); } static void display_group_info(uint32 rid, SAM_GROUP_INFO *a) @@ -148,10 +178,9 @@ static void display_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta) } } - -static void dump_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds) +static void dump_database(struct rpc_pipe_client *pipe_hnd, uint32 db_type) { - unsigned sync_context = 0; + uint32 sync_context = 0; NTSTATUS result; int i; TALLOC_CTX *mem_ctx; @@ -179,10 +208,12 @@ static void dump_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret } do { - result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds, db_type, + result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx, db_type, sync_context, &num_deltas, &hdr_deltas, &deltas); - clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), ret_creds); + if (!NT_STATUS_IS_OK(result)) + break; + for (i = 0; i < num_deltas; i++) { display_sam_entry(&hdr_deltas[i], &deltas[i]); } @@ -193,62 +224,60 @@ static void dump_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret } /* dump sam database via samsync rpc calls */ -int rpc_samdump(int argc, const char **argv) +NTSTATUS rpc_samdump_internals(const DOM_SID *domain_sid, + const char *domain_name, + struct cli_state *cli, + struct rpc_pipe_client *pipe_hnd, + TALLOC_CTX *mem_ctx, + int argc, + const char **argv) { - struct cli_state *cli = NULL; - uchar trust_password[16]; - DOM_CRED ret_creds; - uint32 sec_channel; - - ZERO_STRUCT(ret_creds); - - /* Connect to remote machine */ - if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) { - return 1; - } - - fstrcpy(cli->domain, lp_workgroup()); +#if 0 + /* net_rpc.c now always tries to create an schannel pipe.. */ - if (!cli_nt_session_open(cli, PI_NETLOGON)) { - DEBUG(0,("Could not open connection to NETLOGON pipe\n")); - goto fail; - } + NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL; + uchar trust_password[16]; + uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS; + uint32 sec_channel_type = 0; - if (!secrets_fetch_trust_account_password(lp_workgroup(), + if (!secrets_fetch_trust_account_password(domain_name, trust_password, - NULL, &sec_channel)) { + NULL, &sec_channel_type)) { DEBUG(0,("Could not fetch trust account password\n")); goto fail; } - if (!NT_STATUS_IS_OK(cli_nt_establish_netlogon(cli, sec_channel, - trust_password))) { + nt_status = rpccli_netlogon_setup_creds(pipe_hnd, + cli->desthost, + domain_name, + global_myname(), + trust_password, + sec_channel_type, + &neg_flags); + + if (!NT_STATUS_IS_OK(nt_status)) { DEBUG(0,("Error connecting to NETLOGON pipe\n")); goto fail; } +#endif - dump_database(cli, SAM_DATABASE_DOMAIN, &ret_creds); - dump_database(cli, SAM_DATABASE_BUILTIN, &ret_creds); - dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); - - cli_nt_session_close(cli); - - return 0; + dump_database(pipe_hnd, SAM_DATABASE_DOMAIN); + dump_database(pipe_hnd, SAM_DATABASE_BUILTIN); + dump_database(pipe_hnd, SAM_DATABASE_PRIVS); -fail: - if (cli) { - cli_nt_session_close(cli); - } - return -1; + return NT_STATUS_OK; } -/* Convert a SAM_ACCOUNT_DELTA to a SAM_ACCOUNT. */ +/* Convert a struct samu_DELTA to a struct samu. */ #define STRING_CHANGED (old_string && !new_string) ||\ (!old_string && new_string) ||\ (old_string && new_string && (strcmp(old_string, new_string) != 0)) -static NTSTATUS -sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta) +#define STRING_CHANGED_NC(s1,s2) ((s1) && !(s2)) ||\ + (!(s1) && (s2)) ||\ + ((s1) && (s2) && (strcmp((s1), (s2)) != 0)) + +static NTSTATUS sam_account_from_delta(struct samu *account, SAM_ACCOUNT_INFO *delta) { const char *old_string, *new_string; time_t unix_time, stored_time; @@ -330,6 +359,20 @@ sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta) pdb_set_profile_path(account, new_string, PDB_CHANGED); } + if (delta->hdr_parameters.buffer) { + DATA_BLOB mung; + char *newstr; + old_string = pdb_get_munged_dial(account); + mung.length = delta->hdr_parameters.uni_str_len; + mung.data = (uint8 *) delta->uni_parameters.buffer; + newstr = (mung.length == 0) ? NULL : + base64_encode_data_blob(talloc_tos(), mung); + + if (STRING_CHANGED_NC(old_string, newstr)) + pdb_set_munged_dial(account, newstr, PDB_CHANGED); + TALLOC_FREE(newstr); + } + /* User and group sid */ if (pdb_get_user_rid(account) != delta->user_rid) pdb_set_user_sid_from_rid(account, delta->user_rid, PDB_CHANGED); @@ -338,31 +381,56 @@ sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta) /* Logon and password information */ if (!nt_time_is_zero(&delta->logon_time)) { - unix_time = nt_time_to_unix(&delta->logon_time); + unix_time = nt_time_to_unix(delta->logon_time); stored_time = pdb_get_logon_time(account); if (stored_time != unix_time) pdb_set_logon_time(account, unix_time, PDB_CHANGED); } if (!nt_time_is_zero(&delta->logoff_time)) { - unix_time = nt_time_to_unix(&delta->logoff_time); + unix_time = nt_time_to_unix(delta->logoff_time); stored_time = pdb_get_logoff_time(account); if (stored_time != unix_time) pdb_set_logoff_time(account, unix_time,PDB_CHANGED); } + /* Logon Divs */ if (pdb_get_logon_divs(account) != delta->logon_divs) pdb_set_logon_divs(account, delta->logon_divs, PDB_CHANGED); - /* TODO: logon hours */ - /* TODO: bad password count */ - /* TODO: logon count */ + /* Max Logon Hours */ + if (delta->unknown1 != pdb_get_unknown_6(account)) { + pdb_set_unknown_6(account, delta->unknown1, PDB_CHANGED); + } + + /* Logon Hours Len */ + if (delta->buf_logon_hrs.buf_len != pdb_get_hours_len(account)) { + pdb_set_hours_len(account, delta->buf_logon_hrs.buf_len, PDB_CHANGED); + } + + /* Logon Hours */ + if (delta->buf_logon_hrs.buffer) { + char oldstr[44], newstr[44]; + pdb_sethexhours(oldstr, pdb_get_hours(account)); + pdb_sethexhours(newstr, delta->buf_logon_hrs.buffer); + if (!strequal(oldstr, newstr)) + pdb_set_hours(account, (const uint8 *)delta->buf_logon_hrs.buffer, PDB_CHANGED); + } + + if (pdb_get_bad_password_count(account) != delta->bad_pwd_count) + pdb_set_bad_password_count(account, delta->bad_pwd_count, PDB_CHANGED); + + if (pdb_get_logon_count(account) != delta->logon_count) + pdb_set_logon_count(account, delta->logon_count, PDB_CHANGED); if (!nt_time_is_zero(&delta->pwd_last_set_time)) { - unix_time = nt_time_to_unix(&delta->pwd_last_set_time); + unix_time = nt_time_to_unix(delta->pwd_last_set_time); stored_time = pdb_get_pass_last_set_time(account); if (stored_time != unix_time) pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED); + } else { + /* no last set time, make it now */ + pdb_set_pass_last_set_time(account, time(NULL), PDB_CHANGED); } #if 0 @@ -392,8 +460,7 @@ sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta) /* TODO: account expiry time */ - if (pdb_get_acct_ctrl(account) != delta->acb_info) - pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED); + pdb_set_acct_ctrl(account, delta->acb_info, PDB_CHANGED); pdb_set_domain(account, lp_workgroup(), PDB_CHANGED); @@ -402,10 +469,10 @@ sam_account_from_delta(SAM_ACCOUNT *account, SAM_ACCOUNT_INFO *delta) static NTSTATUS fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta) { - NTSTATUS nt_ret; + NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL; fstring account; - pstring add_script; - SAM_ACCOUNT *sam_account=NULL; + char *add_script = NULL; + struct samu *sam_account=NULL; GROUP_MAP map; struct group *grp; DOM_SID user_sid; @@ -416,52 +483,67 @@ static NTSTATUS fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta) fstrcpy(account, unistr2_static(&delta->uni_acct_name)); d_printf("Creating account: %s\n", account); - if (!NT_STATUS_IS_OK(nt_ret = pdb_init_sam(&sam_account))) - return nt_ret; + if ( !(sam_account = samu_new( NULL )) ) { + return NT_STATUS_NO_MEMORY; + } - if (!(passwd = Get_Pwnam(account))) { + if (!(passwd = Get_Pwnam_alloc(sam_account, account))) { /* Create appropriate user */ if (delta->acb_info & ACB_NORMAL) { - pstrcpy(add_script, lp_adduser_script()); + add_script = talloc_strdup(sam_account, + lp_adduser_script()); } else if ( (delta->acb_info & ACB_WSTRUST) || (delta->acb_info & ACB_SVRTRUST) || (delta->acb_info & ACB_DOMTRUST) ) { - pstrcpy(add_script, lp_addmachine_script()); + add_script = talloc_strdup(sam_account, + lp_addmachine_script()); } else { - *add_script = '\0'; DEBUG(1, ("Unknown user type: %s\n", - smbpasswd_encode_acb_info(delta->acb_info))); + pdb_encode_acct_ctrl(delta->acb_info, NEW_PW_FORMAT_SPACE_PADDED_LEN))); + nt_ret = NT_STATUS_UNSUCCESSFUL; + goto done; + } + if (!add_script) { + nt_ret = NT_STATUS_NO_MEMORY; + goto done; } if (*add_script) { int add_ret; - all_string_sub(add_script, "%u", account, - sizeof(account)); + add_script = talloc_all_string_sub(sam_account, + add_script, + "%u", + account); + if (!add_script) { + nt_ret = NT_STATUS_NO_MEMORY; + goto done; + } add_ret = smbrun(add_script,NULL); - DEBUG(1,("fetch_account: Running the command `%s' " + DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' " "gave %d\n", add_script, add_ret)); - } else { - DEBUG(8,("fetch_account_info: no add user/machine script. Asking winbindd\n")); - - /* don't need a RID allocated since the user already has a SID */ - if ( !winbind_create_user( account, NULL ) ) - DEBUG(4,("fetch_account_info: winbind_create_user() failed\n")); + if (add_ret == 0) { + smb_nscd_flush_user_cache(); + } } - + /* try and find the possible unix account again */ - if ( !(passwd = Get_Pwnam(account)) ) - return NT_STATUS_NO_SUCH_USER; - + if ( !(passwd = Get_Pwnam_alloc(sam_account, account)) ) { + d_fprintf(stderr, "Could not create posix account info for '%s'\n", account); + nt_ret = NT_STATUS_NO_SUCH_USER; + goto done; + } } - + sid_copy(&user_sid, get_global_sam_sid()); sid_append_rid(&user_sid, delta->user_rid); - DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n", sid_to_string(sid_string, &user_sid), account)); + DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n", + sid_to_fstring(sid_string, &user_sid), account)); if (!pdb_getsampwsid(sam_account, &user_sid)) { sam_account_from_delta(sam_account, delta); DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n", - sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account))); - if (!pdb_add_sam_account(sam_account)) { + sid_to_fstring(sid_string, &user_sid), + pdb_get_username(sam_account))); + if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) { DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n", account)); return NT_STATUS_ACCESS_DENIED; @@ -469,15 +551,20 @@ static NTSTATUS fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta) } else { sam_account_from_delta(sam_account, delta); DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n", - sid_to_string(sid_string, &user_sid), pdb_get_username(sam_account))); - if (!pdb_update_sam_account(sam_account)) { + sid_to_fstring(sid_string, &user_sid), + pdb_get_username(sam_account))); + if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) { DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n", account)); - pdb_free_sam(&sam_account); + TALLOC_FREE(sam_account); return NT_STATUS_ACCESS_DENIED; } } + if (pdb_get_group_sid(sam_account) == NULL) { + return NT_STATUS_UNSUCCESSFUL; + } + group_sid = *pdb_get_group_sid(sam_account); if (!pdb_getgrsid(&map, group_sid)) { @@ -487,24 +574,24 @@ static NTSTATUS fetch_account_info(uint32 rid, SAM_ACCOUNT_INFO *delta) if (map.gid != passwd->pw_gid) { if (!(grp = getgrgid(map.gid))) { DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n", - (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_static(&group_sid))); + (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_tos(&group_sid))); } else { smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account)); } } - } + } if ( !passwd ) { DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n", pdb_get_username(sam_account))); } - pdb_free_sam(&sam_account); + done: + TALLOC_FREE(sam_account); return nt_ret; } -static NTSTATUS -fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta) +static NTSTATUS fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta) { fstring name; fstring comment; @@ -512,15 +599,15 @@ fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta) DOM_SID group_sid; fstring sid_string; GROUP_MAP map; - BOOL insert = True; + bool insert = True; - unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)-1); - unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)-1); + unistr2_to_ascii(name, &delta->uni_grp_name, sizeof(name)); + unistr2_to_ascii(comment, &delta->uni_grp_desc, sizeof(comment)); /* add the group to the mapping table */ sid_copy(&group_sid, get_global_sam_sid()); sid_append_rid(&group_sid, rid); - sid_to_string(sid_string, &group_sid); + sid_to_fstring(sid_string, &group_sid); if (pdb_getgrsid(&map, group_sid)) { if ( map.gid != -1 ) @@ -564,8 +651,7 @@ fetch_group_info(uint32 rid, SAM_GROUP_INFO *delta) return NT_STATUS_OK; } -static NTSTATUS -fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta) +static NTSTATUS fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta) { int i; TALLOC_CTX *t = NULL; @@ -599,16 +685,23 @@ fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta) return NT_STATUS_NO_MEMORY; } - nt_members = talloc_zero(t, sizeof(char *) * delta->num_members); + if (delta->num_members) { + if ((nt_members = TALLOC_ZERO_ARRAY(t, char *, delta->num_members)) == NULL) { + DEBUG(0, ("talloc failed\n")); + talloc_free(t); + return NT_STATUS_NO_MEMORY; + } + } else { + nt_members = NULL; + } for (i=0; inum_members; i++) { - NTSTATUS nt_status; - SAM_ACCOUNT *member = NULL; + struct samu *member = NULL; DOM_SID member_sid; - if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) { + if ( !(member = samu_new(t)) ) { talloc_destroy(t); - return nt_status; + return NT_STATUS_NO_MEMORY; } sid_copy(&member_sid, get_global_sam_sid()); @@ -616,20 +709,20 @@ fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta) if (!pdb_getsampwsid(member, &member_sid)) { DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n", - delta->rids[i], sid_string_static(&member_sid), grp->gr_name)); - pdb_free_sam(&member); + delta->rids[i], sid_string_tos(&member_sid), grp->gr_name)); + TALLOC_FREE(member); continue; } if (pdb_get_group_rid(member) == rid) { d_printf("%s(primary),", pdb_get_username(member)); - pdb_free_sam(&member); + TALLOC_FREE(member); continue; } d_printf("%s,", pdb_get_username(member)); nt_members[i] = talloc_strdup(t, pdb_get_username(member)); - pdb_free_sam(&member); + TALLOC_FREE(member); } d_printf("\n"); @@ -637,7 +730,7 @@ fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta) unix_members = grp->gr_mem; while (*unix_members) { - BOOL is_nt_member = False; + bool is_nt_member = False; for (i=0; inum_members; i++) { if (nt_members[i] == NULL) { /* This was a primary group */ @@ -659,7 +752,7 @@ fetch_group_mem_info(uint32 rid, SAM_GROUP_MEM_INFO *delta) } for (i=0; inum_members; i++) { - BOOL is_unix_member = False; + bool is_unix_member = False; if (nt_members[i] == NULL) { /* This was the primary group */ @@ -697,15 +790,15 @@ static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta, DOM_SID alias_sid; fstring sid_string; GROUP_MAP map; - BOOL insert = True; + bool insert = True; - unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)-1); - unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)-1); + unistr2_to_ascii(name, &delta->uni_als_name, sizeof(name)); + unistr2_to_ascii(comment, &delta->uni_als_desc, sizeof(comment)); /* Find out whether the group is already mapped */ sid_copy(&alias_sid, &dom_sid); sid_append_rid(&alias_sid, rid); - sid_to_string(sid_string, &alias_sid); + sid_to_fstring(sid_string, &alias_sid); if (pdb_getgrsid(&map, alias_sid)) { grp = getgrgid(map.gid); @@ -745,153 +838,70 @@ static NTSTATUS fetch_alias_info(uint32 rid, SAM_ALIAS_INFO *delta, return NT_STATUS_OK; } -static NTSTATUS -fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid) +static NTSTATUS fetch_alias_mem(uint32 rid, SAM_ALIAS_MEM_INFO *delta, DOM_SID dom_sid) { -#if 0 /* - * commented out right now after talking to Volker. Can't - * do much with the membership but seemed a shame to waste - * somewhat working code. Needs testing because the membership - * that shows up surprises me. Also can't do much with groups - * in groups (e.g. Domain Admins being a member of Adminsitrators). - * --jerry - */ - - int i; - TALLOC_CTX *t = NULL; - char **nt_members = NULL; - char **unix_members; - DOM_SID group_sid; - GROUP_MAP map; - struct group *grp; - enum SID_NAME_USE sid_type; - - if (delta->num_members == 0) { - return NT_STATUS_OK; - } - - sid_copy(&group_sid, &dom_sid); - sid_append_rid(&group_sid, rid); - - if (sid_equal(&dom_sid, &global_sid_Builtin)) { - sid_type = SID_NAME_WKN_GRP; - if (!get_builtin_group_from_sid(group_sid, &map, False)) { - DEBUG(0, ("Could not find builtin group %s\n", sid_string_static(&group_sid))); - return NT_STATUS_NO_SUCH_GROUP; - } - } else { - sid_type = SID_NAME_ALIAS; - if (!get_local_group_from_sid(group_sid, &map, False)) { - DEBUG(0, ("Could not find local group %s\n", sid_string_static(&group_sid))); - return NT_STATUS_NO_SUCH_GROUP; - } - } - - if (!(grp = getgrgid(map.gid))) { - DEBUG(0, ("Could not find unix group %d\n", map.gid)); - return NT_STATUS_NO_SUCH_GROUP; - } - - d_printf("Group members of %s: ", grp->gr_name); + return NT_STATUS_OK; +} - if (!(t = talloc_init("fetch_group_mem_info"))) { - DEBUG(0, ("could not talloc_init\n")); +static NTSTATUS fetch_domain_info(uint32 rid, SAM_DOMAIN_INFO *delta) +{ + time_t u_max_age, u_min_age, u_logout, u_lockoutreset, u_lockouttime; + NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL; + char *domname; + + u_max_age = uint64s_nt_time_to_unix_abs(&delta->max_pwd_age); + u_min_age = uint64s_nt_time_to_unix_abs(&delta->min_pwd_age); + u_logout = uint64s_nt_time_to_unix_abs(&delta->force_logoff); + u_lockoutreset = uint64s_nt_time_to_unix_abs(&delta->account_lockout.reset_count); + u_lockouttime = uint64s_nt_time_to_unix_abs(&delta->account_lockout.lockout_duration); + + domname = unistr2_to_ascii_talloc(talloc_tos(), &delta->uni_dom_name); + if (!domname) { return NT_STATUS_NO_MEMORY; } - nt_members = talloc_zero(t, sizeof(char *) * delta->num_members); - - for (i=0; inum_members; i++) { - NTSTATUS nt_status; - SAM_ACCOUNT *member = NULL; - DOM_SID member_sid; - - if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(t, &member))) { - talloc_destroy(t); - return nt_status; - } - - sid_copy(&member_sid, &delta->sids[i].sid); - - if (!pdb_getsampwsid(member, &member_sid)) { - DEBUG(1, ("Found bogus group member: (member_sid=%s group=%s)\n", - sid_string_static(&member_sid), grp->gr_name)); - pdb_free_sam(&member); - continue; - } - - if (pdb_get_group_rid(member) == rid) { - d_printf("%s(primary),", pdb_get_username(member)); - pdb_free_sam(&member); - continue; - } - - d_printf("%s,", pdb_get_username(member)); - nt_members[i] = talloc_strdup(t, pdb_get_username(member)); - pdb_free_sam(&member); + /* we don't handle BUILTIN account policies */ + if (!strequal(domname, get_global_sam_name())) { + printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname); + return NT_STATUS_OK; } - d_printf("\n"); - unix_members = grp->gr_mem; + if (!pdb_set_account_policy(AP_PASSWORD_HISTORY, delta->pwd_history_len)) + return nt_status; - while (*unix_members) { - BOOL is_nt_member = False; - for (i=0; inum_members; i++) { - if (nt_members[i] == NULL) { - /* This was a primary group */ - continue; - } + if (!pdb_set_account_policy(AP_MIN_PASSWORD_LEN, delta->min_pwd_len)) + return nt_status; - if (strcmp(*unix_members, nt_members[i]) == 0) { - is_nt_member = True; - break; - } - } - if (!is_nt_member) { - /* We look at a unix group member that is not - an nt group member. So, remove it. NT is - boss here. */ - smb_delete_user_group(grp->gr_name, *unix_members); - } - unix_members += 1; - } + if (!pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (uint32)u_max_age)) + return nt_status; - for (i=0; inum_members; i++) { - BOOL is_unix_member = False; + if (!pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (uint32)u_min_age)) + return nt_status; - if (nt_members[i] == NULL) { - /* This was the primary group */ - continue; - } + if (!pdb_set_account_policy(AP_TIME_TO_LOGOUT, (uint32)u_logout)) + return nt_status; - unix_members = grp->gr_mem; + if (!pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, delta->account_lockout.bad_attempt_lockout)) + return nt_status; - while (*unix_members) { - if (strcmp(*unix_members, nt_members[i]) == 0) { - is_unix_member = True; - break; - } - unix_members += 1; - } + if (!pdb_set_account_policy(AP_RESET_COUNT_TIME, (uint32)u_lockoutreset/60)) + return nt_status; - if (!is_unix_member) { - /* We look at a nt group member that is not a - unix group member currently. So, add the nt - group member. */ - smb_add_user_group(grp->gr_name, nt_members[i]); - } - } - - talloc_destroy(t); + if (u_lockouttime != -1) + u_lockouttime /= 60; + + if (!pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (uint32)u_lockouttime)) + return nt_status; -#endif /* end of fetch_alias_mem() */ + if (!pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, delta->logon_chgpass)) + return nt_status; return NT_STATUS_OK; } -static void -fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta, + +static void fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta, DOM_SID dom_sid) { switch(hdr_delta->type) { @@ -915,10 +925,11 @@ fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta, fetch_alias_mem(hdr_delta->target_rid, &delta->als_mem_info, dom_sid); break; - /* The following types are recognised but not handled */ case SAM_DELTA_DOMAIN_INFO: - d_printf("SAM_DELTA_DOMAIN_INFO not handled\n"); + fetch_domain_info(hdr_delta->target_rid, + &delta->domain_info); break; + /* The following types are recognised but not handled */ case SAM_DELTA_RENAME_GROUP: d_printf("SAM_DELTA_RENAME_GROUP not handled\n"); break; @@ -955,11 +966,9 @@ fetch_sam_entry(SAM_DELTA_HDR *hdr_delta, SAM_DELTA_CTR *delta, } } -static NTSTATUS -fetch_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds, - DOM_SID dom_sid) +static NTSTATUS fetch_database(struct rpc_pipe_client *pipe_hnd, uint32 db_type, DOM_SID dom_sid) { - unsigned sync_context = 0; + uint32 sync_context = 0; NTSTATUS result; int i; TALLOC_CTX *mem_ctx; @@ -986,17 +995,13 @@ fetch_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds, } do { - result = cli_netlogon_sam_sync(cli, mem_ctx, ret_creds, + result = rpccli_netlogon_sam_sync(pipe_hnd, mem_ctx, db_type, sync_context, &num_deltas, &hdr_deltas, &deltas); if (NT_STATUS_IS_OK(result) || NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) { - - clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), - ret_creds); - for (i = 0; i < num_deltas; i++) { fetch_sam_entry(&hdr_deltas[i], &deltas[i], dom_sid); } @@ -1011,76 +1016,1152 @@ fetch_database(struct cli_state *cli, unsigned db_type, DOM_CRED *ret_creds, return result; } -/* dump sam database via samsync rpc calls */ -int rpc_vampire(int argc, const char **argv) +static NTSTATUS populate_ldap_for_ldif(fstring sid, const char *suffix, const char + *builtin_sid, FILE *add_fd) { - NTSTATUS result; - struct cli_state *cli = NULL; - uchar trust_password[16]; - DOM_CRED ret_creds; - DOM_SID dom_sid; - uint32 sec_channel; + const char *user_suffix, *group_suffix, *machine_suffix, *idmap_suffix; + char *user_attr=NULL, *group_attr=NULL; + char *suffix_attr; + int len; + + /* Get the suffix attribute */ + suffix_attr = sstring_sub(suffix, '=', ','); + if (suffix_attr == NULL) { + len = strlen(suffix); + suffix_attr = (char*)SMB_MALLOC(len+1); + memcpy(suffix_attr, suffix, len); + suffix_attr[len] = '\0'; + } - ZERO_STRUCT(ret_creds); + /* Write the base */ + fprintf(add_fd, "# %s\n", suffix); + fprintf(add_fd, "dn: %s\n", suffix); + fprintf(add_fd, "objectClass: dcObject\n"); + fprintf(add_fd, "objectClass: organization\n"); + fprintf(add_fd, "o: %s\n", suffix_attr); + fprintf(add_fd, "dc: %s\n", suffix_attr); + fprintf(add_fd, "\n"); + fflush(add_fd); + + user_suffix = lp_ldap_user_suffix(); + if (user_suffix == NULL) { + SAFE_FREE(suffix_attr); + return NT_STATUS_NO_MEMORY; + } + /* If it exists and is distinct from other containers, + Write the Users entity */ + if (*user_suffix && strcmp(user_suffix, suffix)) { + user_attr = sstring_sub(lp_ldap_user_suffix(), '=', ','); + fprintf(add_fd, "# %s\n", user_suffix); + fprintf(add_fd, "dn: %s\n", user_suffix); + fprintf(add_fd, "objectClass: organizationalUnit\n"); + fprintf(add_fd, "ou: %s\n", user_attr); + fprintf(add_fd, "\n"); + fflush(add_fd); + } - /* Connect to remote machine */ - if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | - NET_FLAGS_PDC))) { - return 1; + + group_suffix = lp_ldap_group_suffix(); + if (group_suffix == NULL) { + SAFE_FREE(suffix_attr); + SAFE_FREE(user_attr); + return NT_STATUS_NO_MEMORY; + } + /* If it exists and is distinct from other containers, + Write the Groups entity */ + if (*group_suffix && strcmp(group_suffix, suffix)) { + group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ','); + fprintf(add_fd, "# %s\n", group_suffix); + fprintf(add_fd, "dn: %s\n", group_suffix); + fprintf(add_fd, "objectClass: organizationalUnit\n"); + fprintf(add_fd, "ou: %s\n", group_attr); + fprintf(add_fd, "\n"); + fflush(add_fd); } - if (!cli_nt_session_open(cli, PI_NETLOGON)) { - DEBUG(0,("Error connecting to NETLOGON pipe\n")); - goto fail; + /* If it exists and is distinct from other containers, + Write the Computers entity */ + machine_suffix = lp_ldap_machine_suffix(); + if (machine_suffix == NULL) { + SAFE_FREE(suffix_attr); + SAFE_FREE(user_attr); + SAFE_FREE(group_attr); + return NT_STATUS_NO_MEMORY; + } + if (*machine_suffix && strcmp(machine_suffix, user_suffix) && + strcmp(machine_suffix, suffix)) { + char *machine_ou = NULL; + fprintf(add_fd, "# %s\n", machine_suffix); + fprintf(add_fd, "dn: %s\n", machine_suffix); + fprintf(add_fd, "objectClass: organizationalUnit\n"); + /* this isn't totally correct as it assumes that + there _must_ be an ou. just fixing memleak now. jmcd */ + machine_ou = sstring_sub(lp_ldap_machine_suffix(), '=', ','); + fprintf(add_fd, "ou: %s\n", machine_ou); + SAFE_FREE(machine_ou); + fprintf(add_fd, "\n"); + fflush(add_fd); } - if (!secrets_fetch_trust_account_password(lp_workgroup(), - trust_password, NULL, - &sec_channel)) { - d_printf("Could not retrieve domain trust secret\n"); - goto fail; + /* If it exists and is distinct from other containers, + Write the IdMap entity */ + idmap_suffix = lp_ldap_idmap_suffix(); + if (idmap_suffix == NULL) { + SAFE_FREE(suffix_attr); + SAFE_FREE(user_attr); + SAFE_FREE(group_attr); + return NT_STATUS_NO_MEMORY; + } + if (*idmap_suffix && + strcmp(idmap_suffix, user_suffix) && + strcmp(idmap_suffix, suffix)) { + char *s; + fprintf(add_fd, "# %s\n", idmap_suffix); + fprintf(add_fd, "dn: %s\n", idmap_suffix); + fprintf(add_fd, "ObjectClass: organizationalUnit\n"); + s = sstring_sub(lp_ldap_idmap_suffix(), '=', ','); + fprintf(add_fd, "ou: %s\n", s); + SAFE_FREE(s); + fprintf(add_fd, "\n"); + fflush(add_fd); + } + + /* Write the domain entity */ + fprintf(add_fd, "# %s, %s\n", lp_workgroup(), suffix); + fprintf(add_fd, "dn: sambaDomainName=%s,%s\n", lp_workgroup(), + suffix); + fprintf(add_fd, "objectClass: sambaDomain\n"); + fprintf(add_fd, "objectClass: sambaUnixIdPool\n"); + fprintf(add_fd, "sambaDomainName: %s\n", lp_workgroup()); + fprintf(add_fd, "sambaSID: %s\n", sid); + fprintf(add_fd, "uidNumber: %d\n", ++ldif_uid); + fprintf(add_fd, "gidNumber: %d\n", ++ldif_gid); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Write the Domain Admins entity */ + fprintf(add_fd, "# Domain Admins, %s, %s\n", group_attr, + suffix); + fprintf(add_fd, "dn: cn=Domain Admins,ou=%s,%s\n", group_attr, + suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "cn: Domain Admins\n"); + fprintf(add_fd, "memberUid: Administrator\n"); + fprintf(add_fd, "description: Netbios Domain Administrators\n"); + fprintf(add_fd, "gidNumber: 512\n"); + fprintf(add_fd, "sambaSID: %s-512\n", sid); + fprintf(add_fd, "sambaGroupType: 2\n"); + fprintf(add_fd, "displayName: Domain Admins\n"); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Write the Domain Users entity */ + fprintf(add_fd, "# Domain Users, %s, %s\n", group_attr, + suffix); + fprintf(add_fd, "dn: cn=Domain Users,ou=%s,%s\n", group_attr, + suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "cn: Domain Users\n"); + fprintf(add_fd, "description: Netbios Domain Users\n"); + fprintf(add_fd, "gidNumber: 513\n"); + fprintf(add_fd, "sambaSID: %s-513\n", sid); + fprintf(add_fd, "sambaGroupType: 2\n"); + fprintf(add_fd, "displayName: Domain Users\n"); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Write the Domain Guests entity */ + fprintf(add_fd, "# Domain Guests, %s, %s\n", group_attr, + suffix); + fprintf(add_fd, "dn: cn=Domain Guests,ou=%s,%s\n", group_attr, + suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "cn: Domain Guests\n"); + fprintf(add_fd, "description: Netbios Domain Guests\n"); + fprintf(add_fd, "gidNumber: 514\n"); + fprintf(add_fd, "sambaSID: %s-514\n", sid); + fprintf(add_fd, "sambaGroupType: 2\n"); + fprintf(add_fd, "displayName: Domain Guests\n"); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Write the Domain Computers entity */ + fprintf(add_fd, "# Domain Computers, %s, %s\n", group_attr, + suffix); + fprintf(add_fd, "dn: cn=Domain Computers,ou=%s,%s\n", + group_attr, suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "gidNumber: 515\n"); + fprintf(add_fd, "cn: Domain Computers\n"); + fprintf(add_fd, "description: Netbios Domain Computers accounts\n"); + fprintf(add_fd, "sambaSID: %s-515\n", sid); + fprintf(add_fd, "sambaGroupType: 2\n"); + fprintf(add_fd, "displayName: Domain Computers\n"); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Write the Admininistrators Groups entity */ + fprintf(add_fd, "# Administrators, %s, %s\n", group_attr, + suffix); + fprintf(add_fd, "dn: cn=Administrators,ou=%s,%s\n", group_attr, + suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "gidNumber: 544\n"); + fprintf(add_fd, "cn: Administrators\n"); + fprintf(add_fd, "description: Netbios Domain Members can fully administer the computer/sambaDomainName\n"); + fprintf(add_fd, "sambaSID: %s-544\n", builtin_sid); + fprintf(add_fd, "sambaGroupType: 5\n"); + fprintf(add_fd, "displayName: Administrators\n"); + fprintf(add_fd, "\n"); + + /* Write the Print Operator entity */ + fprintf(add_fd, "# Print Operators, %s, %s\n", group_attr, + suffix); + fprintf(add_fd, "dn: cn=Print Operators,ou=%s,%s\n", + group_attr, suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "gidNumber: 550\n"); + fprintf(add_fd, "cn: Print Operators\n"); + fprintf(add_fd, "description: Netbios Domain Print Operators\n"); + fprintf(add_fd, "sambaSID: %s-550\n", builtin_sid); + fprintf(add_fd, "sambaGroupType: 5\n"); + fprintf(add_fd, "displayName: Print Operators\n"); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Write the Backup Operators entity */ + fprintf(add_fd, "# Backup Operators, %s, %s\n", group_attr, + suffix); + fprintf(add_fd, "dn: cn=Backup Operators,ou=%s,%s\n", + group_attr, suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "gidNumber: 551\n"); + fprintf(add_fd, "cn: Backup Operators\n"); + fprintf(add_fd, "description: Netbios Domain Members can bypass file security to back up files\n"); + fprintf(add_fd, "sambaSID: %s-551\n", builtin_sid); + fprintf(add_fd, "sambaGroupType: 5\n"); + fprintf(add_fd, "displayName: Backup Operators\n"); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Write the Replicators entity */ + fprintf(add_fd, "# Replicators, %s, %s\n", group_attr, suffix); + fprintf(add_fd, "dn: cn=Replicators,ou=%s,%s\n", group_attr, + suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "gidNumber: 552\n"); + fprintf(add_fd, "cn: Replicators\n"); + fprintf(add_fd, "description: Netbios Domain Supports file replication in a sambaDomainName\n"); + fprintf(add_fd, "sambaSID: %s-552\n", builtin_sid); + fprintf(add_fd, "sambaGroupType: 5\n"); + fprintf(add_fd, "displayName: Replicators\n"); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Deallocate memory, and return */ + SAFE_FREE(suffix_attr); + SAFE_FREE(user_attr); + SAFE_FREE(group_attr); + return NT_STATUS_OK; +} + +static NTSTATUS map_populate_groups(GROUPMAP *groupmap, ACCOUNTMAP *accountmap, fstring sid, + const char *suffix, const char *builtin_sid) +{ + char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ','); + + /* Map the groups created by populate_ldap_for_ldif */ + groupmap[0].rid = 512; + groupmap[0].gidNumber = 512; + snprintf(groupmap[0].sambaSID, sizeof(groupmap[0].sambaSID), + "%s-512", sid); + snprintf(groupmap[0].group_dn, sizeof(groupmap[0].group_dn), + "cn=Domain Admins,ou=%s,%s", + group_attr, suffix); + accountmap[0].rid = 512; + snprintf(accountmap[0].cn, sizeof(accountmap[0].cn), + "%s", "Domain Admins"); + + groupmap[1].rid = 513; + groupmap[1].gidNumber = 513; + snprintf(groupmap[1].sambaSID, sizeof(groupmap[1].sambaSID), + "%s-513", sid); + snprintf(groupmap[1].group_dn, sizeof(groupmap[1].group_dn), + "cn=Domain Users,ou=%s,%s", + group_attr, suffix); + accountmap[1].rid = 513; + snprintf(accountmap[1].cn, sizeof(accountmap[1].cn), + "%s", "Domain Users"); + + groupmap[2].rid = 514; + groupmap[2].gidNumber = 514; + snprintf(groupmap[2].sambaSID, sizeof(groupmap[2].sambaSID), + "%s-514", sid); + snprintf(groupmap[2].group_dn, sizeof(groupmap[2].group_dn), + "cn=Domain Guests,ou=%s,%s", + group_attr, suffix); + accountmap[2].rid = 514; + snprintf(accountmap[2].cn, sizeof(accountmap[2].cn), + "%s", "Domain Guests"); + + groupmap[3].rid = 515; + groupmap[3].gidNumber = 515; + snprintf(groupmap[3].sambaSID, sizeof(groupmap[3].sambaSID), + "%s-515", sid); + snprintf(groupmap[3].group_dn, sizeof(groupmap[3].group_dn), + "cn=Domain Computers,ou=%s,%s", + group_attr, suffix); + accountmap[3].rid = 515; + snprintf(accountmap[3].cn, sizeof(accountmap[3].cn), + "%s", "Domain Computers"); + + groupmap[4].rid = 544; + groupmap[4].gidNumber = 544; + snprintf(groupmap[4].sambaSID, sizeof(groupmap[4].sambaSID), + "%s-544", builtin_sid); + snprintf(groupmap[4].group_dn, sizeof(groupmap[4].group_dn), + "cn=Administrators,ou=%s,%s", + group_attr, suffix); + accountmap[4].rid = 515; + snprintf(accountmap[4].cn, sizeof(accountmap[4].cn), + "%s", "Administrators"); + + groupmap[5].rid = 550; + groupmap[5].gidNumber = 550; + snprintf(groupmap[5].sambaSID, sizeof(groupmap[5].sambaSID), + "%s-550", builtin_sid); + snprintf(groupmap[5].group_dn, sizeof(groupmap[5].group_dn), + "cn=Print Operators,ou=%s,%s", + group_attr, suffix); + accountmap[5].rid = 550; + snprintf(accountmap[5].cn, sizeof(accountmap[5].cn), + "%s", "Print Operators"); + + groupmap[6].rid = 551; + groupmap[6].gidNumber = 551; + snprintf(groupmap[6].sambaSID, sizeof(groupmap[6].sambaSID), + "%s-551", builtin_sid); + snprintf(groupmap[6].group_dn, sizeof(groupmap[6].group_dn), + "cn=Backup Operators,ou=%s,%s", + group_attr, suffix); + accountmap[6].rid = 551; + snprintf(accountmap[6].cn, sizeof(accountmap[6].cn), + "%s", "Backup Operators"); + + groupmap[7].rid = 552; + groupmap[7].gidNumber = 552; + snprintf(groupmap[7].sambaSID, sizeof(groupmap[7].sambaSID), + "%s-552", builtin_sid); + snprintf(groupmap[7].group_dn, sizeof(groupmap[7].group_dn), + "cn=Replicators,ou=%s,%s", + group_attr, suffix); + accountmap[7].rid = 551; + snprintf(accountmap[7].cn, sizeof(accountmap[7].cn), + "%s", "Replicators"); + SAFE_FREE(group_attr); + return NT_STATUS_OK; +} + +/* + * This is a crap routine, but I think it's the quickest way to solve the + * UTF8->base64 problem. + */ + +static int fprintf_attr(FILE *add_fd, const char *attr_name, + const char *fmt, ...) +{ + va_list ap; + char *value, *p, *base64; + DATA_BLOB base64_blob; + bool do_base64 = False; + int res; + + va_start(ap, fmt); + value = talloc_vasprintf(NULL, fmt, ap); + va_end(ap); + + SMB_ASSERT(value != NULL); + + for (p=value; *p; p++) { + if (*p & 0x80) { + do_base64 = True; + break; + } + } + + if (!do_base64) { + bool only_whitespace = True; + for (p=value; *p; p++) { + /* + * I know that this not multibyte safe, but we break + * on the first non-whitespace character anyway. + */ + if (!isspace(*p)) { + only_whitespace = False; + break; + } + } + if (only_whitespace) { + do_base64 = True; + } + } + + if (!do_base64) { + res = fprintf(add_fd, "%s: %s\n", attr_name, value); + TALLOC_FREE(value); + return res; + } + + base64_blob.data = (unsigned char *)value; + base64_blob.length = strlen(value); + + base64 = base64_encode_data_blob(value, base64_blob); + SMB_ASSERT(base64 != NULL); + + res = fprintf(add_fd, "%s:: %s\n", attr_name, base64); + TALLOC_FREE(value); + return res; +} + +static NTSTATUS fetch_group_info_to_ldif(SAM_DELTA_CTR *delta, GROUPMAP *groupmap, + FILE *add_fd, fstring sid, char *suffix) +{ + fstring groupname; + uint32 grouptype = 0, g_rid = 0; + char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ','); + + /* Get the group name */ + unistr2_to_ascii(groupname, + &delta->group_info.uni_grp_name, + sizeof(groupname)); + + /* Set up the group type (always 2 for group info) */ + grouptype = 2; + + /* These groups are entered by populate_ldap_for_ldif */ + if (strcmp(groupname, "Domain Admins") == 0 || + strcmp(groupname, "Domain Users") == 0 || + strcmp(groupname, "Domain Guests") == 0 || + strcmp(groupname, "Domain Computers") == 0 || + strcmp(groupname, "Administrators") == 0 || + strcmp(groupname, "Print Operators") == 0 || + strcmp(groupname, "Backup Operators") == 0 || + strcmp(groupname, "Replicators") == 0) { + SAFE_FREE(group_attr); + return NT_STATUS_OK; + } else { + /* Increment the gid for the new group */ + ldif_gid++; + } + + /* Map the group rid, gid, and dn */ + g_rid = delta->group_info.gid.g_rid; + groupmap->rid = g_rid; + groupmap->gidNumber = ldif_gid; + snprintf(groupmap->sambaSID, sizeof(groupmap->sambaSID), + "%s-%d", sid, g_rid); + snprintf(groupmap->group_dn, sizeof(groupmap->group_dn), + "cn=%s,ou=%s,%s", groupname, group_attr, suffix); + + /* Write the data to the temporary add ldif file */ + fprintf(add_fd, "# %s, %s, %s\n", groupname, group_attr, + suffix); + fprintf_attr(add_fd, "dn", "cn=%s,ou=%s,%s", groupname, group_attr, + suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf_attr(add_fd, "cn", "%s", groupname); + fprintf(add_fd, "gidNumber: %d\n", ldif_gid); + fprintf(add_fd, "sambaSID: %s\n", groupmap->sambaSID); + fprintf(add_fd, "sambaGroupType: %d\n", grouptype); + fprintf_attr(add_fd, "displayName", "%s", groupname); + fprintf(add_fd, "\n"); + fflush(add_fd); + + SAFE_FREE(group_attr); + /* Return */ + return NT_STATUS_OK; +} + +static NTSTATUS fetch_account_info_to_ldif(SAM_DELTA_CTR *delta, + GROUPMAP *groupmap, + ACCOUNTMAP *accountmap, + FILE *add_fd, + fstring sid, char *suffix, + int alloced) +{ + fstring username, logonscript, homedrive, homepath = "", homedir = ""; + fstring hex_nt_passwd, hex_lm_passwd; + fstring description, profilepath, fullname, sambaSID; + uchar lm_passwd[16], nt_passwd[16]; + char *flags, *user_rdn; + const char *ou; + const char* nopasswd = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; + static uchar zero_buf[16]; + uint32 rid = 0, group_rid = 0, gidNumber = 0; + time_t unix_time; + int i; + + /* Get the username */ + unistr2_to_ascii(username, + &(delta->account_info.uni_acct_name), + sizeof(username)); + + /* Get the rid */ + rid = delta->account_info.user_rid; + + /* Map the rid and username for group member info later */ + accountmap->rid = rid; + snprintf(accountmap->cn, sizeof(accountmap->cn), "%s", username); + + /* Get the home directory */ + if (delta->account_info.acb_info & ACB_NORMAL) { + unistr2_to_ascii(homedir, &(delta->account_info.uni_home_dir), + sizeof(homedir)); + if (!*homedir) { + snprintf(homedir, sizeof(homedir), "/home/%s", username); + } else { + snprintf(homedir, sizeof(homedir), "/nobodyshomedir"); + } + ou = lp_ldap_user_suffix(); + } else { + ou = lp_ldap_machine_suffix(); + snprintf(homedir, sizeof(homedir), "/machinehomedir"); + } + + /* Get the logon script */ + unistr2_to_ascii(logonscript, &(delta->account_info.uni_logon_script), + sizeof(logonscript)); + + /* Get the home drive */ + unistr2_to_ascii(homedrive, &(delta->account_info.uni_dir_drive), + sizeof(homedrive)); + + /* Get the home path */ + unistr2_to_ascii(homepath, &(delta->account_info.uni_home_dir), + sizeof(homepath)); + + /* Get the description */ + unistr2_to_ascii(description, &(delta->account_info.uni_acct_desc), + sizeof(description)); + + /* Get the display name */ + unistr2_to_ascii(fullname, &(delta->account_info.uni_full_name), + sizeof(fullname)); + + /* Get the profile path */ + unistr2_to_ascii(profilepath, &(delta->account_info.uni_profile), + sizeof(profilepath)); + + /* Get lm and nt password data */ + if (memcmp(delta->account_info.pass.buf_lm_pwd, zero_buf, 16) != 0) { + sam_pwd_hash(delta->account_info.user_rid, + delta->account_info.pass.buf_lm_pwd, + lm_passwd, 0); + pdb_sethexpwd(hex_lm_passwd, lm_passwd, + delta->account_info.acb_info); + } else { + pdb_sethexpwd(hex_lm_passwd, NULL, 0); + } + if (memcmp(delta->account_info.pass.buf_nt_pwd, zero_buf, 16) != 0) { + sam_pwd_hash(delta->account_info.user_rid, + delta->account_info.pass.buf_nt_pwd, + nt_passwd, 0); + pdb_sethexpwd(hex_nt_passwd, nt_passwd, + delta->account_info.acb_info); + } else { + pdb_sethexpwd(hex_nt_passwd, NULL, 0); + } + unix_time = nt_time_to_unix(delta->account_info.pwd_last_set_time); + + /* Increment the uid for the new user */ + ldif_uid++; + + /* Set up group id and sambaSID for the user */ + group_rid = delta->account_info.group_rid; + for (i=0; iaccount_info.acb_info, + NEW_PW_FORMAT_SPACE_PADDED_LEN); + + /* Add the user to the temporary add ldif file */ + /* this isn't quite right...we can't assume there's just OU=. jmcd */ + user_rdn = sstring_sub(ou, '=', ','); + fprintf(add_fd, "# %s, %s, %s\n", username, user_rdn, suffix); + fprintf_attr(add_fd, "dn", "uid=%s,ou=%s,%s", username, user_rdn, + suffix); + SAFE_FREE(user_rdn); + fprintf(add_fd, "ObjectClass: top\n"); + fprintf(add_fd, "objectClass: inetOrgPerson\n"); + fprintf(add_fd, "objectClass: posixAccount\n"); + fprintf(add_fd, "objectClass: shadowAccount\n"); + fprintf(add_fd, "objectClass: sambaSamAccount\n"); + fprintf_attr(add_fd, "cn", "%s", username); + fprintf_attr(add_fd, "sn", "%s", username); + fprintf_attr(add_fd, "uid", "%s", username); + fprintf(add_fd, "uidNumber: %d\n", ldif_uid); + fprintf(add_fd, "gidNumber: %d\n", gidNumber); + fprintf_attr(add_fd, "homeDirectory", "%s", homedir); + if (*homepath) + fprintf_attr(add_fd, "sambaHomePath", "%s", homepath); + if (*homedrive) + fprintf_attr(add_fd, "sambaHomeDrive", "%s", homedrive); + if (*logonscript) + fprintf_attr(add_fd, "sambaLogonScript", "%s", logonscript); + fprintf(add_fd, "loginShell: %s\n", + ((delta->account_info.acb_info & ACB_NORMAL) ? + "/bin/bash" : "/bin/false")); + fprintf(add_fd, "gecos: System User\n"); + if (*description) + fprintf_attr(add_fd, "description", "%s", description); + fprintf(add_fd, "sambaSID: %s-%d\n", sid, rid); + fprintf(add_fd, "sambaPrimaryGroupSID: %s\n", sambaSID); + if(*fullname) + fprintf_attr(add_fd, "displayName", "%s", fullname); + if(*profilepath) + fprintf_attr(add_fd, "sambaProfilePath", "%s", profilepath); + if (strcmp(nopasswd, hex_lm_passwd) != 0) + fprintf(add_fd, "sambaLMPassword: %s\n", hex_lm_passwd); + if (strcmp(nopasswd, hex_nt_passwd) != 0) + fprintf(add_fd, "sambaNTPassword: %s\n", hex_nt_passwd); + fprintf(add_fd, "sambaPwdLastSet: %d\n", (int)unix_time); + fprintf(add_fd, "sambaAcctFlags: %s\n", flags); + fprintf(add_fd, "\n"); + fflush(add_fd); + + /* Return */ + return NT_STATUS_OK; +} + +static NTSTATUS fetch_alias_info_to_ldif(SAM_DELTA_CTR *delta, + GROUPMAP *groupmap, + FILE *add_fd, fstring sid, + char *suffix, + unsigned db_type) +{ + fstring aliasname, description; + uint32 grouptype = 0, g_rid = 0; + char *group_attr = sstring_sub(lp_ldap_group_suffix(), '=', ','); + + /* Get the alias name */ + unistr2_to_ascii(aliasname, &(delta->alias_info.uni_als_name), + sizeof(aliasname)); + + /* Get the alias description */ + unistr2_to_ascii(description, &(delta->alias_info.uni_als_desc), + sizeof(description)); + + /* Set up the group type */ + switch (db_type) { + case SAM_DATABASE_DOMAIN: + grouptype = 4; + break; + case SAM_DATABASE_BUILTIN: + grouptype = 5; + break; + default: + grouptype = 4; + break; + } + + /* + These groups are entered by populate_ldap_for_ldif + Note that populate creates a group called Relicators, + but NT returns a group called Replicator + */ + if (strcmp(aliasname, "Domain Admins") == 0 || + strcmp(aliasname, "Domain Users") == 0 || + strcmp(aliasname, "Domain Guests") == 0 || + strcmp(aliasname, "Domain Computers") == 0 || + strcmp(aliasname, "Administrators") == 0 || + strcmp(aliasname, "Print Operators") == 0 || + strcmp(aliasname, "Backup Operators") == 0 || + strcmp(aliasname, "Replicator") == 0) { + SAFE_FREE(group_attr); + return NT_STATUS_OK; + } else { + /* Increment the gid for the new group */ + ldif_gid++; + } + + /* Map the group rid and gid */ + g_rid = delta->group_info.gid.g_rid; + groupmap->gidNumber = ldif_gid; + snprintf(groupmap->sambaSID, sizeof(groupmap->sambaSID), + "%s-%d", sid, g_rid); + + /* Write the data to the temporary add ldif file */ + fprintf(add_fd, "# %s, %s, %s\n", aliasname, group_attr, + suffix); + fprintf_attr(add_fd, "dn", "cn=%s,ou=%s,%s", aliasname, group_attr, + suffix); + fprintf(add_fd, "objectClass: posixGroup\n"); + fprintf(add_fd, "objectClass: sambaGroupMapping\n"); + fprintf(add_fd, "cn: %s\n", aliasname); + fprintf(add_fd, "gidNumber: %d\n", ldif_gid); + fprintf(add_fd, "sambaSID: %s\n", groupmap->sambaSID); + fprintf(add_fd, "sambaGroupType: %d\n", grouptype); + fprintf_attr(add_fd, "displayName", "%s", aliasname); + if (description[0]) + fprintf_attr(add_fd, "description", "%s", description); + fprintf(add_fd, "\n"); + fflush(add_fd); + + SAFE_FREE(group_attr); + /* Return */ + return NT_STATUS_OK; +} + +static NTSTATUS fetch_groupmem_info_to_ldif(SAM_DELTA_CTR *delta, + SAM_DELTA_HDR *hdr_delta, + GROUPMAP *groupmap, + ACCOUNTMAP *accountmap, + FILE *mod_fd, int alloced) +{ + fstring group_dn; + uint32 group_rid = 0, rid = 0; + int i, j, k; + + /* Get the dn for the group */ + if (delta->grp_mem_info.num_members > 0) { + group_rid = hdr_delta->target_rid; + for (j=0; jgrp_mem_info.num_members; i++) { + rid = delta->grp_mem_info.rids[i]; + for (k=0; k] [options]\n" + "\t to pull accounts from a remote PDC where we are a BDC\n" + "\t\t no args puts accounts in local passdb from smb.conf\n" + "\t\t ldif - put accounts in ldif format (file defaults to " + "/tmp/tmp.ldif\n"); + + net_common_flags_usage(argc, argv); + return -1; +} + + +/* dump sam database via samsync rpc calls */ +NTSTATUS rpc_vampire_internals(const DOM_SID *domain_sid, + const char *domain_name, + struct cli_state *cli, + struct rpc_pipe_client *pipe_hnd, + TALLOC_CTX *mem_ctx, + int argc, + const char **argv) +{ + NTSTATUS result; + fstring my_dom_sid_str; + fstring rem_dom_sid_str; + + if (!sid_equal(domain_sid, get_global_sam_sid())) { + d_printf("Cannot import users from %s at this time, " + "as the current domain:\n\t%s: %s\nconflicts " + "with the remote domain\n\t%s: %s\n" + "Perhaps you need to set: \n\n\tsecurity=user\n\t" + "workgroup=%s\n\n in your smb.conf?\n", + domain_name, + get_global_sam_name(), + sid_to_fstring(my_dom_sid_str, + get_global_sam_sid()), + domain_name, sid_to_fstring(rem_dom_sid_str, + domain_sid), + domain_name); + return NT_STATUS_UNSUCCESSFUL; } - sid_copy( &dom_sid, get_global_sam_sid() ); - result = fetch_database(cli, SAM_DATABASE_DOMAIN, &ret_creds, dom_sid); + if (argc >= 1 && (strcmp(argv[0], "ldif") == 0)) { + result = fetch_database_to_ldif(pipe_hnd, SAM_DATABASE_DOMAIN, + *domain_sid, argv[1]); + } else { + result = fetch_database(pipe_hnd, SAM_DATABASE_DOMAIN, + *domain_sid); + } if (!NT_STATUS_IS_OK(result)) { - d_printf("Failed to fetch domain database: %s\n", - nt_errstr(result)); + d_fprintf(stderr, "Failed to fetch domain database: %s\n", + nt_errstr(result)); if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED)) - d_printf("Perhaps %s is a Windows 2000 native mode " - "domain?\n", lp_workgroup()); + d_fprintf(stderr, "Perhaps %s is a Windows 2000 " + "native mode domain?\n", domain_name); goto fail; } - sid_copy(&dom_sid, &global_sid_Builtin); - - result = fetch_database(cli, SAM_DATABASE_BUILTIN, &ret_creds, - dom_sid); + if (argc >= 1 && (strcmp(argv[0], "ldif") == 0)) { + result = fetch_database_to_ldif(pipe_hnd, SAM_DATABASE_BUILTIN, + global_sid_Builtin, argv[1]); + } else { + result = fetch_database(pipe_hnd, SAM_DATABASE_BUILTIN, + global_sid_Builtin); + } if (!NT_STATUS_IS_OK(result)) { - d_printf("Failed to fetch builtin database: %s\n", - nt_errstr(result)); + d_fprintf(stderr, "Failed to fetch builtin database: %s\n", + nt_errstr(result)); goto fail; - } + } /* Currently we crash on PRIVS somewhere in unmarshalling */ /* Dump_database(cli, SAM_DATABASE_PRIVS, &ret_creds); */ - cli_nt_session_close(cli); - - return 0; - -fail: - if (cli) - cli_nt_session_close(cli); - - return -1; + fail: + return result; }