easy-devops 1.4.1 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ui/assets/index-0LoFb6uX.js +532 -0
- package/dist/ui/assets/index-CldtlkZm.css +1 -0
- package/dist/ui/assets/{socket-Bv0wCEgi.js → socket-BZkwjlhM.js} +2 -2
- package/dist/ui/index.html +2 -2
- package/package.json +2 -2
- package/src/cli/elevation.js +134 -0
- package/src/cli/index.js +97 -39
- package/src/cli/menus/runtime.js +12 -1
- package/src/cli/menus/service.js +39 -1
- package/src/cli/menus/uninstall.js +11 -1
- package/src/cli/menus/update.js +10 -0
- package/src/core/cert-operations.js +74 -33
- package/src/core/cert-usage.js +181 -0
- package/src/core/deploy/engine.js +25 -1
- package/src/core/deploy/stages.js +80 -0
- package/src/core/domains-db.js +67 -1
- package/src/core/hub/agent.js +23 -3
- package/src/core/hub/deployProgress.js +9 -3
- package/src/core/platform.js +62 -0
- package/src/core/service/index.js +65 -3
- package/src/core/service/operator.js +401 -0
- package/src/core/service/runtimeUpdate.js +19 -1
- package/src/core/service/windowsService.js +91 -18
- package/src/core/updater.js +21 -0
- package/src/dashboard/controllers/domainsController.js +2 -31
- package/src/dashboard/controllers/sslController.js +14 -98
- package/src/dashboard/controllers/updateController.js +42 -1
- package/dist/ui/assets/index-Bu7af-Kj.css +0 -1
- package/dist/ui/assets/index-gndgYe3K.js +0 -532
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/service/operator.js
|
|
3
|
+
*
|
|
4
|
+
* Windows Server mode: what the INSTALLING USER may do without elevation.
|
|
5
|
+
*
|
|
6
|
+
* Server mode needs an Administrator terminal for anything that replaces the
|
|
7
|
+
* service's files. It should not need one to start or stop the service, or to
|
|
8
|
+
* run `ezz hub status` against the shared database. So install and repair give
|
|
9
|
+
* exactly one account — the one that installed it, by SID — two things:
|
|
10
|
+
*
|
|
11
|
+
* 1. On the service's own DACL: start, stop and query (`CC LC LO RP WP`).
|
|
12
|
+
* Not change its configuration (DC), not delete it (SD), not rewrite its
|
|
13
|
+
* permissions (WD) or take ownership (WO). Those stay with Administrators.
|
|
14
|
+
* 2. Modify on the data directory, so the CLI can write the database a
|
|
15
|
+
* service account also writes. Without it every write from a normal
|
|
16
|
+
* terminal failed with "SqliteError: attempt to write a readonly database".
|
|
17
|
+
*
|
|
18
|
+
* ── Why never Users or Everyone ────────────────────────────────────────────
|
|
19
|
+
*
|
|
20
|
+
* The grant is for a person, not for whoever logs on to the server. Only an
|
|
21
|
+
* account SID is accepted (S-1-5-21-…, a local or domain user; S-1-12-1-…, an
|
|
22
|
+
* Entra ID user); well-known groups are refused by shape, so no code path can
|
|
23
|
+
* be talked into granting Users (S-1-5-32-545) or Everyone (S-1-1-0).
|
|
24
|
+
*
|
|
25
|
+
* ── Why the runtime and the wrapper are protected ──────────────────────────
|
|
26
|
+
*
|
|
27
|
+
* Both live INSIDE the data directory (`runtime\`, `bin\`), so the data grant
|
|
28
|
+
* would otherwise be inherited by them — and replacing node.exe or
|
|
29
|
+
* EasyDevOps.exe would run that user's code as NT SERVICE\EasyDevOps. Those two
|
|
30
|
+
* folders stop inheriting and get an explicit list instead: SYSTEM and
|
|
31
|
+
* Administrators full control, Users and the service account read & execute.
|
|
32
|
+
* A deny entry would have been shorter and wrong: the same SID is in the
|
|
33
|
+
* user's ELEVATED token too, so it would also lock out `ezz update`.
|
|
34
|
+
*
|
|
35
|
+
* Every command here goes through shell.js and is injectable, so tests drive a
|
|
36
|
+
* stub and assert the exact commands rather than touching real ACLs.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import fs from 'fs';
|
|
40
|
+
|
|
41
|
+
import { run as shellRun } from '../shell.js';
|
|
42
|
+
import { WINDOWS_SC, windowsCurrentUserCmd } from '../platform.js';
|
|
43
|
+
import { SERVICE_ID, SERVICE_ACCOUNT } from './winsw.js';
|
|
44
|
+
|
|
45
|
+
/** Set by the relaunch, so the elevated copy grants the user who asked — see cli/elevation.js. */
|
|
46
|
+
export const OPERATOR_SID_ENV = 'EZ_OPERATOR_SID';
|
|
47
|
+
|
|
48
|
+
/** Start, stop, query status, query config, interrogate. Nothing that changes the service. */
|
|
49
|
+
export const OPERATOR_SERVICE_RIGHTS = 'CCLCLORPWP';
|
|
50
|
+
|
|
51
|
+
/** Rights an operator ACE must never carry: change config, delete, write DAC, write owner, all access. */
|
|
52
|
+
const FORBIDDEN_RIGHTS = ['DC', 'SD', 'WD', 'WO', 'GA', 'GW', 'FA'];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whether `sid` names one account (a user), not a group or a well-known SID.
|
|
56
|
+
*
|
|
57
|
+
* @param {unknown} sid
|
|
58
|
+
* @returns {boolean}
|
|
59
|
+
*/
|
|
60
|
+
export function isAccountSid(sid) {
|
|
61
|
+
return typeof sid === 'string'
|
|
62
|
+
&& (/^S-1-5-21-\d{1,10}-\d{1,10}-\d{1,10}-\d{1,10}$/.test(sid)
|
|
63
|
+
|| /^S-1-12-1-\d{1,10}-\d{1,10}-\d{1,10}-\d{1,10}$/.test(sid));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The account this process runs as.
|
|
68
|
+
*
|
|
69
|
+
* @returns {Promise<{ sid: string, name: string|null }|null>}
|
|
70
|
+
*/
|
|
71
|
+
export async function currentUser({ run = shellRun } = {}) {
|
|
72
|
+
const result = await run(windowsCurrentUserCmd(), { timeout: 20_000 });
|
|
73
|
+
const line = `${result?.stdout ?? ''}`.trim().split(/\r?\n/).pop() ?? '';
|
|
74
|
+
const [sid, name] = line.split('|');
|
|
75
|
+
if (!isAccountSid(sid?.trim())) return null;
|
|
76
|
+
return { sid: sid.trim(), name: name?.trim() || null };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Who the operator grants are for.
|
|
81
|
+
*
|
|
82
|
+
* The relaunch passes the SID of the user who asked for it; that is who
|
|
83
|
+
* installed the service even when the elevated window runs under a different
|
|
84
|
+
* administrator account. Otherwise it is this process's own account — the
|
|
85
|
+
* usual case, where the same person ran PowerShell "as administrator".
|
|
86
|
+
*
|
|
87
|
+
* @returns {Promise<{ sid: string, name: string|null, via: 'relaunch'|'current' }|null>}
|
|
88
|
+
*/
|
|
89
|
+
export async function resolveOperator({ run = shellRun, env = process.env } = {}) {
|
|
90
|
+
const me = await currentUser({ run });
|
|
91
|
+
const passed = env?.[OPERATOR_SID_ENV];
|
|
92
|
+
if (isAccountSid(passed)) {
|
|
93
|
+
return { sid: passed, name: me?.sid === passed ? me.name : null, via: 'relaunch' };
|
|
94
|
+
}
|
|
95
|
+
return me ? { ...me, via: 'current' } : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ─── The service DACL ────────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Splits an SDDL string into its DACL and the rest.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} sddl
|
|
104
|
+
* @returns {{ head: string, aces: string[], tail: string }|null}
|
|
105
|
+
*/
|
|
106
|
+
function splitDacl(sddl) {
|
|
107
|
+
const match = /^(.*?D:[A-Z]*)((?:\([^()]*\))*)(.*)$/s.exec(sddl);
|
|
108
|
+
if (!match) return null;
|
|
109
|
+
const aces = match[2].match(/\([^()]*\)/g) ?? [];
|
|
110
|
+
return { head: match[1], aces, tail: match[3] };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The trustee field of one ACE, `(type;flags;rights;object;inherit;trustee)`. */
|
|
114
|
+
function aceTrustee(ace) {
|
|
115
|
+
return ace.slice(1, -1).split(';')[5] ?? '';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The SDDL with exactly one operator ACE for `sid`, appended to the DACL.
|
|
120
|
+
*
|
|
121
|
+
* Any earlier ACE for the same SID is removed first, so a repair cannot stack
|
|
122
|
+
* grants. Allow ACEs go after the existing ones, which keeps the DACL in
|
|
123
|
+
* canonical order (explicit denies first).
|
|
124
|
+
*
|
|
125
|
+
* @param {string} sddl
|
|
126
|
+
* @param {string} sid
|
|
127
|
+
* @returns {string}
|
|
128
|
+
*/
|
|
129
|
+
export function withOperatorAce(sddl, sid) {
|
|
130
|
+
if (!isAccountSid(sid)) throw new Error(`not an account SID: ${sid}`);
|
|
131
|
+
const parts = splitDacl(sddl);
|
|
132
|
+
if (!parts) throw new Error('the service security descriptor has no DACL');
|
|
133
|
+
const kept = parts.aces.filter((ace) => aceTrustee(ace).toUpperCase() !== sid.toUpperCase());
|
|
134
|
+
return `${parts.head}${kept.join('')}(A;;${OPERATOR_SERVICE_RIGHTS};;;${sid})${parts.tail}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether the SDDL lets `sid` start and stop the service, and nothing more.
|
|
139
|
+
*
|
|
140
|
+
* @returns {{ granted: boolean, excessive: boolean }}
|
|
141
|
+
*/
|
|
142
|
+
export function operatorAceState(sddl, sid) {
|
|
143
|
+
const parts = splitDacl(sddl ?? '');
|
|
144
|
+
if (!parts || !isAccountSid(sid)) return { granted: false, excessive: false };
|
|
145
|
+
const mine = parts.aces.filter((ace) => aceTrustee(ace).toUpperCase() === sid.toUpperCase()
|
|
146
|
+
&& ace.startsWith('(A;'));
|
|
147
|
+
const fields = mine.map((ace) => ace.slice(1, -1).split(';')[2] ?? '');
|
|
148
|
+
// SDDL service rights are two-letter codes. A hex mask (0x…) cannot be read
|
|
149
|
+
// that way, so it counts as granting too much rather than as fine.
|
|
150
|
+
const hex = fields.some((field) => /^0x/i.test(field));
|
|
151
|
+
const codes = fields.flatMap((field) => (/^0x/i.test(field) ? [] : field.match(/[A-Z]{2}/g) ?? []));
|
|
152
|
+
return {
|
|
153
|
+
granted: ['RP', 'WP', 'LC'].every((code) => codes.includes(code)),
|
|
154
|
+
excessive: hex || FORBIDDEN_RIGHTS.some((code) => codes.includes(code)),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** `sc.exe sdshow`, the DACL line only. */
|
|
159
|
+
export async function readServiceSddl({ run = shellRun } = {}) {
|
|
160
|
+
const result = await run(`${WINDOWS_SC} sdshow ${SERVICE_ID}`, { timeout: 20_000 });
|
|
161
|
+
const line = `${result?.stdout ?? ''}`.split(/\r?\n/).map((l) => l.trim()).find((l) => l.startsWith('D:'));
|
|
162
|
+
return line ?? null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Lets `sid` start, stop and query the service. Needs elevation.
|
|
167
|
+
*
|
|
168
|
+
* @returns {Promise<{ ok: boolean, reason?: string }>}
|
|
169
|
+
*/
|
|
170
|
+
export async function grantServiceControl({ sid, run = shellRun } = {}) {
|
|
171
|
+
if (!isAccountSid(sid)) return { ok: false, reason: 'no user account to grant it to' };
|
|
172
|
+
|
|
173
|
+
const current = await readServiceSddl({ run });
|
|
174
|
+
if (!current) return { ok: false, reason: `could not read the ${SERVICE_ID} service permissions` };
|
|
175
|
+
|
|
176
|
+
let next;
|
|
177
|
+
try {
|
|
178
|
+
next = withOperatorAce(current, sid);
|
|
179
|
+
} catch (err) {
|
|
180
|
+
return { ok: false, reason: err.message };
|
|
181
|
+
}
|
|
182
|
+
// The value is passed to a shell. SDDL is letters, digits and `:;()-`;
|
|
183
|
+
// anything else means it was not read correctly, and it is not sent.
|
|
184
|
+
if (!/^[A-Za-z0-9:;()-]+$/.test(next)) {
|
|
185
|
+
return { ok: false, reason: 'the service permissions contained unexpected characters; nothing was changed' };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const result = await run(`${WINDOWS_SC} sdset ${SERVICE_ID} "${next}"`, { timeout: 20_000 });
|
|
189
|
+
if (!result.success) {
|
|
190
|
+
return { ok: false, reason: `${result.stdout ?? ''}${result.stderr ?? ''}`.trim().slice(0, 300) || 'sc.exe sdset failed' };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const after = operatorAceState(await readServiceSddl({ run }), sid);
|
|
194
|
+
return after.granted && !after.excessive
|
|
195
|
+
? { ok: true }
|
|
196
|
+
: { ok: false, reason: 'the permission was written but does not read back as expected' };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ─── The data directory ──────────────────────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
/** Well-known SIDs, so the commands do not depend on the display language. */
|
|
202
|
+
const SID_SYSTEM = '*S-1-5-18';
|
|
203
|
+
const SID_ADMINISTRATORS = '*S-1-5-32-544';
|
|
204
|
+
const SID_USERS = '*S-1-5-32-545';
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Modify on the data directory, inherited by what it contains.
|
|
208
|
+
*
|
|
209
|
+
* No `/T`: the grant reaches existing files and folders by inheritance, which
|
|
210
|
+
* is exactly what lets the protected folders below opt out of it. `/T` would
|
|
211
|
+
* write it onto every child explicitly, protected or not.
|
|
212
|
+
*/
|
|
213
|
+
export function buildDataDirGrantCommand(dataDir, sid) {
|
|
214
|
+
return `icacls "${safePath(dataDir)}" /grant "*${accountSid(sid)}:(OI)(CI)M" /C /Q`;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Removes every allow entry for the SID under the data directory. */
|
|
218
|
+
export function buildDataDirRevokeCommand(dataDir, sid) {
|
|
219
|
+
return `icacls "${safePath(dataDir)}" /remove:g "*${accountSid(sid)}" /T /C /Q`;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* A path that can sit inside double quotes in a shell command. Windows paths
|
|
224
|
+
* cannot contain `"`, so one that does was not a path; refused rather than
|
|
225
|
+
* escaped. The data directory can come from EASY_DEVOPS_DATA_DIR.
|
|
226
|
+
*/
|
|
227
|
+
function safePath(dir) {
|
|
228
|
+
const value = String(dir ?? '');
|
|
229
|
+
if (!value || /["\r\n`$]/.test(value)) throw new Error(`refusing an unusual path: ${JSON.stringify(value)}`);
|
|
230
|
+
return value;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** The SID, or a throw: nothing but an account SID is ever written into a grant. */
|
|
234
|
+
function accountSid(sid) {
|
|
235
|
+
if (!isAccountSid(sid)) throw new Error(`not an account SID: ${sid}`);
|
|
236
|
+
return sid;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The commands that make one folder administrator-only to change.
|
|
241
|
+
*
|
|
242
|
+
* Inheritance off, then an explicit list replacing whatever was there, then
|
|
243
|
+
* any leftover entry for the operator removed (a grant from before this
|
|
244
|
+
* protection existed was applied with `/T`).
|
|
245
|
+
*/
|
|
246
|
+
export function buildProtectCommands(dir, sid = null, { serviceAccount = true } = {}) {
|
|
247
|
+
const target = safePath(dir);
|
|
248
|
+
// Before the service is registered its account does not exist yet, and
|
|
249
|
+
// icacls refuses an unknown name; install protects again once it does.
|
|
250
|
+
const service = serviceAccount ? ` "${SERVICE_ACCOUNT}:(OI)(CI)RX"` : '';
|
|
251
|
+
const commands = [
|
|
252
|
+
// Administrators own it, so no other account holds the owner's implicit
|
|
253
|
+
// right to rewrite its permissions.
|
|
254
|
+
`icacls "${target}" /setowner "${SID_ADMINISTRATORS}" /T /C /Q`,
|
|
255
|
+
`icacls "${target}" /inheritance:r /grant:r "${SID_SYSTEM}:(OI)(CI)F" "${SID_ADMINISTRATORS}:(OI)(CI)F" `
|
|
256
|
+
+ `"${SID_USERS}:(OI)(CI)RX"${service} /T /C /Q`,
|
|
257
|
+
];
|
|
258
|
+
if (isAccountSid(sid)) commands.push(`icacls "${target}" /remove:g "*${sid}" /T /C /Q`);
|
|
259
|
+
return commands;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Makes the runtime and wrapper folders administrator-only to change.
|
|
264
|
+
*
|
|
265
|
+
* Runs on install and on every repair: a repair replaces the runtime folder
|
|
266
|
+
* with a fresh copy, and a fresh folder inherits again.
|
|
267
|
+
*
|
|
268
|
+
* @param {Object} opts
|
|
269
|
+
* @param {string[]} opts.dirs
|
|
270
|
+
* @param {string|null} [opts.sid]
|
|
271
|
+
* @returns {Promise<{ ok: boolean, protected: string[], failed: Array<{ path: string, reason: string }> }>}
|
|
272
|
+
*/
|
|
273
|
+
export async function protectServiceFiles({ dirs, sid = null, serviceAccount = true, run = shellRun, log = () => {} } = {}) {
|
|
274
|
+
const done = [];
|
|
275
|
+
const failed = [];
|
|
276
|
+
for (const dir of dirs) {
|
|
277
|
+
if (!dir || !fs.existsSync(dir)) continue;
|
|
278
|
+
let error = null;
|
|
279
|
+
let commands = [];
|
|
280
|
+
try {
|
|
281
|
+
commands = buildProtectCommands(dir, sid, { serviceAccount });
|
|
282
|
+
} catch (err) {
|
|
283
|
+
error = err.message;
|
|
284
|
+
}
|
|
285
|
+
for (const command of commands) {
|
|
286
|
+
const result = await run(command, { timeout: 120_000 });
|
|
287
|
+
if (!result.success) {
|
|
288
|
+
error = `${result.stdout ?? ''}${result.stderr ?? ''}`.trim().slice(0, 300) || 'icacls failed';
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (error) {
|
|
293
|
+
failed.push({ path: dir, reason: error });
|
|
294
|
+
log(` ${dir}: could not be protected`);
|
|
295
|
+
} else {
|
|
296
|
+
done.push(dir);
|
|
297
|
+
log(` ${dir}: administrators only (read & execute for the service and users)`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return { ok: failed.length === 0, protected: done, failed };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Everything the operator gets, applied in the order that keeps it safe:
|
|
305
|
+
* protect the service's files first, then grant the data directory.
|
|
306
|
+
*
|
|
307
|
+
* @param {Object} opts
|
|
308
|
+
* @param {{ sid: string, name: string|null }|null} opts.operator
|
|
309
|
+
* @param {string} opts.dataDir
|
|
310
|
+
* @param {string[]} opts.protectedDirs The runtime and wrapper folders
|
|
311
|
+
* @returns {Promise<Object>} the record to keep under `service-state.operator`
|
|
312
|
+
*/
|
|
313
|
+
export async function grantOperator({ operator, dataDir, protectedDirs, run = shellRun, log = () => {} } = {}) {
|
|
314
|
+
const protection = await protectServiceFiles({ dirs: protectedDirs, sid: operator?.sid ?? null, run, log });
|
|
315
|
+
|
|
316
|
+
if (!operator || !isAccountSid(operator.sid)) {
|
|
317
|
+
return {
|
|
318
|
+
record: null,
|
|
319
|
+
protection,
|
|
320
|
+
notes: ['could not tell which account ran the install, so nobody was allowed to start or stop the service '
|
|
321
|
+
+ 'without elevation'],
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const notes = [];
|
|
326
|
+
const control = await grantServiceControl({ sid: operator.sid, run });
|
|
327
|
+
if (control.ok) log(` ${operator.name ?? operator.sid}: may start, stop and restart the service`);
|
|
328
|
+
else notes.push(`start/stop without elevation was not granted: ${control.reason}`);
|
|
329
|
+
|
|
330
|
+
// Only once the service's own files are protected: the data grant is
|
|
331
|
+
// inherited, and until then it would have reached them.
|
|
332
|
+
let dataAccess = false;
|
|
333
|
+
if (protection.ok) {
|
|
334
|
+
const result = await runSafely(() => buildDataDirGrantCommand(dataDir, operator.sid), run, 60_000);
|
|
335
|
+
dataAccess = result.success;
|
|
336
|
+
if (dataAccess) log(` ${operator.name ?? operator.sid}: may use the data directory without elevation`);
|
|
337
|
+
else notes.push(`data directory access was not granted: ${`${result.stdout ?? ''}${result.stderr ?? ''}`.trim().slice(0, 200)}`);
|
|
338
|
+
} else {
|
|
339
|
+
// Fail closed. A grant from an earlier install or repair would otherwise
|
|
340
|
+
// still be inherited by a runtime folder that could not be protected.
|
|
341
|
+
await runSafely(() => buildDataDirRevokeCommand(dataDir, operator.sid), run, 120_000);
|
|
342
|
+
notes.push('the runtime folder could not be protected, so the data directory is not shared with your account; '
|
|
343
|
+
+ 'run `ezz service repair` as administrator again');
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return {
|
|
347
|
+
record: {
|
|
348
|
+
sid: operator.sid,
|
|
349
|
+
name: operator.name ?? null,
|
|
350
|
+
serviceControl: control.ok,
|
|
351
|
+
dataDir: dataAccess,
|
|
352
|
+
grantedAt: new Date().toISOString(),
|
|
353
|
+
},
|
|
354
|
+
protection,
|
|
355
|
+
notes,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Removes the operator's data directory grant. The service DACL entry needs no
|
|
361
|
+
* removal: it belongs to the service and is deleted with it.
|
|
362
|
+
*/
|
|
363
|
+
export async function revokeOperator({ operator, dataDir, run = shellRun, log = () => {} } = {}) {
|
|
364
|
+
if (!operator || !isAccountSid(operator.sid) || !dataDir || !fs.existsSync(dataDir)) return { ok: true };
|
|
365
|
+
const result = await runSafely(() => buildDataDirRevokeCommand(dataDir, operator.sid), run, 120_000);
|
|
366
|
+
if (result.success) log(` ${operator.name ?? operator.sid}: data directory access removed`);
|
|
367
|
+
return result.success
|
|
368
|
+
? { ok: true }
|
|
369
|
+
: { ok: false, reason: `${result.stdout ?? ''}${result.stderr ?? ''}`.trim().slice(0, 300) };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Builds a command and runs it; a refused build is a failed result, never a throw. */
|
|
373
|
+
async function runSafely(build, run, timeout) {
|
|
374
|
+
let command;
|
|
375
|
+
try {
|
|
376
|
+
command = build();
|
|
377
|
+
} catch (err) {
|
|
378
|
+
return { success: false, stdout: '', stderr: err.message };
|
|
379
|
+
}
|
|
380
|
+
return run(command, { timeout });
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Creates a folder the service will run code from and protects it while it is
|
|
385
|
+
* still empty.
|
|
386
|
+
*
|
|
387
|
+
* A fresh folder inside the data directory inherits the installing user's
|
|
388
|
+
* Modify grant, so copying first and protecting afterwards would leave a window
|
|
389
|
+
* in which a file could be placed or replaced before the service starts on it.
|
|
390
|
+
*
|
|
391
|
+
* @returns {Promise<{ ok: boolean, reason?: string }>}
|
|
392
|
+
*/
|
|
393
|
+
export async function prepareProtectedDir({ dir, sid = null, serviceAccount = true, run = shellRun, log = () => {} } = {}) {
|
|
394
|
+
try {
|
|
395
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
396
|
+
} catch (err) {
|
|
397
|
+
return { ok: false, reason: `could not create ${dir}: ${err.message}` };
|
|
398
|
+
}
|
|
399
|
+
const result = await protectServiceFiles({ dirs: [dir], sid, serviceAccount, run, log });
|
|
400
|
+
return result.ok ? { ok: true } : { ok: false, reason: result.failed[0]?.reason ?? `could not protect ${dir}` };
|
|
401
|
+
}
|
|
@@ -48,7 +48,7 @@ import { run } from '../shell.js';
|
|
|
48
48
|
import { isWindows, getDataDir, quoteExe } from '../platform.js';
|
|
49
49
|
import { detectInstallSource } from '../installSource.js';
|
|
50
50
|
import * as windowsPersonal from './windowsPersonal.js';
|
|
51
|
-
import { canWriteProgramData } from './windowsService.js';
|
|
51
|
+
import { canWriteProgramData, isElevated } from './windowsService.js';
|
|
52
52
|
import {
|
|
53
53
|
runtimeDir,
|
|
54
54
|
runtimeNodePath,
|
|
@@ -683,6 +683,24 @@ export async function update({
|
|
|
683
683
|
const mech = deps.mechanism ?? resolveMechanism();
|
|
684
684
|
const service = deps.service ?? (await import('./index.js'));
|
|
685
685
|
|
|
686
|
+
// 0. Windows Server mode: the runtime folder is administrator-only on
|
|
687
|
+
// purpose. Asked of Windows before anything is installed or stopped — the
|
|
688
|
+
// write probe alone is not enough once the installing user may write the
|
|
689
|
+
// data directory around it.
|
|
690
|
+
if (isWindows && mech.mode === MODES.SERVER) {
|
|
691
|
+
const elevated = await (deps.isElevated ?? isElevated)({});
|
|
692
|
+
if (elevated === false) {
|
|
693
|
+
return {
|
|
694
|
+
ok: false,
|
|
695
|
+
step: 'elevation',
|
|
696
|
+
needsElevation: true,
|
|
697
|
+
untouched: true,
|
|
698
|
+
reason: 'Moving the service to another Node version replaces node.exe in an administrator-only folder. '
|
|
699
|
+
+ 'Run `ezz runtime update` from PowerShell opened with "Run as administrator". No password is needed.',
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
686
704
|
// 1. The target has to be installable and allowed.
|
|
687
705
|
if (!target?.version) return { ok: false, step: 'target', reason: 'no target version was chosen' };
|
|
688
706
|
if (!meetsMinimum(target.version)) {
|
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
} from './winsw.js';
|
|
42
42
|
import { requiredGrants, applyGrants, revokeGrants, compareGrants } from './acl.js';
|
|
43
43
|
import { runtimeDir, copyRuntime, verifyNativeModules } from './runtime.js';
|
|
44
|
+
import { grantOperator, revokeOperator, readServiceSddl, operatorAceState, prepareProtectedDir } from './operator.js';
|
|
44
45
|
|
|
45
46
|
/** Where the wrapper, its config and its logs live. Admin-writable only. */
|
|
46
47
|
export function binDir() {
|
|
@@ -260,6 +261,7 @@ export async function install({
|
|
|
260
261
|
dataDir,
|
|
261
262
|
appRoot,
|
|
262
263
|
config = {},
|
|
264
|
+
operator = null,
|
|
263
265
|
log = () => {},
|
|
264
266
|
run = shellRun,
|
|
265
267
|
fetchImpl = fetch,
|
|
@@ -273,6 +275,17 @@ export async function install({
|
|
|
273
275
|
return { ok: false, reason: elevationMessage(), needsElevation: true };
|
|
274
276
|
}
|
|
275
277
|
|
|
278
|
+
// ── 0. The folders the service runs code from, protected while empty ──────
|
|
279
|
+
//
|
|
280
|
+
// An earlier install may have left the installing user a grant on the data
|
|
281
|
+
// directory, which a new folder would inherit. See operator.js.
|
|
282
|
+
for (const dir of [runtimeDir(dataDir), binDir()]) {
|
|
283
|
+
const prepared = await (deps.prepareProtectedDir ?? prepareProtectedDir)({
|
|
284
|
+
dir, sid: operator?.sid ?? null, serviceAccount: false, run, log,
|
|
285
|
+
});
|
|
286
|
+
if (!prepared.ok) return { ok: false, reason: `${prepared.reason}. Nothing was registered.` };
|
|
287
|
+
}
|
|
288
|
+
|
|
276
289
|
// ── 1. The pinned runtime ────────────────────────────────────────────────
|
|
277
290
|
log('Copying the runtime…');
|
|
278
291
|
const runtime = (deps.copyRuntime ?? copyRuntime)({ dataDir, appRoot, log });
|
|
@@ -388,6 +401,20 @@ export async function install({
|
|
|
388
401
|
}
|
|
389
402
|
}
|
|
390
403
|
|
|
404
|
+
// ── 6b. The installing user: start/stop, and the data directory ─────────
|
|
405
|
+
//
|
|
406
|
+
// Before the first start, so the service never runs from a runtime folder
|
|
407
|
+
// that is not yet administrator-only. See operator.js.
|
|
408
|
+
log('Allowing your account to start and stop the service without elevation…');
|
|
409
|
+
const operatorGrant = await (deps.grantOperator ?? grantOperator)({
|
|
410
|
+
operator,
|
|
411
|
+
dataDir,
|
|
412
|
+
protectedDirs: [binDir(), runtimeDir(dataDir)],
|
|
413
|
+
run,
|
|
414
|
+
log,
|
|
415
|
+
});
|
|
416
|
+
for (const note of operatorGrant.notes) log(` note: ${note}`);
|
|
417
|
+
|
|
391
418
|
// ── 7. Start ─────────────────────────────────────────────────────────────
|
|
392
419
|
log('Starting the service…');
|
|
393
420
|
const started = await run(`& "${winswPath()}" start`, { timeout: 60_000 });
|
|
@@ -402,6 +429,7 @@ export async function install({
|
|
|
402
429
|
// status must not report a registered service as "not installed".
|
|
403
430
|
granted: acl.applied.map((g) => g.path),
|
|
404
431
|
aclFailures: acl.failed,
|
|
432
|
+
operator: operatorGrant.record,
|
|
405
433
|
runtime: { nodeExe: runtime.nodeExe, entry: runtime.entry, version: native.version, abi: native.abi },
|
|
406
434
|
};
|
|
407
435
|
}
|
|
@@ -410,6 +438,8 @@ export async function install({
|
|
|
410
438
|
ok: true,
|
|
411
439
|
granted: acl.applied.map((g) => g.path),
|
|
412
440
|
aclFailures: acl.failed,
|
|
441
|
+
operator: operatorGrant.record,
|
|
442
|
+
operatorNotes: operatorGrant.notes,
|
|
413
443
|
runtime: { nodeExe: runtime.nodeExe, entry: runtime.entry, version: native.version, abi: native.abi },
|
|
414
444
|
};
|
|
415
445
|
}
|
|
@@ -422,7 +452,7 @@ export async function install({
|
|
|
422
452
|
* uninstall flow (decision 5), because the ProgramData directory holds the
|
|
423
453
|
* live data and silently relocating it would be a surprise.
|
|
424
454
|
*/
|
|
425
|
-
export async function uninstall({ dataDir, config = {}, log = () => {}, run = shellRun } = {}) {
|
|
455
|
+
export async function uninstall({ dataDir, config = {}, operator = null, log = () => {}, run = shellRun } = {}) {
|
|
426
456
|
if (!isWindows) return { ok: false, reason: 'Server mode is Windows-only' };
|
|
427
457
|
|
|
428
458
|
if ((await isElevated({ run })) === false || !canWriteProgramData()) {
|
|
@@ -462,23 +492,36 @@ export async function uninstall({ dataDir, config = {}, log = () => {}, run = sh
|
|
|
462
492
|
for (const failure of revoked.failed) notes.push(`could not clear grants on ${failure.path}`);
|
|
463
493
|
}
|
|
464
494
|
|
|
495
|
+
// The installing user's data directory access. Its start/stop right was an
|
|
496
|
+
// entry on the service's own DACL, and went with the service.
|
|
497
|
+
const operatorRevoked = await revokeOperator({ operator, dataDir, run, log });
|
|
498
|
+
if (!operatorRevoked.ok) notes.push(`could not remove your account's data directory access: ${operatorRevoked.reason}`);
|
|
499
|
+
|
|
465
500
|
return { ok: true, notes };
|
|
466
501
|
}
|
|
467
502
|
|
|
468
|
-
/**
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
503
|
+
/**
|
|
504
|
+
* `sc.exe stop`, then `sc.exe start`.
|
|
505
|
+
*
|
|
506
|
+
* Not `winsw restart`: the Service Control Manager's own verbs are what the
|
|
507
|
+
* installing user is allowed to call without elevation (operator.js), and
|
|
508
|
+
* "Access is denied" from them is the authoritative answer when they are not.
|
|
509
|
+
* Nothing is pre-checked here, so an allowed user is never told to elevate.
|
|
510
|
+
*/
|
|
511
|
+
export async function restart({ run = shellRun, stopWaitMs = 30_000 } = {}) {
|
|
512
|
+
const stopped = await stop({ run, stopWaitMs });
|
|
513
|
+
if (!stopped.ok) {
|
|
514
|
+
return stopped.needsElevation
|
|
515
|
+
? { ok: false, needsElevation: true, reason: elevationFor('Restarting') }
|
|
516
|
+
: { ok: false, reason: stopped.reason };
|
|
472
517
|
}
|
|
473
|
-
const
|
|
474
|
-
if (
|
|
475
|
-
return
|
|
518
|
+
const started = await start({ run });
|
|
519
|
+
if (!started.ok) {
|
|
520
|
+
return started.needsElevation
|
|
521
|
+
? { ok: false, needsElevation: true, reason: elevationFor('Restarting') }
|
|
522
|
+
: { ok: false, reason: started.reason };
|
|
476
523
|
}
|
|
477
|
-
|
|
478
|
-
return {
|
|
479
|
-
ok: result.success,
|
|
480
|
-
reason: result.success ? null : `${result.stdout ?? ''}${result.stderr ?? ''}`.trim().slice(0, 400),
|
|
481
|
-
};
|
|
524
|
+
return { ok: true };
|
|
482
525
|
}
|
|
483
526
|
|
|
484
527
|
/**
|
|
@@ -524,10 +567,11 @@ export async function waitForState({ wanted, run = shellRun, timeoutMs = 30_000,
|
|
|
524
567
|
function elevationFor(action) {
|
|
525
568
|
// "Stopping" → `ezz service stop`. It used to print `ezz service stopping`.
|
|
526
569
|
const verb = { Stopping: 'stop', Starting: 'start', Restarting: 'restart' }[action] ?? action.toLowerCase();
|
|
527
|
-
return `${action} the Windows service
|
|
528
|
-
+ '
|
|
570
|
+
return `${action} the Windows service was refused for this account.\n\n`
|
|
571
|
+
+ ' Run it from PowerShell opened with "Run as administrator":\n'
|
|
529
572
|
+ ` ezz service ${verb}\n\n`
|
|
530
|
-
+ '
|
|
573
|
+
+ ' To let your account start, stop and restart it without elevation, run\n'
|
|
574
|
+
+ ' `ezz service repair` once as administrator. No password is needed — only elevation.';
|
|
531
575
|
}
|
|
532
576
|
|
|
533
577
|
/**
|
|
@@ -695,7 +739,7 @@ export function countRecentRestarts({ windowMs = 10 * 60_000, now = Date.now(),
|
|
|
695
739
|
* @param {Object} [opts]
|
|
696
740
|
* @returns {Promise<Object>}
|
|
697
741
|
*/
|
|
698
|
-
export async function status({ dataDir, config = {}, run = shellRun, granted = [] } = {}) {
|
|
742
|
+
export async function status({ dataDir, config = {}, run = shellRun, granted = [], operatorSid = null } = {}) {
|
|
699
743
|
const filesPresent = fs.existsSync(winswPath()) && fs.existsSync(serviceXmlPath());
|
|
700
744
|
|
|
701
745
|
// Windows is the authority on whether the service exists — not the files.
|
|
@@ -726,7 +770,10 @@ export async function status({ dataDir, config = {}, run = shellRun, granted = [
|
|
|
726
770
|
};
|
|
727
771
|
}
|
|
728
772
|
|
|
729
|
-
const config2 = await
|
|
773
|
+
const [config2, sddl] = await Promise.all([
|
|
774
|
+
run(`${WINDOWS_SC} qc ${SERVICE_ID}`, { timeout: 20_000 }),
|
|
775
|
+
operatorSid ? readServiceSddl({ run }) : Promise.resolve(null),
|
|
776
|
+
]);
|
|
730
777
|
const startType = `${config2.stdout ?? ''}`.match(/START_TYPE\s*:\s*\d+\s+([\w_]+)/i)?.[1] ?? null;
|
|
731
778
|
|
|
732
779
|
const loop = countRecentRestarts();
|
|
@@ -760,6 +807,10 @@ export async function status({ dataDir, config = {}, run = shellRun, granted = [
|
|
|
760
807
|
crashLooping: loop.crashLooping,
|
|
761
808
|
crashLoopSince: loop.since,
|
|
762
809
|
acl: aclCheck,
|
|
810
|
+
// Whether the recorded installing user may still start and stop it — read
|
|
811
|
+
// from the service's DACL, not from the record. null: nobody recorded, or
|
|
812
|
+
// the DACL could not be read.
|
|
813
|
+
operatorControl: operatorSid && sddl ? operatorAceState(sddl, operatorSid) : null,
|
|
763
814
|
winswPath: winswPath(),
|
|
764
815
|
logs: { out: outLogPath(), err: errLogPath(), wrapper: wrapperLogPath() },
|
|
765
816
|
};
|
|
@@ -797,6 +848,7 @@ export async function repair({
|
|
|
797
848
|
dataDir,
|
|
798
849
|
appRoot,
|
|
799
850
|
config = {},
|
|
851
|
+
operator = null,
|
|
800
852
|
log = () => {},
|
|
801
853
|
run = shellRun,
|
|
802
854
|
waitForStart = async () => ({ alive: true }),
|
|
@@ -883,6 +935,13 @@ export async function repair({
|
|
|
883
935
|
};
|
|
884
936
|
}
|
|
885
937
|
|
|
938
|
+
// The new folder is protected while it is still empty, before anything the
|
|
939
|
+
// service will run is copied into it.
|
|
940
|
+
const prepared = await (deps.prepareProtectedDir ?? prepareProtectedDir)({
|
|
941
|
+
dir, sid: operator?.sid ?? null, run, log,
|
|
942
|
+
});
|
|
943
|
+
if (!prepared.ok) return restorePrevious(`the new runtime folder could not be protected: ${prepared.reason}`);
|
|
944
|
+
|
|
886
945
|
log('Refreshing the runtime copy…');
|
|
887
946
|
const runtime = (deps.copyRuntime ?? copyRuntime)({ dataDir, appRoot, log });
|
|
888
947
|
if (!runtime.ok) return restorePrevious(`the new runtime copy failed: ${runtime.reason}`);
|
|
@@ -904,6 +963,18 @@ export async function repair({
|
|
|
904
963
|
});
|
|
905
964
|
const acl = await applyGrants({ grants, run, log });
|
|
906
965
|
|
|
966
|
+
// The fresh runtime folder inherits again, so it is protected again before
|
|
967
|
+
// the service starts on it; then the installing user's rights are renewed.
|
|
968
|
+
log('Allowing your account to start and stop the service without elevation…');
|
|
969
|
+
const operatorGrant = await (deps.grantOperator ?? grantOperator)({
|
|
970
|
+
operator,
|
|
971
|
+
dataDir,
|
|
972
|
+
protectedDirs: [binDir(), dir],
|
|
973
|
+
run,
|
|
974
|
+
log,
|
|
975
|
+
});
|
|
976
|
+
for (const note of operatorGrant.notes) log(` note: ${note}`);
|
|
977
|
+
|
|
907
978
|
log('Starting the service…');
|
|
908
979
|
const started = await startAgain();
|
|
909
980
|
if (!started.ok) return restorePrevious(`the service did not come up on the new copy: ${started.reason}`);
|
|
@@ -914,6 +985,8 @@ export async function repair({
|
|
|
914
985
|
ok: true,
|
|
915
986
|
granted: acl.applied.map((g) => g.path),
|
|
916
987
|
aclFailures: acl.failed,
|
|
988
|
+
operator: operatorGrant.record,
|
|
989
|
+
operatorNotes: operatorGrant.notes,
|
|
917
990
|
runtime: { version: native.version, abi: native.abi },
|
|
918
991
|
};
|
|
919
992
|
}
|