def handle(self, *args, **options): """ Script Execution. """ reset_config_option = options.get(RESET_CONFIG_VARIABLE) create_config_option = options.get(CREATE_CONFIG_VARIABLE) reinsert_config_option = options.get(REINSERT_CONFIG_VARIABLE) if reset_config_option == True: force_reset_config() if create_config_option == True: create_config_if_not_exist() if len(args) == 2 or reinsert_config_option == True: # Get the config crits_config = create_config_if_not_exist() if len(args) == 2: attr = args[0] value = args[1] # Check to make sure the attribute is a known attribute if set_config_attribute(crits_config, attr, value) == False: raise CE('CRITs has no configuration option %s.' % attr) # Save the config to the database if reinsert_config_option == True: print "Performing a reinsert of the CRITs configuration." reinsert_config(crits_config) else: print "Saving CRITs configuration." crits_config.save() elif reset_config_option == False and create_config_option == False and reinsert_config_option == False: raise CE( 'setconfig: Invalid Parameters! Only takes two ' + 'arguments or --create_config_option or --reset_config_option flags.' )
def set_config_attribute(crits_config, attr, value): """ Sets the value for the attribute for the input CRITsConfig. If the attribute doesn't exist then nothing happens. Args: crits_config: The CRITsConfig to copy attr: The attribute to set value: The value to set for the input attribute Returns: Returns true if the attribute was able to be set. False otherwise. """ is_successful = False if hasattr(crits_config, attr): if attr in ("enable_api", "create_unknown_user", "debug", "ldap_auth", "ldap_tls", "remote_user", "secure_cookie", "ldap_update_on_login", "query_caching", "totp", "crits_email_end_tag"): if value in ('True', 'true', 'yes', '1'): value = True elif value in ('False', 'false', 'no', '0'): value = False else: raise CE('%s is a boolean True/False.' % attr) if attr in ('depth_max', 'invalid_login_attempts', 'rel_max', 'session_timeout', 'total_max'): try: value = int(value) except: raise CE('%s is an Integer' % attr) if attr == "log_level": if not value in ('INFO', 'WARN', 'DEBUG'): raise CE('log_level must be INFO, WARN, or DEBUG.') if attr in ('rar_path', 'temp_dir', 'zip7_path', 'log_directory'): if not os.path.exists(value): raise CE('Not a valid path: %s' % value) if attr == "allowed_hosts": li = value.split(',') value = [l.strip() for l in li] if attr == "service_dirs": li = value.split(',') value = [l.strip() for l in li] for v in value: if not os.path.exists(v): raise CE('Not a valid path: %s' % v) if attr == "service_model": if value not in ('process', 'thread', 'local'): raise CE('service_model must be process, thread, or local') print "Setting [" + str(attr) + "] to a value of [" + str(value) + "]" setattr(crits_config, attr, value) is_successful = True return is_successful
def handle(self, *args, **options): """ Script Execution. """ # Keep state in case we need to exit the test early fail = False # Test python imports imports = [ 'anyjson', 'bson', 'crits', 'dateutil', 'gridfs', 'importlib', 'lxml', 'M2Crypto', 'magic', 'mongoengine', 'nids', 'pymongo', 'pydeep', 'pyparsing', 'requests', 'yaml', ] for i in imports: try: __import__(i) except ImportError: print CE('Could not import %s. Is it installed properly?' % i) # Required to continue script, so totally fail if these # are missing. if i in ('mongoengine', 'crits', 'pymongo'): fail = True if fail: raise CE('Critical python modules missing. Cannot continue.') sys.exit(1) # Check for binaries binaries = ['7z', 'mongod', 'mongos', 'unrar', 'upx'] cmd = "where" if platform.system() == "Windows" else "which" for i in binaries: try: op = subprocess.Popen([cmd, i], stdout=subprocess.PIPE) op.communicate() if op.returncode: print CE('Could not find binary %s. Is it installed properly?' % i) except: print CE('Could not find binary %s. Is it installed properly?' % i) # Required to continue script, so totally fail if these # are missing. if i in ('mongod', 'mongos'): fail = True if fail: raise CE('Critical binaries missing. Cannot continue.') sys.exit(1) # Check database is running and can connect to it try: import mongoengine if settings.MONGO_USER: mongoengine.connect(settings.MONGO_DATABASE, host=settings.MONGO_HOST, port=settings.MONGO_PORT, read_preference=settings.MONGO_READ_PREFERENCE, ssl=settings.MONGO_SSL, username=settings.MONGO_USER, password=settings.MONGO_PASSWORD) else: mongoengine.connect(settings.MONGO_DATABASE, host=settings.MONGO_HOST, port=settings.MONGO_PORT, read_preference=settings.MONGO_READ_PREFERENCE, ssl=settings.MONGO_SSL) except: raise CE('Could not connect to Mongo Database. Is it running' ' and is CRITs configured to connect to it properly?') # Check Configurations import crits config = crits.config.config.CRITsConfig.objects().first() ld = config.log_directory if not os.path.exists(ld) and len(ld) > 0: print CE('Configured CRITs log directory does not exist: %s' % ld) td = config.temp_dir if not os.path.exists(td): print CE('Configured CRITs temp directory does not exist: %s' % td) rp = config.rar_path if not os.path.exists(rp): print CE('Configured CRITs rar path does not exist: %s' % rp) zp = config.zip7_path if not os.path.exists(zp): print CE('Configured CRITs zip path does not exist: %s' % zp) for i in config.service_dirs: if not os.path.exists(i): print CE('Configured CRITs service directory does not exist: %s' % i) print ("Installation check completed. Please fix any above errors before" " attempting to use CRITs!")