/release/src/router/samba3/source/utils/smbcquotas.c
C | 553 lines | 440 code | 71 blank | 42 comment | 70 complexity | 799a120343accd46e86f30ab21d1a420 MD5 | raw file
- /*
- Unix SMB/CIFS implementation.
- QUOTA get/set utility
-
- Copyright (C) Andrew Tridgell 2000
- Copyright (C) Tim Potter 2000
- Copyright (C) Jeremy Allison 2000
- Copyright (C) Stefan (metze) Metzmacher 2003
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- #include "includes.h"
- static pstring server;
- /* numeric is set when the user wants numeric SIDs and ACEs rather
- than going via LSA calls to resolve them */
- static BOOL numeric;
- static BOOL verbose;
- enum todo_values {NOOP_QUOTA=0,FS_QUOTA,USER_QUOTA,LIST_QUOTA,SET_QUOTA};
- enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
- static struct cli_state *cli_ipc;
- static struct rpc_pipe_client *global_pipe_hnd;
- static POLICY_HND pol;
- static BOOL got_policy_hnd;
- static struct cli_state *connect_one(const char *share);
- /* Open cli connection and policy handle */
- static BOOL cli_open_policy_hnd(void)
- {
- /* Initialise cli LSA connection */
- if (!cli_ipc) {
- NTSTATUS ret;
- cli_ipc = connect_one("IPC$");
- global_pipe_hnd = cli_rpc_pipe_open_noauth(cli_ipc, PI_LSARPC, &ret);
- if (!global_pipe_hnd) {
- return False;
- }
- }
-
- /* Open policy handle */
- if (!got_policy_hnd) {
- /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
- but NT sends 0x2000000 so we might as well do it too. */
- if (!NT_STATUS_IS_OK(rpccli_lsa_open_policy(global_pipe_hnd, cli_ipc->mem_ctx, True,
- GENERIC_EXECUTE_ACCESS, &pol))) {
- return False;
- }
- got_policy_hnd = True;
- }
-
- return True;
- }
- /* convert a SID to a string, either numeric or username/group */
- static void SidToString(fstring str, DOM_SID *sid, BOOL _numeric)
- {
- char **domains = NULL;
- char **names = NULL;
- enum lsa_SidType *types = NULL;
- sid_to_string(str, sid);
- if (_numeric) return;
- /* Ask LSA to convert the sid to a name */
- if (!cli_open_policy_hnd() ||
- !NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(global_pipe_hnd, cli_ipc->mem_ctx,
- &pol, 1, sid, &domains,
- &names, &types)) ||
- !domains || !domains[0] || !names || !names[0]) {
- return;
- }
- /* Converted OK */
- slprintf(str, sizeof(fstring) - 1, "%s%s%s",
- domains[0], lp_winbind_separator(),
- names[0]);
-
- }
- /* convert a string to a SID, either numeric or username/group */
- static BOOL StringToSid(DOM_SID *sid, const char *str)
- {
- enum lsa_SidType *types = NULL;
- DOM_SID *sids = NULL;
- BOOL result = True;
- if (strncmp(str, "S-", 2) == 0) {
- return string_to_sid(sid, str);
- }
- if (!cli_open_policy_hnd() ||
- !NT_STATUS_IS_OK(rpccli_lsa_lookup_names(global_pipe_hnd, cli_ipc->mem_ctx,
- &pol, 1, &str, NULL, &sids,
- &types))) {
- result = False;
- goto done;
- }
- sid_copy(sid, &sids[0]);
- done:
- return result;
- }
- #define QUOTA_GET 1
- #define QUOTA_SETLIM 2
- #define QUOTA_SETFLAGS 3
- #define QUOTA_LIST 4
- enum {PARSE_FLAGS,PARSE_LIM};
- static int parse_quota_set(pstring set_str, pstring username_str, enum SMB_QUOTA_TYPE *qtype, int *cmd, SMB_NTQUOTA_STRUCT *pqt)
- {
- char *p = set_str,*p2;
- int todo;
- BOOL stop = False;
- BOOL enable = False;
- BOOL deny = False;
-
- if (strnequal(set_str,"UQLIM:",6)) {
- p += 6;
- *qtype = SMB_USER_QUOTA_TYPE;
- *cmd = QUOTA_SETLIM;
- todo = PARSE_LIM;
- if ((p2=strstr(p,":"))==NULL) {
- return -1;
- }
-
- *p2 = '\0';
- p2++;
-
- fstrcpy(username_str,p);
- p = p2;
- } else if (strnequal(set_str,"FSQLIM:",7)) {
- p +=7;
- *qtype = SMB_USER_FS_QUOTA_TYPE;
- *cmd = QUOTA_SETLIM;
- todo = PARSE_LIM;
- } else if (strnequal(set_str,"FSQFLAGS:",9)) {
- p +=9;
- todo = PARSE_FLAGS;
- *qtype = SMB_USER_FS_QUOTA_TYPE;
- *cmd = QUOTA_SETFLAGS;
- } else {
- return -1;
- }
- switch (todo) {
- case PARSE_LIM:
- #if defined(HAVE_LONGLONG)
- if (sscanf(p,"%llu/%llu",&pqt->softlim,&pqt->hardlim)!=2) {
- #else
- if (sscanf(p,"%lu/%lu",&pqt->softlim,&pqt->hardlim)!=2) {
- #endif
- return -1;
- }
-
- break;
- case PARSE_FLAGS:
- while (!stop) {
- if ((p2=strstr(p,"/"))==NULL) {
- stop = True;
- } else {
- *p2 = '\0';
- p2++;
- }
- if (strnequal(p,"QUOTA_ENABLED",13)) {
- enable = True;
- } else if (strnequal(p,"DENY_DISK",9)) {
- deny = True;
- } else if (strnequal(p,"LOG_SOFTLIMIT",13)) {
- pqt->qflags |= QUOTAS_LOG_THRESHOLD;
- } else if (strnequal(p,"LOG_HARDLIMIT",13)) {
- pqt->qflags |= QUOTAS_LOG_LIMIT;
- } else {
- return -1;
- }
- p=p2;
- }
- if (deny) {
- pqt->qflags |= QUOTAS_DENY_DISK;
- } else if (enable) {
- pqt->qflags |= QUOTAS_ENABLED;
- }
-
- break;
- }
- return 0;
- }
- static int do_quota(struct cli_state *cli, enum SMB_QUOTA_TYPE qtype, uint16 cmd, pstring username_str, SMB_NTQUOTA_STRUCT *pqt)
- {
- uint32 fs_attrs = 0;
- int quota_fnum = 0;
- SMB_NTQUOTA_LIST *qtl = NULL;
- SMB_NTQUOTA_STRUCT qt;
- ZERO_STRUCT(qt);
- if (!cli_get_fs_attr_info(cli, &fs_attrs)) {
- d_printf("Failed to get the filesystem attributes %s.\n",
- cli_errstr(cli));
- return -1;
- }
- if (!(fs_attrs & FILE_VOLUME_QUOTAS)) {
- d_printf("Quotas are not supported by the server.\n");
- return 0;
- }
- if (!cli_get_quota_handle(cli, "a_fnum)) {
- d_printf("Quotas are not enabled on this share.\n");
- d_printf("Failed to open %s %s.\n",
- FAKE_FILE_NAME_QUOTA_WIN32,cli_errstr(cli));
- return -1;
- }
-