gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. //go:build pam
  4. package pam
  5. import (
  6. "errors"
  7. "github.com/msteinert/pam"
  8. )
  9. // Supported is true when built with PAM
  10. var Supported = true
  11. // Auth pam auth service
  12. func Auth(serviceName, userName, passwd string) (string, error) {
  13. t, err := pam.StartFunc(serviceName, userName, func(s pam.Style, msg string) (string, error) {
  14. switch s {
  15. case pam.PromptEchoOff:
  16. return passwd, nil
  17. case pam.PromptEchoOn, pam.ErrorMsg, pam.TextInfo:
  18. return "", nil
  19. }
  20. return "", errors.New("Unrecognized PAM message style")
  21. })
  22. if err != nil {
  23. return "", err
  24. }
  25. if err = t.Authenticate(0); err != nil {
  26. return "", err
  27. }
  28. if err = t.AcctMgmt(0); err != nil {
  29. return "", err
  30. }
  31. // PAM login names might suffer transformations in the PAM stack.
  32. // We should take whatever the PAM stack returns for it.
  33. return t.GetItem(pam.User)
  34. }