Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/lib/fixperms
File: fixperms_cli.py
"""fixperms sys.argv cli argument parsing"""
[0] Fix | Delete
from dataclasses import dataclass
[1] Fix | Delete
from logging import Logger
[2] Fix | Delete
from argparse import ArgumentParser, ArgumentTypeError
[3] Fix | Delete
import os
[4] Fix | Delete
import pwd
[5] Fix | Delete
import sys
[6] Fix | Delete
from typing import Literal
[7] Fix | Delete
import rads
[8] Fix | Delete
import cwp
[9] Fix | Delete
[10] Fix | Delete
[11] Fix | Delete
@dataclass
[12] Fix | Delete
class Args:
[13] Fix | Delete
"""Dataclass for more strict type hints for parse_args"""
[14] Fix | Delete
[15] Fix | Delete
role: Literal['CWP', 'cPanel', 'WP3']
[16] Fix | Delete
users: set[str]
[17] Fix | Delete
skip: set[str]
[18] Fix | Delete
noop: bool
[19] Fix | Delete
procs: int
[20] Fix | Delete
skip_mail: bool
[21] Fix | Delete
preserve_exec: bool
[22] Fix | Delete
logger: Logger
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
class ArgValidator:
[26] Fix | Delete
"""Validates custom args"""
[27] Fix | Delete
[28] Fix | Delete
def __init__(self) -> None:
[29] Fix | Delete
self._all_cwp_users = set()
[30] Fix | Delete
if cwp.HAS_CWP:
[31] Fix | Delete
self.role = 'CWP'
[32] Fix | Delete
self._all_cwp_users = cwp.all_users()
[33] Fix | Delete
self.user = self._cwp_user
[34] Fix | Delete
elif rads.HAS_CPANEL:
[35] Fix | Delete
self.role = 'cPanel'
[36] Fix | Delete
self.user = self._cpanel_user
[37] Fix | Delete
elif os.path.exists('/etc/ansible/wordpress-ultrastack'):
[38] Fix | Delete
self.role = 'WP3'
[39] Fix | Delete
self.user = self._wp3_user
[40] Fix | Delete
else:
[41] Fix | Delete
sys.exit("fixperms requires a cPanel, CWP, or WP3 server")
[42] Fix | Delete
[43] Fix | Delete
def _cwp_user(self, val: str) -> str:
[44] Fix | Delete
if val in self._all_cwp_users:
[45] Fix | Delete
return val
[46] Fix | Delete
raise ArgumentTypeError(f'{val} is not a valid CWP user')
[47] Fix | Delete
[48] Fix | Delete
@staticmethod
[49] Fix | Delete
def _cpanel_user(val: str) -> str:
[50] Fix | Delete
if rads.is_cpuser(val):
[51] Fix | Delete
return val
[52] Fix | Delete
raise ArgumentTypeError(f'{val} is not a valid cPanel user')
[53] Fix | Delete
[54] Fix | Delete
@staticmethod
[55] Fix | Delete
def _wp3_user(val: str) -> str:
[56] Fix | Delete
if val == 'wordpress':
[57] Fix | Delete
try:
[58] Fix | Delete
pwd.getpwnam(val)
[59] Fix | Delete
return val
[60] Fix | Delete
except KeyError:
[61] Fix | Delete
pass
[62] Fix | Delete
raise ArgumentTypeError(f'{val} is not a valid WP3 user')
[63] Fix | Delete
[64] Fix | Delete
@staticmethod
[65] Fix | Delete
def positive(val: str) -> int:
[66] Fix | Delete
"""Test an arg is an int >= 1"""
[67] Fix | Delete
val = int(val)
[68] Fix | Delete
if val >= 1:
[69] Fix | Delete
return val
[70] Fix | Delete
raise ArgumentTypeError(f'{val} must be >= 1')
[71] Fix | Delete
[72] Fix | Delete
[73] Fix | Delete
def parse_args() -> Args:
[74] Fix | Delete
"""Parse sys.argv"""
[75] Fix | Delete
# fmt: off
[76] Fix | Delete
custom_args = ArgValidator()
[77] Fix | Delete
parser = ArgumentParser(
[78] Fix | Delete
description="Safely corrects permission issues on a "
[79] Fix | Delete
f"{custom_args.role} user"
[80] Fix | Delete
)
[81] Fix | Delete
group = parser.add_mutually_exclusive_group()
[82] Fix | Delete
group.add_argument(
[83] Fix | Delete
'-p', '--procs', type=custom_args.positive, default=4, metavar='NUM',
[84] Fix | Delete
help='max users to process at once, if multiple were requested '
[85] Fix | Delete
'(defaults to 4)'
[86] Fix | Delete
)
[87] Fix | Delete
group.add_argument(
[88] Fix | Delete
'-v', '--verbose', action='store_true',
[89] Fix | Delete
help='show verbose output of every permissions change',
[90] Fix | Delete
)
[91] Fix | Delete
group.add_argument(
[92] Fix | Delete
'-q', '--quiet', action='store_true', help='hide all output but errors'
[93] Fix | Delete
)
[94] Fix | Delete
parser.add_argument(
[95] Fix | Delete
'-n', '--noop', action='store_true',
[96] Fix | Delete
help='Test mode; Make no actual changes to the account',
[97] Fix | Delete
)
[98] Fix | Delete
parser.add_argument(
[99] Fix | Delete
'--skip', type=os.path.realpath, metavar='PATH', nargs='+', default=[],
[100] Fix | Delete
help='path(s) to skip changing permissions on. '
[101] Fix | Delete
'If you want to skip etc and mail, use --skip-mail instead',
[102] Fix | Delete
)
[103] Fix | Delete
parser.add_argument(
[104] Fix | Delete
'--no-preserve-exec', dest='preserve_exec', action='store_false',
[105] Fix | Delete
help="Don't preserve the execute bit on files",
[106] Fix | Delete
)
[107] Fix | Delete
if custom_args.role != 'WP3':
[108] Fix | Delete
parser.add_argument(
[109] Fix | Delete
'--skip-mail', action='store_true',
[110] Fix | Delete
help='Do not run mailperm to fix permissions on etc and mail dirs',
[111] Fix | Delete
)
[112] Fix | Delete
parser.add_argument(
[113] Fix | Delete
'users',
[114] Fix | Delete
nargs='+',
[115] Fix | Delete
metavar='USER',
[116] Fix | Delete
type=custom_args.user,
[117] Fix | Delete
help='cPanel user(s)',
[118] Fix | Delete
)
[119] Fix | Delete
# fmt: on
[120] Fix | Delete
args = parser.parse_args()
[121] Fix | Delete
if args.verbose:
[122] Fix | Delete
loglevel = 'DEBUG'
[123] Fix | Delete
elif args.quiet:
[124] Fix | Delete
loglevel = 'ERROR'
[125] Fix | Delete
else:
[126] Fix | Delete
loglevel = 'INFO'
[127] Fix | Delete
logger = rads.setup_verbosity(loglevel)
[128] Fix | Delete
return Args(
[129] Fix | Delete
role=custom_args.role,
[130] Fix | Delete
users=set(args.users),
[131] Fix | Delete
skip=set(args.skip),
[132] Fix | Delete
noop=args.noop,
[133] Fix | Delete
procs=args.procs,
[134] Fix | Delete
skip_mail=args.skip_mail if custom_args.role != 'WP3' else True,
[135] Fix | Delete
preserve_exec=args.preserve_exec,
[136] Fix | Delete
logger=logger,
[137] Fix | Delete
)
[138] Fix | Delete
[139] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function