Skip to content

Commit 4e28c49

Browse files
committed
fix/feat(privilege,args,config,os_utils,main,readme): add --group-id/misc.GroupId and use it automatically where needed (Fixes: #863)
1 parent d85975b commit 4e28c49

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,12 @@ In case of elevating privilege for pacman - pikaur would ask for password every
235235
User ID to run makepkg if pikaur started from root.
236236
0 - means disabled, not that it will use uid=0.
237237
Setting this option would override DynamicUsers settings and force changing to this UID instead of a dynamic one.
238+
The config value would be overriden by `--user-id` flag.
238239

240+
##### GroupId (default: 0)
241+
Group ID to run makepkg if pikaur started from root.
242+
0 - means disabled, not that it will use gid=0.
243+
The config value would be overriden by `--group-id` flag.
239244

240245
##### CachePath (default: ~/.cache)
241246
Path to package cache location.

pikaur/args.py

+4
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ def get_pikaur_int_opts(action: str | None = None) -> ArgSchema:
380380
None, "user-id", PikaurConfig().misc.UserId.get_int(),
381381
translate("user ID to run makepkg if pikaur started from root"),
382382
),
383+
Arg(
384+
None, "group-id", PikaurConfig().misc.GroupId.get_int(),
385+
translate("group ID to run makepkg if pikaur started from root"),
386+
),
383387
]
384388
if action == "extras":
385389
result += [

pikaur/config.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,23 @@ def pre_arg_parser(key: str, fallback: str) -> str:
129129
return fallback
130130

131131

132-
class CustomUserId(IntOrBoolSingleton):
132+
class IntOrBoolSingletonUpdateableFallback(IntOrBoolSingleton):
133+
134+
@classmethod
135+
def update_fallback(cls, new_id: int) -> None:
136+
cls.set_value(cls() or new_id)
137+
138+
139+
class CustomUserId(IntOrBoolSingletonUpdateableFallback):
133140
@classmethod
134141
def init_value(cls) -> int:
135142
return int(pre_arg_parser("--user-id", "0"))
136143

144+
145+
class CustomGroupId(IntOrBoolSingletonUpdateableFallback):
137146
@classmethod
138-
def update_fallback(cls, new_id: int) -> None:
139-
cls.set_value(cls() or new_id)
147+
def init_value(cls) -> int:
148+
return int(pre_arg_parser("--group-id", "0"))
140149

141150

142151
class RunningAsRoot(IntOrBoolSingleton):
@@ -380,7 +389,7 @@ def __new__(cls) -> "ConfigSchemaT": # type: ignore[misc]
380389
"Consider either calling Pikaur from your actual"
381390
" user account without `sudo`,"
382391
" or creating `pikaur` user"
383-
" and specifying its UserId in Pikaur config\n"
392+
" and specifying its UserId and GroupId in Pikaur config\n"
384393
"and set DynamicUsers to `never`.",
385394
),
386395
"when_value": ["root", "always"],
@@ -552,6 +561,10 @@ def __new__(cls) -> "ConfigSchemaT": # type: ignore[misc]
552561
"data_type": INT,
553562
"default": "0",
554563
},
564+
"GroupId": {
565+
"data_type": INT,
566+
"default": "0",
567+
},
555568
"PreserveEnv": {
556569
"data_type": STR,
557570
"default": (
@@ -612,18 +625,24 @@ def write_config(config: configparser.ConfigParser | None = None) -> None:
612625
need_write = True
613626
if need_write:
614627
CustomUserId.update_fallback(int(config["misc"]["UserId"]))
628+
CustomGroupId.update_fallback(int(config["misc"]["GroupId"]))
615629
config_root = ConfigRoot()
616630
config_path = ConfigPath()
617631
if not config_root.exists():
618632
config_root.mkdir(parents=True)
619633
with config_path.open("w", encoding=DEFAULT_CONFIG_ENCODING) as configfile:
620634
config.write(configfile)
621635
if custom_user_id := CustomUserId():
636+
custom_group_id = CustomGroupId()
637+
if not custom_group_id:
638+
_err_write(translate(
639+
"Either both UserId and GroupId or neither of them should be set",
640+
))
622641
for path in (
623642
config_root,
624643
config_path,
625644
):
626-
os.chown(path, custom_user_id, custom_user_id)
645+
os.chown(path, custom_user_id, custom_group_id)
627646

628647

629648
def str_to_bool(value: str) -> bool:
@@ -703,6 +722,7 @@ def get_config(cls) -> configparser.ConfigParser:
703722
write_config(config=cls._config)
704723
cls.validate_config()
705724
CustomUserId.update_fallback(int(cls._config["misc"]["UserId"]))
725+
CustomGroupId.update_fallback(int(cls._config["misc"]["GroupId"]))
706726
return cls._config
707727

708728
def __getattr__(self, attr: str) -> PikaurConfigSection:

pikaur/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def init_readline() -> None:
8484
init_readline()
8585

8686
SYSTEMD_MIN_VERSION: "Final" = 235
87-
logger = create_logger(f"main_{os.getuid()}")
87+
logger = create_logger(f"main_{os.getuid()}_{os.getgid()}")
8888

8989

9090
# @TODO: use arg to enable it

pikaur/os_utils.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,19 @@ def check_executables(dep_names: list[str]) -> None:
107107
def chown_to_current(path: Path) -> None:
108108
args = parse_args()
109109
user_id = args.user_id
110-
if user_id:
110+
group_id = args.group_id
111+
if user_id and group_id:
111112
if not isinstance(user_id, int):
112113
raise TypeError
114+
if not isinstance(group_id, int):
115+
raise TypeError
113116
try:
114-
os.chown(path, user_id, user_id)
117+
os.chown(path, user_id, group_id)
115118
except PermissionError as exc:
116119
print_error()
117120
print_error(
118-
translate("Can't change owner to {user_id}: {exc}").format(
119-
user_id=user_id, exc=exc,
121+
translate("Can't change owner to {user_id}:{group_id}: {exc}").format(
122+
user_id=user_id, group_id=group_id, exc=exc,
120123
),
121124
)
122125
print_error()

pikaur/privilege.py

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def get_args_to_elevate_pikaur(original_args: list[str]) -> list[str]:
102102
if not need_dynamic_users():
103103
extra_args += [
104104
("--user-id", str(args.user_id or os.getuid())),
105+
("--group-id", str(args.group_id or os.getgid())),
105106
("--home-dir", str(args.home_dir or "") or Path.home().as_posix()),
106107
]
107108
for flag, arg_key, env_key in (

0 commit comments

Comments
 (0)