2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-05-16 19:07:01 +00:00
|
|
|
"context"
|
2021-07-13 16:24:18 +00:00
|
|
|
"crypto/tls"
|
2021-04-26 09:53:06 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-07-20 09:03:09 +00:00
|
|
|
"net"
|
2021-04-26 09:53:06 +00:00
|
|
|
"strings"
|
2021-05-08 18:59:31 +00:00
|
|
|
"sync"
|
2021-04-26 09:53:06 +00:00
|
|
|
|
2021-05-04 22:03:19 +00:00
|
|
|
"github.com/go-openapi/strfmt"
|
2021-07-20 09:03:09 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
2021-04-19 22:30:27 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-09-09 13:52:24 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/metrics"
|
2021-04-19 22:30:27 +00:00
|
|
|
)
|
|
|
|
|
2021-08-21 15:53:09 +00:00
|
|
|
const (
|
|
|
|
UsersOU = "users"
|
|
|
|
GroupsOU = "groups"
|
|
|
|
VirtualGroupsOU = "virtual-groups"
|
|
|
|
)
|
|
|
|
|
2021-04-19 22:30:27 +00:00
|
|
|
func (ls *LDAPServer) Refresh() error {
|
2021-05-16 19:35:23 +00:00
|
|
|
outposts, _, err := ls.ac.Client.OutpostsApi.OutpostsLdapList(context.Background()).Execute()
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
if len(outposts.Results) < 1 {
|
2021-04-26 09:53:06 +00:00
|
|
|
return errors.New("no ldap provider defined")
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
providers := make([]*ProviderInstance, len(outposts.Results))
|
|
|
|
for idx, provider := range outposts.Results {
|
2021-08-21 15:53:09 +00:00
|
|
|
userDN := strings.ToLower(fmt.Sprintf("ou=%s,%s", UsersOU, *provider.BaseDn))
|
|
|
|
groupDN := strings.ToLower(fmt.Sprintf("ou=%s,%s", GroupsOU, *provider.BaseDn))
|
|
|
|
virtualGroupDN := strings.ToLower(fmt.Sprintf("ou=%s,%s", VirtualGroupsOU, *provider.BaseDn))
|
2021-07-13 16:24:18 +00:00
|
|
|
logger := log.WithField("logger", "authentik.outpost.ldap").WithField("provider", provider.Name)
|
2021-04-26 09:53:06 +00:00
|
|
|
providers[idx] = &ProviderInstance{
|
2021-05-16 19:07:01 +00:00
|
|
|
BaseDN: *provider.BaseDn,
|
2021-08-21 15:53:09 +00:00
|
|
|
VirtualGroupDN: virtualGroupDN,
|
2021-05-04 22:03:19 +00:00
|
|
|
GroupDN: groupDN,
|
|
|
|
UserDN: userDN,
|
2021-05-16 19:07:01 +00:00
|
|
|
appSlug: provider.ApplicationSlug,
|
|
|
|
flowSlug: provider.BindFlowSlug,
|
|
|
|
searchAllowedGroups: []*strfmt.UUID{(*strfmt.UUID)(provider.SearchGroup.Get())},
|
2021-05-08 18:59:31 +00:00
|
|
|
boundUsersMutex: sync.RWMutex{},
|
|
|
|
boundUsers: make(map[string]UserFlags),
|
2021-05-04 22:03:19 +00:00
|
|
|
s: ls,
|
2021-07-17 19:24:11 +00:00
|
|
|
log: logger,
|
2021-07-13 16:24:18 +00:00
|
|
|
tlsServerName: provider.TlsServerName,
|
2021-07-14 18:37:27 +00:00
|
|
|
uidStartNumber: *provider.UidStartNumber,
|
|
|
|
gidStartNumber: *provider.GidStartNumber,
|
2021-09-16 08:03:31 +00:00
|
|
|
outpostName: ls.ac.Outpost.Name,
|
2021-07-13 16:24:18 +00:00
|
|
|
}
|
|
|
|
if provider.Certificate.Get() != nil {
|
2021-07-21 21:53:43 +00:00
|
|
|
kp := provider.Certificate.Get()
|
|
|
|
err := ls.cs.AddKeypair(*kp)
|
2021-07-13 16:24:18 +00:00
|
|
|
if err != nil {
|
2021-07-21 21:53:43 +00:00
|
|
|
ls.log.WithError(err).Warning("Failed to initially fetch certificate")
|
2021-07-13 16:24:18 +00:00
|
|
|
}
|
2021-07-21 21:53:43 +00:00
|
|
|
providers[idx].cert = ls.cs.Get(*kp)
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ls.providers = providers
|
|
|
|
ls.log.Info("Update providers")
|
2021-04-19 22:30:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 21:07:13 +00:00
|
|
|
func (ls *LDAPServer) StartLDAPServer() error {
|
2021-04-26 12:46:29 +00:00
|
|
|
listen := "0.0.0.0:3389"
|
2021-07-20 09:03:09 +00:00
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", listen)
|
|
|
|
if err != nil {
|
|
|
|
ls.log.Fatalf("FATAL: listen (%s) failed - %s", listen, err)
|
|
|
|
}
|
|
|
|
proxyListener := &proxyproto.Listener{Listener: ln}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
|
2021-06-06 21:07:13 +00:00
|
|
|
ls.log.WithField("listen", listen).Info("Starting ldap server")
|
2021-07-20 09:03:09 +00:00
|
|
|
err = ls.s.Serve(proxyListener)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ls.log.Printf("closing %s", ln.Addr())
|
2021-06-06 21:07:13 +00:00
|
|
|
return ls.s.ListenAndServe(listen)
|
|
|
|
}
|
|
|
|
|
2021-07-13 16:24:18 +00:00
|
|
|
func (ls *LDAPServer) StartLDAPTLSServer() error {
|
|
|
|
listen := "0.0.0.0:6636"
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
MaxVersion: tls.VersionTLS12,
|
|
|
|
GetCertificate: ls.getCertificates,
|
|
|
|
}
|
|
|
|
|
2021-07-20 13:25:11 +00:00
|
|
|
ln, err := net.Listen("tcp", listen)
|
2021-07-13 16:24:18 +00:00
|
|
|
if err != nil {
|
|
|
|
ls.log.Fatalf("FATAL: listen (%s) failed - %s", listen, err)
|
|
|
|
}
|
2021-07-20 13:25:11 +00:00
|
|
|
|
2021-07-20 09:03:09 +00:00
|
|
|
proxyListener := &proxyproto.Listener{Listener: ln}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
|
2021-07-20 13:25:11 +00:00
|
|
|
tln := tls.NewListener(proxyListener, tlsConfig)
|
|
|
|
|
2021-07-13 16:24:18 +00:00
|
|
|
ls.log.WithField("listen", listen).Info("Starting ldap tls server")
|
2021-07-20 13:25:11 +00:00
|
|
|
err = ls.s.Serve(tln)
|
2021-07-13 16:24:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ls.log.Printf("closing %s", ln.Addr())
|
|
|
|
return ls.s.ListenAndServe(listen)
|
|
|
|
}
|
|
|
|
|
2021-06-06 21:07:13 +00:00
|
|
|
func (ls *LDAPServer) Start() error {
|
|
|
|
wg := sync.WaitGroup{}
|
2021-07-13 16:24:18 +00:00
|
|
|
wg.Add(3)
|
2021-06-06 21:07:13 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RunServer()
|
2021-06-06 21:07:13 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
err := ls.StartLDAPServer()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-07-13 16:24:18 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
err := ls.StartLDAPTLSServer()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-06-06 21:07:13 +00:00
|
|
|
wg.Wait()
|
2021-04-19 22:30:27 +00:00
|
|
|
return nil
|
|
|
|
}
|