| 385 | |
| 386 | |
| 387 | def do_command(params, command_line): |
| 388 | # Block control characters to prevent newline-injection at the shell boundary. |
| 389 | if isinstance(command_line, str) and any(ch in command_line for ch in _DISALLOWED_COMMAND_CHARS): |
| 390 | logging.error('Invalid command: control characters are not allowed in command input.') |
| 391 | return |
| 392 | |
| 393 | if getattr(params, 'service_mode', False): |
| 394 | try: |
| 395 | command_tokens = shlex.split(command_line) |
| 396 | except ValueError: |
| 397 | command_tokens = [] |
| 398 | if command_tokens and command_tokens[0].lower() in get_shell_only_command_names(): |
| 399 | logging.error('Interactive shell/session commands are not available in Service Mode') |
| 400 | return |
| 401 | |
| 402 | def is_msp(params_local): |
| 403 | if params_local.enterprise: |
| 404 | if 'licenses' in params_local.enterprise: |
| 405 | msp_license = next((x for x in params_local.enterprise['licenses'] if x['lic_status'].startswith('msp')), |
| 406 | None) |
| 407 | if msp_license: |
| 408 | return True |
| 409 | return False |
| 410 | |
| 411 | if command_line.lower() == 'h' or command_line.lower() == 'history': |
| 412 | display.formatted_history(stack) |
| 413 | return |
| 414 | |
| 415 | if command_line.lower().startswith('server'): |
| 416 | _, sp, args = command_line.partition(' ') |
| 417 | server_command(params, args) |
| 418 | return |
| 419 | |
| 420 | if command_line.startswith('ksm'): |
| 421 | # Exclude cwd from PATH on Windows so a local `ksm.bat` can't hijack |
| 422 | # the `ksm` prefix via CreateProcess's implicit cwd search. |
| 423 | search_path = os.environ.get('PATH', os.defpath) |
| 424 | if sys.platform.startswith('win'): |
| 425 | try: |
| 426 | cwd_abs = os.path.abspath(os.getcwd()) |
| 427 | except OSError: |
| 428 | cwd_abs = None |
| 429 | if cwd_abs: |
| 430 | search_path = os.pathsep.join( |
| 431 | p for p in search_path.split(os.pathsep) |
| 432 | if p and os.path.abspath(p) != cwd_abs |
| 433 | ) |
| 434 | |
| 435 | ksm_path = shutil.which('ksm', path=search_path) |
| 436 | if not ksm_path: |
| 437 | logging.error( |
| 438 | 'Please install the ksm application to run ksm commands.\n' |
| 439 | 'See https://docs.keeper.io/secrets-manager/secrets-manager' |
| 440 | '/secrets-manager-command-line-interface' |
| 441 | '#secrets-manager-cli-installation' |
| 442 | ) |
| 443 | return |
| 444 | |