summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb_cmn_setfile.c
blob: 1b9ed07060ced5bfa8da759e4f1a78078b210ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2020 Tintri by DDN, Inc.  All rights reserved.
 * Copyright 2022 RackTop Systems, Inc.
 */

/*
 * Common functions supporting both:
 * SMB1 Trans2 Set File/Path Info,
 * SMB2 Set File Info
 */

#include <smbsrv/smb2_kproto.h>
#include <smbsrv/smb_fsops.h>

/*
 * smb_set_basic_info
 * [MS-FSCC] 2.4.7
 *	FileBasicInformation
 *	SMB_SET_FILE_BASIC_INFO
 *	SMB_FILE_BASIC_INFORMATION
 *
 * Sets basic file/path information.
 *
 * It is not valid to set FILE_ATTRIBUTE_DIRECTORY if the
 * target is not a directory.
 *
 * For compatibility with windows servers:
 * - if the specified attributes have ONLY FILE_ATTRIBUTE_NORMAL set
 *   clear (0) the file's attributes.
 * - if the specified attributes are 0 do NOT change the file's attributes.
 */
uint32_t
smb_set_basic_info(smb_request_t *sr, smb_setinfo_t *si)
{
	smb_attr_t *attr = &si->si_attr;
	smb_node_t *node = si->si_node;
	uint64_t crtime, atime, mtime, ctime;
	uint32_t attributes;
	int rc;

	if (smb_mbc_decodef(&si->si_data, "qqqql",
	    &crtime, &atime, &mtime, &ctime, &attributes) != 0)
		return (NT_STATUS_INFO_LENGTH_MISMATCH);

	/*
	 * MS-FSA 2.1.5.14.2 FileBasicInformation
	 * Return STATUS_INVALID_PARAMETER if:
	 * FILE_ATTRIBUTE_TEMPORARY on a directory,
	 * FILE_ATTRIBUTE_DIRECTORY on a non-directory.
	 */
	if (smb_node_is_dir(node)) {
		if ((attributes & FILE_ATTRIBUTE_TEMPORARY) != 0)
			return (NT_STATUS_INVALID_PARAMETER);
	} else {
		if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
			return (NT_STATUS_INVALID_PARAMETER);
	}

	bzero(attr, sizeof (*attr));
	if (atime != 0 && atime != (uint64_t)-1) {
		smb_time_nt_to_unix(atime, &attr->sa_vattr.va_atime);
		attr->sa_mask |= SMB_AT_ATIME;
	}
	if (mtime != 0 && mtime != (uint64_t)-1) {
		smb_time_nt_to_unix(mtime, &attr->sa_vattr.va_mtime);
		attr->sa_mask |= SMB_AT_MTIME;
	}
	if (ctime != 0 && ctime != (uint64_t)-1) {
		smb_time_nt_to_unix(ctime, &attr->sa_vattr.va_ctime);
		attr->sa_mask |= SMB_AT_CTIME;
	}
	if (crtime != 0 && crtime != (uint64_t)-1) {
		smb_time_nt_to_unix(crtime, &attr->sa_crtime);
		attr->sa_mask |= SMB_AT_CRTIME;
	}

	if (attributes != 0) {
		attr->sa_dosattr = attributes;
		attr->sa_mask |= SMB_AT_DOSATTR;
	}

	rc = smb_node_setattr(sr, node, sr->user_cr, sr->fid_ofile, attr);
	if (rc != 0)
		return (smb_errno2status(rc));

	return (0);
}

/*
 * smb_set_eof_info
 *	FileEndOfFileInformation
 *	SMB_SET_FILE_END_OF_FILE_INFO
 *	SMB_FILE_END_OF_FILE_INFORMATION
 */
uint32_t
smb_set_eof_info(smb_request_t *sr, smb_setinfo_t *si)
{
	smb_attr_t *attr = &si->si_attr;
	smb_node_t *node = si->si_node;
	uint64_t eof;
	uint32_t status;
	int rc;

	if (smb_mbc_decodef(&si->si_data, "q", &eof) != 0)
		return (NT_STATUS_INFO_LENGTH_MISMATCH);

	if (smb_node_is_dir(node))
		return (NT_STATUS_INVALID_PARAMETER);

	status = smb_oplock_break_SETINFO(node, sr->fid_ofile,
	    FileEndOfFileInformation);
	if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
		if (sr->session->dialect >= SMB_VERS_2_BASE)
			(void) smb2sr_go_async(sr);
		(void) smb_oplock_wait_break(sr, node, 0);
		status = 0;
	}
	if (status != 0)
		return (status);

	bzero(attr, sizeof (*attr));
	attr->sa_mask = SMB_AT_SIZE;
	attr->sa_vattr.va_size = (u_offset_t)eof;
	rc = smb_node_setattr(sr, node, sr->user_cr, sr->fid_ofile, attr);
	if (rc != 0)
		return (smb_errno2status(rc));

	return (0);
}

/*
 * smb_set_alloc_info
 *	FileAllocationInformation
 *	SMB_SET_FILE_ALLOCATION_INFO
 *	SMB_FILE_ALLOCATION_INFORMATION
 */
uint32_t
smb_set_alloc_info(smb_request_t *sr, smb_setinfo_t *si)
{
	smb_attr_t *attr = &si->si_attr;
	smb_node_t *node = si->si_node;
	uint64_t allocsz;
	uint32_t status;
	int rc;

	if (smb_mbc_decodef(&si->si_data, "q", &allocsz) != 0)
		return (NT_STATUS_INFO_LENGTH_MISMATCH);

	if (smb_node_is_dir(node))
		return (NT_STATUS_INVALID_PARAMETER);

	status = smb_oplock_break_SETINFO(node, sr->fid_ofile,
	    FileAllocationInformation);
	if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
		if (sr->session->dialect >= SMB_VERS_2_BASE)
			(void) smb2sr_go_async(sr);
		(void) smb_oplock_wait_break(sr, node, 0);
		status = 0;
	}
	if (status != 0)
		return (status);

	bzero(attr, sizeof (*attr));
	attr->sa_mask = SMB_AT_ALLOCSZ;
	attr->sa_allocsz = (u_offset_t)allocsz;
	rc = smb_node_setattr(sr, node, sr->user_cr, sr->fid_ofile, attr);
	if (rc != 0)
		return (smb_errno2status(rc));

	return (0);
}

/*
 * smb_set_disposition_info
 * See:
 *	FileDispositionInformation
 *	SMB_SET_FILE_DISPOSITION_INFO
 *	SMB_FILE_DISPOSITION_INFORMATION
 *
 * Set/Clear DELETE_ON_CLOSE flag for an open file.
 * File should have been opened with DELETE access otherwise
 * the operation is not permitted.
 *
 * NOTE: The node should be marked delete-on-close upon the receipt
 * of the Trans2SetFileInfo(SetDispositionInfo) if mark_delete is set.
 * It is different than both SmbNtCreateAndX and SmbNtTransact, which
 * set delete-on-close on the ofile and defer setting the flag on the
 * node until the file is closed.
 *
 * Observation of Windows 2000 indicates the following:
 *
 * 1) If a file is not opened with delete-on-close create options and
 * the delete-on-close is set via Trans2SetFileInfo(SetDispositionInfo)
 * using that open file handle, any subsequent open requests will fail
 * with DELETE_PENDING.
 *
 * 2) If a file is opened with delete-on-close create options and the
 * client attempts to unset delete-on-close via Trans2SetFileInfo
 * (SetDispositionInfo) prior to the file close, any subsequent open
 * requests will still fail with DELETE_PENDING after the file is closed.
 *
 * 3) If a file is opened with delete-on-close create options and that
 * file handle (not the last open handle and the only file handle
 * with delete-on-close set) is closed. Any subsequent open requests
 * will fail with DELETE_PENDING. Unsetting delete-on-close via
 * Trans2SetFileInfo(SetDispositionInfo) at this time will unset the
 * node delete-on-close flag, which will result in the file not being
 * removed even after the last file handle is closed.
 */
uint32_t
smb_set_disposition_info(smb_request_t *sr, smb_setinfo_t *si)
{
	smb_node_t *node = si->si_node;
	smb_ofile_t *of = sr->fid_ofile;
	uint8_t		mark_delete;
	uint32_t	status;
	uint32_t	flags = 0;

	if (smb_mbc_decodef(&si->si_data, "b", &mark_delete) != 0)
		return (NT_STATUS_INFO_LENGTH_MISMATCH);

	if ((of == NULL) || !(smb_ofile_granted_access(of) & DELETE))
		return (NT_STATUS_ACCESS_DENIED);

	if (mark_delete == 0) {
		smb_node_reset_delete_on_close(node);
		return (NT_STATUS_SUCCESS);
	}

	/*
	 * Break any oplock handle caching.
	 */
	status = smb_oplock_break_SETINFO(node, of,
	    FileDispositionInformation);
	if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
		if (sr->session->dialect >= SMB_VERS_2_BASE)
			(void) smb2sr_go_async(sr);
		(void) smb_oplock_wait_break(sr, node, 0);
		status = 0;
	}
	if (status != 0)
		return (status);

	if (SMB_TREE_SUPPORTS_CATIA(sr))
		flags |= SMB_CATIA;

	return (smb_node_set_delete_on_close(node, of->f_cr, flags));
}