aboutsummaryrefslogtreecommitdiff
path: root/src/shared/install.js
blob: f2a1abc08b7f1c23261d838fc2c8b16e69cc27b4 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { mkdirSync } from "node:fs";
import { chown, unlink as fsUnlink, lstat, mkdir, readFile, rm, stat } from "node:fs/promises";
import { join, basename as pathBasename, dirname as pathDirname } from "node:path";
import { localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, selinuxRuntimeEnabled, shellEscapeLsName, statAttachNanoseconds, touchStatDate } from "./common.js";
import { backupDestination, cpCopyFileContents, cpDebugLine, validateBackupMode } from "./copy.js";
import { InvocationError, UsageError, stderr, stdout } from "./diagnostics.js";
import { errnoMessage, invalidModeDiagnosticValue, mkdirParents, parseModeSpec, resolveGroup, resolveUser, selinuxSecurityContext, setFileMode, setSelinuxSecurityContext, touchSetPathTimes, withSelinuxCreationContext } from "./filesystem.js";

export const INSTALL_LONG_OPTIONS = ["backup", "compare", "directory", "debug", "group", "mode", "owner", "preserve-timestamps", "strip", "strip-program", "suffix", "target-directory", "no-target-directory", "verbose", "preserve-context", "context", "help", "version"];

export async function installCmd(args, program = "install") {
  args = normalizeInstallLongOptions(args);
  const { opts, operands } = parseOptions(args, { short: { c: false, C: false, D: false, d: false, g: "value", m: "value", o: "value", p: false, t: "value", T: false, v: false, b: false, S: "value", s: false, Z: false }, long: { compare: false, debug: false, directory: false, group: "value", mode: "value", "no-target-directory": false, owner: "value", "preserve-context": false, "preserve-timestamps": false, verbose: false, backup: "optional-value", suffix: "value", strip: false, "strip-program": "value", "target-directory": "value", context: "optional-value", help: false, version: false } });
  if (opts.p) opts["preserve-timestamps"] = true;
  if (opts.b != null && opts.backup == null) opts.backup = opts.b;
  if (opts.S != null && opts.suffix == null) opts.suffix = opts.S;
  if (opts.b || opts.backup != null) validateBackupMode(program, opts.backup);
  if ((opts.C || opts.compare) && (opts.s || opts.strip)) throw new UsageError("options --compare (-C) and --strip are mutually exclusive", true);
  if (opts["strip-program"] != null && !(opts.s || opts.strip)) {
    stderr(`${program}: WARNING: ignoring --strip-program option as -s option was not specified\n`);
  }
  const verbose = opts.v || opts.verbose || opts.debug;
  const securityContext = installSecurityContextOptions(program, opts);
  if (opts.d || opts.directory) {
    if (!operands.length) throw new UsageError("missing file operand", true);
    const mode = opts.m || opts.mode ? parseInstallMode(opts.m ?? opts.mode) : undefined;
    const uid = await resolveUser(opts.o ?? opts.owner);
    const gid = await resolveGroup(opts.g ?? opts.group);
    let status = 0;
    await withSelinuxCreationContext(program, securityContext, async (created, sameThread) => {
      for (const dir of operands) {
        try {
          const existing = await lstat(dir).catch(() => null);
          if (existing && !existing.isDirectory()) {
            stderr(`${program}: cannot create directory ${installQuotedName(dir)}: File exists\n`);
            status = 1;
            continue;
          }
          await mkdirParents(dir, mode ?? 0o755, false, created, sameThread);
          if (mode !== undefined) await setFileMode(dir, mode);
          if (uid != null || gid != null) await chown(dir, uid ?? (await stat(dir)).uid, gid ?? (await stat(dir)).gid);
          if (verbose) stdout(`${program}: creating directory ${installQuotedName(dir)}\n`);
        } catch (error) {
          if (error instanceof UsageError && error.message.startsWith("failed to set default file creation context")) throw error;
          stderr(`${program}: cannot create directory ${installQuotedName(dir)}: ${errnoMessage(error)}\n`);
          status = 1;
        }
      }
    });
    return status;
  }
  const targetDirectory = opts.t ?? opts["target-directory"];
  const noTargetDirectory = opts.T || opts["no-target-directory"];
  if (targetDirectory != null && noTargetDirectory) throw new UsageError("cannot combine --target-directory and --no-target-directory");
  if (noTargetDirectory && operands.length > 2) throw new UsageError(`extra operand ${shellEscapeLsName(operands[2], true)}`, true);
  if (targetDirectory == null && operands.length < 2) throw installMissingOperandError(operands);
  if (targetDirectory != null && operands.length < 1) throw installMissingOperandError(operands);
  const mode = opts.m || opts.mode ? parseInstallMode(opts.m ?? opts.mode) : undefined;
  const uid = await resolveUser(opts.o ?? opts.owner);
  const gid = await resolveGroup(opts.g ?? opts.group);
  const dest = targetDirectory ?? operands.at(-1);
  const sources = targetDirectory == null ? operands.slice(0, -1) : operands;
  let destStat = await stat(dest).catch(() => null);
  if (targetDirectory != null && !destStat) {
    if (opts.D) {
      try {
        await withSelinuxCreationContext(program, securityContext, (created, sameThread) => makeInstallParents(dest, opts, program, created, sameThread));
        destStat = await stat(dest);
      } catch (error) {
        stderr(`${program}: cannot create directory ${installQuotedName(dest)}: ${errnoMessage(error)}\n`);
        return 1;
      }
    } else {
      stderr(`${program}: failed to access ${installQuotedName(dest)}: No such file or directory\n`);
      return 1;
    }
  }
  if (targetDirectory != null && destStat && !destStat.isDirectory()) {
    stderr(`${program}: failed to access ${installQuotedName(dest)}: Not a directory\n`);
    return 1;
  }
  if (sources.length > 1 && !destStat?.isDirectory() && !opts.D) throw new UsageError(`target '${dest}' is not a directory`);
  if (noTargetDirectory && destStat?.isDirectory()) throw new UsageError(`cannot overwrite directory '${dest}' with non-directory '${sources[0]}'`);
  for (const src of sources) {
    const target = !noTargetDirectory && (targetDirectory != null || destStat?.isDirectory()) ? join(dest, pathBasename(src)) : dest;
    const srcStat = await stat(src).catch(() => null);
    if (!srcStat) {
      stderr(`${program}: cannot stat ${installQuotedName(src)}: No such file or directory\n`);
      return 1;
    }
    if (srcStat?.isDirectory()) {
      stderr(`${program}: omitting directory ${installQuotedName(src)}\n`);
      return 1;
    }
    const targetStat = await stat(target).catch(() => null);
    if (srcStat && targetStat && srcStat.dev === targetStat.dev && srcStat.ino === targetStat.ino) {
      const targetDisplay = targetDirectory != null && dest === "." ? `./${pathBasename(src)}` : target;
      stderr(`${program}: ${installQuotedName(src)} and ${installQuotedName(targetDisplay)} are the same file\n`);
      return 1;
    }
    if (opts.D || targetDirectory != null) {
      await withSelinuxCreationContext(program, securityContext, (created, sameThread) => makeInstallParents(pathDirname(target), opts, program, created, sameThread));
    }
    if ((opts.C || opts.compare) && await installTargetMatches(src, target, mode ?? 0o755, opts, securityContext, uid, gid)) continue;
    if (opts.b || opts.backup != null) await backupDestination(target, opts.suffix, opts.backup);
    if (await lstat(target).catch(() => null)) {
      await fsUnlink(target);
      if (opts.v || opts.verbose) stdout(`removed ${installQuotedName(target)}\n`);
    }
    const copyDebug = opts.debug ? { reflinkMode: "auto", sparseMode: "auto", debug: true } : {};
    await withSelinuxCreationContext(program, securityContext, async (created) => {
      await cpCopyFileContents(src, target, await stat(src), copyDebug);
      await created(target);
    });
    await setFileMode(target, mode ?? 0o755);
    if (uid != null || gid != null) {
      const s = await stat(target);
      await chown(target, uid ?? s.uid, gid ?? s.gid);
    }
    if (opts["preserve-timestamps"]) {
      const s = statAttachNanoseconds(await stat(src), src, true);
      await touchSetPathTimes(target, touchStatDate(s, "atime"), touchStatDate(s, "mtime"), false);
    }
    if ((opts.s || opts.strip) && !(await stripInstalledFile(target, opts["strip-program"], program))) return 1;
    await applyInstallSecurityContext(src, target, securityContext);
    if (verbose) stdout(`${installQuotedName(src)} -> ${installQuotedName(target)}\n`);
    if (opts.debug) stdout(cpDebugLine(copyDebug));
  }
  return 0;
}

export function installSecurityContextOptions(program, opts) {
  const enabled = selinuxRuntimeEnabled();
  if (typeof opts.context === "string" && !enabled) stderr(`${program}: warning: ignoring --context; it requires an SELinux-enabled kernel\n`);
  if (opts["preserve-context"] && !enabled) stderr(`${program}: WARNING: ignoring --preserve-context; this kernel is not SELinux-enabled\n`);
  const explicitContext = typeof opts.context === "string" && enabled ? opts.context : null;
  const restoreContext = enabled && (opts.Z || opts.context === true);
  const preserveContext = enabled && Boolean(opts["preserve-context"]);
  if (preserveContext && (explicitContext != null || restoreContext)) {
    throw new InvocationError("cannot set target context and preserve it", 1, false);
  }
  return { enabled, explicitContext, restoreContext, preserveContext };
}

export async function applyInstallSecurityContext(src, target, options) {
  if (!options.preserveContext) return;
  const context = selinuxSecurityContext(src);
  if (context == null) throw new UsageError(`failed to get security context of '${src}'`);
  if (!setSelinuxSecurityContext(target, context)) throw new UsageError(`failed to set the security context of '${target}'`);
}

export function installMissingOperandError(operands) {
  return operands.length
    ? new UsageError(`missing destination file operand after '${operands.at(-1)}'`, true)
    : new UsageError("missing file operand", true);
}

export function parseInstallMode(spec) {
  try {
    return /^[0-7]+$/.test(spec) ? Number.parseInt(spec, 8) : parseModeSpec(spec, 0, true, { ignoreUmask: true }).mode;
  } catch (error) {
    if (error instanceof UsageError && invalidModeDiagnosticValue(error.message) != null) {
      throw new UsageError(`invalid mode ${localeQuotedEscapedDiagnostic(spec)}`);
    }
    throw error;
  }
}

export async function makeInstallParents(dir, opts, program = "install", created = async () => {}, sameThread = false) {
  if (!dir || dir === ".") return;
  const parts = [];
  let cursor = dir;
  while (cursor && cursor !== "." && !(await stat(cursor).catch(() => null))) {
    parts.push(cursor);
    cursor = pathDirname(cursor);
  }
  for (const path of parts.reverse()) {
    if (sameThread) mkdirSync(path);
    else await mkdir(path);
    await created(path);
    if (opts.v || opts.verbose) stdout(`${program}: creating directory ${installQuotedName(path)}\n`);
  }
}

export function installQuotedName(path) {
  return shellEscapeLsName(pathDisplayName(path), true);
}

export async function installTargetMatches(src, target, mode, opts, securityContext = {}, uid = null, gid = null) {
  const srcStat = await stat(src).then((s) => statAttachNanoseconds(s, src, true), () => null);
  const targetLinkStat = await lstat(target).catch(() => null);
  if (!srcStat || !targetLinkStat?.isFile()) return false;
  const targetStat = await stat(target);
  if ((mode & 0o7000) !== 0) return false;
  if ((targetStat.mode & 0o7777) !== mode) return false;
  const expectedUid = uid ?? process.getuid?.() ?? targetStat.uid;
  const expectedGid = gid ?? process.getgid?.() ?? targetStat.gid;
  if (targetStat.uid !== expectedUid || targetStat.gid !== expectedGid) return false;
  const [srcBytes, targetBytes] = await Promise.all([readFile(src), readFile(target)]);
  if (srcBytes.length !== targetBytes.length) return false;
  for (let i = 0; i < srcBytes.length; i++) if (srcBytes[i] !== targetBytes[i]) return false;
  if (securityContext.preserveContext && selinuxSecurityContext(src) !== selinuxSecurityContext(target)) return false;
  if (opts["preserve-timestamps"]) await touchSetPathTimes(target, touchStatDate(srcStat, "atime"), touchStatDate(srcStat, "mtime"), false);
  return true;
}

export async function stripInstalledFile(target, stripProgramOption, program = "install") {
  const stripProgram = stripProgramOption ?? "strip";
  const stripTarget = target.startsWith("-") ? `./${target}` : target;
  try {
    const proc = Bun.spawn([stripProgram, stripTarget], { stdin: "ignore", stdout: "inherit", stderr: "inherit" });
    const code = await proc.exited;
    if (code === 0) return true;
  } catch (error) {
    stderr(`${program}: cannot run strip program ${installQuotedName(stripProgram)}: ${errnoMessage(error)}\n`);
  }
  await rm(target, { force: true });
  return false;
}

export function installMetaOption(args) {
  const normalized = normalizeInstallLongOptions(args);
  const longValueOptions = new Set(["group", "mode", "owner", "strip-program", "suffix", "target-directory"]);
  const longOptionalValueOptions = new Set(["backup", "context"]);
  const longKnownOptions = new Set(INSTALL_LONG_OPTIONS);
  const shortValueOptions = new Set(["g", "m", "o", "S", "t"]);
  const shortKnownOptions = new Set(["b", "c", "C", "D", "d", "g", "m", "o", "p", "s", "S", "t", "T", "v", "Z"]);
  let sawPreserveContext = false;
  let sawContextValue = false;
  for (let i = 0; i < normalized.length; i++) {
    const arg = normalized[i];
    if (arg === "--") return null;
    if (arg.startsWith("--")) {
      const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2);
      if (!longKnownOptions.has(name)) return null;
      if ((arg === "--help" || arg === "--version") && inlineValue == null) {
        if (sawPreserveContext && !selinuxRuntimeEnabled()) stderr("install: WARNING: ignoring --preserve-context; this kernel is not SELinux-enabled\n");
        if (sawContextValue && !selinuxRuntimeEnabled()) stderr("install: warning: ignoring --context; it requires an SELinux-enabled kernel\n");
        return arg;
      }
      if (name === "preserve-context") sawPreserveContext = true;
      if (name === "context" && inlineValue !== undefined) sawContextValue = true;
      if (longValueOptions.has(name)) {
        if (inlineValue == null) i++;
      } else if (longOptionalValueOptions.has(name)) {
        continue;
      } else if (inlineValue != null) return null;
      continue;
    }
    if (!arg.startsWith("-") || arg === "-") continue;
    for (let j = 1; j < arg.length; j++) {
      const ch = arg[j];
      if (!shortKnownOptions.has(ch)) return null;
      if (shortValueOptions.has(ch)) {
        if (arg.slice(j + 1) === "") i++;
        break;
      }
    }
  }
  return null;
}

export function normalizeInstallLongOptions(args) {
  const out = [];
  const valueOptions = new Set(["group", "mode", "owner", "strip-program", "suffix", "target-directory"]);
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "--") {
      out.push(arg, ...args.slice(i + 1));
      break;
    }
    if (!arg.startsWith("--")) {
      out.push(arg);
      continue;
    }
    const normalized = normalizeLongOptionByPrefix(arg, INSTALL_LONG_OPTIONS);
    out.push(normalized);
    const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2);
    if (valueOptions.has(name) && inlineValue == null && i + 1 < args.length) out.push(args[++i]);
  }
  return out;
}