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"
23 NTSTATUS
cli_get_quota_handle(struct cli_state
*cli
, uint16_t *quota_fnum
)
25 return cli_ntcreate(cli
, FAKE_FILE_NAME_QUOTA_WIN32
,
26 0x00000016, DESIRED_ACCESS_PIPE
,
27 0x00000000, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
28 FILE_OPEN
, 0x00000000, 0x03, quota_fnum
);
31 void free_ntquota_list(SMB_NTQUOTA_LIST
**qt_list
)
36 if ((*qt_list
)->mem_ctx
)
37 talloc_destroy((*qt_list
)->mem_ctx
);
44 static bool parse_user_quota_record(const char *rdata
, unsigned int rdata_count
, unsigned int *offset
, SMB_NTQUOTA_STRUCT
*pqt
)
47 SMB_NTQUOTA_STRUCT qt
;
51 if (!rdata
||!offset
||!pqt
) {
52 smb_panic("parse_quota_record: called with NULL POINTER!");
55 if (rdata_count
< 40) {
59 /* offset to next quota record.
60 * 4 bytes IVAL(rdata,0)
63 *offset
= IVAL(rdata
,0);
66 sid_len
= IVAL(rdata
,4);
68 if (rdata_count
< 40+sid_len
) {
72 /* unknown 8 bytes in pdata
73 * maybe its the change time in NTTIME
76 /* the used space 8 bytes (uint64_t)*/
77 qt
.usedspace
= (uint64_t)IVAL(rdata
,16);
78 #ifdef LARGE_SMB_OFF_T
79 qt
.usedspace
|= (((uint64_t)IVAL(rdata
,20)) << 32);
80 #else /* LARGE_SMB_OFF_T */
81 if ((IVAL(rdata
,20) != 0)&&
82 ((qt
.usedspace
!= 0xFFFFFFFF)||
83 (IVAL(rdata
,20)!=0xFFFFFFFF))) {
84 /* more than 32 bits? */
87 #endif /* LARGE_SMB_OFF_T */
89 /* the soft quotas 8 bytes (uint64_t)*/
90 qt
.softlim
= (uint64_t)IVAL(rdata
,24);
91 #ifdef LARGE_SMB_OFF_T
92 qt
.softlim
|= (((uint64_t)IVAL(rdata
,28)) << 32);
93 #else /* LARGE_SMB_OFF_T */
94 if ((IVAL(rdata
,28) != 0)&&
95 ((qt
.softlim
!= 0xFFFFFFFF)||
96 (IVAL(rdata
,28)!=0xFFFFFFFF))) {
97 /* more than 32 bits? */
100 #endif /* LARGE_SMB_OFF_T */
102 /* the hard quotas 8 bytes (uint64_t)*/
103 qt
.hardlim
= (uint64_t)IVAL(rdata
,32);
104 #ifdef LARGE_SMB_OFF_T
105 qt
.hardlim
|= (((uint64_t)IVAL(rdata
,36)) << 32);
106 #else /* LARGE_SMB_OFF_T */
107 if ((IVAL(rdata
,36) != 0)&&
108 ((qt
.hardlim
!= 0xFFFFFFFF)||
109 (IVAL(rdata
,36)!=0xFFFFFFFF))) {
110 /* more than 32 bits? */
113 #endif /* LARGE_SMB_OFF_T */
115 sid_parse(rdata
+40,sid_len
,&qt
.sid
);
117 qt
.qtype
= SMB_USER_QUOTA_TYPE
;
124 bool cli_get_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
129 unsigned int data_len
;
130 char data
[SID_MAX_SIZE
+8];
131 char *rparam
=NULL
, *rdata
=NULL
;
132 unsigned int rparam_count
=0, rdata_count
=0;
133 unsigned int sid_len
;
137 smb_panic("cli_get_user_quota() called with NULL Pointer!");
140 setup
= NT_TRANSACT_GET_USER_QUOTA
;
142 SSVAL(params
, 0,quota_fnum
);
143 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_FOR_SID
);
144 SIVAL(params
, 4,0x00000024);
145 SIVAL(params
, 8,0x00000000);
146 SIVAL(params
,12,0x00000024);
148 sid_len
= ndr_size_dom_sid(&pqt
->sid
, 0);
149 data_len
= sid_len
+8;
150 SIVAL(data
, 0, 0x00000000);
151 SIVAL(data
, 4, sid_len
);
152 sid_linearize(data
+8, sid_len
, &pqt
->sid
);
154 if (!cli_send_nt_trans(cli
,
155 NT_TRANSACT_GET_USER_QUOTA
,
159 data
, data_len
, 112)) {
160 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
165 if (!cli_receive_nt_trans(cli
,
166 &rparam
, &rparam_count
,
167 &rdata
, &rdata_count
)) {
168 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
172 if (cli_is_error(cli
)) {
179 if ((rparam
&&rdata
)&&(rparam_count
>=4&&rdata_count
>=8)) {
180 ret
= parse_user_quota_record(rdata
, rdata_count
, &offset
, pqt
);
182 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
192 bool cli_set_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
198 char *rparam
=NULL
, *rdata
=NULL
;
199 unsigned int rparam_count
=0, rdata_count
=0;
200 unsigned int sid_len
;
201 memset(data
,'\0',112);
204 smb_panic("cli_set_user_quota() called with NULL Pointer!");
207 setup
= NT_TRANSACT_SET_USER_QUOTA
;
209 SSVAL(params
,0,quota_fnum
);
211 sid_len
= ndr_size_dom_sid(&pqt
->sid
, 0);
213 SIVAL(data
,4,sid_len
);
214 SBIG_UINT(data
, 8,(uint64_t)0);
215 SBIG_UINT(data
,16,pqt
->usedspace
);
216 SBIG_UINT(data
,24,pqt
->softlim
);
217 SBIG_UINT(data
,32,pqt
->hardlim
);
218 sid_linearize(data
+40, sid_len
, &pqt
->sid
);
220 if (!cli_send_nt_trans(cli
,
221 NT_TRANSACT_SET_USER_QUOTA
,
226 DEBUG(1,("Failed to send NT_TRANSACT_SET_USER_QUOTA\n"));
231 if (!cli_receive_nt_trans(cli
,
232 &rparam
, &rparam_count
,
233 &rdata
, &rdata_count
)) {
234 DEBUG(1,("NT_TRANSACT_SET_USER_QUOTA failed\n"));
238 if (cli_is_error(cli
)) {
251 bool cli_list_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_LIST
**pqt_list
)
256 char *rparam
=NULL
, *rdata
=NULL
;
257 unsigned int rparam_count
=0, rdata_count
=0;
259 const char *curdata
= NULL
;
260 unsigned int curdata_count
= 0;
261 TALLOC_CTX
*mem_ctx
= NULL
;
262 SMB_NTQUOTA_STRUCT qt
;
263 SMB_NTQUOTA_LIST
*tmp_list_ent
;
265 if (!cli
||!pqt_list
) {
266 smb_panic("cli_list_user_quota() called with NULL Pointer!");
269 setup
= NT_TRANSACT_GET_USER_QUOTA
;
271 SSVAL(params
, 0,quota_fnum
);
272 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_LIST_START
);
273 SIVAL(params
, 4,0x00000000);
274 SIVAL(params
, 8,0x00000000);
275 SIVAL(params
,12,0x00000000);
277 if (!cli_send_nt_trans(cli
,
278 NT_TRANSACT_GET_USER_QUOTA
,
283 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
288 if (!cli_receive_nt_trans(cli
,
289 &rparam
, &rparam_count
,
290 &rdata
, &rdata_count
)) {
291 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
295 if (cli_is_error(cli
)) {
302 if (rdata_count
== 0) {
307 if ((mem_ctx
=talloc_init("SMB_USER_QUOTA_LIST"))==NULL
) {
308 DEBUG(0,("talloc_init() failed\n"));
313 for (curdata
=rdata
,curdata_count
=rdata_count
;
314 ((curdata
)&&(curdata_count
>=8)&&(offset
>0));
315 curdata
+=offset
,curdata_count
-= offset
) {
317 if (!parse_user_quota_record(curdata
, curdata_count
, &offset
, &qt
)) {
318 DEBUG(1,("Failed to parse the quota record\n"));
322 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
323 DEBUG(0,("TALLOC_ZERO() failed\n"));
324 talloc_destroy(mem_ctx
);
328 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
329 DEBUG(0,("TALLOC_ZERO() failed\n"));
330 talloc_destroy(mem_ctx
);
334 memcpy(tmp_list_ent
->quotas
,&qt
,sizeof(qt
));
335 tmp_list_ent
->mem_ctx
= mem_ctx
;
337 DLIST_ADD((*pqt_list
),tmp_list_ent
);
340 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
);
342 if (!cli_send_nt_trans(cli
,
343 NT_TRANSACT_GET_USER_QUOTA
,
348 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
354 if (!cli_receive_nt_trans(cli
,
355 &rparam
, &rparam_count
,
356 &rdata
, &rdata_count
)) {
357 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
361 if (cli_is_error(cli
)) {
368 if (rdata_count
== 0) {
373 for (curdata
=rdata
,curdata_count
=rdata_count
;
374 ((curdata
)&&(curdata_count
>=8)&&(offset
>0));
375 curdata
+=offset
,curdata_count
-= offset
) {
377 if (!parse_user_quota_record(curdata
, curdata_count
, &offset
, &qt
)) {
378 DEBUG(1,("Failed to parse the quota record\n"));
382 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
383 DEBUG(0,("TALLOC_ZERO() failed\n"));
384 talloc_destroy(mem_ctx
);
388 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
389 DEBUG(0,("TALLOC_ZERO() failed\n"));
390 talloc_destroy(mem_ctx
);
394 memcpy(tmp_list_ent
->quotas
,&qt
,sizeof(qt
));
395 tmp_list_ent
->mem_ctx
= mem_ctx
;
397 DLIST_ADD((*pqt_list
),tmp_list_ent
);
410 bool cli_get_fs_quota_info(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
415 char *rparam
=NULL
, *rdata
=NULL
;
416 unsigned int rparam_count
=0, rdata_count
=0;
417 SMB_NTQUOTA_STRUCT qt
;
421 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
424 setup
= TRANSACT2_QFSINFO
;
426 SSVAL(param
,0,SMB_FS_QUOTA_INFORMATION
);
428 if (!cli_send_trans(cli
, SMBtrans2
,
437 if (!cli_receive_trans(cli
, SMBtrans2
,
438 &rparam
, &rparam_count
,
439 &rdata
, &rdata_count
)) {
443 if (cli_is_error(cli
)) {
450 if (rdata_count
< 48) {
454 /* unknown_1 24 NULL bytes in pdata*/
456 /* the soft quotas 8 bytes (uint64_t)*/
457 qt
.softlim
= (uint64_t)IVAL(rdata
,24);
458 #ifdef LARGE_SMB_OFF_T
459 qt
.softlim
|= (((uint64_t)IVAL(rdata
,28)) << 32);
460 #else /* LARGE_SMB_OFF_T */
461 if ((IVAL(rdata
,28) != 0)&&
462 ((qt
.softlim
!= 0xFFFFFFFF)||
463 (IVAL(rdata
,28)!=0xFFFFFFFF))) {
464 /* more than 32 bits? */
467 #endif /* LARGE_SMB_OFF_T */
469 /* the hard quotas 8 bytes (uint64_t)*/
470 qt
.hardlim
= (uint64_t)IVAL(rdata
,32);
471 #ifdef LARGE_SMB_OFF_T
472 qt
.hardlim
|= (((uint64_t)IVAL(rdata
,36)) << 32);
473 #else /* LARGE_SMB_OFF_T */
474 if ((IVAL(rdata
,36) != 0)&&
475 ((qt
.hardlim
!= 0xFFFFFFFF)||
476 (IVAL(rdata
,36)!=0xFFFFFFFF))) {
477 /* more than 32 bits? */
480 #endif /* LARGE_SMB_OFF_T */
482 /* quota_flags 2 bytes **/
483 qt
.qflags
= SVAL(rdata
,40);
485 qt
.qtype
= SMB_USER_FS_QUOTA_TYPE
;
497 bool cli_set_fs_quota_info(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
503 char *rparam
=NULL
, *rdata
=NULL
;
504 unsigned int rparam_count
=0, rdata_count
=0;
505 SMB_NTQUOTA_STRUCT qt
;
507 memset(data
,'\0',48);
510 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
,
540 if (!cli_receive_trans(cli
, SMBtrans2
,
541 &rparam
, &rparam_count
,
542 &rdata
, &rdata_count
)) {
546 if (cli_is_error(cli
)) {
560 static const char *quota_str_static(uint64_t val
, bool special
, bool _numeric
)
564 if (!_numeric
&&special
&&(val
== SMB_NTQUOTAS_NO_LIMIT
)) {
567 result
= talloc_asprintf(talloc_tos(), "%"PRIu64
, val
);
568 SMB_ASSERT(result
!= NULL
);
572 void dump_ntquota(SMB_NTQUOTA_STRUCT
*qt
, bool _verbose
, bool _numeric
, void (*_sidtostring
)(fstring str
, struct dom_sid
*sid
, bool _numeric
))
574 TALLOC_CTX
*frame
= talloc_stackframe();
577 smb_panic("dump_ntquota() called with NULL pointer");
581 case SMB_USER_FS_QUOTA_TYPE
:
583 d_printf("File System QUOTAS:\n");
584 d_printf("Limits:\n");
585 d_printf(" Default Soft Limit: %15s\n",quota_str_static(qt
->softlim
,True
,_numeric
));
586 d_printf(" Default Hard Limit: %15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
587 d_printf("Quota Flags:\n");
588 d_printf(" Quotas Enabled: %s\n",
589 ((qt
->qflags
"AS_ENABLED
)||(qt
->qflags
"AS_DENY_DISK
))?"On":"Off");
590 d_printf(" Deny Disk: %s\n",(qt
->qflags
"AS_DENY_DISK
)?"On":"Off");
591 d_printf(" Log Soft Limit: %s\n",(qt
->qflags
"AS_LOG_THRESHOLD
)?"On":"Off");
592 d_printf(" Log Hard Limit: %s\n",(qt
->qflags
"AS_LOG_LIMIT
)?"On":"Off");
595 case SMB_USER_QUOTA_TYPE
:
597 fstring username_str
= {0};
600 _sidtostring(username_str
,&qt
->sid
,_numeric
);
602 sid_to_fstring(username_str
, &qt
->sid
);
606 d_printf("Quotas for User: %s\n",username_str
);
607 d_printf("Used Space: %15s\n",quota_str_static(qt
->usedspace
,False
,_numeric
));
608 d_printf("Soft Limit: %15s\n",quota_str_static(qt
->softlim
,True
,_numeric
));
609 d_printf("Hard Limit: %15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
611 d_printf("%-30s: ",username_str
);
612 d_printf("%15s/",quota_str_static(qt
->usedspace
,False
,_numeric
));
613 d_printf("%15s/",quota_str_static(qt
->softlim
,True
,_numeric
));
614 d_printf("%15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
619 d_printf("dump_ntquota() invalid qtype(%d)\n",qt
->qtype
);
625 void dump_ntquota_list(SMB_NTQUOTA_LIST
**qtl
, bool _verbose
, bool _numeric
, void (*_sidtostring
)(fstring str
, struct dom_sid
*sid
, bool _numeric
))
627 SMB_NTQUOTA_LIST
*cur
;
629 for (cur
= *qtl
;cur
;cur
= cur
->next
) {
631 dump_ntquota(cur
->quotas
,_verbose
,_numeric
,_sidtostring
);