summaryrefslogtreecommitdiff
blob: 1bce717d7ea1a74b3669a8f5a0eebd2d22ef4998 (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
# kernel-check.py -- Kernel security information
# Copyright (C) 2009  Bjoern Tropf <asymmail@googemail.com>
# Copyright (C) 2009  Robert Buchholz <rbu@gentoo.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import getopt
import portage.output
import sys
import os
import kernellib as lib

def main(argv):
    'Main function'

    info = portage.output.EOutput().einfo
    warn = portage.output.EOutput().ewarn
    error = portage.output.EOutput().eerror
    color = portage.output.colorize

    try:
        opts, args = getopt.getopt(argv, 'hnr:s:v', ['help', 'nocolor', 'report=', 'show=', 'verbose'])
    except getopt.GetoptError:
        usage()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
        elif opt in ('-n', '--nocolor'):
            portage.output.nocolor()
        elif opt in ('-r', '--report'):
            return
            # TODO: report(arg)
        elif opt in ('-s', '--show'):
            return
            # TODO: show_bugid(arg)
        elif opt in ('-v', '--verbose'):
            lib.VERBOSE = True

    print '>>> Gathering system information'

    kernel = lib.extract_version(os.uname()[2])
    if kernel:
        info('Kernel version: ' + color('GOOD', kernel['version'] + '-' + kernel['revision']))
        info('Kernel sources: ' + color('GOOD', kernel['source']))
    else:
        error('No kernel information found!')
        sys.exit()

    genpatch = lib.get_genpatch(lib.read_genpatch_file('out'), kernel)
    if genpatch:
        info('Integrated genpatch: ' + color('GOOD', genpatch['version'] + ' ' + genpatch['want']))
    else:
        warn('No genpatch information found!')

    arch = os.uname()[4]
    if arch:
        info('System architecture: ' + color('GOOD', arch))
    else:
        error('No system architecture found!')
        sys.exit()

    print '\n>>> Reading kernel vulnerabilities'

    cve = [345, 284, 274, 0, 4] #TODO: Implement + use dict!
    if cve:
        info(color('GOOD', str(cve[0])) + ' files read')
        info(color('GOOD', str(cve[1])) + ' match this system')
        info(color('GOOD', str(cve[2])) + ' haven been fixed')

        if cve[3]:
            warn(str(cve[3]) + ' could be fixed by upgrading')
        else:
            info('No vulnerability could be fixed by upgrading')

        if cve[4]:
            warn(str(cve[4]) + ' have not been fixed yet')
        else:
            info('No vulnerability have not been fixed yet')

    else:
        error('No vulnerability files found!')
        sys.exit()


    if not cve[3]:
        best = lib.best_version(kernel['source'])
        info('')
        info('These could be fixed by upgrading:')
        info('')
        #TODO: Print bugs
        '''200000 - This is a insidious kernel bug... - critical'''
        info('')
        info('To print information about a vulnerability try:')
        info('$ ' + sys.argv[0] + ' -i [id]')
        info('')
        info('Upgrading to the latest version')
        info('[' + color('GOOD', best)  + ']')
        info('is recommended!')
    else:
        info('')
        #TODO: 'Your kernel is secure'


def usage():
    'Prints the usage screen'

    print 'Usage: %s [OPTION]...' % sys.argv[0][:-3]
    print 'Kernel security information\r\n'
    print '  -h, --help           display help information'
    print '  -n, --nocolor        disable colors'
    print '  -r, --report [file]  create a security report'
    print '  -s, --show [bugid]   display information about a bug'
    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:])