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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "module_dummy.h"
29 static pstring server;
31 /* numeric is set when the user wants numeric SIDs and ACEs rather
32 than going via LSA calls to resolve them */
36 enum todo_values {NOOP_QUOTA=0,FS_QUOTA,USER_QUOTA,LIST_QUOTA,SET_QUOTA};
37 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
39 static struct cli_state *cli_ipc = NULL;
40 static POLICY_HND pol;
41 static BOOL got_policy_hnd;
43 static struct cli_state *connect_one(const char *share);
45 /* Open cli connection and policy handle */
47 static BOOL cli_open_policy_hnd(void)
49 /* Initialise cli LSA connection */
52 cli_ipc = connect_one("IPC$");
53 if (!cli_nt_session_open (cli_ipc, PI_LSARPC)) {
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(cli_lsa_open_policy(cli_ipc, 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;
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(cli_lsa_lookup_sids(cli_ipc, 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 uint32 *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(cli_lsa_lookup_names(cli_ipc, cli_ipc->mem_ctx,
118 &pol, 1, &str, &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 (strncasecmp(set_str,"UQLIM:",6)==0) {
147 *qtype = SMB_USER_QUOTA_TYPE;
150 if ((p2=strstr(p,":"))==NULL) {
157 fstrcpy(username_str,p);
159 } else if (strncasecmp(set_str,"FSQLIM:",7)==0) {
161 *qtype = SMB_USER_FS_QUOTA_TYPE;
164 } else if (strncasecmp(set_str,"FSQFLAGS:",9)==0) {
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 (strncasecmp(p,"QUOTA_ENABLED",13)==0) {
196 } else if (strncasecmp(p,"DENY_DISK",9)==0) {
198 } else if (strncasecmp(p,"LOG_SOFTLIMIT",13)==0) {
199 pqt->qflags |= QUOTAS_LOG_THRESHOLD;
200 } else if (strncasecmp(p,"LOG_HARDLIMIT",13)==0) {
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("Failed to open \\%s %s.\n",
242 FAKE_FILE_NAME_QUOTA,cli_errstr(cli));
247 case SMB_USER_QUOTA_TYPE:
248 if (!StringToSid(&qt.sid, username_str)) {
249 d_printf("StringToSid() failed for [%s]\n",username_str);
255 if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
256 d_printf("%s cli_get_user_quota %s\n",
257 cli_errstr(cli),username_str);
260 dump_ntquota(&qt,verbose,numeric,SidToString);
264 if (!cli_set_user_quota(cli, quota_fnum, pqt)) {
265 d_printf("%s cli_set_user_quota %s\n",
266 cli_errstr(cli),username_str);
269 if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
270 d_printf("%s cli_get_user_quota %s\n",
271 cli_errstr(cli),username_str);
274 dump_ntquota(&qt,verbose,numeric,SidToString);
277 if (!cli_list_user_quota(cli, quota_fnum, &qtl)) {
278 d_printf("%s cli_set_user_quota %s\n",
279 cli_errstr(cli),username_str);
282 dump_ntquota_list(&qtl,verbose,numeric,SidToString);
283 free_ntquota_list(&qtl);
286 d_printf("Unknown Error\n");
290 case SMB_USER_FS_QUOTA_TYPE:
293 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
294 d_printf("%s cli_get_fs_quota_info\n",
298 dump_ntquota(&qt,True,numeric,NULL);
301 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
302 d_printf("%s cli_get_fs_quota_info\n",
306 qt.softlim = pqt->softlim;
307 qt.hardlim = pqt->hardlim;
308 if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
309 d_printf("%s cli_set_fs_quota_info\n",
313 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
314 d_printf("%s cli_get_fs_quota_info\n",
318 dump_ntquota(&qt,True,numeric,NULL);
321 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
322 d_printf("%s cli_get_fs_quota_info\n",
326 qt.qflags = pqt->qflags;
327 if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
328 d_printf("%s cli_set_fs_quota_info\n",
332 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
333 d_printf("%s cli_get_fs_quota_info\n",
337 dump_ntquota(&qt,True,numeric,NULL);
340 d_printf("Unknown Error\n");
345 d_printf("Unknown Error\n");
349 cli_close(cli, quota_fnum);
354 /*****************************************************
355 return a connection to a server
356 *******************************************************/
357 static struct cli_state *connect_one(const char *share)
364 if (!cmdline_auth_info.got_pass) {
365 char *pass = getpass("Password: ");
367 pstrcpy(cmdline_auth_info.password, pass);
368 cmdline_auth_info.got_pass = True;
372 if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server,
375 cmdline_auth_info.username, lp_workgroup(),
376 cmdline_auth_info.password, 0, NULL))) {
379 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
384 /****************************************************************************
386 ****************************************************************************/
387 int main(int argc, const char *argv[])
393 pstring username_str = {0};
395 pstring set_str = {0};
396 enum SMB_QUOTA_TYPE qtype;
398 static BOOL test_args = False;
399 struct cli_state *cli;
400 BOOL fix_user = False;
401 SMB_NTQUOTA_STRUCT qt;
403 struct poptOption long_options[] = {
405 { "user", 'u', POPT_ARG_STRING, NULL, 'u', "Show quotas for user", "user" },
406 { "list", 'L', POPT_ARG_NONE, NULL, 'L', "List user quotas" },
407 { "fs", 'F', POPT_ARG_NONE, NULL, 'F', "Show filesystem quotas" },
408 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls\n\
410 UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
411 FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
412 FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
413 { "numeric", 'n', POPT_ARG_NONE, &numeric, True, "Don't resolve sids or limits to names" },
414 { "verbose", 'v', POPT_ARG_NONE, &verbose, True, "be verbose" },
415 { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
417 POPT_COMMON_CREDENTIALS
429 setup_logging(argv[0],True);
432 lp_load(dyn_CONFIGFILE,True,False,False);
435 pc = poptGetContext("smbcquotas", argc, argv, long_options, 0);
437 poptSetOtherOptionHelp(pc, "//server1/share1");
439 while ((opt = poptGetNextOpt(pc)) != -1) {
443 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
444 exit(EXIT_PARSE_ERROR);
451 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
452 exit(EXIT_PARSE_ERROR);
459 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
460 exit(EXIT_PARSE_ERROR);
462 pstrcpy(username_str,poptGetOptArg(pc));
469 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
470 exit(EXIT_PARSE_ERROR);
472 pstrcpy(set_str,poptGetOptArg(pc));
482 pstrcpy(username_str,cmdline_auth_info.username);
484 /* Make connection to server */
485 if(!poptPeekArg(pc)) {
486 poptPrintUsage(pc, stderr, 0);
487 exit(EXIT_PARSE_ERROR);
490 pstrcpy(path, poptGetArg(pc));
492 all_string_sub(path,"/","\\",0);
494 pstrcpy(server,path+2);
495 share = strchr_m(server,'\\');
497 share = strchr_m(server,'/');
499 printf("Invalid argument: %s\n", share);
500 exit(EXIT_PARSE_ERROR);
507 if (todo == SET_QUOTA) {
508 if (parse_quota_set(set_str, username_str, &qtype, &cmd, &qt)) {
509 printf("Invalid argument: -S %s\n", set_str);
510 exit(EXIT_PARSE_ERROR);
515 cli = connect_one(share);
524 /* Perform requested action */
528 result = do_quota(cli,SMB_USER_FS_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
531 result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_LIST, username_str, NULL);
534 result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
537 result = do_quota(cli, qtype, cmd, username_str, &qt);
541 result = EXIT_FAILED;