1
0
Fork 0
forked from murx/murx

Color change of the window name when in a root shell.

This commit is contained in:
Johannes Findeisen 2026-11-16 05:00:19 +01:00
commit a53164bcd0
5 changed files with 75 additions and 11 deletions

View file

@ -25,9 +25,9 @@ package() {
}
sha512sums="
efc748dd85e0fda80baaf3b047f8b5a8d57c6231973540be2da9383a8aad8ae4c08a9a1665e219bcd4b4e045d2a88d271d2cde934b1023cf165096c188ea8da4 moerx.c
ae9e049ba65f70bac3acc6c37d0b40b4ce52593b687672a507b753ad3ca55ef02e0d0bcb1a2310982edab9c1ec77b0439fcadbd9762ccecb3143cb22ebe97562 moerx.c
e6336e712a9a6071b16cbde83cd6f591c480da68cf5185f0db27f321b26adb075844a958e792da419dcb82a930de6cff28bc64a98e9b0facd9350e382aa95ce1 Makefile
06aa39542f0842cfddf7c14dda7fcff739c58a6c21ee899be77f9c18fcd9575fc6061d6846318153a2703747cbce814a9f87e63b6e905f529a42af91ddb98470 moerx.1
feaee93a326f0b62b1b655ef8b776c470a49feaaab41d6814ca9dd1d6f3416b70f65deda08e9362dc0e28f347123fa84a84e2f2c727566fb702003fed2caa68c README.md
1158a716d81fc33b850bf1fbb2e2ca821297da173d5024bd88418fc0f82273f9889bebe6ace5b8d9d09c21f833f96a56e5d7cb58f9b96c827b57b2e02640f7c2 moerx.1
e801f1c949bd4a8f4420660787587ca844cc5be739925e2ea8195bc3a5ec8185c7c2517e1e10c2f6f34aac65f2bbd08ac86be07b732d499a97f1987bfda4a7c6 README.md
2fe33ba22c5f68e124904be594208f050be20f33d115f9a56f60cac93304102da32e6a7e78154e831d3704da7ca8bfefd7025e7b8901e65babcd83e7342f6382 LICENSE
"

View file

@ -9,10 +9,10 @@ license=('Apache-2.0')
depends=('glibc')
source=('moerx.c' 'Makefile' 'moerx.1' 'README.md' 'LICENSE')
sha256sums=(
'4418d829418d51903609b03b334cdd5e4b8e2b667d180980890ee3efae1c9f12'
'85c51d8bcfb660287ea1cc2dbb8a0edd6ab137ce766a929ddb4f0ae0e7e37d32'
'ad08bd0acd671f1f1716250528c2d4e5acc78a83bf55fcebdf41fa101fbb71d5'
'882d5876e386ea0539c49f1f45759f454460d36c2ede43f5ec7c0401509fc5f0'
'000a031b54dff60d34c621c0318138b01524ba63b7d166778ccb00c2652e8d1d'
'5787b0b3f36e5369511d165bb7097492888d39a35a936b1a2e2d47760156d6cf'
'be0803b92b688e125133cc62f5df31bb925203e88d8f31749fb7b5ebcdb4c71c'
'31c5556be4cdd9bf52bb55f18ead0c68a4f0e793c2c2dab0a2a2c6d92407d0d4'
)

View file

@ -122,6 +122,7 @@ if [[ -n $SSH_CONNECTION ]]; then
directory="~/${directory#"$HOME/"}"
fi
printf '\033]777;moerx-cwd;%s\007' "$directory"
printf '\033]777;moerx-root;%s\007' "$((EUID == 0))"
}
moerx_ssh_preexec() {
local -a words
@ -159,6 +160,7 @@ if [[ $- == *i* && -n $SSH_CONNECTION && -z ${moerx_ssh_installed-} ]]; then
directory="~/${directory#"$HOME/"}"
fi
printf '\033]777;moerx-cwd;%s\007' "$directory"
printf '\033]777;moerx-root;%s\007' "$((EUID == 0))"
}
moerx_ssh_preexec() {
if [[ ${moerx_ssh_ready-0} == 1 && $1 != moerx_ssh_precmd ]]; then
@ -209,6 +211,10 @@ including percent-encoded paths. These display the reported absolute path;
`OSC 777;moerx-cwd;path` accepts either an absolute path or a path starting with
`~` and preserves that abbreviation. Reports belong to the foreground job
that sent them, so an old SSH connection cannot override the local shell's path.
The hooks also send `OSC 777;moerx-root;1` for effective UID 0 and
`OSC 777;moerx-root;0` otherwise, allowing the active remote window name to turn
red in a root shell. This is a visual hint based on shell reports, not a
security indicator.
Other shells can use equivalent
hooks sending `printf '\033]2;server:vim\007'`; use a hostname containing only
@ -279,8 +285,13 @@ Hard-coded:
directory label is hidden when fewer than four columns remain. Remote SSH
directories require a shell report; see [SSH window names and directories](#ssh-window-names-and-directories).
- `HH:MM:SS` on the right in `colour12`, refreshed every second.
- Window list right-aligned: current name `colour11`, other names `colour12`,
separators/flags `colour14`.
- Window list right-aligned: current name `colour11`, or red `colour9` when
its active pane's foreground process is `su` or has effective UID 0. Other
window names use `colour12`; separators/flags use `colour14`. The color
refreshes within one second and returns to yellow after leaving the root
shell or switching to an unprivileged pane. Over SSH, the remote hooks above
report root status; install them in each remote shell, including root's.
A root shell that does not run the hooks cannot report its privilege change.
- Pane borders are thin box-drawing lines (`─`, `│`, `┬`, `┼`, ...) in
`colour8`, drawn on the terminal's default background. Only splits have
borders; panes have no title row, leaving more space for terminal content.

11
moerx.1
View file

@ -225,6 +225,12 @@ The outer terminal title follows [moerx] hostname:program.
Hostnames are shortened at the first dot.
Panes have no title rows; only splits have separator lines.
The status uses colour12, accents colour14 and the current window colour11.
The current window name turns bright red (colour9) when its active pane's
foreground process is su or has effective UID 0, including programs launched
from a root shell. Inactive names retain colour12. The color refreshes within
one second and returns to yellow for an unprivileged active pane.
Over SSH the shell must report root status; install the hooks in root's shell
as well as the regular user's. This is a visual hint, not a security indicator.
Borders use colour8 on the terminal's default background.
Messages use foreground colour6 and background colour8.
.PP
@ -270,6 +276,9 @@ the moerx-specific form preserves remote ~ abbreviations.
Reports are associated with the foreground job that sent them.
Disconnecting SSH restores the local shell's path and name.
With splits the window and status follow the active pane.
OSC 777;moerx-root;1 reports a root shell and OSC 777;moerx-root;0 clears it.
The report is tied to the foreground SSH job and is ignored after disconnect.
Remote shells without the hooks cannot report privilege changes.
.SS Zsh
Add the following to ~/.zshrc on the remote host.
The preexec hook reports the first command of a pipeline;
@ -288,6 +297,7 @@ if [[ \-n $SSH_CONNECTION ]]; then
directory="~/${directory#"$HOME/"}"
fi
printf '\e033]777;moerx\-cwd;%s\e007' "$directory"
printf '\e033]777;moerx\-root;%s\e007' "$((EUID == 0))"
}
moerx_ssh_preexec() {
local \-a words
@ -321,6 +331,7 @@ if [[ $\- == *i* && \-n $SSH_CONNECTION && \-z ${moerx_ssh_installed\-} ]]; then
directory="~/${directory#"$HOME/"}"
fi
printf '\e033]777;moerx\-cwd;%s\e007' "$directory"
printf '\e033]777;moerx\-root;%s\e007' "$((EUID == 0))"
}
moerx_ssh_preexec() {
if [[ ${moerx_ssh_ready\-0} == 1 && $1 != moerx_ssh_precmd ]]; then

48
moerx.c
View file

@ -42,7 +42,6 @@
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
@ -82,6 +81,7 @@ static int send_msg(int fd, uint32_t type, const void *data, uint32_t len);
#define C_CYAN 6
#define C_WHITE 7
#define C_GREY 8
#define C_RED 9
#define C_YELLOW 11
#define C_BLUE 12
#define C_CYAN2 14
@ -330,6 +330,8 @@ struct Pane {
pid_t remote_title_pgrp;
char reported_cwd[PATH_MAX];
pid_t reported_cwd_pgrp;
int reported_root;
pid_t reported_root_pgrp;
int id;
int tty_fd;
@ -895,6 +897,15 @@ static void em_osc_cwd(Pane *p) {
static void em_osc_done(Pane *p) {
p->seq[p->seqlen] = '\0';
em_osc_cwd(p);
if (strcmp(p->seq, "777;moerx-root;0") == 0 ||
strcmp(p->seq, "777;moerx-root;1") == 0) {
pid_t pid = tcgetpgrp(p->fd);
if (pid <= 0) pid = tcgetpgrp(p->tty_fd);
if (pid > 0) {
p->reported_root = p->seq[15] == '1';
p->reported_root_pgrp = pid;
}
}
if (p->seqlen > 2 && (p->seq[0] == '0' || p->seq[0] == '2') && p->seq[1] == ';') {
const char *t = p->seq + 2;
size_t n = strlen(t);
@ -1082,6 +1093,7 @@ typedef struct Layout {
typedef struct Window {
char name[128];
int index;
int privileged;
Layout *root;
Pane *active;
struct Window *next;
@ -1173,6 +1185,31 @@ static int pane_remote_title(Pane *p, pid_t pid) {
return 1;
}
/* Inspect the effective UID, so sudo/su descendants stay marked even
when their executable name changes from the shell to another program. */
static int process_is_root(pid_t pid) {
unsigned long uid = 1;
#ifdef __linux__
char path[64], line[256];
snprintf(path, sizeof path, "/proc/%ld/status", (long)pid);
FILE *f = fopen(path, "r");
if (!f) return 0;
while (fgets(line, sizeof line, f)) {
unsigned long real_uid;
if (sscanf(line, "Uid: %lu %lu", &real_uid, &uid) == 2) break;
}
fclose(f);
#else
char command[96];
snprintf(command, sizeof command, "ps -p %ld -o uid=", (long)pid);
FILE *f = popen(command, "r");
if (!f) return 0;
int ok = fscanf(f, "%lu", &uid) == 1;
if (pclose(f) != 0 || !ok) return 0;
#endif
return uid == 0;
}
/* Use the active pane's foreground process group, so returning from a job
restores the shell name. */
static int window_update_name(Session *s, Window *w) {
@ -1211,6 +1248,11 @@ static int window_update_name(Session *s, Window *w) {
for (char *s = base; *s; s++) {
if ((unsigned char)*s < 32 || (unsigned char)*s == 127) *s = '?';
}
int privileged = strcmp(base, "su") == 0 || process_is_root(pid);
if (strcmp(base, "ssh") == 0 && p->reported_root_pgrp == pid)
privileged = p->reported_root;
int changed = w->privileged != privileged;
w->privileged = privileged;
char next[sizeof w->name];
if (strcmp(base, "ssh") == 0 && p->remote_title_pgrp == pid && p->remote_title[0]) {
if (s->show_hostname) {
@ -1232,7 +1274,7 @@ static int window_update_name(Session *s, Window *w) {
} else {
snprintf(next, sizeof next, "%.63s", base);
}
if (strcmp(w->name, next) == 0) return 0;
if (strcmp(w->name, next) == 0) return changed;
snprintf(w->name, sizeof w->name, "%s", next);
return 1;
}
@ -1883,7 +1925,7 @@ static void status_draw(Grid *g, Session *s, Client *c) {
flags[0] = 0;
if (w == s->cur) strcat(flags, "*");
else if (w == s->last) strcat(flags, "-");
int fg_name = (w == s->cur) ? P_CURRENT : P_STATUS;
int fg_name = (w == s->cur) ? (w->privileged ? C_RED : P_CURRENT) : P_STATUS;
wx = draw_text(g, wx, y, num, P_STATUS, DEFAULT_COLOR, 0);
wx = draw_text(g, wx, y, ":", P_ACCENT, DEFAULT_COLOR, 0);
wx = draw_text(g, wx, y, w->name, fg_name, DEFAULT_COLOR, 0);