cliquota: refactor and cleanup listing of user quotas
[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 static NTSTATUS cli_list_user_quota_step(struct cli_state *cli,
214                                          TALLOC_CTX *mem_ctx,
215                                          int quota_fnum,
216                                          SMB_NTQUOTA_LIST **pqt_list,
217                                          bool first)
218 {
219         uint16_t setup[1];
220         uint8_t params[16];
221         uint8_t *rparam=NULL, *rdata=NULL;
222         uint32_t rparam_count=0, rdata_count=0;
223         unsigned int offset;
224         const uint8_t *curdata = NULL;
225         unsigned int curdata_count = 0;
226         SMB_NTQUOTA_STRUCT qt;
227         SMB_NTQUOTA_LIST *tmp_list_ent;
228         NTSTATUS status;
229         uint16_t op = first ? TRANSACT_GET_USER_QUOTA_LIST_START
230                             : TRANSACT_GET_USER_QUOTA_LIST_CONTINUE;
231
232         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
233
234         SSVAL(params, 0,quota_fnum);
235         SSVAL(params, 2, op);
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                 goto cleanup;
254         }
255
256         /* compat. with smbd + safeguard against
257          * endless loop
258          */
259         if (rdata_count == 0) {
260                 status = NT_STATUS_NO_MORE_ENTRIES;
261         }
262
263         offset = 1;
264         for (curdata=rdata,curdata_count=rdata_count;
265                 ((curdata)&&(curdata_count>=8)&&(offset>0));
266                 curdata +=offset,curdata_count -= offset) {
267                 ZERO_STRUCT(qt);
268                 if (!parse_user_quota_record((const uint8_t *)curdata, curdata_count,
269                                              &offset, &qt)) {
270                         DEBUG(1,("Failed to parse the quota record\n"));
271                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
272                         goto cleanup;
273                 }
274
275                 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
276                         status = NT_STATUS_NO_MEMORY;
277                         goto cleanup;
278                 }
279
280                 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
281                         status = NT_STATUS_NO_MEMORY;
282                         goto cleanup;
283                 }
284
285                 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
286                 tmp_list_ent->mem_ctx = mem_ctx;                
287
288                 DLIST_ADD((*pqt_list),tmp_list_ent);
289         }
290
291 cleanup:
292         TALLOC_FREE(rparam);
293         TALLOC_FREE(rdata);
294
295         return status;
296 }
297
298 NTSTATUS cli_list_user_quota(struct cli_state *cli,
299                              int quota_fnum,
300                              SMB_NTQUOTA_LIST **pqt_list)
301 {
302         NTSTATUS status;
303         TALLOC_CTX *mem_ctx = NULL;
304         bool first = true;
305
306         if (!cli || !pqt_list) {
307                 smb_panic("cli_list_user_quota() called with NULL Pointer!");
308         }
309
310         *pqt_list = NULL;
311
312         if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
313                 return NT_STATUS_NO_MEMORY;
314         }
315
316         do {
317                 status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
318                                                   pqt_list, first);
319                 first = false;
320         } while (NT_STATUS_IS_OK(status));
321
322         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
323                 status = NT_STATUS_OK;
324         }
325
326         if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
327                 TALLOC_FREE(mem_ctx);
328         }
329
330         return status;
331 }
332
333 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
334                                SMB_NTQUOTA_STRUCT *pqt)
335 {
336         uint16_t setup[1];
337         uint8_t param[2];
338         uint8_t *rdata=NULL;
339         uint32_t rdata_count=0;
340         SMB_NTQUOTA_STRUCT qt;
341         NTSTATUS status;
342
343         ZERO_STRUCT(qt);
344
345         if (!cli||!pqt) {
346                 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
347         }
348
349         SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
350
351         SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
352
353         status = cli_trans(talloc_tos(), cli, SMBtrans2,
354                            NULL, -1, /* name, fid */
355                            0, 0,     /* function, flags */
356                            setup, 1, 0, /* setup */
357                            param, 2, 0, /* param */
358                            NULL, 0, 560, /* data */
359                            NULL,         /* recv_flags2 */
360                            NULL, 0, NULL, /* rsetup */
361                            NULL, 0, NULL, /* rparam */
362                            &rdata, 48, &rdata_count);
363
364         if (!NT_STATUS_IS_OK(status)) {
365                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
366                           nt_errstr(status)));
367                 return status;
368         }
369
370         /* unknown_1 24 NULL bytes in pdata*/
371
372         /* the soft quotas 8 bytes (uint64_t)*/
373         qt.softlim = BVAL(rdata,24);
374
375         /* the hard quotas 8 bytes (uint64_t)*/
376         qt.hardlim = BVAL(rdata,32);
377
378         /* quota_flags 2 bytes **/
379         qt.qflags = SVAL(rdata,40);
380
381         qt.qtype = SMB_USER_FS_QUOTA_TYPE;
382
383         *pqt = qt;
384
385         TALLOC_FREE(rdata);
386         return status;
387 }
388
389 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
390                                SMB_NTQUOTA_STRUCT *pqt)
391 {
392         uint16_t setup[1];
393         uint8_t param[4];
394         uint8_t data[48];
395         SMB_NTQUOTA_STRUCT qt;
396         NTSTATUS status;
397         ZERO_STRUCT(qt);
398         memset(data,'\0',48);
399
400         if (!cli||!pqt) {
401                 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
402         }
403
404         SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
405
406         SSVAL(param,0,quota_fnum);
407         SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
408
409         /* Unknown1 24 NULL bytes*/
410
411         /* Default Soft Quota 8 bytes */
412         SBIG_UINT(data,24,pqt->softlim);
413
414         /* Default Hard Quota 8 bytes */
415         SBIG_UINT(data,32,pqt->hardlim);
416
417         /* Quota flag 2 bytes */
418         SSVAL(data,40,pqt->qflags);
419
420         /* Unknown3 6 NULL bytes */
421
422         status = cli_trans(talloc_tos(), cli, SMBtrans2,
423                            NULL, -1, /* name, fid */
424                            0, 0,     /* function, flags */
425                            setup, 1, 0, /* setup */
426                            param, 4, 0, /* param */
427                            data, 48, 0, /* data */
428                            NULL,         /* recv_flags2 */
429                            NULL, 0, NULL, /* rsetup */
430                            NULL, 0, NULL, /* rparam */
431                            NULL, 0, NULL); /* rdata */
432
433         if (!NT_STATUS_IS_OK(status)) {
434                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
435                           nt_errstr(status)));
436         }
437
438         return status;
439 }