|
@@ -2,8 +2,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+
|
|
package ldap
|
|
package ldap
|
|
|
|
|
|
import (
|
|
import (
|
|
@@ -15,19 +15,31 @@ import (
|
|
log "unknwon.dev/clog/v2"
|
|
log "unknwon.dev/clog/v2"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+
|
|
type SecurityProtocol int
|
|
type SecurityProtocol int
|
|
|
|
|
|
-
|
|
+
|
|
const (
|
|
const (
|
|
SecurityProtocolUnencrypted SecurityProtocol = iota
|
|
SecurityProtocolUnencrypted SecurityProtocol = iota
|
|
SecurityProtocolLDAPS
|
|
SecurityProtocolLDAPS
|
|
SecurityProtocolStartTLS
|
|
SecurityProtocolStartTLS
|
|
)
|
|
)
|
|
|
|
|
|
-
|
|
+
|
|
-type Source struct {
|
|
+func SecurityProtocolName(protocol SecurityProtocol) string {
|
|
|
|
+ return map[SecurityProtocol]string{
|
|
|
|
+ SecurityProtocolUnencrypted: "Unencrypted",
|
|
|
|
+ SecurityProtocolLDAPS: "LDAPS",
|
|
|
|
+ SecurityProtocolStartTLS: "StartTLS",
|
|
|
|
+ }[protocol]
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+type Config struct {
|
|
Host string
|
|
Host string
|
|
- Port int
|
|
+ Port int
|
|
SecurityProtocol SecurityProtocol
|
|
SecurityProtocol SecurityProtocol
|
|
SkipVerify bool
|
|
SkipVerify bool
|
|
BindDN string `ini:"bind_dn,omitempty"`
|
|
BindDN string `ini:"bind_dn,omitempty"`
|
|
@@ -37,18 +49,22 @@ type Source struct {
|
|
AttributeUsername string
|
|
AttributeUsername string
|
|
AttributeName string
|
|
AttributeName string
|
|
AttributeSurname string
|
|
AttributeSurname string
|
|
- AttributeMail string
|
|
+ AttributeMail string
|
|
- AttributesInBind bool
|
|
+ AttributesInBind bool
|
|
Filter string
|
|
Filter string
|
|
AdminFilter string
|
|
AdminFilter string
|
|
- GroupEnabled bool
|
|
+ GroupEnabled bool
|
|
- GroupDN string `ini:"group_dn"`
|
|
+ GroupDN string `ini:"group_dn"`
|
|
- GroupFilter string
|
|
+ GroupFilter string
|
|
GroupMemberUID string `ini:"group_member_uid"`
|
|
GroupMemberUID string `ini:"group_member_uid"`
|
|
- UserUID string `ini:"user_uid"`
|
|
+ UserUID string `ini:"user_uid"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (c *Config) SecurityProtocolName() string {
|
|
|
|
+ return SecurityProtocolName(c.SecurityProtocol)
|
|
}
|
|
}
|
|
|
|
|
|
-func (ls *Source) sanitizedUserQuery(username string) (string, bool) {
|
|
+func (c *Config) sanitizedUserQuery(username string) (string, bool) {
|
|
|
|
|
|
badCharacters := "\x00()*\\"
|
|
badCharacters := "\x00()*\\"
|
|
if strings.ContainsAny(username, badCharacters) {
|
|
if strings.ContainsAny(username, badCharacters) {
|
|
@@ -56,10 +72,10 @@ func (ls *Source) sanitizedUserQuery(username string) (string, bool) {
|
|
return "", false
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
|
|
- return strings.Replace(ls.Filter, "%s", username, -1), true
|
|
+ return strings.Replace(c.Filter, "%s", username, -1), true
|
|
}
|
|
}
|
|
|
|
|
|
-func (ls *Source) sanitizedUserDN(username string) (string, bool) {
|
|
+func (c *Config) sanitizedUserDN(username string) (string, bool) {
|
|
|
|
|
|
badCharacters := "\x00()*\\,='\"#+;<>"
|
|
badCharacters := "\x00()*\\,='\"#+;<>"
|
|
if strings.ContainsAny(username, badCharacters) || strings.HasPrefix(username, " ") || strings.HasSuffix(username, " ") {
|
|
if strings.ContainsAny(username, badCharacters) || strings.HasPrefix(username, " ") || strings.HasSuffix(username, " ") {
|
|
@@ -67,10 +83,10 @@ func (ls *Source) sanitizedUserDN(username string) (string, bool) {
|
|
return "", false
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
|
|
- return strings.Replace(ls.UserDN, "%s", username, -1), true
|
|
+ return strings.Replace(c.UserDN, "%s", username, -1), true
|
|
}
|
|
}
|
|
|
|
|
|
-func (ls *Source) sanitizedGroupFilter(group string) (string, bool) {
|
|
+func (c *Config) sanitizedGroupFilter(group string) (string, bool) {
|
|
|
|
|
|
badCharacters := "\x00*\\"
|
|
badCharacters := "\x00*\\"
|
|
if strings.ContainsAny(group, badCharacters) {
|
|
if strings.ContainsAny(group, badCharacters) {
|
|
@@ -81,7 +97,7 @@ func (ls *Source) sanitizedGroupFilter(group string) (string, bool) {
|
|
return group, true
|
|
return group, true
|
|
}
|
|
}
|
|
|
|
|
|
-func (ls *Source) sanitizedGroupDN(groupDn string) (string, bool) {
|
|
+func (c *Config) sanitizedGroupDN(groupDn string) (string, bool) {
|
|
|
|
|
|
badCharacters := "\x00()*\\'\"#+;<>"
|
|
badCharacters := "\x00()*\\'\"#+;<>"
|
|
if strings.ContainsAny(groupDn, badCharacters) || strings.HasPrefix(groupDn, " ") || strings.HasSuffix(groupDn, " ") {
|
|
if strings.ContainsAny(groupDn, badCharacters) || strings.HasPrefix(groupDn, " ") || strings.HasSuffix(groupDn, " ") {
|
|
@@ -92,12 +108,12 @@ func (ls *Source) sanitizedGroupDN(groupDn string) (string, bool) {
|
|
return groupDn, true
|
|
return groupDn, true
|
|
}
|
|
}
|
|
|
|
|
|
-func (ls *Source) findUserDN(l *ldap.Conn, name string) (string, bool) {
|
|
+func (c *Config) findUserDN(l *ldap.Conn, name string) (string, bool) {
|
|
log.Trace("Search for LDAP user: %s", name)
|
|
log.Trace("Search for LDAP user: %s", name)
|
|
- if len(ls.BindDN) > 0 && len(ls.BindPassword) > 0 {
|
|
+ if len(c.BindDN) > 0 && len(c.BindPassword) > 0 {
|
|
|
|
|
|
- bindDN := strings.Replace(ls.BindDN, "%s", name, -1)
|
|
+ bindDN := strings.Replace(c.BindDN, "%s", name, -1)
|
|
- err := l.Bind(bindDN, ls.BindPassword)
|
|
+ err := l.Bind(bindDN, c.BindPassword)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Trace("LDAP: Failed to bind as BindDN '%s': %v", bindDN, err)
|
|
log.Trace("LDAP: Failed to bind as BindDN '%s': %v", bindDN, err)
|
|
return "", false
|
|
return "", false
|
|
@@ -108,23 +124,23 @@ func (ls *Source) findUserDN(l *ldap.Conn, name string) (string, bool) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- userFilter, ok := ls.sanitizedUserQuery(name)
|
|
+ userFilter, ok := c.sanitizedUserQuery(name)
|
|
if !ok {
|
|
if !ok {
|
|
return "", false
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
|
|
- log.Trace("LDAP: Searching for DN using filter '%s' and base '%s'", userFilter, ls.UserBase)
|
|
+ log.Trace("LDAP: Searching for DN using filter %q and base %q", userFilter, c.UserBase)
|
|
search := ldap.NewSearchRequest(
|
|
search := ldap.NewSearchRequest(
|
|
- ls.UserBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0,
|
|
+ c.UserBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0,
|
|
false, userFilter, []string{}, nil)
|
|
false, userFilter, []string{}, nil)
|
|
|
|
|
|
|
|
|
|
sr, err := l.Search(search)
|
|
sr, err := l.Search(search)
|
|
if err != nil || len(sr.Entries) < 1 {
|
|
if err != nil || len(sr.Entries) < 1 {
|
|
- log.Trace("LDAP: Failed search using filter '%s': %v", userFilter, err)
|
|
+ log.Trace("LDAP: Failed to search using filter %q: %v", userFilter, err)
|
|
return "", false
|
|
return "", false
|
|
} else if len(sr.Entries) > 1 {
|
|
} else if len(sr.Entries) > 1 {
|
|
- log.Trace("LDAP: Filter '%s' returned more than one user", userFilter)
|
|
+ log.Trace("LDAP: Filter %q returned more than one user", userFilter)
|
|
return "", false
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
|
|
@@ -137,7 +153,7 @@ func (ls *Source) findUserDN(l *ldap.Conn, name string) (string, bool) {
|
|
return userDN, true
|
|
return userDN, true
|
|
}
|
|
}
|
|
|
|
|
|
-func dial(ls *Source) (*ldap.Conn, error) {
|
|
+func dial(ls *Config) (*ldap.Conn, error) {
|
|
log.Trace("LDAP: Dialing with security protocol '%v' without verifying: %v", ls.SecurityProtocol, ls.SkipVerify)
|
|
log.Trace("LDAP: Dialing with security protocol '%v' without verifying: %v", ls.SecurityProtocol, ls.SkipVerify)
|
|
|
|
|
|
tlsCfg := &tls.Config{
|
|
tlsCfg := &tls.Config{
|
|
@@ -174,26 +190,26 @@ func bindUser(l *ldap.Conn, userDN, passwd string) error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
+
|
|
-func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, string, string, string, bool, bool) {
|
|
+func (c *Config) searchEntry(name, passwd string, directBind bool) (string, string, string, string, bool, bool) {
|
|
|
|
|
|
if len(passwd) == 0 {
|
|
if len(passwd) == 0 {
|
|
log.Trace("authentication failed for '%s' with empty password", name)
|
|
log.Trace("authentication failed for '%s' with empty password", name)
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
- l, err := dial(ls)
|
|
+ l, err := dial(c)
|
|
if err != nil {
|
|
if err != nil {
|
|
- log.Error("LDAP connect failed for '%s': %v", ls.Host, err)
|
|
+ log.Error("LDAP connect failed for '%s': %v", c.Host, err)
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
defer l.Close()
|
|
defer l.Close()
|
|
|
|
|
|
var userDN string
|
|
var userDN string
|
|
if directBind {
|
|
if directBind {
|
|
- log.Trace("LDAP will bind directly via UserDN template: %s", ls.UserDN)
|
|
+ log.Trace("LDAP will bind directly via UserDN template: %s", c.UserDN)
|
|
|
|
|
|
var ok bool
|
|
var ok bool
|
|
- userDN, ok = ls.sanitizedUserDN(name)
|
|
+ userDN, ok = c.sanitizedUserDN(name)
|
|
if !ok {
|
|
if !ok {
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
@@ -201,13 +217,13 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
|
|
log.Trace("LDAP will use BindDN")
|
|
log.Trace("LDAP will use BindDN")
|
|
|
|
|
|
var found bool
|
|
var found bool
|
|
- userDN, found = ls.findUserDN(l, name)
|
|
+ userDN, found = c.findUserDN(l, name)
|
|
if !found {
|
|
if !found {
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if directBind || !ls.AttributesInBind {
|
|
+ if directBind || !c.AttributesInBind {
|
|
|
|
|
|
err = bindUser(l, userDN, passwd)
|
|
err = bindUser(l, userDN, passwd)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -215,18 +231,18 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- userFilter, ok := ls.sanitizedUserQuery(name)
|
|
+ userFilter, ok := c.sanitizedUserQuery(name)
|
|
if !ok {
|
|
if !ok {
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
|
|
|
|
- log.Trace("Fetching attributes '%v', '%v', '%v', '%v', '%v' with filter '%s' and base '%s'",
|
|
+ log.Trace("Fetching attributes %q, %q, %q, %q, %q with user filter %q and user DN %q",
|
|
- ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.UserUID, userFilter, userDN)
|
|
+ c.AttributeUsername, c.AttributeName, c.AttributeSurname, c.AttributeMail, c.UserUID, userFilter, userDN)
|
|
|
|
+
|
|
search := ldap.NewSearchRequest(
|
|
search := ldap.NewSearchRequest(
|
|
userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, userFilter,
|
|
userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, userFilter,
|
|
- []string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.UserUID},
|
|
+ []string{c.AttributeUsername, c.AttributeName, c.AttributeSurname, c.AttributeMail, c.UserUID},
|
|
nil)
|
|
nil)
|
|
-
|
|
|
|
sr, err := l.Search(search)
|
|
sr, err := l.Search(search)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Error("LDAP: User search failed: %v", err)
|
|
log.Error("LDAP: User search failed: %v", err)
|
|
@@ -241,27 +257,27 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
|
|
|
|
- username := sr.Entries[0].GetAttributeValue(ls.AttributeUsername)
|
|
+ username := sr.Entries[0].GetAttributeValue(c.AttributeUsername)
|
|
- firstname := sr.Entries[0].GetAttributeValue(ls.AttributeName)
|
|
+ firstname := sr.Entries[0].GetAttributeValue(c.AttributeName)
|
|
- surname := sr.Entries[0].GetAttributeValue(ls.AttributeSurname)
|
|
+ surname := sr.Entries[0].GetAttributeValue(c.AttributeSurname)
|
|
- mail := sr.Entries[0].GetAttributeValue(ls.AttributeMail)
|
|
+ mail := sr.Entries[0].GetAttributeValue(c.AttributeMail)
|
|
- uid := sr.Entries[0].GetAttributeValue(ls.UserUID)
|
|
+ uid := sr.Entries[0].GetAttributeValue(c.UserUID)
|
|
|
|
|
|
|
|
|
|
- if ls.GroupEnabled {
|
|
+ if c.GroupEnabled {
|
|
- groupFilter, ok := ls.sanitizedGroupFilter(ls.GroupFilter)
|
|
+ groupFilter, ok := c.sanitizedGroupFilter(c.GroupFilter)
|
|
if !ok {
|
|
if !ok {
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
- groupDN, ok := ls.sanitizedGroupDN(ls.GroupDN)
|
|
+ groupDN, ok := c.sanitizedGroupDN(c.GroupDN)
|
|
if !ok {
|
|
if !ok {
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
|
|
|
|
- log.Trace("LDAP: Fetching groups '%v' with filter '%s' and base '%s'", ls.GroupMemberUID, groupFilter, groupDN)
|
|
+ log.Trace("LDAP: Fetching groups '%v' with filter '%s' and base '%s'", c.GroupMemberUID, groupFilter, groupDN)
|
|
groupSearch := ldap.NewSearchRequest(
|
|
groupSearch := ldap.NewSearchRequest(
|
|
groupDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, groupFilter,
|
|
groupDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, groupFilter,
|
|
- []string{ls.GroupMemberUID},
|
|
+ []string{c.GroupMemberUID},
|
|
nil)
|
|
nil)
|
|
|
|
|
|
srg, err := l.Search(groupSearch)
|
|
srg, err := l.Search(groupSearch)
|
|
@@ -274,9 +290,9 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
|
|
}
|
|
}
|
|
|
|
|
|
isMember := false
|
|
isMember := false
|
|
- if ls.UserUID == "dn" {
|
|
+ if c.UserUID == "dn" {
|
|
for _, group := range srg.Entries {
|
|
for _, group := range srg.Entries {
|
|
- for _, member := range group.GetAttributeValues(ls.GroupMemberUID) {
|
|
+ for _, member := range group.GetAttributeValues(c.GroupMemberUID) {
|
|
if member == sr.Entries[0].DN {
|
|
if member == sr.Entries[0].DN {
|
|
isMember = true
|
|
isMember = true
|
|
}
|
|
}
|
|
@@ -284,7 +300,7 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
for _, group := range srg.Entries {
|
|
for _, group := range srg.Entries {
|
|
- for _, member := range group.GetAttributeValues(ls.GroupMemberUID) {
|
|
+ for _, member := range group.GetAttributeValues(c.GroupMemberUID) {
|
|
if member == uid {
|
|
if member == uid {
|
|
isMember = true
|
|
isMember = true
|
|
}
|
|
}
|
|
@@ -293,17 +309,17 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
|
|
}
|
|
}
|
|
|
|
|
|
if !isMember {
|
|
if !isMember {
|
|
- log.Trace("LDAP: Group membership test failed [username: %s, group_member_uid: %s, user_uid: %s", username, ls.GroupMemberUID, uid)
|
|
+ log.Trace("LDAP: Group membership test failed [username: %s, group_member_uid: %s, user_uid: %s", username, c.GroupMemberUID, uid)
|
|
return "", "", "", "", false, false
|
|
return "", "", "", "", false, false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
isAdmin := false
|
|
isAdmin := false
|
|
- if len(ls.AdminFilter) > 0 {
|
|
+ if len(c.AdminFilter) > 0 {
|
|
- log.Trace("Checking admin with filter '%s' and base '%s'", ls.AdminFilter, userDN)
|
|
+ log.Trace("Checking admin with filter '%s' and base '%s'", c.AdminFilter, userDN)
|
|
search = ldap.NewSearchRequest(
|
|
search = ldap.NewSearchRequest(
|
|
- userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, ls.AdminFilter,
|
|
+ userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, c.AdminFilter,
|
|
- []string{ls.AttributeName},
|
|
+ []string{c.AttributeName},
|
|
nil)
|
|
nil)
|
|
|
|
|
|
sr, err = l.Search(search)
|
|
sr, err = l.Search(search)
|
|
@@ -316,7 +332,7 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if !directBind && ls.AttributesInBind {
|
|
+ if !directBind && c.AttributesInBind {
|
|
|
|
|
|
err = bindUser(l, userDN, passwd)
|
|
err = bindUser(l, userDN, passwd)
|
|
if err != nil {
|
|
if err != nil {
|