2021-12-08 10:03:00 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-02-14 14:37:33 +01:00
|
|
|
"time"
|
2021-12-08 10:03:00 +01:00
|
|
|
|
2022-01-27 10:35:26 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/log"
|
|
|
|
|
2021-12-08 10:03:00 +01:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LdapConfig struct {
|
2022-02-15 10:00:55 +01:00
|
|
|
Url string `json:"url"`
|
|
|
|
UserBase string `json:"user_base"`
|
|
|
|
SearchDN string `json:"search_dn"`
|
|
|
|
UserBind string `json:"user_bind"`
|
|
|
|
UserFilter string `json:"user_filter"`
|
|
|
|
SyncInterval string `json:"sync_interval"` // Parsed using time.ParseDuration.
|
2022-02-14 14:37:33 +01:00
|
|
|
SyncDelOldUsers bool `json:"sync_del_old_users"`
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 14:22:44 +01:00
|
|
|
func (auth *Authentication) initLdap() error {
|
|
|
|
auth.ldapSyncUserPassword = os.Getenv("LDAP_ADMIN_PASSWORD")
|
|
|
|
if auth.ldapSyncUserPassword == "" {
|
2022-01-27 10:35:26 +01:00
|
|
|
log.Warn("environment variable 'LDAP_ADMIN_PASSWORD' not set (ldap sync or authentication will not work)")
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
2022-02-14 14:37:33 +01:00
|
|
|
|
|
|
|
if auth.ldapConfig.SyncInterval != "" {
|
|
|
|
interval, err := time.ParseDuration(auth.ldapConfig.SyncInterval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if interval == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
for t := range ticker.C {
|
|
|
|
log.Printf("LDAP sync started at %s", t.Format(time.RFC3339))
|
|
|
|
if err := auth.SyncWithLDAP(auth.ldapConfig.SyncDelOldUsers); err != nil {
|
|
|
|
log.Errorf("LDAP sync failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
log.Print("LDAP sync done")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-12-08 10:03:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add a connection pool or something like
|
|
|
|
// that so that connections can be reused/cached.
|
2022-02-14 14:22:44 +01:00
|
|
|
func (auth *Authentication) getLdapConnection(admin bool) (*ldap.Conn, error) {
|
|
|
|
conn, err := ldap.DialURL(auth.ldapConfig.Url)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:29:11 +01:00
|
|
|
if admin {
|
2022-02-14 14:22:44 +01:00
|
|
|
if err := conn.Bind(auth.ldapConfig.SearchDN, auth.ldapSyncUserPassword); err != nil {
|
2022-01-27 09:29:11 +01:00
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2022-02-14 14:22:44 +01:00
|
|
|
func (auth *Authentication) loginViaLdap(user *User, password string) error {
|
|
|
|
l, err := auth.getLdapConnection(false)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-14 14:22:44 +01:00
|
|
|
defer l.Close()
|
2021-12-08 10:03:00 +01:00
|
|
|
|
2022-02-14 14:22:44 +01:00
|
|
|
userDn := strings.Replace(auth.ldapConfig.UserBind, "{username}", user.Username, -1)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err := l.Bind(userDn, password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
user.ViaLdap = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete users where user.ldap is 1 and that do not show up in the ldap search results.
|
|
|
|
// Add users to the users table that are new in the ldap search results.
|
2022-02-14 14:22:44 +01:00
|
|
|
func (auth *Authentication) SyncWithLDAP(deleteOldUsers bool) error {
|
|
|
|
if auth.ldapConfig == nil {
|
2021-12-08 10:03:00 +01:00
|
|
|
return errors.New("ldap not enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
const IN_DB int = 1
|
|
|
|
const IN_LDAP int = 2
|
|
|
|
const IN_BOTH int = 3
|
|
|
|
|
|
|
|
users := map[string]int{}
|
2022-02-14 14:22:44 +01:00
|
|
|
rows, err := auth.db.Query(`SELECT username FROM user WHERE user.ldap = 1`)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var username string
|
|
|
|
if err := rows.Scan(&username); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
users[username] = IN_DB
|
|
|
|
}
|
|
|
|
|
2022-02-14 14:22:44 +01:00
|
|
|
l, err := auth.getLdapConnection(true)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-14 14:22:44 +01:00
|
|
|
defer l.Close()
|
2021-12-08 10:03:00 +01:00
|
|
|
|
|
|
|
ldapResults, err := l.Search(ldap.NewSearchRequest(
|
2022-02-14 14:22:44 +01:00
|
|
|
auth.ldapConfig.UserBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
|
|
auth.ldapConfig.UserFilter, []string{"dn", "uid", "gecos"}, nil))
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newnames := map[string]string{}
|
|
|
|
for _, entry := range ldapResults.Entries {
|
|
|
|
username := entry.GetAttributeValue("uid")
|
|
|
|
if username == "" {
|
|
|
|
return errors.New("no attribute 'uid'")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := users[username]
|
|
|
|
if !ok {
|
|
|
|
users[username] = IN_LDAP
|
|
|
|
newnames[username] = entry.GetAttributeValue("gecos")
|
|
|
|
} else {
|
|
|
|
users[username] = IN_BOTH
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for username, where := range users {
|
2022-02-14 14:22:44 +01:00
|
|
|
if where == IN_DB && deleteOldUsers {
|
2022-01-27 10:35:26 +01:00
|
|
|
log.Infof("ldap-sync: remove %#v (does not show up in LDAP anymore)", username)
|
2022-02-14 14:22:44 +01:00
|
|
|
if _, err := auth.db.Exec(`DELETE FROM user WHERE user.username = ?`, username); err != nil {
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if where == IN_LDAP {
|
|
|
|
name := newnames[username]
|
2022-05-02 11:59:24 +02:00
|
|
|
log.Infof("ldap-sync: add %#v (name: %#v, roles: [user], ldap: true)", username, name)
|
2022-02-14 14:22:44 +01:00
|
|
|
if _, err := auth.db.Exec(`INSERT INTO user (username, ldap, name, roles) VALUES (?, ?, ?, ?)`,
|
2022-05-02 11:59:24 +02:00
|
|
|
username, 1, name, "[\""+RoleUser+"\"]"); err != nil {
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|