Created
February 7, 2013 01:54
-
-
Save nemtsov/4727739 to your computer and use it in GitHub Desktop.
Workaround for getting the uid / gid when only the 'username' is available. - Advantages: works without needing to compile a C++ addon - Disadvantages: need to be root to run it :(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------- | |
// userinfo.js | |
//----------------------------------- | |
/** | |
* Workaround for getting | |
* the uid / gid when only | |
* the 'username' is available. | |
* | |
* Works on POSIX ONLY (i.e. not Windows) | |
*/ | |
var fork = require('child_process').fork | |
, child; | |
exports.getUserInfo = function (username, groupname, cb) { | |
if (2 === arguments.length) { | |
cb = groupname; | |
groupname = -1; | |
} | |
child = fork(__dirname + '/getuid'); | |
child.on('message', function (userInfo) { | |
cb(null, userInfo); | |
}); | |
child.on('error', cb); | |
child.send({ | |
username: username | |
, groupname: groupname | |
}); | |
}; | |
//----------------------------------- | |
// getuid.js | |
//----------------------------------- | |
/** | |
* Child process for the workaround | |
* for getting user-info. | |
*/ | |
exports.getUserInfo = function (user) { | |
process.setgid(user.groupname); | |
process.setuid(user.username); | |
return { | |
username: user.username | |
, groupname: user.groupname | |
, uid: process.getuid() | |
, gid: process.getgid() | |
}; | |
} | |
process.on('message', function (userName) { | |
process.send(exports.getUserInfo(userName)); | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment