qrexec: add support for filtering terminal escape chars

This commit is contained in:
Marek Marczykowski-Górecki 2014-04-15 03:12:53 +02:00
parent 6f1ba98230
commit 0be6f8431f

View File

@ -30,6 +30,10 @@
#include "qrexec.h" #include "qrexec.h"
#include "libqrexec-utils.h" #include "libqrexec-utils.h"
// whether qrexec-client should replace ESC with _ before printing the output
int replace_esc_stdout = 0;
int replace_esc_stderr = 0;
int connect_unix_socket(const char *domname) int connect_unix_socket(const char *domname)
{ {
int s, len; int s, len;
@ -135,6 +139,14 @@ void handle_input(int s)
} }
} }
void do_replace_esc(char *buf, int len) {
int i;
for (i = 0; i < len; i++)
if (buf[i] == '\033')
buf[i] = '_';
}
void handle_daemon_data(int s) void handle_daemon_data(int s)
{ {
int status; int status;
@ -156,6 +168,8 @@ void handle_daemon_data(int s)
switch (hdr.type) { switch (hdr.type) {
case MSG_SERVER_TO_CLIENT_STDOUT: case MSG_SERVER_TO_CLIENT_STDOUT:
if (replace_esc_stdout)
do_replace_esc(buf, hdr.len);
if (local_stdin_fd == -1) if (local_stdin_fd == -1)
break; break;
if (hdr.len == 0) { if (hdr.len == 0) {
@ -173,6 +187,8 @@ void handle_daemon_data(int s)
} }
break; break;
case MSG_SERVER_TO_CLIENT_STDERR: case MSG_SERVER_TO_CLIENT_STDERR:
if (replace_esc_stderr)
do_replace_esc(buf, hdr.len);
write_all(2, buf, hdr.len); write_all(2, buf, hdr.len);
break; break;
case MSG_SERVER_TO_CLIENT_EXIT_CODE: case MSG_SERVER_TO_CLIENT_EXIT_CODE:
@ -238,8 +254,9 @@ void select_loop(int s)
void usage(const char *name) void usage(const char *name)
{ {
fprintf(stderr, fprintf(stderr,
"usage: %s -d domain_num [-l local_prog] -e -c remote_cmdline\n" "usage: %s -d domain_num [-l local_prog] -e -t -T -c remote_cmdline\n"
"-e means exit after sending cmd, -c: connect to existing process\n", "-e means exit after sending cmd, -c: connect to existing process\n"
"-t enables replacing ESC character with '_' in command output, -T is the same for stderr\n",
name); name);
exit(1); exit(1);
} }
@ -252,7 +269,7 @@ int main(int argc, char **argv)
int just_exec = 0; int just_exec = 0;
int connect_existing = 0; int connect_existing = 0;
char *local_cmdline = NULL; char *local_cmdline = NULL;
while ((opt = getopt(argc, argv, "d:l:ec")) != -1) { while ((opt = getopt(argc, argv, "d:l:ectT")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
domname = strdup(optarg); domname = strdup(optarg);
@ -266,6 +283,12 @@ int main(int argc, char **argv)
case 'c': case 'c':
connect_existing = 1; connect_existing = 1;
break; break;
case 't':
replace_esc_stdout = 1;
break;
case 'T':
replace_esc_stderr = 1;
break;
default: default:
usage(argv[0]); usage(argv[0]);
} }