fix: root actions in tui/gui & userControl
This commit is contained in:
32 files changed
+665
-317
No files matched your search
@@ -56,7 +56,7 @@ let
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundsGuardPrivate
|
||||
proxyInboundViaOutbounds
|
||||
userControlEnabled
|
||||
userControlAllows
|
||||
;
|
||||
|
||||
# Tool paths – defined once here and passed into sub-modules as needed.
|
||||
@@ -88,9 +88,7 @@ let
|
||||
|
||||
builders = import ./builders.nix { inherit lib pkgs; };
|
||||
|
||||
polkit = import ./polkit.nix {
|
||||
inherit lib cfg userControlCfg;
|
||||
};
|
||||
polkit = import ./polkit.nix { inherit userControlCfg; };
|
||||
|
||||
scripts = import ./scripts.nix {
|
||||
inherit
|
||||
@@ -135,7 +133,7 @@ let
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundsGuardPrivate
|
||||
proxyInboundViaOutbounds
|
||||
userControlEnabled
|
||||
userControlAllows
|
||||
builders
|
||||
;
|
||||
};
|
||||
|
||||
@@ -62,6 +62,7 @@ let
|
||||
perAppRoutingTproxy
|
||||
userControlCfg
|
||||
userControlEnabled
|
||||
userControlAllows
|
||||
perAppZapretEnabled
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
@@ -129,6 +130,7 @@ let
|
||||
inherit lib pkgs cfg;
|
||||
inherit (control) proxyCtl;
|
||||
inherit (constants) autoProxyStateDir;
|
||||
inherit userControlAllows;
|
||||
}
|
||||
);
|
||||
in
|
||||
@@ -170,36 +172,51 @@ lib.mkMerge [
|
||||
"${userControlCfg.group}" = { };
|
||||
};
|
||||
|
||||
security.polkit.enable = lib.mkIf (cfg.enable && userControlEnabled) true;
|
||||
security.polkit.extraConfig = lib.mkIf (cfg.enable && userControlEnabled) (
|
||||
lib.mkAfter ''
|
||||
polkit.addRule(function(action, subject) {
|
||||
if (!subject.isInGroup("${userControlCfg.group}")) {
|
||||
security.polkit.enable = lib.mkIf (cfg.enable && (userControlEnabled || cfg.gui.enable)) true;
|
||||
# The GUI's "Retry as Root" and root toggle run proxy-ctl through pkexec: one
|
||||
# admin password then covers the next few minutes, as sudo's does in the TUI.
|
||||
security.polkit.extraConfig = lib.mkMerge [
|
||||
(lib.mkIf (cfg.enable && cfg.gui.enable) (
|
||||
lib.mkAfter ''
|
||||
polkit.addRule(function(action, subject) {
|
||||
if (action.id === "org.freedesktop.policykit.exec" &&
|
||||
action.lookup("program") === "${control.proxyCtl}/bin/proxy-ctl") {
|
||||
return polkit.Result.AUTH_ADMIN_KEEP;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
''
|
||||
))
|
||||
(lib.mkIf (cfg.enable && userControlEnabled) (
|
||||
lib.mkAfter ''
|
||||
polkit.addRule(function(action, subject) {
|
||||
if (!subject.isInGroup("${userControlCfg.group}")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (action.id !== "org.freedesktop.systemd1.manage-units") {
|
||||
return null;
|
||||
}
|
||||
|
||||
var unit = action.lookup("unit");
|
||||
${polkit.userControlPolkitRules}
|
||||
|
||||
if (action.id !== "org.freedesktop.systemd1.manage-units") {
|
||||
return null;
|
||||
}
|
||||
|
||||
var unit = action.lookup("unit");
|
||||
${polkit.userControlPolkitRules}
|
||||
|
||||
return null;
|
||||
});
|
||||
''
|
||||
);
|
||||
});
|
||||
''
|
||||
))
|
||||
];
|
||||
|
||||
# Spool dirs for outbounds and subscriptions added at runtime. Setgid so the
|
||||
# files proxy-ctl drops here inherit the group; without userControl only root
|
||||
# writes them. tmpfiles rather than the start scripts, so the group can add an
|
||||
# files proxy-ctl drops here inherit the group; without the outbounds scope
|
||||
# only root writes them. tmpfiles rather than the start scripts, so the group can add an
|
||||
# outbound before the proxy has ever run.
|
||||
systemd.tmpfiles.rules = lib.mkIf (cfg.enable && proxyEnabled) (
|
||||
map
|
||||
(
|
||||
dir:
|
||||
"d ${dir} ${
|
||||
if userControlEnabled then "2770 root ${userControlCfg.group}" else "0700 root root"
|
||||
if userControlAllows "outbounds" then "2770 root ${userControlCfg.group}" else "0700 root root"
|
||||
} -"
|
||||
)
|
||||
[
|
||||
|
||||
@@ -1,26 +1,35 @@
|
||||
# Polkit rules granting proxy-suite group members passwordless control over services.
|
||||
{
|
||||
lib,
|
||||
cfg,
|
||||
userControlCfg,
|
||||
}:
|
||||
# Polkit rule body granting userControl group members passwordless control over the
|
||||
# units of their scopes.
|
||||
{ userControlCfg }:
|
||||
|
||||
let
|
||||
userControlEnabled = userControlCfg.allow != [ ];
|
||||
|
||||
userControlPolkitRules =
|
||||
lib.optionalString (builtins.elem "perApp" userControlCfg.allow) ''
|
||||
if (unit.indexOf("proxy-suite-per-app-") === 0) {
|
||||
return polkit.Result.YES;
|
||||
}
|
||||
''
|
||||
+ lib.optionalString (builtins.elem "global" userControlCfg.allow) ''
|
||||
if (unit.indexOf("proxy-suite-") === 0 &&
|
||||
unit.indexOf("proxy-suite-per-app-") !== 0) {
|
||||
return polkit.Result.YES;
|
||||
}
|
||||
'';
|
||||
# Units a narrower scope starts; every other proxy-suite unit is "services".
|
||||
scopeByUnitPrefix = {
|
||||
"proxy-suite-per-app-" = "perApp";
|
||||
"proxy-suite-outbound-pin@" = "routing";
|
||||
"proxy-suite-outbound-unpin." = "routing";
|
||||
"proxy-suite-route-mode@" = "routing";
|
||||
"proxy-suite-outbound-reload." = "outbounds";
|
||||
"proxy-suite-subscription-update." = "outbounds";
|
||||
"proxy-suite-autoproxy-learn." = "autoProxy";
|
||||
"proxy-suite-zapret2-cutoff." = "zapret";
|
||||
"proxy-suite-inbound-stats." = "stats";
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit userControlEnabled userControlPolkitRules;
|
||||
userControlPolkitRules = ''
|
||||
var scopeByUnitPrefix = ${builtins.toJSON scopeByUnitPrefix};
|
||||
var scope = unit.indexOf("proxy-suite-") === 0 ? "services" : null;
|
||||
for (var prefix in scopeByUnitPrefix) {
|
||||
if (unit.indexOf(prefix) === 0) {
|
||||
scope = scopeByUnitPrefix[prefix];
|
||||
break;
|
||||
}
|
||||
}
|
||||
// No scopes listed means all of them.
|
||||
var scopes = ${builtins.toJSON userControlCfg.scopes};
|
||||
if (scope !== null && (scopes.length === 0 || scopes.indexOf(scope) !== -1)) {
|
||||
return polkit.Result.YES;
|
||||
}
|
||||
'';
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
proxyInboundsNeedLocalProxy,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlCfg,
|
||||
userControlEnabled,
|
||||
userControlAllows,
|
||||
localProxyAuth,
|
||||
localProxyAuthEnabled,
|
||||
localProxyAuthPasswordSource,
|
||||
@@ -91,10 +91,10 @@ let
|
||||
|
||||
writeLinksBlock = lib.optionalString proxyInboundsCfg.shareLinks ''
|
||||
${jq} -c '.links' <<< "$RENDERED" > "${linksFile}"
|
||||
${lib.optionalString userControlEnabled ''
|
||||
${lib.optionalString (userControlAllows "secrets") ''
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${linksFile}"
|
||||
''}
|
||||
chmod ${if userControlEnabled then "640" else "600"} "${linksFile}"
|
||||
chmod ${if userControlAllows "secrets" then "640" else "600"} "${linksFile}"
|
||||
'';
|
||||
|
||||
# One file per user for the web server, plus a token index for proxy-ctl. Filled aside
|
||||
@@ -113,10 +113,10 @@ let
|
||||
rm -rf "$SUB_DIR"
|
||||
mv "$SUB_DIR.new" "$SUB_DIR"
|
||||
${jq} -c '[.subscriptions[] | {user, token}]' <<< "$RENDERED" > "${subscriptionsFile}"
|
||||
${lib.optionalString userControlEnabled ''
|
||||
${lib.optionalString (userControlAllows "secrets") ''
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${subscriptionsFile}"
|
||||
''}
|
||||
chmod ${if userControlEnabled then "640" else "600"} "${subscriptionsFile}"
|
||||
chmod ${if userControlAllows "secrets" then "640" else "600"} "${subscriptionsFile}"
|
||||
'';
|
||||
|
||||
startInbounds = pkgs.writeShellScript "proxy-suite-inbounds" ''
|
||||
@@ -177,8 +177,20 @@ let
|
||||
done < <(${jq} -r '[.inbounds[].streamSettings.tlsSettings.certificates[]? | (.certificateFile, .keyFile) | strings]
|
||||
| unique[]' "$RUNTIME_DIR/config.json.tmp")
|
||||
|
||||
${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$RUNTIME_DIR/config.json.tmp"
|
||||
chmod 640 "$RUNTIME_DIR/config.json.tmp"
|
||||
# Read by XRay as ${constants.serviceUser}, and by the userControl group: the daemon
|
||||
# as owner then, since a file has one group.
|
||||
${
|
||||
if userControlAllows "secrets" then
|
||||
''
|
||||
${pkgs.coreutils}/bin/chown ${constants.serviceUser}:${lib.escapeShellArg userControlCfg.group} "$RUNTIME_DIR/config.json.tmp"
|
||||
chmod 440 "$RUNTIME_DIR/config.json.tmp"
|
||||
''
|
||||
else
|
||||
''
|
||||
${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$RUNTIME_DIR/config.json.tmp"
|
||||
chmod 640 "$RUNTIME_DIR/config.json.tmp"
|
||||
''
|
||||
}
|
||||
mv "$RUNTIME_DIR/config.json.tmp" "$RUNTIME_DIR/config.json"
|
||||
|
||||
${writeLinksBlock}
|
||||
@@ -213,11 +225,11 @@ let
|
||||
path = ../inbound-stats-add.jq;
|
||||
}
|
||||
} "$file" > "$tmp"
|
||||
${lib.optionalString userControlEnabled ''
|
||||
${lib.optionalString (userControlAllows "stats") ''
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$tmp" ||
|
||||
echo "proxy-suite: group ${userControlCfg.group} cannot read the stats; they stay root-only" >&2
|
||||
''}
|
||||
chmod ${if userControlEnabled then "640" else "600"} "$tmp"
|
||||
chmod ${if userControlAllows "stats" then "640" else "600"} "$tmp"
|
||||
mv -f "$tmp" "$file"
|
||||
'';
|
||||
in
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
proxyInboundsNeedLocalProxy,
|
||||
proxyInboundsGuardPrivate,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlEnabled,
|
||||
userControlAllows,
|
||||
buildInboundPy,
|
||||
proxyInboundsFile,
|
||||
proxyInboundsSpecFile,
|
||||
@@ -162,6 +162,7 @@ let
|
||||
proxyCfg
|
||||
perAppRoutingCfg
|
||||
userControlCfg
|
||||
userControlAllows
|
||||
globalTproxy
|
||||
xrayEnabled
|
||||
hybridEnabled
|
||||
@@ -199,7 +200,7 @@ let
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundViaOutbounds
|
||||
userControlCfg
|
||||
userControlEnabled
|
||||
userControlAllows
|
||||
localProxyAuth
|
||||
localProxyAuthEnabled
|
||||
localProxyAuthPasswordSource
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
proxyCfg,
|
||||
perAppRoutingCfg,
|
||||
userControlCfg,
|
||||
userControlAllows,
|
||||
globalTproxy,
|
||||
xrayEnabled,
|
||||
hybridEnabled,
|
||||
@@ -41,6 +42,8 @@ let
|
||||
|
||||
autoProxyRender = import ../autoproxy-render.nix { inherit pkgs; };
|
||||
runBackend = constants.runAsServiceUser pkgs constants.backendCaps;
|
||||
userControlGroup = lib.escapeShellArg userControlCfg.group;
|
||||
chgrp = "${pkgs.coreutils}/bin/chgrp";
|
||||
|
||||
routeModeBlacklistTail =
|
||||
if pureXrayEnabled then
|
||||
@@ -207,8 +210,9 @@ let
|
||||
AUTOPROXY_RULES_JSON='[]'
|
||||
${lib.optionalString enableAutoProxy ''
|
||||
AUTOPROXY_DIR=${lib.escapeShellArg autoProxyStateDir}
|
||||
# 0751: sing-box, running as ${constants.serviceUser}, reads the rule-sets inside.
|
||||
install -d -m 0751 "$AUTOPROXY_DIR"
|
||||
# 0751: sing-box, running as ${constants.serviceUser}, reads the rule-sets inside;
|
||||
# 0771 with userControl's autoProxy scope, whose group queues learn requests there.
|
||||
install -d -m ${if userControlAllows "autoProxy" then "0771" else "0751"} "$AUTOPROXY_DIR"
|
||||
[ -s "$AUTOPROXY_DIR/state.json" ] || echo '{"domains":{},"hosts":{},"exits":{},"backlog":{}}' > "$AUTOPROXY_DIR/state.json"
|
||||
|
||||
# direct is always exit 0; state is keyed by tag, so shifting indices are
|
||||
@@ -263,7 +267,7 @@ let
|
||||
' <<< "$OUTBOUNDS_JSON" > "$ENDPOINTS_TMP"
|
||||
# Where each proxy server is: not credentials, but not for every local user either.
|
||||
${
|
||||
if userControlCfg.allow != [ ] || localProxyAuthEnabled then
|
||||
if userControlCfg.enable || localProxyAuthEnabled then
|
||||
''
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$ENDPOINTS_TMP"
|
||||
chmod 640 "$ENDPOINTS_TMP"
|
||||
@@ -274,7 +278,7 @@ let
|
||||
mv "$ENDPOINTS_TMP" "$RUNTIME_DIR/outbound-endpoints.json"
|
||||
|
||||
# proxy-ctl's share links: the URL each outbound was given, and its backend JSON.
|
||||
# Credentials, so root only, whatever userControl allows.
|
||||
# Credentials: root and the userControl group only.
|
||||
SHARE_TMP="$RUNTIME_DIR/outbound-share.json.tmp"
|
||||
(umask 077 && ${jq} -c --argjson sidecar "''${XRAY_OUTBOUNDS_JSON:-[]}" --arg collapsed "''${PROXY_TAG:-}" \
|
||||
--argjson urls "$OUTBOUND_URLS_JSON" --argjson subs "$SUBSCRIPTION_URLS_JSON" '
|
||||
@@ -285,7 +289,8 @@ let
|
||||
| from_entries),
|
||||
subscriptions: $subs}
|
||||
' <<< "$OUTBOUNDS_JSON" > "$SHARE_TMP")
|
||||
chmod 600 "$SHARE_TMP"
|
||||
${lib.optionalString (userControlAllows "secrets") ''${chgrp} ${userControlGroup} "$SHARE_TMP"''}
|
||||
chmod ${if userControlAllows "secrets" then "640" else "600"} "$SHARE_TMP"
|
||||
mv "$SHARE_TMP" "$RUNTIME_DIR/outbound-share.json"
|
||||
|
||||
${lib.optionalString (!pureXrayEnabled) ''
|
||||
@@ -313,7 +318,8 @@ let
|
||||
# rule-set path and reloads the file when the probe renames a new one in.
|
||||
if [ "$ROUTE_MODE" != all-bypass ]; then
|
||||
CUTOFF_RULE_SET=${lib.escapeShellArg "${constants.zapret2CutoffDir}/proxy.json"}
|
||||
install -d -m 0755 "$(dirname "$CUTOFF_RULE_SET")"
|
||||
# The cutoff unit owns the directory's mode (the group asks for probes there).
|
||||
mkdir -p "$(dirname "$CUTOFF_RULE_SET")"
|
||||
[ -s "$CUTOFF_RULE_SET" ] || echo '{"version":1,"rules":[]}' > "$CUTOFF_RULE_SET"
|
||||
chmod 644 "$CUTOFF_RULE_SET"
|
||||
AUTOPROXY_RULE_SETS_JSON=$(${jq} -c --arg p "$CUTOFF_RULE_SET" \
|
||||
@@ -353,11 +359,23 @@ let
|
||||
"$RUNTIME_DIR/config.json" > "$RUNTIME_DIR/config.json.next"
|
||||
mv "$RUNTIME_DIR/config.json.next" "$RUNTIME_DIR/config.json"
|
||||
''}
|
||||
# The backend runs as ${constants.serviceUser}: its configs are group-readable.
|
||||
# Credentials, read by the backend running as ${constants.serviceUser}. With
|
||||
# userControl its group reads them too; a file has one group, so the daemon
|
||||
# reads as owner then.
|
||||
for backend_config in "$RUNTIME_DIR/config.json" "$RUNTIME_DIR/xray-sidecar.json"; do
|
||||
[ -e "$backend_config" ] || continue
|
||||
${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$backend_config"
|
||||
chmod ${if enableLocalProxyAuth then "640" else "g+r"} "$backend_config"
|
||||
${
|
||||
if userControlAllows "secrets" then
|
||||
''
|
||||
${pkgs.coreutils}/bin/chown ${constants.serviceUser}:${userControlGroup} "$backend_config"
|
||||
chmod 440 "$backend_config"
|
||||
''
|
||||
else
|
||||
''
|
||||
${chgrp} ${constants.serviceUser} "$backend_config"
|
||||
chmod 640 "$backend_config"
|
||||
''
|
||||
}
|
||||
done
|
||||
FAKE_IP_CACHE=$(${jq} -r '.experimental.cache_file.path? // empty' "$RUNTIME_DIR/config.json")
|
||||
if [ -n "$FAKE_IP_CACHE" ]; then
|
||||
|
||||
Reference in new issue
Block a user