@@ -68,15 +68,15 @@ type Permission = {
68
68
outbound ?: string [ ] ;
69
69
} ;
70
70
71
- type Roles = {
71
+ type Role = {
72
72
appId : string ;
73
73
type : string ;
74
74
allowedNames : string [ ] ;
75
75
trustLevel : string ;
76
76
permissions : Permission [ ] ;
77
77
} ;
78
78
79
- type LegacyRoles = {
79
+ type LegacyRole = {
80
80
role : {
81
81
exeName : string ;
82
82
type : string ;
@@ -89,27 +89,27 @@ function isRecord(obj: unknown): obj is Record<string, any> {
89
89
return typeof obj === 'object' && ! Array . isArray ( obj ) ;
90
90
}
91
91
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 ;
95
95
96
96
let allowedNames : string [ ] | null = null ;
97
97
98
98
if ( legacy ) {
99
99
// 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 ;
103
103
} else {
104
- console. warn ( '[!] Legacy roles is missing allowedNames' ) ;
104
+ console. warn ( '[!] Legacy role file is missing allowedNames' ) ;
105
105
}
106
106
} else {
107
107
// 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 ;
111
111
} else {
112
- console . warn ( '[!] Roles file is missing allowedNames' ) ;
112
+ console . warn ( '[!] Role file is missing allowedNames' ) ;
113
113
}
114
114
}
115
115
@@ -130,9 +130,9 @@ function patchRolesFile(path: string, legacy: boolean, requiredNames: string[] =
130
130
// pieces of software verify explicit permission "service" key, thus we
131
131
// sometimes may need some extra allowedNames/permissions, even though we
132
132
// default to "*"
133
- if ( Array . isArray ( rolesNew . permissions ) ) {
133
+ if ( Array . isArray ( roleNew . permissions ) ) {
134
134
const missingPermissionNames = requiredNames ;
135
- rolesNew . permissions . forEach ( ( perm : { outbound ?: string [ ] ; service ?: string } ) => {
135
+ roleNew . permissions . forEach ( ( perm : { outbound ?: string [ ] ; service ?: string } ) => {
136
136
if ( perm . service && missingPermissionNames . includes ( perm . service ) )
137
137
missingPermissionNames . splice ( missingPermissionNames . indexOf ( perm . service ) , 1 ) ;
138
138
if ( perm . outbound && ! perm . outbound . includes ( '*' ) ) {
@@ -142,20 +142,20 @@ function patchRolesFile(path: string, legacy: boolean, requiredNames: string[] =
142
142
143
143
for ( const name of missingPermissionNames ) {
144
144
console . info ( `[ ] Adding permission for name: ${ name } ` ) ;
145
- rolesNew . permissions . push ( {
145
+ roleNew . permissions . push ( {
146
146
service : name ,
147
147
inbound : [ '*' ] ,
148
148
outbound : [ '*' ] ,
149
149
} ) ;
150
150
}
151
151
}
152
152
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 ) ;
159
159
return true ;
160
160
}
161
161
@@ -181,7 +181,58 @@ function main(argv: string[]) {
181
181
182
182
let configChanged = false ;
183
183
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 ] ) {
185
236
const serviceFile = `${ lunaRoot } /services.d/${ serviceName } .service` ;
186
237
const clientPermFile = `${ lunaRoot } /client-permissions.d/${ serviceName } .root.json` ;
187
238
const apiPermFile = `${ lunaRoot } /api-permissions.d/${ serviceName } .api.public.json` ;
@@ -193,8 +244,8 @@ function main(argv: string[]) {
193
244
if ( patchServiceFile ( serviceFile ) ) {
194
245
configChanged = true ;
195
246
}
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)
198
249
continue ;
199
250
}
200
251
@@ -221,7 +272,7 @@ function main(argv: string[]) {
221
272
}
222
273
223
274
if ( isFile ( roleFile ) ) {
224
- if ( patchRolesFile ( roleFile , false ) ) {
275
+ if ( patchRoleFile ( roleFile , false ) ) {
225
276
configChanged = true ;
226
277
}
227
278
}
@@ -251,40 +302,6 @@ function main(argv: string[]) {
251
302
}
252
303
}
253
304
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
-
288
305
if ( configChanged ) {
289
306
console . info ( '[+] Refreshing services...' ) ;
290
307
execFile ( 'ls-control' , [ 'scan-services' ] , { timeout : 10000 } , ( err , stderr , stdout ) => {
0 commit comments