-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpam.go
More file actions
126 lines (115 loc) · 3.41 KB
/
Copy pathpam.go
File metadata and controls
126 lines (115 loc) · 3.41 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
package main
/*
#cgo LDFLAGS: -lpam -fPIC
#define PAM_SM_AUTH
#define PAM_SM_SESSION
#include <security/pam_modules.h>
#include <stdlib.h>
#include <string.h>
char *string_from_argv(int i, char **argv);
char *get_username(pam_handle_t *pamh);
char *get_password(pam_handle_t *pamh);
*/
import "C"
import (
"unsafe"
"strings"
"github.com/op/go-logging"
)
//export pamAuthenticate
func pamAuthenticate( pamh *C.pam_handle_t, flags C.int, argc C.int, argv **C.char ) C.int {
username := getPamUsername( pamh )
if username == "" {
return C.PAM_USER_UNKNOWN
}
_password := C.get_password( pamh )
if _password == nil {
return C.PAM_ABORT
}
defer C.free( unsafe.Pointer( _password ) )
password := C.GoString(_password);
if password == "" {
// No ohmage user can have an empty password!
return C.PAM_USER_UNKNOWN
}
cli_params := mapFromArgv( argc, argv )
ohmage_url, err := parseOhmageUrl( cli_params[ "url" ] )
if err != nil {
return C.PAM_ABORT
}
if cli_params[ "debug" ] == "true" {
logging.SetLevel( logging.DEBUG, "pam_ohmage" )
}
test_class := ""
if cli_params[ "test_class_participation" ] != "" {
test_class = cli_params[ "test_class_participation" ]
}
authenticated, err := isUserAuthenticated( ohmage_url, username, password, test_class )
if err != nil {
log.Error( "userame:", username, err )
return C.PAM_AUTH_ERR
} else if authenticated {
// RStudio expects the local account to be present after authentication succeeds.
// This is a mechanism to get around this assumption: We call the open session
// module from within this module before returning.
// The open session module is still needed for RStudio PAM sessions
log.Debug( "Calling open_session module" )
user_account_ready, err := isUserAccountReady( username )
if err != nil {
log.Error( err )
return C.PAM_ABORT
} else if user_account_ready {
return C.PAM_SUCCESS
} else {
return C.PAM_ABORT
}
} else {
return C.PAM_AUTH_ERR
}
}
//export pamOpenSession
func pamOpenSession( pamh *C.pam_handle_t, flags C.int, argc C.int, argv **C.char ) C.int {
cli_params := mapFromArgv( argc, argv )
if cli_params[ "debug" ] == "true" {
logging.SetLevel( logging.DEBUG, "pam_ohmage" )
}
username := getPamUsername( pamh )
if username != "" {
user_account_ready, err := isUserAccountReady( username )
if err != nil {
log.Error( "username:", username, err )
return C.PAM_SESSION_ERR
} else if user_account_ready {
return C.PAM_SUCCESS
}
}
return C.PAM_SESSION_ERR
}
func getPamUsername( pamh *C.pam_handle_t ) string {
_username := C.get_username( pamh )
if _username == nil {
return ""
}
defer C.free( unsafe.Pointer( _username ) )
username := C.GoString(_username);
return username
}
func mapFromArgv( argc C.int, argv **C.char ) map[string]string {
parameters := make( []string, 0, argc )
for i := 0; i < int( argc ); i++ {
str := C.string_from_argv( C.int( i ), argv )
defer C.free( unsafe.Pointer( str ) )
parameters = append( parameters, C.GoString( str ) )
}
result := make( map[string]string )
if len( parameters ) > 0 {
for _ , _parameter := range parameters {
parameter := strings.Split( _parameter, "=" )
if len( parameter ) > 1 && parameter[ 0 ] != "" {
result[ parameter[ 0 ] ] = parameter[ 1 ]
}
}
}
return result
}
func main( ) { }