Fix bug #6089 - Winbind samr_OpenDomain not possible with Samba 3.2.6+
[samba.git] / source / rpc_server / srv_samr_nt.c
index 9fc1d651070db429a4c585bf9bbac60546b6f7e6..ec62662de22d101a874eb2f7bfa51651d3cb7da2 100644 (file)
@@ -454,7 +454,7 @@ static void free_samr_info(void *ptr)
 
 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
                                                 struct timed_event *te,
-                                                const struct timeval *now,
+                                                struct timeval now,
                                                 void *private_data)
 {
        DISP_INFO *disp_info = (DISP_INFO *)private_data;
@@ -483,7 +483,6 @@ static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromno
        disp_info->cache_timeout_event = event_add_timed(
                smbd_event_context(), NULL,
                timeval_current_ofs(secs_fromnow, 0),
-               "disp_info_cache_idle_timeout_handler",
                disp_info_cache_idle_timeout_handler, (void *)disp_info);
 }
 
@@ -621,13 +620,6 @@ NTSTATUS _samr_OpenDomain(pipes_struct *p,
        if ( !find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info) )
                return NT_STATUS_INVALID_HANDLE;
 
-       status = access_check_samr_function(info->acc_granted,
-                                           SAMR_ACCESS_OPEN_DOMAIN,
-                                           "_samr_OpenDomain" );
-
-       if ( !NT_STATUS_IS_OK(status) )
-               return status;
-
        /*check if access can be granted as requested by client. */
        map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
 
@@ -1009,6 +1001,7 @@ NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
        if (!samr_array) {
                return NT_STATUS_NO_MEMORY;
        }
+       *r->out.sam = samr_array;
 
        become_root();
 
@@ -1068,7 +1061,6 @@ NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
        samr_array->entries = samr_entries;
 
        *r->out.resume_handle = *r->in.resume_handle + num_account;
-       *r->out.sam = samr_array;
        *r->out.num_entries = num_account;
 
        DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
@@ -1178,9 +1170,7 @@ NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
 
        *r->out.sam = samr_array;
        *r->out.num_entries = num_groups;
-       /* this was missing, IMHO:
        *r->out.resume_handle = num_groups + *r->in.resume_handle;
-       */
 
        DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
 
@@ -1781,66 +1771,6 @@ NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
        return NT_STATUS_OK;
 }
 
-#if 0
-/*******************************************************************
- samr_reply_lookup_ids
- ********************************************************************/
-
- uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
-{
-    uint32 rid[MAX_SAM_ENTRIES];
-    int num_rids = q_u->num_sids1;
-
-    r_u->status = NT_STATUS_OK;
-
-    DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
-
-    if (num_rids > MAX_SAM_ENTRIES) {
-        num_rids = MAX_SAM_ENTRIES;
-        DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
-    }
-
-#if 0
-    int i;
-    SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
-
-    for (i = 0; i < num_rids && status == 0; i++)
-    {
-        struct sam_passwd *sam_pass;
-        fstring user_name;
-
-
-        fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
-                                    q_u->uni_user_name[i].uni_str_len));
-
-        /* find the user account */
-        become_root();
-        sam_pass = get_smb21pwd_entry(user_name, 0);
-        unbecome_root();
-
-        if (sam_pass == NULL)
-        {
-            status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
-            rid[i] = 0;
-        }
-        else
-        {
-            rid[i] = sam_pass->user_rid;
-        }
-    }
-#endif
-
-    num_rids = 1;
-    rid[0] = BUILTIN_ALIAS_RID_USERS;
-
-    init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
-
-    DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
-
-    return r_u->status;
-}
-#endif
-
 /*******************************************************************
  _samr_LookupNames
  ********************************************************************/
@@ -2312,11 +2242,9 @@ static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
                                struct samr_UserInfo5 *r,
-                               DOM_SID *user_sid,
+                               struct samu *pw,
                                DOM_SID *domain_sid)
 {
-       struct samu *pw = NULL;
-       bool ret;
        const DOM_SID *sid_user, *sid_group;
        uint32_t rid, primary_gid;
        NTTIME last_logon, last_logoff, last_password_change,
@@ -2328,24 +2256,6 @@ static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
 
        ZERO_STRUCTP(r);
 
-       if (!(pw = samu_new(mem_ctx))) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(pw, user_sid);
-       unbecome_root();
-
-       if (ret == False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(pw);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       samr_clear_sam_passwd(pw);
-
-       DEBUG(3,("User:[%s]\n", pdb_get_username(pw)));
-
        sid_user = pdb_get_user_sid(pw);
 
        if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
@@ -2353,7 +2263,6 @@ static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
                          "the domain sid %s.  Failing operation.\n",
                          pdb_get_username(pw), sid_string_dbg(sid_user),
                          sid_string_dbg(domain_sid)));
-               TALLOC_FREE(pw);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
@@ -2366,7 +2275,6 @@ static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
                          "which conflicts with the domain sid %s.  Failing operation.\n",
                          pdb_get_username(pw), sid_string_dbg(sid_group),
                          sid_string_dbg(domain_sid)));
-               TALLOC_FREE(pw);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
@@ -2407,8 +2315,6 @@ static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
                             acct_expiry,
                             pdb_get_acct_ctrl(pw));
 
-       TALLOC_FREE(pw);
-
        return NT_STATUS_OK;
 }
 
@@ -2418,35 +2324,16 @@ static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
                                struct samr_UserInfo7 *r,
-                               DOM_SID *user_sid)
+                               struct samu *smbpass)
 {
-       struct samu *smbpass=NULL;
-       bool ret;
        const char *account_name = NULL;
 
        ZERO_STRUCTP(r);
 
-       if ( !(smbpass = samu_new( mem_ctx )) ) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(smbpass, user_sid);
-       unbecome_root();
-
-       if ( !ret ) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
        account_name = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
        if (!account_name) {
-               TALLOC_FREE(smbpass);
                return NT_STATUS_NO_MEMORY;
        }
-       TALLOC_FREE(smbpass);
-
-       DEBUG(3,("User:[%s]\n", account_name));
 
        init_samr_user_info7(r, account_name);
 
@@ -2459,33 +2346,12 @@ static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
                                struct samr_UserInfo9 *r,
-                               DOM_SID *user_sid)
+                               struct samu *smbpass)
 {
-       struct samu *smbpass=NULL;
-       bool ret;
-
        ZERO_STRUCTP(r);
 
-       if ( !(smbpass = samu_new( mem_ctx )) ) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(smbpass, user_sid);
-       unbecome_root();
-
-       if (ret==False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(smbpass);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
-
        init_samr_user_info9(r, pdb_get_group_rid(smbpass));
 
-       TALLOC_FREE(smbpass);
-
        return NT_STATUS_OK;
 }
 
@@ -2495,33 +2361,12 @@ static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
                                 struct samr_UserInfo16 *r,
-                                DOM_SID *user_sid)
+                                struct samu *smbpass)
 {
-       struct samu *smbpass=NULL;
-       bool ret;
-
        ZERO_STRUCTP(r);
 
-       if ( !(smbpass = samu_new( mem_ctx )) ) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(smbpass, user_sid);
-       unbecome_root();
-
-       if (ret==False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(smbpass);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
-
        init_samr_user_info16(r, pdb_get_acct_ctrl(smbpass));
 
-       TALLOC_FREE(smbpass);
-
        return NT_STATUS_OK;
 }
 
@@ -2572,8 +2417,10 @@ static NTSTATUS get_user_info_18(pipes_struct *p,
                return NT_STATUS_ACCOUNT_DISABLED;
        }
 
-       init_samr_user_info18(r, pdb_get_lanman_passwd(smbpass),
-                             pdb_get_nt_passwd(smbpass));
+       init_samr_user_info18(r,
+                             pdb_get_lanman_passwd(smbpass),
+                             pdb_get_nt_passwd(smbpass),
+                             0 /* FIXME */);
 
        TALLOC_FREE(smbpass);
 
@@ -2586,10 +2433,8 @@ static NTSTATUS get_user_info_18(pipes_struct *p,
 
 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
                                 struct samr_UserInfo20 *r,
-                                DOM_SID *user_sid)
+                                struct samu *sampass)
 {
-       struct samu *sampass=NULL;
-       bool ret;
        const char *munged_dial = NULL;
        DATA_BLOB blob;
        NTSTATUS status;
@@ -2597,24 +2442,8 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
 
        ZERO_STRUCTP(r);
 
-       if ( !(sampass = samu_new( mem_ctx )) ) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(sampass, user_sid);
-       unbecome_root();
-
-       if (ret == False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(sampass);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
        munged_dial = pdb_get_munged_dial(sampass);
 
-       samr_clear_sam_passwd(sampass);
-
        DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
                munged_dial, (int)strlen(munged_dial)));
 
@@ -2626,7 +2455,6 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
 
        status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
        data_blob_free(&blob);
-       TALLOC_FREE(sampass);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2643,12 +2471,10 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                                 struct samr_UserInfo21 *r,
-                                DOM_SID *user_sid,
+                                struct samu *pw,
                                 DOM_SID *domain_sid)
 {
        NTSTATUS status;
-       struct samu *pw = NULL;
-       bool ret;
        const DOM_SID *sid_user, *sid_group;
        uint32_t rid, primary_gid;
        NTTIME last_logon, last_logoff, last_password_change,
@@ -2665,24 +2491,6 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
 
        ZERO_STRUCTP(r);
 
-       if (!(pw = samu_new(mem_ctx))) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(pw, user_sid);
-       unbecome_root();
-
-       if (ret == False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(pw);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       samr_clear_sam_passwd(pw);
-
-       DEBUG(3,("User:[%s]\n", pdb_get_username(pw)));
-
        sid_user = pdb_get_user_sid(pw);
 
        if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
@@ -2690,7 +2498,6 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                          "the domain sid %s.  Failing operation.\n",
                          pdb_get_username(pw), sid_string_dbg(sid_user),
                          sid_string_dbg(domain_sid)));
-               TALLOC_FREE(pw);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
@@ -2703,7 +2510,6 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                          "which conflicts with the domain sid %s.  Failing operation.\n",
                          pdb_get_username(pw), sid_string_dbg(sid_group),
                          sid_string_dbg(domain_sid)));
-               TALLOC_FREE(pw);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
@@ -2736,7 +2542,6 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
        status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
        data_blob_free(&blob);
        if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(pw);
                return status;
        }
 
@@ -2791,10 +2596,9 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                              pdb_get_logon_count(pw),
                              0, /* country_code */
                              0, /* code_page */
-                             0, /* nt_password_set */
                              0, /* lm_password_set */
+                             0, /* nt_password_set */
                              password_expired);
-       TALLOC_FREE(pw);
 
        return NT_STATUS_OK;
 }
@@ -2811,6 +2615,8 @@ NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
        struct samr_info *info = NULL;
        DOM_SID domain_sid;
        uint32 rid;
+       bool ret = false;
+       struct samu *pwd = NULL;
 
        /* search for the handle */
        if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info))
@@ -2840,58 +2646,54 @@ NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
 
        DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
 
+       if (!(pwd = samu_new(p->mem_ctx))) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       become_root();
+       ret = pdb_getsampwsid(pwd, &info->sid);
+       unbecome_root();
+
+       if (ret == false) {
+               DEBUG(4,("User %s not found\n", sid_string_dbg(&info->sid)));
+               TALLOC_FREE(pwd);
+               return NT_STATUS_NO_SUCH_USER;
+       }
+
+       DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
+
+       samr_clear_sam_passwd(pwd);
+
        switch (r->in.level) {
        case 5:
-               status = get_user_info_5(p->mem_ctx, &user_info->info5, &info->sid, &domain_sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
                break;
        case 7:
-               status = get_user_info_7(p->mem_ctx, &user_info->info7, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
                break;
        case 9:
-               status = get_user_info_9(p->mem_ctx, &user_info->info9, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
                break;
        case 16:
-               status = get_user_info_16(p->mem_ctx, &user_info->info16, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
                break;
-
        case 18:
+               /* level 18 is special */
                status = get_user_info_18(p, p->mem_ctx, &user_info->info18, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
                break;
-
        case 20:
-               status = get_user_info_20(p->mem_ctx, &user_info->info20, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
                break;
-
        case 21:
-               status = get_user_info_21(p->mem_ctx, &user_info->info21,
-                                         &info->sid, &domain_sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid);
                break;
-
        default:
-               return NT_STATUS_INVALID_INFO_CLASS;
+               status = NT_STATUS_INVALID_INFO_CLASS;
+               break;
        }
 
+       TALLOC_FREE(pwd);
+
        *r->out.info = user_info;
 
        DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
@@ -3088,7 +2890,7 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
        }
 
        status = access_check_samr_function(info->acc_granted,
-                                           SAMR_ACCESS_OPEN_DOMAIN,
+                                           SAMR_ACCESS_LOOKUP_DOMAIN,
                                            "_samr_QueryDomainInfo" );
 
        if ( !NT_STATUS_IS_OK(status) )
@@ -3513,7 +3315,7 @@ NTSTATUS _samr_Connect(pipes_struct *p,
        map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
 
        se_map_generic( &des_access, &sam_generic_mapping );
-       info->acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS|SAMR_ACCESS_OPEN_DOMAIN);
+       info->acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS|SAMR_ACCESS_LOOKUP_DOMAIN);
 
        /* get a (unique) handle.  open a policy on it. */
        if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
@@ -3535,14 +3337,26 @@ NTSTATUS _samr_Connect2(pipes_struct *p,
        uint32    des_access = r->in.access_mask;
        NTSTATUS  nt_status;
        size_t    sd_size;
+       const char *fn = "_samr_Connect2";
 
+       switch (p->hdr_req.opnum) {
+       case NDR_SAMR_CONNECT2:
+               fn = "_samr_Connect2";
+               break;
+       case NDR_SAMR_CONNECT4:
+               fn = "_samr_Connect4";
+               break;
+       case NDR_SAMR_CONNECT5:
+               fn = "_samr_Connect5";
+               break;
+       }
 
-       DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
+       DEBUG(5,("%s: %d\n", fn, __LINE__));
 
        /* Access check */
 
        if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to _samr_Connect2\n"));
+               DEBUG(3, ("access denied to %s\n", fn));
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -3552,7 +3366,7 @@ NTSTATUS _samr_Connect2(pipes_struct *p,
        se_map_generic(&des_access, &sam_generic_mapping);
 
        nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
-               NULL, 0, des_access, &acc_granted, "_samr_Connect2");
+               NULL, 0, des_access, &acc_granted, fn);
 
        if ( !NT_STATUS_IS_OK(nt_status) )
                return nt_status;
@@ -3568,7 +3382,7 @@ NTSTATUS _samr_Connect2(pipes_struct *p,
        if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
-       DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
+       DEBUG(5,("%s: %d\n", fn, __LINE__));
 
        return nt_status;
 }
@@ -3580,48 +3394,13 @@ NTSTATUS _samr_Connect2(pipes_struct *p,
 NTSTATUS _samr_Connect4(pipes_struct *p,
                        struct samr_Connect4 *r)
 {
-       struct samr_info *info = NULL;
-       SEC_DESC *psd = NULL;
-       uint32    acc_granted;
-       uint32    des_access = r->in.access_mask;
-       NTSTATUS  nt_status;
-       size_t    sd_size;
-
-
-       DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
-
-       /* Access check */
-
-       if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to samr_Connect4\n"));
-               return NT_STATUS_ACCESS_DENIED;
-       }
-
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
-
-       make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
-       se_map_generic(&des_access, &sam_generic_mapping);
-
-       nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
-               NULL, 0, des_access, &acc_granted, "_samr_Connect4");
-
-       if ( !NT_STATUS_IS_OK(nt_status) )
-               return nt_status;
-
-       /* associate the user's SID and access granted with the new handle. */
-       if ((info = get_samr_info_by_sid(NULL)) == NULL)
-               return NT_STATUS_NO_MEMORY;
-
-       info->acc_granted = acc_granted;
-       info->status = r->in.access_mask; /* ??? */
-
-       /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       struct samr_Connect2 c;
 
-       DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
+       c.in.system_name        = r->in.system_name;
+       c.in.access_mask        = r->in.access_mask;
+       c.out.connect_handle    = r->out.connect_handle;
 
-       return NT_STATUS_OK;
+       return _samr_Connect2(p, &c);
 }
 
 /*******************************************************************
@@ -3631,50 +3410,22 @@ NTSTATUS _samr_Connect4(pipes_struct *p,
 NTSTATUS _samr_Connect5(pipes_struct *p,
                        struct samr_Connect5 *r)
 {
-       struct samr_info *info = NULL;
-       SEC_DESC *psd = NULL;
-       uint32    acc_granted;
-       uint32    des_access = r->in.access_mask;
-       NTSTATUS  nt_status;
-       size_t    sd_size;
+       NTSTATUS status;
+       struct samr_Connect2 c;
        struct samr_ConnectInfo1 info1;
 
-       DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
+       info1.client_version = SAMR_CONNECT_AFTER_W2K;
+       info1.unknown2 = 0;
 
-       /* Access check */
+       c.in.system_name        = r->in.system_name;
+       c.in.access_mask        = r->in.access_mask;
+       c.out.connect_handle    = r->out.connect_handle;
 
-       if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to samr_Connect5\n"));
-               return NT_STATUS_ACCESS_DENIED;
+       status = _samr_Connect2(p, &c);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
-
-       make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
-       se_map_generic(&des_access, &sam_generic_mapping);
-
-       nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
-               NULL, 0, des_access, &acc_granted, "_samr_Connect5");
-
-       if ( !NT_STATUS_IS_OK(nt_status) )
-               return nt_status;
-
-       /* associate the user's SID and access granted with the new handle. */
-       if ((info = get_samr_info_by_sid(NULL)) == NULL)
-               return NT_STATUS_NO_MEMORY;
-
-       info->acc_granted = acc_granted;
-       info->status = r->in.access_mask; /* ??? */
-
-       /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
-
-       DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
-
-       info1.client_version = SAMR_CONNECT_AFTER_W2K;
-       info1.unknown2 = 0;
-
        *r->out.level_out = 1;
        r->out.info_out->info1 = info1;
 
@@ -3700,7 +3451,7 @@ NTSTATUS _samr_LookupDomain(pipes_struct *p,
           Reverted that change so we will work with RAS servers again */
 
        status = access_check_samr_function(info->acc_granted,
-                                           SAMR_ACCESS_OPEN_DOMAIN,
+                                           SAMR_ACCESS_LOOKUP_DOMAIN,
                                            "_samr_LookupDomain");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -3937,29 +3688,57 @@ static bool set_user_info_16(struct samr_UserInfo16 *id16,
  set_user_info_18
  ********************************************************************/
 
-static bool set_user_info_18(struct samr_UserInfo18 *id18,
-                            struct samu *pwd)
+static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
+                                TALLOC_CTX *mem_ctx,
+                                DATA_BLOB *session_key,
+                                struct samu *pwd)
 {
        if (id18 == NULL) {
                DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
-               return False;
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd.hash, PDB_CHANGED)) {
-               return False;
+       if (id18->nt_pwd_active || id18->lm_pwd_active) {
+               if (!session_key->length) {
+                       return NT_STATUS_NO_USER_SESSION_KEY;
+               }
        }
-       if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd.hash, PDB_CHANGED)) {
-               return False;
+
+       if (id18->nt_pwd_active) {
+
+               DATA_BLOB in, out;
+
+               in = data_blob_const(id18->nt_pwd.hash, 16);
+               out = data_blob_talloc_zero(mem_ctx, 16);
+
+               sess_crypt_blob(&out, &in, session_key, false);
+
+               if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+
+               pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
        }
-       if (!pdb_set_pass_last_set_time (pwd, time(NULL), PDB_CHANGED)) {
-               return False;
+
+       if (id18->lm_pwd_active) {
+
+               DATA_BLOB in, out;
+
+               in = data_blob_const(id18->lm_pwd.hash, 16);
+               out = data_blob_talloc_zero(mem_ctx, 16);
+
+               sess_crypt_blob(&out, &in, session_key, false);
+
+               if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+
+               pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
        }
 
-       if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               return False;
-       }
+       copy_id18_to_sam_passwd(pwd, id18);
 
-       return True;
+       return pdb_update_sam_account(pwd);
 }
 
 /*******************************************************************
@@ -3999,6 +3778,10 @@ static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       if (id21->fields_present == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
        if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -4074,7 +3857,7 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
 {
        char *plaintext_buf = NULL;
        uint32 len = 0;
-       uint16 acct_ctrl;
+       uint32_t acct_ctrl;
        NTSTATUS status;
 
        if (id23 == NULL) {
@@ -4082,36 +3865,43 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       if (id23->info.fields_present == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
        if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
                return NT_STATUS_ACCESS_DENIED;
        }
 
+       if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
+           (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
 
-       DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
-                 pdb_get_username(pwd)));
-
-       acct_ctrl = pdb_get_acct_ctrl(pwd);
+               DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
+                         pdb_get_username(pwd)));
 
-       if (!decode_pw_buffer(mem_ctx,
-                               id23->password.data,
-                               &plaintext_buf,
-                               &len,
-                               STR_UNICODE)) {
-               return NT_STATUS_WRONG_PASSWORD;
-       }
+               if (!decode_pw_buffer(mem_ctx,
+                                     id23->password.data,
+                                     &plaintext_buf,
+                                     &len,
+                                     STR_UNICODE)) {
+                       return NT_STATUS_WRONG_PASSWORD;
+               }
 
-       if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
-               return NT_STATUS_ACCESS_DENIED;
+               if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
        }
 
        copy_id23_to_sam_passwd(pwd, id23);
 
+       acct_ctrl = pdb_get_acct_ctrl(pwd);
+
        /* if it's a trust account, don't update /etc/passwd */
        if (    ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
                ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
                ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
                DEBUG(5, ("Changing trust account.  Not updating /etc/passwd\n"));
-       } else  {
+       } else if (plaintext_buf) {
                /* update the UNIX password */
                if (lp_unix_password_sync() ) {
                        struct passwd *passwd;
@@ -4132,7 +3922,9 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
                }
        }
 
-       memset(plaintext_buf, '\0', strlen(plaintext_buf));
+       if (plaintext_buf) {
+               memset(plaintext_buf, '\0', strlen(plaintext_buf));
+       }
 
        if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
            (!NT_STATUS_IS_OK(status =  pdb_set_unix_primary_group(mem_ctx,
@@ -4151,23 +3943,16 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
  set_user_info_pw
  ********************************************************************/
 
-static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
-                            int level)
+static bool set_user_info_pw(uint8 *pass, struct samu *pwd)
 {
        uint32 len = 0;
        char *plaintext_buf = NULL;
        uint32 acct_ctrl;
-       time_t last_set_time;
-       enum pdb_value_state last_set_state;
 
        DEBUG(5, ("Attempting administrator password change for user %s\n",
                  pdb_get_username(pwd)));
 
        acct_ctrl = pdb_get_acct_ctrl(pwd);
-       /* we need to know if it's expired, because this is an admin change, not a
-          user change, so it's still expired when we're done */
-       last_set_state = pdb_get_init_flags(pwd, PDB_PASSLASTSET);
-       last_set_time = pdb_get_pass_last_set_time(pwd);
 
        if (!decode_pw_buffer(talloc_tos(),
                                pass,
@@ -4210,29 +3995,38 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
 
        memset(plaintext_buf, '\0', strlen(plaintext_buf));
 
-       /*
-        * A level 25 change does reset the pwdlastset field, a level 24
-        * change does not. I know this is probably not the full story, but
-        * it is needed to make XP join LDAP correctly, without it the later
-        * auth2 check can fail with PWD_MUST_CHANGE.
-        */
-       if (level != 25) {
-               /*
-                * restore last set time as this is an admin change, not a
-                * user pw change
-                */
-               pdb_set_pass_last_set_time (pwd, last_set_time,
-                                           last_set_state);
+       DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
+
+       return True;
+}
+
+/*******************************************************************
+ set_user_info_24
+ ********************************************************************/
+
+static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
+                                struct samr_UserInfo24 *id24,
+                                struct samu *pwd)
+{
+       NTSTATUS status;
+
+       if (id24 == NULL) {
+               DEBUG(5, ("set_user_info_24: NULL id24\n"));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
+       if (!set_user_info_pw(id24->password.data, pwd)) {
+               return NT_STATUS_WRONG_PASSWORD;
+       }
 
-       /* update the SAMBA password */
-       if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               return False;
+       copy_id24_to_sam_passwd(pwd, id24);
+
+       status = pdb_update_sam_account(pwd);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       return True;
+       return NT_STATUS_OK;
 }
 
 /*******************************************************************
@@ -4250,10 +4044,22 @@ static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       if (id25->info.fields_present == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
        if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
                return NT_STATUS_ACCESS_DENIED;
        }
 
+       if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
+           (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
+
+               if (!set_user_info_pw(id25->password.data, pwd)) {
+                       return NT_STATUS_WRONG_PASSWORD;
+               }
+       }
+
        copy_id25_to_sam_passwd(pwd, id25);
 
        /* write the change out */
@@ -4279,6 +4085,36 @@ static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
+/*******************************************************************
+ set_user_info_26
+ ********************************************************************/
+
+static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
+                                struct samr_UserInfo26 *id26,
+                                struct samu *pwd)
+{
+       NTSTATUS status;
+
+       if (id26 == NULL) {
+               DEBUG(5, ("set_user_info_26: NULL id26\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!set_user_info_pw(id26->password.data, pwd)) {
+               return NT_STATUS_WRONG_PASSWORD;
+       }
+
+       copy_id26_to_sam_passwd(pwd, id26);
+
+       status = pdb_update_sam_account(pwd);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}
+
+
 /*******************************************************************
  samr_SetUserInfo
  ********************************************************************/
@@ -4399,9 +4235,10 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                case 18:
                        /* Used by AS/U JRA. */
-                       if (!set_user_info_18(&info->info18, pwd)) {
-                               status = NT_STATUS_ACCESS_DENIED;
-                       }
+                       status = set_user_info_18(&info->info18,
+                                                 p->mem_ctx,
+                                                 &p->server_info->user_session_key,
+                                                 pwd);
                        break;
 
                case 20:
@@ -4438,10 +4275,8 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        dump_data(100, info->info24.password.data, 516);
 
-                       if (!set_user_info_pw(info->info24.password.data, pwd,
-                                             switch_value)) {
-                               status = NT_STATUS_WRONG_PASSWORD;
-                       }
+                       status = set_user_info_24(p->mem_ctx,
+                                                 &info->info24, pwd);
                        break;
 
                case 25:
@@ -4456,13 +4291,6 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        status = set_user_info_25(p->mem_ctx,
                                                  &info->info25, pwd);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               goto done;
-                       }
-                       if (!set_user_info_pw(info->info25.password.data, pwd,
-                                             switch_value)) {
-                               status = NT_STATUS_WRONG_PASSWORD;
-                       }
                        break;
 
                case 26:
@@ -4475,18 +4303,14 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        dump_data(100, info->info26.password.data, 516);
 
-                       if (!set_user_info_pw(info->info26.password.data, pwd,
-                                             switch_value)) {
-                               status = NT_STATUS_WRONG_PASSWORD;
-                       }
+                       status = set_user_info_26(p->mem_ctx,
+                                                 &info->info26, pwd);
                        break;
 
                default:
                        status = NT_STATUS_INVALID_INFO_CLASS;
        }
 
- done:
-
        TALLOC_FREE(pwd);
 
        if (has_enough_rights) {