From 4554f1eef645e9d703c9cdb4ef10ab38b3950c05 Mon Sep 17 00:00:00 2001 From: Eelco Date: Mon, 15 Oct 2012 16:49:55 +0200 Subject: [PATCH 1/7] Moved to node-gyp. Now builds on OS X with node v0.8.11 --- binding.gyp | 19 +++++++++++++++++++ package.json | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 binding.gyp diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..13e5bb6 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,19 @@ +{ + "targets": [ + { + "target_name": "unixlib", + "include_dirs": [ "security/pam_appl.h" ], + "direct_dependent_settings": { + "linkflags": [ "-lpam" ] + }, + "conditions": [ + [ "OS=='win'", { + + }, { # OS != win + "cflags": [ "-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall" ] + } + ] ], + "sources": [ "unixlib.cc" ] + } + ] +} \ No newline at end of file diff --git a/package.json b/package.json index 93f85f4..144a0fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "unixlib", - "version" : "v0.1.3", + "version" : "v0.1.4", "description" : "Native Linux utilities for Node.js, currently PAM authentication, flock() and mkstemp", "homepage" : "https://github.com/ditesh/node-unixlib", "keywords": ["linux", "flock", "pam", "authentication", "unix"], @@ -11,7 +11,7 @@ }, "repository" : { "type" : "git", "url" : "https://github.com/ditesh/node-unixlib.git" }, "scripts" : { - "preinstall" : "node-waf configure && node-waf build", + "preinstall" : "node-gyp configure && node-gyp build", "preuninstall" : "rm -r build/*" }, "main" : "build/Release/unixlib.node" From 5f423df29cdad8e2513e2d5f3f430759769c1fb9 Mon Sep 17 00:00:00 2001 From: Eelco Date: Mon, 15 Oct 2012 17:19:18 +0200 Subject: [PATCH 2/7] Fixed demo cases --- demo/flock.js | 2 +- demo/mkstemp.js | 2 +- demo/pam.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/demo/flock.js b/demo/flock.js index 312ff89..ea94727 100644 --- a/demo/flock.js +++ b/demo/flock.js @@ -1,5 +1,5 @@ var fs = require("fs"); -var unixlib = require("../build/Release/unixlib"); +var unixlib = require("../build/Release/unixlib.node"); var filename = "/tmp/flock.example"; // Let's try flocking diff --git a/demo/mkstemp.js b/demo/mkstemp.js index dc7ff15..9ab0ac9 100644 --- a/demo/mkstemp.js +++ b/demo/mkstemp.js @@ -1,4 +1,4 @@ -var unixlib = require("../build/Release/unixlib"); +var unixlib = require("../build/Release/unixlib.node"); var goodstrtemplate = "/tmp/mkstempXXXXXX"; var badstrtemplate = "/tmp/mkstempXXX"; diff --git a/demo/pam.js b/demo/pam.js index 1307523..8f6aa8e 100644 --- a/demo/pam.js +++ b/demo/pam.js @@ -1,4 +1,4 @@ -var unixlib = require("../build/Release/unixlib"); +var unixlib = require("../build/Release/unixlib.node"); // Change accordingly or write your own. var service = "system-auth"; From de252db72c32b56a6c3b0a85b55adaf557a2a90c Mon Sep 17 00:00:00 2001 From: nka11 Date: Thu, 27 Dec 2012 10:26:44 +0100 Subject: [PATCH 3/7] adding crypt call + compile with node 0.8 - still ev_ref warn --- .gitignore | 1 + CHANGELOG | 4 ++ binding.gyp | 8 +++- demo/testcrypt.js | 9 +++++ package.json | 4 +- unixlib.cc | 99 +++++++++++++++++++++++++++++++++++++++++++++++ wscript | 5 ++- 7 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 demo/testcrypt.js diff --git a/.gitignore b/.gitignore index 4e0355d..2a3d2d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .*.sw? .lock-wscript +build diff --git a/CHANGELOG b/CHANGELOG index 7d2fb09..7e6be40 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +0.2.0 (): + * Node.js v0.8 support + * async crypt + 0.1.3 (Tue Nov 8 14:50:59 MYT 2011): * Node.js v0.6 support, fixes issue #3 * Updated demos, fixed miscellaneos typos diff --git a/binding.gyp b/binding.gyp index 13e5bb6..24b948f 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,10 +2,14 @@ "targets": [ { "target_name": "unixlib", - "include_dirs": [ "security/pam_appl.h" ], + "include_dirs": [ "security/pam_appl.h","crypt.h" ], "direct_dependent_settings": { - "linkflags": [ "-lpam" ] + "linkflags": [ "-lpam", "-lcrypt" ] }, + 'link_settings': { + 'libraries': ["-lpam", "-lcrypt"], + }, + "conditions": [ [ "OS=='win'", { diff --git a/demo/testcrypt.js b/demo/testcrypt.js new file mode 100644 index 0000000..8c4eb2b --- /dev/null +++ b/demo/testcrypt.js @@ -0,0 +1,9 @@ +var unixlib = require("../build/Release/unixlib.node"); + +unixlib.crypt("testpasswd","12",function(){ + console.log("with salt : ", arguments); + }); + +unixlib.crypt("testpasswd",function(){ + console.log("without salt : ",arguments); + }); \ No newline at end of file diff --git a/package.json b/package.json index 144a0fc..97e426a 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name" : "unixlib", - "version" : "v0.1.4", + "version" : "v0.2.0", "description" : "Native Linux utilities for Node.js, currently PAM authentication, flock() and mkstemp", "homepage" : "https://github.com/ditesh/node-unixlib", - "keywords": ["linux", "flock", "pam", "authentication", "unix"], + "keywords": ["linux", "flock", "pam", "authentication", "unix","crypt"], "author" : { "name" : "Ditesh Shashikant Gathani", "email" : "ditesh@gathani.org", diff --git a/unixlib.cc b/unixlib.cc index a4a2c1c..b39e3bc 100644 --- a/unixlib.cc +++ b/unixlib.cc @@ -4,8 +4,13 @@ #include #include #include +#include #include +#ifndef NODE_MAX_SALT_LEN +#define NODE_MAX_SALT_LEN 2 +#endif + #define REQ_FUN_ARG(I, VAR) \ if (args.Length() <= (I) || !args[I]->IsFunction()) \ return ThrowException(Exception::TypeError( \ @@ -25,6 +30,9 @@ static int AfterFlock(eio_req *); static Handle PAMAuthAsync(const Arguments&); static void PAMAuth(eio_req *); static int AfterPAMAuth(eio_req *); +static Handle CryptAsync(const Arguments&); +static void Crypt(eio_req *); +static int AfterCrypt(eio_req *); extern "C" void init(Handle); extern "C" { @@ -85,6 +93,13 @@ struct pam_baton { const char *password; Persistent cb; }; +struct crypt_baton { + char *passwd; + char *salt; + char *result; + Persistent cb; +}; + static Handle MkstempAsync(const Arguments& args) { @@ -165,6 +180,52 @@ static Handle PAMAuthAsync(const Arguments& args) { } +static Handle CryptAsync(const Arguments& args) { + + HandleScope scope; + const char *usage = "usage: crypt(password [, salt ], callback)"; + char salt[NODE_MAX_SALT_LEN + 1]; + bool salt_def = false; + bool valid_call = false; + int cbid = 2; + salt[0] = salt[NODE_MAX_SALT_LEN] = '\0'; + strncpy(salt, "$1$", NODE_MAX_SALT_LEN); + if (args.Length() == 2) { + valid_call = true; + cbid = 1; + } + if (args.Length() == 3) { + valid_call = true; + salt_def = true; + cbid = 2; + } + if (!valid_call) + return ThrowException(Exception::Error(String::New(usage))); + + REQ_FUN_ARG(cbid, cb); + + crypt_baton *baton = new crypt_baton(); + baton->result = false; + + String::Utf8Value password(args[0]); + if (salt_def) { + String::Utf8Value salt_arg(args[1]); + baton->salt = strdup(ToCString(salt_arg)); + } + else + baton->salt = strdup(salt); + + + baton->passwd = strdup(ToCString(password)); + + baton->cb = Persistent::New(cb); + + eio_custom(Crypt, EIO_PRI_DEFAULT, AfterCrypt, baton); + ev_ref(EV_DEFAULT_UC); + return scope.Close(Undefined()); + +} + static void Mkstemp(eio_req *req) { struct mkstemp_baton * baton = (struct mkstemp_baton *)req->data; @@ -203,6 +264,15 @@ static void PAMAuth(eio_req *req) { if (retval == PAM_SUCCESS) baton->result = true; +} +static void Crypt(eio_req *req) { + + struct crypt_baton* baton = (struct crypt_baton*) req->data; + //struct crypt_data buffer; + char *passwd = strdup(baton->passwd); + char *salt = strdup(baton->salt); + baton->result = crypt(passwd, salt); + } static int AfterMkstemp(eio_req *req) { @@ -288,11 +358,40 @@ static int AfterPAMAuth(eio_req *req) { } +static int AfterCrypt(eio_req *req) { + + HandleScope scope; + ev_unref(EV_DEFAULT_UC); + crypt_baton *baton = static_cast(req->data); + + Local argv[1]; + + if (baton->result) { + argv[0] = Local::New(True()); + argv[1] = String::New(baton->result); + } + else + argv[0] = Local::New(False()); + + TryCatch try_catch; + + baton->cb->Call(Context::GetCurrent()->Global(), 2, argv); + + if (try_catch.HasCaught()) + FatalException(try_catch); + + baton->cb.Dispose(); + delete baton; + return 0; + +} + extern "C" void init (Handle target) { HandleScope scope; NODE_SET_METHOD(target, "flock", FlockAsync); NODE_SET_METHOD(target, "pamauth", PAMAuthAsync); NODE_SET_METHOD(target, "mkstemp", MkstempAsync); + NODE_SET_METHOD(target, "crypt", CryptAsync); } diff --git a/wscript b/wscript index fd91736..51652b9 100644 --- a/wscript +++ b/wscript @@ -1,6 +1,6 @@ srcdir = '.' blddir = 'build' -VERSION = '0.0.1' +VERSION = '0.2.0' def set_options(opt): opt.tool_options('compiler_cxx') @@ -9,7 +9,8 @@ def configure(conf): conf.check_tool('compiler_cxx') conf.check_tool('node_addon') conf.check(header_name="security/pam_appl.h", mandatory=True) - conf.env.LINKFLAGS = ["-lpam"] + conf.check(header_name="crypt.h", mandatory=True) + conf.env.LINKFLAGS = ["-lpam","-lcrypt"] def build(bld): obj = bld.new_task_gen('cxx', 'shlib', 'node_addon') From 469f72ad8e7551459f706ebaf53e2ef31a331bdb Mon Sep 17 00:00:00 2001 From: nka11 Date: Thu, 27 Dec 2012 10:30:40 +0100 Subject: [PATCH 4/7] adding contributor --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 97e426a..87ab6c4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "unixlib", "version" : "v0.2.0", - "description" : "Native Linux utilities for Node.js, currently PAM authentication, flock() and mkstemp", + "description" : "Native Linux utilities for Node.js, currently PAM authentication, flock(), mkstemp and crypt()", "homepage" : "https://github.com/ditesh/node-unixlib", "keywords": ["linux", "flock", "pam", "authentication", "unix","crypt"], "author" : { @@ -9,6 +9,9 @@ "email" : "ditesh@gathani.org", "url" : "http://ditesh.gathani.org/blog/" }, + "contributors": [ + { "name": "Nicolas Karageuzian", "email": "nico@karageuzian.com" }, + }, "repository" : { "type" : "git", "url" : "https://github.com/ditesh/node-unixlib.git" }, "scripts" : { "preinstall" : "node-gyp configure && node-gyp build", From 20bc77affb939471daae582fe8ff88d101f8ed27 Mon Sep 17 00:00:00 2001 From: nka11 Date: Thu, 27 Dec 2012 12:59:41 +0100 Subject: [PATCH 5/7] fixing package.json error --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 87ab6c4..9d54e8f 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "url" : "http://ditesh.gathani.org/blog/" }, "contributors": [ - { "name": "Nicolas Karageuzian", "email": "nico@karageuzian.com" }, - }, + { "name": "Nicolas Karageuzian", "email": "nico@karageuzian.com" } + ], "repository" : { "type" : "git", "url" : "https://github.com/ditesh/node-unixlib.git" }, "scripts" : { "preinstall" : "node-gyp configure && node-gyp build", From d0c11bdcd3c631cce1a535f70f53afc777236d6b Mon Sep 17 00:00:00 2001 From: nka11 Date: Thu, 27 Dec 2012 17:55:14 +0100 Subject: [PATCH 6/7] adding sync crypt call --- demo/testcrypt.js | 5 ++++- unixlib.cc | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/demo/testcrypt.js b/demo/testcrypt.js index 8c4eb2b..c872d36 100644 --- a/demo/testcrypt.js +++ b/demo/testcrypt.js @@ -3,7 +3,10 @@ var unixlib = require("../build/Release/unixlib.node"); unixlib.crypt("testpasswd","12",function(){ console.log("with salt : ", arguments); }); - + +var res = unixlib.cryptSync("testpasswd"); +console.log(res); + unixlib.crypt("testpasswd",function(){ console.log("without salt : ",arguments); }); \ No newline at end of file diff --git a/unixlib.cc b/unixlib.cc index b39e3bc..c26a0a1 100644 --- a/unixlib.cc +++ b/unixlib.cc @@ -31,6 +31,7 @@ static Handle PAMAuthAsync(const Arguments&); static void PAMAuth(eio_req *); static int AfterPAMAuth(eio_req *); static Handle CryptAsync(const Arguments&); +static Handle CryptSync(const Arguments&); static void Crypt(eio_req *); static int AfterCrypt(eio_req *); extern "C" void init(Handle); @@ -180,6 +181,33 @@ static Handle PAMAuthAsync(const Arguments& args) { } +static Handle CryptSync(const Arguments& args) { + HandleScope scope; + const char *usage = "usage: cryptSync(password [, salt ])"; + char salt[NODE_MAX_SALT_LEN + 1]; + bool salt_def = false; + bool valid_call = false; + + strncpy(salt, "$1$", NODE_MAX_SALT_LEN); + if (args.Length() == 1) { + valid_call = true; + } + if (args.Length() == 2) { + valid_call = true; + salt_def = true; + } + if (!valid_call) + return ThrowException(Exception::Error(String::New(usage))); + +String::Utf8Value password(args[0]); + if (salt_def) { + String::Utf8Value salt_arg(args[1]); + strncpy(salt, ToCString(salt_arg), NODE_MAX_SALT_LEN); + } + char *result = crypt(ToCString(password),salt); + return scope.Close(String::New(result)); +} + static Handle CryptAsync(const Arguments& args) { HandleScope scope; @@ -188,7 +216,7 @@ static Handle CryptAsync(const Arguments& args) { bool salt_def = false; bool valid_call = false; int cbid = 2; - salt[0] = salt[NODE_MAX_SALT_LEN] = '\0'; + //salt[0] = salt[NODE_MAX_SALT_LEN] = '\0'; strncpy(salt, "$1$", NODE_MAX_SALT_LEN); if (args.Length() == 2) { valid_call = true; @@ -210,10 +238,9 @@ static Handle CryptAsync(const Arguments& args) { String::Utf8Value password(args[0]); if (salt_def) { String::Utf8Value salt_arg(args[1]); - baton->salt = strdup(ToCString(salt_arg)); + strncpy(salt, ToCString(salt_arg), NODE_MAX_SALT_LEN); } - else - baton->salt = strdup(salt); + baton->salt = strdup(salt); baton->passwd = strdup(ToCString(password)); @@ -393,5 +420,6 @@ extern "C" void init (Handle target) { NODE_SET_METHOD(target, "pamauth", PAMAuthAsync); NODE_SET_METHOD(target, "mkstemp", MkstempAsync); NODE_SET_METHOD(target, "crypt", CryptAsync); + NODE_SET_METHOD(target, "cryptSync", CryptSync); } From 658f8d639caef9342d0fbcdb01c69e86209a3355 Mon Sep 17 00:00:00 2001 From: nka11 Date: Thu, 27 Dec 2012 23:41:06 +0100 Subject: [PATCH 7/7] removing static keywords and changed api --- demo/testcrypt.js | 17 +++++++++++------ unixlib.cc | 28 +++++++++++++++------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/demo/testcrypt.js b/demo/testcrypt.js index c872d36..821cd68 100644 --- a/demo/testcrypt.js +++ b/demo/testcrypt.js @@ -1,12 +1,17 @@ var unixlib = require("../build/Release/unixlib.node"); -unixlib.crypt("testpasswd","12",function(){ - console.log("with salt : ", arguments); - }); -var res = unixlib.cryptSync("testpasswd"); -console.log(res); +var res = unixlib.crypt("testpasswd"); +console.log("without salt : ",res); + +res = unixlib.crypt("testpasswd","12"); +console.log("with salt : ",res); -unixlib.crypt("testpasswd",function(){ +// async call has unpredictable results here +// TODO: fix bugs in cc impl +unixlib.cryptAsync("testpasswd","12",function(){ + console.log("with salt : ", arguments); + }); +unixlib.cryptAsync("testpasswd",function(){ console.log("without salt : ",arguments); }); \ No newline at end of file diff --git a/unixlib.cc b/unixlib.cc index c26a0a1..014d42c 100644 --- a/unixlib.cc +++ b/unixlib.cc @@ -30,10 +30,10 @@ static int AfterFlock(eio_req *); static Handle PAMAuthAsync(const Arguments&); static void PAMAuth(eio_req *); static int AfterPAMAuth(eio_req *); -static Handle CryptAsync(const Arguments&); -static Handle CryptSync(const Arguments&); -static void Crypt(eio_req *); -static int AfterCrypt(eio_req *); +Handle CryptAsync(const Arguments&); +Handle CryptSync(const Arguments&); +void Crypt(eio_req *); +int AfterCrypt(eio_req *); extern "C" void init(Handle); extern "C" { @@ -181,10 +181,11 @@ static Handle PAMAuthAsync(const Arguments& args) { } -static Handle CryptSync(const Arguments& args) { +Handle CryptSync(const Arguments& args) { HandleScope scope; const char *usage = "usage: cryptSync(password [, salt ])"; char salt[NODE_MAX_SALT_LEN + 1]; + salt[0] = salt[NODE_MAX_SALT_LEN] = '\0'; bool salt_def = false; bool valid_call = false; @@ -205,18 +206,20 @@ String::Utf8Value password(args[0]); strncpy(salt, ToCString(salt_arg), NODE_MAX_SALT_LEN); } char *result = crypt(ToCString(password),salt); + return scope.Close(String::New(result)); } -static Handle CryptAsync(const Arguments& args) { +Handle CryptAsync(const Arguments& args) { HandleScope scope; const char *usage = "usage: crypt(password [, salt ], callback)"; char salt[NODE_MAX_SALT_LEN + 1]; + //Local salt_def = Local::New(Boolean::New(false)); bool salt_def = false; bool valid_call = false; int cbid = 2; - //salt[0] = salt[NODE_MAX_SALT_LEN] = '\0'; + salt[0] = salt[NODE_MAX_SALT_LEN] = '\0'; strncpy(salt, "$1$", NODE_MAX_SALT_LEN); if (args.Length() == 2) { valid_call = true; @@ -241,7 +244,6 @@ static Handle CryptAsync(const Arguments& args) { strncpy(salt, ToCString(salt_arg), NODE_MAX_SALT_LEN); } baton->salt = strdup(salt); - baton->passwd = strdup(ToCString(password)); @@ -292,13 +294,13 @@ static void PAMAuth(eio_req *req) { baton->result = true; } -static void Crypt(eio_req *req) { +void Crypt(eio_req *req) { struct crypt_baton* baton = (struct crypt_baton*) req->data; //struct crypt_data buffer; char *passwd = strdup(baton->passwd); char *salt = strdup(baton->salt); - baton->result = crypt(passwd, salt); + baton->result = crypt(passwd, salt); } @@ -385,7 +387,7 @@ static int AfterPAMAuth(eio_req *req) { } -static int AfterCrypt(eio_req *req) { +int AfterCrypt(eio_req *req) { HandleScope scope; ev_unref(EV_DEFAULT_UC); @@ -419,7 +421,7 @@ extern "C" void init (Handle target) { NODE_SET_METHOD(target, "flock", FlockAsync); NODE_SET_METHOD(target, "pamauth", PAMAuthAsync); NODE_SET_METHOD(target, "mkstemp", MkstempAsync); - NODE_SET_METHOD(target, "crypt", CryptAsync); - NODE_SET_METHOD(target, "cryptSync", CryptSync); + NODE_SET_METHOD(target, "cryptAsync", CryptAsync); + NODE_SET_METHOD(target, "crypt", CryptSync); }