summaryrefslogtreecommitdiff
blob: 30f327246f83594d0f2a56b6639d0f25060b7a06 (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
#!/usr/bin/env python
# kernel-check -- Kernel security information
# Copyright 2009-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import getopt
import os
import sys
import time

import kernellib as lib


def main(argv):
    'Main function'

    try:
        opts, args = getopt.getopt(argv, 'd:fh:sv',
        ['delay=', 'force', 'help', 'skip', 'verbose'])
    except getopt.GetoptError:
        usage()
        return

    for opt, arg in opts:
        if opt in ('-d', '--delay'):
            if arg.isdigit():
                lib.DELAY = int(arg)
        elif opt in ('-f', '--force'):
            lib.FORCE = True
        elif opt in ('-h', '--help'):
            usage()
            return
        elif opt in ('-s', '--skip'):
            lib.SKIP = True
        elif opt in ('-v', '--verbose'):
            lib.VERBOSE = True

    for directory in lib.DIR:
        if not os.path.isdir(lib.DIR[directory]):
            os.makedirs(lib.DIR[directory])

    print 'Reading available genpatches...'
    try:
        read_patches = lib.read_genpatch_file(lib.DIR['out'])
    except IOError:
        read_patches = list()

    print 'Parsing genpatches from portage...'
    found_patches = lib.parse_genpatch_list(lib.PORTDIR)

    new_patches = 0
    for item in found_patches:
        if item not in read_patches:
            read_patches.append(item)
            new_patches += 1

    if (new_patches):
        lib.write_genpatch_file(lib.DIR['out'], read_patches)
        print 'Added %i new genpatches!' % new_patches

    print '\nReceiving the latest xml file from the nvd...'
    lib.receive_nvd_recent(lib.DIR['nvd'])

    if not lib.SKIP:
        print 'Receiving earlier xml files from the nvd...'
        lib.receive_nvd_all(lib.DIR['nvd'])

    print 'Creating the nvd dictionary...'
    nvd_dict = lib.parse_nvd_dict(lib.DIR['nvd'])

    print 'Receiving the kernel vulnerability list from bugzilla...'
    lib.receive_bugzilla_list(lib.DIR['tmp'])

    buglist = lib.parse_bugzilla_list(lib.DIR['tmp'])
    print 'Found %i kernel vulnerabilities!' % len(buglist)

    print '\nCreating the xml files...'

    created_files = 0
    for item in buglist:
        try:
            lib.receive_bugzilla_bug(lib.DIR['bug'], item)
            vul = lib.parse_bugzilla_dict(lib.DIR['bug'], item)
            vul = lib.search_nvd_dict(nvd_dict, vul)
            lib.write_cve_file(lib.DIR['out'], vul)

            created_files += 1
            time.sleep(lib.DELAY)

        except lib.InvalidWhiteboardError, e:
            print '\n[%s] Invalid whiteboard' % item
            print '%s' % e.value

        except lib.InvalidCveError, e:
            print '\n[%s] Invalid CVE' % item
            print '%s' % e.value

        except lib.NvdEntryError, e:
            print '\n[%s] No Nvd Entry' % item
            print '%s' % e.value

        except lib.CveDuplicateError, e:
            print '\n[%s] CVE Duplicate' % item
            print '%s' % e.value

    print '\nCreated %i xml files!' % created_files


def usage():
    'Prints the usage screen'

    print 'Usage: %s [OPTION]...' % sys.argv[0][:-3]
    print 'Kernel security information %s\r\n' % lib.VERSION
    print '  -d, --delay [ticks]  add delay to xml file creation'
    print '  -f, --force          force update of xml files'
    print '  -h, --help           display help information'
    print '  -s, --skip           skip update of prior nvd files'
    print '  -v, --verbose        display additional information'


if __name__ == '__main__':
    main(sys.argv[1:])