2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan (metze) Metzmacher 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libsmb/libsmb.h"
22 #include "../librpc/gen_ndr/ndr_security.h"
23 #include "fake_file.h"
24 #include "../libcli/security/security.h"
26 #include "../libcli/smb/smbXcli_base.h"
28 NTSTATUS cli_get_quota_handle(struct cli_state *cli, uint16_t *quota_fnum)
30 return cli_ntcreate(cli, FAKE_FILE_NAME_QUOTA_WIN32,
31 0x00000016, DESIRED_ACCESS_PIPE,
32 0x00000000, FILE_SHARE_READ|FILE_SHARE_WRITE,
33 FILE_OPEN, 0x00000000, 0x03, quota_fnum, NULL);
36 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
38 if (!qt_list || !*qt_list) {
42 if ((*qt_list)->mem_ctx)
43 talloc_destroy((*qt_list)->mem_ctx);
50 bool parse_user_quota_record(const uint8_t *rdata,
51 unsigned int rdata_count,
53 SMB_NTQUOTA_STRUCT *pqt)
56 SMB_NTQUOTA_STRUCT qt;
60 if (!rdata||!offset||!pqt) {
61 smb_panic("parse_quota_record: called with NULL POINTER!");
64 if (rdata_count < 40) {
68 /* offset to next quota record.
69 * 4 bytes IVAL(rdata,0)
72 *offset = IVAL(rdata,0);
75 sid_len = IVAL(rdata,4);
76 if (40 + sid_len < 40) {
80 if (rdata_count < 40+sid_len) {
84 if (*offset != 0 && *offset < 40 + sid_len) {
88 /* unknown 8 bytes in pdata
89 * maybe its the change time in NTTIME
92 /* the used space 8 bytes (uint64_t)*/
93 qt.usedspace = BVAL(rdata,16);
95 /* the soft quotas 8 bytes (uint64_t)*/
96 qt.softlim = BVAL(rdata,24);
98 /* the hard quotas 8 bytes (uint64_t)*/
99 qt.hardlim = BVAL(rdata,32);
101 if (!sid_parse(rdata+40,sid_len,&qt.sid)) {
105 qt.qtype = SMB_USER_QUOTA_TYPE;
112 NTSTATUS parse_user_quota_list(const uint8_t *curdata,
113 uint32_t curdata_count,
115 SMB_NTQUOTA_LIST **pqt_list)
117 NTSTATUS status = NT_STATUS_OK;
119 SMB_NTQUOTA_STRUCT qt;
120 SMB_NTQUOTA_LIST *tmp_list_ent;
124 if (!parse_user_quota_record(curdata, curdata_count, &offset,
126 DEBUG(1, ("Failed to parse the quota record\n"));
127 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
131 if ((tmp_list_ent = talloc_zero(mem_ctx, SMB_NTQUOTA_LIST)) ==
133 status = NT_STATUS_NO_MEMORY;
137 if ((tmp_list_ent->quotas =
138 talloc_zero(mem_ctx, SMB_NTQUOTA_STRUCT)) == NULL) {
139 status = NT_STATUS_NO_MEMORY;
143 memcpy(tmp_list_ent->quotas, &qt, sizeof(qt));
144 tmp_list_ent->mem_ctx = mem_ctx;
146 DLIST_ADD((*pqt_list), tmp_list_ent);
148 if (offset > curdata_count) {
149 DEBUG(1, ("out of bounds offset in quota record\n"));
150 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
154 if (curdata + offset < curdata) {
155 DEBUG(1, ("Pointer overflow in quota record\n"));
156 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
161 curdata_count -= offset;
171 NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
172 SMB_NTQUOTA_STRUCT *pqt)
176 unsigned int data_len;
177 uint8_t data[SID_MAX_SIZE+8];
178 uint8_t *rparam, *rdata;
179 uint32_t rparam_count, rdata_count;
180 unsigned int sid_len;
185 smb_panic("cli_get_user_quota() called with NULL Pointer!");
188 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
189 return cli_smb2_get_user_quota(cli, quota_fnum, pqt);
192 SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
194 SSVAL(params, 0,quota_fnum);
195 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
196 SIVAL(params, 4,0x00000024);
197 SIVAL(params, 8,0x00000000);
198 SIVAL(params,12,0x00000024);
200 sid_len = ndr_size_dom_sid(&pqt->sid, 0);
201 data_len = sid_len+8;
202 SIVAL(data, 0, 0x00000000);
203 SIVAL(data, 4, sid_len);
204 sid_linearize(data+8, sid_len, &pqt->sid);
206 status = cli_trans(talloc_tos(), cli, SMBnttrans,
207 NULL, -1, /* name, fid */
208 NT_TRANSACT_GET_USER_QUOTA, 0,
209 setup, 1, 0, /* setup */
210 params, 16, 4, /* params */
211 data, data_len, 112, /* data */
212 NULL, /* recv_flags2 */
213 NULL, 0, NULL, /* rsetup */
214 &rparam, 4, &rparam_count,
215 &rdata, 8, &rdata_count);
216 if (!NT_STATUS_IS_OK(status)) {
217 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
222 if (!parse_user_quota_record(rdata, rdata_count, &offset, pqt)) {
223 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
224 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
232 NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
233 SMB_NTQUOTA_STRUCT *pqt)
238 unsigned int sid_len;
241 memset(data,'\0',112);
244 smb_panic("cli_set_user_quota() called with NULL Pointer!");
247 SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
249 SSVAL(params,0,quota_fnum);
251 sid_len = ndr_size_dom_sid(&pqt->sid, 0);
253 SIVAL(data,4,sid_len);
254 SBIG_UINT(data, 8,(uint64_t)0);
255 SBIG_UINT(data,16,pqt->usedspace);
256 SBIG_UINT(data,24,pqt->softlim);
257 SBIG_UINT(data,32,pqt->hardlim);
258 sid_linearize(data+40, sid_len, &pqt->sid);
260 status = cli_trans(talloc_tos(), cli, SMBnttrans,
261 NULL, -1, /* name, fid */
262 NT_TRANSACT_SET_USER_QUOTA, 0,
263 setup, 1, 0, /* setup */
264 params, 2, 0, /* params */
265 data, 112, 0, /* data */
266 NULL, /* recv_flags2 */
267 NULL, 0, NULL, /* rsetup */
268 NULL, 0, NULL, /* rparams */
269 NULL, 0, NULL); /* rdata */
271 if (!NT_STATUS_IS_OK(status)) {
272 DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
279 static NTSTATUS cli_list_user_quota_step(struct cli_state *cli,
282 SMB_NTQUOTA_LIST **pqt_list,
287 uint8_t *rparam=NULL, *rdata=NULL;
288 uint32_t rparam_count=0, rdata_count=0;
290 uint16_t op = first ? TRANSACT_GET_USER_QUOTA_LIST_START
291 : TRANSACT_GET_USER_QUOTA_LIST_CONTINUE;
293 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
294 return cli_smb2_list_user_quota_step(cli, mem_ctx, quota_fnum,
298 SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
300 SSVAL(params, 0,quota_fnum);
301 SSVAL(params, 2, op);
302 SIVAL(params, 4,0x00000000);
303 SIVAL(params, 8,0x00000000);
304 SIVAL(params,12,0x00000000);
306 status = cli_trans(talloc_tos(), cli, SMBnttrans,
307 NULL, -1, /* name, fid */
308 NT_TRANSACT_GET_USER_QUOTA, 0,
309 setup, 1, 0, /* setup */
310 params, 16, 4, /* params */
311 NULL, 0, 2048, /* data */
312 NULL, /* recv_flags2 */
313 NULL, 0, NULL, /* rsetup */
314 &rparam, 0, &rparam_count,
315 &rdata, 0, &rdata_count);
317 /* compat. with smbd + safeguard against
320 if (NT_STATUS_IS_OK(status) && rdata_count == 0) {
321 status = NT_STATUS_NO_MORE_ENTRIES;
324 if (!NT_STATUS_IS_OK(status)) {
328 status = parse_user_quota_list(rdata, rdata_count, mem_ctx, pqt_list);
337 NTSTATUS cli_list_user_quota(struct cli_state *cli,
339 SMB_NTQUOTA_LIST **pqt_list)
342 TALLOC_CTX *mem_ctx = NULL;
345 if (!cli || !pqt_list) {
346 smb_panic("cli_list_user_quota() called with NULL Pointer!");
351 if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
352 return NT_STATUS_NO_MEMORY;
356 status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
359 } while (NT_STATUS_IS_OK(status));
361 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
362 status = NT_STATUS_OK;
365 if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
366 TALLOC_FREE(mem_ctx);
372 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
373 SMB_NTQUOTA_STRUCT *pqt)
378 uint32_t rdata_count=0;
379 SMB_NTQUOTA_STRUCT qt;
385 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
388 SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
390 SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
392 status = cli_trans(talloc_tos(), cli, SMBtrans2,
393 NULL, -1, /* name, fid */
394 0, 0, /* function, flags */
395 setup, 1, 0, /* setup */
396 param, 2, 0, /* param */
397 NULL, 0, 560, /* data */
398 NULL, /* recv_flags2 */
399 NULL, 0, NULL, /* rsetup */
400 NULL, 0, NULL, /* rparam */
401 &rdata, 48, &rdata_count);
403 if (!NT_STATUS_IS_OK(status)) {
404 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
409 /* unknown_1 24 NULL bytes in pdata*/
411 /* the soft quotas 8 bytes (uint64_t)*/
412 qt.softlim = BVAL(rdata,24);
414 /* the hard quotas 8 bytes (uint64_t)*/
415 qt.hardlim = BVAL(rdata,32);
417 /* quota_flags 2 bytes **/
418 qt.qflags = SVAL(rdata,40);
420 qt.qtype = SMB_USER_FS_QUOTA_TYPE;
428 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
429 SMB_NTQUOTA_STRUCT *pqt)
434 SMB_NTQUOTA_STRUCT qt;
437 memset(data,'\0',48);
440 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
443 SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
445 SSVAL(param,0,quota_fnum);
446 SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
448 /* Unknown1 24 NULL bytes*/
450 /* Default Soft Quota 8 bytes */
451 SBIG_UINT(data,24,pqt->softlim);
453 /* Default Hard Quota 8 bytes */
454 SBIG_UINT(data,32,pqt->hardlim);
456 /* Quota flag 2 bytes */
457 SSVAL(data,40,pqt->qflags);
459 /* Unknown3 6 NULL bytes */
461 status = cli_trans(talloc_tos(), cli, SMBtrans2,
462 NULL, -1, /* name, fid */
463 0, 0, /* function, flags */
464 setup, 1, 0, /* setup */
465 param, 4, 0, /* param */
466 data, 48, 0, /* data */
467 NULL, /* recv_flags2 */
468 NULL, 0, NULL, /* rsetup */
469 NULL, 0, NULL, /* rparam */
470 NULL, 0, NULL); /* rdata */
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",