s3:utils change data_blob_dup_talloc() to take a DATA_BLOB by value
[nivanova/samba-autobuild/.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
27 NTSTATUS cli_get_quota_handle(struct cli_state *cli, uint16_t *quota_fnum)
28 {
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);
33 }
34
35 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
36 {
37         if (!qt_list)
38                 return;
39
40         if ((*qt_list)->mem_ctx)
41                 talloc_destroy((*qt_list)->mem_ctx);
42
43         (*qt_list) = NULL;
44
45         return; 
46 }
47
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)
52 {
53         int sid_len;
54         SMB_NTQUOTA_STRUCT qt;
55
56         ZERO_STRUCT(qt);
57
58         if (!rdata||!offset||!pqt) {
59                 smb_panic("parse_quota_record: called with NULL POINTER!");
60         }
61
62         if (rdata_count < 40) {
63                 return False;
64         }
65
66         /* offset to next quota record.
67          * 4 bytes IVAL(rdata,0)
68          * unused here...
69          */
70         *offset = IVAL(rdata,0);
71
72         /* sid len */
73         sid_len = IVAL(rdata,4);
74
75         if (rdata_count < 40+sid_len) {
76                 return False;           
77         }
78
79         /* unknown 8 bytes in pdata 
80          * maybe its the change time in NTTIME
81          */
82
83         /* the used space 8 bytes (uint64_t)*/
84         qt.usedspace = BVAL(rdata,16);
85
86         /* the soft quotas 8 bytes (uint64_t)*/
87         qt.softlim = BVAL(rdata,24);
88
89         /* the hard quotas 8 bytes (uint64_t)*/
90         qt.hardlim = BVAL(rdata,32);
91
92         if (!sid_parse((const char *)rdata+40,sid_len,&qt.sid)) {
93                 return false;
94         }
95
96         qt.qtype = SMB_USER_QUOTA_TYPE;
97
98         *pqt = qt;
99
100         return True;
101 }
102
103 NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
104                             SMB_NTQUOTA_STRUCT *pqt)
105 {
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;
115
116         if (!cli||!pqt) {
117                 smb_panic("cli_get_user_quota() called with NULL Pointer!");
118         }
119
120         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
121
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);
127
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);
133
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;
148         }
149
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"));
153         }
154
155         TALLOC_FREE(rparam);
156         TALLOC_FREE(rdata);
157         return status;
158 }
159
160 NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
161                             SMB_NTQUOTA_STRUCT *pqt)
162 {
163         uint16_t setup[1];
164         uint8_t params[2];
165         uint8_t data[112];
166         unsigned int sid_len;   
167         NTSTATUS status;
168
169         memset(data,'\0',112);
170
171         if (!cli||!pqt) {
172                 smb_panic("cli_set_user_quota() called with NULL Pointer!");
173         }
174
175         SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
176
177         SSVAL(params,0,quota_fnum);
178
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);
187
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 */
198
199         if (!NT_STATUS_IS_OK(status)) {
200                 DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
201                           nt_errstr(status)));
202         }
203
204         return status;
205 }
206
207 NTSTATUS cli_list_user_quota(struct cli_state *cli, int quota_fnum,
208                              SMB_NTQUOTA_LIST **pqt_list)
209 {
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;
221
222         if (!cli||!pqt_list) {
223                 smb_panic("cli_list_user_quota() called with NULL Pointer!");
224         }
225
226         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
227
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);
233
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);
244
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;
249         }
250
251         if (rdata_count == 0) {
252                 *pqt_list = NULL;
253                 return NT_STATUS_OK;
254         }
255
256         if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
257                 DEBUG(0,("talloc_init() failed\n"));
258                 return NT_STATUS_NO_MEMORY;
259         }
260
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;
270                 }
271
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;
276                 }
277
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;
282                 }
283
284                 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
285                 tmp_list_ent->mem_ctx = mem_ctx;                
286
287                 DLIST_ADD((*pqt_list),tmp_list_ent);
288         }
289
290         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE); 
291         while(1) {
292
293                 TALLOC_FREE(rparam);
294                 TALLOC_FREE(rdata);
295
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);
306
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;
311                 }
312
313                 if (rdata_count == 0) {
314                         break;
315                 }
316
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;
327                         }
328
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;
333                         }
334
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;
339                         }
340
341                         memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
342                         tmp_list_ent->mem_ctx = mem_ctx;                
343
344                         DLIST_ADD((*pqt_list),tmp_list_ent);
345                 }
346         }
347
348  cleanup:
349         TALLOC_FREE(rparam);
350         TALLOC_FREE(rdata);
351
352         return status;
353 }
354
355 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
356                                SMB_NTQUOTA_STRUCT *pqt)
357 {
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;
364
365         ZERO_STRUCT(qt);
366
367         if (!cli||!pqt) {
368                 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
369         }
370
371         SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
372
373         SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
374
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);
385
386         if (!NT_STATUS_IS_OK(status)) {
387                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
388                           nt_errstr(status)));
389                 return status;
390         }
391
392         /* unknown_1 24 NULL bytes in pdata*/
393
394         /* the soft quotas 8 bytes (uint64_t)*/
395         qt.softlim = BVAL(rdata,24);
396
397         /* the hard quotas 8 bytes (uint64_t)*/
398         qt.hardlim = BVAL(rdata,32);
399
400         /* quota_flags 2 bytes **/
401         qt.qflags = SVAL(rdata,40);
402
403         qt.qtype = SMB_USER_FS_QUOTA_TYPE;
404
405         *pqt = qt;
406
407         TALLOC_FREE(rdata);
408         return status;
409 }
410
411 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
412                                SMB_NTQUOTA_STRUCT *pqt)
413 {
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);
421
422         if (!cli||!pqt) {
423                 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
424         }
425
426         SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
427
428         SSVAL(param,0,quota_fnum);
429         SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
430
431         /* Unknown1 24 NULL bytes*/
432
433         /* Default Soft Quota 8 bytes */
434         SBIG_UINT(data,24,pqt->softlim);
435
436         /* Default Hard Quota 8 bytes */
437         SBIG_UINT(data,32,pqt->hardlim);
438
439         /* Quota flag 2 bytes */
440         SSVAL(data,40,pqt->qflags);
441
442         /* Unknown3 6 NULL bytes */
443
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 */
454
455         if (!NT_STATUS_IS_OK(status)) {
456                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
457                           nt_errstr(status)));
458         }
459
460         return status;
461 }