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"
27 NTSTATUS cli_get_quota_handle(struct cli_state *cli, uint16_t *quota_fnum)
29 return cli_ntcreate(cli, FAKE_FILE_NAME_QUOTA_WIN32,
30 0x00000016, DESIRED_ACCESS_PIPE,
31 0x00000000, FILE_SHARE_READ|FILE_SHARE_WRITE,
32 FILE_OPEN, 0x00000000, 0x03, quota_fnum, NULL);
35 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
40 if ((*qt_list)->mem_ctx)
41 talloc_destroy((*qt_list)->mem_ctx);
48 static bool parse_user_quota_record(const uint8_t *rdata,
49 unsigned int rdata_count,
51 SMB_NTQUOTA_STRUCT *pqt)
54 SMB_NTQUOTA_STRUCT qt;
58 if (!rdata||!offset||!pqt) {
59 smb_panic("parse_quota_record: called with NULL POINTER!");
62 if (rdata_count < 40) {
66 /* offset to next quota record.
67 * 4 bytes IVAL(rdata,0)
70 *offset = IVAL(rdata,0);
73 sid_len = IVAL(rdata,4);
75 if (rdata_count < 40+sid_len) {
79 /* unknown 8 bytes in pdata
80 * maybe its the change time in NTTIME
83 /* the used space 8 bytes (uint64_t)*/
84 qt.usedspace = BVAL(rdata,16);
86 /* the soft quotas 8 bytes (uint64_t)*/
87 qt.softlim = BVAL(rdata,24);
89 /* the hard quotas 8 bytes (uint64_t)*/
90 qt.hardlim = BVAL(rdata,32);
92 if (!sid_parse(rdata+40,sid_len,&qt.sid)) {
96 qt.qtype = SMB_USER_QUOTA_TYPE;
103 NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
104 SMB_NTQUOTA_STRUCT *pqt)
108 unsigned int data_len;
109 uint8_t data[SID_MAX_SIZE+8];
110 uint8_t *rparam, *rdata;
111 uint32_t rparam_count, rdata_count;
112 unsigned int sid_len;
117 smb_panic("cli_get_user_quota() called with NULL Pointer!");
120 SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
122 SSVAL(params, 0,quota_fnum);
123 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
124 SIVAL(params, 4,0x00000024);
125 SIVAL(params, 8,0x00000000);
126 SIVAL(params,12,0x00000024);
128 sid_len = ndr_size_dom_sid(&pqt->sid, 0);
129 data_len = sid_len+8;
130 SIVAL(data, 0, 0x00000000);
131 SIVAL(data, 4, sid_len);
132 sid_linearize(data+8, sid_len, &pqt->sid);
134 status = cli_trans(talloc_tos(), cli, SMBnttrans,
135 NULL, -1, /* name, fid */
136 NT_TRANSACT_GET_USER_QUOTA, 0,
137 setup, 1, 0, /* setup */
138 params, 16, 4, /* params */
139 data, data_len, 112, /* data */
140 NULL, /* recv_flags2 */
141 NULL, 0, NULL, /* rsetup */
142 &rparam, 4, &rparam_count,
143 &rdata, 8, &rdata_count);
144 if (!NT_STATUS_IS_OK(status)) {
145 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
150 if (!parse_user_quota_record(rdata, rdata_count, &offset, pqt)) {
151 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
152 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
160 NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
161 SMB_NTQUOTA_STRUCT *pqt)
166 unsigned int sid_len;
169 memset(data,'\0',112);
172 smb_panic("cli_set_user_quota() called with NULL Pointer!");
175 SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
177 SSVAL(params,0,quota_fnum);
179 sid_len = ndr_size_dom_sid(&pqt->sid, 0);
181 SIVAL(data,4,sid_len);
182 SBIG_UINT(data, 8,(uint64_t)0);
183 SBIG_UINT(data,16,pqt->usedspace);
184 SBIG_UINT(data,24,pqt->softlim);
185 SBIG_UINT(data,32,pqt->hardlim);
186 sid_linearize(data+40, sid_len, &pqt->sid);
188 status = cli_trans(talloc_tos(), cli, SMBnttrans,
189 NULL, -1, /* name, fid */
190 NT_TRANSACT_SET_USER_QUOTA, 0,
191 setup, 1, 0, /* setup */
192 params, 2, 0, /* params */
193 data, 112, 0, /* data */
194 NULL, /* recv_flags2 */
195 NULL, 0, NULL, /* rsetup */
196 NULL, 0, NULL, /* rparams */
197 NULL, 0, NULL); /* rdata */
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
207 NTSTATUS cli_list_user_quota(struct cli_state *cli, int quota_fnum,
208 SMB_NTQUOTA_LIST **pqt_list)
212 uint8_t *rparam=NULL, *rdata=NULL;
213 uint32_t rparam_count=0, rdata_count=0;
215 const uint8_t *curdata = NULL;
216 unsigned int curdata_count = 0;
217 TALLOC_CTX *mem_ctx = NULL;
218 SMB_NTQUOTA_STRUCT qt;
219 SMB_NTQUOTA_LIST *tmp_list_ent;
222 if (!cli||!pqt_list) {
223 smb_panic("cli_list_user_quota() called with NULL Pointer!");
226 SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
228 SSVAL(params, 0,quota_fnum);
229 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_START);
230 SIVAL(params, 4,0x00000000);
231 SIVAL(params, 8,0x00000000);
232 SIVAL(params,12,0x00000000);
234 status = cli_trans(talloc_tos(), cli, SMBnttrans,
235 NULL, -1, /* name, fid */
236 NT_TRANSACT_GET_USER_QUOTA, 0,
237 setup, 1, 0, /* setup */
238 params, 16, 4, /* params */
239 NULL, 0, 2048, /* data */
240 NULL, /* recv_flags2 */
241 NULL, 0, NULL, /* rsetup */
242 &rparam, 0, &rparam_count,
243 &rdata, 0, &rdata_count);
245 if (!NT_STATUS_IS_OK(status) &&
246 !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
247 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
252 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) ||
258 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
259 DEBUG(0,("talloc_init() failed\n"));
260 return NT_STATUS_NO_MEMORY;
264 for (curdata=rdata,curdata_count=rdata_count;
265 ((curdata)&&(curdata_count>=8)&&(offset>0));
266 curdata +=offset,curdata_count -= offset) {
268 if (!parse_user_quota_record((const uint8_t *)curdata, curdata_count,
270 DEBUG(1,("Failed to parse the quota record\n"));
274 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
275 DEBUG(0,("TALLOC_ZERO() failed\n"));
276 talloc_destroy(mem_ctx);
277 return NT_STATUS_NO_MEMORY;
280 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
281 DEBUG(0,("TALLOC_ZERO() failed\n"));
282 talloc_destroy(mem_ctx);
283 return NT_STATUS_NO_MEMORY;
286 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
287 tmp_list_ent->mem_ctx = mem_ctx;
289 DLIST_ADD((*pqt_list),tmp_list_ent);
292 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE);
298 status = cli_trans(talloc_tos(), cli, SMBnttrans,
299 NULL, -1, /* name, fid */
300 NT_TRANSACT_GET_USER_QUOTA, 0,
301 setup, 1, 0, /* setup */
302 params, 16, 4, /* params */
303 NULL, 0, 2048, /* data */
304 NULL, /* recv_flags2 */
305 NULL, 0, NULL, /* rsetup */
306 &rparam, 0, &rparam_count,
307 &rdata, 0, &rdata_count);
309 if (!NT_STATUS_IS_OK(status) &&
310 !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
311 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
316 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) ||
318 status = NT_STATUS_OK;
323 for (curdata=rdata,curdata_count=rdata_count;
324 ((curdata)&&(curdata_count>=8)&&(offset>0));
325 curdata +=offset,curdata_count -= offset) {
327 if (!parse_user_quota_record((const uint8_t *)curdata,
328 curdata_count, &offset,
330 DEBUG(1,("Failed to parse the quota record\n"));
334 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
335 DEBUG(0,("TALLOC_ZERO() failed\n"));
336 talloc_destroy(mem_ctx);
340 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
341 DEBUG(0,("TALLOC_ZERO() failed\n"));
342 talloc_destroy(mem_ctx);
346 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
347 tmp_list_ent->mem_ctx = mem_ctx;
349 DLIST_ADD((*pqt_list),tmp_list_ent);
360 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
361 SMB_NTQUOTA_STRUCT *pqt)
366 uint32_t rdata_count=0;
367 SMB_NTQUOTA_STRUCT qt;
373 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
376 SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
378 SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
380 status = cli_trans(talloc_tos(), cli, SMBtrans2,
381 NULL, -1, /* name, fid */
382 0, 0, /* function, flags */
383 setup, 1, 0, /* setup */
384 param, 2, 0, /* param */
385 NULL, 0, 560, /* data */
386 NULL, /* recv_flags2 */
387 NULL, 0, NULL, /* rsetup */
388 NULL, 0, NULL, /* rparam */
389 &rdata, 48, &rdata_count);
391 if (!NT_STATUS_IS_OK(status)) {
392 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
397 /* unknown_1 24 NULL bytes in pdata*/
399 /* the soft quotas 8 bytes (uint64_t)*/
400 qt.softlim = BVAL(rdata,24);
402 /* the hard quotas 8 bytes (uint64_t)*/
403 qt.hardlim = BVAL(rdata,32);
405 /* quota_flags 2 bytes **/
406 qt.qflags = SVAL(rdata,40);
408 qt.qtype = SMB_USER_FS_QUOTA_TYPE;
416 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
417 SMB_NTQUOTA_STRUCT *pqt)
422 SMB_NTQUOTA_STRUCT qt;
425 memset(data,'\0',48);
428 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
431 SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
433 SSVAL(param,0,quota_fnum);
434 SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
436 /* Unknown1 24 NULL bytes*/
438 /* Default Soft Quota 8 bytes */
439 SBIG_UINT(data,24,pqt->softlim);
441 /* Default Hard Quota 8 bytes */
442 SBIG_UINT(data,32,pqt->hardlim);
444 /* Quota flag 2 bytes */
445 SSVAL(data,40,pqt->qflags);
447 /* Unknown3 6 NULL bytes */
449 status = cli_trans(talloc_tos(), cli, SMBtrans2,
450 NULL, -1, /* name, fid */
451 0, 0, /* function, flags */
452 setup, 1, 0, /* setup */
453 param, 8, 0, /* param */
454 data, 48, 0, /* data */
455 NULL, /* recv_flags2 */
456 NULL, 0, NULL, /* rsetup */
457 NULL, 0, NULL, /* rparam */
458 NULL, 0, NULL); /* rdata */
460 if (!NT_STATUS_IS_OK(status)) {
461 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",