utils.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2020 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 conf
  5. import (
  6. "path/filepath"
  7. "strings"
  8. "github.com/pkg/errors"
  9. "gogs.io/gogs/internal/osutil"
  10. "gogs.io/gogs/internal/process"
  11. )
  12. // openSSHVersion returns string representation of OpenSSH version via command "ssh -V".
  13. func openSSHVersion() (string, error) {
  14. // NOTE: Somehow the version is printed to stderr.
  15. _, stderr, err := process.Exec("conf.openSSHVersion", "ssh", "-V")
  16. if err != nil {
  17. return "", errors.Wrap(err, stderr)
  18. }
  19. // Trim unused information, see https://github.com/gogs/gogs/issues/4507#issuecomment-305150441.
  20. v := strings.TrimRight(strings.Fields(stderr)[0], ",1234567890")
  21. v = strings.TrimSuffix(strings.TrimPrefix(v, "OpenSSH_"), "p")
  22. return v, nil
  23. }
  24. // ensureAbs prepends the WorkDir to the given path if it is not an absolute path.
  25. func ensureAbs(path string) string {
  26. if filepath.IsAbs(path) {
  27. return path
  28. }
  29. return filepath.Join(WorkDir(), path)
  30. }
  31. // CheckRunUser returns false if configured run user does not match actual user that
  32. // runs the app. The first return value is the actual user name. This check is ignored
  33. // under Windows since SSH remote login is not the main method to login on Windows.
  34. func CheckRunUser(runUser string) (string, bool) {
  35. if IsWindowsRuntime() {
  36. return "", true
  37. }
  38. currentUser := osutil.CurrentUsername()
  39. return currentUser, runUser == currentUser
  40. }