r13590: * replace all pdb_init_sam[_talloc]() calls with samu_new()
[kai/samba-autobuild/.git] / source / rpc_server / srv_samr_nt.c
index 75a72fa0280a50c58bfecab6e94b27fe363ee216..5c2950b4917e6ed86448c99b5cf984b3f1387545 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)
@@ -1505,7 +1516,8 @@ NTSTATUS _samr_chgpasswd_user3(pipes_struct *p, SAMR_Q_CHGPASSWD_USER3 *q_u, SAM
        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)) {
+       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;
@@ -1655,7 +1667,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;
@@ -1681,10 +1693,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 */
        
@@ -1715,7 +1726,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)
@@ -1735,21 +1746,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;
        }
@@ -1759,7 +1767,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;
 }
@@ -1769,14 +1777,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();
@@ -1793,7 +1798,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;
 }
@@ -1804,14 +1809,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();
@@ -1828,7 +1830,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;
 }
@@ -1841,9 +1843,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;
@@ -1857,31 +1858,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;
 }
@@ -1892,10 +1891,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);
@@ -1913,7 +1914,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;
 }
@@ -1925,13 +1926,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();
@@ -1950,7 +1950,7 @@ 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;
 }
@@ -2061,7 +2061,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;
@@ -2101,8 +2101,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();
@@ -2138,7 +2140,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;
        }
 
@@ -2409,18 +2411,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;
@@ -2449,33 +2446,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 );
@@ -2485,7 +2469,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 );
@@ -2493,7 +2476,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 );
@@ -2511,40 +2493,9 @@ NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
        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();
 
@@ -2556,8 +2507,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);
@@ -2573,7 +2524,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;
        }
 
@@ -2583,19 +2533,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;
 }
 
@@ -2996,20 +2941,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;
        }
 
@@ -3029,7 +2974,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;
 }
 
@@ -3037,26 +2982,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;
 }
@@ -3065,77 +3010,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);
+       TALLOC_FREE(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;
-       }
-
-       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"));
@@ -3146,11 +3056,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;
 }
@@ -3158,12 +3068,14 @@ 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)
 {
+       NTSTATUS status;
+
        if (id21 == NULL) {
                DEBUG(5, ("set_user_info_21: NULL id21\n"));
-               return False;
+               return NT_STATUS_INVALID_PARAMETER;
        }
  
        copy_id21_to_sam_passwd(pwd, id21);
@@ -3175,33 +3087,38 @@ 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) &&
+           !NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
+                                                                pwd))) {
+               return status;
+       }
 
        /* 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",
@@ -3210,13 +3127,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);
@@ -3235,32 +3152,36 @@ static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
                        }
                        
                        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;
@@ -3274,12 +3195,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;
        }
  
@@ -3297,7 +3218,7 @@ static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
                        }
                        
                        if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
-                               pdb_free_sam(&pwd);
+                               TALLOC_FREE(pwd);
                                return False;
                        }
                }
@@ -3309,11 +3230,11 @@ 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;
 }
@@ -3324,7 +3245,7 @@ static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
 
 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;
@@ -3359,14 +3280,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;
        }
        
@@ -3443,8 +3366,8 @@ 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:
@@ -3470,7 +3393,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;
@@ -3507,14 +3430,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;
        }
        
@@ -3557,8 +3482,8 @@ 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) {
@@ -3568,8 +3493,8 @@ NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_
 
                        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;
                case 26:
                        if (!p->session_key.length) {
@@ -3712,70 +3637,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
 *********************************************************************/
@@ -3930,16 +3791,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;
@@ -3953,53 +3805,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;
-
-       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;
-       }
+       DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
 
-       /* 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 );
@@ -4009,28 +3819,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;
 }
 
 /*********************************************************************
@@ -4040,11 +3839,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;
@@ -4063,36 +3858,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 );
@@ -4102,45 +3872,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;
 }
 
 /*********************************************************************
@@ -4150,10 +3891,9 @@ 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;
        DISP_INFO *disp_info = NULL;
 
        DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
@@ -4170,11 +3910,14 @@ 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 ( !(sam_pass = samu_new( NULL )) ) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        if(!pdb_getsampwsid(sam_pass, &user_sid)) {
                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;
        }
        
@@ -4185,33 +3928,23 @@ NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAM
        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;
@@ -4228,16 +3961,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__));
@@ -4249,27 +3976,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));
-
-       /* 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;
+       DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
 
-       /* 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 );
@@ -4279,26 +3992,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;
 
@@ -4372,15 +4080,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. */
@@ -4394,7 +4098,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)) {
@@ -4411,35 +4118,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();
@@ -4448,13 +4127,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;
@@ -4567,9 +4247,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;
@@ -4592,14 +4269,25 @@ 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 3:
                        ctr->switch_value1 = 3;
                        init_samr_group_info3(&ctr->group.info3);