include updates
[tomato.git] / release / src-rt-6.x.4708 / router / samba-3.0.25b / source / libsmb / cliquota.c
blob2a47ae246328fe4b8eb6b79699dbbe1803af24b6
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 BOOL cli_get_quota_handle(struct cli_state *cli, int *quota_fnum)
25 *quota_fnum = cli_nt_create_full(cli, FAKE_FILE_NAME_QUOTA_WIN32,
26 0x00000016, DESIRED_ACCESS_PIPE,
27 0x00000000, FILE_SHARE_READ|FILE_SHARE_WRITE,
28 FILE_OPEN, 0x00000000, 0x03);
30 if (*quota_fnum == (-1)) {
31 return False;
34 return True;
37 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
39 if (!qt_list)
40 return;
42 if ((*qt_list)->mem_ctx)
43 talloc_destroy((*qt_list)->mem_ctx);
45 (*qt_list) = NULL;
47 return;
50 static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count, unsigned int *offset, SMB_NTQUOTA_STRUCT *pqt)
52 int sid_len;
53 SMB_NTQUOTA_STRUCT qt;
55 ZERO_STRUCT(qt);
57 if (!rdata||!offset||!pqt)
58 smb_panic("parse_quota_record: called with NULL POINTER!\n");
60 if (rdata_count < 40) {
61 return False;
64 /* offset to next quota record.
65 * 4 bytes IVAL(rdata,0)
66 * unused here...
68 *offset = IVAL(rdata,0);
70 /* sid len */
71 sid_len = IVAL(rdata,4);
73 if (rdata_count < 40+sid_len) {
74 return False;
77 /* unknown 8 bytes in pdata
78 * maybe its the change time in NTTIME
81 /* the used space 8 bytes (SMB_BIG_UINT)*/
82 qt.usedspace = (SMB_BIG_UINT)IVAL(rdata,16);
83 #ifdef LARGE_SMB_OFF_T
84 qt.usedspace |= (((SMB_BIG_UINT)IVAL(rdata,20)) << 32);
85 #else /* LARGE_SMB_OFF_T */
86 if ((IVAL(rdata,20) != 0)&&
87 ((qt.usedspace != 0xFFFFFFFF)||
88 (IVAL(rdata,20)!=0xFFFFFFFF))) {
89 /* more than 32 bits? */
90 return False;
92 #endif /* LARGE_SMB_OFF_T */
94 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
95 qt.softlim = (SMB_BIG_UINT)IVAL(rdata,24);
96 #ifdef LARGE_SMB_OFF_T
97 qt.softlim |= (((SMB_BIG_UINT)IVAL(rdata,28)) << 32);
98 #else /* LARGE_SMB_OFF_T */
99 if ((IVAL(rdata,28) != 0)&&
100 ((qt.softlim != 0xFFFFFFFF)||
101 (IVAL(rdata,28)!=0xFFFFFFFF))) {
102 /* more than 32 bits? */
103 return False;
105 #endif /* LARGE_SMB_OFF_T */
107 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
108 qt.hardlim = (SMB_BIG_UINT)IVAL(rdata,32);
109 #ifdef LARGE_SMB_OFF_T
110 qt.hardlim |= (((SMB_BIG_UINT)IVAL(rdata,36)) << 32);
111 #else /* LARGE_SMB_OFF_T */
112 if ((IVAL(rdata,36) != 0)&&
113 ((qt.hardlim != 0xFFFFFFFF)||
114 (IVAL(rdata,36)!=0xFFFFFFFF))) {
115 /* more than 32 bits? */
116 return False;
118 #endif /* LARGE_SMB_OFF_T */
120 sid_parse(rdata+40,sid_len,&qt.sid);
122 qt.qtype = SMB_USER_QUOTA_TYPE;
124 *pqt = qt;
126 return True;
129 BOOL cli_get_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
131 BOOL ret = False;
132 uint16 setup;
133 char params[16];
134 unsigned int data_len;
135 char data[SID_MAX_SIZE+8];
136 char *rparam=NULL, *rdata=NULL;
137 unsigned int rparam_count=0, rdata_count=0;
138 unsigned int sid_len;
139 unsigned int offset;
141 if (!cli||!pqt)
142 smb_panic("cli_get_user_quota() called with NULL Pointer!");
144 setup = NT_TRANSACT_GET_USER_QUOTA;
146 SSVAL(params, 0,quota_fnum);
147 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
148 SIVAL(params, 4,0x00000024);
149 SIVAL(params, 8,0x00000000);
150 SIVAL(params,12,0x00000024);
152 sid_len = sid_size(&pqt->sid);
153 data_len = sid_len+8;
154 SIVAL(data, 0, 0x00000000);
155 SIVAL(data, 4, sid_len);
156 sid_linearize(data+8, sid_len, &pqt->sid);
158 if (!cli_send_nt_trans(cli,
159 NT_TRANSACT_GET_USER_QUOTA,
161 &setup, 1, 0,
162 params, 16, 4,
163 data, data_len, 112)) {
164 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
165 goto cleanup;
169 if (!cli_receive_nt_trans(cli,
170 &rparam, &rparam_count,
171 &rdata, &rdata_count)) {
172 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
173 goto cleanup;
176 if (cli_is_error(cli)) {
177 ret = False;
178 goto cleanup;
179 } else {
180 ret = True;
183 if ((rparam&&rdata)&&(rparam_count>=4&&rdata_count>=8)) {
184 ret = parse_user_quota_record(rdata, rdata_count, &offset, pqt);
185 } else {
186 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
187 ret = False;
190 cleanup:
191 SAFE_FREE(rparam);
192 SAFE_FREE(rdata);
193 return ret;
196 BOOL cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
198 BOOL ret = False;
199 uint16 setup;
200 char params[2];
201 char data[112];
202 char *rparam=NULL, *rdata=NULL;
203 unsigned int rparam_count=0, rdata_count=0;
204 unsigned int sid_len;
205 memset(data,'\0',112);
207 if (!cli||!pqt)
208 smb_panic("cli_set_user_quota() called with NULL Pointer!");
210 setup = NT_TRANSACT_SET_USER_QUOTA;
212 SSVAL(params,0,quota_fnum);
214 sid_len = sid_size(&pqt->sid);
215 SIVAL(data,0,0);
216 SIVAL(data,4,sid_len);
217 SBIG_UINT(data, 8,(SMB_BIG_UINT)0);
218 SBIG_UINT(data,16,pqt->usedspace);
219 SBIG_UINT(data,24,pqt->softlim);
220 SBIG_UINT(data,32,pqt->hardlim);
221 sid_linearize(data+40, sid_len, &pqt->sid);
223 if (!cli_send_nt_trans(cli,
224 NT_TRANSACT_SET_USER_QUOTA,
226 &setup, 1, 0,
227 params, 2, 0,
228 data, 112, 0)) {
229 DEBUG(1,("Failed to send NT_TRANSACT_SET_USER_QUOTA\n"));
230 goto cleanup;
234 if (!cli_receive_nt_trans(cli,
235 &rparam, &rparam_count,
236 &rdata, &rdata_count)) {
237 DEBUG(1,("NT_TRANSACT_SET_USER_QUOTA failed\n"));
238 goto cleanup;
241 if (cli_is_error(cli)) {
242 ret = False;
243 goto cleanup;
244 } else {
245 ret = True;
248 cleanup:
249 SAFE_FREE(rparam);
250 SAFE_FREE(rdata);
251 return ret;
254 BOOL cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST **pqt_list)
256 BOOL ret = False;
257 uint16 setup;
258 char params[16];
259 char *rparam=NULL, *rdata=NULL;
260 unsigned int rparam_count=0, rdata_count=0;
261 unsigned int offset;
262 const char *curdata = NULL;
263 unsigned int curdata_count = 0;
264 TALLOC_CTX *mem_ctx = NULL;
265 SMB_NTQUOTA_STRUCT qt;
266 SMB_NTQUOTA_LIST *tmp_list_ent;
268 if (!cli||!pqt_list)
269 smb_panic("cli_list_user_quota() called with NULL Pointer!");
271 setup = NT_TRANSACT_GET_USER_QUOTA;
273 SSVAL(params, 0,quota_fnum);
274 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_START);
275 SIVAL(params, 4,0x00000000);
276 SIVAL(params, 8,0x00000000);
277 SIVAL(params,12,0x00000000);
279 if (!cli_send_nt_trans(cli,
280 NT_TRANSACT_GET_USER_QUOTA,
282 &setup, 1, 0,
283 params, 16, 4,
284 NULL, 0, 2048)) {
285 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
286 goto cleanup;
290 if (!cli_receive_nt_trans(cli,
291 &rparam, &rparam_count,
292 &rdata, &rdata_count)) {
293 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
294 goto cleanup;
297 if (cli_is_error(cli)) {
298 ret = False;
299 goto cleanup;
300 } else {
301 ret = True;
304 if (rdata_count == 0) {
305 *pqt_list = NULL;
306 return True;
309 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
310 DEBUG(0,("talloc_init() failed\n"));
311 return (-1);
314 offset = 1;
315 for (curdata=rdata,curdata_count=rdata_count;
316 ((curdata)&&(curdata_count>=8)&&(offset>0));
317 curdata +=offset,curdata_count -= offset) {
318 ZERO_STRUCT(qt);
319 if (!parse_user_quota_record(curdata, curdata_count, &offset, &qt)) {
320 DEBUG(1,("Failed to parse the quota record\n"));
321 goto cleanup;
324 if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
325 DEBUG(0,("TALLOC_ZERO() failed\n"));
326 talloc_destroy(mem_ctx);
327 return (-1);
330 if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
331 DEBUG(0,("TALLOC_ZERO() failed\n"));
332 talloc_destroy(mem_ctx);
333 return (-1);
336 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
337 tmp_list_ent->mem_ctx = mem_ctx;
339 DLIST_ADD((*pqt_list),tmp_list_ent);
342 SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE);
343 while(1) {
344 if (!cli_send_nt_trans(cli,
345 NT_TRANSACT_GET_USER_QUOTA,
347 &setup, 1, 0,
348 params, 16, 4,
349 NULL, 0, 2048)) {
350 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
351 goto cleanup;
354 SAFE_FREE(rparam);
355 SAFE_FREE(rdata);
356 if (!cli_receive_nt_trans(cli,
357 &rparam, &rparam_count,
358 &rdata, &rdata_count)) {
359 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
360 goto cleanup;
363 if (cli_is_error(cli)) {
364 ret = False;
365 goto cleanup;
366 } else {
367 ret = True;
370 if (rdata_count == 0) {
371 break;
374 offset = 1;
375 for (curdata=rdata,curdata_count=rdata_count;
376 ((curdata)&&(curdata_count>=8)&&(offset>0));
377 curdata +=offset,curdata_count -= offset) {
378 ZERO_STRUCT(qt);
379 if (!parse_user_quota_record(curdata, curdata_count, &offset, &qt)) {
380 DEBUG(1,("Failed to parse the quota record\n"));
381 goto cleanup;
384 if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
385 DEBUG(0,("TALLOC_ZERO() failed\n"));
386 talloc_destroy(mem_ctx);
387 goto cleanup;
390 if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
391 DEBUG(0,("TALLOC_ZERO() failed\n"));
392 talloc_destroy(mem_ctx);
393 goto cleanup;
396 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
397 tmp_list_ent->mem_ctx = mem_ctx;
399 DLIST_ADD((*pqt_list),tmp_list_ent);
404 ret = True;
405 cleanup:
406 SAFE_FREE(rparam);
407 SAFE_FREE(rdata);
409 return ret;
412 BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
414 BOOL ret = False;
415 uint16 setup;
416 char param[2];
417 char *rparam=NULL, *rdata=NULL;
418 unsigned int rparam_count=0, rdata_count=0;
419 SMB_NTQUOTA_STRUCT qt;
420 ZERO_STRUCT(qt);
422 if (!cli||!pqt)
423 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
425 setup = TRANSACT2_QFSINFO;
427 SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
429 if (!cli_send_trans(cli, SMBtrans2,
430 NULL,
431 0, 0,
432 &setup, 1, 0,
433 param, 2, 0,
434 NULL, 0, 560)) {
435 goto cleanup;
438 if (!cli_receive_trans(cli, SMBtrans2,
439 &rparam, &rparam_count,
440 &rdata, &rdata_count)) {
441 goto cleanup;
444 if (cli_is_error(cli)) {
445 ret = False;
446 goto cleanup;
447 } else {
448 ret = True;
451 if (rdata_count < 48) {
452 goto cleanup;
455 /* unknown_1 24 NULL bytes in pdata*/
457 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
458 qt.softlim = (SMB_BIG_UINT)IVAL(rdata,24);
459 #ifdef LARGE_SMB_OFF_T
460 qt.softlim |= (((SMB_BIG_UINT)IVAL(rdata,28)) << 32);
461 #else /* LARGE_SMB_OFF_T */
462 if ((IVAL(rdata,28) != 0)&&
463 ((qt.softlim != 0xFFFFFFFF)||
464 (IVAL(rdata,28)!=0xFFFFFFFF))) {
465 /* more than 32 bits? */
466 goto cleanup;
468 #endif /* LARGE_SMB_OFF_T */
470 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
471 qt.hardlim = (SMB_BIG_UINT)IVAL(rdata,32);
472 #ifdef LARGE_SMB_OFF_T
473 qt.hardlim |= (((SMB_BIG_UINT)IVAL(rdata,36)) << 32);
474 #else /* LARGE_SMB_OFF_T */
475 if ((IVAL(rdata,36) != 0)&&
476 ((qt.hardlim != 0xFFFFFFFF)||
477 (IVAL(rdata,36)!=0xFFFFFFFF))) {
478 /* more than 32 bits? */
479 goto cleanup;
481 #endif /* LARGE_SMB_OFF_T */
483 /* quota_flags 2 bytes **/
484 qt.qflags = SVAL(rdata,40);
486 qt.qtype = SMB_USER_FS_QUOTA_TYPE;
488 *pqt = qt;
490 ret = True;
491 cleanup:
492 SAFE_FREE(rparam);
493 SAFE_FREE(rdata);
495 return ret;
498 BOOL cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
500 BOOL ret = False;
501 uint16 setup;
502 char param[4];
503 char data[48];
504 char *rparam=NULL, *rdata=NULL;
505 unsigned int rparam_count=0, rdata_count=0;
506 SMB_NTQUOTA_STRUCT qt;
507 ZERO_STRUCT(qt);
508 memset(data,'\0',48);
510 if (!cli||!pqt)
511 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
513 setup = TRANSACT2_SETFSINFO;
515 SSVAL(param,0,quota_fnum);
516 SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
518 /* Unknown1 24 NULL bytes*/
520 /* Default Soft Quota 8 bytes */
521 SBIG_UINT(data,24,pqt->softlim);
523 /* Default Hard Quota 8 bytes */
524 SBIG_UINT(data,32,pqt->hardlim);
526 /* Quota flag 2 bytes */
527 SSVAL(data,40,pqt->qflags);
529 /* Unknown3 6 NULL bytes */
531 if (!cli_send_trans(cli, SMBtrans2,
532 NULL,
533 0, 0,
534 &setup, 1, 0,
535 param, 4, 0,
536 data, 48, 0)) {
537 goto cleanup;
540 if (!cli_receive_trans(cli, SMBtrans2,
541 &rparam, &rparam_count,
542 &rdata, &rdata_count)) {
543 goto cleanup;
546 if (cli_is_error(cli)) {
547 ret = False;
548 goto cleanup;
549 } else {
550 ret = True;
553 cleanup:
554 SAFE_FREE(rparam);
555 SAFE_FREE(rdata);
557 return ret;
560 static char *quota_str_static(SMB_BIG_UINT val, BOOL special, BOOL _numeric)
562 static fstring buffer;
564 memset(buffer,'\0',sizeof(buffer));
566 if (!_numeric&&special&&(val == SMB_NTQUOTAS_NO_LIMIT)) {
567 fstr_sprintf(buffer,"NO LIMIT");
568 return buffer;
570 #if defined(HAVE_LONGLONG)
571 fstr_sprintf(buffer,"%llu",val);
572 #else
573 fstr_sprintf(buffer,"%lu",val);
574 #endif
575 return buffer;
578 void dump_ntquota(SMB_NTQUOTA_STRUCT *qt, BOOL _verbose, BOOL _numeric, void (*_sidtostring)(fstring str, DOM_SID *sid, BOOL _numeric))
580 if (!qt)
581 smb_panic("dump_ntquota() called with NULL pointer");
583 switch (qt->qtype) {
584 case SMB_USER_FS_QUOTA_TYPE:
586 d_printf("File System QUOTAS:\n");
587 d_printf("Limits:\n");
588 d_printf(" Default Soft Limit: %15s\n",quota_str_static(qt->softlim,True,_numeric));
589 d_printf(" Default Hard Limit: %15s\n",quota_str_static(qt->hardlim,True,_numeric));
590 d_printf("Quota Flags:\n");
591 d_printf(" Quotas Enabled: %s\n",
592 ((qt->qflags&QUOTAS_ENABLED)||(qt->qflags&QUOTAS_DENY_DISK))?"On":"Off");
593 d_printf(" Deny Disk: %s\n",(qt->qflags&QUOTAS_DENY_DISK)?"On":"Off");
594 d_printf(" Log Soft Limit: %s\n",(qt->qflags&QUOTAS_LOG_THRESHOLD)?"On":"Off");
595 d_printf(" Log Hard Limit: %s\n",(qt->qflags&QUOTAS_LOG_LIMIT)?"On":"Off");
597 break;
598 case SMB_USER_QUOTA_TYPE:
600 fstring username_str = {0};
602 if (_sidtostring) {
603 _sidtostring(username_str,&qt->sid,_numeric);
604 } else {
605 fstrcpy(username_str,sid_string_static(&qt->sid));
608 if (_verbose) {
609 d_printf("Quotas for User: %s\n",username_str);
610 d_printf("Used Space: %15s\n",quota_str_static(qt->usedspace,False,_numeric));
611 d_printf("Soft Limit: %15s\n",quota_str_static(qt->softlim,True,_numeric));
612 d_printf("Hard Limit: %15s\n",quota_str_static(qt->hardlim,True,_numeric));
613 } else {
614 d_printf("%-30s: ",username_str);
615 d_printf("%15s/",quota_str_static(qt->usedspace,False,_numeric));
616 d_printf("%15s/",quota_str_static(qt->softlim,True,_numeric));
617 d_printf("%15s\n",quota_str_static(qt->hardlim,True,_numeric));
620 break;
621 default:
622 d_printf("dump_ntquota() invalid qtype(%d)\n",qt->qtype);
623 return;
627 void dump_ntquota_list(SMB_NTQUOTA_LIST **qtl, BOOL _verbose, BOOL _numeric, void (*_sidtostring)(fstring str, DOM_SID *sid, BOOL _numeric))
629 SMB_NTQUOTA_LIST *cur;
631 for (cur = *qtl;cur;cur = cur->next) {
632 if (cur->quotas)
633 dump_ntquota(cur->quotas,_verbose,_numeric,_sidtostring);