summaryrefslogtreecommitdiff
blob: 810eb0a50b35bef38548dfe611a9ccfe9ac0d1c6 (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
#! /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'

    DELAY = 0
    SKIP = False
    TREE = '/usr/portage'

    folder = {
        'bug'  : os.path.join('tmp', 'bug'),
        'nvd'  : os.path.join('tmp', 'nvd'),
        'temp' : 'tmp',
        'out'  : 'out'
    }

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

    for opt, arg in opts:
        if opt in ('-d', '--delay'):
            try:
                DELAY = int(arg)
            except ValueError:
                pass
        elif opt in ('-f', '--force'):
            lib.FORCE = True
        elif opt in ('-h', '--help'):
            usage()
        elif opt in ('-s', '--skip'):
            SKIP = True
        elif opt in ('-t', '--tree'):
            if os.access(os.path.dirname(arg) , os.W_OK) and os.path.isdir(arg):
                env['tree'] = arg
        elif opt in ('-v', '--verbose'):
            lib.VERBOSE = True

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

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

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

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

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

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

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

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

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

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

    print('\nCreating the xml files...')
    for item in buglist:
        lib.receive_bugzilla_bug(folder['bug'], item)
        vul = lib.parse_bugzilla_dict(folder['bug'], item)
        vul = lib.search_nvd_dict(nvd_dict, vul)
        lib.write_cve_file(folder['out'], vul)
        time.sleep(DELAY)


def usage():
    'Prints the usage screen'

    print 'Usage: %s [OPTION]...' % sys.argv[0][:-3]
    print 'Kernel security information\r\n'
    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 '  -t, --tree [dir]     set the portage path'
    print '  -s, --skip           skip update of earlier xml files'
    print '  -v, --verbose        display debugging information'
    print '\r\nVersion: %s' % lib.VERSION
    print 'Copyright (C) 2009  Bjoern Tropf <asymmail@googemail.com>'
    print 'Copyright (C) 2009  Robert Buchholz <rbu@gentoo.org>'
    sys.exit()


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