Skip to content

Commit 17a7370

Browse files
committed
fix elevate-service on webOS 3.5
On webOS 3.5 (and probably 3.0), both the old and new Luna directories are used. Therefore, we have to process the new directory even if a service file is only found in the old one.
1 parent 657da19 commit 17a7370

File tree

1 file changed

+77
-60
lines changed

1 file changed

+77
-60
lines changed

services/elevate-service.ts

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ type Permission = {
6868
outbound?: string[];
6969
};
7070

71-
type Roles = {
71+
type Role = {
7272
appId: string;
7373
type: string;
7474
allowedNames: string[];
7575
trustLevel: string;
7676
permissions: Permission[];
7777
};
7878

79-
type LegacyRoles = {
79+
type LegacyRole = {
8080
role: {
8181
exeName: string;
8282
type: string;
@@ -89,27 +89,27 @@ function isRecord(obj: unknown): obj is Record<string, any> {
8989
return typeof obj === 'object' && !Array.isArray(obj);
9090
}
9191

92-
function patchRolesFile(path: string, legacy: boolean, requiredNames: string[] = ['*', 'com.webos.service.capture.client*']) {
93-
const rolesOriginal = readFileSync(path).toString();
94-
const rolesNew = JSON.parse(rolesOriginal) as Roles | LegacyRoles;
92+
function patchRoleFile(path: string, legacy: boolean, requiredNames: string[] = ['*', 'com.webos.service.capture.client*']) {
93+
const roleOriginal = readFileSync(path).toString();
94+
const roleNew = JSON.parse(roleOriginal) as Role | LegacyRole;
9595

9696
let allowedNames: string[] | null = null;
9797

9898
if (legacy) {
9999
// webOS <4.x /var/palm/ls2-dev role file
100-
const legacyRoles = rolesNew as LegacyRoles;
101-
if (isRecord(legacyRoles.role) && Array.isArray(legacyRoles.role.allowedNames)) {
102-
allowedNames = legacyRoles.role.allowedNames;
100+
const legacyRole = roleNew as LegacyRole;
101+
if (isRecord(legacyRole.role) && Array.isArray(legacyRole.role.allowedNames)) {
102+
allowedNames = legacyRole.role.allowedNames;
103103
} else {
104-
console.warn('[!] Legacy roles is missing allowedNames');
104+
console.warn('[!] Legacy role file is missing allowedNames');
105105
}
106106
} else {
107107
// webOS 4.x+ /var/luna-service2 role file
108-
const newRoles = rolesNew as Roles;
109-
if (Array.isArray(newRoles.allowedNames)) {
110-
allowedNames = newRoles.allowedNames;
108+
const newRole = roleNew as Role;
109+
if (Array.isArray(newRole.allowedNames)) {
110+
allowedNames = newRole.allowedNames;
111111
} else {
112-
console.warn('[!] Roles file is missing allowedNames');
112+
console.warn('[!] Role file is missing allowedNames');
113113
}
114114
}
115115

@@ -130,9 +130,9 @@ function patchRolesFile(path: string, legacy: boolean, requiredNames: string[] =
130130
// pieces of software verify explicit permission "service" key, thus we
131131
// sometimes may need some extra allowedNames/permissions, even though we
132132
// default to "*"
133-
if (Array.isArray(rolesNew.permissions)) {
133+
if (Array.isArray(roleNew.permissions)) {
134134
const missingPermissionNames = requiredNames;
135-
rolesNew.permissions.forEach((perm: { outbound?: string[]; service?: string }) => {
135+
roleNew.permissions.forEach((perm: { outbound?: string[]; service?: string }) => {
136136
if (perm.service && missingPermissionNames.includes(perm.service))
137137
missingPermissionNames.splice(missingPermissionNames.indexOf(perm.service), 1);
138138
if (perm.outbound && !perm.outbound.includes('*')) {
@@ -142,20 +142,20 @@ function patchRolesFile(path: string, legacy: boolean, requiredNames: string[] =
142142

143143
for (const name of missingPermissionNames) {
144144
console.info(`[ ] Adding permission for name: ${name}`);
145-
rolesNew.permissions.push({
145+
roleNew.permissions.push({
146146
service: name,
147147
inbound: ['*'],
148148
outbound: ['*'],
149149
});
150150
}
151151
}
152152

153-
const rolesNewContents = JSON.stringify(rolesNew);
154-
if (rolesNewContents !== JSON.stringify(JSON.parse(rolesOriginal))) {
155-
console.info(`[ ] Updating roles definition: ${path}`);
156-
console.info('-', rolesOriginal);
157-
console.info('+', rolesNewContents);
158-
writeFileSync(path, rolesNewContents);
153+
const roleNewContents = JSON.stringify(roleNew);
154+
if (roleNewContents !== JSON.stringify(JSON.parse(roleOriginal))) {
155+
console.info(`[ ] Updating role definition: ${path}`);
156+
console.info('-', roleOriginal);
157+
console.info('+', roleNewContents);
158+
writeFileSync(path, roleNewContents);
159159
return true;
160160
}
161161

@@ -181,7 +181,58 @@ function main(argv: string[]) {
181181

182182
let configChanged = false;
183183

184-
for (const lunaRoot of ['/var/luna-service2-dev', '/var/luna-service2']) {
184+
let foundLegacyDev = false;
185+
let foundLegacyNonDev = false;
186+
187+
const legacyLunaRootDev = '/var/palm/ls2-dev';
188+
const legacyLunaRootNonDev = '/var/palm/ls2';
189+
190+
for (const legacyLunaRoot of [legacyLunaRootDev, legacyLunaRootNonDev]) {
191+
const legacyPubServiceFile = `${legacyLunaRoot}/services/pub/${serviceName}.service`;
192+
const legacyPrvServiceFile = `${legacyLunaRoot}/services/prv/${serviceName}.service`;
193+
const legacyPubRoleFile = `${legacyLunaRoot}/roles/pub/${serviceName}.json`;
194+
const legacyPrvRoleFile = `${legacyLunaRoot}/roles/prv/${serviceName}.json`;
195+
196+
if (isFile(legacyPubServiceFile)) {
197+
console.info(`[~] Found legacy webOS <3.x service file: ${legacyPubServiceFile}`);
198+
if (patchServiceFile(legacyPubServiceFile)) {
199+
configChanged = true;
200+
}
201+
202+
if (isFile(legacyPrvServiceFile)) {
203+
if (patchServiceFile(legacyPrvServiceFile)) {
204+
configChanged = true;
205+
}
206+
} else {
207+
console.warn(`[!] Did not find legacy private service file: ${legacyPrvServiceFile}`);
208+
}
209+
210+
if (legacyLunaRoot === legacyLunaRootDev) {
211+
foundLegacyDev = true;
212+
} else if (legacyLunaRoot === legacyLunaRootNonDev) {
213+
foundLegacyNonDev = true;
214+
} else {
215+
console.error('[!] Something is wrong: unexpected path');
216+
}
217+
}
218+
219+
if (isFile(legacyPubRoleFile)) {
220+
if (patchRoleFile(legacyPubRoleFile, true)) {
221+
configChanged = true;
222+
}
223+
}
224+
225+
if (isFile(legacyPrvRoleFile)) {
226+
if (patchRoleFile(legacyPrvRoleFile, true)) {
227+
configChanged = true;
228+
}
229+
}
230+
}
231+
232+
const lunaRootDev = '/var/luna-service2-dev';
233+
const lunaRootNonDev = '/var/luna-service2';
234+
235+
for (const lunaRoot of [lunaRootDev, lunaRootNonDev]) {
185236
const serviceFile = `${lunaRoot}/services.d/${serviceName}.service`;
186237
const clientPermFile = `${lunaRoot}/client-permissions.d/${serviceName}.root.json`;
187238
const apiPermFile = `${lunaRoot}/api-permissions.d/${serviceName}.api.public.json`;
@@ -193,8 +244,8 @@ function main(argv: string[]) {
193244
if (patchServiceFile(serviceFile)) {
194245
configChanged = true;
195246
}
196-
} else {
197-
// Skip everything else if service file is not found.
247+
} else if ((lunaRoot === lunaRootDev && !foundLegacyDev) || (lunaRoot === lunaRootNonDev && !foundLegacyNonDev)) {
248+
// Skip everything else if no service file was found (including in legacy dir)
198249
continue;
199250
}
200251

@@ -221,7 +272,7 @@ function main(argv: string[]) {
221272
}
222273

223274
if (isFile(roleFile)) {
224-
if (patchRolesFile(roleFile, false)) {
275+
if (patchRoleFile(roleFile, false)) {
225276
configChanged = true;
226277
}
227278
}
@@ -251,40 +302,6 @@ function main(argv: string[]) {
251302
}
252303
}
253304

254-
for (const legacyLunaRoot of ['/var/palm/ls2-dev', '/var/palm/ls2']) {
255-
const legacyPubServiceFile = `${legacyLunaRoot}/services/pub/${serviceName}.service`;
256-
const legacyPrvServiceFile = `${legacyLunaRoot}/services/prv/${serviceName}.service`;
257-
const legacyPubRolesFile = `${legacyLunaRoot}/roles/pub/${serviceName}.json`;
258-
const legacyPrvRolesFile = `${legacyLunaRoot}/roles/prv/${serviceName}.json`;
259-
260-
if (isFile(legacyPubServiceFile)) {
261-
console.info(`[~] Found legacy webOS <3.x service file: ${legacyPubServiceFile}`);
262-
if (patchServiceFile(legacyPubServiceFile)) {
263-
configChanged = true;
264-
}
265-
266-
if (isFile(legacyPrvServiceFile)) {
267-
if (patchServiceFile(legacyPrvServiceFile)) {
268-
configChanged = true;
269-
}
270-
} else {
271-
console.warn(`[!] Did not find legacy private service file: ${legacyPrvServiceFile}`);
272-
}
273-
}
274-
275-
if (isFile(legacyPubRolesFile)) {
276-
if (patchRolesFile(legacyPubRolesFile, true)) {
277-
configChanged = true;
278-
}
279-
}
280-
281-
if (isFile(legacyPrvRolesFile)) {
282-
if (patchRolesFile(legacyPrvRolesFile, true)) {
283-
configChanged = true;
284-
}
285-
}
286-
}
287-
288305
if (configChanged) {
289306
console.info('[+] Refreshing services...');
290307
execFile('ls-control', ['scan-services'], { timeout: 10000 }, (err, stderr, stdout) => {

0 commit comments

Comments
 (0)