s3-samr: _samr_EnumDomainUsers always returns a sam array (even w/o users).
[kai/samba.git] / source3 / rpc_server / srv_samr_nt.c
index 537d58fac1019d46798dec4080d6e8adc665efac..dc2377b90259d2f3fd80807032e5e9d8c9a5350c 100644 (file)
@@ -1012,6 +1012,7 @@ NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
        if (!samr_array) {
                return NT_STATUS_NO_MEMORY;
        }
+       *r->out.sam = samr_array;
 
        become_root();
 
@@ -1071,7 +1072,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__));
@@ -3109,7 +3109,7 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
                                                        lp_workgroup(),
                                                        global_myname(),
                                                        seq_num,
-                                                       1,
+                                                       DOMAIN_SERVER_ENABLED,
                                                        server_role,
                                                        1,
                                                        num_users,
@@ -3478,14 +3478,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;
        }
 
@@ -3495,7 +3507,7 @@ NTSTATUS _samr_Connect2(pipes_struct *p,
        se_map_generic(&des_access, &sam_generic_mapping);
 
        nt_status = access_check_samr_object(psd, p->server_info->ptok,
-               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;
@@ -3511,7 +3523,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;
 }
@@ -3523,48 +3535,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->server_info->ptok, &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->server_info->ptok,
-               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;
+       struct samr_Connect2 c;
 
-       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_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);
 }
 
 /*******************************************************************
@@ -3574,50 +3551,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->server_info->ptok, &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->server_info->ptok,
-               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;
 
@@ -3825,13 +3774,11 @@ static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
 
        if (id7 == NULL) {
                DEBUG(5, ("set_user_info_7: NULL id7\n"));
-               TALLOC_FREE(pwd);
                return NT_STATUS_ACCESS_DENIED;
        }
 
        if (!id7->account_name.string) {
                DEBUG(5, ("set_user_info_7: failed to get new username\n"));
-               TALLOC_FREE(pwd);
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -3851,7 +3798,6 @@ static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
 
        rc = pdb_rename_sam_account(pwd, id7->account_name.string);
 
-       TALLOC_FREE(pwd);
        return rc;
 }
 
@@ -3864,23 +3810,18 @@ static bool set_user_info_16(struct samr_UserInfo16 *id16,
 {
        if (id16 == NULL) {
                DEBUG(5, ("set_user_info_16: NULL id16\n"));
-               TALLOC_FREE(pwd);
                return False;
        }
 
        /* FIX ME: check if the value is really changed --metze */
        if (!pdb_set_acct_ctrl(pwd, id16->acct_flags, PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
-       TALLOC_FREE(pwd);
-
        return True;
 }
 
@@ -3893,29 +3834,23 @@ static bool set_user_info_18(struct samr_UserInfo18 *id18,
 {
        if (id18 == NULL) {
                DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
-               TALLOC_FREE(pwd);
                return False;
        }
 
        if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd.hash, PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
                return False;
        }
        if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd.hash, PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
                return False;
        }
        if (!pdb_set_pass_last_set_time (pwd, time(NULL), PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
-       TALLOC_FREE(pwd);
        return True;
 }
 
@@ -3935,12 +3870,9 @@ static bool set_user_info_20(struct samr_UserInfo20 *id20,
 
        /* write the change out */
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
-       TALLOC_FREE(pwd);
-
        return True;
 }
 
@@ -3959,6 +3891,14 @@ 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;
+       }
+
        /* we need to separately check for an account rename first */
 
        if (id21->account_name.string &&
@@ -3984,7 +3924,6 @@ static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
                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;
                }
 
@@ -4015,12 +3954,9 @@ static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
 
        /* write the change out */
        if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return status;
        }
 
-       TALLOC_FREE(pwd);
-
        return NT_STATUS_OK;
 }
 
@@ -4042,6 +3978,15 @@ 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;
+       }
+
+
        DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
                  pdb_get_username(pwd)));
 
@@ -4052,12 +3997,10 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
                                &plaintext_buf,
                                &len,
                                STR_UNICODE)) {
-               TALLOC_FREE(pwd);
-               return NT_STATUS_INVALID_PARAMETER;
+               return NT_STATUS_WRONG_PASSWORD;
        }
 
        if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
-               TALLOC_FREE(pwd);
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -4074,7 +4017,6 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
                        struct passwd *passwd;
                        if (pdb_get_username(pwd) == NULL) {
                                DEBUG(1, ("chgpasswd: User without name???\n"));
-                               TALLOC_FREE(pwd);
                                return NT_STATUS_ACCESS_DENIED;
                        }
 
@@ -4084,7 +4026,6 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
                        }
 
                        if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
-                               TALLOC_FREE(pwd);
                                return NT_STATUS_ACCESS_DENIED;
                        }
                        TALLOC_FREE(passwd);
@@ -4096,17 +4037,13 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
        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(status = pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return status;
        }
 
-       TALLOC_FREE(pwd);
-
        return NT_STATUS_OK;
 }
 
@@ -4137,12 +4074,10 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
                                &plaintext_buf,
                                &len,
                                STR_UNICODE)) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
        if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
@@ -4158,7 +4093,6 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
 
                        if (pdb_get_username(pwd) == NULL) {
                                DEBUG(1, ("chgpasswd: User without name???\n"));
-                               TALLOC_FREE(pwd);
                                return False;
                        }
 
@@ -4168,7 +4102,6 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
                        }
 
                        if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
-                               TALLOC_FREE(pwd);
                                return False;
                        }
                        TALLOC_FREE(passwd);
@@ -4196,12 +4129,9 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
 
        /* update the SAMBA password */
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
-       TALLOC_FREE(pwd);
-
        return True;
 }
 
@@ -4220,11 +4150,18 @@ 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;
+       }
+
        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;
        }
 
@@ -4243,9 +4180,6 @@ static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
                }
        }
 
-       /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
-        * hereafter! */
-
        return NT_STATUS_OK;
 }
 
@@ -4410,7 +4344,7 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        if (!set_user_info_pw(info->info24.password.data, pwd,
                                              switch_value)) {
-                               status = NT_STATUS_ACCESS_DENIED;
+                               status = NT_STATUS_WRONG_PASSWORD;
                        }
                        break;
 
@@ -4431,7 +4365,7 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
                        }
                        if (!set_user_info_pw(info->info25.password.data, pwd,
                                              switch_value)) {
-                               status = NT_STATUS_ACCESS_DENIED;
+                               status = NT_STATUS_WRONG_PASSWORD;
                        }
                        break;
 
@@ -4447,7 +4381,7 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        if (!set_user_info_pw(info->info26.password.data, pwd,
                                              switch_value)) {
-                               status = NT_STATUS_ACCESS_DENIED;
+                               status = NT_STATUS_WRONG_PASSWORD;
                        }
                        break;
 
@@ -4457,6 +4391,8 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
  done:
 
+       TALLOC_FREE(pwd);
+
        if (has_enough_rights) {
                unbecome_root();
        }