2 Unix SMB/CIFS implementation.
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/>.
21 #include "../librpc/gen_ndr/ndr_security.h"
22 #include "fake_file.h"
24 NTSTATUS
cli_get_quota_handle(struct cli_state
*cli
, uint16_t *quota_fnum
)
26 return cli_ntcreate(cli
, FAKE_FILE_NAME_QUOTA_WIN32
,
27 0x00000016, DESIRED_ACCESS_PIPE
,
28 0x00000000, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
29 FILE_OPEN
, 0x00000000, 0x03, quota_fnum
);
32 void free_ntquota_list(SMB_NTQUOTA_LIST
**qt_list
)
37 if ((*qt_list
)->mem_ctx
)
38 talloc_destroy((*qt_list
)->mem_ctx
);
45 static bool parse_user_quota_record(const char *rdata
, unsigned int rdata_count
, unsigned int *offset
, SMB_NTQUOTA_STRUCT
*pqt
)
48 SMB_NTQUOTA_STRUCT qt
;
52 if (!rdata
||!offset
||!pqt
) {
53 smb_panic("parse_quota_record: called with NULL POINTER!");
56 if (rdata_count
< 40) {
60 /* offset to next quota record.
61 * 4 bytes IVAL(rdata,0)
64 *offset
= IVAL(rdata
,0);
67 sid_len
= IVAL(rdata
,4);
69 if (rdata_count
< 40+sid_len
) {
73 /* unknown 8 bytes in pdata
74 * maybe its the change time in NTTIME
77 /* the used space 8 bytes (uint64_t)*/
78 qt
.usedspace
= (uint64_t)IVAL(rdata
,16);
79 #ifdef LARGE_SMB_OFF_T
80 qt
.usedspace
|= (((uint64_t)IVAL(rdata
,20)) << 32);
81 #else /* LARGE_SMB_OFF_T */
82 if ((IVAL(rdata
,20) != 0)&&
83 ((qt
.usedspace
!= 0xFFFFFFFF)||
84 (IVAL(rdata
,20)!=0xFFFFFFFF))) {
85 /* more than 32 bits? */
88 #endif /* LARGE_SMB_OFF_T */
90 /* the soft quotas 8 bytes (uint64_t)*/
91 qt
.softlim
= (uint64_t)IVAL(rdata
,24);
92 #ifdef LARGE_SMB_OFF_T
93 qt
.softlim
|= (((uint64_t)IVAL(rdata
,28)) << 32);
94 #else /* LARGE_SMB_OFF_T */
95 if ((IVAL(rdata
,28) != 0)&&
96 ((qt
.softlim
!= 0xFFFFFFFF)||
97 (IVAL(rdata
,28)!=0xFFFFFFFF))) {
98 /* more than 32 bits? */
101 #endif /* LARGE_SMB_OFF_T */
103 /* the hard quotas 8 bytes (uint64_t)*/
104 qt
.hardlim
= (uint64_t)IVAL(rdata
,32);
105 #ifdef LARGE_SMB_OFF_T
106 qt
.hardlim
|= (((uint64_t)IVAL(rdata
,36)) << 32);
107 #else /* LARGE_SMB_OFF_T */
108 if ((IVAL(rdata
,36) != 0)&&
109 ((qt
.hardlim
!= 0xFFFFFFFF)||
110 (IVAL(rdata
,36)!=0xFFFFFFFF))) {
111 /* more than 32 bits? */
114 #endif /* LARGE_SMB_OFF_T */
116 sid_parse(rdata
+40,sid_len
,&qt
.sid
);
118 qt
.qtype
= SMB_USER_QUOTA_TYPE
;
125 bool cli_get_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
130 unsigned int data_len
;
131 char data
[SID_MAX_SIZE
+8];
132 char *rparam
=NULL
, *rdata
=NULL
;
133 unsigned int rparam_count
=0, rdata_count
=0;
134 unsigned int sid_len
;
138 smb_panic("cli_get_user_quota() called with NULL Pointer!");
141 setup
= NT_TRANSACT_GET_USER_QUOTA
;
143 SSVAL(params
, 0,quota_fnum
);
144 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_FOR_SID
);
145 SIVAL(params
, 4,0x00000024);
146 SIVAL(params
, 8,0x00000000);
147 SIVAL(params
,12,0x00000024);
149 sid_len
= ndr_size_dom_sid(&pqt
->sid
, 0);
150 data_len
= sid_len
+8;
151 SIVAL(data
, 0, 0x00000000);
152 SIVAL(data
, 4, sid_len
);
153 sid_linearize(data
+8, sid_len
, &pqt
->sid
);
155 if (!cli_send_nt_trans(cli
,
156 NT_TRANSACT_GET_USER_QUOTA
,
160 data
, data_len
, 112)) {
161 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
166 if (!cli_receive_nt_trans(cli
,
167 &rparam
, &rparam_count
,
168 &rdata
, &rdata_count
)) {
169 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
173 if (cli_is_error(cli
)) {
180 if ((rparam
&&rdata
)&&(rparam_count
>=4&&rdata_count
>=8)) {
181 ret
= parse_user_quota_record(rdata
, rdata_count
, &offset
, pqt
);
183 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
193 bool cli_set_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
199 char *rparam
=NULL
, *rdata
=NULL
;
200 unsigned int rparam_count
=0, rdata_count
=0;
201 unsigned int sid_len
;
202 memset(data
,'\0',112);
205 smb_panic("cli_set_user_quota() called with NULL Pointer!");
208 setup
= NT_TRANSACT_SET_USER_QUOTA
;
210 SSVAL(params
,0,quota_fnum
);
212 sid_len
= ndr_size_dom_sid(&pqt
->sid
, 0);
214 SIVAL(data
,4,sid_len
);
215 SBIG_UINT(data
, 8,(uint64_t)0);
216 SBIG_UINT(data
,16,pqt
->usedspace
);
217 SBIG_UINT(data
,24,pqt
->softlim
);
218 SBIG_UINT(data
,32,pqt
->hardlim
);
219 sid_linearize(data
+40, sid_len
, &pqt
->sid
);
221 if (!cli_send_nt_trans(cli
,
222 NT_TRANSACT_SET_USER_QUOTA
,
227 DEBUG(1,("Failed to send NT_TRANSACT_SET_USER_QUOTA\n"));
232 if (!cli_receive_nt_trans(cli
,
233 &rparam
, &rparam_count
,
234 &rdata
, &rdata_count
)) {
235 DEBUG(1,("NT_TRANSACT_SET_USER_QUOTA failed\n"));
239 if (cli_is_error(cli
)) {
252 bool cli_list_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_LIST
**pqt_list
)
257 char *rparam
=NULL
, *rdata
=NULL
;
258 unsigned int rparam_count
=0, rdata_count
=0;
260 const char *curdata
= NULL
;
261 unsigned int curdata_count
= 0;
262 TALLOC_CTX
*mem_ctx
= NULL
;
263 SMB_NTQUOTA_STRUCT qt
;
264 SMB_NTQUOTA_LIST
*tmp_list_ent
;
266 if (!cli
||!pqt_list
) {
267 smb_panic("cli_list_user_quota() called with NULL Pointer!");
270 setup
= NT_TRANSACT_GET_USER_QUOTA
;
272 SSVAL(params
, 0,quota_fnum
);
273 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_LIST_START
);
274 SIVAL(params
, 4,0x00000000);
275 SIVAL(params
, 8,0x00000000);
276 SIVAL(params
,12,0x00000000);
278 if (!cli_send_nt_trans(cli
,
279 NT_TRANSACT_GET_USER_QUOTA
,
284 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
289 if (!cli_receive_nt_trans(cli
,
290 &rparam
, &rparam_count
,
291 &rdata
, &rdata_count
)) {
292 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
296 if (cli_is_error(cli
)) {
303 if (rdata_count
== 0) {
308 if ((mem_ctx
=talloc_init("SMB_USER_QUOTA_LIST"))==NULL
) {
309 DEBUG(0,("talloc_init() failed\n"));
314 for (curdata
=rdata
,curdata_count
=rdata_count
;
315 ((curdata
)&&(curdata_count
>=8)&&(offset
>0));
316 curdata
+=offset
,curdata_count
-= offset
) {
318 if (!parse_user_quota_record(curdata
, curdata_count
, &offset
, &qt
)) {
319 DEBUG(1,("Failed to parse the quota record\n"));
323 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
324 DEBUG(0,("TALLOC_ZERO() failed\n"));
325 talloc_destroy(mem_ctx
);
329 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
330 DEBUG(0,("TALLOC_ZERO() failed\n"));
331 talloc_destroy(mem_ctx
);
335 memcpy(tmp_list_ent
->quotas
,&qt
,sizeof(qt
));
336 tmp_list_ent
->mem_ctx
= mem_ctx
;
338 DLIST_ADD((*pqt_list
),tmp_list_ent
);
341 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
);
343 if (!cli_send_nt_trans(cli
,
344 NT_TRANSACT_GET_USER_QUOTA
,
349 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
355 if (!cli_receive_nt_trans(cli
,
356 &rparam
, &rparam_count
,
357 &rdata
, &rdata_count
)) {
358 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
362 if (cli_is_error(cli
)) {
369 if (rdata_count
== 0) {
374 for (curdata
=rdata
,curdata_count
=rdata_count
;
375 ((curdata
)&&(curdata_count
>=8)&&(offset
>0));
376 curdata
+=offset
,curdata_count
-= offset
) {
378 if (!parse_user_quota_record(curdata
, curdata_count
, &offset
, &qt
)) {
379 DEBUG(1,("Failed to parse the quota record\n"));
383 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
384 DEBUG(0,("TALLOC_ZERO() failed\n"));
385 talloc_destroy(mem_ctx
);
389 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
390 DEBUG(0,("TALLOC_ZERO() failed\n"));
391 talloc_destroy(mem_ctx
);
395 memcpy(tmp_list_ent
->quotas
,&qt
,sizeof(qt
));
396 tmp_list_ent
->mem_ctx
= mem_ctx
;
398 DLIST_ADD((*pqt_list
),tmp_list_ent
);
411 bool cli_get_fs_quota_info(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
416 char *rparam
=NULL
, *rdata
=NULL
;
417 unsigned int rparam_count
=0, rdata_count
=0;
418 SMB_NTQUOTA_STRUCT qt
;
422 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
,
438 if (!cli_receive_trans(cli
, SMBtrans2
,
439 &rparam
, &rparam_count
,
440 &rdata
, &rdata_count
)) {
444 if (cli_is_error(cli
)) {
451 if (rdata_count
< 48) {
455 /* unknown_1 24 NULL bytes in pdata*/
457 /* the soft quotas 8 bytes (uint64_t)*/
458 qt
.softlim
= (uint64_t)IVAL(rdata
,24);
459 #ifdef LARGE_SMB_OFF_T
460 qt
.softlim
|= (((uint64_t)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? */
468 #endif /* LARGE_SMB_OFF_T */
470 /* the hard quotas 8 bytes (uint64_t)*/
471 qt
.hardlim
= (uint64_t)IVAL(rdata
,32);
472 #ifdef LARGE_SMB_OFF_T
473 qt
.hardlim
|= (((uint64_t)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? */
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
;
498 bool cli_set_fs_quota_info(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
504 char *rparam
=NULL
, *rdata
=NULL
;
505 unsigned int rparam_count
=0, rdata_count
=0;
506 SMB_NTQUOTA_STRUCT qt
;
508 memset(data
,'\0',48);
511 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
514 setup
= TRANSACT2_SETFSINFO
;
516 SSVAL(param
,0,quota_fnum
);
517 SSVAL(param
,2,SMB_FS_QUOTA_INFORMATION
);
519 /* Unknown1 24 NULL bytes*/
521 /* Default Soft Quota 8 bytes */
522 SBIG_UINT(data
,24,pqt
->softlim
);
524 /* Default Hard Quota 8 bytes */
525 SBIG_UINT(data
,32,pqt
->hardlim
);
527 /* Quota flag 2 bytes */
528 SSVAL(data
,40,pqt
->qflags
);
530 /* Unknown3 6 NULL bytes */
532 if (!cli_send_trans(cli
, SMBtrans2
,
541 if (!cli_receive_trans(cli
, SMBtrans2
,
542 &rparam
, &rparam_count
,
543 &rdata
, &rdata_count
)) {
547 if (cli_is_error(cli
)) {
561 static const char *quota_str_static(uint64_t val
, bool special
, bool _numeric
)
565 if (!_numeric
&&special
&&(val
== SMB_NTQUOTAS_NO_LIMIT
)) {
568 result
= talloc_asprintf(talloc_tos(), "%"PRIu64
, val
);
569 SMB_ASSERT(result
!= NULL
);
573 void dump_ntquota(SMB_NTQUOTA_STRUCT
*qt
, bool _verbose
, bool _numeric
, void (*_sidtostring
)(fstring str
, struct dom_sid
*sid
, bool _numeric
))
575 TALLOC_CTX
*frame
= talloc_stackframe();
578 smb_panic("dump_ntquota() called with NULL pointer");
582 case SMB_USER_FS_QUOTA_TYPE
:
584 d_printf("File System QUOTAS:\n");
585 d_printf("Limits:\n");
586 d_printf(" Default Soft Limit: %15s\n",quota_str_static(qt
->softlim
,True
,_numeric
));
587 d_printf(" Default Hard Limit: %15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
588 d_printf("Quota Flags:\n");
589 d_printf(" Quotas Enabled: %s\n",
590 ((qt
->qflags
"AS_ENABLED
)||(qt
->qflags
"AS_DENY_DISK
))?"On":"Off");
591 d_printf(" Deny Disk: %s\n",(qt
->qflags
"AS_DENY_DISK
)?"On":"Off");
592 d_printf(" Log Soft Limit: %s\n",(qt
->qflags
"AS_LOG_THRESHOLD
)?"On":"Off");
593 d_printf(" Log Hard Limit: %s\n",(qt
->qflags
"AS_LOG_LIMIT
)?"On":"Off");
596 case SMB_USER_QUOTA_TYPE
:
598 fstring username_str
= {0};
601 _sidtostring(username_str
,&qt
->sid
,_numeric
);
603 sid_to_fstring(username_str
, &qt
->sid
);
607 d_printf("Quotas for User: %s\n",username_str
);
608 d_printf("Used Space: %15s\n",quota_str_static(qt
->usedspace
,False
,_numeric
));
609 d_printf("Soft Limit: %15s\n",quota_str_static(qt
->softlim
,True
,_numeric
));
610 d_printf("Hard Limit: %15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
612 d_printf("%-30s: ",username_str
);
613 d_printf("%15s/",quota_str_static(qt
->usedspace
,False
,_numeric
));
614 d_printf("%15s/",quota_str_static(qt
->softlim
,True
,_numeric
));
615 d_printf("%15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
620 d_printf("dump_ntquota() invalid qtype(%d)\n",qt
->qtype
);
626 void dump_ntquota_list(SMB_NTQUOTA_LIST
**qtl
, bool _verbose
, bool _numeric
, void (*_sidtostring
)(fstring str
, struct dom_sid
*sid
, bool _numeric
))
628 SMB_NTQUOTA_LIST
*cur
;
630 for (cur
= *qtl
;cur
;cur
= cur
->next
) {
632 dump_ntquota(cur
->quotas
,_verbose
,_numeric
,_sidtostring
);