2
0

utils.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // cleanUpOpenSSHVersion cleans up the raw output of "ssh -V" and returns a clean version string.
  13. func cleanUpOpenSSHVersion(raw string) string {
  14. v := strings.TrimRight(strings.Fields(raw)[0], ",1234567890")
  15. v = strings.TrimSuffix(strings.TrimPrefix(v, "OpenSSH_"), "p")
  16. return v
  17. }
  18. // openSSHVersion returns string representation of OpenSSH version via command "ssh -V".
  19. func openSSHVersion() (string, error) {
  20. // NOTE: Somehow the version is printed to stderr.
  21. _, stderr, err := process.Exec("conf.openSSHVersion", "ssh", "-V")
  22. if err != nil {
  23. return "", errors.Wrap(err, stderr)
  24. }
  25. return cleanUpOpenSSHVersion(stderr), nil
  26. }
  27. // ensureAbs prepends the WorkDir to the given path if it is not an absolute path.
  28. func ensureAbs(path string) string {
  29. if filepath.IsAbs(path) {
  30. return path
  31. }
  32. return filepath.Join(WorkDir(), path)
  33. }
  34. // CheckRunUser returns false if configured run user does not match actual user that
  35. // runs the app. The first return value is the actual user name. This check is ignored
  36. // under Windows since SSH remote login is not the main method to login on Windows.
  37. func CheckRunUser(runUser string) (string, bool) {
  38. if IsWindowsRuntime() {
  39. return "", true
  40. }
  41. currentUser := osutil.CurrentUsername()
  42. return currentUser, runUser == currentUser
  43. }