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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#!/usr/bin/env bun
import { access, lstat } from "node:fs/promises";
import { invalidOptionMessage, normalizeLongOptionByPrefix, parseOptions, pathLikeJoin, readdirPathEntries } from "../shared/common.js";
import { UsageError, stdout } from "../shared/diagnostics.js";
import { selinuxSecurityContext, setSelinuxSecurityContext } from "../shared/filesystem.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export const CHCON_LONG_OPTIONS = ["dereference", "no-dereference", "no-preserve-root", "preserve-root", "recursive", "reference", "user", "role", "type", "range", "verbose", "help", "version"];
export function chconMetaOption(args) {
const longValueOptions = new Set(["reference", "user", "role", "type", "range"]);
const longFlagOptions = new Set(["dereference", "no-dereference", "no-preserve-root", "preserve-root", "recursive", "verbose"]);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") return null;
let normalized = arg;
if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, CHCON_LONG_OPTIONS);
const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined];
if (name === "help" || name === "version") {
if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
return normalized;
}
if (longFlagOptions.has(name)) {
if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
continue;
}
if (longValueOptions.has(name)) {
if (inlineValue === undefined) i++;
continue;
}
if (["-u", "-r", "-t", "-l"].includes(arg)) {
i++;
continue;
}
if (/^-[urtl].+/.test(arg)) continue;
if (/^-[Rhv]+$/.test(arg)) continue;
if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true);
continue;
}
return null;
}
export async function chconCmd(args) {
const { opts, operands } = parseOptions(normalizeChconArgs(args), {
short: { R: false, h: false, u: "value", r: "value", t: "value", l: "value", v: false },
long: { recursive: false, dereference: false, "no-dereference": false, "no-preserve-root": false, "preserve-root": false, reference: "value", user: "value", role: "value", type: "value", range: "value", verbose: false },
});
const reference = opts.reference;
const componentChange = opts.u != null || opts.user != null || opts.r != null || opts.role != null || opts.t != null || opts.type != null || opts.l != null || opts.range != null;
if (reference !== undefined && componentChange) throw new UsageError("conflicting security context specifiers given", true);
let context = null;
if (reference !== undefined) {
if (!operands.length) throw new UsageError("missing operand", true);
await access(reference);
context = selinuxSecurityContext(reference, opts.h || opts["no-dereference"]);
if (context == null) throw new UsageError(`failed to get security context of '${reference}'`);
for (const file of operands) await chconAccessPath(file, opts, context);
return 0;
}
const files = componentChange ? operands : operands.slice(1);
if (files.length < 1) throw new UsageError("missing operand", true);
if (!componentChange) context = operands[0];
for (const file of files) await chconAccessPath(file, opts, context);
return 0;
}
export function normalizeChconArgs(args) {
const out = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") {
out.push(...args.slice(i));
break;
}
if (arg.startsWith("--")) {
const normalized = normalizeLongOptionByPrefix(arg, CHCON_LONG_OPTIONS);
out.push(normalized);
if (["--reference", "--user", "--role", "--type", "--range"].includes(normalized) && i + 1 < args.length) out.push(args[++i]);
continue;
}
out.push(arg);
if (["-u", "-r", "-t", "-l"].includes(arg) && i + 1 < args.length) out.push(args[++i]);
}
return out;
}
export async function chconAccessPath(path, opts, context = null) {
const s = await lstat(path);
if ((opts.R || opts.recursive) && s.isDirectory() && !s.isSymbolicLink()) {
for (const entry of await readdirPathEntries(path)) await chconAccessPath(pathLikeJoin(path, entry), opts, context);
}
const noDereference = opts.h || opts["no-dereference"];
const current = selinuxSecurityContext(path, noDereference);
if (opts.v || opts.verbose) stdout(`changing security context of '${path}'\n`);
if (context == null && current == null) throw new UsageError(`can't apply partial context to unlabeled file '${path}'`);
const target = context ?? chconUpdatedContext(current, opts);
if (target !== current) {
if (!setSelinuxSecurityContext(path, target, noDereference)) throw new UsageError(`failed to change context of '${path}' to '${target}'`);
}
}
export function chconUpdatedContext(current, opts) {
const parts = String(current).split(":");
if (parts.length < 3) return current;
const range = parts.slice(3).join(":");
const user = opts.u ?? opts.user ?? parts[0];
const role = opts.r ?? opts.role ?? parts[1];
const type = opts.t ?? opts.type ?? parts[2];
const newRange = opts.l ?? opts.range ?? range;
return [user, role, type, newRange].filter((part, index) => index < 3 || part !== "").join(":");
}
const singleCall = defineCommand("chcon", chconCmd, chconMetaOption);
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|