Permalink
Browse files

Added libeio support

  • Loading branch information...
1 parent 4fe9f2c commit 9c782a722f640d4db2923aa7e09ed248507c3f9a @ditesh committed Jun 14, 2011
Showing with 84 additions and 11 deletions.
  1. +6 −1 demo/demo.js
  2. +78 −10 pam.cc
View
@@ -9,5 +9,10 @@ var pam = new pamlib.PAM();
var service = "system-auth";
var username = "myusername";
var password = "mypassword";
-console.log("Username: " + username + ", password: " + password + ", output: " + pam.authenticate(service, username, password));
+pam.authenticate(service, username, password, function(result) {
+ console.log("Username: " + username + ", password: " + password + ", result: " + result);
+
+});
+
+console.log("This will get printed first");
View
88 pam.cc
@@ -2,10 +2,15 @@
#include <node.h>
#include <string.h>
#include <stdlib.h>
-#include <typeinfo>
-#include <iostream>
#include <security/pam_appl.h>
+#define REQ_FUN_ARG(I, VAR) \
+ if (args.Length() <= (I) || !args[I]->IsFunction()) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be a function"))); \
+ Local<Function> VAR = Local<Function>::Cast(args[I]);
+
+
struct pam_response *reply;
int null_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) {
@@ -22,15 +27,15 @@ const char* ToCString(const v8::String::Utf8Value& value) {
}
extern "C" {
- int _pam_authenticate(char *service, char *username, char *password) {
+ int _pam_authenticate(const char *service, const char *username, const 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 = (char *) password;
reply[0].resp_retcode = 0;
retval = pam_authenticate(pamh, 0);
@@ -78,20 +83,83 @@ private: int m_count;
}
+ struct baton_t {
+ PAM *hw;
+ const char *service;
+ const char *username;
+ const char *password;
+ bool result;
+ Persistent<Function> cb;
+ };
+
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;
+ REQ_FUN_ARG(3, cb);
+
+ PAM* hw = ObjectWrap::Unwrap<PAM>(args.This());
+ baton_t *baton = new baton_t();
+ baton->hw = hw;
+
+ String::Utf8Value service(args[0]);
+ String::Utf8Value username(args[1]);
+ String::Utf8Value password(args[2]);
+
+ baton->service = strdup(ToCString(service));
+ baton->username = strdup(ToCString(username));
+ baton->password = strdup(ToCString(password));
+ baton->cb = Persistent<Function>::New(cb);
+ baton->result = false;
- int retval = _pam_authenticate(strdup((char *) ToCString(service)), strdup((char *) ToCString(username)), strdup((char *) ToCString(password)));
+ hw->Ref();
+
+ eio_custom(EIO_pam, EIO_PRI_DEFAULT, EIO_AfterPam, baton);
+ ev_ref(EV_DEFAULT_UC);
+
+ return Undefined();
+
+ }
+
+ static int EIO_pam(eio_req *req) {
+
+ bool result = false;
+ struct baton_t* args = (struct baton_t *) req->data;
+ int retval = _pam_authenticate(args->service, args->username, args->password);
if (retval == PAM_SUCCESS)
result = true;
- return Boolean::New(result);
+ args->result = result;
+ return 0;
+
+ }
+
+ static int EIO_AfterPam(eio_req *req) {
+
+ HandleScope scope;
+ baton_t *baton = static_cast<baton_t *>(req->data);
+ ev_unref(EV_DEFAULT_UC);
+ baton->hw->Unref();
+
+ Local<Value> argv[1];
+
+ // This doesn't work
+ //argv[0] = False();
+
+ // This works, but this is not what we want
+ argv[0] = Integer::New(baton->result);
+
+ TryCatch try_catch;
+
+ baton->cb->Call(Context::GetCurrent()->Global(), 1, argv);
+
+ if (try_catch.HasCaught())
+ FatalException(try_catch);
+
+ baton->cb.Dispose();
+
+ delete baton;
+ return 0;
}
};

0 comments on commit 9c782a7

Please sign in to comment.