Skip to content

Commit

Permalink
Merge pull request Unitech#4267 from SagePtr/issue4262
Browse files Browse the repository at this point in the history
Fix: Allow usernames in uid/gid/user again (fixes Unitech#4058, Unitech#4262)
  • Loading branch information
Unitech committed Oct 11, 2019
2 parents d03f650 + f165e54 commit 79a4f7c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
10 changes: 8 additions & 2 deletions lib/API/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,19 @@
"docDescription": "Current user that started the process"
},
"uid": {
"type" : "number",
"type" : [
"number",
"string"
],
"alias": "user",
"docDefault": "Current user uid",
"docDescription": "Set user id"
},
"gid": {
"type" : "number",
"type" : [
"number",
"string"
],
"docDefault": "Current user gid",
"docDescription": "Set group id"
},
Expand Down
22 changes: 20 additions & 2 deletions lib/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ Common.verifyConfs = function(appConfs) {
* Checks + Resolve UID/GID
* comes from pm2 --uid <> --gid <> or --user
*/
if ((app.uid || app.gid || app.user) && app.force !== true) {
if (app.uid || app.gid || app.user) {
// 1/ Check if windows
if (cst.IS_WINDOWS === true) {
Common.printError(cst.PREFIX_MSG_ERR + '--uid and --git does not works on windows');
Expand Down Expand Up @@ -718,7 +718,25 @@ Common.verifyConfs = function(appConfs) {

app.env.HOME = user_info.homedir
app.uid = parseInt(user_info.userId)
app.gid = parseInt(user_info.groupId)

// 4/ Resolve group id if gid is specified
if (app.gid) {
var groups
try {
groups = passwd.getGroups()
} catch(e) {
Common.printError(e);
return new Error(e);
}
var group_info = groups[app.gid]
if (!group_info) {
Common.printError(`${cst.PREFIX_MSG_ERR} Group ${app.gid} cannot be found`);
return new Error(`${cst.PREFIX_MSG_ERR} Group ${app.gid} cannot be found`);
}
app.gid = parseInt(group_info.id)
} else {
app.gid = parseInt(user_info.groupId)
}
}

/**
Expand Down
9 changes: 5 additions & 4 deletions lib/tools/passwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var getUsers = function() {
.reduce(function(map, user) {
var fields = user.split(':');

map[fields[0]] = {
map[fields[0]] = map[fields[2]] = {
username : fields[0],
password : fields[1],
userId : fields[2],
Expand Down Expand Up @@ -40,15 +40,16 @@ var getGroups = function(cb) {
.filter(function (group) {
return group.length && group[0] != '#';
})
.map(function (group) {
.reduce(function(map, group) {
var fields = group.split(':');
return {
map[fields[0]] = map[fields[2]] = {
name : fields[0],
password : fields[1],
id : fields[2],
members : fields[3].split(',')
};
})
return map;
}, {})
}

module.exports = {
Expand Down

0 comments on commit 79a4f7c

Please sign in to comment.