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 add_record_to_ntquota_list(TALLOC_CTX *mem_ctx,
51 SMB_NTQUOTA_STRUCT *pqt,
52 SMB_NTQUOTA_LIST **pqt_list)
54 SMB_NTQUOTA_LIST *tmp_list_ent;
56 if ((tmp_list_ent = talloc_zero(mem_ctx, SMB_NTQUOTA_LIST)) == NULL) {
60 if ((tmp_list_ent->quotas = talloc_zero(mem_ctx, SMB_NTQUOTA_STRUCT)) ==
65 *tmp_list_ent->quotas = *pqt;
66 tmp_list_ent->mem_ctx = mem_ctx;
68 DLIST_ADD((*pqt_list), tmp_list_ent);
73 bool parse_user_quota_record(const uint8_t *rdata,
74 unsigned int rdata_count,
76 SMB_NTQUOTA_STRUCT *pqt)
79 SMB_NTQUOTA_STRUCT qt;
83 if (!rdata||!offset||!pqt) {
84 smb_panic("parse_quota_record: called with NULL POINTER!");
87 if (rdata_count < 40) {
91 /* offset to next quota record.
92 * 4 bytes IVAL(rdata,0)
95 *offset = IVAL(rdata,0);
98 sid_len = IVAL(rdata,4);
99 if (40 + sid_len < 40) {
103 if (rdata_count < 40+sid_len) {
107 if (*offset != 0 && *offset < 40 + sid_len) {
111 /* unknown 8 bytes in pdata
112 * maybe its the change time in NTTIME
115 /* the used space 8 bytes (uint64_t)*/
116 qt.usedspace = BVAL(rdata,16);
118 /* the soft quotas 8 bytes (uint64_t)*/
119 qt.softlim = BVAL(rdata,24);
121 /* the hard quotas 8 bytes (uint64_t)*/
122 qt.hardlim = BVAL(rdata,32);
124 if (!sid_parse(rdata+40,sid_len,&qt.sid)) {
128 qt.qtype = SMB_USER_QUOTA_TYPE;
135 NTSTATUS parse_user_quota_list(const uint8_t *curdata,
136 uint32_t curdata_count,
138 SMB_NTQUOTA_LIST **pqt_list)
140 NTSTATUS status = NT_STATUS_OK;
142 SMB_NTQUOTA_STRUCT qt;
146 if (!parse_user_quota_record(curdata, curdata_count, &offset,
148 DEBUG(1, ("Failed to parse the quota record\n"));
149 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
153 if (offset > curdata_count) {
154 DEBUG(1, ("out of bounds offset in quota record\n"));
155 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
159 if (curdata + offset < curdata) {
160 DEBUG(1, ("Pointer overflow in quota record\n"));
161 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
165 if (!add_record_to_ntquota_list(mem_ctx, &qt, pqt_list)) {
166 status = NT_STATUS_NO_MEMORY;
171 curdata_count -= offset;
181 NTSTATUS parse_fs_quota_buffer(const uint8_t *rdata,
182 unsigned int rdata_count,
183 SMB_NTQUOTA_STRUCT *pqt)
185 SMB_NTQUOTA_STRUCT qt;
189 if (rdata_count < 48) {
190 /* minimum length is not enforced by SMB2 client.
192 DEBUG(1, ("small returned fs quota buffer\n"));
193 return NT_STATUS_INVALID_NETWORK_RESPONSE;
196 /* unknown_1 24 NULL bytes in pdata*/
198 /* the soft quotas 8 bytes (uint64_t)*/
199 qt.softlim = BVAL(rdata, 24);
201 /* the hard quotas 8 bytes (uint64_t)*/
202 qt.hardlim = BVAL(rdata, 32);
204 /* quota_flags 2 bytes **/
205 qt.qflags = SVAL(rdata, 40);
207 qt.qtype = SMB_USER_FS_QUOTA_TYPE;
214 NTSTATUS build_user_quota_buffer(SMB_NTQUOTA_LIST *qt_list,
218 SMB_NTQUOTA_LIST **end_ptr)
224 SMB_NTQUOTA_LIST *qtl;
225 DATA_BLOB qbuf = data_blob_null;
226 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
228 if (qt_list == NULL) {
229 status = NT_STATUS_OK;
230 *outbuf = data_blob_null;
237 for (qtl = qt_list; qtl != NULL; qtl = qtl->next) {
239 sid_len = ndr_size_dom_sid(&qtl->quotas->sid, 0);
240 if (47 + sid_len < 47) {
241 status = NT_STATUS_INVALID_PARAMETER;
244 entry_len = 40 + sid_len;
245 entry_len = ((entry_len + 7) / 8) * 8;
247 if (qt_len + entry_len < qt_len) {
248 status = NT_STATUS_INVALID_PARAMETER;
254 if (maxlen > 0 && qt_len > maxlen) {
258 qbuf = data_blob_talloc_zero(mem_ctx, qt_len);
259 if (qbuf.data == NULL) {
260 status = NT_STATUS_NO_MEMORY;
264 for (qt_len = 0, entry = qbuf.data; qt_list != NULL;
265 qt_list = qt_list->next, qt_len += entry_len, entry += entry_len) {
267 sid_len = ndr_size_dom_sid(&qt_list->quotas->sid, 0);
268 entry_len = 40 + sid_len;
269 entry_len = ((entry_len + 7) / 8) * 8;
271 if (qt_len + entry_len > qbuf.length) {
272 /* check for not-enough room even for a single
276 status = NT_STATUS_BUFFER_TOO_SMALL;
283 /* nextoffset entry 4 bytes */
284 SIVAL(entry, 0, entry_len);
286 /* then the len of the SID 4 bytes */
287 SIVAL(entry, 4, sid_len);
289 /* NTTIME of last record change */
290 SBIG_UINT(entry, 8, (uint64_t)0);
292 /* the used disk space 8 bytes uint64_t */
293 SBIG_UINT(entry, 16, qt_list->quotas->usedspace);
295 /* the soft quotas 8 bytes uint64_t */
296 SBIG_UINT(entry, 24, qt_list->quotas->softlim);
298 /* the hard quotas 8 bytes uint64_t */
299 SBIG_UINT(entry, 32, qt_list->quotas->hardlim);
301 /* and now the SID */
302 sid_linearize((uint8_t *)(entry + 40), sid_len,
303 &qt_list->quotas->sid);
306 /* overwrite the offset of the last entry */
307 SIVAL(entry - entry_len, 0, 0);
309 /*potentially shrink the buffer if max was given
310 * and we haven't quite reached the max
312 qbuf.length = qt_len;
314 qbuf = data_blob_null;
315 status = NT_STATUS_OK;
322 data_blob_free(&qbuf);
327 NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
328 SMB_NTQUOTA_STRUCT *pqt)
332 unsigned int data_len;
333 uint8_t data[SID_MAX_SIZE+8];
334 uint8_t *rparam, *rdata;
335 uint32_t rparam_count, rdata_count;
336 unsigned int sid_len;
341 smb_panic("cli_get_user_quota() called with NULL Pointer!");
344 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
345 return cli_smb2_get_user_quota(cli, quota_fnum, pqt);
348 SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
350 SSVAL(params, 0,quota_fnum);
351 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
352 SIVAL(params, 4,0x00000024);
353 SIVAL(params, 8,0x00000000);
354 SIVAL(params,12,0x00000024);
356 sid_len = ndr_size_dom_sid(&pqt->sid, 0);
357 data_len = sid_len+8;
358 SIVAL(data, 0, 0x00000000);
359 SIVAL(data, 4, sid_len);
360 sid_linearize(data+8, sid_len, &pqt->sid);
362 status = cli_trans(talloc_tos(), cli, SMBnttrans,
363 NULL, -1, /* name, fid */
364 NT_TRANSACT_GET_USER_QUOTA, 0,
365 setup, 1, 0, /* setup */
366 params, 16, 4, /* params */
367 data, data_len, 112, /* data */
368 NULL, /* recv_flags2 */
369 NULL, 0, NULL, /* rsetup */
370 &rparam, 4, &rparam_count,
371 &rdata, 8, &rdata_count);
372 if (!NT_STATUS_IS_OK(status)) {
373 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
378 if (!parse_user_quota_record(rdata, rdata_count, &offset, pqt)) {
379 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
380 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
389 cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST *qtl)
393 DATA_BLOB data = data_blob_null;
394 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
397 smb_panic("cli_set_user_quota() called with NULL Pointer!");
400 status = build_user_quota_buffer(qtl, 0, talloc_tos(), &data, NULL);
401 if (!NT_STATUS_IS_OK(status)) {
405 SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
407 SSVAL(params,0,quota_fnum);
409 status = cli_trans(talloc_tos(), cli, SMBnttrans,
410 NULL, -1, /* name, fid */
411 NT_TRANSACT_SET_USER_QUOTA, 0,
412 setup, 1, 0, /* setup */
413 params, 2, 0, /* params */
414 data.data, data.length, 0, /* data */
415 NULL, /* recv_flags2 */
416 NULL, 0, NULL, /* rsetup */
417 NULL, 0, NULL, /* rparams */
418 NULL, 0, NULL); /* rdata */
420 if (!NT_STATUS_IS_OK(status)) {
421 DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
426 data_blob_free(&data);
430 static NTSTATUS cli_list_user_quota_step(struct cli_state *cli,
433 SMB_NTQUOTA_LIST **pqt_list,
438 uint8_t *rparam=NULL, *rdata=NULL;
439 uint32_t rparam_count=0, rdata_count=0;
441 uint16_t op = first ? TRANSACT_GET_USER_QUOTA_LIST_START
442 : TRANSACT_GET_USER_QUOTA_LIST_CONTINUE;
444 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
445 return cli_smb2_list_user_quota_step(cli, mem_ctx, quota_fnum,
449 SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
451 SSVAL(params, 0,quota_fnum);
452 SSVAL(params, 2, op);
453 SIVAL(params, 4,0x00000000);
454 SIVAL(params, 8,0x00000000);
455 SIVAL(params,12,0x00000000);
457 status = cli_trans(talloc_tos(), cli, SMBnttrans,
458 NULL, -1, /* name, fid */
459 NT_TRANSACT_GET_USER_QUOTA, 0,
460 setup, 1, 0, /* setup */
461 params, 16, 4, /* params */
462 NULL, 0, 2048, /* data */
463 NULL, /* recv_flags2 */
464 NULL, 0, NULL, /* rsetup */
465 &rparam, 0, &rparam_count,
466 &rdata, 0, &rdata_count);
468 /* compat. with smbd + safeguard against
471 if (NT_STATUS_IS_OK(status) && rdata_count == 0) {
472 status = NT_STATUS_NO_MORE_ENTRIES;
475 if (!NT_STATUS_IS_OK(status)) {
479 status = parse_user_quota_list(rdata, rdata_count, mem_ctx, pqt_list);
488 NTSTATUS cli_list_user_quota(struct cli_state *cli,
490 SMB_NTQUOTA_LIST **pqt_list)
493 TALLOC_CTX *mem_ctx = NULL;
496 if (!cli || !pqt_list) {
497 smb_panic("cli_list_user_quota() called with NULL Pointer!");
502 if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
503 return NT_STATUS_NO_MEMORY;
507 status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
510 } while (NT_STATUS_IS_OK(status));
512 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
513 status = NT_STATUS_OK;
516 if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
517 TALLOC_FREE(mem_ctx);
523 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
524 SMB_NTQUOTA_STRUCT *pqt)
529 uint32_t rdata_count=0;
533 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
536 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
537 return cli_smb2_get_fs_quota_info(cli, quota_fnum, pqt);
540 SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
542 SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
544 status = cli_trans(talloc_tos(), cli, SMBtrans2,
545 NULL, -1, /* name, fid */
546 0, 0, /* function, flags */
547 setup, 1, 0, /* setup */
548 param, 2, 0, /* param */
549 NULL, 0, 560, /* data */
550 NULL, /* recv_flags2 */
551 NULL, 0, NULL, /* rsetup */
552 NULL, 0, NULL, /* rparam */
553 &rdata, 48, &rdata_count);
555 if (!NT_STATUS_IS_OK(status)) {
556 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
561 status = parse_fs_quota_buffer(rdata, rdata_count, pqt);
567 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
568 SMB_NTQUOTA_STRUCT *pqt)
573 SMB_NTQUOTA_STRUCT qt;
576 memset(data,'\0',48);
579 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
582 SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
584 SSVAL(param,0,quota_fnum);
585 SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
587 /* Unknown1 24 NULL bytes*/
589 /* Default Soft Quota 8 bytes */
590 SBIG_UINT(data,24,pqt->softlim);
592 /* Default Hard Quota 8 bytes */
593 SBIG_UINT(data,32,pqt->hardlim);
595 /* Quota flag 2 bytes */
596 SSVAL(data,40,pqt->qflags);
598 /* Unknown3 6 NULL bytes */
600 status = cli_trans(talloc_tos(), cli, SMBtrans2,
601 NULL, -1, /* name, fid */
602 0, 0, /* function, flags */
603 setup, 1, 0, /* setup */
604 param, 4, 0, /* param */
605 data, 48, 0, /* data */
606 NULL, /* recv_flags2 */
607 NULL, 0, NULL, /* rsetup */
608 NULL, 0, NULL, /* rparam */
609 NULL, 0, NULL); /* rdata */
611 if (!NT_STATUS_IS_OK(status)) {
612 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",