Skip to content

Commit a9da665

Browse files
committedJul 12, 2016
refactor!
1 parent 3d5086f commit a9da665

5 files changed

+127
-55
lines changed
 

‎binding.cc

+15-54
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,33 @@
1-
#include <sys/types.h>
2-
#include <sys/socket.h>
3-
#if __APPLE__ || __FreeBSD__
4-
#include <sys/ucred.h>
5-
#include <sys/un.h>
6-
#include <sys/proc_info.h> // for struct proc_bsdshortinfo
7-
#include <libproc.h> // for proc_pidinfo()
8-
#endif
1+
#include "credentials.hh"
92
#include <nan.h>
103

11-
using v8::FunctionTemplate;
12-
134
NAN_METHOD(FromFd) {
145
auto fd = Nan::To<int>(info[0]).FromJust();
15-
16-
176
auto ret = Nan::New<v8::Object>();
187
info.GetReturnValue().Set(ret);
19-
#define SET_RETVAL(key, value) Nan::Set(ret, Nan::New(#key).ToLocalChecked(), Nan::New(value))
20-
21-
22-
#if defined(SO_PEERCRED)
23-
struct ucred creds;
24-
socklen_t creds_len = sizeof creds;
25-
int fail = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &creds, &creds_len);
26-
#elif defined(LOCAL_PEERCRED) && defined(LOCAL_PEERPID)
27-
#define USE_PROC
28-
struct proc_bsdshortinfo proc;
8+
#define SET_RETVAL(key, value) Nan::Set(ret, Nan::New(key).ToLocalChecked(), Nan::New(value))
299

30-
struct {
31-
pid_t pid;
32-
uid_t uid;
33-
} creds;
10+
Credentials creds;
11+
if (!creds.Init(fd)) {
12+
SET_RETVAL("errno", errno);
13+
return;
14+
}
3415

35-
struct xucred xcreds;
36-
socklen_t xcreds_len = sizeof xcreds;
37-
int fail = getsockopt(fd, SOL_LOCAL, LOCAL_PEERCRED, &xcreds, &xcreds_len);
38-
if (!fail) {
39-
assert(xcreds.cr_version == XUCRED_VERSION);
40-
creds.uid = xcreds.cr_uid;
16+
SET_RETVAL("pid", creds.GetPid());
17+
SET_RETVAL("uid", creds.GetUid());
18+
SET_RETVAL("gid", creds.GetGid());
4119

42-
socklen_t pid_len = sizeof creds.pid;
43-
fail = getsockopt(fd, SOL_LOCAL, LOCAL_PEERPID, &creds.pid, &pid_len);
44-
}
45-
if (!fail) {
46-
fail = proc_pidinfo(creds.pid, PROC_PIDT_SHORTBSDINFO, 1, &proc, PROC_PIDT_SHORTBSDINFO_SIZE) == 0;
47-
}
48-
#else
49-
#error "unsupported platform"
20+
#ifdef CREDENTIALS_HAS_PPID
21+
SET_RETVAL("ppid", creds.GetPpid());
5022
#endif
51-
52-
if (fail) {
53-
SET_RETVAL(errno, errno);
54-
} else {
55-
SET_RETVAL(pid, creds.pid);
56-
SET_RETVAL(uid, creds.uid);
57-
#ifdef USE_PROC
58-
SET_RETVAL(ppid, proc.pbsi_ppid);
59-
SET_RETVAL(pgid, proc.pbsi_pgid);
60-
SET_RETVAL(gid, proc.pbsi_gid);
61-
#else
62-
SET_RETVAL(gid, creds.gid);
23+
#ifdef CREDENTIALS_HAS_PGID
24+
SET_RETVAL("pgid", creds.GetPgid());
6325
#endif
64-
}
6526
}
6627

6728
NAN_MODULE_INIT(Init) {
6829
Nan::Set(target, Nan::New("fromFd").ToLocalChecked(),
69-
Nan::GetFunction(Nan::New<FunctionTemplate>(FromFd)).ToLocalChecked());
30+
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(FromFd)).ToLocalChecked());
7031
}
7132

7233
NODE_MODULE(NativeExtension, Init)

‎binding.gyp

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
"targets": [
44
{
55
"target_name": "binding",
6-
"sources": [ "binding.cc" ],
76
"include_dirs": [
87
"<!(node -e \"require('nan')\")"
98
],
9+
"sources": [
10+
"binding.cc",
11+
"credentials_linux.cc",
12+
"credentials_osx.cc"
13+
],
14+
"conditions": [
15+
[ 'OS != "linux"', {
16+
"sources!": [
17+
"credentials_linux.cc",
18+
]
19+
} ],
20+
[ 'OS != "mac"', {
21+
"sources!": [
22+
"credentials_osx.cc",
23+
]
24+
} ],
25+
]
1026
}
1127
],
1228
}

‎credentials.hh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <sys/types.h>
2+
#include <sys/socket.h>
3+
#if __APPLE__
4+
#include <sys/ucred.h>
5+
#include <sys/un.h>
6+
#include <sys/proc_info.h>
7+
#include <libproc.h>
8+
#define CREDENTIALS_HAS_PPID
9+
#define CREDENTIALS_HAS_PGID
10+
#elif __linux__
11+
// no extra headers
12+
#else
13+
#error "Unsupported platform"
14+
#endif
15+
16+
class Credentials {
17+
#ifdef __APPLE__
18+
struct proc_bsdshortinfo proc;
19+
#elif __linux__
20+
struct ucred ucred;
21+
#endif
22+
23+
public:
24+
Credentials() {}
25+
auto Init(int fd) -> bool;
26+
27+
auto GetPid() -> pid_t;
28+
#ifdef CREDENTIALS_HAS_PPID
29+
auto GetPpid() -> pid_t;
30+
#endif
31+
#ifdef CREDENTIALS_HAS_PGID
32+
auto GetPgid() -> pid_t;
33+
#endif
34+
35+
auto GetUid() -> uid_t;
36+
auto GetGid() -> gid_t;
37+
};

‎credentials_linux.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "credentials.hh"
2+
#include <cassert>
3+
4+
auto Credentials::Init(int fd) -> bool {
5+
socklen_t ucred_len = sizeof ucred;
6+
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len)) {
7+
return false;
8+
}
9+
assert(ucred_len == sizeof ucred);
10+
return true;
11+
}
12+
13+
auto Credentials::GetPid() -> pid_t {
14+
return ucred.pid;
15+
}
16+
17+
auto Credentials::GetUid() -> uid_t {
18+
return ucred.uid;
19+
}
20+
21+
auto Credentials::GetGid() -> gid_t {
22+
return ucred.uid;
23+
}

‎credentials_osx.cc

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "credentials.hh"
2+
#include <cassert>
3+
4+
auto Credentials::Init(int fd) -> bool {
5+
auto pid = (pid_t) -1;
6+
socklen_t pid_len = sizeof pid;
7+
if (getsockopt(fd, SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_len)) {
8+
return false;
9+
}
10+
assert(pid_len == sizeof pid);
11+
if (proc_pidinfo(pid, PROC_PIDT_SHORTBSDINFO, 1, &proc, PROC_PIDT_SHORTBSDINFO_SIZE) == 0) {
12+
return false;
13+
}
14+
return true;
15+
}
16+
17+
auto Credentials::GetPid() -> pid_t {
18+
return (pid_t) proc.pbsi_pid;
19+
}
20+
21+
auto Credentials::GetPpid() -> pid_t {
22+
return (pid_t) proc.pbsi_ppid;
23+
}
24+
25+
auto Credentials::GetPgid() -> pid_t {
26+
return (pid_t) proc.pbsi_pgid;
27+
}
28+
29+
auto Credentials::GetUid() -> uid_t {
30+
return proc.pbsi_uid;
31+
}
32+
33+
auto Credentials::GetGid() -> gid_t {
34+
return proc.pbsi_gid;
35+
}

0 commit comments

Comments
 (0)
Please sign in to comment.