-
Notifications
You must be signed in to change notification settings - Fork 0
/
readjust_perms.py
executable file
·59 lines (48 loc) · 1.66 KB
/
readjust_perms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p python3
import argparse
import os
import sys
from pathlib import Path
from pwd import getpwnam
from distribute_symlinks import (
USER,
ROOT,
CONFIG_DESTINATIONS,
ensure_root,
expanduser,
)
BACKUP_DIR = expanduser("~/_archive")
def readjust_perms(destinations=CONFIG_DESTINATIONS, user=USER, root=ROOT):
for link_name in list(destinations.values()) + [BACKUP_DIR]:
if str(link_name).startswith("/"):
# most likely a system config path in /etc, we don't want to change those
continue
_, _, uid, gid, *_ = getpwnam(user)
try:
os.chown(
expanduser(link_name, root=root, user=user),
uid,
gid,
follow_symlinks=False,
)
except PermissionError:
print(f"Skipping {link_name} due to missing perms", file=sys.stderr)
except FileNotFoundError:
print(f"Skipping {link_name} due to not existing", file=sys.stderr)
def parse_args():
parser = argparse.ArgumentParser(
description="Adjusts the symlinks made by the partner script to match the proper owner. Does not take any precautions against path traversal attacks."
)
parser.add_argument("--root", action="store", default=ROOT)
parser.add_argument("--user", action="store", default=USER)
return parser.parse_args()
def main():
ensure_root(
"Should be run as root, since the symlinks are very likely to be owned by root",
fail_fast=False,
)
args = parse_args()
readjust_perms(user=args.user, root=args.root)
if __name__ == "__main__":
main()