login.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 models
  5. import (
  6. "crypto/tls"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "net/smtp"
  11. "strings"
  12. "time"
  13. "github.com/Unknwon/com"
  14. "github.com/go-xorm/core"
  15. "github.com/go-xorm/xorm"
  16. "github.com/gogits/gogs/modules/auth/ldap"
  17. "github.com/gogits/gogs/modules/auth/pam"
  18. "github.com/gogits/gogs/modules/log"
  19. )
  20. type LoginType int
  21. // Note: new type must be added at the end of list to maintain compatibility.
  22. const (
  23. NOTYPE LoginType = iota
  24. PLAIN
  25. LDAP
  26. SMTP
  27. PAM
  28. DLDAP
  29. )
  30. var (
  31. ErrAuthenticationAlreadyExist = errors.New("Authentication already exist")
  32. ErrAuthenticationNotExist = errors.New("Authentication does not exist")
  33. ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users")
  34. )
  35. var LoginNames = map[LoginType]string{
  36. LDAP: "LDAP (via BindDN)",
  37. DLDAP: "LDAP (simple auth)",
  38. SMTP: "SMTP",
  39. PAM: "PAM",
  40. }
  41. // Ensure structs implemented interface.
  42. var (
  43. _ core.Conversion = &LDAPConfig{}
  44. _ core.Conversion = &SMTPConfig{}
  45. _ core.Conversion = &PAMConfig{}
  46. )
  47. type LDAPConfig struct {
  48. ldap.Ldapsource
  49. }
  50. func (cfg *LDAPConfig) FromDB(bs []byte) error {
  51. return json.Unmarshal(bs, &cfg.Ldapsource)
  52. }
  53. func (cfg *LDAPConfig) ToDB() ([]byte, error) {
  54. return json.Marshal(cfg.Ldapsource)
  55. }
  56. type SMTPConfig struct {
  57. Auth string
  58. Host string
  59. Port int
  60. AllowedDomains string `xorm:"TEXT"`
  61. TLS bool
  62. SkipVerify bool
  63. }
  64. func (cfg *SMTPConfig) FromDB(bs []byte) error {
  65. return json.Unmarshal(bs, cfg)
  66. }
  67. func (cfg *SMTPConfig) ToDB() ([]byte, error) {
  68. return json.Marshal(cfg)
  69. }
  70. type PAMConfig struct {
  71. ServiceName string // pam service (e.g. system-auth)
  72. }
  73. func (cfg *PAMConfig) FromDB(bs []byte) error {
  74. return json.Unmarshal(bs, &cfg)
  75. }
  76. func (cfg *PAMConfig) ToDB() ([]byte, error) {
  77. return json.Marshal(cfg)
  78. }
  79. type LoginSource struct {
  80. ID int64 `xorm:"pk autoincr"`
  81. Type LoginType
  82. Name string `xorm:"UNIQUE"`
  83. IsActived bool `xorm:"NOT NULL DEFAULT false"`
  84. Cfg core.Conversion `xorm:"TEXT"`
  85. AllowAutoRegister bool `xorm:"NOT NULL DEFAULT false"`
  86. Created time.Time `xorm:"CREATED"`
  87. Updated time.Time `xorm:"UPDATED"`
  88. }
  89. func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
  90. switch colName {
  91. case "type":
  92. switch LoginType((*val).(int64)) {
  93. case LDAP, DLDAP:
  94. source.Cfg = new(LDAPConfig)
  95. case SMTP:
  96. source.Cfg = new(SMTPConfig)
  97. case PAM:
  98. source.Cfg = new(PAMConfig)
  99. default:
  100. panic("unrecognized login source type: " + com.ToStr(*val))
  101. }
  102. }
  103. }
  104. func (source *LoginSource) TypeName() string {
  105. return LoginNames[source.Type]
  106. }
  107. func (source *LoginSource) IsLDAP() bool {
  108. return source.Type == LDAP
  109. }
  110. func (source *LoginSource) IsDLDAP() bool {
  111. return source.Type == DLDAP
  112. }
  113. func (source *LoginSource) IsSMTP() bool {
  114. return source.Type == SMTP
  115. }
  116. func (source *LoginSource) IsPAM() bool {
  117. return source.Type == PAM
  118. }
  119. func (source *LoginSource) UseTLS() bool {
  120. switch source.Type {
  121. case LDAP, DLDAP:
  122. return source.LDAP().UseSSL
  123. case SMTP:
  124. return source.SMTP().TLS
  125. }
  126. return false
  127. }
  128. func (source *LoginSource) LDAP() *LDAPConfig {
  129. return source.Cfg.(*LDAPConfig)
  130. }
  131. func (source *LoginSource) SMTP() *SMTPConfig {
  132. return source.Cfg.(*SMTPConfig)
  133. }
  134. func (source *LoginSource) PAM() *PAMConfig {
  135. return source.Cfg.(*PAMConfig)
  136. }
  137. // CountLoginSources returns number of login sources.
  138. func CountLoginSources() int64 {
  139. count, _ := x.Count(new(LoginSource))
  140. return count
  141. }
  142. func CreateSource(source *LoginSource) error {
  143. _, err := x.Insert(source)
  144. return err
  145. }
  146. func GetAuths() ([]*LoginSource, error) {
  147. auths := make([]*LoginSource, 0, 5)
  148. return auths, x.Find(&auths)
  149. }
  150. func GetLoginSourceByID(id int64) (*LoginSource, error) {
  151. source := new(LoginSource)
  152. has, err := x.Id(id).Get(source)
  153. if err != nil {
  154. return nil, err
  155. } else if !has {
  156. return nil, ErrAuthenticationNotExist
  157. }
  158. return source, nil
  159. }
  160. func UpdateSource(source *LoginSource) error {
  161. _, err := x.Id(source.ID).AllCols().Update(source)
  162. return err
  163. }
  164. func DeleteSource(source *LoginSource) error {
  165. count, err := x.Count(&User{LoginSource: source.ID})
  166. if err != nil {
  167. return err
  168. } else if count > 0 {
  169. return ErrAuthenticationUserUsed
  170. }
  171. _, err = x.Id(source.ID).Delete(new(LoginSource))
  172. return err
  173. }
  174. // UserSignIn validates user name and password.
  175. func UserSignIn(uname, passwd string) (*User, error) {
  176. var u *User
  177. if strings.Contains(uname, "@") {
  178. u = &User{Email: uname}
  179. } else {
  180. u = &User{LowerName: strings.ToLower(uname)}
  181. }
  182. userExists, err := x.Get(u)
  183. if err != nil {
  184. return nil, err
  185. }
  186. if userExists {
  187. switch u.LoginType {
  188. case NOTYPE, PLAIN:
  189. if u.ValidatePassword(passwd) {
  190. return u, nil
  191. }
  192. return nil, ErrUserNotExist{u.Id, u.Name}
  193. default:
  194. var source LoginSource
  195. hasSource, err := x.Id(u.LoginSource).Get(&source)
  196. if err != nil {
  197. return nil, err
  198. } else if !hasSource {
  199. return nil, ErrLoginSourceNotExist
  200. }
  201. return ExternalUserLogin(u, u.LoginName, passwd, &source, false)
  202. }
  203. }
  204. var sources []LoginSource
  205. if err = x.UseBool().Find(&sources, &LoginSource{IsActived: true}); err != nil {
  206. return nil, err
  207. }
  208. for _, source := range sources {
  209. u, err := ExternalUserLogin(nil, uname, passwd, &source, source.AllowAutoRegister)
  210. if err == nil {
  211. return u, nil
  212. }
  213. log.Warn("Failed to login '%s' via '%s': %v", uname, source.Name, err)
  214. }
  215. return nil, ErrUserNotExist{u.Id, u.Name}
  216. }
  217. func ExternalUserLogin(u *User, name, passwd string, source *LoginSource, autoRegister bool) (*User, error) {
  218. if !source.IsActived {
  219. return nil, ErrLoginSourceNotActived
  220. }
  221. switch source.Type {
  222. case LDAP, DLDAP:
  223. return LoginUserLdapSource(u, name, passwd, source, autoRegister)
  224. case SMTP:
  225. return LoginUserSMTPSource(u, name, passwd, source.ID, source.Cfg.(*SMTPConfig), autoRegister)
  226. case PAM:
  227. return LoginUserPAMSource(u, name, passwd, source.ID, source.Cfg.(*PAMConfig), autoRegister)
  228. }
  229. return nil, ErrUnsupportedLoginType
  230. }
  231. // Query if name/passwd can login against the LDAP directory pool
  232. // Create a local user if success
  233. // Return the same LoginUserPlain semantic
  234. // FIXME: https://github.com/gogits/gogs/issues/672
  235. func LoginUserLdapSource(u *User, name, passwd string, source *LoginSource, autoRegister bool) (*User, error) {
  236. cfg := source.Cfg.(*LDAPConfig)
  237. directBind := (source.Type == DLDAP)
  238. fn, sn, mail, admin, logged := cfg.Ldapsource.SearchEntry(name, passwd, directBind)
  239. if !logged {
  240. // User not in LDAP, do nothing
  241. return nil, ErrUserNotExist{0, name}
  242. }
  243. if !autoRegister {
  244. return u, nil
  245. }
  246. // Fallback.
  247. if len(mail) == 0 {
  248. mail = fmt.Sprintf("%s@localhost", name)
  249. }
  250. u = &User{
  251. LowerName: strings.ToLower(name),
  252. Name: name,
  253. FullName: fn + " " + sn,
  254. LoginType: source.Type,
  255. LoginSource: source.ID,
  256. LoginName: name,
  257. Passwd: passwd,
  258. Email: mail,
  259. IsAdmin: admin,
  260. IsActive: true,
  261. }
  262. return u, CreateUser(u)
  263. }
  264. type loginAuth struct {
  265. username, password string
  266. }
  267. func LoginAuth(username, password string) smtp.Auth {
  268. return &loginAuth{username, password}
  269. }
  270. func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  271. return "LOGIN", []byte(a.username), nil
  272. }
  273. func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  274. if more {
  275. switch string(fromServer) {
  276. case "Username:":
  277. return []byte(a.username), nil
  278. case "Password:":
  279. return []byte(a.password), nil
  280. }
  281. }
  282. return nil, nil
  283. }
  284. const (
  285. SMTP_PLAIN = "PLAIN"
  286. SMTP_LOGIN = "LOGIN"
  287. )
  288. var SMTPAuths = []string{SMTP_PLAIN, SMTP_LOGIN}
  289. func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
  290. c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
  291. if err != nil {
  292. return err
  293. }
  294. defer c.Close()
  295. if err = c.Hello("gogs"); err != nil {
  296. return err
  297. }
  298. if cfg.TLS {
  299. if ok, _ := c.Extension("STARTTLS"); ok {
  300. if err = c.StartTLS(&tls.Config{
  301. InsecureSkipVerify: cfg.SkipVerify,
  302. ServerName: cfg.Host,
  303. }); err != nil {
  304. return err
  305. }
  306. } else {
  307. return errors.New("SMTP server unsupports TLS")
  308. }
  309. }
  310. if ok, _ := c.Extension("AUTH"); ok {
  311. if err = c.Auth(a); err != nil {
  312. return err
  313. }
  314. return nil
  315. }
  316. return ErrUnsupportedLoginType
  317. }
  318. // Query if name/passwd can login against the LDAP directory pool
  319. // Create a local user if success
  320. // Return the same LoginUserPlain semantic
  321. func LoginUserSMTPSource(u *User, name, passwd string, sourceId int64, cfg *SMTPConfig, autoRegister bool) (*User, error) {
  322. // Verify allowed domains.
  323. if len(cfg.AllowedDomains) > 0 {
  324. idx := strings.Index(name, "@")
  325. if idx == -1 {
  326. return nil, ErrUserNotExist{0, name}
  327. } else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), name[idx+1:]) {
  328. return nil, ErrUserNotExist{0, name}
  329. }
  330. }
  331. var auth smtp.Auth
  332. if cfg.Auth == SMTP_PLAIN {
  333. auth = smtp.PlainAuth("", name, passwd, cfg.Host)
  334. } else if cfg.Auth == SMTP_LOGIN {
  335. auth = LoginAuth(name, passwd)
  336. } else {
  337. return nil, errors.New("Unsupported SMTP auth type")
  338. }
  339. if err := SMTPAuth(auth, cfg); err != nil {
  340. if strings.Contains(err.Error(), "Username and Password not accepted") {
  341. return nil, ErrUserNotExist{0, name}
  342. }
  343. return nil, err
  344. }
  345. if !autoRegister {
  346. return u, nil
  347. }
  348. var loginName = name
  349. idx := strings.Index(name, "@")
  350. if idx > -1 {
  351. loginName = name[:idx]
  352. }
  353. // fake a local user creation
  354. u = &User{
  355. LowerName: strings.ToLower(loginName),
  356. Name: strings.ToLower(loginName),
  357. LoginType: SMTP,
  358. LoginSource: sourceId,
  359. LoginName: name,
  360. IsActive: true,
  361. Passwd: passwd,
  362. Email: name,
  363. }
  364. err := CreateUser(u)
  365. return u, err
  366. }
  367. // Query if name/passwd can login against PAM
  368. // Create a local user if success
  369. // Return the same LoginUserPlain semantic
  370. func LoginUserPAMSource(u *User, name, passwd string, sourceId int64, cfg *PAMConfig, autoRegister bool) (*User, error) {
  371. if err := pam.PAMAuth(cfg.ServiceName, name, passwd); err != nil {
  372. if strings.Contains(err.Error(), "Authentication failure") {
  373. return nil, ErrUserNotExist{u.Id, u.Name}
  374. }
  375. return nil, err
  376. }
  377. if !autoRegister {
  378. return u, nil
  379. }
  380. // fake a local user creation
  381. u = &User{
  382. LowerName: strings.ToLower(name),
  383. Name: strings.ToLower(name),
  384. LoginType: PAM,
  385. LoginSource: sourceId,
  386. LoginName: name,
  387. IsActive: true,
  388. Passwd: passwd,
  389. Email: name,
  390. }
  391. err := CreateUser(u)
  392. return u, err
  393. }