├── LICENSE ├── README.md ├── get-uid-gid.js ├── package.json └── uid-number.js /LICENSE: -------------------------------------------------------------------------------- 1 | The ISC License 2 | 3 | Copyright (c) Isaac Z. Schlueter 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR 15 | IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Use this module to convert a username/groupname to a uid/gid number. 2 | 3 | Usage: 4 | 5 | ``` 6 | npm install uid-number 7 | ``` 8 | 9 | Then, in your node program: 10 | 11 | ```javascript 12 | var uidNumber = require("uid-number") 13 | uidNumber("isaacs", function (er, uid, gid) { 14 | // gid is null because we didn't ask for a group name 15 | // uid === 24561 because that's my number. 16 | }) 17 | ``` 18 | -------------------------------------------------------------------------------- /get-uid-gid.js: -------------------------------------------------------------------------------- 1 | if (module !== require.main) { 2 | throw new Error("This file should not be loaded with require()") 3 | } 4 | 5 | if (!process.getuid || !process.getgid) { 6 | throw new Error("this file should not be called without uid/gid support") 7 | } 8 | 9 | var argv = process.argv.slice(2) 10 | , user = argv[0] || process.getuid() 11 | , group = argv[1] || process.getgid() 12 | 13 | if (!isNaN(user)) user = +user 14 | if (!isNaN(group)) group = +group 15 | 16 | console.error([user, group]) 17 | 18 | try { 19 | process.setgid(group) 20 | process.setuid(user) 21 | console.log(JSON.stringify({uid:+process.getuid(), gid:+process.getgid()})) 22 | } catch (ex) { 23 | console.log(JSON.stringify({error:ex.message,errno:ex.errno})) 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Isaac Z. Schlueter (http://blog.izs.me/)", 3 | "name": "uid-number", 4 | "description": "Convert a username/group name to a uid/gid number", 5 | "version": "0.0.6", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/npm/uid-number.git" 9 | }, 10 | "main": "uid-number.js", 11 | "dependencies": {}, 12 | "devDependencies": {}, 13 | "optionalDependencies": {}, 14 | "engines": { 15 | "node": "*" 16 | }, 17 | "license": "ISC" 18 | } 19 | -------------------------------------------------------------------------------- /uid-number.js: -------------------------------------------------------------------------------- 1 | module.exports = uidNumber 2 | 3 | // This module calls into get-uid-gid.js, which sets the 4 | // uid and gid to the supplied argument, in order to find out their 5 | // numeric value. This can't be done in the main node process, 6 | // because otherwise node would be running as that user from this 7 | // point on. 8 | 9 | var child_process = require("child_process") 10 | , path = require("path") 11 | , uidSupport = process.getuid && process.setuid 12 | , uidCache = {} 13 | , gidCache = {} 14 | 15 | function uidNumber (uid, gid, cb) { 16 | if (!uidSupport) return cb() 17 | if (typeof cb !== "function") cb = gid, gid = null 18 | if (typeof cb !== "function") cb = uid, uid = null 19 | if (gid == null) gid = process.getgid() 20 | if (uid == null) uid = process.getuid() 21 | if (!isNaN(gid)) gid = gidCache[gid] = +gid 22 | if (!isNaN(uid)) uid = uidCache[uid] = +uid 23 | 24 | if (uidCache.hasOwnProperty(uid)) uid = uidCache[uid] 25 | if (gidCache.hasOwnProperty(gid)) gid = gidCache[gid] 26 | 27 | if (typeof gid === "number" && typeof uid === "number") { 28 | return process.nextTick(cb.bind(null, null, uid, gid)) 29 | } 30 | 31 | var getter = require.resolve("./get-uid-gid.js") 32 | 33 | child_process.execFile( process.execPath 34 | , [getter, uid, gid] 35 | , function (code, out, stderr) { 36 | if (code) { 37 | var er = new Error("could not get uid/gid\n" + stderr) 38 | er.code = code 39 | return cb(er) 40 | } 41 | 42 | try { 43 | out = JSON.parse(out+"") 44 | } catch (ex) { 45 | return cb(ex) 46 | } 47 | 48 | if (out.error) { 49 | var er = new Error(out.error) 50 | er.errno = out.errno 51 | return cb(er) 52 | } 53 | 54 | if (isNaN(out.uid) || isNaN(out.gid)) return cb(new Error( 55 | "Could not get uid/gid: "+JSON.stringify(out))) 56 | 57 | cb(null, uidCache[uid] = +out.uid, gidCache[gid] = +out.gid) 58 | }) 59 | } 60 | --------------------------------------------------------------------------------