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 SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
295 SSVAL(params, 0,quota_fnum);
296 SSVAL(params, 2, op);
297 SIVAL(params, 4,0x00000000);
298 SIVAL(params, 8,0x00000000);
299 SIVAL(params,12,0x00000000);
301 status = cli_trans(talloc_tos(), cli, SMBnttrans,
302 NULL, -1, /* name, fid */
303 NT_TRANSACT_GET_USER_QUOTA, 0,
304 setup, 1, 0, /* setup */
305 params, 16, 4, /* params */
306 NULL, 0, 2048, /* data */
307 NULL, /* recv_flags2 */
308 NULL, 0, NULL, /* rsetup */
309 &rparam, 0, &rparam_count,
310 &rdata, 0, &rdata_count);
312 /* compat. with smbd + safeguard against
315 if (NT_STATUS_IS_OK(status) && rdata_count == 0) {
316 status = NT_STATUS_NO_MORE_ENTRIES;
319 if (!NT_STATUS_IS_OK(status)) {
323 status = parse_user_quota_list(rdata, rdata_count, mem_ctx, pqt_list);
332 NTSTATUS cli_list_user_quota(struct cli_state *cli,
334 SMB_NTQUOTA_LIST **pqt_list)
337 TALLOC_CTX *mem_ctx = NULL;
340 if (!cli || !pqt_list) {
341 smb_panic("cli_list_user_quota() called with NULL Pointer!");
346 if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
347 return NT_STATUS_NO_MEMORY;
351 status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
354 } while (NT_STATUS_IS_OK(status));
356 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
357 status = NT_STATUS_OK;
360 if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
361 TALLOC_FREE(mem_ctx);
367 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
368 SMB_NTQUOTA_STRUCT *pqt)
373 uint32_t rdata_count=0;
374 SMB_NTQUOTA_STRUCT qt;
380 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
383 SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
385 SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
387 status = cli_trans(talloc_tos(), cli, SMBtrans2,
388 NULL, -1, /* name, fid */
389 0, 0, /* function, flags */
390 setup, 1, 0, /* setup */
391 param, 2, 0, /* param */
392 NULL, 0, 560, /* data */
393 NULL, /* recv_flags2 */
394 NULL, 0, NULL, /* rsetup */
395 NULL, 0, NULL, /* rparam */
396 &rdata, 48, &rdata_count);
398 if (!NT_STATUS_IS_OK(status)) {
399 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
404 /* unknown_1 24 NULL bytes in pdata*/
406 /* the soft quotas 8 bytes (uint64_t)*/
407 qt.softlim = BVAL(rdata,24);
409 /* the hard quotas 8 bytes (uint64_t)*/
410 qt.hardlim = BVAL(rdata,32);
412 /* quota_flags 2 bytes **/
413 qt.qflags = SVAL(rdata,40);
415 qt.qtype = SMB_USER_FS_QUOTA_TYPE;
423 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
424 SMB_NTQUOTA_STRUCT *pqt)
429 SMB_NTQUOTA_STRUCT qt;
432 memset(data,'\0',48);
435 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
438 SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
440 SSVAL(param,0,quota_fnum);
441 SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
443 /* Unknown1 24 NULL bytes*/
445 /* Default Soft Quota 8 bytes */
446 SBIG_UINT(data,24,pqt->softlim);
448 /* Default Hard Quota 8 bytes */
449 SBIG_UINT(data,32,pqt->hardlim);
451 /* Quota flag 2 bytes */
452 SSVAL(data,40,pqt->qflags);
454 /* Unknown3 6 NULL bytes */
456 status = cli_trans(talloc_tos(), cli, SMBtrans2,
457 NULL, -1, /* name, fid */
458 0, 0, /* function, flags */
459 setup, 1, 0, /* setup */
460 param, 4, 0, /* param */
461 data, 48, 0, /* data */
462 NULL, /* recv_flags2 */
463 NULL, 0, NULL, /* rsetup */
464 NULL, 0, NULL, /* rparam */
465 NULL, 0, NULL); /* rdata */
467 if (!NT_STATUS_IS_OK(status)) {
468 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",