|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ERR_INVALID_ARG_TYPE, |
| 5 | + ERR_INVALID_ARG_VALUE, |
| 6 | + ERR_UNKNOWN_CREDENTIAL |
| 7 | +} = require('internal/errors').codes; |
| 8 | +const { |
| 9 | + validateUint32 |
| 10 | +} = require('internal/validators'); |
| 11 | + |
| 12 | +function setupProcessMethods() { |
| 13 | + // Non-POSIX platforms like Windows don't have certain methods. |
| 14 | + if (process.setgid !== undefined) { |
| 15 | + setupPosixMethods(); |
| 16 | + } |
| 17 | + |
| 18 | + const { |
| 19 | + chdir: _chdir, |
| 20 | + umask: _umask, |
| 21 | + } = process; |
| 22 | + |
| 23 | + process.chdir = chdir; |
| 24 | + process.umask = umask; |
| 25 | + |
| 26 | + function chdir(directory) { |
| 27 | + if (typeof directory !== 'string') { |
| 28 | + throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory); |
| 29 | + } |
| 30 | + return _chdir(directory); |
| 31 | + } |
| 32 | + |
| 33 | + const octalReg = /^[0-7]+$/; |
| 34 | + function umask(mask) { |
| 35 | + if (typeof mask === 'undefined') { |
| 36 | + return _umask(mask); |
| 37 | + } |
| 38 | + |
| 39 | + if (typeof mask === 'number') { |
| 40 | + validateUint32(mask, 'mask'); |
| 41 | + return _umask(mask); |
| 42 | + } |
| 43 | + |
| 44 | + if (typeof mask === 'string') { |
| 45 | + if (!octalReg.test(mask)) { |
| 46 | + throw new ERR_INVALID_ARG_VALUE('mask', mask, |
| 47 | + 'must be an octal string'); |
| 48 | + } |
| 49 | + const octal = Number.parseInt(mask, 8); |
| 50 | + validateUint32(octal, 'mask'); |
| 51 | + return _umask(octal); |
| 52 | + } |
| 53 | + |
| 54 | + throw new ERR_INVALID_ARG_TYPE('mask', ['number', 'string', 'undefined'], |
| 55 | + mask); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function setupPosixMethods() { |
| 60 | + const { |
| 61 | + initgroups: _initgroups, |
| 62 | + setegid: _setegid, |
| 63 | + seteuid: _seteuid, |
| 64 | + setgid: _setgid, |
| 65 | + setuid: _setuid, |
| 66 | + setgroups: _setgroups |
| 67 | + } = process; |
| 68 | + |
| 69 | + process.initgroups = initgroups; |
| 70 | + process.setegid = setegid; |
| 71 | + process.seteuid = seteuid; |
| 72 | + process.setgid = setgid; |
| 73 | + process.setuid = setuid; |
| 74 | + process.setgroups = setgroups; |
| 75 | + |
| 76 | + function initgroups(user, extraGroup) { |
| 77 | + validateId(user, 'user'); |
| 78 | + validateId(extraGroup, 'extraGroup'); |
| 79 | + // Result is 0 on success, 1 if user is unknown, 2 if group is unknown. |
| 80 | + const result = _initgroups(user, extraGroup); |
| 81 | + if (result === 1) { |
| 82 | + throw new ERR_UNKNOWN_CREDENTIAL('User', user); |
| 83 | + } else if (result === 2) { |
| 84 | + throw new ERR_UNKNOWN_CREDENTIAL('Group', extraGroup); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + function setegid(id) { |
| 89 | + return execId(id, 'Group', _setegid); |
| 90 | + } |
| 91 | + |
| 92 | + function seteuid(id) { |
| 93 | + return execId(id, 'User', _seteuid); |
| 94 | + } |
| 95 | + |
| 96 | + function setgid(id) { |
| 97 | + return execId(id, 'Group', _setgid); |
| 98 | + } |
| 99 | + |
| 100 | + function setuid(id) { |
| 101 | + return execId(id, 'User', _setuid); |
| 102 | + } |
| 103 | + |
| 104 | + function setgroups(groups) { |
| 105 | + if (!Array.isArray(groups)) { |
| 106 | + throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups); |
| 107 | + } |
| 108 | + for (var i = 0; i < groups.length; i++) { |
| 109 | + validateId(groups[i], `groups[${i}]`); |
| 110 | + } |
| 111 | + // Result is 0 on success. A positive integer indicates that the |
| 112 | + // corresponding group was not found. |
| 113 | + const result = _setgroups(groups); |
| 114 | + if (result > 0) { |
| 115 | + throw new ERR_UNKNOWN_CREDENTIAL('Group', groups[result - 1]); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + function execId(id, type, method) { |
| 120 | + validateId(id, 'id'); |
| 121 | + // Result is 0 on success, 1 if credential is unknown. |
| 122 | + const result = method(id); |
| 123 | + if (result === 1) { |
| 124 | + throw new ERR_UNKNOWN_CREDENTIAL(type, id); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + function validateId(id, name) { |
| 129 | + if (typeof id === 'number') { |
| 130 | + validateUint32(id, name); |
| 131 | + } else if (typeof id !== 'string') { |
| 132 | + throw new ERR_INVALID_ARG_TYPE(name, ['number', 'string'], id); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +exports.setup = setupProcessMethods; |
0 commit comments