Added LDAP admin filter
This commit is contained in:
No files matched your search
@@ -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.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.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.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-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.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)")
|
flag.UintVar(&ldapConfig.Port, "ldap-port", 389, "what port the LDAP server is hosted on (optional)")
|
||||||
|
|||||||
+65
-21
@@ -14,7 +14,9 @@ type Config struct {
|
|||||||
BindUser string
|
BindUser string
|
||||||
BindPass string
|
BindPass string
|
||||||
BaseDN string
|
BaseDN string
|
||||||
|
|
||||||
Filter string
|
Filter string
|
||||||
|
AdminFilter string
|
||||||
|
|
||||||
FQDN string
|
FQDN string
|
||||||
Port uint
|
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.
|
// Create the user if it doesn't exist on the database already.
|
||||||
err = createUserFromLDAP(username, dbc, config, l)
|
err = createUserFromLDAP(username, dbc, config, l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("Failed to create user from LDAP:", err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,20 +75,37 @@ func createUserFromLDAP(username string, dbc *db.DB, config Config, l *ldap.Conn
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// After we have a connection, let's try binding
|
isAdmin := doesLDAPAdminExist(username, config, l)
|
||||||
_, err := l.SimpleBind(&ldap.SimpleBindRequest{
|
|
||||||
Username: fmt.Sprintf("uid=%s,%s", config.BindUser, config.BaseDN),
|
if doesLDAPUserExist(username, config, l) && !isAdmin {
|
||||||
Password: config.BindPass,
|
return errors.New("no such user")
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to bind to LDAP:", err)
|
|
||||||
return errors.New("wrong username or password")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(
|
searchReq := ldap.NewSearchRequest(
|
||||||
config.BaseDN,
|
config.BaseDN,
|
||||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||||
fmt.Sprintf("(&%s(uid=%s))", config.Filter, ldap.EscapeFilter(username)),
|
filter,
|
||||||
[]string{"dn"},
|
[]string{"dn"},
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
@@ -93,25 +113,39 @@ func createUserFromLDAP(username string, dbc *db.DB, config Config, l *ldap.Conn
|
|||||||
result, err := l.Search(searchReq)
|
result, err := l.Search(searchReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to query LDAP server:", err)
|
log.Println("failed to query LDAP server:", err)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
switch len(result.Entries) {
|
if len(result.Entries) == 1 {
|
||||||
case 1:
|
return true
|
||||||
user := db.User{
|
|
||||||
Name: username,
|
|
||||||
Password: "", // no password because we want auth to fail.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dbc.Create(&user).Error; err != nil {
|
return false
|
||||||
log.Println("User created via LDAP:", username)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
// doesLDAPUserExist checks if a user exists on the server.
|
||||||
case 0:
|
func doesLDAPUserExist(username string, config Config, l *ldap.Conn) bool {
|
||||||
return errors.New("invalid username")
|
filter := fmt.Sprintf("(&(uid=%s)%s)", ldap.EscapeFilter(username), config.Filter)
|
||||||
default:
|
|
||||||
return errors.New("ambiguous user")
|
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.
|
// Creates a connection to an LDAP server.
|
||||||
@@ -129,5 +163,15 @@ func createLDAPconnection(config Config) (*ldap.Conn, error) {
|
|||||||
return nil, err
|
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
|
return l, nil
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user