aboutsummaryrefslogtreecommitdiff
path: root/cmd/cmd.go
blob: f70eaaea344b83500ab98d52196bd364150411a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Command-line interface
package cmd

import (
  "compress/gzip"
  "encoding/json"
  "encoding/xml"
  "github.com/pablotron/cvez/cisa"
  "github.com/pablotron/cvez/cpedict"
  "github.com/pablotron/cvez/cpematch"
  "github.com/pablotron/cvez/datadir"
  "github.com/pablotron/cvez/dbstore"
  "github.com/pablotron/cvez/nvdmirror"
  "github.com/rs/zerolog"
  "github.com/rs/zerolog/log"
  nvd_feed "github.com/pablotron/cvez/feed"
  "os"
  "path/filepath"
  "strings"
  "time"
)

// Build query string.
func getSearchQuery(args []string) string {
  r := make([]string, len(args))

  for i, v := range(args) {
    r[i] = "\"" + strings.ReplaceAll(strings.ToLower(v), "\"", "\"\"") + "\""
  }

  return strings.Join(r, " AND ")
}

// Get database store.
func getDb() dbstore.DbStore {
  // build database path
  dbPath, err := datadir.Join("cvez.db")
  if err != nil {
    log.Error().Err(err).Msg("Join")
    os.Exit(-1)
  }

  // open database
  db, err := dbstore.Open(dbPath)
  if err != nil {
    log.Error().Err(err).Msg("Open")
    os.Exit(-1)
  }

  return db
}

// Get cache and cache directory path.
func getCache() (nvdmirror.JsonCache, string) {
  // get path to cache directory
  dir, err := datadir.CacheDir()
  if err != nil {
    log.Error().Err(err).Msg("CacheDir")
    os.Exit(-1)
  }

  // build cache file path
  cachePath := filepath.Join(dir, "cache.json.gz")

  // create cache
  cache, err := nvdmirror.NewJsonCache(cachePath)
  if err != nil {
    log.Error().Err(err).Msg("NewJsonCache")
    os.Exit(-1)
  }

  // return cache
  return cache, dir
}

// JSON encode data and write it to standard output
func jsonEncode(data interface{}) {
  // search for CVEs, write result
  e := json.NewEncoder(os.Stdout)
  if err := e.Encode(data); err != nil {
    log.Error().Err(err).Msg("Encode")
    os.Exit(-1)
  }
}

func init() {
  // set global logging options
  zerolog.TimeFieldFormat = time.RFC3339
  zerolog.SetGlobalLevel(zerolog.InfoLevel)

  rootCmd.AddCommand(searchCmd)
  rootCmd.AddCommand(cisaSearchCmd)
  rootCmd.AddCommand(cvssCmd)
  rootCmd.AddCommand(updateCmd)
}

// read gzipped file, pass reader to callback.
func gzOpen(path string, fn func(*gzip.Reader)) {
  f, err := os.Open(path)
  if err != nil {
    log.Error().Err(err).Msg("Open")
    os.Exit(-1)
  }
  defer f.Close()

  // create gzip reader
  r, err := gzip.NewReader(f)
  if err != nil {
    log.Error().Err(err).Msg("NewReader")
    os.Exit(-1)
  }

  fn(r)
}

// read gzipped json, pass decoder to callback
func jsonGzOpen(path string, fn func(*json.Decoder)) {
  gzOpen(path, func(r *gzip.Reader) {
    fn(json.NewDecoder(r))
  })
}

// read gzipped xml, pass decoder to callback
func xmlGzOpen(path string, fn func(*xml.Decoder)) {
  gzOpen(path, func(r *gzip.Reader) {
    fn(xml.NewDecoder(r))
  })
}

// Read feed from file.
func getFeed(path string) nvd_feed.Feed {
  var feed nvd_feed.Feed

  jsonGzOpen(path, func(d *json.Decoder) {
    if err := d.Decode(&feed); err != nil {
      log.Error().Err(err).Msg("Decode")
      os.Exit(-1)
    }
  })

  // return feed
  return feed
}

// Load CVE feeds in parallel.
func getFeeds(cacheDir string, updates []nvdmirror.Update) []nvd_feed.Feed {
  numFeeds := 0

  // load feeds in parallel
  ch := make(chan nvd_feed.Feed)
  for _, row := range(updates) {
    if row.Type == nvdmirror.UpdateCveYear {
      numFeeds += 1
      go func(path string) {
        ch <- getFeed(path)
      }(filepath.Join(cacheDir, row.Path))
    }
  }

  // collect feeds
  feeds := make([]nvd_feed.Feed, numFeeds)
  for i := 0; i < numFeeds; i++ {
    feeds[i] = <-ch
  }

  return feeds
}

// Read cpe matches from file.
func getCpeMatches(path string) cpematch.Matches {
  var matches cpematch.Matches

  jsonGzOpen(path, func(d *json.Decoder) {
    if err := d.Decode(&matches); err != nil {
      log.Error().Err(err).Msg("Decode")
      os.Exit(-1)
    }
  })

  // return matches
  return matches
}

// Read cpe dictionary from file.
func getCpeDict(path string) cpedict.Dictionary {
  var dict cpedict.Dictionary

  xmlGzOpen(path, func(d *xml.Decoder) {
    if err := d.Decode(&dict); err != nil {
      log.Error().Err(err).Msg("Decode")
      os.Exit(-1)
    }
  })

  // return dictionary
  return dict
}

// Read cisa catalog from file.
func getCisaCatalog(path string) cisa.Catalog {
  f, err := os.Open(path)
  if err != nil {
    log.Error().Err(err).Msg("Open")
    os.Exit(-1)
  }
  defer f.Close()

  // create decoder, decode catalog
  var cat cisa.Catalog
  d := json.NewDecoder(f)
  if err = d.Decode(&cat); err != nil {
    log.Error().Err(err).Msg("Decode")
    os.Exit(-1)
  }

  // return catalog
  return cat
}