Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 4d5dfc599a8c6bcdb46698cae128a64a7d2212df @ditesh committed Jun 13, 2011
Showing with 172 additions and 0 deletions.
  1. +26 −0 LICENSE
  2. +1 −0 README
  3. +5 −0 demo/demo.js
  4. +20 −0 package.json
  5. +107 −0 pam.cc
  6. +13 −0 wscript
@@ -0,0 +1,26 @@
+Copyright 2011, Ditesh Shashikant Gathani. All rights reserved.
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of Olivier Lalonde nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1 @@
+PAM authentication module for NodeJS.
@@ -0,0 +1,5 @@
+var pamlib = require('../build/default/pam.node');
+var pam = new pamlib.PAM();
+console.log("Username: ditesh, password: mypass, output: " + pam.authenticate("system-auth", "ditesh", 'mypass')); // should output false
+console.log("Username: ditesh, password: mypass, output: " + pam.authenticate("system-auth", "ditesh", 'efg')); // should output true
+
@@ -0,0 +1,20 @@
+{
+ "name" : "pam"
+ , "version" : "v0.1.0"
+ , "description" : "Native Node.js C++ extension for authenticating off PAM."
+ , "homepage" : "https://github.com/ditesh/node-pam"
+ , "author" : {
+ "name" : "Ditesh Shashikant Gathani"
+ , "email" : "ditesh@gathani.org"
+ , "url" : "http://ditesh.gathani.org/blog/"
+ }
+ , "repository" : {
+ "type" : "git"
+ , "url" : "https://github.com/ditesh/node-pam.git"
+ }
+ , "scripts" : {
+ "preinstall" : "node-waf configure && node-waf build"
+ , "preuninstall" : "rm -rf build/*"
+ }
+ , "main" : "build/default/pam.node"
+}
@@ -0,0 +1,107 @@
+#include <v8.h>
+#include <node.h>
+#include <stdlib.h>
+#include <typeinfo>
+#include <iostream>
+#include <security/pam_appl.h>
+
+struct pam_response *reply;
+
+int null_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) {
+
+ printf("in nullconv");
+ *resp = reply;
+ return PAM_SUCCESS;
+
+}
+
+static struct pam_conv conv = { null_conv, NULL };
+
+const char* ToCString(const v8::String::Utf8Value& value) {
+ return *value ? *value : "<string conversion failed>";
+}
+
+int _pam_authenticate(char *service, char *username, char *password) {
+
+ pam_handle_t *pamh = NULL;
+ int retval = pam_start(service, username, &conv, &pamh);
+
+ if (retval == PAM_SUCCESS) {
+
+ reply = (struct pam_response *) malloc(sizeof(struct pam_response));
+ reply[0].resp = password;
+ reply[0].resp_retcode = 0;
+
+ retval = pam_authenticate(pamh, 0);
+ pam_end(pamh, PAM_SUCCESS);
+
+ }
+
+ return retval;
+
+}
+
+using namespace node;
+using namespace v8;
+
+class PAM:ObjectWrap {
+
+private: int m_count;
+public:
+
+ static Persistent<FunctionTemplate> s_ct;
+ static void Init(Handle<Object> target) {
+
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(New);
+
+ s_ct = Persistent<FunctionTemplate>::New(t);
+ s_ct->InstanceTemplate()->SetInternalFieldCount(1);
+ s_ct->SetClassName(String::NewSymbol("PAM"));
+
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "authenticate", authenticate);
+ target->Set(String::NewSymbol("PAM"), s_ct->GetFunction());
+
+ }
+
+ ~PAM() {}
+
+ static Handle<Value> New(const Arguments& args) {
+
+ HandleScope scope;
+ PAM* hw = new PAM();
+ hw->Wrap(args.This());
+ return args.This();
+
+ }
+
+ static Handle<Value> authenticate(const Arguments& args) {
+
+ HandleScope scope;
+ v8::String::Utf8Value service(args[0]);
+ v8::String::Utf8Value username(args[1]);
+ v8::String::Utf8Value password(args[2]);
+ bool result = false;
+
+ int retval = _pam_authenticate((char *) ToCString(service), (char *) ToCString(username), (char *) ToCString(password));
+
+ if (retval == PAM_SUCCESS)
+ result = true;
+
+ Local<String> ret = String::New("abc");
+ return ret;//scope.Close(ret);
+
+ }
+};
+
+Persistent<FunctionTemplate> PAM::s_ct;
+
+extern "C" {
+
+ static void init (Handle<Object> target) {
+ PAM::Init(target);
+ }
+
+ NODE_MODULE(pam, init);
+}
@@ -0,0 +1,13 @@
+def set_options(opt):
+ opt.tool_options("compiler_cxx")
+
+def configure(conf):
+ conf.check_tool("compiler_cxx")
+ conf.check_tool("node_addon")
+ conf.env.LINKFLAGS = ["-lpam"]
+
+def build(bld):
+ obj = bld.new_task_gen("cxx", "shlib", "node_addon")
+ obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
+ obj.target = "pam"
+ obj.source = "pam.cc"

0 comments on commit 4d5dfc5

Please sign in to comment.