def cli(command, jail, host_user, jail_user): """Runs the command given inside the specified jail as the supplied user.""" # We may be getting ';', '&&' and so forth. Adding the shell for safety. if len(command) == 1: command = ("/bin/sh", "-c") + command if jail.startswith("-"): ioc_common.logit({ "level" : "ERROR", "message": "Please specify a jail first!" }) exit(1) if host_user and jail_user: ioc_common.logit({ "level" : "ERROR", "message": "Please only specify either host_user or" " jail_user, not both!" }) exit(1) jails, paths = ioc_list.IOCList("uuid").list_datasets() _jail = {tag: uuid for (tag, uuid) in jails.items() if uuid.startswith(jail) or tag == jail} if len(_jail) == 1: tag, uuid = next(iter(_jail.items())) path = paths[tag] elif len(_jail) > 1: ioc_common.logit({ "level" : "ERROR", "message": f"Multiple jails found for {jail}:" }) for t, u in sorted(_jail.items()): ioc_common.logit({ "level" : "ERROR", "message": f" {u} ({t})" }) exit(1) else: ioc_common.logit({ "level" : "ERROR", "message": f"{jail} not found!" }) exit(1) msg, err = ioc_exec.IOCExec(command, uuid, tag, path, host_user, jail_user).exec_jail() if err: ioc_common.logit({ "level" : "ERROR", "message": err.decode() }) else: ioc_common.logit({ "level" : "INFO", "message": msg.decode("utf-8") })
def cli(command, jail): """Runs pkg with the command given inside the specified jail.""" jails, paths = ioc_list.IOCList("uuid").list_datasets() _jail = { tag: uuid for (tag, uuid) in jails.items() if uuid.startswith(jail) or tag == jail } if len(_jail) == 1: tag, uuid = next(iter(_jail.items())) path = paths[tag] elif len(_jail) > 1: ioc_common.logit({ "level": "ERROR", "message": f"Multiple jails found for {jail}:" }) for t, u in sorted(_jail.items()): ioc_common.logit({"level": "ERROR", "message": f" {u} ({t})"}) exit(1) else: ioc_common.logit({"level": "ERROR", "message": f"{jail} not found!"}) exit(1) cmd = ("pkg", ) + command ioc_exec.IOCExec(cmd, uuid, tag, path).exec_jail()
def exec(self, command, host_user="******", jail_user=None, console=False): """Executes a command in the jail as the supplied users.""" if host_user and jail_user: ioc_common.logit( { "level": "EXCEPTION", "message": "Please only specify either host_user or" " jail_user, not both!" }, _callback=self.callback, silent=self.silent) tag, uuid, path = self.__check_jail_existence__() msg, err = ioc_exec.IOCExec(command, uuid, tag, path, host_user, jail_user, console=console).exec_jail() if not console: if err: ioc_common.logit( { "level": "EXCEPTION", "message": err.decode() }, _callback=self.callback, silent=self.silent) else: ioc_common.logit( { "level": "INFO", "message": msg.decode("utf-8") }, _callback=self.callback, silent=self.silent)