-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (143 loc) · 3.33 KB
/
main.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
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
package main
import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/zro/pam"
ldap "gopkg.in/ldap.v2"
yaml "gopkg.in/yaml.v2"
)
var pl pamLdap
type pamLdap struct {
Insecure bool `yaml:"Insecure"`
LoginAttr string `yaml:"LoginAttr"`
ObjectClass string `yaml:"ObjectClass"`
Remote string `yaml:"Remote"`
Port string `yaml:"Port"`
SearchBase string `yaml:"SearchBase"`
DirectBindAuth bool `yaml:"DirectBindAuth"`
BindDN string `yaml:"BindDN,omitempty"`
BindPW string `yaml:"BindPW,omitempty"`
}
func parseConfig(file string, pl *pamLdap) error {
f, err := ioutil.ReadFile(file)
if err != nil {
return err
}
err = yaml.Unmarshal(f, &pl)
if err != nil {
return err
}
return nil
}
func ldapAuth(pl *pamLdap, authuser, authtoken string) error {
remote := pl.Remote + ":" + pl.Port
tlsConfig := &tls.Config{
InsecureSkipVerify: pl.Insecure,
ServerName: pl.Remote,
}
l, err := ldap.DialTLS("tcp", remote, tlsConfig)
if err != nil {
return err
}
defer l.Close()
if pl.DirectBindAuth {
// this shortcut the search and try to bind authuser directly
user := fmt.Sprintf("%s=%s,%s", pl.LoginAttr, authuser, pl.SearchBase)
// bind auth user
err = l.Bind(user, authtoken)
} else {
// normal binding when DirectBindAuth is false
// bind search user -> search -> bind auth user
err = l.Bind(pl.BindDN, pl.BindPW)
if err != nil {
return err
}
filter := fmt.Sprintf("(%s(%s=%s))", pl.ObjectClass, pl.LoginAttr, authuser)
searchRequest := ldap.NewSearchRequest(
pl.SearchBase,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
filter,
[]string{"dn"},
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
return err
}
if len(sr.Entries) != 1 {
return errors.New("User does not exist or too many entries returned")
}
userdn := sr.Entries[0].DN
err = l.Bind(userdn, authtoken)
if err != nil {
return err
}
}
return err
}
func (pl *pamLdap) Authenticate(hdl pam.Handle, args pam.Args) pam.Value {
// default config file
config := "/etc/pam_ldap.yaml"
if val, ok := args["config"]; ok {
config = val
}
if _, err := os.Stat(config); os.IsNotExist(err) {
return pam.UserUnknown
}
err := parseConfig(config, pl)
if err != nil {
return pam.UserUnknown
}
authuser, err := hdl.GetUser()
if err != nil {
return pam.UserUnknown
}
authtoken, err := hdl.GetItem(pam.AuthToken)
if err != nil {
return pam.AuthTokenError
}
// if there is no socache for authtoken, retrive from conv
if len(authtoken) == 0 {
m := pam.Message{
Style: pam.MessageEchoOff,
Msg: "",
}
response, err := hdl.Conversation(m)
if err != nil {
return pam.AuthError
}
authtoken = response[0]
}
err = ldapAuth(pl, authuser, authtoken)
if err != nil {
return pam.UserUnknown
}
return pam.Success
}
func (pl *pamLdap) Validate(hdl pam.Handle, args pam.Args) pam.Value {
skipValidate := false
if _, ok := args["skipValidate"]; ok {
skipValidate = true
}
if skipValidate {
return pam.Success
}
return pam.PermissionDenied
}
func (pl *pamLdap) SetCredential(hdl pam.Handle, args pam.Args) pam.Value {
return pam.CredentialError
}
func init() {
pam.RegisterAuthHandler(&pl)
pam.RegisterAccountHandler(&pl)
}
func main() {
// needed in c-shared buildmode
}