Fix bug #8509 - Read-only handles on SAMR allow SAMR_DOMAIN_ACCESS_CREATE_USER.
[Samba/id10ts.git] / source3 / libsmb / cliquota.c
blob9136506e48c2307b3188928bedf3669a13ed7665
1 /*
2 Unix SMB/CIFS implementation.
3 client quota functions
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/>.
20 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "../librpc/gen_ndr/ndr_security.h"
23 #include "fake_file.h"
24 #include "../libcli/security/security.h"
25 #include "trans2.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);
35 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
37 if (!qt_list)
38 return;
40 if ((*qt_list)->mem_ctx)
41 talloc_destroy((*qt_list)->mem_ctx);
43 (*qt_list) = NULL;
45 return;
48 static bool parse_user_quota_record(const uint8_t *rdata,
49 unsigned int rdata_count,
50 unsigned int *offset,
51 SMB_NTQUOTA_STRUCT *pqt)
53 int sid_len;
54 SMB_NTQUOTA_STRUCT qt;
56 ZERO_STRUCT(qt);
58 if (!rdata||!offset||!pqt) {
59 smb_panic("parse_quota_record: called with NULL POINTER!");
62 if (rdata_count < 40) {
63 return False;
66 /* offset to next quota record.
67 * 4 bytes IVAL(rdata,0)
68 * unused here...
70 *offset = IVAL(rdata,0);
72 /* sid len */
73 sid_len = IVAL(rdata,4);
75 if (rdata_count < 40+sid_len) {
76 return False;
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((const char *)rdata+40,sid_len,&qt.sid)) {
93 return false;
96 qt.qtype = SMB_USER_QUOTA_TYPE;
98 *pqt = qt;
100 return True;
103 NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
104 SMB_NTQUOTA_STRUCT *pqt)
106 uint16_t setup[1];
107 uint8_t params[16];
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;
113 unsigned int offset;
114 NTSTATUS status;
116 if (!cli||!pqt) {
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((char *)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",
146 nt_errstr(status)));
147 return status;
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"));
155 TALLOC_FREE(rparam);
156 TALLOC_FREE(rdata);
157 return status;
160 NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
161 SMB_NTQUOTA_STRUCT *pqt)
163 uint16_t setup[1];
164 uint8_t params[2];
165 uint8_t data[112];
166 unsigned int sid_len;
167 NTSTATUS status;
169 memset(data,'\0',112);
171 if (!cli||!pqt) {
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);
180 SIVAL(data,0,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((char *)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",
201 nt_errstr(status)));
204 return status;
207 NTSTATUS cli_list_user_quota(struct cli_state *cli, int quota_fnum,
208 SMB_NTQUOTA_LIST **pqt_list)
210 uint16_t setup[1];
211 uint8_t params[16];
212 uint8_t *rparam=NULL, *rdata=NULL;
213 uint32_t rparam_count=0, rdata_count=0;
214 unsigned int offset;
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;
220 NTSTATUS status;
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 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
247 nt_errstr(status)));
248 goto cleanup;
251 if (rdata_count == 0) {
252 *pqt_list = NULL;
253 return NT_STATUS_OK;
256 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
257 DEBUG(0,("talloc_init() failed\n"));
258 return NT_STATUS_NO_MEMORY;
261 offset = 1;
262 for (curdata=rdata,curdata_count=rdata_count;
263 ((curdata)&&(curdata_count>=8)&&(offset>0));
264 curdata +=offset,curdata_count -= offset) {
265 ZERO_STRUCT(qt);
266 if (!parse_user_quota_record((const uint8_t *)curdata, curdata_count,
267 &offset, &qt)) {
268 DEBUG(1,("Failed to parse the quota record\n"));
269 goto cleanup;
272 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
273 DEBUG(0,("TALLOC_ZERO() failed\n"));
274 talloc_destroy(mem_ctx);
275 return NT_STATUS_NO_MEMORY;
278 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
279 DEBUG(0,("TALLOC_ZERO() failed\n"));
280 talloc_destroy(mem_ctx);
281 return NT_STATUS_NO_MEMORY;
284 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
285 tmp_list_ent->mem_ctx = mem_ctx;
287 DLIST_ADD((*pqt_list),tmp_list_ent);
290 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE);
291 while(1) {
293 TALLOC_FREE(rparam);
294 TALLOC_FREE(rdata);
296 status = cli_trans(talloc_tos(), cli, SMBnttrans,
297 NULL, -1, /* name, fid */
298 NT_TRANSACT_GET_USER_QUOTA, 0,
299 setup, 1, 0, /* setup */
300 params, 16, 4, /* params */
301 NULL, 0, 2048, /* data */
302 NULL, /* recv_flags2 */
303 NULL, 0, NULL, /* rsetup */
304 &rparam, 0, &rparam_count,
305 &rdata, 0, &rdata_count);
307 if (!NT_STATUS_IS_OK(status)) {
308 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
309 nt_errstr(status)));
310 goto cleanup;
313 if (rdata_count == 0) {
314 break;
317 offset = 1;
318 for (curdata=rdata,curdata_count=rdata_count;
319 ((curdata)&&(curdata_count>=8)&&(offset>0));
320 curdata +=offset,curdata_count -= offset) {
321 ZERO_STRUCT(qt);
322 if (!parse_user_quota_record((const uint8_t *)curdata,
323 curdata_count, &offset,
324 &qt)) {
325 DEBUG(1,("Failed to parse the quota record\n"));
326 goto cleanup;
329 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
330 DEBUG(0,("TALLOC_ZERO() failed\n"));
331 talloc_destroy(mem_ctx);
332 goto cleanup;
335 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
336 DEBUG(0,("TALLOC_ZERO() failed\n"));
337 talloc_destroy(mem_ctx);
338 goto cleanup;
341 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
342 tmp_list_ent->mem_ctx = mem_ctx;
344 DLIST_ADD((*pqt_list),tmp_list_ent);
348 cleanup:
349 TALLOC_FREE(rparam);
350 TALLOC_FREE(rdata);
352 return status;
355 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
356 SMB_NTQUOTA_STRUCT *pqt)
358 uint16_t setup[1];
359 uint8_t param[2];
360 uint8_t *rdata=NULL;
361 uint32_t rdata_count=0;
362 SMB_NTQUOTA_STRUCT qt;
363 NTSTATUS status;
365 ZERO_STRUCT(qt);
367 if (!cli||!pqt) {
368 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
371 SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
373 SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
375 status = cli_trans(talloc_tos(), cli, SMBtrans2,
376 NULL, -1, /* name, fid */
377 0, 0, /* function, flags */
378 setup, 1, 0, /* setup */
379 param, 2, 0, /* param */
380 NULL, 0, 560, /* data */
381 NULL, /* recv_flags2 */
382 NULL, 0, NULL, /* rsetup */
383 NULL, 0, NULL, /* rparam */
384 &rdata, 48, &rdata_count);
386 if (!NT_STATUS_IS_OK(status)) {
387 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
388 nt_errstr(status)));
389 return status;
392 /* unknown_1 24 NULL bytes in pdata*/
394 /* the soft quotas 8 bytes (uint64_t)*/
395 qt.softlim = BVAL(rdata,24);
397 /* the hard quotas 8 bytes (uint64_t)*/
398 qt.hardlim = BVAL(rdata,32);
400 /* quota_flags 2 bytes **/
401 qt.qflags = SVAL(rdata,40);
403 qt.qtype = SMB_USER_FS_QUOTA_TYPE;
405 *pqt = qt;
407 TALLOC_FREE(rdata);
408 return status;
411 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
412 SMB_NTQUOTA_STRUCT *pqt)
414 uint16_t setup[1];
415 uint8_t param[4];
416 uint8_t data[48];
417 SMB_NTQUOTA_STRUCT qt;
418 NTSTATUS status;
419 ZERO_STRUCT(qt);
420 memset(data,'\0',48);
422 if (!cli||!pqt) {
423 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
426 SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
428 SSVAL(param,0,quota_fnum);
429 SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
431 /* Unknown1 24 NULL bytes*/
433 /* Default Soft Quota 8 bytes */
434 SBIG_UINT(data,24,pqt->softlim);
436 /* Default Hard Quota 8 bytes */
437 SBIG_UINT(data,32,pqt->hardlim);
439 /* Quota flag 2 bytes */
440 SSVAL(data,40,pqt->qflags);
442 /* Unknown3 6 NULL bytes */
444 status = cli_trans(talloc_tos(), cli, SMBtrans2,
445 NULL, -1, /* name, fid */
446 0, 0, /* function, flags */
447 setup, 1, 0, /* setup */
448 param, 8, 0, /* param */
449 data, 48, 0, /* data */
450 NULL, /* recv_flags2 */
451 NULL, 0, NULL, /* rsetup */
452 NULL, 0, NULL, /* rparam */
453 NULL, 0, NULL); /* rdata */
455 if (!NT_STATUS_IS_OK(status)) {
456 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
457 nt_errstr(status)));
460 return status;