-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpam.cc
More file actions
179 lines (119 loc) · 3.79 KB
/
pam.cc
File metadata and controls
179 lines (119 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <v8.h>
#include <node.h>
#include <string.h>
#include <stdlib.h>
#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) {
*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>";
}
extern "C" {
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 = (char *) 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 {
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();
}
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;
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;
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;
char *service = strdup(args->service);
char *username = strdup(args->username);
char *password = strdup(args->password);
int retval = _pam_authenticate(service, username, password);
if (retval == PAM_SUCCESS)
result = true;
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;
}
};
Persistent<FunctionTemplate> PAM::s_ct;
extern "C" {
static void init (Handle<Object> target) {
PAM::Init(target);
}
NODE_MODULE(pam, init);
}