aboutsummaryrefslogtreecommitdiffstats
path: root/testcases/SECTests/OpenSCAP.py
blob: 6f7510aca6917d76b64bb5d0ccba9c8a9307f167 (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
219
#!/usr/bin/python
#
# Copyright (c) 2016 Red Hat
# Luke Hinds (lhinds@redhat.com)
# This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 0.1: This script installs OpenSCAP on the remote host, and scans the
# nominated node. Post scan a report is downloaded and if '--clean' is passed
# all trace of the scan is removed from the remote system.

import os
import datetime
import argparse

__version__ = 0.1
__author__ = 'Luke Hinds (lhinds@redhat.com)'
__url__ = 'https://wiki.opnfv.org/display/functest/Functest+Security'

'''
Example Run:
    python ./OpenSCAP.py --host 192.168.0.24 --port 22 --user root --password
    p6ssw0rd oval --secpolicy
    /usr/share/xml/scap/ssg/content/ssg-rhel7-oval.xml --report report.html
    --results results.xml

'''

# Variables needed..
pwd = os.getcwd()
oscap = '/bin/oscap'
currenttime = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')

# Set up the main parser
parser = argparse.ArgumentParser(description='OpenSCAP Python Scanner')

# Main args
# Todo  add required = True
parser.add_argument('--user',
                    action='store',
                    dest='user',
                    help='user')
parser.add_argument('--password',
                    action='store',
                    dest='password',
                    help='Password')
parser.add_argument('--host',
                    action='store',
                    dest='host',
                    help='host',
                    required=True)
parser.add_argument('--port',
                    action='store',
                    dest='port"',
                    help='port',
                   required=True)
parser.add_argument('--dist',
                    action='store',
                    dest='dist',
                    help='Distribution')
parser.add_argument('--clean',
                    action='store_true',
                    dest='clean',
                    help='Clean all files from host')

# And the subparser
subparsers = parser.add_subparsers(
    title='subcommands',
    description='valid subcommands',
    help='additional help')


parser_xccdf = subparsers.add_parser('xccdf')
parser_xccdf.set_defaults(which='xccdf')

parser_oval = subparsers.add_parser('oval')
parser_oval.set_defaults(which='oval')

parser_oval_collect = subparsers.add_parser('oval-collect')
parser_oval_collect.set_defaults(which='oval-collect')

parser_xccdf.add_argument(
    '--profile',
    action='store',
    dest='profile',
    help='xccdf profile')

parser_oval.add_argument(
    '--results',
    action='store',
    dest='results',
    help='Report name (inc extension (.html)')

parser_oval.add_argument(
    '--report',
    action='store',
    dest='report',
    help='Report name (inc extension (.html)')

parser_oval.add_argument(
    '--secpolicy',
    action='store',
    dest='secpolicy',
    help='Security Policy')

parserout = parser.parse_args()
args = vars(parser.parse_args())


def createfiles():
    import connect
    global tmpdir
    localpath = os.getcwd() + '/scripts/createfiles.py'
    remotepath = '/tmp/createfiles.py'
    com = 'python /tmp/createfiles.py'
    connect = connect.connectionManager(parserout.host,
                                        parserout.user,
                                        parserout.password,
                                        localpath,
                                        remotepath,
                                        com)
    tmpdir = connect.remotescript()


def install_pkg():
    import connect
    com = 'yum -y install openscap-scanner scap-security-guide'
    connect = connect.connectionManager(parserout.host,
                                        parserout.user,
                                        parserout.password,
                                        com)
    install_pkg = connect.remotecmd()
    print install_pkg


def run_scanner():
    import connect

    if args['which'] == 'xccdf':
        print 'xccdf'
        com = '{0} xccdf eval'.format(oscap)
        connect = connect.connectionManager(parserout.host,
                                            parserout.user,
                                            parserout.password,
                                            com)
    elif args['which'] == 'oval':
        com = '{0} oval eval --results {1}/{2} --report {1}/{3} {4}'.format(oscap,
                                                                    tmpdir.rstrip(),
                                                                    parserout.results,
                                                                    parserout.report,
                                                                    parserout.secpolicy)
        connect = connect.connectionManager(parserout.host,
                                            parserout.user,
                                            parserout.password,
                                            com)
        run_tool = connect.remotecmd()
    else:
        com = '{0} oval-collect '.format(oscap)
        connect = connect.connectionManager(parserout.host,
                                            parserout.user,
                                            parserout.password,
                                            com)
        run_tool = connect.remotecmd()


def post_tasks():
    import connect
    dl_folder = os.path.join(os.getcwd(), parserout.host + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
    os.mkdir(dl_folder, 0755)
    reportfile = '{0}/{1}'.format(tmpdir.rstrip(), parserout.report)
    connect = connect.connectionManager(parserout.host,
                                        parserout.user,
                                        parserout.password,
                                        dl_folder,
                                        reportfile,
                                        parserout.report,
                                        parserout.results)
    run_tool = connect.download_reports()


def removepkg():
    import connect
    com = 'yum -y remove openscap-scanner scap-security-guide'
    connect = connect.connectionManager(parserout.host,
                                        parserout.user,
                                        parserout.password,
                                        com)
    yumremove = connect.remotecmd()
    print yumremove


def cleandir():
    import connect
    com = 'rm -r {0}'.format(tmpdir.rstrip())
    connect = connect.connectionManager(parserout.host,
                                        parserout.user,
                                        parserout.password,
                                        com)
    deldir = connect.remotecmd()


if __name__ == '__main__':
    print 'Creating temp file structure...\n'
    createfiles()
    print 'Install OpenSCAP scanner...\n'
    install_pkg()
    print 'Running scan...\n'
    run_scanner()
    print 'Post installation tasks...\n'
    post_tasks()
    if parserout.clean:
        print 'Cleaning down environment...\n'
        print 'Removing OpenSCAP...\n'
        removepkg()
        print 'Deleting tmp file and reports (remote)...\n'
        cleandir()
="p">(controller->tx_channel[ch_num]) : &(controller->rx_channel[ch_num]) ; /* Check if channel is already used. */ if (ux500_channel->is_allocated) return NULL; ux500_channel->hw_ep = hw_ep; ux500_channel->is_allocated = 1; dev_dbg(musb->controller, "hw_ep=%d, is_tx=0x%x, channel=%d\n", hw_ep->epnum, is_tx, ch_num); return &(ux500_channel->channel); } static void ux500_dma_channel_release(struct dma_channel *channel) { struct ux500_dma_channel *ux500_channel = channel->private_data; struct musb *musb = ux500_channel->controller->private_data; dev_dbg(musb->controller, "channel=%d\n", ux500_channel->ch_num); if (ux500_channel->is_allocated) { ux500_channel->is_allocated = 0; channel->status = MUSB_DMA_STATUS_FREE; channel->actual_len = 0; } } static int ux500_dma_is_compatible(struct dma_channel *channel, u16 maxpacket, void *buf, u32 length) { if ((maxpacket & 0x3) || ((unsigned long int) buf & 0x3) || (length < 512) || (length & 0x3)) return false; else return true; } static int ux500_dma_channel_program(struct dma_channel *channel, u16 packet_sz, u8 mode, dma_addr_t dma_addr, u32 len) { int ret; BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN || channel->status == MUSB_DMA_STATUS_BUSY); if (!ux500_dma_is_compatible(channel, packet_sz, (void *)dma_addr, len)) return false; channel->status = MUSB_DMA_STATUS_BUSY; channel->actual_len = 0; ret = ux500_configure_channel(channel, packet_sz, mode, dma_addr, len); if (!ret) channel->status = MUSB_DMA_STATUS_FREE; return ret; } static int ux500_dma_channel_abort(struct dma_channel *channel) { struct ux500_dma_channel *ux500_channel = channel->private_data; struct ux500_dma_controller *controller = ux500_channel->controller; struct musb *musb = controller->private_data; void __iomem *epio = musb->endpoints[ux500_channel->hw_ep->epnum].regs; u16 csr; dev_dbg(musb->controller, "channel=%d, is_tx=%d\n", ux500_channel->ch_num, ux500_channel->is_tx); if (channel->status == MUSB_DMA_STATUS_BUSY) { if (ux500_channel->is_tx) { csr = musb_readw(epio, MUSB_TXCSR); csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE); musb_writew(epio, MUSB_TXCSR, csr); } else { csr = musb_readw(epio, MUSB_RXCSR); csr &= ~(MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB | MUSB_RXCSR_DMAMODE); musb_writew(epio, MUSB_RXCSR, csr); } dmaengine_terminate_all(ux500_channel->dma_chan); channel->status = MUSB_DMA_STATUS_FREE; } return 0; } static void ux500_dma_controller_stop(struct ux500_dma_controller *controller) { struct ux500_dma_channel *ux500_channel; struct dma_channel *channel; u8 ch_num; for (ch_num = 0; ch_num < UX500_MUSB_DMA_NUM_RX_TX_CHANNELS; ch_num++) { channel = &controller->rx_channel[ch_num].channel; ux500_channel = channel->private_data; ux500_dma_channel_release(channel); if (ux500_channel->dma_chan) dma_release_channel(ux500_channel->dma_chan); } for (ch_num = 0; ch_num < UX500_MUSB_DMA_NUM_RX_TX_CHANNELS; ch_num++) { channel = &controller->tx_channel[ch_num].channel; ux500_channel = channel->private_data; ux500_dma_channel_release(channel); if (ux500_channel->dma_chan) dma_release_channel(ux500_channel->dma_chan); } } static int ux500_dma_controller_start(struct ux500_dma_controller *controller) { struct ux500_dma_channel *ux500_channel = NULL; struct musb *musb = controller->private_data; struct device *dev = musb->controller; struct musb_hdrc_platform_data *plat = dev_get_platdata(dev); struct ux500_musb_board_data *data; struct dma_channel *dma_channel = NULL; char **chan_names; u32 ch_num; u8 dir; u8 is_tx = 0; void **param_array; struct ux500_dma_channel *channel_array; dma_cap_mask_t mask; if (!plat) { dev_err(musb->controller, "No platform data\n"); return -EINVAL; } data = plat->board_data; dma_cap_zero(mask); dma_cap_set(DMA_SLAVE, mask); /* Prepare the loop for RX channels */ channel_array = controller->rx_channel; param_array = data ? data->dma_rx_param_array : NULL; chan_names = (char **)iep_chan_names; for (dir = 0; dir < 2; dir++) { for (ch_num = 0; ch_num < UX500_MUSB_DMA_NUM_RX_TX_CHANNELS; ch_num++) { ux500_channel = &channel_array[ch_num]; ux500_channel->controller = controller; ux500_channel->ch_num = ch_num; ux500_channel->is_tx = is_tx; dma_channel = &(ux500_channel->channel); dma_channel->private_data = ux500_channel; dma_channel->status = MUSB_DMA_STATUS_FREE; dma_channel->max_len = SZ_16M; ux500_channel->dma_chan = dma_request_slave_channel(dev, chan_names[ch_num]); if (!ux500_channel->dma_chan) ux500_channel->dma_chan = dma_request_channel(mask, data ? data->dma_filter : NULL, param_array ? param_array[ch_num] : NULL); if (!ux500_channel->dma_chan) { ERR("Dma pipe allocation error dir=%d ch=%d\n", dir, ch_num); /* Release already allocated channels */ ux500_dma_controller_stop(controller); return -EBUSY; } } /* Prepare the loop for TX channels */ channel_array = controller->tx_channel; param_array = data ? data->dma_tx_param_array : NULL; chan_names = (char **)oep_chan_names; is_tx = 1; } return 0; } void ux500_dma_controller_destroy(struct dma_controller *c) { struct ux500_dma_controller *controller = container_of(c, struct ux500_dma_controller, controller); ux500_dma_controller_stop(controller); kfree(controller); } EXPORT_SYMBOL_GPL(ux500_dma_controller_destroy); struct dma_controller * ux500_dma_controller_create(struct musb *musb, void __iomem *base) { struct ux500_dma_controller *controller; struct platform_device *pdev = to_platform_device(musb->controller); struct resource *iomem; int ret; controller = kzalloc(sizeof(*controller), GFP_KERNEL); if (!controller) goto kzalloc_fail; controller->private_data = musb; /* Save physical address for DMA controller. */ iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!iomem) { dev_err(musb->controller, "no memory resource defined\n"); goto plat_get_fail; } controller->phy_base = (dma_addr_t) iomem->start; controller->controller.channel_alloc = ux500_dma_channel_allocate; controller->controller.channel_release = ux500_dma_channel_release; controller->controller.channel_program = ux500_dma_channel_program; controller->controller.channel_abort = ux500_dma_channel_abort; controller->controller.is_compatible = ux500_dma_is_compatible; ret = ux500_dma_controller_start(controller); if (ret) goto plat_get_fail; return &controller->controller; plat_get_fail: kfree(controller); kzalloc_fail: return NULL; } EXPORT_SYMBOL_GPL(ux500_dma_controller_create);