@iobroker/js-controller-common 4.0.4 → 4.0.5-alpha.2-20220208-ac4cf51e

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.
Files changed (2) hide show
  1. package/lib/common/tools.js +46 -19
  2. package/package.json +2 -2
@@ -3439,33 +3439,60 @@ async function getInstancesOrderedByStartPrio(objects, logger, logPrefix = '') {
3439
3439
  * @returns {Promise<void>}
3440
3440
  */
3441
3441
  async function setExecutableCapabilities(execPath, capabilities, modeEffective, modePermitted, modeInherited) {
3442
+ if (!Array.isArray(capabilities) || !capabilities.length) {
3443
+ throw new Error('No capabilities array provided');
3444
+ }
3445
+
3442
3446
  // if not linux do nothing and silently exit
3443
- if (os.platform() === 'linux') {
3444
- if (Array.isArray(capabilities) && capabilities.length) {
3445
- let modes = '';
3446
- const capabilitiesStr = capabilities.join(',');
3447
+ if (os.platform() !== 'linux') {
3448
+ return;
3449
+ }
3447
3450
 
3448
- if (modeEffective) {
3449
- modes += 'e';
3451
+ // if Docker and Admin Capabilities should be set check if we are allowed to do that
3452
+ if (isDocker() && capabilities.includes('cap_net_admin')) {
3453
+ try {
3454
+ const systemCaps = fs.readFileSync(`/proc/${process.pid}/status`, 'utf-8');
3455
+ const capBnd = systemCaps.match(/^CapBnd:\s(.+)$/m);
3456
+ // We found a value in CapBnd line
3457
+ if (capBnd && capBnd[1]) {
3458
+ const { stdout } = await execAsync(`capsh --decode=${capBnd[1]}`);
3459
+ // Stdout looks like "0x00000000a80425fb=cap_chown,cap_dac_override,..."
3460
+ if (stdout && stdout.startsWith(`0x${capBnd[1]}=`)) {
3461
+ const capBndArr = stdout.substring(capBnd[1].length + 3).split(',');
3462
+ // if Admin Capability is not included in System Capabilities we remove it from array
3463
+ if (!capBndArr.includes('cap_net_admin')) {
3464
+ capabilities = capabilities.filter(c => c !== 'cap_net_admin');
3465
+ }
3466
+ }
3450
3467
  }
3468
+ } catch {
3469
+ // Ok we could not find it out, so update Caps but better without Admin Capability
3470
+ capabilities = capabilities.filter(c => c !== 'cap_net_admin');
3471
+ }
3472
+ }
3451
3473
 
3452
- if (modePermitted) {
3453
- modes += 'p';
3454
- }
3474
+ if (capabilities.length) {
3475
+ let modes = '';
3476
+ const capabilitiesStr = capabilities.join(',');
3455
3477
 
3456
- if (modeInherited) {
3457
- modes += 'i';
3458
- }
3478
+ if (modeEffective) {
3479
+ modes += 'e';
3480
+ }
3459
3481
 
3460
- if (modes.length) {
3461
- modes = `+${modes}`;
3462
- }
3482
+ if (modePermitted) {
3483
+ modes += 'p';
3484
+ }
3463
3485
 
3464
- // if this throws it needs to be caught outside
3465
- await cpPromise.exec(`sudo setcap ${capabilitiesStr}${modes} ${execPath}`);
3466
- } else {
3467
- throw new Error('No capabilities array provided');
3486
+ if (modeInherited) {
3487
+ modes += 'i';
3488
+ }
3489
+
3490
+ if (modes.length) {
3491
+ modes = `+${modes}`;
3468
3492
  }
3493
+
3494
+ // if this throws it needs to be caught outside
3495
+ await cpPromise.exec(`sudo setcap ${capabilitiesStr}${modes} ${execPath}`);
3469
3496
  }
3470
3497
  }
3471
3498
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/js-controller-common",
3
- "version": "4.0.4",
3
+ "version": "4.0.5-alpha.2-20220208-ac4cf51e",
4
4
  "engines": {
5
5
  "node": ">=12.0.0"
6
6
  },
@@ -47,5 +47,5 @@
47
47
  "lib/",
48
48
  "index.js"
49
49
  ],
50
- "gitHead": "184f8340136fe668fb4d64d572ebeecb7b06b6e7"
50
+ "gitHead": "164e97c24ac7094a4442a3d44268e2b480db1171"
51
51
  }