2023-08-11 10:00:23 +02:00
|
|
|
// Copyright (C) 2023 NHR@FAU, University Erlangen-Nuremberg.
|
2022-07-29 06:29:21 +02:00
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2021-12-08 10:03:00 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-06-15 12:00:45 +02:00
|
|
|
"fmt"
|
2022-07-07 14:08:37 +02:00
|
|
|
"net/http"
|
2021-12-08 10:03:00 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
2022-02-14 14:37:33 +01:00
|
|
|
"time"
|
2021-12-08 10:03:00 +01:00
|
|
|
|
2022-06-21 17:52:36 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
2022-09-07 12:24:45 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
2021-12-08 10:03:00 +01:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
)
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
type LdapAuthenticator struct {
|
2022-07-07 14:08:37 +02:00
|
|
|
auth *Authentication
|
2022-09-07 12:24:45 +02:00
|
|
|
config *schema.LdapConfig
|
2022-07-07 14:08:37 +02:00
|
|
|
syncPassword string
|
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
var _ Authenticator = (*LdapAuthenticator)(nil)
|
|
|
|
|
|
|
|
func (la *LdapAuthenticator) Init(
|
|
|
|
auth *Authentication,
|
|
|
|
conf interface{}) error {
|
2022-07-07 14:08:37 +02:00
|
|
|
|
|
|
|
la.auth = auth
|
2022-09-07 12:24:45 +02:00
|
|
|
la.config = conf.(*schema.LdapConfig)
|
2022-07-07 14:08:37 +02:00
|
|
|
|
|
|
|
la.syncPassword = os.Getenv("LDAP_ADMIN_PASSWORD")
|
|
|
|
if la.syncPassword == "" {
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Warn("environment variable 'LDAP_ADMIN_PASSWORD' not set (ldap sync will not work)")
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
2022-02-14 14:37:33 +01:00
|
|
|
|
2022-07-25 17:27:42 +02:00
|
|
|
if la.config != nil && la.config.SyncInterval != "" {
|
2022-07-07 14:08:37 +02:00
|
|
|
interval, err := time.ParseDuration(la.config.SyncInterval)
|
2022-02-14 14:37:33 +01:00
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warnf("Could not parse duration for sync interval: %v", la.config.SyncInterval)
|
2022-02-14 14:37:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if interval == 0 {
|
2023-02-15 11:50:51 +01:00
|
|
|
log.Info("Sync interval is zero")
|
2022-02-14 14:37:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
for t := range ticker.C {
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Printf("sync started at %s", t.Format(time.RFC3339))
|
2022-07-07 14:08:37 +02:00
|
|
|
if err := la.Sync(); err != nil {
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Errorf("sync failed: %s", err.Error())
|
2022-02-14 14:37:33 +01:00
|
|
|
}
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Print("sync done")
|
2022-02-14 14:37:33 +01:00
|
|
|
}
|
|
|
|
}()
|
2023-08-16 09:19:41 +02:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("missing LDAP configuration")
|
2022-02-14 14:37:33 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 10:03:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) CanLogin(
|
|
|
|
user *User,
|
2023-08-14 12:40:21 +02:00
|
|
|
username string,
|
2022-09-07 12:24:45 +02:00
|
|
|
rw http.ResponseWriter,
|
|
|
|
r *http.Request) bool {
|
|
|
|
|
2023-08-14 12:40:21 +02:00
|
|
|
if user != nil && user.AuthSource == AuthViaLDAP {
|
|
|
|
return true
|
|
|
|
} else {
|
2023-08-16 09:19:41 +02:00
|
|
|
if la.config != nil && la.config.SyncUserOnLogin {
|
2023-08-14 12:40:21 +02:00
|
|
|
l, err := la.getLdapConnection(true)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("LDAP connection error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the given username
|
|
|
|
searchRequest := ldap.NewSearchRequest(
|
|
|
|
la.config.UserBase,
|
|
|
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
|
|
fmt.Sprintf("(%s(uid=%s))", la.config.UserFilter, username),
|
|
|
|
[]string{"dn", "uid", "gecos"}, nil)
|
|
|
|
|
|
|
|
sr, err := l.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sr.Entries) != 1 {
|
|
|
|
log.Warn("User does not exist or too many entries returned")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := sr.Entries[0]
|
|
|
|
name := entry.GetAttributeValue("gecos")
|
|
|
|
|
|
|
|
if _, err := la.auth.db.Exec(`INSERT INTO user (username, ldap, name, roles) VALUES (?, ?, ?, ?)`,
|
|
|
|
username, 1, name, "[\""+GetRoleString(RoleUser)+"\"]"); err != nil {
|
|
|
|
log.Errorf("User '%s' new in LDAP: Insert into DB failed", username)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) Login(
|
|
|
|
user *User,
|
|
|
|
rw http.ResponseWriter,
|
|
|
|
r *http.Request) (*User, error) {
|
|
|
|
|
2022-07-07 14:08:37 +02:00
|
|
|
l, err := la.getLdapConnection(false)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("Error while getting ldap connection")
|
2022-07-07 14:08:37 +02:00
|
|
|
return nil, err
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
2022-02-14 14:22:44 +01:00
|
|
|
defer l.Close()
|
2021-12-08 10:03:00 +01:00
|
|
|
|
2022-07-07 14:08:37 +02:00
|
|
|
userDn := strings.Replace(la.config.UserBind, "{username}", user.Username, -1)
|
|
|
|
if err := l.Bind(userDn, r.FormValue("password")); err != nil {
|
2023-06-15 12:00:45 +02:00
|
|
|
log.Errorf("AUTH/LOCAL > Authentication for user %s failed: %v", user.Username, err)
|
|
|
|
return nil, fmt.Errorf("AUTH/LDAP > Authentication failed")
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:08:37 +02:00
|
|
|
return user, nil
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) Sync() error {
|
|
|
|
|
2021-12-08 10:03:00 +01:00
|
|
|
const IN_DB int = 1
|
|
|
|
const IN_LDAP int = 2
|
|
|
|
const IN_BOTH int = 3
|
|
|
|
|
|
|
|
users := map[string]int{}
|
2022-07-07 14:08:37 +02:00
|
|
|
rows, err := la.auth.db.Query(`SELECT username FROM user WHERE user.ldap = 1`)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("Error while querying LDAP users")
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var username string
|
|
|
|
if err := rows.Scan(&username); err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warnf("Error while scanning for user '%s'", username)
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
users[username] = IN_DB
|
|
|
|
}
|
|
|
|
|
2022-07-07 14:08:37 +02:00
|
|
|
l, err := la.getLdapConnection(true)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
2023-01-31 18:28:44 +01:00
|
|
|
log.Error("LDAP connection error")
|
2021-12-08 10:03:00 +01:00
|
|
|
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(
|
2023-08-14 12:40:21 +02:00
|
|
|
la.config.UserBase,
|
|
|
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
|
|
fmt.Sprintf("(%s(uid=%s))", la.config.UserFilter, "*"),
|
|
|
|
[]string{"dn", "uid", "gecos"}, nil))
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("LDAP search error")
|
2021-12-08 10:03:00 +01:00
|
|
|
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-07-07 14:08:37 +02:00
|
|
|
if where == IN_DB && la.config.SyncDelOldUsers {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Debugf("sync: remove %v (does not show up in LDAP anymore)", username)
|
2022-07-07 14:08:37 +02:00
|
|
|
if _, err := la.auth.db.Exec(`DELETE FROM user WHERE user.username = ?`, username); err != nil {
|
2023-01-31 18:28:44 +01:00
|
|
|
log.Errorf("User '%s' not in LDAP anymore: Delete from DB failed", username)
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if where == IN_LDAP {
|
|
|
|
name := newnames[username]
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Debugf("sync: add %v (name: %v, roles: [user], ldap: true)", username, name)
|
2022-07-07 14:08:37 +02:00
|
|
|
if _, err := la.auth.db.Exec(`INSERT INTO user (username, ldap, name, roles) VALUES (?, ?, ?, ?)`,
|
2023-03-06 11:44:38 +01:00
|
|
|
username, 1, name, "[\""+GetRoleString(RoleUser)+"\"]"); err != nil {
|
2023-01-31 18:28:44 +01:00
|
|
|
log.Errorf("User '%s' new in LDAP: Insert into DB failed", username)
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-07 14:08:37 +02:00
|
|
|
|
|
|
|
// TODO: Add a connection pool or something like
|
|
|
|
// that so that connections can be reused/cached.
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) getLdapConnection(admin bool) (*ldap.Conn, error) {
|
|
|
|
|
2022-07-07 14:08:37 +02:00
|
|
|
conn, err := ldap.DialURL(la.config.Url)
|
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("LDAP URL dial failed")
|
2022-07-07 14:08:37 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if admin {
|
|
|
|
if err := conn.Bind(la.config.SearchDN, la.syncPassword); err != nil {
|
|
|
|
conn.Close()
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("LDAP connection bind failed")
|
2022-07-07 14:08:37 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|