unraid / webgui

Unraid Web UI
158 stars 70 forks source link

Feature Request: Use user's actual shell in OpenTerminal.php #1832

Closed donbuehl closed 2 months ago

donbuehl commented 2 months ago

Description

Currently, OpenTerminal.php is using a hardcoded 'bash' shell. I propose to add a function that determines the user's actual shell and uses it when opening the terminal.

Proposed Solution

  1. Add a getUserShell() function to OpenTerminal.php that retrieves the user's shell from /etc/passwd. If the shell can't be determined, it should default to 'bash'.

  2. Modify the existing exec call to use the result of getUserShell(). The original line:

    if ($retval != 0) exec("ttyd-exec -i '$sock' bash --login");

    should be changed to:

if ($retval != 0) exec("ttyd-exec -i '$sock' " . getUserShell() . " --login");

Here's the proposed function:

function getUserShell() {
    $shell = 'bash';

    try {
        $username = posix_getpwuid(posix_geteuid())['name'];
        $passwd = file_get_contents('/etc/passwd');
        $lines = explode("\n", $passwd);
        foreach ($lines as $line) {
            if (strpos($line, $username) === 0) {
                $parts = explode(':', $line);
                $fullShellPath = end($parts);
                $shell = basename(trim($fullShellPath));
                break;
            }
        }
    } catch (Exception $e) {
        syslog(LOG_ERR, "Error determining user shell: " . $e->getMessage());
    }

    return $shell;
}

Benefits