summaryrefslogtreecommitdiffstats
path: root/suricata/update/commands/listsources.py
blob: d35c3cd2cab36754d2669a9c2b262e5a0838ed5e (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
# Copyright (C) 2017 Open Information Security Foundation
#
# You can copy, redistribute or modify this Program under the terms of
# the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# 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
# version 2 along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

from __future__ import print_function

import logging

from suricata.update import config
from suricata.update import sources
from suricata.update import util
from suricata.update import exceptions

logger = logging.getLogger()

def register(parser):
    parser.add_argument("--free", action="store_true",
                        default=False, help="List all freely available sources")
    parser.add_argument("--enabled", action="store_true",
                        help="List all enabled sources")
    parser.add_argument("--all", action="store_true",
                        help="List all sources (including deprecated and obsolete)")
    parser.set_defaults(func=list_sources)

def list_sources():
    enabled = config.args().enabled or \
        config.args().subcommand == "list-enabled-sources"

    if enabled:
        found = False

        # First list sources from the main config.
        config_sources = config.get("sources")
        if config_sources:
            found = True
            print("From %s:" % (config.filename))
            for source in config_sources:
                print("  - %s" % (source))

        # And local files.
        local = config.get("local")
        if local:
            found = True
            print("Local files/directories:")
            for filename in local:
                print("  - %s" % (filename))

        enabled_sources = sources.get_enabled_sources()
        if enabled_sources:
            found = True
            print("Enabled sources:")
            for source in enabled_sources.values():
                print("  - %s" % (source["source"]))

        # If no enabled sources were found, log it.
        if not found:
            logger.warning("No enabled sources.")
        return 0

    free_only = config.args().free
    if not sources.source_index_exists(config):
        logger.warning("Source index does not exist, will use bundled one.")
        logger.warning("Please run suricata-update update-sources.")
        
    index = sources.load_source_index(config)
    for name, source in index.get_sources().items():
        is_not_free = source.get("subscribe-url")
        if free_only and is_not_free:
            continue
        if not config.args().all:
            if source.get("deprecated") is not None or \
               source.get("obsolete") is not None:
                continue
        print("%s: %s" % (util.bright_cyan("Name"), util.bright_magenta(name)))
        print("  %s: %s" % (
            util.bright_cyan("Vendor"), util.bright_magenta(source["vendor"])))
        print("  %s: %s" % (
            util.bright_cyan("Summary"), util.bright_magenta(source["summary"])))
        print("  %s: %s" % (
            util.bright_cyan("License"), util.bright_magenta(source["license"])))
        if "tags" in source:
            print("  %s: %s" % (
                util.bright_cyan("Tags"),
                util.bright_magenta(", ".join(source["tags"]))))
        if "replaces" in source:
            print("  %s: %s" % (
                util.bright_cyan("Replaces"),
                util.bright_magenta(", ".join(source["replaces"]))))
        if "parameters" in source:
            print("  %s: %s" % (
                util.bright_cyan("Parameters"),
                util.bright_magenta(", ".join(source["parameters"]))))
        if "subscribe-url" in source:
            print("  %s: %s" % (
                util.bright_cyan("Subscription"),
                util.bright_magenta(source["subscribe-url"])))
        if "deprecated" in source:
            print("  %s: %s" % (
                util.orange("Deprecated"),
                util.bright_magenta(source["deprecated"])))
        if "obsolete" in source:
            print("  %s: %s" % (
                util.orange("Obsolete"),
                util.bright_magenta(source["obsolete"])))