1f89176adb5cfc6385a6a9b60c9f927d56e6c3cc
[metze/samba/wip.git] / source3 / libsmb / cliquota.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client quota functions
4    Copyright (C) Stefan (metze) Metzmacher      2003
5
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.
10
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.
15
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/>.
18 */
19
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"
26 #include "../libcli/smb/smbXcli_base.h"
27
28 NTSTATUS cli_get_quota_handle(struct cli_state *cli, uint16_t *quota_fnum)
29 {
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);
34 }
35
36 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
37 {
38         if (!qt_list || !*qt_list) {
39                 return;
40         }
41
42         if ((*qt_list)->mem_ctx)
43                 talloc_destroy((*qt_list)->mem_ctx);
44
45         (*qt_list) = NULL;
46
47         return; 
48 }
49
50 bool parse_user_quota_record(const uint8_t *rdata,
51                              unsigned int rdata_count,
52                              unsigned int *offset,
53                              SMB_NTQUOTA_STRUCT *pqt)
54 {
55         int sid_len;
56         SMB_NTQUOTA_STRUCT qt;
57
58         ZERO_STRUCT(qt);
59
60         if (!rdata||!offset||!pqt) {
61                 smb_panic("parse_quota_record: called with NULL POINTER!");
62         }
63
64         if (rdata_count < 40) {
65                 return False;
66         }
67
68         /* offset to next quota record.
69          * 4 bytes IVAL(rdata,0)
70          * unused here...
71          */
72         *offset = IVAL(rdata,0);
73
74         /* sid len */
75         sid_len = IVAL(rdata,4);
76
77         if (rdata_count < 40+sid_len) {
78                 return False;           
79         }
80
81         /* unknown 8 bytes in pdata 
82          * maybe its the change time in NTTIME
83          */
84
85         /* the used space 8 bytes (uint64_t)*/
86         qt.usedspace = BVAL(rdata,16);
87
88         /* the soft quotas 8 bytes (uint64_t)*/
89         qt.softlim = BVAL(rdata,24);
90
91         /* the hard quotas 8 bytes (uint64_t)*/
92         qt.hardlim = BVAL(rdata,32);
93
94         if (!sid_parse(rdata+40,sid_len,&qt.sid)) {
95                 return false;
96         }
97
98         qt.qtype = SMB_USER_QUOTA_TYPE;
99
100         *pqt = qt;
101
102         return True;
103 }
104
105 NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
106                             SMB_NTQUOTA_STRUCT *pqt)
107 {
108         uint16_t setup[1];
109         uint8_t params[16];
110         unsigned int data_len;
111         uint8_t data[SID_MAX_SIZE+8];
112         uint8_t *rparam, *rdata;
113         uint32_t rparam_count, rdata_count;
114         unsigned int sid_len;
115         unsigned int offset;
116         NTSTATUS status;
117
118         if (!cli||!pqt) {
119                 smb_panic("cli_get_user_quota() called with NULL Pointer!");
120         }
121
122         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
123                 return cli_smb2_get_user_quota(cli, quota_fnum, pqt);
124         }
125
126         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
127
128         SSVAL(params, 0,quota_fnum);
129         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
130         SIVAL(params, 4,0x00000024);
131         SIVAL(params, 8,0x00000000);
132         SIVAL(params,12,0x00000024);
133
134         sid_len = ndr_size_dom_sid(&pqt->sid, 0);
135         data_len = sid_len+8;
136         SIVAL(data, 0, 0x00000000);
137         SIVAL(data, 4, sid_len);
138         sid_linearize(data+8, sid_len, &pqt->sid);
139
140         status = cli_trans(talloc_tos(), cli, SMBnttrans,
141                            NULL, -1, /* name, fid */
142                            NT_TRANSACT_GET_USER_QUOTA, 0,
143                            setup, 1, 0, /* setup */
144                            params, 16, 4, /* params */
145                            data, data_len, 112, /* data */
146                            NULL,                /* recv_flags2 */
147                            NULL, 0, NULL,       /* rsetup */
148                            &rparam, 4, &rparam_count,
149                            &rdata, 8, &rdata_count);
150         if (!NT_STATUS_IS_OK(status)) {
151                 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
152                           nt_errstr(status)));
153                 return status;
154         }
155
156         if (!parse_user_quota_record(rdata, rdata_count, &offset, pqt)) {
157                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
158                 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
159         }
160
161         TALLOC_FREE(rparam);
162         TALLOC_FREE(rdata);
163         return status;
164 }
165
166 NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
167                             SMB_NTQUOTA_STRUCT *pqt)
168 {
169         uint16_t setup[1];
170         uint8_t params[2];
171         uint8_t data[112];
172         unsigned int sid_len;   
173         NTSTATUS status;
174
175         memset(data,'\0',112);
176
177         if (!cli||!pqt) {
178                 smb_panic("cli_set_user_quota() called with NULL Pointer!");
179         }
180
181         SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
182
183         SSVAL(params,0,quota_fnum);
184
185         sid_len = ndr_size_dom_sid(&pqt->sid, 0);
186         SIVAL(data,0,0);
187         SIVAL(data,4,sid_len);
188         SBIG_UINT(data, 8,(uint64_t)0);
189         SBIG_UINT(data,16,pqt->usedspace);
190         SBIG_UINT(data,24,pqt->softlim);
191         SBIG_UINT(data,32,pqt->hardlim);
192         sid_linearize(data+40, sid_len, &pqt->sid);
193
194         status = cli_trans(talloc_tos(), cli, SMBnttrans,
195                            NULL, -1, /* name, fid */
196                            NT_TRANSACT_SET_USER_QUOTA, 0,
197                            setup, 1, 0, /* setup */
198                            params, 2, 0, /* params */
199                            data, 112, 0, /* data */
200                            NULL,                /* recv_flags2 */
201                            NULL, 0, NULL,       /* rsetup */
202                            NULL, 0, NULL,       /* rparams */
203                            NULL, 0, NULL);      /* rdata */
204
205         if (!NT_STATUS_IS_OK(status)) {
206                 DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
207                           nt_errstr(status)));
208         }
209
210         return status;
211 }
212
213 NTSTATUS cli_list_user_quota(struct cli_state *cli, int quota_fnum,
214                              SMB_NTQUOTA_LIST **pqt_list)
215 {
216         uint16_t setup[1];
217         uint8_t params[16];
218         uint8_t *rparam=NULL, *rdata=NULL;
219         uint32_t rparam_count=0, rdata_count=0;
220         unsigned int offset;
221         const uint8_t *curdata = NULL;
222         unsigned int curdata_count = 0;
223         TALLOC_CTX *mem_ctx = NULL;
224         SMB_NTQUOTA_STRUCT qt;
225         SMB_NTQUOTA_LIST *tmp_list_ent;
226         NTSTATUS status;
227
228         if (!cli||!pqt_list) {
229                 smb_panic("cli_list_user_quota() called with NULL Pointer!");
230         }
231
232         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
233
234         SSVAL(params, 0,quota_fnum);
235         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_START);
236         SIVAL(params, 4,0x00000000);
237         SIVAL(params, 8,0x00000000);
238         SIVAL(params,12,0x00000000);
239
240         status = cli_trans(talloc_tos(), cli, SMBnttrans,
241                            NULL, -1, /* name, fid */
242                            NT_TRANSACT_GET_USER_QUOTA, 0,
243                            setup, 1, 0, /* setup */
244                            params, 16, 4, /* params */
245                            NULL, 0, 2048, /* data */
246                            NULL,                /* recv_flags2 */
247                            NULL, 0, NULL,       /* rsetup */
248                            &rparam, 0, &rparam_count,
249                            &rdata, 0, &rdata_count);
250
251         if (!NT_STATUS_IS_OK(status) &&
252             !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
253                 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
254                           nt_errstr(status)));
255                 goto cleanup;
256         }
257
258         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) ||
259             rdata_count == 0) {
260                 *pqt_list = NULL;
261                 return NT_STATUS_OK;
262         }
263
264         if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
265                 DEBUG(0,("talloc_init() failed\n"));
266                 return NT_STATUS_NO_MEMORY;
267         }
268
269         offset = 1;
270         for (curdata=rdata,curdata_count=rdata_count;
271                 ((curdata)&&(curdata_count>=8)&&(offset>0));
272                 curdata +=offset,curdata_count -= offset) {
273                 ZERO_STRUCT(qt);
274                 if (!parse_user_quota_record((const uint8_t *)curdata, curdata_count,
275                                              &offset, &qt)) {
276                         DEBUG(1,("Failed to parse the quota record\n"));
277                         goto cleanup;
278                 }
279
280                 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
281                         DEBUG(0,("TALLOC_ZERO() failed\n"));
282                         talloc_destroy(mem_ctx);
283                         return NT_STATUS_NO_MEMORY;
284                 }
285
286                 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
287                         DEBUG(0,("TALLOC_ZERO() failed\n"));
288                         talloc_destroy(mem_ctx);
289                         return NT_STATUS_NO_MEMORY;
290                 }
291
292                 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
293                 tmp_list_ent->mem_ctx = mem_ctx;                
294
295                 DLIST_ADD((*pqt_list),tmp_list_ent);
296         }
297
298         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE); 
299         while(1) {
300
301                 TALLOC_FREE(rparam);
302                 TALLOC_FREE(rdata);
303
304                 status = cli_trans(talloc_tos(), cli, SMBnttrans,
305                                    NULL, -1, /* name, fid */
306                                    NT_TRANSACT_GET_USER_QUOTA, 0,
307                                    setup, 1, 0, /* setup */
308                                    params, 16, 4, /* params */
309                                    NULL, 0, 2048, /* data */
310                                    NULL,                /* recv_flags2 */
311                                    NULL, 0, NULL,       /* rsetup */
312                                    &rparam, 0, &rparam_count,
313                                    &rdata, 0, &rdata_count);
314
315                 if (!NT_STATUS_IS_OK(status) &&
316                     !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
317                         DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
318                                   nt_errstr(status)));
319                         goto cleanup;
320                 }
321
322                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) ||
323                     rdata_count == 0) {
324                         status = NT_STATUS_OK;
325                         break;
326                 }
327
328                 offset = 1;
329                 for (curdata=rdata,curdata_count=rdata_count;
330                         ((curdata)&&(curdata_count>=8)&&(offset>0));
331                         curdata +=offset,curdata_count -= offset) {
332                         ZERO_STRUCT(qt);
333                         if (!parse_user_quota_record((const uint8_t *)curdata,
334                                                      curdata_count, &offset,
335                                                      &qt)) {
336                                 DEBUG(1,("Failed to parse the quota record\n"));
337                                 goto cleanup;
338                         }
339
340                         if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
341                                 DEBUG(0,("TALLOC_ZERO() failed\n"));
342                                 talloc_destroy(mem_ctx);
343                                 goto cleanup;
344                         }
345
346                         if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
347                                 DEBUG(0,("TALLOC_ZERO() failed\n"));
348                                 talloc_destroy(mem_ctx);
349                                 goto cleanup;
350                         }
351
352                         memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
353                         tmp_list_ent->mem_ctx = mem_ctx;                
354
355                         DLIST_ADD((*pqt_list),tmp_list_ent);
356                 }
357         }
358
359  cleanup:
360         TALLOC_FREE(rparam);
361         TALLOC_FREE(rdata);
362
363         return status;
364 }
365
366 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
367                                SMB_NTQUOTA_STRUCT *pqt)
368 {
369         uint16_t setup[1];
370         uint8_t param[2];
371         uint8_t *rdata=NULL;
372         uint32_t rdata_count=0;
373         SMB_NTQUOTA_STRUCT qt;
374         NTSTATUS status;
375
376         ZERO_STRUCT(qt);
377
378         if (!cli||!pqt) {
379                 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
380         }
381
382         SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
383
384         SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
385
386         status = cli_trans(talloc_tos(), cli, SMBtrans2,
387                            NULL, -1, /* name, fid */
388                            0, 0,     /* function, flags */
389                            setup, 1, 0, /* setup */
390                            param, 2, 0, /* param */
391                            NULL, 0, 560, /* data */
392                            NULL,         /* recv_flags2 */
393                            NULL, 0, NULL, /* rsetup */
394                            NULL, 0, NULL, /* rparam */
395                            &rdata, 48, &rdata_count);
396
397         if (!NT_STATUS_IS_OK(status)) {
398                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
399                           nt_errstr(status)));
400                 return status;
401         }
402
403         /* unknown_1 24 NULL bytes in pdata*/
404
405         /* the soft quotas 8 bytes (uint64_t)*/
406         qt.softlim = BVAL(rdata,24);
407
408         /* the hard quotas 8 bytes (uint64_t)*/
409         qt.hardlim = BVAL(rdata,32);
410
411         /* quota_flags 2 bytes **/
412         qt.qflags = SVAL(rdata,40);
413
414         qt.qtype = SMB_USER_FS_QUOTA_TYPE;
415
416         *pqt = qt;
417
418         TALLOC_FREE(rdata);
419         return status;
420 }
421
422 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
423                                SMB_NTQUOTA_STRUCT *pqt)
424 {
425         uint16_t setup[1];
426         uint8_t param[4];
427         uint8_t data[48];
428         SMB_NTQUOTA_STRUCT qt;
429         NTSTATUS status;
430         ZERO_STRUCT(qt);
431         memset(data,'\0',48);
432
433         if (!cli||!pqt) {
434                 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
435         }
436
437         SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
438
439         SSVAL(param,0,quota_fnum);
440         SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
441
442         /* Unknown1 24 NULL bytes*/
443
444         /* Default Soft Quota 8 bytes */
445         SBIG_UINT(data,24,pqt->softlim);
446
447         /* Default Hard Quota 8 bytes */
448         SBIG_UINT(data,32,pqt->hardlim);
449
450         /* Quota flag 2 bytes */
451         SSVAL(data,40,pqt->qflags);
452
453         /* Unknown3 6 NULL bytes */
454
455         status = cli_trans(talloc_tos(), cli, SMBtrans2,
456                            NULL, -1, /* name, fid */
457                            0, 0,     /* function, flags */
458                            setup, 1, 0, /* setup */
459                            param, 4, 0, /* param */
460                            data, 48, 0, /* data */
461                            NULL,         /* recv_flags2 */
462                            NULL, 0, NULL, /* rsetup */
463                            NULL, 0, NULL, /* rparam */
464                            NULL, 0, NULL); /* rdata */
465
466         if (!NT_STATUS_IS_OK(status)) {
467                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
468                           nt_errstr(status)));
469         }
470
471         return status;
472 }