def get_output_path(self): """ Get path to output directory and create if needed """ output_path = None if self.interactive: output_path = input("Enter path to store generated config " "[{}]: ".format( os.path.abspath(self.args.output))) output_path = output_path or self.args.output try: util.try_make_dir(output_path) except OSError: logger.exception( "Problem encountered when trying to create the " "output directory %s.", os.path.abspath(output_path)) else: logger.debug("Created the output directory '%s'.", os.path.abspath(output_path)) # The output directory should be empty to avoid having conflict keys # or config files. if not util.is_directory_empty(output_path): logger.error( "The specified output directory '%s' is not empty. Please " "delete any files and folders or specify another output " "directory.", output_path) sys.exit(1) return output_path
def write_v2_instance_files(self): for i, (instance_address, instance_key) in enumerate(self.instances): # Create a numbered directory for instance instance_dir = os.path.join(self.output_path, '{}{}'.format(self.tag, i + 1)) instance_key_dir = os.path.join(instance_dir, instance_address) util.try_make_dir(instance_key_dir) os.chmod(instance_key_dir, 1472) # chmod 2700 in decimal instance_key_file = os.path.join(instance_key_dir, 'private_key') with open(instance_key_file, "wb") as key_file: os.chmod(instance_key_file, 384) # chmod 0600 in decimal key_file.write(instance_key.exportKey()) logger.debug( "Successfully wrote key for instance %s.onion to " "file.", instance_address) # Write torrc file for each instance instance_torrc = os.path.join(instance_dir, 'instance_torrc') instance_torrc_template = pkg_resources.resource_string( __name__, 'data/torrc-instance-v2') with open(instance_torrc, "w") as torrc_file: torrc_file.write(instance_torrc_template.decode('utf-8')) # The ./ relative path prevents Tor from raising relative # path warnings. The relative path may need to be edited manual # to work on Windows systems. torrc_file.write( u"HiddenServiceDir {}\n".format(instance_address)) torrc_file.write(u"{}\n".format(self.torrc_port_line))
def write_master_key_to_disk(self): # Finished reading input, starting to write config files. util.try_make_dir(self.master_dir) master_key_file = os.path.join(self.master_dir, '{}.key'.format(self.master_onion_address)) with open(master_key_file, "wb") as key_file: os.chmod(master_key_file, 384) # chmod 0600 in decimal if self.hs_version == 'v2': master_passphrase = self.get_master_key_passphrase() key_file.write(self.master_key.exportKey(passphrase=master_passphrase)) elif self.hs_version == 'v3' and self.v3_loaded_key_from_file: # If we loaded a v3 key from a file, copy the file directly # (see loaded_key_from_file comments). shutil.copyfile(self.master_key_path, master_key_file) logger.info("Copied v3 master key from %s to %s.", self.master_key_path, master_key_file) else: # If we generated our own v3 master key, write it to file. If # 'master_key' does not exist, it means that we are loading it # from a file, so we dont need to write it to disk. master_key_formatted = self.master_key.private_bytes(encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()) key_file.write(master_key_formatted) logger.debug("Successfully wrote master key to file %s.", os.path.abspath(master_key_file))
def get_output_path(self): """ Get path to output directory and create if needed """ output_path = None if self.interactive: output_path = input("Enter path to store generated config " "[{}]: ".format( os.path.abspath(self.args.output))) output_path = output_path or self.args.output try: util.try_make_dir(output_path) except OSError: logger.exception( "Problem encountered when trying to create the " "output directory %s.", os.path.abspath(output_path)) sys.exit(1) else: logger.debug("Created the output directory '%s'.", os.path.abspath(output_path)) # Do some directory validation if self.hs_version == 'v2' and not util.is_directory_empty( output_path): # The output directory should be empty to avoid having conflict keys # or config files. logger.error( "The specified output directory '%s' is not empty. Please " "delete any files and folders or specify another output " "directory.", output_path) sys.exit(1) elif self.hs_version == 'v3': config_path = os.path.join(output_path, 'config.yaml') if os.path.isfile(config_path): logger.error( "The specified output directory '%s' already contains a 'config.yaml' " "file. Please clean the directory before starting config_generator.", output_path) sys.exit(1) return output_path
def write_master_key_to_disk(self): # Finished reading input, starting to write config files. util.try_make_dir(self.master_dir) master_key_file = os.path.join( self.master_dir, '{}.key'.format(self.master_onion_address)) with open(master_key_file, "wb") as key_file: os.chmod(master_key_file, 384) # chmod 0600 in decimal if self.hs_version == 'v2': master_passphrase = self.get_master_key_passphrase() key_file.write( self.master_key.exportKey(passphrase=master_passphrase)) else: master_key_formatted = self.master_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()) key_file.write(master_key_formatted) logger.debug("Successfully wrote master key to file %s.", os.path.abspath(master_key_file))