|
@@ -0,0 +1,111 @@
|
|
|
|
+import sys
|
|
|
|
+import os
|
|
|
|
+import logging
|
|
|
|
+import configparser
|
|
|
|
+import datetime
|
|
|
|
+from pymongo import MongoClient
|
|
|
|
+import json
|
|
|
|
+
|
|
|
|
+##### GLOBAL VARIABLES #####
|
|
|
|
+logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
|
|
|
|
+
|
|
|
|
+# Increase the recursion limit by much to allow bs to parse large files ()
|
|
|
|
+sys.setrecursionlimit(6000)
|
|
|
|
+
|
|
|
|
+# Load config file as library
|
|
|
|
+config = configparser.ConfigParser()
|
|
|
|
+
|
|
|
|
+secondsperday = 86400
|
|
|
|
+now = datetime.datetime.now()
|
|
|
|
+verbosity = 1
|
|
|
|
+
|
|
|
|
+###### FUNCTIONS ######
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def load_state():
|
|
|
|
+ """
|
|
|
|
+ Load state, different from DBs in that we always need it.
|
|
|
|
+ Retrieves the cached state for current configuration.
|
|
|
|
+ :return: state , error number
|
|
|
|
+ """
|
|
|
|
+ cache = config['DIR']['cache_dir'] + 'state'
|
|
|
|
+ err = 0
|
|
|
|
+ state = dict()
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ with open(cache) as json_data:
|
|
|
|
+ state = json.load(json_data)
|
|
|
|
+ except FileNotFoundError:
|
|
|
|
+ # Load default state - start from the beginning
|
|
|
|
+ print('File not found in: '+config['DIR']['cache_dir'] + 'state')
|
|
|
|
+ print('Loading default state.')
|
|
|
|
+ state['cache_dir'] = cache
|
|
|
|
+ state['next_adv'] = 0
|
|
|
|
+ state['next_fsa'] = 0
|
|
|
|
+ state['Packages'] = ''
|
|
|
|
+ state['Sources'] = ''
|
|
|
|
+ state['Sha1Sums'] = ''
|
|
|
|
+ err += 1
|
|
|
|
+
|
|
|
|
+ return state, err
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def load_DBs():
|
|
|
|
+ cache = config['DIR']['cache_dir']
|
|
|
|
+
|
|
|
|
+ tables = ['dsatable', 'src2dsa', 'dsa2cve', 'cvetable', 'src2deps', 'src2month', 'src2sloccount', 'src2pop']
|
|
|
|
+ result = []
|
|
|
|
+
|
|
|
|
+ for i in range(0, len(tables)):
|
|
|
|
+ try:
|
|
|
|
+ with open(cache + tables[i]) as t:
|
|
|
|
+ result.append(json.load(t))
|
|
|
|
+ except (IOError, ValueError):
|
|
|
|
+ print('read cache '+tables[i]+' failed!! Maybe first run of the system?')
|
|
|
|
+ result.append("zero")
|
|
|
|
+
|
|
|
|
+ return tuple(result)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def aptsec_help():
|
|
|
|
+ """
|
|
|
|
+ :return:
|
|
|
|
+ """
|
|
|
|
+ print('See manual for correct usage!')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+'''
|
|
|
|
+TODO:
|
|
|
|
+ - add parameterized extensions
|
|
|
|
+ - for input config file
|
|
|
|
+ - for action
|
|
|
|
+ - vendor ( OPTIONAL )
|
|
|
|
+
|
|
|
|
+'''
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def __main__(configfile='config', vendorname='debian', action='help'):
|
|
|
|
+ # initialize structures for further computation
|
|
|
|
+ if not config.read(configfile):
|
|
|
|
+ raise IOError('Cannot open configuration file')
|
|
|
|
+ client = MongoClient()
|
|
|
|
+ dsatable = dict()
|
|
|
|
+ cve_db = client.cvedb
|
|
|
|
+
|
|
|
|
+ (state, err) = load_state()
|
|
|
|
+ state['vendor'] = vendorname
|
|
|
|
+
|
|
|
|
+ if action == 'update':
|
|
|
|
+ (dsatable, src2dsa, dsa2cve, cvetable, src2month, src2sloccount, src2pop, src2deps) = load_DBs()
|
|
|
|
+ elif action == 'status':
|
|
|
|
+ (dsatable, src2dsa, dsa2cve, cvetable, src2month, src2sloccount, src2pop, src2deps) = load_DBs()
|
|
|
|
+ #aptsec_status(sys.argv[2])
|
|
|
|
+ elif action == 'show':
|
|
|
|
+ (dsatable, src2dsa, dsa2cve, cvetable, src2month, src2sloccount, src2pop, src2deps) = load_DBs()
|
|
|
|
+ #src2sum = plot_all(src2month)
|
|
|
|
+ #save_DBs(dsatable, src2dsa, dsa2cve, cvetable, src2month, src2sloccount, src2pop, src2deps, src2sum)
|
|
|
|
+ else:
|
|
|
|
+ aptsec_help()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+__main__(action='update')
|