This package is a work in progress.
ldapserver is a helper library for building server software capable of speaking the LDAP protocol. This could be an alternate implementation of LDAP, a custom LDAP proxy or even a completely different backend capable of "masquerading" its API as a LDAP Server.
The package supports
- All basic LDAP Operations (bind, search, add, compare, modify, delete, extended)
- SSL
- StartTLS
- Unbind request is implemented, but is handled internally to close the connection.
- Graceful stopping
- Basic request routing inspired by net/http ServeMux
- Logger customisation (log interface)
If you don't set a route to handle AbandonRequest, the package will handle it for you. (signal sent to message.Done chan)
When no route matches the request, the server will first try to call a special NotFound route, if nothing is specified, it will return an UnwillingToResponse Error code (53)
Feel free to contribute, comment :)
// Listen to 10389 port for LDAP Request// and route bind request to the handleBind funcpackage main
import (
"log""os""os/signal""syscall"
ldap "github.com/vjeantet/ldapserver"
)
funcmain() {
//ldap loggerldap.Logger=log.New(os.Stdout, "[server] ", log.LstdFlags)
//Create a new LDAP Serverserver:=ldap.NewServer()
routes:=ldap.NewRouteMux()
routes.Bind(handleBind)
server.Handle(routes)
// listen on 10389goserver.ListenAndServe("127.0.0.1:10389")
// When CTRL+C, SIGINT and SIGTERM signal occurs// Then stop server gracefullych:=make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-chclose(ch)
server.Stop()
}
// handleBind return Success if login == mysqlfunchandleBind(w ldap.ResponseWriter, m*ldap.Message) {
r:=m.GetBindRequest()
res:=ldap.NewBindResponse(ldap.LDAPResultSuccess)
ifstring(r.Name()) =="myLogin" {
w.Write(res)
return
}
log.Printf("Bind failed User=%s, Pass=%s", string(r.Name()), string(r.AuthenticationSimple()))
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
res.SetDiagnosticMessage("invalid credentials")
w.Write(res)
}Look into the "examples" folder