-
Notifications
You must be signed in to change notification settings - Fork 28
/
auth.go
executable file
·43 lines (36 loc) · 1.29 KB
/
auth.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
package main
import (
"fmt"
"log"
"golang.org/x/crypto/ssh"
ldap "github.com/tonnerre/go-ldap"
)
func AuthUserPass(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
perm := &ssh.Permissions{
Extensions: map[string]string{
"authType": "password",
"password": string(password),
},
}
if _, ok := config.Users[conn.User()]; ! ok {
return nil, fmt.Errorf("User Doesn't Exist in Config")
}
if string(password) == "" {
// Blank password isn't handled properly by LDAP library, fail here.
return nil, fmt.Errorf("Blank Password Not Allowed")
}
if config.Global.AuthType == "ad" {
l, err := ldap.Dial("tcp", config.Global.LDAP_Server)
if err != nil {
log.Printf("LDAP Connect Failed: %s", err)
return nil, fmt.Errorf("LDAP Connect Failed: %s", err)
}
if err := l.Bind(fmt.Sprintf("%s@%s", conn.User(), config.Global.LDAP_Domain), string(password)); err != nil {
log.Printf("LDAP Bind Failed: %s", err)
return nil, fmt.Errorf("LDAP Bind Failed: %s", err)
}
return perm, nil
} else {
return nil, fmt.Errorf("No Valid Auth Types")
}
}