Coverage Report

Created: 2025-11-16 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/smbd/smb2_pipes.c
Line
Count
Source
1
/*
2
   Unix SMB/CIFS implementation.
3
   Pipe SMB reply routines
4
   Copyright (C) Andrew Tridgell 1992-1998
5
   Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6
   Copyright (C) Paul Ashton  1997-1998.
7
   Copyright (C) Jeremy Allison 2005.
8
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
19
   You should have received a copy of the GNU General Public License
20
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
*/
22
/*
23
   This file handles reply_ calls on named pipes that the server
24
   makes to handle specific protocols
25
*/
26
27
28
#include "includes.h"
29
#include "smbd/smbd.h"
30
#include "smbd/globals.h"
31
#include "libcli/security/security.h"
32
#include "rpc_server/srv_pipe_hnd.h"
33
#include "auth/auth_util.h"
34
#include "librpc/rpc/dcerpc_helper.h"
35
36
NTSTATUS open_np_file(struct smb_request *smb_req, const char *name,
37
          struct files_struct **pfsp)
38
0
{
39
0
  struct smbXsrv_connection *xconn = smb_req->xconn;
40
0
  struct connection_struct *conn = smb_req->conn;
41
0
  struct files_struct *fsp;
42
0
  struct smb_filename *smb_fname = NULL;
43
0
  struct auth_session_info *session_info = conn->session_info;
44
0
  NTSTATUS status;
45
46
0
  status = file_new(smb_req, conn, &fsp);
47
0
  if (!NT_STATUS_IS_OK(status)) {
48
0
    DEBUG(0, ("file_new failed: %s\n", nt_errstr(status)));
49
0
    return status;
50
0
  }
51
52
0
  fsp->conn = conn;
53
0
  fsp_set_fd(fsp, -1);
54
0
  fsp->vuid = smb_req->vuid;
55
0
  fsp->fsp_flags.can_lock = false;
56
0
  fsp->access_mask = FILE_READ_DATA | FILE_WRITE_DATA;
57
58
0
  smb_fname = cp_smb_basename(talloc_tos(), name);
59
0
  if (smb_fname == NULL) {
60
0
    file_free(smb_req, fsp);
61
0
    return NT_STATUS_NO_MEMORY;
62
0
  }
63
0
  status = fsp_set_smb_fname(fsp, smb_fname);
64
0
  TALLOC_FREE(smb_fname);
65
0
  if (!NT_STATUS_IS_OK(status)) {
66
0
    file_free(smb_req, fsp);
67
0
    return status;
68
0
  }
69
70
0
  if (smb_req->smb2req != NULL && smb_req->smb2req->was_encrypted) {
71
0
    struct security_token *security_token = NULL;
72
0
    uint16_t dialect = xconn->smb2.server.dialect;
73
0
    uint16_t srv_smb_encrypt = DCERPC_SMB_ENCRYPTION_REQUIRED;
74
0
    uint16_t cipher = xconn->smb2.server.cipher;
75
0
    struct dom_sid smb3_sid = global_sid_Samba_SMB3;
76
0
    size_t num_smb3_sids;
77
0
    bool ok;
78
79
0
    session_info = copy_session_info(fsp, conn->session_info);
80
0
    if (session_info == NULL) {
81
0
      DBG_ERR("Failed to copy session info\n");
82
0
      file_free(smb_req, fsp);
83
0
      return NT_STATUS_NO_MEMORY;
84
0
    }
85
0
    security_token = session_info->security_token;
86
87
    /*
88
     * Security check:
89
     *
90
     * Make sure we don't have a SMB3 SID in the security token!
91
     */
92
0
    num_smb3_sids = security_token_count_flag_sids(security_token,
93
0
                     &smb3_sid,
94
0
                     3,
95
0
                     NULL);
96
0
    if (num_smb3_sids != 0) {
97
0
      DBG_ERR("ERROR: %zu SMB3 SIDs have already been "
98
0
        "detected in the security token!\n",
99
0
        num_smb3_sids);
100
0
      file_free(smb_req, fsp);
101
0
      return NT_STATUS_ACCESS_DENIED;
102
0
    }
103
104
0
    ok = sid_append_rid(&smb3_sid, dialect);
105
0
    ok &= sid_append_rid(&smb3_sid, srv_smb_encrypt);
106
0
    ok &= sid_append_rid(&smb3_sid, cipher);
107
108
0
    if (!ok) {
109
0
      DBG_ERR("sid too small\n");
110
0
      file_free(smb_req, fsp);
111
0
      return NT_STATUS_BUFFER_TOO_SMALL;
112
0
    }
113
114
0
    status = add_sid_to_array_unique(security_token,
115
0
             &smb3_sid,
116
0
             &security_token->sids,
117
0
             &security_token->num_sids);
118
0
    if (!NT_STATUS_IS_OK(status)) {
119
0
      DBG_ERR("Failed to add SMB3 SID to security token\n");
120
0
      file_free(smb_req, fsp);
121
0
      return status;
122
0
    }
123
124
0
    fsp->fsp_flags.encryption_required = true;
125
0
  }
126
127
0
  status = np_open(fsp, name,
128
0
       conn->sconn->remote_address,
129
0
       conn->sconn->local_address,
130
0
       session_info,
131
0
       conn->sconn->ev_ctx,
132
0
       conn->sconn->msg_ctx,
133
0
       conn->sconn->dce_ctx,
134
0
       &fsp->fake_file_handle);
135
0
  if (!NT_STATUS_IS_OK(status)) {
136
0
    DEBUG(10, ("np_open(%s) returned %s\n", name,
137
0
         nt_errstr(status)));
138
0
    file_free(smb_req, fsp);
139
0
    return status;
140
0
  }
141
142
0
  *pfsp = fsp;
143
144
0
  return NT_STATUS_OK;
145
0
}