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.

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