[Pkg-javascript-commits] [node-uid-number] 01/02: Imported Upstream version 0.0.6
Thorsten Alteholz
alteholz at moszumanska.debian.org
Sun Mar 27 15:39:36 UTC 2016
This is an automated email from the git hooks/post-receive script.
alteholz pushed a commit to branch master
in repository node-uid-number.
commit 11223b7127f6bf1cf54f7bb3f3ec8e727cb1789d
Author: Thorsten Alteholz <debian at alteholz.de>
Date: Sun Mar 27 17:39:33 2016 +0200
Imported Upstream version 0.0.6
---
LICENSE | 15 +++++++++++++++
README.md | 17 +++++++++++++++++
get-uid-gid.js | 24 ++++++++++++++++++++++++
package.json | 18 ++++++++++++++++++
uid-number.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 133 insertions(+)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..05eeeb8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+The ISC License
+
+Copyright (c) Isaac Z. Schlueter
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8116675
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+Use this module to convert a username/groupname to a uid/gid number.
+
+Usage:
+
+```
+npm install uid-number
+```
+
+Then, in your node program:
+
+```javascript
+var uidNumber = require("uid-number")
+uidNumber("isaacs", function (er, uid, gid) {
+ // gid is null because we didn't ask for a group name
+ // uid === 24561 because that's my number.
+})
+```
diff --git a/get-uid-gid.js b/get-uid-gid.js
new file mode 100755
index 0000000..0b39174
--- /dev/null
+++ b/get-uid-gid.js
@@ -0,0 +1,24 @@
+if (module !== require.main) {
+ throw new Error("This file should not be loaded with require()")
+}
+
+if (!process.getuid || !process.getgid) {
+ throw new Error("this file should not be called without uid/gid support")
+}
+
+var argv = process.argv.slice(2)
+ , user = argv[0] || process.getuid()
+ , group = argv[1] || process.getgid()
+
+if (!isNaN(user)) user = +user
+if (!isNaN(group)) group = +group
+
+console.error([user, group])
+
+try {
+ process.setgid(group)
+ process.setuid(user)
+ console.log(JSON.stringify({uid:+process.getuid(), gid:+process.getgid()}))
+} catch (ex) {
+ console.log(JSON.stringify({error:ex.message,errno:ex.errno}))
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..616223f
--- /dev/null
+++ b/package.json
@@ -0,0 +1,18 @@
+{
+ "author": "Isaac Z. Schlueter <i at izs.me> (http://blog.izs.me/)",
+ "name": "uid-number",
+ "description": "Convert a username/group name to a uid/gid number",
+ "version": "0.0.6",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/isaacs/uid-number.git"
+ },
+ "main": "uid-number.js",
+ "dependencies": {},
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "license": "ISC"
+}
diff --git a/uid-number.js b/uid-number.js
new file mode 100644
index 0000000..bd62184
--- /dev/null
+++ b/uid-number.js
@@ -0,0 +1,59 @@
+module.exports = uidNumber
+
+// This module calls into get-uid-gid.js, which sets the
+// uid and gid to the supplied argument, in order to find out their
+// numeric value. This can't be done in the main node process,
+// because otherwise node would be running as that user from this
+// point on.
+
+var child_process = require("child_process")
+ , path = require("path")
+ , uidSupport = process.getuid && process.setuid
+ , uidCache = {}
+ , gidCache = {}
+
+function uidNumber (uid, gid, cb) {
+ if (!uidSupport) return cb()
+ if (typeof cb !== "function") cb = gid, gid = null
+ if (typeof cb !== "function") cb = uid, uid = null
+ if (gid == null) gid = process.getgid()
+ if (uid == null) uid = process.getuid()
+ if (!isNaN(gid)) gid = gidCache[gid] = +gid
+ if (!isNaN(uid)) uid = uidCache[uid] = +uid
+
+ if (uidCache.hasOwnProperty(uid)) uid = uidCache[uid]
+ if (gidCache.hasOwnProperty(gid)) gid = gidCache[gid]
+
+ if (typeof gid === "number" && typeof uid === "number") {
+ return process.nextTick(cb.bind(null, null, uid, gid))
+ }
+
+ var getter = require.resolve("./get-uid-gid.js")
+
+ child_process.execFile( process.execPath
+ , [getter, uid, gid]
+ , function (code, out, stderr) {
+ if (code) {
+ var er = new Error("could not get uid/gid\n" + stderr)
+ er.code = code
+ return cb(er)
+ }
+
+ try {
+ out = JSON.parse(out+"")
+ } catch (ex) {
+ return cb(ex)
+ }
+
+ if (out.error) {
+ var er = new Error(out.error)
+ er.errno = out.errno
+ return cb(er)
+ }
+
+ if (isNaN(out.uid) || isNaN(out.gid)) return cb(new Error(
+ "Could not get uid/gid: "+JSON.stringify(out)))
+
+ cb(null, uidCache[uid] = +out.uid, gidCache[gid] = +out.gid)
+ })
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-uid-number.git
More information about the Pkg-javascript-commits
mailing list