-
Notifications
You must be signed in to change notification settings - Fork 2
/
ergo-ldap.go
103 lines (88 loc) · 2.12 KB
/
ergo-ldap.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
// Copyright (c) Shivaram Lingamneni <slingamn@cs.stanford.edu>
// Released under the Apache 2.0 license
package main
import (
"bufio"
"encoding/json"
"errors"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
"github.com/ergochat/ergo-ldap/ldap"
)
var (
ErrNoConfigFile = errors.New("no config file supplied")
)
// JSON-serializable input and output types for the script
type AuthScriptInput struct {
AccountName string `json:"accountName,omitempty"`
Passphrase string `json:"passphrase,omitempty"`
Certfp string `json:"certfp,omitempty"`
IP string `json:"ip,omitempty"`
}
type AuthScriptOutput struct {
AccountName string `json:"accountName"`
Success bool `json:"success"`
Error string `json:"error"`
}
func LoadRawConfig(filename string) (config ldap.ServerConfig, err error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return
}
return
}
func run() (success bool, err error) {
if len(os.Args) < 2 {
return false, ErrNoConfigFile
}
config, err := LoadRawConfig(os.Args[1])
if err != nil {
return
}
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadBytes('\n')
if err != nil {
return
}
var input AuthScriptInput
err = json.Unmarshal(line, &input)
if err != nil {
return
}
// sanity check; for certfp-based auth, these will be empty,
// don't let the check pass even if the LDAP server is weirdly misconfigured
if input.AccountName == "" || input.Passphrase == "" {
return false, nil
}
ldapErr := ldap.CheckLDAPPassphrase(config, input.AccountName, input.Passphrase)
switch ldapErr {
case nil:
// success
return true, nil
case ldap.ErrCouldNotFindUser, ldap.ErrInvalidCredentials, ldap.ErrUserNotInRequiredGroup:
// auth failed, but not an error
return false, nil
default:
return false, ldapErr
}
}
func main() {
var output AuthScriptOutput
success, err := run()
if success && err == nil {
output.Success = true
} else if err != nil {
output.Error = err.Error()
}
out, err := json.Marshal(output)
if err != nil {
panic(err)
}
out = append(out, '\n')
os.Stdout.Write(out)
}