Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LDAP support #779

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 81 additions & 1 deletion irc/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package irc

import (
"crypto/tls"
"encoding/json"
"fmt"
"net/smtp"
Expand All @@ -14,6 +15,7 @@ import (
"time"
"unicode"

"github.com/go-ldap/ldap"
"github.com/oragono/oragono/irc/caps"
"github.com/oragono/oragono/irc/passwd"
"github.com/oragono/oragono/irc/utils"
Expand Down Expand Up @@ -828,8 +830,86 @@ func (am *AccountManager) checkPassphrase(accountName, passphrase string) (accou
return
}

func (am *AccountManager) checkLDAPPassphrase(accountName, passphrase string) (account ClientAccount, err error) {
var (
host, url string
port int
sr *ldap.SearchResult
l *ldap.Conn
)

host = am.server.AccountConfig().LDAP.Servers.Host
port = am.server.AccountConfig().LDAP.Servers.Port

account, err = am.LoadAccount(accountName)
if err != nil {
return
}

if !account.Verified {
err = errAccountUnverified
return
}

if am.server.AccountConfig().LDAP.Servers.UseSSL {
url = fmt.Sprintf("ldaps://%s:%d", host, port)
} else {
url = fmt.Sprintf("ldap://%s:%d", host, port)
}

l, err = ldap.DialURL(url)
if err != nil {
return
}
defer l.Close()

if am.server.AccountConfig().LDAP.Servers.StartTLS {
err = l.StartTLS(&tls.Config{InsecureSkipVerify: am.server.AccountConfig().LDAP.Servers.SkipTLSVerify})
if err != nil {
return
}
}

err = l.Bind(am.server.AccountConfig().LDAP.BindDN, am.server.AccountConfig().LDAP.BindPwd)
if err != nil {
return
}

for _, baseDN := range am.server.AccountConfig().LDAP.SearchBaseDNs {
req := ldap.NewSearchRequest(baseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, am.server.AccountConfig().LDAP.Timeout, false, fmt.Sprintf("(&(objectClass=organizationalPerson)(uid=%s))", accountName), []string{"dn"}, nil)
sr, err = l.Search(req)
if err != nil {
return
}

userdn := sr.Entries[0].DN

if len(sr.Entries) > 0 {
// verify the user passphrase
err = l.Bind(userdn, passphrase)
if err != nil {
continue
}
break
}
}

return
}

func (am *AccountManager) AuthenticateByPassphrase(client *Client, accountName string, passphrase string) error {
account, err := am.checkPassphrase(accountName, passphrase)
var account ClientAccount
var err error

if am.server.AccountConfig().LDAP.Enabled {
account, err = am.checkLDAPPassphrase(accountName, passphrase)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a return statement on success

if err == nil {
am.Login(client, account)
return nil
}
}

account, err = am.checkPassphrase(accountName, passphrase)
if err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions irc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type AccountConfig struct {
Exempted []string
exemptedNets []net.IPNet
} `yaml:"require-sasl"`
LDAP LDAPConfig
LoginThrottling struct {
Enabled bool
Duration time.Duration
Expand All @@ -82,6 +83,28 @@ type AccountConfig struct {
VHosts VHostConfig
}

type LDAPConfig struct {
Timeout int
Enabled bool
AllowSignup bool `yaml:"allow-signup"`
BindDN string `yaml:"bind-dn"`
BindPwd string `yaml:"bind-password"`
SearchFilter string `yaml:"search-filter"`
SearchBaseDNs []string `yaml:"search-base-dns"`
Attributes map[string]string
Servers LDAPServerConfig
}

type LDAPServerConfig struct {
Host string
Port int
UseSSL bool `yaml:"use-ssl"`
StartTLS bool `yaml:"start-tls"`
SkipTLSVerify bool `yaml:"skip-tls-verify"`
ClientCert string `yaml:"client-cert"`
ClientKey string `yaml:"client-key"`
}

// AccountRegistrationConfig controls account registration.
type AccountRegistrationConfig struct {
Enabled bool
Expand Down