PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/samba3/source/utils/smbcquotas.c

https://gitlab.com/envieidoc/advancedtomato2
C | 553 lines | 440 code | 71 blank | 42 comment | 70 complexity | 799a120343accd46e86f30ab21d1a420 MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. QUOTA get/set utility
  4. Copyright (C) Andrew Tridgell 2000
  5. Copyright (C) Tim Potter 2000
  6. Copyright (C) Jeremy Allison 2000
  7. Copyright (C) Stefan (metze) Metzmacher 2003
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. 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, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include "includes.h"
  21. static pstring server;
  22. /* numeric is set when the user wants numeric SIDs and ACEs rather
  23. than going via LSA calls to resolve them */
  24. static BOOL numeric;
  25. static BOOL verbose;
  26. enum todo_values {NOOP_QUOTA=0,FS_QUOTA,USER_QUOTA,LIST_QUOTA,SET_QUOTA};
  27. enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
  28. static struct cli_state *cli_ipc;
  29. static struct rpc_pipe_client *global_pipe_hnd;
  30. static POLICY_HND pol;
  31. static BOOL got_policy_hnd;
  32. static struct cli_state *connect_one(const char *share);
  33. /* Open cli connection and policy handle */
  34. static BOOL cli_open_policy_hnd(void)
  35. {
  36. /* Initialise cli LSA connection */
  37. if (!cli_ipc) {
  38. NTSTATUS ret;
  39. cli_ipc = connect_one("IPC$");
  40. global_pipe_hnd = cli_rpc_pipe_open_noauth(cli_ipc, PI_LSARPC, &ret);
  41. if (!global_pipe_hnd) {
  42. return False;
  43. }
  44. }
  45. /* Open policy handle */
  46. if (!got_policy_hnd) {
  47. /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
  48. but NT sends 0x2000000 so we might as well do it too. */
  49. if (!NT_STATUS_IS_OK(rpccli_lsa_open_policy(global_pipe_hnd, cli_ipc->mem_ctx, True,
  50. GENERIC_EXECUTE_ACCESS, &pol))) {
  51. return False;
  52. }
  53. got_policy_hnd = True;
  54. }
  55. return True;
  56. }
  57. /* convert a SID to a string, either numeric or username/group */
  58. static void SidToString(fstring str, DOM_SID *sid, BOOL _numeric)
  59. {
  60. char **domains = NULL;
  61. char **names = NULL;
  62. enum lsa_SidType *types = NULL;
  63. sid_to_string(str, sid);
  64. if (_numeric) return;
  65. /* Ask LSA to convert the sid to a name */
  66. if (!cli_open_policy_hnd() ||
  67. !NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(global_pipe_hnd, cli_ipc->mem_ctx,
  68. &pol, 1, sid, &domains,
  69. &names, &types)) ||
  70. !domains || !domains[0] || !names || !names[0]) {
  71. return;
  72. }
  73. /* Converted OK */
  74. slprintf(str, sizeof(fstring) - 1, "%s%s%s",
  75. domains[0], lp_winbind_separator(),
  76. names[0]);
  77. }
  78. /* convert a string to a SID, either numeric or username/group */
  79. static BOOL StringToSid(DOM_SID *sid, const char *str)
  80. {
  81. enum lsa_SidType *types = NULL;
  82. DOM_SID *sids = NULL;
  83. BOOL result = True;
  84. if (strncmp(str, "S-", 2) == 0) {
  85. return string_to_sid(sid, str);
  86. }
  87. if (!cli_open_policy_hnd() ||
  88. !NT_STATUS_IS_OK(rpccli_lsa_lookup_names(global_pipe_hnd, cli_ipc->mem_ctx,
  89. &pol, 1, &str, NULL, &sids,
  90. &types))) {
  91. result = False;
  92. goto done;
  93. }
  94. sid_copy(sid, &sids[0]);
  95. done:
  96. return result;
  97. }
  98. #define QUOTA_GET 1
  99. #define QUOTA_SETLIM 2
  100. #define QUOTA_SETFLAGS 3
  101. #define QUOTA_LIST 4
  102. enum {PARSE_FLAGS,PARSE_LIM};
  103. static int parse_quota_set(pstring set_str, pstring username_str, enum SMB_QUOTA_TYPE *qtype, int *cmd, SMB_NTQUOTA_STRUCT *pqt)
  104. {
  105. char *p = set_str,*p2;
  106. int todo;
  107. BOOL stop = False;
  108. BOOL enable = False;
  109. BOOL deny = False;
  110. if (strnequal(set_str,"UQLIM:",6)) {
  111. p += 6;
  112. *qtype = SMB_USER_QUOTA_TYPE;
  113. *cmd = QUOTA_SETLIM;
  114. todo = PARSE_LIM;
  115. if ((p2=strstr(p,":"))==NULL) {
  116. return -1;
  117. }
  118. *p2 = '\0';
  119. p2++;
  120. fstrcpy(username_str,p);
  121. p = p2;
  122. } else if (strnequal(set_str,"FSQLIM:",7)) {
  123. p +=7;
  124. *qtype = SMB_USER_FS_QUOTA_TYPE;
  125. *cmd = QUOTA_SETLIM;
  126. todo = PARSE_LIM;
  127. } else if (strnequal(set_str,"FSQFLAGS:",9)) {
  128. p +=9;
  129. todo = PARSE_FLAGS;
  130. *qtype = SMB_USER_FS_QUOTA_TYPE;
  131. *cmd = QUOTA_SETFLAGS;
  132. } else {
  133. return -1;
  134. }
  135. switch (todo) {
  136. case PARSE_LIM:
  137. #if defined(HAVE_LONGLONG)
  138. if (sscanf(p,"%llu/%llu",&pqt->softlim,&pqt->hardlim)!=2) {
  139. #else
  140. if (sscanf(p,"%lu/%lu",&pqt->softlim,&pqt->hardlim)!=2) {
  141. #endif
  142. return -1;
  143. }
  144. break;
  145. case PARSE_FLAGS:
  146. while (!stop) {
  147. if ((p2=strstr(p,"/"))==NULL) {
  148. stop = True;
  149. } else {
  150. *p2 = '\0';
  151. p2++;
  152. }
  153. if (strnequal(p,"QUOTA_ENABLED",13)) {
  154. enable = True;
  155. } else if (strnequal(p,"DENY_DISK",9)) {
  156. deny = True;
  157. } else if (strnequal(p,"LOG_SOFTLIMIT",13)) {
  158. pqt->qflags |= QUOTAS_LOG_THRESHOLD;
  159. } else if (strnequal(p,"LOG_HARDLIMIT",13)) {
  160. pqt->qflags |= QUOTAS_LOG_LIMIT;
  161. } else {
  162. return -1;
  163. }
  164. p=p2;
  165. }
  166. if (deny) {
  167. pqt->qflags |= QUOTAS_DENY_DISK;
  168. } else if (enable) {
  169. pqt->qflags |= QUOTAS_ENABLED;
  170. }
  171. break;
  172. }
  173. return 0;
  174. }
  175. static int do_quota(struct cli_state *cli, enum SMB_QUOTA_TYPE qtype, uint16 cmd, pstring username_str, SMB_NTQUOTA_STRUCT *pqt)
  176. {
  177. uint32 fs_attrs = 0;
  178. int quota_fnum = 0;
  179. SMB_NTQUOTA_LIST *qtl = NULL;
  180. SMB_NTQUOTA_STRUCT qt;
  181. ZERO_STRUCT(qt);
  182. if (!cli_get_fs_attr_info(cli, &fs_attrs)) {
  183. d_printf("Failed to get the filesystem attributes %s.\n",
  184. cli_errstr(cli));
  185. return -1;
  186. }
  187. if (!(fs_attrs & FILE_VOLUME_QUOTAS)) {
  188. d_printf("Quotas are not supported by the server.\n");
  189. return 0;
  190. }
  191. if (!cli_get_quota_handle(cli, &quota_fnum)) {
  192. d_printf("Quotas are not enabled on this share.\n");
  193. d_printf("Failed to open %s %s.\n",
  194. FAKE_FILE_NAME_QUOTA_WIN32,cli_errstr(cli));
  195. return -1;
  196. }
  197. switch(qtype) {
  198. case SMB_USER_QUOTA_TYPE:
  199. if (!StringToSid(&qt.sid, username_str)) {
  200. d_printf("StringToSid() failed for [%s]\n",username_str);
  201. return -1;
  202. }
  203. switch(cmd) {
  204. case QUOTA_GET:
  205. if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
  206. d_printf("%s cli_get_user_quota %s\n",
  207. cli_errstr(cli),username_str);
  208. return -1;
  209. }
  210. dump_ntquota(&qt,verbose,numeric,SidToString);
  211. break;
  212. case QUOTA_SETLIM:
  213. pqt->sid = qt.sid;
  214. if (!cli_set_user_quota(cli, quota_fnum, pqt)) {
  215. d_printf("%s cli_set_user_quota %s\n",
  216. cli_errstr(cli),username_str);
  217. return -1;
  218. }
  219. if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
  220. d_printf("%s cli_get_user_quota %s\n",
  221. cli_errstr(cli),username_str);
  222. return -1;
  223. }
  224. dump_ntquota(&qt,verbose,numeric,SidToString);
  225. break;
  226. case QUOTA_LIST:
  227. if (!cli_list_user_quota(cli, quota_fnum, &qtl)) {
  228. d_printf("%s cli_set_user_quota %s\n",
  229. cli_errstr(cli),username_str);
  230. return -1;
  231. }
  232. dump_ntquota_list(&qtl,verbose,numeric,SidToString);
  233. free_ntquota_list(&qtl);
  234. break;
  235. default:
  236. d_printf("Unknown Error\n");
  237. return -1;
  238. }
  239. break;
  240. case SMB_USER_FS_QUOTA_TYPE:
  241. switch(cmd) {
  242. case QUOTA_GET:
  243. if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
  244. d_printf("%s cli_get_fs_quota_info\n",
  245. cli_errstr(cli));
  246. return -1;
  247. }
  248. dump_ntquota(&qt,True,numeric,NULL);
  249. break;
  250. case QUOTA_SETLIM:
  251. if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
  252. d_printf("%s cli_get_fs_quota_info\n",
  253. cli_errstr(cli));
  254. return -1;
  255. }
  256. qt.softlim = pqt->softlim;
  257. qt.hardlim = pqt->hardlim;
  258. if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
  259. d_printf("%s cli_set_fs_quota_info\n",
  260. cli_errstr(cli));
  261. return -1;
  262. }
  263. if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
  264. d_printf("%s cli_get_fs_quota_info\n",
  265. cli_errstr(cli));
  266. return -1;
  267. }
  268. dump_ntquota(&qt,True,numeric,NULL);
  269. break;
  270. case QUOTA_SETFLAGS:
  271. if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
  272. d_printf("%s cli_get_fs_quota_info\n",
  273. cli_errstr(cli));
  274. return -1;
  275. }
  276. qt.qflags = pqt->qflags;
  277. if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
  278. d_printf("%s cli_set_fs_quota_info\n",
  279. cli_errstr(cli));
  280. return -1;
  281. }
  282. if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
  283. d_printf("%s cli_get_fs_quota_info\n",
  284. cli_errstr(cli));
  285. return -1;
  286. }
  287. dump_ntquota(&qt,True,numeric,NULL);
  288. break;
  289. default:
  290. d_printf("Unknown Error\n");
  291. return -1;
  292. }
  293. break;
  294. default:
  295. d_printf("Unknown Error\n");
  296. return -1;
  297. }
  298. cli_close(cli, quota_fnum);
  299. return 0;
  300. }
  301. /*****************************************************
  302. return a connection to a server
  303. *******************************************************/
  304. static struct cli_state *connect_one(const char *share)
  305. {
  306. struct cli_state *c;
  307. struct in_addr ip;
  308. NTSTATUS nt_status;
  309. zero_ip(&ip);
  310. if (!cmdline_auth_info.got_pass) {
  311. char *pass = getpass("Password: ");
  312. if (pass) {
  313. pstrcpy(cmdline_auth_info.password, pass);
  314. cmdline_auth_info.got_pass = True;
  315. }
  316. }
  317. if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server,
  318. &ip, 0,
  319. share, "?????",
  320. cmdline_auth_info.username, lp_workgroup(),
  321. cmdline_auth_info.password, 0,
  322. cmdline_auth_info.signing_state, NULL))) {
  323. return c;
  324. } else {
  325. DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
  326. return NULL;
  327. }
  328. }
  329. /****************************************************************************
  330. main program
  331. ****************************************************************************/
  332. int main(int argc, const char *argv[])
  333. {
  334. char *share;
  335. int opt;
  336. int result;
  337. int todo = 0;
  338. pstring username_str = {0};
  339. pstring path = {0};
  340. pstring set_str = {0};
  341. enum SMB_QUOTA_TYPE qtype = SMB_INVALID_QUOTA_TYPE;
  342. int cmd = 0;
  343. static BOOL test_args = False;
  344. struct cli_state *cli;
  345. BOOL fix_user = False;
  346. SMB_NTQUOTA_STRUCT qt;
  347. poptContext pc;
  348. struct poptOption long_options[] = {
  349. POPT_AUTOHELP
  350. { "user", 'u', POPT_ARG_STRING, NULL, 'u', "Show quotas for user", "user" },
  351. { "list", 'L', POPT_ARG_NONE, NULL, 'L', "List user quotas" },
  352. { "fs", 'F', POPT_ARG_NONE, NULL, 'F', "Show filesystem quotas" },
  353. { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls\n\
  354. SETSTRING:\n\
  355. UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
  356. FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
  357. FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
  358. { "numeric", 'n', POPT_ARG_NONE, &numeric, True, "Don't resolve sids or limits to names" },
  359. { "verbose", 'v', POPT_ARG_NONE, &verbose, True, "be verbose" },
  360. { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
  361. POPT_COMMON_SAMBA
  362. POPT_COMMON_CREDENTIALS
  363. { NULL }
  364. };
  365. load_case_tables();
  366. ZERO_STRUCT(qt);
  367. /* set default debug level to 1 regardless of what smb.conf sets */
  368. setup_logging( "smbcquotas", True );
  369. DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
  370. dbf = x_stderr;
  371. x_setbuf( x_stderr, NULL );
  372. setlinebuf(stdout);
  373. fault_setup(NULL);
  374. lp_load(dyn_CONFIGFILE,True,False,False,True);
  375. load_interfaces();
  376. pc = poptGetContext("smbcquotas", argc, argv, long_options, 0);
  377. poptSetOtherOptionHelp(pc, "//server1/share1");
  378. while ((opt = poptGetNextOpt(pc)) != -1) {
  379. switch (opt) {
  380. case 'L':
  381. if (todo != 0) {
  382. d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
  383. exit(EXIT_PARSE_ERROR);
  384. }
  385. todo = LIST_QUOTA;
  386. break;
  387. case 'F':
  388. if (todo != 0) {
  389. d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
  390. exit(EXIT_PARSE_ERROR);
  391. }
  392. todo = FS_QUOTA;
  393. break;
  394. case 'u':
  395. if (todo != 0) {
  396. d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
  397. exit(EXIT_PARSE_ERROR);
  398. }
  399. pstrcpy(username_str,poptGetOptArg(pc));
  400. todo = USER_QUOTA;
  401. fix_user = True;
  402. break;
  403. case 'S':
  404. if (todo != 0) {
  405. d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
  406. exit(EXIT_PARSE_ERROR);
  407. }
  408. pstrcpy(set_str,poptGetOptArg(pc));
  409. todo = SET_QUOTA;
  410. break;
  411. }
  412. }
  413. if (todo == 0)
  414. todo = USER_QUOTA;
  415. if (!fix_user)
  416. pstrcpy(username_str,cmdline_auth_info.username);
  417. /* Make connection to server */
  418. if(!poptPeekArg(pc)) {
  419. poptPrintUsage(pc, stderr, 0);
  420. exit(EXIT_PARSE_ERROR);
  421. }
  422. pstrcpy(path, poptGetArg(pc));
  423. all_string_sub(path,"/","\\",0);
  424. pstrcpy(server,path+2);
  425. share = strchr_m(server,'\\');
  426. if (!share) {
  427. share = strchr_m(server,'/');
  428. if (!share) {
  429. printf("Invalid argument: %s\n", share);
  430. exit(EXIT_PARSE_ERROR);
  431. }
  432. }
  433. *share = 0;
  434. share++;
  435. if (todo == SET_QUOTA) {
  436. if (parse_quota_set(set_str, username_str, &qtype, &cmd, &qt)) {
  437. printf("Invalid argument: -S %s\n", set_str);
  438. exit(EXIT_PARSE_ERROR);
  439. }
  440. }
  441. if (!test_args) {
  442. cli = connect_one(share);
  443. if (!cli) {
  444. exit(EXIT_FAILED);
  445. }
  446. } else {
  447. exit(EXIT_OK);
  448. }
  449. /* Perform requested action */
  450. switch (todo) {
  451. case FS_QUOTA:
  452. result = do_quota(cli,SMB_USER_FS_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
  453. break;
  454. case LIST_QUOTA:
  455. result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_LIST, username_str, NULL);
  456. break;
  457. case USER_QUOTA:
  458. result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
  459. break;
  460. case SET_QUOTA:
  461. result = do_quota(cli, qtype, cmd, username_str, &qt);
  462. break;
  463. default:
  464. result = EXIT_FAILED;
  465. break;
  466. }
  467. return result;
  468. }