ldap.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Package ldap provide functions & structure to query a LDAP ldap directory
  5. // For now, it's mainly tested again an MS Active Directory service, see README.md for more information
  6. package ldap
  7. import (
  8. "crypto/tls"
  9. "fmt"
  10. "github.com/gogits/gogs/modules/ldap"
  11. "github.com/gogits/gogs/modules/log"
  12. )
  13. // Basic LDAP authentication service
  14. type Source struct {
  15. Name string // canonical name (ie. corporate.ad)
  16. Host string // LDAP host
  17. Port int // port number
  18. UseSSL bool // Use SSL
  19. SkipVerify bool
  20. BindDN string // DN to bind with
  21. BindPassword string // Bind DN password
  22. UserBase string // Base search path for users
  23. UserDN string // Template for the DN of the user for simple auth
  24. AttributeName string // First name attribute
  25. AttributeSurname string // Surname attribute
  26. AttributeMail string // E-mail attribute
  27. Filter string // Query filter to validate entry
  28. AdminFilter string // Query filter to check if user is admin
  29. Enabled bool // if this source is disabled
  30. }
  31. func (ls *Source) FindUserDN(name string) (string, bool) {
  32. l, err := ldapDial(ls)
  33. if err != nil {
  34. log.Error(4, "LDAP Connect error, %s:%v", ls.Host, err)
  35. ls.Enabled = false
  36. return "", false
  37. }
  38. defer l.Close()
  39. log.Trace("Search for LDAP user: %s", name)
  40. if ls.BindDN != "" && ls.BindPassword != "" {
  41. err = l.Bind(ls.BindDN, ls.BindPassword)
  42. if err != nil {
  43. log.Debug("Failed to bind as BindDN[%s]: %v", ls.BindDN, err)
  44. return "", false
  45. }
  46. log.Trace("Bound as BindDN %s", ls.BindDN)
  47. } else {
  48. log.Trace("Proceeding with anonymous LDAP search.")
  49. }
  50. // A search for the user.
  51. userFilter := fmt.Sprintf(ls.Filter, name)
  52. log.Trace("Searching using filter %s", userFilter)
  53. search := ldap.NewSearchRequest(
  54. ls.UserBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0,
  55. false, userFilter, []string{}, nil)
  56. // Ensure we found a user
  57. sr, err := l.Search(search)
  58. if err != nil || len(sr.Entries) < 1 {
  59. log.Debug("Failed search using filter[%s]: %v", userFilter, err)
  60. return "", false
  61. } else if len(sr.Entries) > 1 {
  62. log.Debug("Filter '%s' returned more than one user.", userFilter)
  63. return "", false
  64. }
  65. userDN := sr.Entries[0].DN
  66. if userDN == "" {
  67. log.Error(4, "LDAP search was succesful, but found no DN!")
  68. return "", false
  69. }
  70. return userDN, true
  71. }
  72. // searchEntry : search an LDAP source if an entry (name, passwd) is valid and in the specific filter
  73. func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, string, string, bool, bool) {
  74. var userDN string
  75. if directBind {
  76. log.Trace("LDAP will bind directly via UserDN template: %s", ls.UserDN)
  77. userDN = fmt.Sprintf(ls.UserDN, name)
  78. } else {
  79. log.Trace("LDAP will use BindDN.")
  80. var found bool
  81. userDN, found = ls.FindUserDN(name)
  82. if !found {
  83. return "", "", "", false, false
  84. }
  85. }
  86. l, err := ldapDial(ls)
  87. if err != nil {
  88. log.Error(4, "LDAP Connect error, %s:%v", ls.Host, err)
  89. ls.Enabled = false
  90. return "", "", "", false, false
  91. }
  92. defer l.Close()
  93. log.Trace("Binding with userDN: %s", userDN)
  94. err = l.Bind(userDN, passwd)
  95. if err != nil {
  96. log.Debug("LDAP auth. failed for %s, reason: %v", userDN, err)
  97. return "", "", "", false, false
  98. }
  99. log.Trace("Bound successfully with userDN: %s", userDN)
  100. userFilter := fmt.Sprintf(ls.Filter, name)
  101. search := ldap.NewSearchRequest(
  102. userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, userFilter,
  103. []string{ls.AttributeName, ls.AttributeSurname, ls.AttributeMail},
  104. nil)
  105. sr, err := l.Search(search)
  106. if err != nil {
  107. log.Error(4, "LDAP Search failed unexpectedly! (%v)", err)
  108. return "", "", "", false, false
  109. } else if len(sr.Entries) < 1 {
  110. if directBind {
  111. log.Error(4, "User filter inhibited user login.")
  112. } else {
  113. log.Error(4, "LDAP Search failed unexpectedly! (0 entries)")
  114. }
  115. return "", "", "", false, false
  116. }
  117. name_attr := sr.Entries[0].GetAttributeValue(ls.AttributeName)
  118. sn_attr := sr.Entries[0].GetAttributeValue(ls.AttributeSurname)
  119. mail_attr := sr.Entries[0].GetAttributeValue(ls.AttributeMail)
  120. admin_attr := false
  121. if len(ls.AdminFilter) > 0 {
  122. search = ldap.NewSearchRequest(
  123. userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, ls.AdminFilter,
  124. []string{ls.AttributeName},
  125. nil)
  126. sr, err = l.Search(search)
  127. if err != nil {
  128. log.Error(4, "LDAP Admin Search failed unexpectedly! (%v)", err)
  129. } else if len(sr.Entries) < 1 {
  130. log.Error(4, "LDAP Admin Search failed")
  131. } else {
  132. admin_attr = true
  133. }
  134. }
  135. return name_attr, sn_attr, mail_attr, admin_attr, true
  136. }
  137. func ldapDial(ls *Source) (*ldap.Conn, error) {
  138. if ls.UseSSL {
  139. log.Debug("Using TLS for LDAP without verifying: %v", ls.SkipVerify)
  140. return ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", ls.Host, ls.Port), &tls.Config{
  141. InsecureSkipVerify: ls.SkipVerify,
  142. })
  143. } else {
  144. return ldap.Dial("tcp", fmt.Sprintf("%s:%d", ls.Host, ls.Port))
  145. }
  146. }