2021-11-05 09:37:30 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/pires/go-proxyproto"
|
2022-08-03 19:33:27 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2021-11-05 09:37:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ls *LDAPServer) getCertificates(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
if len(ls.providers) == 1 {
|
|
|
|
if ls.providers[0].cert != nil {
|
|
|
|
ls.log.WithField("server-name", info.ServerName).Debug("We only have a single provider, using their cert")
|
|
|
|
return ls.providers[0].cert, nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-28 18:15:29 +00:00
|
|
|
allIdenticalCerts := true
|
2021-11-05 09:37:30 +00:00
|
|
|
for _, provider := range ls.providers {
|
|
|
|
if provider.tlsServerName == &info.ServerName {
|
|
|
|
if provider.cert == nil {
|
|
|
|
ls.log.WithField("server-name", info.ServerName).Debug("Handler does not have a certificate")
|
|
|
|
return ls.defaultCert, nil
|
|
|
|
}
|
|
|
|
return provider.cert, nil
|
|
|
|
}
|
2022-12-28 20:29:36 +00:00
|
|
|
if provider.certUUID != ls.providers[0].certUUID || provider.cert == nil {
|
2022-12-28 18:15:29 +00:00
|
|
|
allIdenticalCerts = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if allIdenticalCerts {
|
|
|
|
ls.log.WithField("server-name", info.ServerName).Debug("all providers have the same keypair, using keypair")
|
|
|
|
return ls.providers[0].cert, nil
|
2021-11-05 09:37:30 +00:00
|
|
|
}
|
|
|
|
ls.log.WithField("server-name", info.ServerName).Debug("Fallback to default cert")
|
|
|
|
return ls.defaultCert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LDAPServer) StartLDAPTLSServer() error {
|
2022-08-03 19:33:27 +00:00
|
|
|
listen := config.Get().Listen.LDAPS
|
2021-11-05 09:37:30 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
MaxVersion: tls.VersionTLS12,
|
|
|
|
GetCertificate: ls.getCertificates,
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", listen)
|
|
|
|
if err != nil {
|
2021-12-11 21:09:18 +00:00
|
|
|
ls.log.WithField("listen", listen).WithError(err).Fatalf("listen failed")
|
2021-11-05 09:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxyListener := &proxyproto.Listener{Listener: ln}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
|
|
|
|
tln := tls.NewListener(proxyListener, tlsConfig)
|
|
|
|
|
2021-12-20 20:46:01 +00:00
|
|
|
ls.log.WithField("listen", listen).Info("Starting LDAP SSL server")
|
2021-11-05 09:37:30 +00:00
|
|
|
err = ls.s.Serve(tln)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-22 09:33:21 +00:00
|
|
|
ls.log.WithField("listen", listen).Info("Stopping LDAP SSL Server")
|
|
|
|
return nil
|
2021-11-05 09:37:30 +00:00
|
|
|
}
|