-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam.go
146 lines (126 loc) · 3.06 KB
/
pam.go
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
package main
/*
#cgo LDFLAGS: -lpam -fPIC
#include <security/pam_appl.h>
#include <stdlib.h>
char *string_from_argv(int, char**);
char *get_host(pam_handle_t *pamh);
char *get_user(pam_handle_t *pamh);
int get_uid(char *user);
*/
import "C"
import (
"fmt"
"os"
"os/user"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"unsafe"
)
var (
serverURL = "https://api.werbot.com"
serviceID = "bef57548-dfdd-4aba-a545-50aa9e4f50db"
serviceKey = "7ba94fcbac95b794fc6efc25e1c23f0d6b"
offlineUsers = "ubuntu"
debug = true
matchTfa = regexp.MustCompile(`^[0-9]{6}$`)
)
//export pam_sm_authenticate
func pam_sm_authenticate(pamh *C.pam_handle_t, flags, argc C.int, argv **C.char) C.int {
cUsername := C.get_user(pamh)
if cUsername == nil {
return C.PAM_USER_UNKNOWN
}
defer C.free(unsafe.Pointer(cUsername))
uid := int(C.get_uid(cUsername))
if uid < 0 {
return C.PAM_USER_UNKNOWN
}
return pamAuthenticate(pamh, uid, C.GoString(cUsername), sliceFromArgv(argc, argv))
}
//export pam_sm_setcred
func pam_sm_setcred(pamh *C.pam_handle_t, flags, argc C.int, argv **C.char) C.int {
return C.PAM_IGNORE
}
func pamAuthenticate(pamh *C.pam_handle_t, uid int, username string, argv []string) C.int {
runtime.GOMAXPROCS(1)
for _, arg := range argv {
opt := strings.SplitN(arg, "=", 2)
switch opt[0] {
case "server_url":
serverURL = opt[1]
case "service_id":
serviceID = opt[1]
case "service_key":
serviceKey = opt[1]
case "ofline_users":
offlineUsers = opt[1]
case "debug":
t, _ := strconv.ParseBool(opt[1])
debug = t
}
}
// get remote user ip address
host := getUserHost(pamh)
if debug {
writeLog(fmt.Sprintf("host result: %s", host))
}
// get tfa code
tfaVal, err := conversation(pamh, "Your werbot code: ")
if err != nil || !matchTfa.MatchString(tfaVal) {
return C.PAM_AUTH_ERR
}
if debug {
writeLog(fmt.Sprintf("tfaVal result: %s", tfaVal))
}
//
origEUID := os.Geteuid()
if os.Getuid() != origEUID || origEUID == 0 {
if !seteuid(uid) {
if debug {
writeLog(fmt.Sprintf("error dropping privs from %d to %d", origEUID, uid))
}
return C.PAM_AUTH_ERR
}
defer func() {
if !seteuid(origEUID) {
if debug {
writeLog(fmt.Sprintf("error resetting uid to %d", origEUID))
}
}
}()
}
user, err := user.LookupId(strconv.Itoa(uid))
if err != nil {
if debug {
writeLog(fmt.Sprintf("error looking for user %d", uid))
}
return C.PAM_AUTH_ERR
}
// call tfaReuqest flow here
tfaResp := sendTfaReq(user.Username, tfaVal, host)
if debug {
writeLog(fmt.Sprintf("final result: %v", tfaResp))
}
if tfaResp == true {
return C.PAM_SUCCESS
}
return C.PAM_AUTH_ERR
}
// writeLog is error or info log writer for wpam. writes log in /var/log/wpam.log
func writeLog(errval string) {
logPath := "/var/log/wpam.log"
file, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
fmt.Println(err)
}
defer file.Close()
var buf []byte
data := fmt.Sprintf("%s - %s\n", time.Now().String(), errval)
buf = append(buf, []byte(data)...)
_, err = file.Write(buf)
}
func main() {}