-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLdap2Pop3.go
186 lines (150 loc) · 4.42 KB
/
Ldap2Pop3.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
180
181
182
183
184
185
186
// Ldap2Pop3
package main
import (
"errors"
// "fmt"
"encoding/json"
pop3 "github.com/bytbox/go-pop3"
message "github.com/vjeantet/goldap/message"
ldap "github.com/vjeantet/ldapserver"
"log"
"os"
"os/signal"
"regexp"
"syscall"
)
func GetNameFromFilter(fstr string) (string, error) {
reg := regexp.MustCompile("(?:\\(uid=){1}(.+?)(?:\\)){1}")
result := reg.FindAllStringSubmatch(fstr, -1)
if len(result) == 0 {
return "", errors.New("Not find uid")
}
return result[0][1], nil
}
var c Config
func main() {
//read config
r, err := os.Open("conf.json")
if err != nil {
log.Fatalln(err)
}
decoder := json.NewDecoder(r)
err = decoder.Decode(&c)
if err != nil {
log.Fatalln(err)
}
//Create a new LDAP Server
server := ldap.NewServer()
routes := ldap.NewRouteMux()
routes.NotFound(handleNotFound)
routes.Abandon(handleAbandon)
routes.Bind(handleBind)
// routes.Search(handleSearchDSE).
// BaseDn("").
// Scope(ldap.SearchRequestScopeBaseObject).
// Filter("(objectclass=*)").
// Label("Search - ROOT DSE")
// routes.Search(handleSearchMyCompany).
// BaseDn("o=My Company, c=US").
// Scope(ldap.SearchRequestScopeBaseObject).
// Label("Search - Compagny Root")
routes.Search(handleSearch).Label("Search - Generic")
//Attach routes to server
server.Handle(routes)
// listen on 10389 and serve
go server.ListenAndServe(c.ListenAddr)
// When CTRL+C, SIGINT and SIGTERM signal occurs
// Then stop server gracefully
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
close(ch)
server.Stop()
}
func handleNotFound(w ldap.ResponseWriter, r *ldap.Message) {
switch r.ProtocolOpType() {
case ldap.ApplicationBindRequest:
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
res.SetDiagnosticMessage("Default binding behavior set to return Success")
w.Write(res)
default:
res := ldap.NewResponse(ldap.LDAPResultUnwillingToPerform)
res.SetDiagnosticMessage("Operation not implemented by server")
w.Write(res)
}
}
func invokePop3Auth(userName string, pwd string) error {
addr := c.Pop3ServerTlsAddr
c, err := pop3.DialTLS(addr)
if err != nil {
return errors.New("NewClient failed:" + err.Error())
}
if err = c.Auth(userName, pwd); err != nil {
return errors.New("Auth failed" + err.Error())
}
c.Noop()
c.Quit()
return nil
}
func handleBind(w ldap.ResponseWriter, m *ldap.Message) {
r := m.GetBindRequest()
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
if r.AuthenticationChoice() == "simple" {
uName := r.Name()
log.Println("uName:", uName)
if string(uName) == c.LookupUser {
w.Write(res)
return
}
pwd, ok := r.Authentication().(message.OCTETSTRING)
if !ok {
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
res.SetDiagnosticMessage("invalid credentials")
} else {
err := invokePop3Auth(string(uName), pwd.String())
if err != nil {
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
res.SetDiagnosticMessage("invalid credentials," + err.Error())
log.Println("invalid credentials," + err.Error())
}
}
//log.Printf("Bind failed User=%s, Pass=%#v", uName, r.Authentication())
} else {
res.SetResultCode(ldap.LDAPResultUnwillingToPerform)
res.SetDiagnosticMessage("Authentication choice not supported")
}
w.Write(res)
}
func handleAbandon(w ldap.ResponseWriter, m *ldap.Message) {
var req = m.GetAbandonRequest()
// retreive the request to abandon, and send a abort signal to it
if requestToAbandon, ok := m.Client.GetMessageByID(int(req)); ok {
requestToAbandon.Abandon()
log.Printf("Abandon signal sent to request processor [messageID=%d]", int(req))
}
}
func handleSearch(w ldap.ResponseWriter, m *ldap.Message) {
r := m.GetSearchRequest()
log.Printf("Request BaseDn=%s", r.BaseObject())
log.Printf("Request Filter=%s", r.Filter())
log.Printf("Request FilterString=%s", r.FilterString())
log.Printf("Request Attributes=%s", r.Attributes())
log.Printf("Request TimeLimit=%d", r.TimeLimit().Int())
// Handle Stop Signal (server stop / client disconnected / Abandoned request....)
select {
case <-m.Done:
log.Print("Leaving handleSearch...")
return
default:
}
uName, err := GetNameFromFilter(r.FilterString())
if err == nil {
log.Println("search uName done:", uName)
e := ldap.NewSearchResultEntry(uName)
e.AddAttribute("mail", "")
e.AddAttribute("cn", message.AttributeValue(uName))
w.Write(e)
}
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
w.Write(res)
}