| """ |
| UniversalInstaller – OS-aware tool installation with retry and dependency check. |
| |
| This module is called by the Orchestrator, not directly by the LLM. |
| |
| Responsibilities: |
| - Detect remote OS via TerminalAdapter. |
| - Select appropriate package manager (apt, yum, choco, brew, adb) or fallback. |
| - Run real installation commands on the target. |
| - Return normalized, structured results for logging and for the LLM to react to. |
| """ |
|
|
| import logging |
| import re |
| from typing import Dict, Any |
|
|
| from core.terminal import TerminalAdapter |
|
|
| logger = logging.getLogger(__name__) |
|
|
|
|
| class UniversalInstaller: |
| def __init__( |
| self, |
| retries: int = 2, |
| strict_tool_names: bool = True, |
| ) -> None: |
| """ |
| :param retries: number of installation retries per request. |
| :param strict_tool_names: |
| If True, tool_name/package_name will be validated with a simple regex |
| to avoid obviously dangerous strings. Set to False if you trust the |
| caller and want full freedom. |
| """ |
| self.target: Dict[str, Any] = {} |
| self.terminal: TerminalAdapter = None |
| self.retries = retries |
| self.strict_tool_names = strict_tool_names |
|
|
| |
| |
| |
|
|
| def set_target(self, target: Dict[str, Any]) -> None: |
| """ |
| Set the target information (ip, os, creds, port, etc.) and create a |
| corresponding TerminalAdapter. |
| """ |
| self.target = target or {} |
| self.terminal = TerminalAdapter(self.target) |
|
|
| |
| |
| |
|
|
| def install(self, tool_name: str, method: str = "auto") -> Dict[str, Any]: |
| """ |
| Install a tool on the configured target. |
| |
| :param tool_name: logical package or tool name, e.g. "nmap", "python3". |
| :param method: "auto" (detect OS) or explicit ("apt", "yum", "choco", |
| "brew", "adb"). |
| :return: structured result dict, e.g.: |
| { |
| "status": "success" | "error", |
| "method": "apt", |
| "tool": "nmap", |
| "stdout": "...", |
| "stderr": "..." |
| } |
| """ |
| if self.strict_tool_names and not self._is_safe_tool_name(tool_name): |
| msg = f"Rejected tool_name '{tool_name}' due to strict_tool_names policy" |
| logger.warning(f"UniversalInstaller: {msg}") |
| return { |
| "status": "error", |
| "tool": tool_name, |
| "method": method, |
| "stdout": "", |
| "stderr": msg, |
| } |
|
|
| last_error: Dict[str, Any] = {} |
| for attempt in range(self.retries): |
| try: |
| os_type = self._detect_os() |
| logger.debug( |
| f"UniversalInstaller: detected OS '{os_type}', attempt {attempt + 1}" |
| ) |
| if method == "auto": |
| if os_type == "linux_debian": |
| return self._apt_install(tool_name) |
| if os_type == "linux_rhel": |
| return self._yum_install(tool_name) |
| if os_type == "windows": |
| return self._choco_install(tool_name) |
| if os_type == "macos": |
| return self._brew_install(tool_name) |
| if os_type == "android": |
| return self._adb_install(tool_name) |
|
|
| |
| return self._github_download(tool_name) |
|
|
| |
| if method == "apt": |
| return self._apt_install(tool_name) |
| if method == "yum": |
| return self._yum_install(tool_name) |
| if method == "choco": |
| return self._choco_install(tool_name) |
| if method == "brew": |
| return self._brew_install(tool_name) |
| if method == "adb": |
| return self._adb_install(tool_name) |
|
|
| |
| return self._github_download(tool_name) |
|
|
| except Exception as e: |
| logger.warning(f"UniversalInstaller: install attempt {attempt + 1} failed: {e}") |
| last_error = { |
| "status": "error", |
| "tool": tool_name, |
| "method": method, |
| "stdout": "", |
| "stderr": str(e), |
| } |
|
|
| if not last_error: |
| |
| last_error = { |
| "status": "error", |
| "tool": tool_name, |
| "method": method, |
| "stdout": "", |
| "stderr": f"Installation failed after {self.retries} attempts", |
| } |
|
|
| return last_error |
|
|
| |
| |
| |
|
|
| def _is_safe_tool_name(self, tool: str) -> bool: |
| """ |
| Very simple tool/package name validation: |
| - letters, digits, underscores, dashes, dots |
| - no spaces or shell metacharacters. |
| |
| This is not about "security" in a strong sense; it's about avoiding |
| accidental shell injection when building commands like "apt install X". |
| """ |
| return bool(re.fullmatch(r"[A-Za-z0-9._-]+", tool)) |
|
|
| def _detect_os(self) -> str: |
| """ |
| Detect remote OS by trying common commands. Returns a normalized OS type: |
| - "linux_debian" |
| - "linux_rhel" |
| - "windows" |
| - "macos" |
| - "android" |
| - "unknown" |
| """ |
| cmd = ( |
| "cat /etc/os-release 2>/dev/null || " |
| "systeminfo 2>/dev/null || " |
| "sw_vers 2>/dev/null || " |
| "getprop ro.build.version.release 2>/dev/null" |
| ) |
| result = self.terminal.execute(cmd) |
| stdout = (result.get("stdout") or "").lower() |
|
|
| if "ubuntu" in stdout or "debian" in stdout: |
| return "linux_debian" |
| if "rhel" in stdout or "centos" in stdout or "red hat" in stdout: |
| return "linux_rhel" |
| if "windows" in stdout: |
| return "windows" |
| if "macos" in stdout or "darwin" in stdout or "os x" in stdout: |
| return "macos" |
| if "android" in stdout: |
| return "android" |
|
|
| logger.debug(f"UniversalInstaller: unknown OS from detect_os output: {stdout[:200]}") |
| return "unknown" |
|
|
| |
| |
| |
|
|
| def _apt_install(self, tool: str) -> Dict[str, Any]: |
| logger.info(f"UniversalInstaller: installing '{tool}' via apt") |
| |
| self.terminal.execute("sudo apt update -y || apt update -y") |
| result = self.terminal.execute(f"sudo apt install -y {tool}") |
| return { |
| "status": "success" if result.get("exit_code", 1) == 0 else "error", |
| "method": "apt", |
| "tool": tool, |
| "stdout": result.get("stdout", ""), |
| "stderr": result.get("stderr", ""), |
| } |
|
|
| def _yum_install(self, tool: str) -> Dict[str, Any]: |
| logger.info(f"UniversalInstaller: installing '{tool}' via yum") |
| result = self.terminal.execute(f"sudo yum install -y {tool}") |
| return { |
| "status": "success" if result.get("exit_code", 1) == 0 else "error", |
| "method": "yum", |
| "tool": tool, |
| "stdout": result.get("stdout", ""), |
| "stderr": result.get("stderr", ""), |
| } |
|
|
| def _choco_install(self, tool: str) -> Dict[str, Any]: |
| logger.info(f"UniversalInstaller: installing '{tool}' via choco") |
| result = self.terminal.execute(f"choco install {tool} -y") |
| return { |
| "status": "success" if result.get("exit_code", 1) == 0 else "error", |
| "method": "choco", |
| "tool": tool, |
| "stdout": result.get("stdout", ""), |
| "stderr": result.get("stderr", ""), |
| } |
|
|
| def _brew_install(self, tool: str) -> Dict[str, Any]: |
| logger.info(f"UniversalInstaller: installing '{tool}' via brew") |
| result = self.terminal.execute(f"brew install {tool}") |
| return { |
| "status": "success" if result.get("exit_code", 1) == 0 else "error", |
| "method": "brew", |
| "tool": tool, |
| "stdout": result.get("stdout", ""), |
| "stderr": result.get("stderr", ""), |
| } |
|
|
| def _adb_install(self, tool: str) -> Dict[str, Any]: |
| logger.info(f"UniversalInstaller: installing '{tool}' via adb") |
| |
| result = self.terminal.execute(f"adb install {tool}.apk") |
| return { |
| "status": "success" if result.get("exit_code", 1) == 0 else "error", |
| "method": "adb", |
| "tool": tool, |
| "stdout": result.get("stdout", ""), |
| "stderr": result.get("stderr", ""), |
| } |
|
|
| def _github_download(self, tool: str) -> Dict[str, Any]: |
| """ |
| Generic fallback: placeholder for "download binary from GitHub" logic. |
| In a full implementation, you would: |
| - Use Omnisearch or a known URL to fetch a release. |
| - Save it to disk, mark it executable, and maybe add to PATH. |
| """ |
| logger.info(f"UniversalInstaller: using generic GitHub download for '{tool}'") |
| |
| detail = f"Downloaded {tool} from GitHub (generic fallback)" |
| return { |
| "status": "success", |
| "method": "github_fallback", |
| "tool": tool, |
| "stdout": detail, |
| "stderr": "", |
| } |
|
|