2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2000
8 Copyright (C) Stefan (metze) Metzmacher 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 static pstring server
;
28 /* numeric is set when the user wants numeric SIDs and ACEs rather
29 than going via LSA calls to resolve them */
33 enum todo_values
{NOOP_QUOTA
=0,FS_QUOTA
,USER_QUOTA
,LIST_QUOTA
,SET_QUOTA
};
34 enum exit_values
{EXIT_OK
, EXIT_FAILED
, EXIT_PARSE_ERROR
};
36 static struct cli_state
*cli_ipc
;
37 static struct rpc_pipe_client
*global_pipe_hnd
;
38 static POLICY_HND pol
;
39 static BOOL got_policy_hnd
;
41 static struct cli_state
*connect_one(const char *share
);
43 /* Open cli connection and policy handle */
45 static BOOL
cli_open_policy_hnd(void)
47 /* Initialise cli LSA connection */
51 cli_ipc
= connect_one("IPC$");
52 global_pipe_hnd
= cli_rpc_pipe_open_noauth(cli_ipc
, PI_LSARPC
, &ret
);
53 if (!global_pipe_hnd
) {
58 /* Open policy handle */
60 if (!got_policy_hnd
) {
62 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
63 but NT sends 0x2000000 so we might as well do it too. */
65 if (!NT_STATUS_IS_OK(rpccli_lsa_open_policy(global_pipe_hnd
, cli_ipc
->mem_ctx
, True
,
66 GENERIC_EXECUTE_ACCESS
, &pol
))) {
70 got_policy_hnd
= True
;
76 /* convert a SID to a string, either numeric or username/group */
77 static void SidToString(fstring str
, DOM_SID
*sid
, BOOL _numeric
)
79 char **domains
= NULL
;
81 enum lsa_SidType
*types
= NULL
;
83 sid_to_string(str
, sid
);
87 /* Ask LSA to convert the sid to a name */
89 if (!cli_open_policy_hnd() ||
90 !NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(global_pipe_hnd
, cli_ipc
->mem_ctx
,
91 &pol
, 1, sid
, &domains
,
93 !domains
|| !domains
[0] || !names
|| !names
[0]) {
99 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
100 domains
[0], lp_winbind_separator(),
105 /* convert a string to a SID, either numeric or username/group */
106 static BOOL
StringToSid(DOM_SID
*sid
, const char *str
)
108 enum lsa_SidType
*types
= NULL
;
109 DOM_SID
*sids
= NULL
;
112 if (strncmp(str
, "S-", 2) == 0) {
113 return string_to_sid(sid
, str
);
116 if (!cli_open_policy_hnd() ||
117 !NT_STATUS_IS_OK(rpccli_lsa_lookup_names(global_pipe_hnd
, cli_ipc
->mem_ctx
,
118 &pol
, 1, &str
, NULL
, 1, &sids
,
124 sid_copy(sid
, &sids
[0]);
131 #define QUOTA_SETLIM 2
132 #define QUOTA_SETFLAGS 3
135 enum {PARSE_FLAGS
,PARSE_LIM
};
137 static int parse_quota_set(pstring set_str
, pstring username_str
, enum SMB_QUOTA_TYPE
*qtype
, int *cmd
, SMB_NTQUOTA_STRUCT
*pqt
)
139 char *p
= set_str
,*p2
;
145 if (strnequal(set_str
,"UQLIM:",6)) {
147 *qtype
= SMB_USER_QUOTA_TYPE
;
150 if ((p2
=strstr(p
,":"))==NULL
) {
157 fstrcpy(username_str
,p
);
159 } else if (strnequal(set_str
,"FSQLIM:",7)) {
161 *qtype
= SMB_USER_FS_QUOTA_TYPE
;
164 } else if (strnequal(set_str
,"FSQFLAGS:",9)) {
167 *qtype
= SMB_USER_FS_QUOTA_TYPE
;
168 *cmd
= QUOTA_SETFLAGS
;
175 #if defined(HAVE_LONGLONG)
176 if (sscanf(p
,"%llu/%llu",&pqt
->softlim
,&pqt
->hardlim
)!=2) {
178 if (sscanf(p
,"%lu/%lu",&pqt
->softlim
,&pqt
->hardlim
)!=2) {
187 if ((p2
=strstr(p
,"/"))==NULL
) {
194 if (strnequal(p
,"QUOTA_ENABLED",13)) {
196 } else if (strnequal(p
,"DENY_DISK",9)) {
198 } else if (strnequal(p
,"LOG_SOFTLIMIT",13)) {
199 pqt
->qflags
|= QUOTAS_LOG_THRESHOLD
;
200 } else if (strnequal(p
,"LOG_HARDLIMIT",13)) {
201 pqt
->qflags
|= QUOTAS_LOG_LIMIT
;
210 pqt
->qflags
|= QUOTAS_DENY_DISK
;
212 pqt
->qflags
|= QUOTAS_ENABLED
;
221 static int do_quota(struct cli_state
*cli
, enum SMB_QUOTA_TYPE qtype
, uint16 cmd
, pstring username_str
, SMB_NTQUOTA_STRUCT
*pqt
)
225 SMB_NTQUOTA_LIST
*qtl
= NULL
;
226 SMB_NTQUOTA_STRUCT qt
;
229 if (!cli_get_fs_attr_info(cli
, &fs_attrs
)) {
230 d_printf("Failed to get the filesystem attributes %s.\n",
235 if (!(fs_attrs
& FILE_VOLUME_QUOTAS
)) {
236 d_printf("Quotas are not supported by the server.\n");
240 if (!cli_get_quota_handle(cli
, "a_fnum
)) {
241 d_printf("Quotas are not enabled on this share.\n");
242 d_printf("Failed to open %s %s.\n",
243 FAKE_FILE_NAME_QUOTA_WIN32
,cli_errstr(cli
));
248 case SMB_USER_QUOTA_TYPE
:
249 if (!StringToSid(&qt
.sid
, username_str
)) {
250 d_printf("StringToSid() failed for [%s]\n",username_str
);
256 if (!cli_get_user_quota(cli
, quota_fnum
, &qt
)) {
257 d_printf("%s cli_get_user_quota %s\n",
258 cli_errstr(cli
),username_str
);
261 dump_ntquota(&qt
,verbose
,numeric
,SidToString
);
265 if (!cli_set_user_quota(cli
, quota_fnum
, pqt
)) {
266 d_printf("%s cli_set_user_quota %s\n",
267 cli_errstr(cli
),username_str
);
270 if (!cli_get_user_quota(cli
, quota_fnum
, &qt
)) {
271 d_printf("%s cli_get_user_quota %s\n",
272 cli_errstr(cli
),username_str
);
275 dump_ntquota(&qt
,verbose
,numeric
,SidToString
);
278 if (!cli_list_user_quota(cli
, quota_fnum
, &qtl
)) {
279 d_printf("%s cli_set_user_quota %s\n",
280 cli_errstr(cli
),username_str
);
283 dump_ntquota_list(&qtl
,verbose
,numeric
,SidToString
);
284 free_ntquota_list(&qtl
);
287 d_printf("Unknown Error\n");
291 case SMB_USER_FS_QUOTA_TYPE
:
294 if (!cli_get_fs_quota_info(cli
, quota_fnum
, &qt
)) {
295 d_printf("%s cli_get_fs_quota_info\n",
299 dump_ntquota(&qt
,True
,numeric
,NULL
);
302 if (!cli_get_fs_quota_info(cli
, quota_fnum
, &qt
)) {
303 d_printf("%s cli_get_fs_quota_info\n",
307 qt
.softlim
= pqt
->softlim
;
308 qt
.hardlim
= pqt
->hardlim
;
309 if (!cli_set_fs_quota_info(cli
, quota_fnum
, &qt
)) {
310 d_printf("%s cli_set_fs_quota_info\n",
314 if (!cli_get_fs_quota_info(cli
, quota_fnum
, &qt
)) {
315 d_printf("%s cli_get_fs_quota_info\n",
319 dump_ntquota(&qt
,True
,numeric
,NULL
);
322 if (!cli_get_fs_quota_info(cli
, quota_fnum
, &qt
)) {
323 d_printf("%s cli_get_fs_quota_info\n",
327 qt
.qflags
= pqt
->qflags
;
328 if (!cli_set_fs_quota_info(cli
, quota_fnum
, &qt
)) {
329 d_printf("%s cli_set_fs_quota_info\n",
333 if (!cli_get_fs_quota_info(cli
, quota_fnum
, &qt
)) {
334 d_printf("%s cli_get_fs_quota_info\n",
338 dump_ntquota(&qt
,True
,numeric
,NULL
);
341 d_printf("Unknown Error\n");
346 d_printf("Unknown Error\n");
350 cli_close(cli
, quota_fnum
);
355 /*****************************************************
356 return a connection to a server
357 *******************************************************/
358 static struct cli_state
*connect_one(const char *share
)
365 if (!cmdline_auth_info
.got_pass
) {
366 char *pass
= getpass("Password: ");
368 pstrcpy(cmdline_auth_info
.password
, pass
);
369 cmdline_auth_info
.got_pass
= True
;
373 if (NT_STATUS_IS_OK(nt_status
= cli_full_connection(&c
, global_myname(), server
,
376 cmdline_auth_info
.username
, lp_workgroup(),
377 cmdline_auth_info
.password
, 0,
378 cmdline_auth_info
.signing_state
, NULL
))) {
381 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status
)));
386 /****************************************************************************
388 ****************************************************************************/
389 int main(int argc
, const char *argv
[])
395 pstring username_str
= {0};
397 pstring set_str
= {0};
398 enum SMB_QUOTA_TYPE qtype
= SMB_INVALID_QUOTA_TYPE
;
400 static BOOL test_args
= False
;
401 struct cli_state
*cli
;
402 BOOL fix_user
= False
;
403 SMB_NTQUOTA_STRUCT qt
;
404 TALLOC_CTX
*frame
= talloc_stackframe();
406 struct poptOption long_options
[] = {
408 { "user", 'u', POPT_ARG_STRING
, NULL
, 'u', "Show quotas for user", "user" },
409 { "list", 'L', POPT_ARG_NONE
, NULL
, 'L', "List user quotas" },
410 { "fs", 'F', POPT_ARG_NONE
, NULL
, 'F', "Show filesystem quotas" },
411 { "set", 'S', POPT_ARG_STRING
, NULL
, 'S', "Set acls\n\
413 UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
414 FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
415 FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
416 { "numeric", 'n', POPT_ARG_NONE
, &numeric
, True
, "Don't resolve sids or limits to names" },
417 { "verbose", 'v', POPT_ARG_NONE
, &verbose
, True
, "be verbose" },
418 { "test-args", 't', POPT_ARG_NONE
, &test_args
, True
, "Test arguments"},
420 POPT_COMMON_CREDENTIALS
428 /* set default debug level to 1 regardless of what smb.conf sets */
429 setup_logging( "smbcquotas", True
);
430 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 1;
432 x_setbuf( x_stderr
, NULL
);
438 lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
);
441 pc
= poptGetContext("smbcquotas", argc
, argv
, long_options
, 0);
443 poptSetOtherOptionHelp(pc
, "//server1/share1");
445 while ((opt
= poptGetNextOpt(pc
)) != -1) {
449 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
450 exit(EXIT_PARSE_ERROR
);
457 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
458 exit(EXIT_PARSE_ERROR
);
465 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
466 exit(EXIT_PARSE_ERROR
);
468 pstrcpy(username_str
,poptGetOptArg(pc
));
475 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
476 exit(EXIT_PARSE_ERROR
);
478 pstrcpy(set_str
,poptGetOptArg(pc
));
488 pstrcpy(username_str
,cmdline_auth_info
.username
);
490 /* Make connection to server */
491 if(!poptPeekArg(pc
)) {
492 poptPrintUsage(pc
, stderr
, 0);
493 exit(EXIT_PARSE_ERROR
);
496 pstrcpy(path
, poptGetArg(pc
));
498 all_string_sub(path
,"/","\\",0);
500 pstrcpy(server
,path
+2);
501 share
= strchr_m(server
,'\\');
503 printf("Invalid argument: %s\n", share
);
504 exit(EXIT_PARSE_ERROR
);
510 if (todo
== SET_QUOTA
) {
511 if (parse_quota_set(set_str
, username_str
, &qtype
, &cmd
, &qt
)) {
512 printf("Invalid argument: -S %s\n", set_str
);
513 exit(EXIT_PARSE_ERROR
);
518 cli
= connect_one(share
);
527 /* Perform requested action */
531 result
= do_quota(cli
,SMB_USER_FS_QUOTA_TYPE
, QUOTA_GET
, username_str
, NULL
);
534 result
= do_quota(cli
,SMB_USER_QUOTA_TYPE
, QUOTA_LIST
, username_str
, NULL
);
537 result
= do_quota(cli
,SMB_USER_QUOTA_TYPE
, QUOTA_GET
, username_str
, NULL
);
540 result
= do_quota(cli
, qtype
, cmd
, username_str
, &qt
);
544 result
= EXIT_FAILED
;