summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb_nt_transact_quota.c
blob: c389f38694df824aee95eb844c2f160bf269ee54 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * 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) 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
 */

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

/*
 * smb_nt_transact_query_quota
 *
 * This method allows the client to retrieve quota information from
 * the server. The result of the call is returned to the client in the
 * Data part of the transaction response.
 *
 * On entry, the 'TotalParameterCount' field must be equal to 16, and the
 * client parameter block must be encoded with the following parameters:
 *
 * Request                    Description
 * ========================== ==================================
 * WORD fid                   SMB file identifier of the target directory
 * BYTE ReturnSingleEntry     A boolean indicating whether to return
 *                            a single entry (TRUE) or multiple entries (FALSE).
 * BYTE RestartScan           A boolean indicating whether to continue from
 *                            the previous request (FALSE) or restart a new
 *                            sequence (TRUE).
 * DWORD SidListLength        The length, in bytes, of the SidList in the
 *                            data block or 0 if there is no SidList.
 * DWORD StartSidLength       If SidListLength is 0 (i.e. there is no SidList
 *                            in the data block), then this is either:
 *                                 1) the (non-zero) length in bytes of the
 *                                    StartSid in the parameter buffer, or
 *                                 2) if 0, there is no StartSid in the
 *                                    parameter buffer, in which case, all SIDs
 *                                    are to be enumerated as if they were
 *                                    passed in the SidList.
 *                            Otherwise, StartSidLength is ignored.
 * DWORD StartSidOffset       The offset, in bytes, to the StartSid in the
 *                            parameter block (if one exists).
 *
 * One of SidListLength and StartSidLength must be 0.
 *
 * An SMB_COM_NT_TRANSACTION response is sent in reply when the request
 * is successful.  The 'TotalParameterCount' is set to 4, and the parameter
 * block in the server response contains a 32-bit unsigned integer
 * indicating the length, in bytes, of the returned quota information.
 * The 'TotalDataCount' is set to indicate the length of the data buffer,
 * and the data buffer contains the following quota information:
 *
 *  Data Block Encoding                Description
 *  ================================== =================================
 *  ULONG NextEntryOffset;             Offset to start of next entry from
 *                                     start of this entry, or 0 for the
 *                                     final entry
 *  ULONG SidLength;                   Length (bytes) of SID
 *  SMB_TIME ChangeTime;               Time that the quota was last changed
 *  LARGE_INTEGER QuotaUsed;           Amount of quota (bytes) used by user
 *  LARGE_INTEGER QuotaThreshold;      Quota warning limit (bytes) for user
 *  LARGE_INTEGER QuotaLimit;          The quota limit (bytes) for this user
 *  USHORT Sid;                        Search handle
 */
smb_sdrc_t
smb_nt_transact_query_quota(smb_request_t *sr, smb_xa_t *xa)
{
	uint8_t		single, restart;
	uint32_t	sidlistlen, startsidlen, startsidoff;
	smb_node_t	*tnode;
	smb_ofile_t	*ofile;
	smb_quota_query_t request;
	smb_quota_response_t reply;
	uint32_t status = NT_STATUS_SUCCESS;

	bzero(&request, sizeof (smb_quota_query_t));
	bzero(&reply, sizeof (smb_quota_response_t));

	if (!smb_tree_has_feature(sr->tid_tree, SMB_TREE_QUOTA)) {
		smbsr_error(sr, NT_STATUS_NOT_SUPPORTED, 0, 0);
		return (SDRC_ERROR);
	}

	if (xa->smb_tpscnt != 16) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
		return (SDRC_ERROR);
	}

	if (smb_mbc_decodef(&xa->req_param_mb, "%wbblll", sr, &sr->smb_fid,
	    &single, &restart, &sidlistlen, &startsidlen, &startsidoff)) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
		return (SDRC_ERROR);
	}

	if ((sidlistlen != 0) && (startsidlen != 0)) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
		return (SDRC_ERROR);
	}

	smbsr_lookup_file(sr);
	ofile = sr->fid_ofile;
	if (ofile == NULL) {
		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
		return (SDRC_ERROR);
	}

	if ((ofile->f_node == NULL) || (ofile->f_ftype != SMB_FTYPE_DISK)) {
		smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS,
		    ERROR_ACCESS_DENIED);
		smbsr_release_file(sr);
		return (SDRC_ERROR);
	}

	tnode = sr->tid_tree->t_snode;
	request.qq_root_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
	if (smb_node_getmntpath(tnode, request.qq_root_path, MAXPATHLEN) != 0) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, ERRDOS,
		    ERROR_INVALID_PARAMETER);
		smbsr_release_file(sr);
		kmem_free(request.qq_root_path, MAXPATHLEN);
		return (SDRC_ERROR);
	}

	if (sidlistlen != 0)
		request.qq_query_op = SMB_QUOTA_QUERY_SIDLIST;
	else if (startsidlen != 0)
		request.qq_query_op = SMB_QUOTA_QUERY_STARTSID;
	else
		request.qq_query_op = SMB_QUOTA_QUERY_ALL;

	request.qq_single = single;
	request.qq_restart = restart;
	smb_quota_max_quota(&xa->rep_data_mb, &request);

	status = smb_quota_init_sids(&xa->req_data_mb, &request, ofile);

	if (status == NT_STATUS_SUCCESS) {
		if (smb_quota_query(sr->sr_server, &request, &reply) != 0) {
			status = NT_STATUS_INTERNAL_ERROR;
		} else {
			status = reply.qr_status;
			if (status == NT_STATUS_SUCCESS) {
				status = smb_quota_encode_quotas(
				    &xa->rep_data_mb,
				    &request, &reply, ofile);
			}
			(void) smb_mbc_encodef(&xa->rep_param_mb, "l",
			    xa->rep_data_mb.chain_offset);
			xdr_free(smb_quota_response_xdr, (char *)&reply);
		}
	}

	kmem_free(request.qq_root_path, MAXPATHLEN);
	smb_quota_free_sids(&request);

	if (status != NT_STATUS_SUCCESS) {
		if (status == NT_STATUS_NO_MORE_ENTRIES) {
			smb_ofile_set_quota_resume(ofile, NULL);
			smbsr_warn(sr, status, 0, 0);
			status = NT_STATUS_SUCCESS;
		} else {
			smbsr_error(sr, status, 0, 0);
		}
		(void) smb_mbc_encodef(&xa->rep_param_mb, "l", 0);
	}

	smbsr_release_file(sr);
	return ((status == NT_STATUS_SUCCESS) ? SDRC_SUCCESS : SDRC_ERROR);
}

/*
 * smb_nt_transact_set_quota
 *
 * This method allows the client to set quota information on the server.
 * The result status of the call is returned to the client in the
 * 'status' field of the SMB response header.
 *
 * On entry, the 'TotalParameterCount' field must be equal to 2, and the
 * client parameter block must be encoded with the following parameters:
 *
 *  Data Block Encoding                Description
 *  ================================== =================================
 *  ULONG NextEntryOffset;             Offset to start of next entry from
 *                                     start of this entry, or 0 for the
 *                                     final entry
 *  ULONG SidLength;                   Length (bytes) of SID
 *  SMB_TIME ChangeTime;               Time that the quota was last changed
 *  LARGE_INTEGER QuotaUsed;           Amount of quota (bytes) used by user
 *  LARGE_INTEGER QuotaThreshold;      Quota warning limit (bytes) for user
 *  LARGE_INTEGER QuotaLimit;          The quota limit (bytes) for this user
 *  VARIABLE Sid;                      Security identifier of the user
 *
 * An SMB_COM_NT_TRANSACTION response is sent in reply when the request
 * is successful.  The 'TotalParameterCount' and the 'TotalDataCount' are set
 * to 0, and the parameter block 'Status' field in the server SMB response
 * header contains a 32-bit unsigned integer indicating the result status
 * (NT_STATUS_SUCCESS if successful).
 *
 * Only users with Admin privileges (i.e. of the BUILTIN/Administrators
 * group) will be allowed to set quotas.
 */
smb_sdrc_t
smb_nt_transact_set_quota(smb_request_t *sr, smb_xa_t *xa)
{
	char		*root_path;
	uint32_t	status = NT_STATUS_SUCCESS;
	smb_node_t	*tnode;
	smb_ofile_t	*ofile;
	smb_quota_set_t request;
	uint32_t	reply;
	list_t 		*quota_list;

	bzero(&request, sizeof (smb_quota_set_t));

	if (!smb_tree_has_feature(sr->tid_tree, SMB_TREE_QUOTA)) {
		smbsr_error(sr, NT_STATUS_NOT_SUPPORTED, 0, 0);
		return (SDRC_ERROR);
	}

	if (!smb_user_is_admin(sr->uid_user)) {
		smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRSRV, ERRaccess);
		return (-1);
	}

	if (xa->smb_tpscnt != 2) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
		return (SDRC_ERROR);
	}

	if (smb_mbc_decodef(&xa->req_param_mb, "%w", sr,
	    &sr->smb_fid)) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
		return (SDRC_ERROR);
	}

	smbsr_lookup_file(sr);
	ofile = sr->fid_ofile;
	if (ofile == NULL) {
		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
		return (SDRC_ERROR);
	}

	if ((ofile->f_node == NULL) || (ofile->f_ftype != SMB_FTYPE_DISK)) {
		smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS,
		    ERROR_ACCESS_DENIED);
		smbsr_release_file(sr);
		return (SDRC_ERROR);
	}

	tnode = sr->tid_tree->t_snode;
	root_path  = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
	if (smb_node_getmntpath(tnode, root_path, MAXPATHLEN) != 0) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, ERRDOS,
		    ERROR_INVALID_PARAMETER);
		smbsr_release_file(sr);
		kmem_free(root_path, MAXPATHLEN);
		return (SDRC_ERROR);
	}

	quota_list = &request.qs_quota_list;
	list_create(quota_list, sizeof (smb_quota_t),
	    offsetof(smb_quota_t, q_list_node));

	status = smb_quota_decode_quotas(&xa->req_data_mb, quota_list);
	if (status == NT_STATUS_SUCCESS) {
		request.qs_root_path = root_path;
		if (smb_quota_set(sr->sr_server, &request, &reply) != 0) {
			status = NT_STATUS_INTERNAL_ERROR;
		} else {
			status = reply;
			xdr_free(xdr_uint32_t, (char *)&reply);
		}
	}

	kmem_free(root_path, MAXPATHLEN);
	smb_quota_free_quotas(&request.qs_quota_list);
	smbsr_release_file(sr);

	if (status != NT_STATUS_SUCCESS) {
		smbsr_error(sr, status, 0, 0);
		(void) smb_mbc_encodef(&xa->rep_param_mb, "l", 0);
		return (SDRC_ERROR);
	}

	return (SDRC_SUCCESS);
}