mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 12:28:07 -08:00
A signup made the account in the users file only. Now it makes the account in each backend of [general] auth that is set up to make accounts: the users file, SQL ([sql] insert_query), LDAP ([ldap] add_dn, added as add_bind_dn, the password set by Password Modify or unicodePwd for AD) and system accounts ([system] create_command, with a useradd sample script). [signup] backends limits the list. Before the account is made, each backend checks that the name is free. If one backend fails, the accounts already made are taken back (delete_query, entry delete, remove_command). With pending = yes, each backend must lock the account, or dmodem-login does not start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRRfuh3h6uNuHtaugux
299 lines
9.0 KiB
Go
299 lines
9.0 KiB
Go
// Copyright (C) 2026 D-Modem Authors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf16"
|
|
|
|
"github.com/D-Modem/login/internal/config"
|
|
"github.com/go-ldap/ldap/v3"
|
|
)
|
|
|
|
// LDAP: a bind as the user, to bind_dn with {user} in it, or to the
|
|
// DN that search_filter finds under search_base (searched as
|
|
// search_bind_dn, anonymously if none)
|
|
type LDAP struct {
|
|
url string
|
|
startTLS bool
|
|
tls *tls.Config
|
|
timeout time.Duration
|
|
bindDN string
|
|
base string
|
|
filter string
|
|
searchDN string
|
|
searchPass string
|
|
// groups for the items' groups = (group_base set): a group is an
|
|
// entry under group_base that group_filter finds
|
|
groupBase string
|
|
groupFilter string
|
|
// how a password is changed: exop (Password Modify) or ad
|
|
// (Active Directory's unicodePwd)
|
|
passwordChange string
|
|
// new accounts (signups): an entry at add_dn with add_attributes
|
|
// (add_pending_attributes for a pending one), added as add_bind_dn
|
|
addDN string
|
|
addBindDN string
|
|
addBindPass string
|
|
addAttrs []ldapAttr
|
|
pendingAttrs []ldapAttr
|
|
}
|
|
|
|
// NewLDAP: the [ldap] backend from its section, checked
|
|
func NewLDAP(s *config.Section) (Authenticator, error) {
|
|
var err error
|
|
a := &LDAP{
|
|
url: s.Get("url", "ldap://127.0.0.1:389"),
|
|
bindDN: s.Get("bind_dn", ""),
|
|
base: s.Get("search_base", ""),
|
|
filter: s.Get("search_filter", "(uid={user})"),
|
|
searchDN: s.Get("search_bind_dn", ""),
|
|
searchPass: s.Get("search_bind_password", ""),
|
|
groupBase: s.Get("group_base", ""),
|
|
groupFilter: s.Get("group_filter",
|
|
"(&(cn={group})(|(member={dn})(uniqueMember={dn})(memberUid={user})))"),
|
|
passwordChange: strings.ToLower(s.Get("password_change", "exop")),
|
|
}
|
|
if a.passwordChange != "exop" && a.passwordChange != "ad" {
|
|
return nil, fmt.Errorf("[ldap] password_change: %q (exop or ad)", a.passwordChange)
|
|
}
|
|
a.addDN = s.Get("add_dn", "")
|
|
a.addBindDN = s.Get("add_bind_dn", a.searchDN)
|
|
a.addBindPass = s.Get("add_bind_password", a.searchPass)
|
|
if a.addAttrs, err = parseLDAPAttrs(s.Get("add_attributes",
|
|
"objectClass=inetOrgPerson | cn={user} | sn={user}")); err != nil {
|
|
return nil, fmt.Errorf("[ldap] add_attributes: %v", err)
|
|
}
|
|
if a.pendingAttrs, err = parseLDAPAttrs(s.Get("add_pending_attributes", "")); err != nil {
|
|
return nil, fmt.Errorf("[ldap] add_pending_attributes: %v", err)
|
|
}
|
|
if a.addDN != "" {
|
|
if !strings.Contains(a.addDN, "{user}") {
|
|
return nil, fmt.Errorf("[ldap] add_dn: no {user} in %q", a.addDN)
|
|
}
|
|
if a.addBindDN == "" {
|
|
return nil, fmt.Errorf("[ldap] add_dn: needs add_bind_dn (or search_bind_dn), an account that can add entries")
|
|
}
|
|
}
|
|
if a.startTLS, err = s.Bool("starttls", false); err != nil {
|
|
return nil, err
|
|
}
|
|
if a.timeout, err = s.Duration("timeout", 10*time.Second); err != nil {
|
|
return nil, err
|
|
}
|
|
skip, err := s.Bool("insecure_skip_verify", false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
a.tls = &tls.Config{InsecureSkipVerify: skip}
|
|
if ca := s.Get("ca_file", ""); ca != "" {
|
|
pem, err := os.ReadFile(ca)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("[ldap] ca_file: %v", err)
|
|
}
|
|
pool := x509.NewCertPool()
|
|
if !pool.AppendCertsFromPEM(pem) {
|
|
return nil, fmt.Errorf("[ldap] ca_file: no certificate in %s", ca)
|
|
}
|
|
a.tls.RootCAs = pool
|
|
}
|
|
if a.bindDN == "" && a.base == "" {
|
|
return nil, fmt.Errorf("[ldap] needs bind_dn or search_base")
|
|
}
|
|
if a.bindDN != "" && !strings.Contains(a.bindDN, "{user}") {
|
|
return nil, fmt.Errorf("[ldap] bind_dn: no {user} in %q", a.bindDN)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// Name: the backend's Name, for {auth} and the log
|
|
func (a *LDAP) Name() string { return "ldap" }
|
|
|
|
// Authenticate: a bind as the user (found by search, or by bind_dn)
|
|
func (a *LDAP) Authenticate(ctx context.Context, user, password string) error {
|
|
conn, _, err := a.bindUser(user, password)
|
|
if conn != nil {
|
|
conn.Close()
|
|
}
|
|
return err
|
|
}
|
|
|
|
// bindUser: a connection bound as the user, and the user's DN
|
|
func (a *LDAP) bindUser(user, password string) (*ldap.Conn, string, error) {
|
|
// an empty password would be an unauthenticated bind, which servers
|
|
// accept for any DN
|
|
if password == "" {
|
|
return nil, "", ErrBadPassword
|
|
}
|
|
conn, err := a.dial()
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
dn, err := a.userDN(conn, user)
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, "", err
|
|
}
|
|
err = conn.Bind(dn, password)
|
|
var le *ldap.Error
|
|
if errors.As(err, &le) && le.ResultCode == ldap.LDAPResultInvalidCredentials {
|
|
conn.Close()
|
|
if a.bindDN != "" {
|
|
// a direct bind cannot tell an unknown DN from a wrong
|
|
// password: let the other backends try
|
|
return nil, "", ErrNoUser
|
|
}
|
|
return nil, "", ErrBadPassword
|
|
}
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, "", err
|
|
}
|
|
return conn, dn, nil
|
|
}
|
|
|
|
// dial the server, with StartTLS if configured
|
|
func (a *LDAP) dial() (*ldap.Conn, error) {
|
|
d := &net.Dialer{Timeout: a.timeout}
|
|
host := a.url
|
|
if i := strings.Index(host, "://"); i >= 0 {
|
|
host = host[i+3:]
|
|
}
|
|
host, _, _ = strings.Cut(host, "/")
|
|
tc := a.tls.Clone()
|
|
if tc.ServerName == "" {
|
|
tc.ServerName, _, _ = strings.Cut(host, ":")
|
|
}
|
|
conn, err := ldap.DialURL(a.url, ldap.DialWithDialer(d), ldap.DialWithTLSConfig(tc))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
conn.SetTimeout(a.timeout)
|
|
if a.startTLS {
|
|
if err := conn.StartTLS(tc); err != nil {
|
|
conn.Close()
|
|
return nil, fmt.Errorf("starttls: %v", err)
|
|
}
|
|
}
|
|
return conn, nil
|
|
}
|
|
|
|
// userDN: the user's DN, from bind_dn or by the search
|
|
func (a *LDAP) userDN(conn *ldap.Conn, user string) (string, error) {
|
|
if a.bindDN != "" {
|
|
return config.Expand(a.bindDN, map[string]string{"user": user}, ldap.EscapeDN), nil
|
|
}
|
|
if a.searchDN != "" {
|
|
if err := conn.Bind(a.searchDN, a.searchPass); err != nil {
|
|
return "", fmt.Errorf("bind %s: %v", a.searchDN, err)
|
|
}
|
|
}
|
|
filter := config.Expand(a.filter, map[string]string{"user": user}, ldap.EscapeFilter)
|
|
res, err := conn.Search(ldap.NewSearchRequest(a.base, ldap.ScopeWholeSubtree,
|
|
ldap.NeverDerefAliases, 2, int(a.timeout/time.Second), false,
|
|
filter, []string{"dn"}, nil))
|
|
if err != nil {
|
|
return "", fmt.Errorf("search %s: %v", filter, err)
|
|
}
|
|
switch len(res.Entries) {
|
|
case 0:
|
|
return "", ErrNoUser
|
|
case 1:
|
|
return res.Entries[0].DN, nil
|
|
}
|
|
return "", fmt.Errorf("search %s: more than one entry", filter)
|
|
}
|
|
|
|
// HasGroups: whether groups are looked up here (group_base is set)
|
|
func (a *LDAP) HasGroups() bool { return a.groupBase != "" }
|
|
|
|
// CanChangePassword: always (exop or ad)
|
|
func (a *LDAP) CanChangePassword() bool { return true }
|
|
|
|
// ChangePassword bound as the user, so that the server applies its own
|
|
// password policy: by the Password Modify extended operation (RFC 3062),
|
|
// or for Active Directory (password_change = ad, which does not have it)
|
|
// by replacing unicodePwd, the old value deleted and the new one added
|
|
// (which AD takes only over TLS: ldaps:// or starttls)
|
|
func (a *LDAP) ChangePassword(_ context.Context, _ Messenger, user, old, new string) error {
|
|
conn, dn, err := a.bindUser(user, old)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Close()
|
|
if a.passwordChange == "ad" {
|
|
err = conn.Modify(adPasswordChange(dn, old, new))
|
|
} else {
|
|
_, err = conn.PasswordModify(ldap.NewPasswordModifyRequest(dn, old, new))
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("%w: %v", ErrRefused, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// adPasswordChange: AD's change of a user's own password, unicodePwd's
|
|
// old value deleted and the new one added, each in "quotes" as UTF-16LE
|
|
func adPasswordChange(dn, old, new string) *ldap.ModifyRequest {
|
|
m := ldap.NewModifyRequest(dn, nil)
|
|
m.Delete("unicodePwd", []string{adPassword(old)})
|
|
m.Add("unicodePwd", []string{adPassword(new)})
|
|
return m
|
|
}
|
|
|
|
// adPassword: a password as AD's unicodePwd takes it, in double quotes
|
|
// and UTF-16LE
|
|
func adPassword(pw string) string {
|
|
u := utf16.Encode([]rune(`"` + pw + `"`))
|
|
b := make([]byte, 2*len(u))
|
|
for i, c := range u {
|
|
b[2*i] = byte(c)
|
|
b[2*i+1] = byte(c >> 8)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// MemberOf: whether user is in group, an entry under group_base that
|
|
// group_filter ({group}, the user's {dn} and {user}) finds; searched as
|
|
// search_bind_dn, else as the user (with their password), else
|
|
// anonymously. ErrNoUser without group_base: LDAP has no groups here.
|
|
func (a *LDAP) MemberOf(user, password, group string) (bool, error) {
|
|
if a.groupBase == "" {
|
|
return false, ErrNoUser
|
|
}
|
|
var conn *ldap.Conn
|
|
var dn string
|
|
var err error
|
|
if a.searchDN == "" && password != "" {
|
|
conn, dn, err = a.bindUser(user, password)
|
|
} else {
|
|
if conn, err = a.dial(); err == nil {
|
|
// userDN binds as search_bind_dn when there is one
|
|
if dn, err = a.userDN(conn, user); err != nil {
|
|
conn.Close()
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer conn.Close()
|
|
filter := config.Expand(a.groupFilter, map[string]string{"group": group, "dn": dn, "user": user}, ldap.EscapeFilter)
|
|
res, err := conn.Search(ldap.NewSearchRequest(a.groupBase, ldap.ScopeWholeSubtree,
|
|
ldap.NeverDerefAliases, 1, int(a.timeout/time.Second), false,
|
|
filter, []string{"dn"}, nil))
|
|
if err != nil {
|
|
return false, fmt.Errorf("group search %s: %v", filter, err)
|
|
}
|
|
return len(res.Entries) > 0, nil
|
|
}
|