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/>.
22 NTSTATUS
cli_get_quota_handle(struct cli_state
*cli
, uint16_t *quota_fnum
)
24 return cli_ntcreate(cli
, FAKE_FILE_NAME_QUOTA_WIN32
,
25 0x00000016, DESIRED_ACCESS_PIPE
,
26 0x00000000, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
27 FILE_OPEN
, 0x00000000, 0x03, quota_fnum
);
30 void free_ntquota_list(SMB_NTQUOTA_LIST
**qt_list
)
35 if ((*qt_list
)->mem_ctx
)
36 talloc_destroy((*qt_list
)->mem_ctx
);
43 static bool parse_user_quota_record(const char *rdata
, unsigned int rdata_count
, unsigned int *offset
, SMB_NTQUOTA_STRUCT
*pqt
)
46 SMB_NTQUOTA_STRUCT qt
;
50 if (!rdata
||!offset
||!pqt
) {
51 smb_panic("parse_quota_record: called with NULL POINTER!");
54 if (rdata_count
< 40) {
58 /* offset to next quota record.
59 * 4 bytes IVAL(rdata,0)
62 *offset
= IVAL(rdata
,0);
65 sid_len
= IVAL(rdata
,4);
67 if (rdata_count
< 40+sid_len
) {
71 /* unknown 8 bytes in pdata
72 * maybe its the change time in NTTIME
75 /* the used space 8 bytes (uint64_t)*/
76 qt
.usedspace
= (uint64_t)IVAL(rdata
,16);
77 #ifdef LARGE_SMB_OFF_T
78 qt
.usedspace
|= (((uint64_t)IVAL(rdata
,20)) << 32);
79 #else /* LARGE_SMB_OFF_T */
80 if ((IVAL(rdata
,20) != 0)&&
81 ((qt
.usedspace
!= 0xFFFFFFFF)||
82 (IVAL(rdata
,20)!=0xFFFFFFFF))) {
83 /* more than 32 bits? */
86 #endif /* LARGE_SMB_OFF_T */
88 /* the soft quotas 8 bytes (uint64_t)*/
89 qt
.softlim
= (uint64_t)IVAL(rdata
,24);
90 #ifdef LARGE_SMB_OFF_T
91 qt
.softlim
|= (((uint64_t)IVAL(rdata
,28)) << 32);
92 #else /* LARGE_SMB_OFF_T */
93 if ((IVAL(rdata
,28) != 0)&&
94 ((qt
.softlim
!= 0xFFFFFFFF)||
95 (IVAL(rdata
,28)!=0xFFFFFFFF))) {
96 /* more than 32 bits? */
99 #endif /* LARGE_SMB_OFF_T */
101 /* the hard quotas 8 bytes (uint64_t)*/
102 qt
.hardlim
= (uint64_t)IVAL(rdata
,32);
103 #ifdef LARGE_SMB_OFF_T
104 qt
.hardlim
|= (((uint64_t)IVAL(rdata
,36)) << 32);
105 #else /* LARGE_SMB_OFF_T */
106 if ((IVAL(rdata
,36) != 0)&&
107 ((qt
.hardlim
!= 0xFFFFFFFF)||
108 (IVAL(rdata
,36)!=0xFFFFFFFF))) {
109 /* more than 32 bits? */
112 #endif /* LARGE_SMB_OFF_T */
114 sid_parse(rdata
+40,sid_len
,&qt
.sid
);
116 qt
.qtype
= SMB_USER_QUOTA_TYPE
;
123 bool cli_get_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
128 unsigned int data_len
;
129 char data
[SID_MAX_SIZE
+8];
130 char *rparam
=NULL
, *rdata
=NULL
;
131 unsigned int rparam_count
=0, rdata_count
=0;
132 unsigned int sid_len
;
136 smb_panic("cli_get_user_quota() called with NULL Pointer!");
139 setup
= NT_TRANSACT_GET_USER_QUOTA
;
141 SSVAL(params
, 0,quota_fnum
);
142 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_FOR_SID
);
143 SIVAL(params
, 4,0x00000024);
144 SIVAL(params
, 8,0x00000000);
145 SIVAL(params
,12,0x00000024);
147 sid_len
= ndr_size_dom_sid(&pqt
->sid
, 0);
148 data_len
= sid_len
+8;
149 SIVAL(data
, 0, 0x00000000);
150 SIVAL(data
, 4, sid_len
);
151 sid_linearize(data
+8, sid_len
, &pqt
->sid
);
153 if (!cli_send_nt_trans(cli
,
154 NT_TRANSACT_GET_USER_QUOTA
,
158 data
, data_len
, 112)) {
159 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
164 if (!cli_receive_nt_trans(cli
,
165 &rparam
, &rparam_count
,
166 &rdata
, &rdata_count
)) {
167 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
171 if (cli_is_error(cli
)) {
178 if ((rparam
&&rdata
)&&(rparam_count
>=4&&rdata_count
>=8)) {
179 ret
= parse_user_quota_record(rdata
, rdata_count
, &offset
, pqt
);
181 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
191 bool cli_set_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
197 char *rparam
=NULL
, *rdata
=NULL
;
198 unsigned int rparam_count
=0, rdata_count
=0;
199 unsigned int sid_len
;
200 memset(data
,'\0',112);
203 smb_panic("cli_set_user_quota() called with NULL Pointer!");
206 setup
= NT_TRANSACT_SET_USER_QUOTA
;
208 SSVAL(params
,0,quota_fnum
);
210 sid_len
= ndr_size_dom_sid(&pqt
->sid
, 0);
212 SIVAL(data
,4,sid_len
);
213 SBIG_UINT(data
, 8,(uint64_t)0);
214 SBIG_UINT(data
,16,pqt
->usedspace
);
215 SBIG_UINT(data
,24,pqt
->softlim
);
216 SBIG_UINT(data
,32,pqt
->hardlim
);
217 sid_linearize(data
+40, sid_len
, &pqt
->sid
);
219 if (!cli_send_nt_trans(cli
,
220 NT_TRANSACT_SET_USER_QUOTA
,
225 DEBUG(1,("Failed to send NT_TRANSACT_SET_USER_QUOTA\n"));
230 if (!cli_receive_nt_trans(cli
,
231 &rparam
, &rparam_count
,
232 &rdata
, &rdata_count
)) {
233 DEBUG(1,("NT_TRANSACT_SET_USER_QUOTA failed\n"));
237 if (cli_is_error(cli
)) {
250 bool cli_list_user_quota(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_LIST
**pqt_list
)
255 char *rparam
=NULL
, *rdata
=NULL
;
256 unsigned int rparam_count
=0, rdata_count
=0;
258 const char *curdata
= NULL
;
259 unsigned int curdata_count
= 0;
260 TALLOC_CTX
*mem_ctx
= NULL
;
261 SMB_NTQUOTA_STRUCT qt
;
262 SMB_NTQUOTA_LIST
*tmp_list_ent
;
264 if (!cli
||!pqt_list
) {
265 smb_panic("cli_list_user_quota() called with NULL Pointer!");
268 setup
= NT_TRANSACT_GET_USER_QUOTA
;
270 SSVAL(params
, 0,quota_fnum
);
271 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_LIST_START
);
272 SIVAL(params
, 4,0x00000000);
273 SIVAL(params
, 8,0x00000000);
274 SIVAL(params
,12,0x00000000);
276 if (!cli_send_nt_trans(cli
,
277 NT_TRANSACT_GET_USER_QUOTA
,
282 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
287 if (!cli_receive_nt_trans(cli
,
288 &rparam
, &rparam_count
,
289 &rdata
, &rdata_count
)) {
290 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
294 if (cli_is_error(cli
)) {
301 if (rdata_count
== 0) {
306 if ((mem_ctx
=talloc_init("SMB_USER_QUOTA_LIST"))==NULL
) {
307 DEBUG(0,("talloc_init() failed\n"));
312 for (curdata
=rdata
,curdata_count
=rdata_count
;
313 ((curdata
)&&(curdata_count
>=8)&&(offset
>0));
314 curdata
+=offset
,curdata_count
-= offset
) {
316 if (!parse_user_quota_record(curdata
, curdata_count
, &offset
, &qt
)) {
317 DEBUG(1,("Failed to parse the quota record\n"));
321 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
322 DEBUG(0,("TALLOC_ZERO() failed\n"));
323 talloc_destroy(mem_ctx
);
327 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
328 DEBUG(0,("TALLOC_ZERO() failed\n"));
329 talloc_destroy(mem_ctx
);
333 memcpy(tmp_list_ent
->quotas
,&qt
,sizeof(qt
));
334 tmp_list_ent
->mem_ctx
= mem_ctx
;
336 DLIST_ADD((*pqt_list
),tmp_list_ent
);
339 SSVAL(params
, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
);
341 if (!cli_send_nt_trans(cli
,
342 NT_TRANSACT_GET_USER_QUOTA
,
347 DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
353 if (!cli_receive_nt_trans(cli
,
354 &rparam
, &rparam_count
,
355 &rdata
, &rdata_count
)) {
356 DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
360 if (cli_is_error(cli
)) {
367 if (rdata_count
== 0) {
372 for (curdata
=rdata
,curdata_count
=rdata_count
;
373 ((curdata
)&&(curdata_count
>=8)&&(offset
>0));
374 curdata
+=offset
,curdata_count
-= offset
) {
376 if (!parse_user_quota_record(curdata
, curdata_count
, &offset
, &qt
)) {
377 DEBUG(1,("Failed to parse the quota record\n"));
381 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
382 DEBUG(0,("TALLOC_ZERO() failed\n"));
383 talloc_destroy(mem_ctx
);
387 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
388 DEBUG(0,("TALLOC_ZERO() failed\n"));
389 talloc_destroy(mem_ctx
);
393 memcpy(tmp_list_ent
->quotas
,&qt
,sizeof(qt
));
394 tmp_list_ent
->mem_ctx
= mem_ctx
;
396 DLIST_ADD((*pqt_list
),tmp_list_ent
);
409 bool cli_get_fs_quota_info(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
414 char *rparam
=NULL
, *rdata
=NULL
;
415 unsigned int rparam_count
=0, rdata_count
=0;
416 SMB_NTQUOTA_STRUCT qt
;
420 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
423 setup
= TRANSACT2_QFSINFO
;
425 SSVAL(param
,0,SMB_FS_QUOTA_INFORMATION
);
427 if (!cli_send_trans(cli
, SMBtrans2
,
436 if (!cli_receive_trans(cli
, SMBtrans2
,
437 &rparam
, &rparam_count
,
438 &rdata
, &rdata_count
)) {
442 if (cli_is_error(cli
)) {
449 if (rdata_count
< 48) {
453 /* unknown_1 24 NULL bytes in pdata*/
455 /* the soft quotas 8 bytes (uint64_t)*/
456 qt
.softlim
= (uint64_t)IVAL(rdata
,24);
457 #ifdef LARGE_SMB_OFF_T
458 qt
.softlim
|= (((uint64_t)IVAL(rdata
,28)) << 32);
459 #else /* LARGE_SMB_OFF_T */
460 if ((IVAL(rdata
,28) != 0)&&
461 ((qt
.softlim
!= 0xFFFFFFFF)||
462 (IVAL(rdata
,28)!=0xFFFFFFFF))) {
463 /* more than 32 bits? */
466 #endif /* LARGE_SMB_OFF_T */
468 /* the hard quotas 8 bytes (uint64_t)*/
469 qt
.hardlim
= (uint64_t)IVAL(rdata
,32);
470 #ifdef LARGE_SMB_OFF_T
471 qt
.hardlim
|= (((uint64_t)IVAL(rdata
,36)) << 32);
472 #else /* LARGE_SMB_OFF_T */
473 if ((IVAL(rdata
,36) != 0)&&
474 ((qt
.hardlim
!= 0xFFFFFFFF)||
475 (IVAL(rdata
,36)!=0xFFFFFFFF))) {
476 /* more than 32 bits? */
479 #endif /* LARGE_SMB_OFF_T */
481 /* quota_flags 2 bytes **/
482 qt
.qflags
= SVAL(rdata
,40);
484 qt
.qtype
= SMB_USER_FS_QUOTA_TYPE
;
496 bool cli_set_fs_quota_info(struct cli_state
*cli
, int quota_fnum
, SMB_NTQUOTA_STRUCT
*pqt
)
502 char *rparam
=NULL
, *rdata
=NULL
;
503 unsigned int rparam_count
=0, rdata_count
=0;
504 SMB_NTQUOTA_STRUCT qt
;
506 memset(data
,'\0',48);
509 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
512 setup
= TRANSACT2_SETFSINFO
;
514 SSVAL(param
,0,quota_fnum
);
515 SSVAL(param
,2,SMB_FS_QUOTA_INFORMATION
);
517 /* Unknown1 24 NULL bytes*/
519 /* Default Soft Quota 8 bytes */
520 SBIG_UINT(data
,24,pqt
->softlim
);
522 /* Default Hard Quota 8 bytes */
523 SBIG_UINT(data
,32,pqt
->hardlim
);
525 /* Quota flag 2 bytes */
526 SSVAL(data
,40,pqt
->qflags
);
528 /* Unknown3 6 NULL bytes */
530 if (!cli_send_trans(cli
, SMBtrans2
,
539 if (!cli_receive_trans(cli
, SMBtrans2
,
540 &rparam
, &rparam_count
,
541 &rdata
, &rdata_count
)) {
545 if (cli_is_error(cli
)) {
559 static const char *quota_str_static(uint64_t val
, bool special
, bool _numeric
)
563 if (!_numeric
&&special
&&(val
== SMB_NTQUOTAS_NO_LIMIT
)) {
566 result
= talloc_asprintf(talloc_tos(), "%"PRIu64
, val
);
567 SMB_ASSERT(result
!= NULL
);
571 void dump_ntquota(SMB_NTQUOTA_STRUCT
*qt
, bool _verbose
, bool _numeric
, void (*_sidtostring
)(fstring str
, DOM_SID
*sid
, bool _numeric
))
573 TALLOC_CTX
*frame
= talloc_stackframe();
576 smb_panic("dump_ntquota() called with NULL pointer");
580 case SMB_USER_FS_QUOTA_TYPE
:
582 d_printf("File System QUOTAS:\n");
583 d_printf("Limits:\n");
584 d_printf(" Default Soft Limit: %15s\n",quota_str_static(qt
->softlim
,True
,_numeric
));
585 d_printf(" Default Hard Limit: %15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
586 d_printf("Quota Flags:\n");
587 d_printf(" Quotas Enabled: %s\n",
588 ((qt
->qflags
"AS_ENABLED
)||(qt
->qflags
"AS_DENY_DISK
))?"On":"Off");
589 d_printf(" Deny Disk: %s\n",(qt
->qflags
"AS_DENY_DISK
)?"On":"Off");
590 d_printf(" Log Soft Limit: %s\n",(qt
->qflags
"AS_LOG_THRESHOLD
)?"On":"Off");
591 d_printf(" Log Hard Limit: %s\n",(qt
->qflags
"AS_LOG_LIMIT
)?"On":"Off");
594 case SMB_USER_QUOTA_TYPE
:
596 fstring username_str
= {0};
599 _sidtostring(username_str
,&qt
->sid
,_numeric
);
601 sid_to_fstring(username_str
, &qt
->sid
);
605 d_printf("Quotas for User: %s\n",username_str
);
606 d_printf("Used Space: %15s\n",quota_str_static(qt
->usedspace
,False
,_numeric
));
607 d_printf("Soft Limit: %15s\n",quota_str_static(qt
->softlim
,True
,_numeric
));
608 d_printf("Hard Limit: %15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
610 d_printf("%-30s: ",username_str
);
611 d_printf("%15s/",quota_str_static(qt
->usedspace
,False
,_numeric
));
612 d_printf("%15s/",quota_str_static(qt
->softlim
,True
,_numeric
));
613 d_printf("%15s\n",quota_str_static(qt
->hardlim
,True
,_numeric
));
618 d_printf("dump_ntquota() invalid qtype(%d)\n",qt
->qtype
);
624 void dump_ntquota_list(SMB_NTQUOTA_LIST
**qtl
, bool _verbose
, bool _numeric
, void (*_sidtostring
)(fstring str
, DOM_SID
*sid
, bool _numeric
))
626 SMB_NTQUOTA_LIST
*cur
;
628 for (cur
= *qtl
;cur
;cur
= cur
->next
) {
630 dump_ntquota(cur
->quotas
,_verbose
,_numeric
,_sidtostring
);