From 5638ef5df7346adf28335c0c125100004416e876 Mon Sep 17 00:00:00 2001 From: Daniel Hall Date: Sun, 7 Apr 2024 11:24:26 -0700 Subject: [PATCH] Added LDAP admin filter --- cmd/gonic/gonic.go | 2 + ldap/ldap.go | 96 +++++++++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index f5af91e..aa385be 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -87,7 +87,9 @@ func main() { flag.StringVar(&ldapConfig.BindUser, "ldap-bind-user", "", "the bind user to bind to LDAP with (required for LDAP)") flag.StringVar(&ldapConfig.BindPass, "ldap-bind-pass", "", "the password of the LDAP bind user (required for LDAP)") flag.StringVar(&ldapConfig.BaseDN, "ldap-base-dn", "", "the base DN for LDAP objects (required for LDAP)") + flag.StringVar(&ldapConfig.Filter, "ldap-filter", "(uid=*)", "the filter to select LDAP objects with (optional)") + flag.StringVar(&ldapConfig.Filter, "ldap-admin-filter", "(memberof=cn=admin)", "the filter to select LDAP objects with (optional)") flag.StringVar(&ldapConfig.FQDN, "ldap-fqdn", "", "the name of the server to connect to (required for LDAP)") flag.UintVar(&ldapConfig.Port, "ldap-port", 389, "what port the LDAP server is hosted on (optional)") diff --git a/ldap/ldap.go b/ldap/ldap.go index e20fda2..e4864b8 100644 --- a/ldap/ldap.go +++ b/ldap/ldap.go @@ -14,7 +14,9 @@ type Config struct { BindUser string BindPass string BaseDN string - Filter string + + Filter string + AdminFilter string FQDN string Port uint @@ -44,6 +46,7 @@ func CheckLDAPcreds(username string, password string, dbc *db.DB, config Config) // Create the user if it doesn't exist on the database already. err = createUserFromLDAP(username, dbc, config, l) if err != nil { + log.Println("Failed to create user from LDAP:", err) return false, err } @@ -72,20 +75,37 @@ func createUserFromLDAP(username string, dbc *db.DB, config Config, l *ldap.Conn return nil } - // After we have a connection, let's try binding - _, err := l.SimpleBind(&ldap.SimpleBindRequest{ - Username: fmt.Sprintf("uid=%s,%s", config.BindUser, config.BaseDN), - Password: config.BindPass, - }) - if err != nil { - log.Println("Failed to bind to LDAP:", err) - return errors.New("wrong username or password") + isAdmin := doesLDAPAdminExist(username, config, l) + + if doesLDAPUserExist(username, config, l) && !isAdmin { + return errors.New("no such user") } + newUser := db.User{ + Name: username, + Password: "", // no password because we want auth to fail. + IsAdmin: isAdmin, + } + + log.Println(username, isAdmin) + + err := dbc.Create(&newUser).Error + if err != nil { + return err + } + + log.Println("User created via LDAP:", username) + return nil +} + +// doesLDAPAdminExist checks if an admin exists on the server. +func doesLDAPAdminExist(username string, config Config, l *ldap.Conn) bool { + filter := fmt.Sprintf("(&(uid=%s)%s)", ldap.EscapeFilter(username), config.AdminFilter) + searchReq := ldap.NewSearchRequest( config.BaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, - fmt.Sprintf("(&%s(uid=%s))", config.Filter, ldap.EscapeFilter(username)), + filter, []string{"dn"}, nil, ) @@ -93,25 +113,39 @@ func createUserFromLDAP(username string, dbc *db.DB, config Config, l *ldap.Conn result, err := l.Search(searchReq) if err != nil { log.Println("failed to query LDAP server:", err) + return false } - switch len(result.Entries) { - case 1: - user := db.User{ - Name: username, - Password: "", // no password because we want auth to fail. - } - - if err := dbc.Create(&user).Error; err != nil { - log.Println("User created via LDAP:", username) - } - - return nil - case 0: - return errors.New("invalid username") - default: - return errors.New("ambiguous user") + if len(result.Entries) == 1 { + return true } + + return false +} + +// doesLDAPUserExist checks if a user exists on the server. +func doesLDAPUserExist(username string, config Config, l *ldap.Conn) bool { + filter := fmt.Sprintf("(&(uid=%s)%s)", ldap.EscapeFilter(username), config.Filter) + + searchReq := ldap.NewSearchRequest( + config.BaseDN, + ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, + filter, + []string{"dn"}, + nil, + ) + + result, err := l.Search(searchReq) + if err != nil { + log.Println("failed to query LDAP server:", err) + return false + } + + if len(result.Entries) == 1 { + return true + } + + return false } // Creates a connection to an LDAP server. @@ -129,5 +163,15 @@ func createLDAPconnection(config Config) (*ldap.Conn, error) { return nil, err } + // After we have a connection, let's try binding + _, err = l.SimpleBind(&ldap.SimpleBindRequest{ + Username: fmt.Sprintf("uid=%s,%s", config.BindUser, config.BaseDN), + Password: config.BindPass, + }) + if err != nil { + log.Println("Failed to bind to LDAP:", err) + return nil, errors.New("wrong username or password") + } + return l, nil }