from suricata.update.commands import addsource
from suricata.update.commands import listsources
from suricata.update.commands import updatesources
+from suricata.update.commands import enablesource
--- /dev/null
+# 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 os
+import logging
+
+import yaml
+
+from suricata.update import sources
+
+logger = logging.getLogger()
+
+def register(parser):
+ parser.add_argument("name")
+ parser.add_argument("params", nargs="*", metavar="param=val")
+ parser.set_defaults(func=enable_source)
+
+def enable_source(config):
+ name = config.args.name
+
+ # Check if source is already enabled.
+ enabled_source_filename = sources.get_enabled_source_filename(name)
+ if os.path.exists(enabled_source_filename):
+ logger.error("The source %s is already enabled.", name)
+ return 1
+
+ # First check if this source was previous disabled and then just
+ # re-enable it.
+ disabled_source_filename = sources.get_disabled_source_filename(name)
+ if os.path.exists(disabled_source_filename):
+ logger.info("Re-enabling previous disabled source for %s.", name)
+ os.rename(disabled_source_filename, enabled_source_filename)
+ return 0
+
+ if not os.path.exists(sources.get_index_filename(config)):
+ logger.warning(
+ "Source index does not exist, "
+ "try running suricata-update update-sources.")
+ return 1
+
+ source_index = sources.load_source_index(config)
+
+ if not name in source_index.get_sources():
+ logger.error("Unknown source: %s", name)
+ return 1
+
+ # Parse key=val options.
+ opts = {}
+ for param in config.args.params:
+ key, val = param.split("=", 1)
+ opts[key] = val
+
+ source = source_index.get_sources()[name]
+
+ if "subscribe-url" in source:
+ print("The source %s requires a subscription. Subscribe here:" % (name))
+ print(" %s" % source["subscribe-url"])
+
+ params = {}
+ if "parameters" in source:
+ for param in source["parameters"]:
+ if param in opts:
+ params[param] = opts[param]
+ else:
+ prompt = source["parameters"][param]["prompt"]
+ while True:
+ r = raw_input("%s (%s): " % (prompt, param))
+ r = r.strip()
+ if r:
+ break
+ params[param] = r.strip()
+ new_source = sources.SourceConfiguration(name, params=params).dict()
+
+ if not os.path.exists(sources.get_source_directory()):
+ try:
+ logger.info("Creating directory %s", sources.get_source_directory())
+ os.makedirs(sources.get_source_directory())
+ except Exception as err:
+ logger.error("Failed to create directory %s: %s",
+ sources.get_source_directory(), err)
+ return 1
+
+ filename = os.path.join(
+ sources.get_source_directory(), "%s.yaml" % (sources.safe_filename(name)))
+ logger.info("Writing %s", filename)
+ with open(filename, "w") as fileobj:
+ fileobj.write(yaml.dump(new_source, default_flow_style=False))
"remove-source", help="Remove a source", parents=[common_parser])
remove_source_parser.add_argument("name")
- enable_source_parser = subparsers.add_parser(
- "enable-source", parents=[common_parser])
- enable_source_parser.add_argument("name")
- enable_source_parser.add_argument("params", nargs="*", metavar="param=val")
-
commands.listsources.register(subparsers.add_parser(
"list-sources", parents=[common_parser]))
commands.listenabledsources.register(subparsers.add_parser(
"add-source", parents=[common_parser]))
commands.updatesources.register(subparsers.add_parser(
"update-sources", parents=[common_parser]))
+ commands.enablesource.register(subparsers.add_parser(
+ "enable-source", parents=[common_parser]))
args = parser.parse_args()
return 1
if args.subcommand:
- if args.subcommand == "enable-source":
- return sources.enable_source(config)
- elif args.subcommand == "disable-source":
+ if args.subcommand == "disable-source":
return sources.disable_source(config)
elif args.subcommand == "remove-source":
return sources.remove_source(config)
return sources
-def load_sources(config):
- sources_cache_filename = os.path.join(
- config.get_cache_dir(), SOURCE_INDEX_FILENAME)
- if os.path.exists(sources_cache_filename):
- index = yaml.load(open(sources_cache_filename).read())
- return index["sources"]
- return {}
-
-def enable_source(config):
- name = config.args.name
-
- # Check if source is already enabled.
- enabled_source_filename = os.path.join(
- get_source_directory(), "%s.yaml" % (safe_filename(name)))
- if os.path.exists(enabled_source_filename):
- logger.error("The source %s is already enabled.", name)
- return 1
-
- # First check if this source was previous disabled and then just
- # re-enable it.
- disabled_source_filename = os.path.join(
- get_source_directory(), "%s.yaml.disabled" % (safe_filename(name)))
- if os.path.exists(disabled_source_filename):
- logger.info("Re-enabling previous disabled source for %s.", name)
- os.rename(disabled_source_filename, enabled_source_filename)
- return 0
-
- if not os.path.exists(get_index_filename(config)):
- logger.warning(
- "Source index does not exist, "
- "try running suricata-update update-sources.")
- return 1
-
- sources = load_sources(config)
- if not config.args.name in sources:
- logger.error("Unknown source: %s", config.args.name)
- return 1
-
- # Parse key=val options.
- opts = {}
- for param in config.args.params:
- key, val = param.split("=", 1)
- opts[key] = val
-
- source = sources[config.args.name]
-
- if "subscribe-url" in source:
- print("The source %s requires a subscription. Subscribe here:" % (name))
- print(" %s" % source["subscribe-url"])
-
- params = {}
- if "parameters" in source:
- for param in source["parameters"]:
- if param in opts:
- params[param] = opts[param]
- else:
- prompt = source["parameters"][param]["prompt"]
- while True:
- r = raw_input("%s (%s): " % (prompt, param))
- r = r.strip()
- if r:
- break
- params[param] = r.strip()
- new_source = SourceConfiguration(name, params=params).dict()
-
- if not os.path.exists(get_source_directory()):
- try:
- logger.info("Creating directory %s", get_source_directory())
- os.makedirs(get_source_directory())
- except Exception as err:
- logger.error("Failed to create directory %s: %s",
- get_source_directory(), err)
- return 1
-
- filename = os.path.join(
- get_source_directory(), "%s.yaml" % (safe_filename(name)))
- logger.info("Writing %s", filename)
- with open(filename, "w") as fileobj:
- fileobj.write(yaml.dump(new_source, default_flow_style=False))
-
def disable_source(config):
name = config.args.name
filename = os.path.join(get_source_directory(), "%s.yaml" % (