r17439: Fix logic error in checking TALLOC return. Spotted by Volker.
[ira/wip.git] / source3 / rpc_server / srv_samr_nt.c
index 81344cdc1eca9db84a667889ef46973b76ddeec7..31e434cbfb1cf7b48f7ad505e7083c51d5f55043 100644 (file)
@@ -252,6 +252,17 @@ static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid, const char *sid_str)
        TALLOC_CTX *mem_ctx;
        DISP_INFO *dpi;
 
+       /* There are two cases to consider here:
+          1) The SID is a domain SID and we look for an equality match, or
+          2) This is an account SID and so we return the DISP_INFO* for our 
+             domain */
+
+       if ( psid && sid_check_is_in_our_domain( psid ) ) {
+               DEBUG(10,("get_samr_dispinfo_by_sid: Replacing %s with our domain SID\n",
+                       sid_str));
+               psid = get_global_sam_sid();
+       }
+
        for (dpi = disp_info_list; dpi; dpi = dpi->next) {
                if (sid_equal(psid, &dpi->sid)) {
                        return dpi;
@@ -459,7 +470,7 @@ static void force_flush_samr_cache(DISP_INFO *disp_info)
  Ensure password info is never given out. Paranioa... JRA.
  ********************************************************************/
 
-static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
+static void samr_clear_sam_passwd(struct samu *sam_pass)
 {
        
        if (!sam_pass)
@@ -471,7 +482,7 @@ static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
        pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
 }
 
-static uint32 count_sam_users(struct disp_info *info, uint16 acct_flags)
+static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
 {
        struct samr_displayentry *entry;
 
@@ -1424,7 +1435,7 @@ NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LO
                }
        }
 
-       init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
+       init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, type, r_u->status);
 
        DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
 
@@ -1462,7 +1473,7 @@ NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_
         */
 
        r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
-                               q_u->nt_newpass.pass, q_u->nt_oldhash.hash);
+                               q_u->nt_newpass.pass, q_u->nt_oldhash.hash, NULL);
 
        init_samr_r_chgpasswd_user(r_u, r_u->status);
 
@@ -1471,6 +1482,98 @@ NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_
        return r_u->status;
 }
 
+/*******************************************************************
+ _samr_chgpasswd_user3
+ ********************************************************************/
+
+NTSTATUS _samr_chgpasswd_user3(pipes_struct *p, SAMR_Q_CHGPASSWD_USER3 *q_u, SAMR_R_CHGPASSWD_USER3 *r_u)
+{
+       fstring user_name;
+       fstring wks;
+       uint32 reject_reason;
+       SAM_UNK_INFO_1 *info = NULL;
+       SAMR_CHANGE_REJECT *reject = NULL;
+
+       DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
+
+       rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
+       rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
+
+       DEBUG(5,("_samr_chgpasswd_user3: user: %s wks: %s\n", user_name, wks));
+
+       /*
+        * Pass the user through the NT -> unix user mapping
+        * function.
+        */
+       (void)map_username(user_name);
+       /*
+        * UNIX username case mangling not required, pass_oem_change 
+        * is case insensitive.
+        */
+
+       r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
+                                     q_u->nt_newpass.pass, q_u->nt_oldhash.hash, &reject_reason);
+
+       if (NT_STATUS_EQUAL(r_u->status, NT_STATUS_PASSWORD_RESTRICTION) || 
+           NT_STATUS_EQUAL(r_u->status, NT_STATUS_ACCOUNT_RESTRICTION)) {
+
+               uint32 min_pass_len,pass_hist,password_properties;
+               time_t u_expire, u_min_age;
+               NTTIME nt_expire, nt_min_age;
+               uint32 account_policy_temp;
+
+               if ((info = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_INFO_1)) == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               if ((reject = TALLOC_ZERO_P(p->mem_ctx, SAMR_CHANGE_REJECT)) == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               ZERO_STRUCTP(info);
+               ZERO_STRUCTP(reject);
+
+               become_root();
+
+               /* AS ROOT !!! */
+
+               pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
+               min_pass_len = account_policy_temp;
+
+               pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
+               pass_hist = account_policy_temp;
+
+               pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
+               password_properties = account_policy_temp;
+
+               pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
+               u_expire = account_policy_temp;
+
+               pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
+               u_min_age = account_policy_temp;
+
+               /* !AS ROOT */
+               
+               unbecome_root();
+
+               unix_to_nt_time_abs(&nt_expire, u_expire);
+               unix_to_nt_time_abs(&nt_min_age, u_min_age);
+
+               init_unk_info1(info, (uint16)min_pass_len, (uint16)pass_hist, 
+                              password_properties, nt_expire, nt_min_age);
+
+               reject->reject_reason = reject_reason;
+       }
+       
+       init_samr_r_chgpasswd_user3(r_u, r_u->status, reject, info);
+
+       DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
+
+       return r_u->status;
+}
+
 /*******************************************************************
 makes a SAMR_R_LOOKUP_RIDS structure.
 ********************************************************************/
@@ -1515,13 +1618,15 @@ static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
 {
        const char **names;
-       uint32 *attrs = NULL;
+       enum SID_NAME_USE *attrs = NULL;
+       uint32 *wire_attrs = NULL;
        UNIHDR *hdr_name = NULL;
        UNISTR2 *uni_name = NULL;
        DOM_SID pol_sid;
        int num_rids = q_u->num_rids1;
        uint32 acc_granted;
-       
+       int i;
+
        r_u->status = NT_STATUS_OK;
 
        DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
@@ -1537,9 +1642,10 @@ NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOK
        }
 
        names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
-       attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
+       attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum SID_NAME_USE, num_rids);
+       wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
 
-       if ((num_rids != 0) && ((names == NULL) || (attrs == NULL)))
+       if ((num_rids != 0) && ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL)))
                return NT_STATUS_NO_MEMORY;
 
        become_root();  /* lookup_sid can require root privs */
@@ -1547,11 +1653,20 @@ NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOK
                                      names, attrs);
        unbecome_root();
 
+       if ( NT_STATUS_EQUAL(r_u->status, NT_STATUS_NONE_MAPPED) && (num_rids == 0) ) {
+               r_u->status = NT_STATUS_OK;
+       }
+
        if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
                                  &hdr_name, &uni_name))
                return NT_STATUS_NO_MEMORY;
 
-       init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, attrs);
+       /* Convert from enum SID_NAME_USE to uint32 for wire format. */
+       for (i = 0; i < num_rids; i++) {
+               wire_attrs[i] = (uint32)attrs[i];
+       }
+
+       init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, wire_attrs);
 
        DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
 
@@ -1564,7 +1679,7 @@ NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOK
 
 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
 {
-       SAM_ACCOUNT *sampass=NULL;
+       struct samu *sampass=NULL;
        DOM_SID sid;
        POLICY_HND domain_pol = q_u->domain_pol;
        POLICY_HND *user_pol = &r_u->user_pol;
@@ -1590,10 +1705,9 @@ NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USE
        if ( !NT_STATUS_IS_OK(nt_status) )
                return nt_status;
 
-       nt_status = pdb_init_sam_talloc(p->mem_ctx, &sampass);
-       
-       if (!NT_STATUS_IS_OK(nt_status))
-               return nt_status;
+       if ( !(sampass = samu_new( p->mem_ctx )) ) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        /* append the user's RID to it */
        
@@ -1624,7 +1738,7 @@ NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USE
                return NT_STATUS_NO_SUCH_USER;
        }
 
-       pdb_free_sam(&sampass);
+       TALLOC_FREE(sampass);
 
        /* associate the user's SID and access bits with the new handle. */
        if ((info = get_samr_info_by_sid(&sid)) == NULL)
@@ -1644,21 +1758,18 @@ NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USE
 
 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
 {
-       SAM_ACCOUNT *smbpass=NULL;
+       struct samu *smbpass=NULL;
        BOOL ret;
-       NTSTATUS nt_status;
 
-       nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
-       
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return nt_status;
+       if ( !(smbpass = samu_new( mem_ctx )) ) {
+               return NT_STATUS_NO_MEMORY;
        }
-
+       
        become_root();
        ret = pdb_getsampwsid(smbpass, user_sid);
        unbecome_root();
 
-       if (ret==False) {
+       if ( !ret ) {
                DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
                return NT_STATUS_NO_SUCH_USER;
        }
@@ -1668,7 +1779,7 @@ static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_S
        ZERO_STRUCTP(id7);
        init_sam_user_info7(id7, pdb_get_username(smbpass) );
 
-       pdb_free_sam(&smbpass);
+       TALLOC_FREE(smbpass);
 
        return NT_STATUS_OK;
 }
@@ -1678,14 +1789,11 @@ static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_S
  *************************************************************************/
 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx, SAM_USER_INFO_9 * id9, DOM_SID *user_sid)
 {
-       SAM_ACCOUNT *smbpass=NULL;
+       struct samu *smbpass=NULL;
        BOOL ret;
-       NTSTATUS nt_status;
-
-       nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
 
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return nt_status;
+       if ( !(smbpass = samu_new( mem_ctx )) ) {
+               return NT_STATUS_NO_MEMORY;
        }
 
        become_root();
@@ -1702,7 +1810,7 @@ static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx, SAM_USER_INFO_9 * id9, DOM_
        ZERO_STRUCTP(id9);
        init_sam_user_info9(id9, pdb_get_group_rid(smbpass) );
 
-       pdb_free_sam(&smbpass);
+       TALLOC_FREE(smbpass);
 
        return NT_STATUS_OK;
 }
@@ -1713,14 +1821,11 @@ static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx, SAM_USER_INFO_9 * id9, DOM_
 
 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
 {
-       SAM_ACCOUNT *smbpass=NULL;
+       struct samu *smbpass=NULL;
        BOOL ret;
-       NTSTATUS nt_status;
 
-       nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
-       
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return nt_status;
+       if ( !(smbpass = samu_new( mem_ctx )) ) {
+               return NT_STATUS_NO_MEMORY;
        }
 
        become_root();
@@ -1737,7 +1842,7 @@ static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DO
        ZERO_STRUCTP(id16);
        init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
 
-       pdb_free_sam(&smbpass);
+       TALLOC_FREE(smbpass);
 
        return NT_STATUS_OK;
 }
@@ -1750,9 +1855,8 @@ static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DO
 
 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
 {
-       SAM_ACCOUNT *smbpass=NULL;
+       struct samu *smbpass=NULL;
        BOOL ret;
-       NTSTATUS nt_status;
 
        if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
                return NT_STATUS_ACCESS_DENIED;
@@ -1766,31 +1870,29 @@ static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_
         * Do *NOT* do become_root()/unbecome_root() here ! JRA.
         */
 
-       nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
-       
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return nt_status;
+       if ( !(smbpass = samu_new( mem_ctx )) ) {
+               return NT_STATUS_NO_MEMORY;
        }
 
        ret = pdb_getsampwsid(smbpass, user_sid);
 
        if (ret == False) {
                DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
-               pdb_free_sam(&smbpass);
+               TALLOC_FREE(smbpass);
                return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
        }
 
        DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
 
        if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
-               pdb_free_sam(&smbpass);
+               TALLOC_FREE(smbpass);
                return NT_STATUS_ACCOUNT_DISABLED;
        }
 
        ZERO_STRUCTP(id18);
        init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
        
-       pdb_free_sam(&smbpass);
+       TALLOC_FREE(smbpass);
 
        return NT_STATUS_OK;
 }
@@ -1801,10 +1903,12 @@ static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_
 
 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
 {
-       SAM_ACCOUNT *sampass=NULL;
+       struct samu *sampass=NULL;
        BOOL ret;
 
-       pdb_init_sam_talloc(mem_ctx, &sampass);
+       if ( !(sampass = samu_new( mem_ctx )) ) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        become_root();
        ret = pdb_getsampwsid(sampass, user_sid);
@@ -1822,7 +1926,7 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DO
        ZERO_STRUCTP(id20);
        init_sam_user_info20A(id20, sampass);
        
-       pdb_free_sam(&sampass);
+       TALLOC_FREE(sampass);
 
        return NT_STATUS_OK;
 }
@@ -1834,13 +1938,12 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DO
 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21, 
                                 DOM_SID *user_sid, DOM_SID *domain_sid)
 {
-       SAM_ACCOUNT *sampass=NULL;
+       struct samu *sampass=NULL;
        BOOL ret;
        NTSTATUS nt_status;
 
-       nt_status = pdb_init_sam_talloc(mem_ctx, &sampass);
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return nt_status;
+       if ( !(sampass = samu_new( mem_ctx )) ) {
+               return NT_STATUS_NO_MEMORY;
        }
 
        become_root();
@@ -1859,9 +1962,9 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
        ZERO_STRUCTP(id21);
        nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
        
-       pdb_free_sam(&sampass);
+       TALLOC_FREE(sampass);
 
-       return NT_STATUS_OK;
+       return nt_status;
 }
 
 /*******************************************************************
@@ -1970,7 +2073,7 @@ NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_
 
 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
 {
-       SAM_ACCOUNT *sam_pass=NULL;
+       struct samu *sam_pass=NULL;
        DOM_SID  sid;
        DOM_SID *sids;
        DOM_GID dom_gid;
@@ -2010,8 +2113,10 @@ NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, S
        if (!sid_check_is_in_our_domain(&sid))
                return NT_STATUS_OBJECT_TYPE_MISMATCH;
 
-       pdb_init_sam_talloc(p->mem_ctx, &sam_pass);
-       
+        if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
+                return NT_STATUS_NO_MEMORY;
+        }
+
        become_root();
        ret = pdb_getsampwsid(sam_pass, &sid);
        unbecome_root();
@@ -2047,7 +2152,7 @@ NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, S
                DEBUG(5, ("Group sid %s for user %s not in our domain\n",
                          sid_string_static(pdb_get_group_sid(sam_pass)),
                          pdb_get_username(sam_pass)));
-               pdb_free_sam(&sam_pass);
+               TALLOC_FREE(sam_pass);
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
@@ -2083,14 +2188,16 @@ NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, S
 }
 
 /*******************************************************************
- _samr_query_dom_info
+ _samr_query_domain_info
  ********************************************************************/
 
-NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
+NTSTATUS _samr_query_domain_info(pipes_struct *p, 
+                                SAMR_Q_QUERY_DOMAIN_INFO *q_u, 
+                                SAMR_R_QUERY_DOMAIN_INFO *r_u)
 {
        struct samr_info *info = NULL;
        SAM_UNK_CTR *ctr;
-       uint32 min_pass_len,pass_hist,flag;
+       uint32 min_pass_len,pass_hist,password_properties;
        time_t u_expire, u_min_age;
        NTTIME nt_expire, nt_min_age;
 
@@ -2115,7 +2222,7 @@ NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SA
 
        r_u->status = NT_STATUS_OK;
        
-       DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
+       DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
        
        /* find the policy handle.  open a policy on it. */
        if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info)) {
@@ -2136,7 +2243,7 @@ NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SA
                        pass_hist = account_policy_temp;
 
                        pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
-                       flag = account_policy_temp;
+                       password_properties = account_policy_temp;
 
                        pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
                        u_expire = account_policy_temp;
@@ -2152,7 +2259,7 @@ NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SA
                        unix_to_nt_time_abs(&nt_min_age, u_min_age);
 
                        init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
-                                      flag, nt_expire, nt_min_age);
+                                      password_properties, nt_expire, nt_min_age);
                        break;
                case 0x02:
 
@@ -2203,11 +2310,17 @@ NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SA
                        
                        init_unk_info3(&ctr->info.inf3, nt_logout);
                        break;
+               case 0x04:
+                       init_unk_info4(&ctr->info.inf4, lp_serverstring());
+                       break;
                case 0x05:
-                       init_unk_info5(&ctr->info.inf5, global_myname());
+                       init_unk_info5(&ctr->info.inf5, get_global_sam_name());
                        break;
                case 0x06:
-                       init_unk_info6(&ctr->info.inf6);
+                       /* NT returns its own name when a PDC. win2k and later
+                        * only the name of the PDC if itself is a BDC (samba4
+                        * idl) */
+                       init_unk_info6(&ctr->info.inf6, global_myname());
                        break;
                case 0x07:
                        server_role = ROLE_DOMAIN_PDC;
@@ -2264,9 +2377,9 @@ NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SA
                }
        
 
-       init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
+       init_samr_r_query_domain_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
        
-       DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
+       DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
        
        return r_u->status;
 }
@@ -2318,18 +2431,13 @@ static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
                           SAMR_R_CREATE_USER *r_u)
 {
-       SAM_ACCOUNT *sam_pass=NULL;
-       fstring account;
+       char *account;
        DOM_SID sid;
-       pstring add_script;
        POLICY_HND dom_pol = q_u->domain_pol;
-       UNISTR2 user_account = q_u->uni_name;
        uint16 acb_info = q_u->acb_info;
        POLICY_HND *user_pol = &r_u->user_pol;
        struct samr_info *info = NULL;
-       BOOL ret;
        NTSTATUS nt_status;
-       struct passwd *pw;
        uint32 acc_granted;
        SEC_DESC *psd;
        size_t    sd_size;
@@ -2358,33 +2466,20 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       rpcstr_pull(account, user_account.buffer, sizeof(account),
-                   user_account.uni_str_len*2, 0);
-       strlower_m(account);
+       account = rpcstr_pull_unistr2_talloc(p->mem_ctx, &q_u->uni_name);
+       if (account == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        nt_status = can_create(p->mem_ctx, account);
        if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
        }
 
-       /*********************************************************************
-        * HEADS UP!  If we have to create a new user account, we have to get
-        * a new RID from somewhere.  This used to be done by the passdb
-        * backend. It has been moved into idmap now.  Since idmap is now
-        * wrapped up behind winbind, this means you have to run winbindd if
-        * you want new accounts to get a new RID when "enable rid algorithm =
-        * no".  Tough.  We now have a uniform way of allocating RIDs
-        * regardless of what ever passdb backend people may use.  --jerry
-        * (2003-07-10)
-        *********************************************************************/
-
-       pw = Get_Pwnam(account);
-
        /* determine which user right we need to check based on the acb_info */
        
        if ( acb_info & ACB_WSTRUST )
        {
-               pstrcpy(add_script, lp_addmachine_script());
                se_priv_copy( &se_rights, &se_machine_account );
                can_add_account = user_has_privileges(
                        p->pipe_user.nt_user_token, &se_rights );
@@ -2394,7 +2489,6 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
        else if ( acb_info & ACB_NORMAL &&
                  (account[strlen(account)-1] != '$') )
        {
-               pstrcpy(add_script, lp_adduser_script());
                se_priv_copy( &se_rights, &se_add_users );
                can_add_account = user_has_privileges(
                        p->pipe_user.nt_user_token, &se_rights );
@@ -2402,7 +2496,6 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
        else    /* implicit assumption of a BDC or domain trust account here
                 * (we already check the flags earlier) */
        {
-               pstrcpy(add_script, lp_addmachine_script());
                if ( lp_enable_privileges() ) {
                        /* only Domain Admins can add a BDC or domain trust */
                        se_priv_copy( &se_rights, &se_priv_none );
@@ -2413,47 +2506,17 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
        }
                
        DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
-               p->pipe_user_name, can_add_account ? "True":"False" ));
+                 uidtoname(p->pipe_user.ut.uid),
+                 can_add_account ? "True":"False" ));
                
        /********** BEGIN Admin BLOCK **********/
 
        if ( can_add_account )
                become_root();
 
-       if ( !pw ) {
-               if (*add_script) {
-                       int add_ret;
-
-                       all_string_sub(add_script, "%u", account,
-                                      sizeof(add_script));
-                       add_ret = smbrun(add_script,NULL);
-                       DEBUG(add_ret ? 0 : 3,("_samr_create_user: Running "
-                                              "the command `%s' gave %d\n",
-                                              add_script, add_ret));
-               }
-       }
-
-       /* implicit call to getpwnam() next.  we have a valid SID coming out
-        * of this call */
+       nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
+                                   &r_u->user_rid);
 
-       flush_pwnam_cache();
-       nt_status = pdb_init_sam_new(&sam_pass, account);
-
-       /* this code is order such that we have no unnecessary retuns 
-          out of the admin block of code */    
-          
-       if ( NT_STATUS_IS_OK(nt_status) ) {
-               pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
-       
-               if ( !(ret = pdb_add_sam_account(sam_pass)) ) {
-                       pdb_free_sam(&sam_pass);
-                       DEBUG(0, ("could not add user/computer %s to passdb. "
-                                 "Check permissions?\n", 
-                               account));
-                       nt_status = NT_STATUS_ACCESS_DENIED;
-               }
-       }
-               
        if ( can_add_account )
                unbecome_root();
 
@@ -2465,8 +2528,8 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
                return nt_status;
                        
        /* Get the user's SID */
-       
-       sid_copy(&sid, pdb_get_user_sid(sam_pass));
+
+       sid_compose(&sid, get_global_sam_sid(), r_u->user_rid);
        
        make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
                            &sid, SAMR_USR_RIGHTS_WRITE_PW);
@@ -2482,7 +2545,6 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
 
        /* associate the user's SID with the new handle. */
        if ((info = get_samr_info_by_sid(&sid)) == NULL) {
-               pdb_free_sam(&sam_pass);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -2492,19 +2554,14 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
 
        /* get a (unique) handle.  open a policy on it. */
        if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
-               pdb_free_sam(&sam_pass);
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
        /* After a "set" ensure we have no cached display info. */
        force_flush_samr_cache(info->disp_info);
 
-       r_u->user_rid=pdb_get_user_rid(sam_pass);
-
        r_u->access_granted = acc_granted;
 
-       pdb_free_sam(&sam_pass);
-
        return NT_STATUS_OK;
 }
 
@@ -2537,6 +2594,11 @@ NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CO
           was observed from a win98 client trying to enumerate users (when configured  
           user level access control on shares)   --jerry */
           
+       if (des_access == MAXIMUM_ALLOWED_ACCESS) {
+               /* Map to max possible knowing we're filtered below. */
+               des_access = GENERIC_ALL_ACCESS;
+       }
+
        se_map_generic( &des_access, &sam_generic_mapping );
        info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
        
@@ -2873,6 +2935,7 @@ NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_A
                /* Check we actually have the requested alias */
                enum SID_NAME_USE type;
                BOOL result;
+               gid_t gid;
 
                become_root();
                result = lookup_sid(NULL, &sid, NULL, NULL, &type);
@@ -2881,6 +2944,13 @@ NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_A
                if (!result || (type != SID_NAME_ALIAS)) {
                        return NT_STATUS_NO_SUCH_ALIAS;
                }
+
+               /* make sure there is a mapping */
+               
+               if ( !sid_to_gid( &sid, &gid ) ) {
+                       return NT_STATUS_NO_SUCH_ALIAS;
+               }
+
        }
 
        /* associate the alias SID with the new handle. */
@@ -2900,20 +2970,20 @@ NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_A
  set_user_info_7
  ********************************************************************/
 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
-                               const SAM_USER_INFO_7 *id7, SAM_ACCOUNT *pwd)
+                               const SAM_USER_INFO_7 *id7, struct samu *pwd)
 {
        fstring new_name;
        NTSTATUS rc;
 
        if (id7 == NULL) {
                DEBUG(5, ("set_user_info_7: NULL id7\n"));
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return NT_STATUS_ACCESS_DENIED;
        }
 
        if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
                DEBUG(5, ("set_user_info_7: failed to get new username\n"));
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -2933,7 +3003,7 @@ static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
 
        rc = pdb_rename_sam_account(pwd, new_name);
 
-       pdb_free_sam(&pwd);
+       TALLOC_FREE(pwd);
        return rc;
 }
 
@@ -2941,26 +3011,26 @@ static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
  set_user_info_16
  ********************************************************************/
 
-static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, SAM_ACCOUNT *pwd)
+static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, struct samu *pwd)
 {
        if (id16 == NULL) {
                DEBUG(5, ("set_user_info_16: NULL id16\n"));
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
        
        /* FIX ME: check if the value is really changed --metze */
        if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
 
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
 
-       pdb_free_sam(&pwd);
+       TALLOC_FREE(pwd);
 
        return True;
 }
@@ -2969,77 +3039,42 @@ static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, SAM_ACCOUNT *pwd)
  set_user_info_18
  ********************************************************************/
 
-static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, SAM_ACCOUNT *pwd)
+static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, struct samu *pwd)
 {
 
        if (id18 == NULL) {
                DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
  
        if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
        if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd, PDB_CHANGED)) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
        if (!pdb_set_pass_changed_now (pwd)) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False; 
        }
  
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
 
-       pdb_free_sam(&pwd);
-       return True;
-}
-
-/*******************************************************************
- The GROUPSID field in the SAM_ACCOUNT changed. Try to tell unix.
- ********************************************************************/
-static BOOL set_unix_primary_group(SAM_ACCOUNT *sampass)
-{
-       struct group *grp;
-       gid_t gid;
-
-       if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
-               DEBUG(2,("Could not get gid for primary group of "
-                        "user %s\n", pdb_get_username(sampass)));
-               return False;
-       }
-
-       grp = getgrgid(gid);
-
-       if (grp == NULL) {
-               DEBUG(2,("Could not find primary group %lu for "
-                        "user %s\n", (unsigned long)gid, 
-                        pdb_get_username(sampass)));
-               return False;
-       }
-
-       if (smb_set_primary_group(grp->gr_name,
-                                 pdb_get_username(sampass)) != 0) {
-               DEBUG(2,("Could not set primary group for user %s to "
-                        "%s\n",
-                        pdb_get_username(sampass), grp->gr_name));
-               return False;
-       }
-
+       TALLOC_FREE(pwd);
        return True;
 }
-       
 
 /*******************************************************************
  set_user_info_20
  ********************************************************************/
 
-static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
+static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, struct samu *pwd)
 {
        if (id20 == NULL) {
                DEBUG(5, ("set_user_info_20: NULL id20\n"));
@@ -3050,11 +3085,11 @@ static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
 
        /* write the change out */
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
 
-       pdb_free_sam(&pwd);
+       TALLOC_FREE(pwd);
 
        return True;
 }
@@ -3062,14 +3097,52 @@ static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
  set_user_info_21
  ********************************************************************/
 
-static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, SAM_ACCOUNT *pwd)
+static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
+                                struct samu *pwd)
 {
+       fstring new_name;
+       NTSTATUS status;
+       
        if (id21 == NULL) {
                DEBUG(5, ("set_user_info_21: NULL id21\n"));
-               return False;
+               return NT_STATUS_INVALID_PARAMETER;
        }
+
+       /* we need to separately check for an account rename first */
+       
+       if (rpcstr_pull(new_name, id21->uni_user_name.buffer, 
+               sizeof(new_name), id21->uni_user_name.uni_str_len*2, 0) 
+               && (!strequal(new_name, pdb_get_username(pwd)))) 
+       {
+
+               /* check to see if the new username already exists.  Note: we can't
+                  reliably lock all backends, so there is potentially the 
+                  possibility that a user can be created in between this check and
+                  the rename.  The rename should fail, but may not get the
+                  exact same failure status code.  I think this is small enough
+                  of a window for this type of operation and the results are
+                  simply that the rename fails with a slightly different status
+                  code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
+
+               status = can_create(mem_ctx, new_name);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = pdb_rename_sam_account(pwd, new_name);
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("set_user_info_21: failed to rename account: %s\n", 
+                               nt_errstr(status)));
+                       TALLOC_FREE(pwd);
+                       return status;
+               }
+
+               /* set the new username so that later 
+                  functions can work on the new account */
+               pdb_set_username(pwd, new_name, PDB_SET);
+       }
+
        copy_id21_to_sam_passwd(pwd, id21);
  
        /*
@@ -3079,33 +3152,43 @@ static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, SAM_ACCOUNT *pwd)
         * id21.  I don't know if they need to be set.    --jerry
         */
  
-       if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
-               set_unix_primary_group(pwd);
+       if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
+               status = pdb_set_unix_primary_group(mem_ctx, pwd);
+               if ( !NT_STATUS_IS_OK(status) ) {
+                       return status;
+               }
+       }
+       
+       /* Don't worry about writing out the user account since the
+          primary group SID is generated solely from the user's Unix 
+          primary group. */
 
        /* write the change out */
-       if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               pdb_free_sam(&pwd);
-               return False;
+       if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
+               TALLOC_FREE(pwd);
+               return status;
        }
 
-       pdb_free_sam(&pwd);
+       TALLOC_FREE(pwd);
 
-       return True;
+       return NT_STATUS_OK;
 }
 
 /*******************************************************************
  set_user_info_23
  ********************************************************************/
 
-static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
+static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx, SAM_USER_INFO_23 *id23,
+                                struct samu *pwd)
 {
        pstring plaintext_buf;
        uint32 len;
        uint16 acct_ctrl;
+       NTSTATUS status;
  
        if (id23 == NULL) {
                DEBUG(5, ("set_user_info_23: NULL id23\n"));
-               return False;
+               return NT_STATUS_INVALID_PARAMETER;
        }
  
        DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
@@ -3114,13 +3197,13 @@ static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
        acct_ctrl = pdb_get_acct_ctrl(pwd);
 
        if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
-               pdb_free_sam(&pwd);
-               return False;
+               TALLOC_FREE(pwd);
+               return NT_STATUS_INVALID_PARAMETER;
        }
   
        if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
-               pdb_free_sam(&pwd);
-               return False;
+               TALLOC_FREE(pwd);
+               return NT_STATUS_ACCESS_DENIED;
        }
  
        copy_id23_to_sam_passwd(pwd, id23);
@@ -3129,46 +3212,56 @@ static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
        if (    ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
                ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
                ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
-               DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
+               DEBUG(5, ("Changing trust account.  Not updating /etc/passwd\n"));
        } else  {
                /* update the UNIX password */
                if (lp_unix_password_sync() ) {
-                       struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
-                       if (!passwd) {
+                       struct passwd *passwd;
+                       if (pdb_get_username(pwd) == NULL) {
+                               DEBUG(1, ("chgpasswd: User without name???\n"));
+                               TALLOC_FREE(pwd);
+                               return NT_STATUS_ACCESS_DENIED;
+                       }
+
+                       if ((passwd = Get_Pwnam(pdb_get_username(pwd))) == NULL) {
                                DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
                        }
                        
                        if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
-                               pdb_free_sam(&pwd);
-                               return False;
+                               TALLOC_FREE(pwd);
+                               return NT_STATUS_ACCESS_DENIED;
                        }
                }
        }
  
        ZERO_STRUCT(plaintext_buf);
  
-       if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
-               set_unix_primary_group(pwd);
+       if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
+           (!NT_STATUS_IS_OK(status =  pdb_set_unix_primary_group(mem_ctx,
+                                                                  pwd)))) {
+               TALLOC_FREE(pwd);
+               return status;
+       }
 
-       if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               pdb_free_sam(&pwd);
-               return False;
+       if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
+               TALLOC_FREE(pwd);
+               return status;
        }
  
-       pdb_free_sam(&pwd);
+       TALLOC_FREE(pwd);
 
-       return True;
+       return NT_STATUS_OK;
 }
 
 /*******************************************************************
  set_user_info_pw
  ********************************************************************/
 
-static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
+static BOOL set_user_info_pw(uint8 *pass, struct samu *pwd)
 {
        uint32 len;
        pstring plaintext_buf;
-       uint16 acct_ctrl;
+       uint32 acct_ctrl;
  
        DEBUG(5, ("Attempting administrator password change for user %s\n",
                  pdb_get_username(pwd)));
@@ -3178,12 +3271,12 @@ static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
        ZERO_STRUCT(plaintext_buf);
  
        if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
 
        if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
  
@@ -3195,13 +3288,20 @@ static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
        } else {
                /* update the UNIX password */
                if (lp_unix_password_sync()) {
-                       struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
-                       if (!passwd) {
+                       struct passwd *passwd;
+
+                       if (pdb_get_username(pwd) == NULL) {
+                               DEBUG(1, ("chgpasswd: User without name???\n"));
+                               TALLOC_FREE(pwd);
+                               return False;
+                       }
+
+                       if ((passwd = Get_Pwnam(pdb_get_username(pwd))) == NULL) {
                                DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
                        }
                        
                        if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
-                               pdb_free_sam(&pwd);
+                               TALLOC_FREE(pwd);
                                return False;
                        }
                }
@@ -3213,22 +3313,65 @@ static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
  
        /* update the SAMBA password */
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return False;
        }
 
-       pdb_free_sam(&pwd);
+       TALLOC_FREE(pwd);
 
        return True;
 }
 
+/*******************************************************************
+ set_user_info_25
+ ********************************************************************/
+
+static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx, SAM_USER_INFO_25 *id25,
+                                struct samu *pwd)
+{
+       NTSTATUS status;
+       
+       if (id25 == NULL) {
+               DEBUG(5, ("set_user_info_25: NULL id25\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       copy_id25_to_sam_passwd(pwd, id25);
+       /* write the change out */
+       if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
+               TALLOC_FREE(pwd);
+               return status;
+       }
+
+       /*
+        * We need to "pdb_update_sam_account" before the unix primary group
+        * is set, because the idealx scripts would also change the
+        * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
+        * the delete explicit / add explicit, which would then fail to find
+        * the previous primaryGroupSid value.
+        */
+
+       if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
+               status = pdb_set_unix_primary_group(mem_ctx, pwd);
+               if ( !NT_STATUS_IS_OK(status) ) {
+                       return status;
+               }
+       }
+       
+       /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
+        * hereafter! */
+
+       return NT_STATUS_OK;
+}
+
 /*******************************************************************
  samr_reply_set_userinfo
  ********************************************************************/
 
 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
 {
-       SAM_ACCOUNT *pwd = NULL;
+       struct samu *pwd = NULL;
        DOM_SID sid;
        POLICY_HND *pol = &q_u->pol;
        uint16 switch_value = q_u->switch_value;
@@ -3248,10 +3391,25 @@ NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SE
        if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* observed when joining an XP client to a Samba domain */
+       /* This is tricky.  A WinXP domain join sets 
+         (SA_RIGHT_USER_SET_PASSWORD|SA_RIGHT_USER_SET_ATTRIBUTES|SA_RIGHT_USER_ACCT_FLAGS_EXPIRY)
+         The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser().  But the 
+         standard Win32 API calls just ask for SA_RIGHT_USER_SET_PASSWORD in the SamrOpenUser().  
+         This should be enough for levels 18, 24, 25,& 26.  Info level 23 can set more so 
+         we'll use the set from the WinXP join as the basis. */
+       
+       switch (switch_value) {
+       case 18:
+       case 24:
+       case 25:
+       case 26:
+               acc_required = SA_RIGHT_USER_SET_PASSWORD;
+               break;
+       default:
+               acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
+               break;
+       }
        
-       acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;     
-
        if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
                return r_u->status;
        }
@@ -3263,14 +3421,16 @@ NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SE
                return NT_STATUS_INVALID_INFO_CLASS;
        }
        
-       pdb_init_sam(&pwd);     
+       if ( !(pwd = samu_new( NULL )) ) {
+               return NT_STATUS_NO_MEMORY;
+       }
        
        become_root();
        ret = pdb_getsampwsid(pwd, &sid);
        unbecome_root();
        
        if ( !ret ) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return NT_STATUS_NO_SUCH_USER;
        }
        
@@ -3288,7 +3448,8 @@ NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SE
        }
        
        DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
-               p->pipe_user_name, has_enough_rights ? "" : " not"));
+                 uidtoname(p->pipe_user.ut.uid),
+                 has_enough_rights ? "" : " not"));
 
        /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
        
@@ -3316,27 +3477,32 @@ NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SE
                        break;
 
                case 25:
-#if 0
-                       /*
-                        * Currently we don't really know how to unmarshall
-                        * the level 25 struct, and the password encryption
-                        * is different. This is a placeholder for when we
-                        * do understand it. In the meantime just return INVALID
-                        * info level and W2K SP2 drops down to level 23... JRA.
-                        */
-
                        if (!p->session_key.length) {
                                r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
                        }
-                       SamOEMhashBlob(ctr->info.id25->pass, 532, &p->session_key);
+                       encode_or_decode_arc4_passwd_buffer(ctr->info.id25->pass, &p->session_key);
 
                        dump_data(100, (char *)ctr->info.id25->pass, 532);
 
-                       if (!set_user_info_pw(ctr->info.id25->pass, &sid))
+                       r_u->status = set_user_info_25(p->mem_ctx,
+                                                      ctr->info.id25, pwd);
+                       if (!NT_STATUS_IS_OK(r_u->status)) {
+                               goto done;
+                       }
+                       if (!set_user_info_pw(ctr->info.id25->pass, pwd))
                                r_u->status = NT_STATUS_ACCESS_DENIED;
                        break;
-#endif
-                       r_u->status = NT_STATUS_INVALID_INFO_CLASS;
+
+               case 26:
+                       if (!p->session_key.length) {
+                               r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
+                       }
+                       encode_or_decode_arc4_passwd_buffer(ctr->info.id26->pass, &p->session_key);
+
+                       dump_data(100, (char *)ctr->info.id26->pass, 516);
+
+                       if (!set_user_info_pw(ctr->info.id26->pass, pwd))
+                               r_u->status = NT_STATUS_ACCESS_DENIED;
                        break;
 
                case 23:
@@ -3347,14 +3513,15 @@ NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SE
 
                        dump_data(100, (char *)ctr->info.id23->pass, 516);
 
-                       if (!set_user_info_23(ctr->info.id23, pwd))
-                               r_u->status = NT_STATUS_ACCESS_DENIED;
+                       r_u->status = set_user_info_23(p->mem_ctx,
+                                                      ctr->info.id23, pwd);
                        break;
 
                default:
                        r_u->status = NT_STATUS_INVALID_INFO_CLASS;
        }
 
+ done:
        
        if ( has_enough_rights )                                
                unbecome_root();
@@ -3374,7 +3541,7 @@ NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SE
 
 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
 {
-       SAM_ACCOUNT *pwd = NULL;
+       struct samu *pwd = NULL;
        DOM_SID sid;
        SAM_USERINFO_CTR *ctr = q_u->ctr;
        POLICY_HND *pol = &q_u->pol;
@@ -3394,9 +3561,14 @@ NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_
        if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* observed when joining XP client to Samba domain */
                
+#if 0  /* this really should be applied on a per info level basis   --jerry */
+
+       /* observed when joining XP client to Samba domain */
        acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
+#else
+       acc_required = SA_RIGHT_USER_SET_ATTRIBUTES;
+#endif
        
        if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
                return r_u->status;
@@ -3411,14 +3583,16 @@ NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_
 
        switch_value=ctr->switch_value;
 
-       pdb_init_sam(&pwd);     
-       
+       if ( !(pwd = samu_new( NULL )) ) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        become_root();
        ret = pdb_getsampwsid(pwd, &sid);
        unbecome_root();
        
        if ( !ret ) {
-               pdb_free_sam(&pwd);
+               TALLOC_FREE(pwd);
                return NT_STATUS_NO_SUCH_USER;
        }
        
@@ -3432,8 +3606,9 @@ NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_
                        has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
        }
        
-       DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
-               p->pipe_user_name, has_enough_rights ? "" : " not"));
+       DEBUG(5, ("_samr_set_userinfo2: %s does%s possess sufficient rights\n",
+                 uidtoname(p->pipe_user.ut.uid),
+                 has_enough_rights ? "" : " not"));
 
        /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
        
@@ -3461,8 +3636,30 @@ NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_
                                r_u->status = NT_STATUS_ACCESS_DENIED;
                        break;
                case 21:
-                       if (!set_user_info_21(ctr->info.id21, pwd))
-                               return NT_STATUS_ACCESS_DENIED;
+                       r_u->status = set_user_info_21(p->mem_ctx,
+                                                      ctr->info.id21, pwd);
+                       break;
+               case 23:
+                       if (!p->session_key.length) {
+                               r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
+                       }
+                       SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
+
+                       dump_data(100, (char *)ctr->info.id23->pass, 516);
+
+                       r_u->status = set_user_info_23(p->mem_ctx,
+                                                      ctr->info.id23, pwd);
+                       break;
+               case 26:
+                       if (!p->session_key.length) {
+                               r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
+                       }
+                       encode_or_decode_arc4_passwd_buffer(ctr->info.id26->pass, &p->session_key);
+
+                       dump_data(100, (char *)ctr->info.id26->pass, 516);
+
+                       if (!set_user_info_pw(ctr->info.id26->pass, pwd))
+                               r_u->status = NT_STATUS_ACCESS_DENIED;
                        break;
                default:
                        r_u->status = NT_STATUS_INVALID_INFO_CLASS;
@@ -3571,7 +3768,9 @@ NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_
 
        DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
 
+       become_root();
        status = pdb_enum_aliasmem(&alias_sid, &sids, &num_sids);
+       unbecome_root();
 
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -3594,70 +3793,6 @@ NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_
        return NT_STATUS_OK;
 }
 
-static void add_uid_to_array_unique(uid_t uid, uid_t **uids, int *num)
-{
-       int i;
-
-       for (i=0; i<*num; i++) {
-               if ((*uids)[i] == uid)
-                       return;
-       }
-       
-       *uids = SMB_REALLOC_ARRAY(*uids, uid_t, *num+1);
-
-       if (*uids == NULL)
-               return;
-
-       (*uids)[*num] = uid;
-       *num += 1;
-}
-
-
-static BOOL get_memberuids(gid_t gid, uid_t **uids, int *num)
-{
-       struct group *grp;
-       char **gr;
-       struct sys_pwent *userlist, *user;
-       *uids = NULL;
-       *num = 0;
-
-       /* We only look at our own sam, so don't care about imported stuff */
-
-       winbind_off();
-
-       if ((grp = getgrgid(gid)) == NULL) {
-               winbind_on();
-               return False;
-       }
-
-       /* Primary group members */
-
-       userlist = getpwent_list();
-
-       for (user = userlist; user != NULL; user = user->next) {
-               if (user->pw_gid != gid)
-                       continue;
-               add_uid_to_array_unique(user->pw_uid, uids, num);
-       }
-
-       pwent_free(userlist);
-
-       /* Secondary group members */
-
-       for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
-               struct passwd *pw = getpwnam(*gr);
-
-               if (pw == NULL)
-                       continue;
-               add_uid_to_array_unique(pw->pw_uid, uids, num);
-       }
-
-       winbind_on();
-
-       return True;
-}      
-
 /*********************************************************************
  _samr_query_groupmem
 *********************************************************************/
@@ -3703,7 +3838,7 @@ NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_
 
        attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
        
-       if ((num_members!=0) && (rid==NULL))
+       if ((num_members!=0) && (attr==NULL))
                return NT_STATUS_NO_MEMORY;
        
        for (i=0; i<num_members; i++)
@@ -3812,16 +3947,7 @@ NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DE
 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
 {
        DOM_SID group_sid;
-       DOM_SID user_sid;
-       fstring group_sid_str;
-       uid_t uid;
-       struct passwd *pwd;
-       struct group *grp;
-       fstring grp_name;
-       GROUP_MAP map;
-       NTSTATUS ret;
-       SAM_ACCOUNT *sam_user=NULL;
-       BOOL check;
+       uint32 group_rid;
        uint32 acc_granted;
        SE_PRIV se_rights;
        BOOL can_add_accounts;
@@ -3835,53 +3961,11 @@ NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_AD
                return r_u->status;
        }
 
-       sid_to_string(group_sid_str, &group_sid);
-       DEBUG(10, ("sid is %s\n", group_sid_str));
-
-       if (sid_compare(&group_sid, get_global_sam_sid())<=0)
-               return NT_STATUS_NO_SUCH_GROUP;
-
-       DEBUG(10, ("lookup on Domain SID\n"));
-
-       if(!get_domain_group_from_sid(group_sid, &map))
-               return NT_STATUS_NO_SUCH_GROUP;
+       DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
 
-       sid_copy(&user_sid, get_global_sam_sid());
-       sid_append_rid(&user_sid, q_u->rid);
-
-       ret = pdb_init_sam(&sam_user);
-       if (!NT_STATUS_IS_OK(ret))
-               return ret;
-       
-       check = pdb_getsampwsid(sam_user, &user_sid);
-       
-       if (check != True) {
-               pdb_free_sam(&sam_user);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       /* check a real user exist before we run the script to add a user to a group */
-       if (!sid_to_uid(pdb_get_user_sid(sam_user), &uid)) {
-               pdb_free_sam(&sam_user);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       pdb_free_sam(&sam_user);
-
-       if ((pwd=getpwuid_alloc(p->mem_ctx, uid)) == NULL) {
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       if ((grp=getgrgid(map.gid)) == NULL) {
-               return NT_STATUS_NO_SUCH_GROUP;
-       }
-
-       /* we need to copy the name otherwise it's overloaded in user_in_unix_group_list */
-       fstrcpy(grp_name, grp->gr_name);
-
-       /* if the user is already in the group */
-       if(user_in_unix_group(pwd->pw_name, grp_name)) {
-               return NT_STATUS_MEMBER_IN_GROUP;
+       if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
+                               &group_rid)) {
+               return NT_STATUS_INVALID_HANDLE;
        }
 
        se_priv_copy( &se_rights, &se_add_users );
@@ -3891,28 +3975,17 @@ NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_AD
        
        if ( can_add_accounts )
                become_root();
-               
-       /* 
-        * ok, the group exist, the user exist, the user is not in the group,
-        *
-        * we can (finally) add it to the group !
-        */
-
-       smb_add_user_group(grp_name, pwd->pw_name);
 
+       r_u->status = pdb_add_groupmem(p->mem_ctx, group_rid, q_u->rid);
+               
        if ( can_add_accounts )
                unbecome_root();
                
        /******** END SeAddUsers BLOCK *********/
        
-       /* check if the user has been added then ... */
-       if(!user_in_unix_group(pwd->pw_name, grp_name)) {
-               return NT_STATUS_MEMBER_NOT_IN_GROUP;           /* don't know what to reply else */
-       }
-
        force_flush_samr_cache(disp_info);
 
-       return NT_STATUS_OK;
+       return r_u->status;
 }
 
 /*********************************************************************
@@ -3922,11 +3995,7 @@ NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_AD
 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
 {
        DOM_SID group_sid;
-       DOM_SID user_sid;
-       SAM_ACCOUNT *sam_pass=NULL;
-       GROUP_MAP map;
-       fstring grp_name;
-       struct group *grp;
+       uint32 group_rid;
        uint32 acc_granted;
        SE_PRIV se_rights;
        BOOL can_add_accounts;
@@ -3945,36 +4014,11 @@ NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DE
        if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
                return r_u->status;
        }
-               
-       if (!sid_check_is_in_our_domain(&group_sid))
-               return NT_STATUS_NO_SUCH_GROUP;
-
-       sid_copy(&user_sid, get_global_sam_sid());
-       sid_append_rid(&user_sid, q_u->rid);
-
-       if (!get_domain_group_from_sid(group_sid, &map))
-               return NT_STATUS_NO_SUCH_GROUP;
-
-       if ((grp=getgrgid(map.gid)) == NULL)
-               return NT_STATUS_NO_SUCH_GROUP;
-
-       /* we need to copy the name otherwise it's overloaded in user_in_group_list */
-       fstrcpy(grp_name, grp->gr_name);
-
-       /* check if the user exists before trying to remove it from the group */
-       pdb_init_sam(&sam_pass);
-       if (!pdb_getsampwsid(sam_pass, &user_sid)) {
-               DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
-               pdb_free_sam(&sam_pass);
-               return NT_STATUS_NO_SUCH_USER;
-       }
 
-       /* if the user is not in the group */
-       if (!user_in_unix_group(pdb_get_username(sam_pass), grp_name)) {
-               pdb_free_sam(&sam_pass);
-               return NT_STATUS_MEMBER_NOT_IN_GROUP;
+       if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
+                               &group_rid)) {
+               return NT_STATUS_INVALID_HANDLE;
        }
-       
 
        se_priv_copy( &se_rights, &se_add_users );
        can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
@@ -3984,45 +4028,16 @@ NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DE
        if ( can_add_accounts )
                become_root();
                
-       smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
+       r_u->status = pdb_del_groupmem(p->mem_ctx, group_rid, q_u->rid);
 
        if ( can_add_accounts )
                unbecome_root();
                
        /******** END SeAddUsers BLOCK *********/
        
-       /* check if the user has been removed then ... */
-       if (user_in_unix_group(pdb_get_username(sam_pass), grp_name)) {
-               pdb_free_sam(&sam_pass);
-               return NT_STATUS_ACCESS_DENIED;         /* don't know what to reply else */
-       }
-       
-       pdb_free_sam(&sam_pass);
-
        force_flush_samr_cache(disp_info);
 
-       return NT_STATUS_OK;
-
-}
-
-/****************************************************************************
- Delete a UNIX user on demand.
-****************************************************************************/
-
-static int smb_delete_user(const char *unix_user)
-{
-       pstring del_script;
-       int ret;
-
-       pstrcpy(del_script, lp_deluser_script());
-       if (! *del_script)
-               return -1;
-       all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
-       ret = smbrun(del_script,NULL);
-       flush_pwnam_cache();
-       DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
-
-       return ret;
+       return r_u->status;
 }
 
 /*********************************************************************
@@ -4032,11 +4047,12 @@ static int smb_delete_user(const char *unix_user)
 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
 {
        DOM_SID user_sid;
-       SAM_ACCOUNT *sam_pass=NULL;
+       struct samu *sam_pass=NULL;
        uint32 acc_granted;
        BOOL can_add_accounts;
-       BOOL ret;
+       uint32 acb_info;
        DISP_INFO *disp_info = NULL;
+       BOOL ret;
 
        DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
 
@@ -4052,48 +4068,52 @@ NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAM
                return NT_STATUS_CANNOT_DELETE;
 
        /* check if the user exists before trying to delete */
-       pdb_init_sam(&sam_pass);
-       if(!pdb_getsampwsid(sam_pass, &user_sid)) {
+       if ( !(sam_pass = samu_new( NULL )) ) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       become_root();
+       ret = pdb_getsampwsid(sam_pass, &user_sid);
+       unbecome_root();
+
+       if( !ret ) {
                DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n", 
                        sid_string_static(&user_sid)));
-               pdb_free_sam(&sam_pass);
+               TALLOC_FREE(sam_pass);
                return NT_STATUS_NO_SUCH_USER;
        }
        
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
+       acb_info = pdb_get_acct_ctrl(sam_pass);
+
+       /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
+       if ( acb_info & ACB_WSTRUST ) {
+               can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account );
+       } else {
+               can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
+       } 
 
        /******** BEGIN SeAddUsers BLOCK *********/
        
        if ( can_add_accounts )
                become_root();
 
-       /* First delete the samba side....
-          code is order to prevent unnecessary returns out of the admin 
-          block of code */
-          
-       if ( (ret = pdb_delete_sam_account(sam_pass)) == True ) {
-               /*
-                * Now delete the unix side ....
-                * note: we don't check if the delete really happened
-                * as the script is not necessary present
-                * and maybe the sysadmin doesn't want to delete the unix side
-                */
-               smb_delete_user( pdb_get_username(sam_pass) );
-       }
-       
+       r_u->status = pdb_delete_user(p->mem_ctx, sam_pass);
+
        if ( can_add_accounts )
                unbecome_root();
                
        /******** END SeAddUsers BLOCK *********/
                
-       if ( !ret ) {
-               DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
-               pdb_free_sam(&sam_pass);
-               return NT_STATUS_CANNOT_DELETE;
+       if ( !NT_STATUS_IS_OK(r_u->status) ) {
+               DEBUG(5,("_samr_delete_dom_user: Failed to delete entry for "
+                        "user %s: %s.\n", pdb_get_username(sam_pass),
+                        nt_errstr(r_u->status)));
+               TALLOC_FREE(sam_pass);
+               return r_u->status;
        }
 
 
-       pdb_free_sam(&sam_pass);
+       TALLOC_FREE(sam_pass);
 
        if (!close_policy_hnd(p, &q_u->user_pol))
                return NT_STATUS_OBJECT_NAME_INVALID;
@@ -4110,16 +4130,10 @@ NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAM
 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
 {
        DOM_SID group_sid;
-       DOM_SID dom_sid;
        uint32 group_rid;
-       fstring group_sid_str;
-       gid_t gid;
-       struct group *grp;
-       GROUP_MAP map;
        uint32 acc_granted;
        SE_PRIV se_rights;
        BOOL can_add_accounts;
-       BOOL ret;
        DISP_INFO *disp_info = NULL;
 
        DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
@@ -4131,27 +4145,13 @@ NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, S
        if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
                return r_u->status;
        }
-               
-       sid_copy(&dom_sid, &group_sid);
-       sid_to_string(group_sid_str, &dom_sid);
-       sid_split_rid(&dom_sid, &group_rid);
 
-       DEBUG(10, ("sid is %s\n", group_sid_str));
+       DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
 
-       /* we check if it's our SID before deleting */
-       if (!sid_equal(&dom_sid, get_global_sam_sid()))
-               return NT_STATUS_NO_SUCH_GROUP;
-
-       DEBUG(10, ("lookup on Domain SID\n"));
-
-       if(!get_domain_group_from_sid(group_sid, &map))
-               return NT_STATUS_NO_SUCH_GROUP;
-
-       gid=map.gid;
-
-       /* check if group really exists */
-       if ( (grp=getgrgid(gid)) == NULL) 
+       if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
+                               &group_rid)) {
                return NT_STATUS_NO_SUCH_GROUP;
+       }
 
        se_priv_copy( &se_rights, &se_add_users );
        can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
@@ -4161,26 +4161,21 @@ NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, S
        if ( can_add_accounts )
                become_root();
 
-       /* delete mapping first */
-       
-       if ( (ret = pdb_delete_group_mapping_entry(group_sid)) == True ) {
-               smb_delete_group( grp->gr_name );
-       }
+       r_u->status = pdb_delete_dom_group(p->mem_ctx, group_rid);
 
        if ( can_add_accounts )
                unbecome_root();
                
        /******** END SeAddUsers BLOCK *********/
        
-       if ( !ret ) {
-               DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping entry for group %s.\n", 
-                       group_sid_str));
-               return NT_STATUS_ACCESS_DENIED;
+       if ( !NT_STATUS_IS_OK(r_u->status) ) {
+               DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping "
+                        "entry for group %s: %s\n",
+                        sid_string_static(&group_sid),
+                        nt_errstr(r_u->status)));
+               return r_u->status;
        }
        
-       /* don't check that the unix group has been deleted.  Work like 
-          _samr_delet_dom_user() */
-
        if (!close_policy_hnd(p, &q_u->group_pol))
                return NT_STATUS_OBJECT_NAME_INVALID;
 
@@ -4208,12 +4203,22 @@ NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, S
        if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
                return NT_STATUS_INVALID_HANDLE;
        
+       /* copy the handle to the outgoing reply */
+
+       memcpy( &r_u->pol, &q_u->alias_pol, sizeof(r_u->pol) );
+
        if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
                return r_u->status;
        }
 
        DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
 
+       /* Don't let Windows delete builtin groups */
+
+       if ( sid_check_is_in_builtin( &alias_sid ) ) {
+               return NT_STATUS_SPECIAL_ACCOUNT;
+       }
+
        if (!sid_check_is_in_our_domain(&alias_sid))
                return NT_STATUS_NO_SUCH_ALIAS;
                
@@ -4254,15 +4259,11 @@ NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, S
 {
        DOM_SID dom_sid;
        DOM_SID info_sid;
-       fstring name;
-       fstring sid_string;
-       struct group *grp;
+       const char *name;
        struct samr_info *info;
        uint32 acc_granted;
-       gid_t gid;
        SE_PRIV se_rights;
        BOOL can_add_accounts;
-       NTSTATUS result;
        DISP_INFO *disp_info = NULL;
 
        /* Find the policy handle. Open a policy on it. */
@@ -4276,7 +4277,10 @@ NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, S
        if (!sid_equal(&dom_sid, get_global_sam_sid()))
                return NT_STATUS_ACCESS_DENIED;
 
-       unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
+       name = rpcstr_pull_unistr2_talloc(p->mem_ctx, &q_u->uni_acct_desc);
+       if (name == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        r_u->status = can_create(p->mem_ctx, name);
        if (!NT_STATUS_IS_OK(r_u->status)) {
@@ -4293,35 +4297,7 @@ NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, S
        
        /* check that we successfully create the UNIX group */
        
-       result = NT_STATUS_ACCESS_DENIED;
-       if ( (smb_create_group(name, &gid) == 0) && ((grp=getgrgid(gid)) != NULL) ) {
-       
-               /* so far, so good */
-               
-               result = NT_STATUS_OK;
-
-               if (pdb_rid_algorithm()) {
-                       r_u->rid = pdb_gid_to_group_rid( grp->gr_gid );
-               } else {
-                       if (!pdb_new_rid(&r_u->rid)) {
-                               result = NT_STATUS_ACCESS_DENIED;
-                       }
-               }
-
-               if (NT_STATUS_IS_OK(result)) {
-
-                       /* add the group to the mapping table */
-               
-                       sid_copy( &info_sid, get_global_sam_sid() );
-                       sid_append_rid( &info_sid, r_u->rid );
-                       sid_to_string( sid_string, &info_sid );
-               
-                       /* reset the error code if we fail to add the mapping entry */
-               
-                       if ( !add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL) )
-                               result = NT_STATUS_ACCESS_DENIED;
-               }
-       }
+       r_u->status = pdb_create_dom_group(p->mem_ctx, name, &r_u->rid);
 
        if ( can_add_accounts )
                unbecome_root();
@@ -4330,13 +4306,14 @@ NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, S
        
        /* check if we should bail out here */
        
-       if ( !NT_STATUS_IS_OK(result) )
-               return result;
+       if ( !NT_STATUS_IS_OK(r_u->status) )
+               return r_u->status;
+
+       sid_compose(&info_sid, get_global_sam_sid(), r_u->rid);
        
        if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
                return NT_STATUS_NO_MEMORY;
 
-
        /* they created it; let the user do what he wants with it */
 
        info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
@@ -4449,9 +4426,6 @@ NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAM
 {
        DOM_SID group_sid;
        GROUP_MAP map;
-       DOM_SID *sids=NULL;
-       uid_t *uids;
-       int num=0;
        GROUP_INFO_CTR *ctr;
        uint32 acc_granted;
        BOOL ret;
@@ -4474,13 +4448,28 @@ NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAM
                return NT_STATUS_NO_MEMORY;
 
        switch (q_u->switch_level) {
-               case 1:
+               case 1: {
+                       uint32 *members;
+                       size_t num_members;
+
                        ctr->switch_value1 = 1;
-                       if(!get_memberuids(map.gid, &uids, &num))
-                               return NT_STATUS_NO_SUCH_GROUP;
-                       SAFE_FREE(uids);
-                       init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num);
-                       SAFE_FREE(sids);
+
+                       become_root();
+                       r_u->status = pdb_enum_group_members(
+                               p->mem_ctx, &group_sid, &members, &num_members);
+                       unbecome_root();
+       
+                       if (!NT_STATUS_IS_OK(r_u->status)) {
+                               return r_u->status;
+                       }
+
+                       init_samr_group_info1(&ctr->group.info1, map.nt_name,
+                                     map.comment, num_members);
+                       break;
+               }
+               case 2:
+                       ctr->switch_value1 = 2;
+                       init_samr_group_info2(&ctr->group.info2, map.nt_name);
                        break;
                case 3:
                        ctr->switch_value1 = 3;
@@ -4490,6 +4479,28 @@ NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAM
                        ctr->switch_value1 = 4;
                        init_samr_group_info4(&ctr->group.info4, map.comment);
                        break;
+               case 5: {
+                       /*
+                       uint32 *members;
+                       size_t num_members;
+                       */
+
+                       ctr->switch_value1 = 5;
+
+                       /*
+                       become_root();
+                       r_u->status = pdb_enum_group_members(
+                               p->mem_ctx, &group_sid, &members, &num_members);
+                       unbecome_root();
+       
+                       if (!NT_STATUS_IS_OK(r_u->status)) {
+                               return r_u->status;
+                       }
+                       */
+                       init_samr_group_info5(&ctr->group.info5, map.nt_name,
+                                     map.comment, 0 /* num_members */); /* in w2k3 this is always 0 */
+                       break;
+               }
                default:
                        return NT_STATUS_INVALID_INFO_CLASS;
        }
@@ -4588,7 +4599,47 @@ NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_
                
        ctr=&q_u->ctr;
 
+       /* get the current group information */
+
+       become_root();
+       ret = pdb_get_aliasinfo( &group_sid, &info );
+       unbecome_root();
+
+       if ( !ret ) {
+               return NT_STATUS_NO_SUCH_ALIAS;
+       }
+
        switch (ctr->level) {
+               case 2:
+               {
+                       fstring group_name;
+                       NTSTATUS status;
+
+                       /* We currently do not support renaming groups in the
+                          the BUILTIN domain.  Refer to util_builtin.c to understand 
+                          why.  The eventually needs to be fixed to be like Windows
+                          where you can rename builtin groups, just not delete them */
+
+                       if ( sid_check_is_in_builtin( &group_sid ) ) {
+                               return NT_STATUS_SPECIAL_ACCOUNT;
+                       }
+
+                       /* There has to be a valid name */
+                       if ( !ctr->alias.info2.name.string ) 
+                               return NT_STATUS_INVALID_PARAMETER;
+
+                       unistr2_to_ascii( info.acct_name, ctr->alias.info2.name.string, 
+                               sizeof(info.acct_name)-1 );
+
+                       /* make sure the name doesn't already exist as a user 
+                          or local group */
+
+                       fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
+                       status = can_create( p->mem_ctx, group_name );
+                       if ( !NT_STATUS_IS_OK( status ) ) 
+                               return status;
+                       break;
+               }
                case 3:
                        if ( ctr->alias.info3.description.string ) {
                                unistr2_to_ascii( info.acct_desc, 
@@ -4783,144 +4834,31 @@ NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p,
  ********************************************************************/
 
 NTSTATUS _samr_query_domain_info2(pipes_struct *p,
-               SAMR_Q_QUERY_DOMAIN_INFO2 *q_u,
-               SAMR_R_QUERY_DOMAIN_INFO2 *r_u)
+                                 SAMR_Q_QUERY_DOMAIN_INFO2 *q_u,
+                                 SAMR_R_QUERY_DOMAIN_INFO2 *r_u)
 {
-       struct samr_info *info = NULL;
-       SAM_UNK_CTR *ctr;
-       uint32 min_pass_len,pass_hist,flag;
-       time_t u_expire, u_min_age;
-       NTTIME nt_expire, nt_min_age;
+       SAMR_Q_QUERY_DOMAIN_INFO q;
+       SAMR_R_QUERY_DOMAIN_INFO r;
 
-       time_t u_lock_duration, u_reset_time;
-       NTTIME nt_lock_duration, nt_reset_time;
-       uint32 lockout;
-       
-       time_t u_logout;
-       NTTIME nt_logout;
-
-       uint32 num_users=0, num_groups=0, num_aliases=0;
-
-       uint32 account_policy_temp;
-
-       time_t seq_num;
-       uint32 server_role;
-
-       if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL)
-               return NT_STATUS_NO_MEMORY;
-
-       ZERO_STRUCTP(ctr);
-
-       r_u->status = NT_STATUS_OK;
+       ZERO_STRUCT(q);
+       ZERO_STRUCT(r);
 
        DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
 
-       /* find the policy handle.  open a policy on it. */
-       if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info))
-               return NT_STATUS_INVALID_HANDLE;
+       q.domain_pol = q_u->domain_pol;
+       q.switch_value = q_u->switch_value;
 
-       switch (q_u->switch_value) {
-               case 0x01:
-                       pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
-                       min_pass_len = account_policy_temp;
-
-                       pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
-                       pass_hist = account_policy_temp;
-
-                       pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
-                       flag = account_policy_temp;
-
-                       pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
-                       u_expire = account_policy_temp;
-
-                       pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
-                       u_min_age = account_policy_temp;
-
-                       unix_to_nt_time_abs(&nt_expire, u_expire);
-                       unix_to_nt_time_abs(&nt_min_age, u_min_age);
-
-                       init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
-                                      flag, nt_expire, nt_min_age);
-                       break;
-               case 0x02:
-                       become_root();          
-                       num_users = count_sam_users(info->disp_info, ACB_NORMAL);
-                       num_groups = count_sam_groups(info->disp_info);
-                       num_aliases = count_sam_aliases(info->disp_info);
-                       unbecome_root();
-
-                       pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
-                       u_logout = account_policy_temp;
-
-                       unix_to_nt_time_abs(&nt_logout, u_logout);
-
-                       if (!pdb_get_seq_num(&seq_num))
-                               seq_num = time(NULL);
-
-                       server_role = ROLE_DOMAIN_PDC;
-                       if (lp_server_role() == ROLE_DOMAIN_BDC)
-                               server_role = ROLE_DOMAIN_BDC;
-
-                       init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
-                                      num_users, num_groups, num_aliases, nt_logout, server_role);
-                       break;
-               case 0x03:
-                       pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
-                       u_logout = account_policy_temp;
-
-                       unix_to_nt_time_abs(&nt_logout, u_logout);
-                       
-                       init_unk_info3(&ctr->info.inf3, nt_logout);
-                       break;
-               case 0x05:
-                       init_unk_info5(&ctr->info.inf5, global_myname());
-                       break;
-               case 0x06:
-                       init_unk_info6(&ctr->info.inf6);
-                       break;
-               case 0x07:
-                       server_role = ROLE_DOMAIN_PDC;
-                       if (lp_server_role() == ROLE_DOMAIN_BDC)
-                               server_role = ROLE_DOMAIN_BDC;
+       r_u->status = _samr_query_domain_info(p, &q, &r);
 
-                       init_unk_info7(&ctr->info.inf7, server_role);
-                       break;
-               case 0x08:
-                       if (!pdb_get_seq_num(&seq_num))
-                               seq_num = time(NULL);
-
-                       init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
-                       break;
-               case 0x0c:
-                       pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
-                       u_lock_duration = account_policy_temp;
-                       if (u_lock_duration != -1)
-                               u_lock_duration *= 60;
-
-                       pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
-                       u_reset_time = account_policy_temp * 60;
-
-                       pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
-                       lockout = account_policy_temp;
-       
-                       unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
-                       unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
-       
-                       init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
-                       break;
-               default:
-                       return NT_STATUS_INVALID_INFO_CLASS;
-       }
-
-       init_samr_r_samr_query_domain_info2(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
-
-       DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
+       r_u->ptr_0              = r.ptr_0;
+       r_u->switch_value       = r.switch_value;
+       r_u->ctr                = r.ctr;
 
        return r_u->status;
 }
 
 /*******************************************************************
- _samr_
+ _samr_set_dom_info
  ********************************************************************/
 
 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)