cfssl-scan.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/cloudflare/cfssl/cli"
  8. "github.com/cloudflare/cfssl/cli/scan"
  9. "github.com/cloudflare/cfssl/config"
  10. )
  11. // main defines the scan usage and registers all defined commands and flags.
  12. func main() {
  13. var scanFlagSet = flag.NewFlagSet("scan", flag.ExitOnError)
  14. var c cli.Config
  15. var usageText = `cfssl scan -- scan a host for issues
  16. Usage of scan:
  17. cfssl scan [-family regexp] [-scanner regexp] [-timeout duration] [-ip IPAddr] [-num-workers num] [-max-hosts num] [-csv hosts.csv] HOST+
  18. cfssl scan -list
  19. Arguments:
  20. HOST: Host(s) to scan (including port)
  21. Flags:
  22. `
  23. registerFlags(&c, scanFlagSet)
  24. scanFlagSet.Usage = func() {
  25. fmt.Fprintf(os.Stderr, "\t%s", usageText)
  26. for _, name := range scan.Command.Flags {
  27. if f := scanFlagSet.Lookup(name); f != nil {
  28. printDefaultValue(f)
  29. }
  30. }
  31. }
  32. args := os.Args[1:]
  33. scanFlagSet.Parse(args)
  34. args = scanFlagSet.Args()
  35. var err error
  36. c.CFG, err = config.LoadFile(c.ConfigFile)
  37. if c.ConfigFile != "" && err != nil {
  38. fmt.Fprintf(os.Stderr, "Failed to load config file: %v", err)
  39. }
  40. if err := scan.Command.Main(args, c); err != nil {
  41. fmt.Fprintln(os.Stderr, err)
  42. }
  43. }
  44. // printDefaultValue is a helper function to print out a user friendly
  45. // usage message of a flag. It's useful since we want to write customized
  46. // usage message on selected subsets of the global flag set. It is
  47. // borrowed from standard library source code. Since flag value type is
  48. // not exported, default string flag values are printed without
  49. // quotes. The only exception is the empty string, which is printed as "".
  50. func printDefaultValue(f *flag.Flag) {
  51. format := " -%s=%s: %s\n"
  52. if f.DefValue == "" {
  53. format = " -%s=%q: %s\n"
  54. }
  55. fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage)
  56. }
  57. // registerFlags defines all cfssl command flags and associates their values with variables.
  58. func registerFlags(c *cli.Config, f *flag.FlagSet) {
  59. f.BoolVar(&c.List, "list", false, "list possible scanners")
  60. f.StringVar(&c.Family, "family", "", "scanner family regular expression")
  61. f.StringVar(&c.Scanner, "scanner", "", "scanner regular expression")
  62. f.DurationVar(&c.Timeout, "timeout", 5*time.Minute, "duration (ns, us, ms, s, m, h) to scan each host before timing out")
  63. f.StringVar(&c.CSVFile, "csv", "", "file containing CSV of hosts")
  64. f.IntVar(&c.NumWorkers, "num-workers", 10, "number of workers to use for scan")
  65. f.IntVar(&c.MaxHosts, "max-hosts", 100, "maximum number of hosts to scan")
  66. f.StringVar(&c.IP, "ip", "", "remote server ip")
  67. f.StringVar(&c.CABundleFile, "ca-bundle", "", "path to root certificate store")
  68. }