From 67d6564cfba146422f2b9cbb4109ab97c566735c Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Mon, 10 Oct 2016 22:08:17 +0800 Subject: [PATCH] Add support for the --readonly option --- README.md | 1 + src/protocol.c | 2 ++ src/server.c | 9 ++++++++- src/server.h | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 117cf00..e44aa53 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ OPTIONS: --gid, -g Group id to run with --signal, -s Signal to send to the command when exit it (default: SIGHUP) --reconnect, -r Time to reconnect for the client in seconds (default: 10) + --readonly, -R Do not allow clients to write to the TTY --once, -o Accept only one client and exit on disconnection --ssl, -S Enable ssl --ssl-cert, -C Ssl certificate file path diff --git a/src/protocol.c b/src/protocol.c index 7218c86..5e68dcc 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -258,6 +258,8 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, switch (command) { case INPUT: + if (server->readonly) + return 0; if (write(client->pty, client->buffer + 1, client->len - 1) < client->len - 1) { lwsl_err("write INPUT to pty\n"); return -1; diff --git a/src/server.c b/src/server.c index 2f5b412..a401409 100644 --- a/src/server.c +++ b/src/server.c @@ -33,13 +33,14 @@ static const struct option options[] = { {"ssl-cert", required_argument, NULL, 'C'}, {"ssl-key", required_argument, NULL, 'K'}, {"ssl-ca", required_argument, NULL, 'A'}, + {"readonly", no_argument, NULL, 'R'}, {"once", no_argument, NULL, 'o'}, {"debug", required_argument, NULL, 'd'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, 0, 0} }; -static const char *opt_string = "p:i:c:u:g:s:r:aSC:K:A:od:vh"; +static const char *opt_string = "p:i:c:u:g:s:r:aSC:K:A:Rod:vh"; void print_help() { fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n" @@ -55,6 +56,7 @@ void print_help() { " --gid, -g Group id to run with\n" " --signal, -s Signal to send to the command when exit it (default: SIGHUP)\n" " --reconnect, -r Time to reconnect for the client in seconds (default: 10)\n" + " --readonly, -R Do not allow clients to write to the TTY\n" " --once, -o Accept only one client and exit on disconnection\n" " --ssl, -S Enable ssl\n" " --ssl-cert, -C Ssl certificate file path\n" @@ -202,6 +204,9 @@ main(int argc, char **argv) { case 'd': debug_level = atoi(optarg); break; + case 'R': + server->readonly = true; + break; case 'o': server->once = true; break; @@ -322,6 +327,8 @@ main(int argc, char **argv) { lwsl_notice(" start command: %s\n", server->command); lwsl_notice(" reconnect timeout: %ds\n", server->reconnect); lwsl_notice(" close signal: %s (%d)\n", server->sig_name, server->sig_code); + if (server->readonly) + lwsl_notice(" readonly: true\n"); if (server->once) lwsl_notice(" once: true\n"); diff --git a/src/server.h b/src/server.h index 8c197e8..c985b07 100644 --- a/src/server.h +++ b/src/server.h @@ -71,6 +71,7 @@ struct tty_server { char **argv; // command with arguments int sig_code; // close signal char *sig_name; // human readable signal string + bool readonly; // whether not allow clients to write to the TTY bool once; // whether accept only one client and exit on disconnection pthread_mutex_t lock; }; -- 2.43.4