You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB

  1. // Copyright 2018 The Gitea 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 cmd
  5. import (
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/urfave/cli"
  12. )
  13. // CmdKeys represents the available keys sub-command
  14. var CmdKeys = cli.Command{
  15. Name: "keys",
  16. Usage: "This command queries the Gitea database to get the authorized command for a given ssh key fingerprint",
  17. Action: runKeys,
  18. Flags: []cli.Flag{
  19. cli.StringFlag{
  20. Name: "expected, e",
  21. Value: "git",
  22. Usage: "Expected user for whom provide key commands",
  23. },
  24. cli.StringFlag{
  25. Name: "username, u",
  26. Value: "",
  27. Usage: "Username trying to log in by SSH",
  28. },
  29. cli.StringFlag{
  30. Name: "type, t",
  31. Value: "",
  32. Usage: "Type of the SSH key provided to the SSH Server (requires content to be provided too)",
  33. },
  34. cli.StringFlag{
  35. Name: "content, k",
  36. Value: "",
  37. Usage: "Base64 encoded content of the SSH key provided to the SSH Server (requires type to be provided too)",
  38. },
  39. cli.StringFlag{
  40. Name: "config, c",
  41. Value: "custom/conf/app.ini",
  42. Usage: "Custom configuration file path",
  43. },
  44. },
  45. }
  46. func runKeys(c *cli.Context) error {
  47. if c.IsSet("config") {
  48. setting.CustomConf = c.String("config")
  49. }
  50. if !c.IsSet("username") {
  51. return errors.New("No username provided")
  52. }
  53. // Check username matches the expected username
  54. if strings.TrimSpace(c.String("username")) != strings.TrimSpace(c.String("expected")) {
  55. return nil
  56. }
  57. content := ""
  58. if c.IsSet("type") && c.IsSet("content") {
  59. content = fmt.Sprintf("%s %s", strings.TrimSpace(c.String("type")), strings.TrimSpace(c.String("content")))
  60. }
  61. if content == "" {
  62. return errors.New("No key type and content provided")
  63. }
  64. if err := initDBDisableConsole(true); err != nil {
  65. return err
  66. }
  67. publicKey, err := models.SearchPublicKeyByContent(content)
  68. if err != nil {
  69. return err
  70. }
  71. fmt.Println(publicKey.AuthorizedString())
  72. return nil
  73. }