From 1e7434ccb3d721195c78137b70d4d44b6927cda5 Mon Sep 17 00:00:00 2001 From: Vaishali Kumanan Date: Thu, 5 Aug 2021 16:03:43 -0400 Subject: [PATCH 1/6] Add functionality to write to a temp file through url arguments --- man/ttyd.1 | 4 ++++ man/ttyd.man.md | 3 +++ src/protocol.c | 27 ++++++++++++++++++++++++--- src/server.c | 48 ++++++++++++++++++++++++++++-------------------- src/server.h | 1 + 5 files changed, 60 insertions(+), 23 deletions(-) diff --git a/man/ttyd.1 b/man/ttyd.1 index 000cb45..bb412ed 100644 --- a/man/ttyd.1 +++ b/man/ttyd.1 @@ -63,6 +63,10 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows Allow client to send command line arguments in URL (eg: \[la]http://localhost:7681?arg=foo&arg=bar\[ra]) +.PP +\-f, \-\-arg\-file + Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument + .PP \-R, \-\-readonly Do not allow clients to write to the TTY diff --git a/man/ttyd.man.md b/man/ttyd.man.md index b90cd97..1b80179 100644 --- a/man/ttyd.man.md +++ b/man/ttyd.man.md @@ -40,6 +40,9 @@ ttyd 1 "September 2016" ttyd "User Manual" -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar) + -f, --arg-file + Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument + -R, --readonly Do not allow clients to write to the TTY diff --git a/src/protocol.c b/src/protocol.c index 5324655..6044c73 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -100,9 +100,30 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) for (i = 0; i < server->argc; i++) { argv[n++] = server->argv[i]; } - for (i = 0; i < pss->argc; i++) { - argv[n++] = pss->args[i]; + if (server->url_arg) { + for (i = 0; i < pss->argc; i++) { + argv[n++] = pss->args[i]; + } } + else if (server->arg_file) { + int fd = -1; + char filePath[] = "/tmp/XXXXXX"; + + if ((fd = mkstemp(filePath)) == -1) { + lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno)); + return false; + } + + for (i = 0; i < pss->argc; i++) { + if (dprintf(fd, "%s\n", pss->args[i]) < 0) { + lwsl_err("Write to temp file failed with error: %d (%s)\n", errno, strerror(errno)); + return false; + } + } + + argv[n++] = filePath; + } + argv[n] = NULL; pty_process *process = process_init((void *)pss, server->loop, argv); @@ -181,7 +202,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, pss->wsi = wsi; pss->lws_close_status = LWS_CLOSE_STATUS_NOSTATUS; - if (server->url_arg) { + if (server->url_arg || server->arg_file) { while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) { if (strncmp(buf, "arg=", 4) == 0) { pss->args = xrealloc(pss->args, (pss->argc + 1) * sizeof(char *)); diff --git a/src/server.c b/src/server.c index f1af441..4fda888 100644 --- a/src/server.c +++ b/src/server.c @@ -62,29 +62,30 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'}, #if LWS_LIBRARY_VERSION_NUMBER >= 4000000 {"ping-interval", required_argument, NULL, 'P'}, #endif - {"ipv6", no_argument, NULL, '6'}, - {"ssl", no_argument, NULL, 'S'}, - {"ssl-cert", required_argument, NULL, 'C'}, - {"ssl-key", required_argument, NULL, 'K'}, - {"ssl-ca", required_argument, NULL, 'A'}, - {"url-arg", no_argument, NULL, 'a'}, - {"readonly", no_argument, NULL, 'R'}, - {"terminal-type", required_argument, NULL, 'T'}, - {"client-option", required_argument, NULL, 't'}, - {"check-origin", no_argument, NULL, 'O'}, - {"max-clients", required_argument, NULL, 'm'}, - {"once", no_argument, NULL, 'o'}, - {"browser", no_argument, NULL, 'B'}, - {"debug", required_argument, NULL, 'd'}, - {"version", no_argument, NULL, 'v'}, - {"help", no_argument, NULL, 'h'}, - {NULL, 0, 0, 0}}; + {"ipv6", no_argument, NULL, '6'}, + {"ssl", no_argument, NULL, 'S'}, + {"ssl-cert", required_argument, NULL, 'C'}, + {"ssl-key", required_argument, NULL, 'K'}, + {"ssl-ca", required_argument, NULL, 'A'}, + {"url-arg", no_argument, NULL, 'a'}, + {"arg-file", no_argument, NULL, 'f'}, + {"readonly", no_argument, NULL, 'R'}, + {"terminal-type", required_argument, NULL, 'T'}, + {"client-option", required_argument, NULL, 't'}, + {"check-origin", no_argument, NULL, 'O'}, + {"max-clients", required_argument, NULL, 'm'}, + {"once", no_argument, NULL, 'o'}, + {"browser", no_argument, NULL, 'B'}, + {"debug", required_argument, NULL, 'd'}, + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, 0, 0}}; #if LWS_LIBRARY_VERSION_NUMBER < 4000000 -static const char *opt_string = "p:i:c:u:g:s:I:b:6aSC:K:A:Rt:T:Om:oBd:vh"; +static const char *opt_string = "p:i:c:u:g:s:I:b:6afSC:K:A:Rt:T:Om:oBd:vh"; #endif #if LWS_LIBRARY_VERSION_NUMBER >= 4000000 -static const char *opt_string = "p:i:c:u:g:s:I:b:P:6aSC:K:A:Rt:T:Om:oBd:vh"; +static const char *opt_string = "p:i:c:u:g:s:I:b:P:6afSC:K:A:Rt:T:Om:oBd:vh"; #endif static void print_help() { @@ -102,6 +103,7 @@ static void print_help() { " -g, --gid Group id to run with\n" " -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)\n" " -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)\n" + " -f, --arg-file Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument\n" " -R, --readonly Do not allow clients to write to the TTY\n" " -t, --client-option Send option to client (format: key=value), repeat to add more options\n" " -T, --terminal-type Terminal type to report, default: xterm-256color\n" @@ -326,6 +328,11 @@ int main(int argc, char **argv) { break; case 'a': server->url_arg = true; + server->arg_file = false; + break; + case 'f': + server->arg_file = true; + server->url_arg = false; break; case 'R': server->readonly = true; @@ -527,7 +534,8 @@ int main(int argc, char **argv) { lwsl_notice(" websocket: %s\n", endpoints.ws); } if (server->check_origin) lwsl_notice(" check origin: true\n"); - if (server->url_arg) lwsl_notice(" allow url arg: true\n"); + if (server->url_arg) lwsl_notice(" allow url arg to cli arg: true\n"); + if (server->arg_file) lwsl_notice(" allow url arg to tmp file: true\n"); if (server->readonly) lwsl_notice(" readonly: true\n"); if (server->max_clients > 0) lwsl_notice(" max clients: %d\n", server->max_clients); if (server->once) lwsl_notice(" once: true\n"); diff --git a/src/server.h b/src/server.h index 5f369ae..ea951fd 100644 --- a/src/server.h +++ b/src/server.h @@ -65,6 +65,7 @@ struct server { int sig_code; // close signal char sig_name[20]; // human readable signal string bool url_arg; // allow client to send cli arguments in URL + bool arg_file; // allow client to write to a temp file through URL arguments bool readonly; // whether not allow clients to write to the TTY bool check_origin; // whether allow websocket connection from different origin int max_clients; // maximum clients to support From 3e60dcb2168f5ad6df6bcb3ca45ec528efe68b62 Mon Sep 17 00:00:00 2001 From: Vaishali Kumanan Date: Fri, 6 Aug 2021 12:09:17 -0400 Subject: [PATCH 2/6] close file --- src/protocol.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/protocol.c b/src/protocol.c index 6044c73..7464e90 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -122,6 +122,7 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) } argv[n++] = filePath; + close(fd); } argv[n] = NULL; From 789e87c4d2ab69dcf0d2b8eda1c88b67bbf6de1f Mon Sep 17 00:00:00 2001 From: Vaishali Kumanan Date: Fri, 6 Aug 2021 12:27:44 -0400 Subject: [PATCH 3/6] Add argument for temp file path/prefix --- README.md | 1 + man/ttyd.1 | 3 +-- man/ttyd.man.md | 2 +- src/protocol.c | 6 +++--- src/server.c | 15 ++++++++------- src/server.h | 2 +- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index bdcd1fc..c37783a 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ OPTIONS: -g, --gid Group id to run with -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP) -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar) + -f, --arg-file File prefix for a unique generated temp file that URL arguments are written to (ex. /tmp/prefix); the generated file's full path is then passed in as a command line argument (ex. /tmp/prefix{unique string}) -R, --readonly Do not allow clients to write to the TTY -t, --client-option Send option to client (format: key=value), repeat to add more options -T, --terminal-type Terminal type to report, default: xterm-256color diff --git a/man/ttyd.1 b/man/ttyd.1 index bb412ed..b2f02aa 100644 --- a/man/ttyd.1 +++ b/man/ttyd.1 @@ -65,8 +65,7 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows .PP \-f, \-\-arg\-file - Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument - + File prefix for a unique generated temp file that URL arguments are written to (ex. /tmp/prefix); the generated file's full path is then passed in as a command line argument (ex. /tmp/prefix{unique string}) .PP \-R, \-\-readonly Do not allow clients to write to the TTY diff --git a/man/ttyd.man.md b/man/ttyd.man.md index 1b80179..2895360 100644 --- a/man/ttyd.man.md +++ b/man/ttyd.man.md @@ -41,7 +41,7 @@ ttyd 1 "September 2016" ttyd "User Manual" Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar) -f, --arg-file - Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument + File prefix for a unique generated temp file that URL arguments are written to (ex. /tmp/prefix); the generated file's full path is then passed in as a command line argument (ex. /tmp/prefix{unique string}) -R, --readonly Do not allow clients to write to the TTY diff --git a/src/protocol.c b/src/protocol.c index 7464e90..d7447f4 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -105,9 +105,9 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) argv[n++] = pss->args[i]; } } - else if (server->arg_file) { + else if (server->arg_file != NULL) { int fd = -1; - char filePath[] = "/tmp/XXXXXX"; + char *filePath = strcat(server->arg_file, "XXXXXX"); if ((fd = mkstemp(filePath)) == -1) { lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno)); @@ -203,7 +203,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, pss->wsi = wsi; pss->lws_close_status = LWS_CLOSE_STATUS_NOSTATUS; - if (server->url_arg || server->arg_file) { + if (server->url_arg || server->arg_file != NULL) { while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) { if (strncmp(buf, "arg=", 4) == 0) { pss->args = xrealloc(pss->args, (pss->argc + 1) * sizeof(char *)); diff --git a/src/server.c b/src/server.c index 4fda888..6fe0a5d 100644 --- a/src/server.c +++ b/src/server.c @@ -68,7 +68,7 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'}, {"ssl-key", required_argument, NULL, 'K'}, {"ssl-ca", required_argument, NULL, 'A'}, {"url-arg", no_argument, NULL, 'a'}, - {"arg-file", no_argument, NULL, 'f'}, + {"arg-file", required_argument, NULL, 'f'}, {"readonly", no_argument, NULL, 'R'}, {"terminal-type", required_argument, NULL, 'T'}, {"client-option", required_argument, NULL, 't'}, @@ -82,10 +82,10 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'}, {NULL, 0, 0, 0}}; #if LWS_LIBRARY_VERSION_NUMBER < 4000000 -static const char *opt_string = "p:i:c:u:g:s:I:b:6afSC:K:A:Rt:T:Om:oBd:vh"; +static const char *opt_string = "p:i:c:u:g:s:I:b:6af:SC:K:A:Rt:T:Om:oBd:vh"; #endif #if LWS_LIBRARY_VERSION_NUMBER >= 4000000 -static const char *opt_string = "p:i:c:u:g:s:I:b:P:6afSC:K:A:Rt:T:Om:oBd:vh"; +static const char *opt_string = "p:i:c:u:g:s:I:b:P:6af:SC:K:A:Rt:T:Om:oBd:vh"; #endif static void print_help() { @@ -103,7 +103,7 @@ static void print_help() { " -g, --gid Group id to run with\n" " -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)\n" " -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)\n" - " -f, --arg-file Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument\n" + " -f, --arg-file File prefix for a unique generated temp file that URL arguments are written to (ex. /tmp/prefix); the generated file's full path is then passed in as a command line argument (ex. /tmp/prefix{unique string})\n" " -R, --readonly Do not allow clients to write to the TTY\n" " -t, --client-option Send option to client (format: key=value), repeat to add more options\n" " -T, --terminal-type Terminal type to report, default: xterm-256color\n" @@ -179,6 +179,7 @@ static struct server *server_new(int argc, char **argv, int start) { static void server_free(struct server *ts) { if (ts == NULL) return; + if (ts->arg_file != NULL) free(ts->arg_file); if (ts->credential != NULL) free(ts->credential); if (ts->index != NULL) free(ts->index); free(ts->command); @@ -328,10 +329,10 @@ int main(int argc, char **argv) { break; case 'a': server->url_arg = true; - server->arg_file = false; + server->arg_file = NULL; break; case 'f': - server->arg_file = true; + server->arg_file = strdup(optarg); server->url_arg = false; break; case 'R': @@ -535,7 +536,7 @@ int main(int argc, char **argv) { } if (server->check_origin) lwsl_notice(" check origin: true\n"); if (server->url_arg) lwsl_notice(" allow url arg to cli arg: true\n"); - if (server->arg_file) lwsl_notice(" allow url arg to tmp file: true\n"); + if (server->arg_file != NULL) lwsl_notice(" temp file name prefix: %s\n", server->arg_file); if (server->readonly) lwsl_notice(" readonly: true\n"); if (server->max_clients > 0) lwsl_notice(" max clients: %d\n", server->max_clients); if (server->once) lwsl_notice(" once: true\n"); diff --git a/src/server.h b/src/server.h index ea951fd..e88fce3 100644 --- a/src/server.h +++ b/src/server.h @@ -65,7 +65,7 @@ struct server { int sig_code; // close signal char sig_name[20]; // human readable signal string bool url_arg; // allow client to send cli arguments in URL - bool arg_file; // allow client to write to a temp file through URL arguments + char *arg_file; // file prefix for a generated temp file that URL arguments are written to bool readonly; // whether not allow clients to write to the TTY bool check_origin; // whether allow websocket connection from different origin int max_clients; // maximum clients to support From c68b7a454efd1b1a5ee10aea896b83b72aab73a3 Mon Sep 17 00:00:00 2001 From: Vaishali Kumanan Date: Mon, 9 Aug 2021 13:28:32 -0400 Subject: [PATCH 4/6] Concat temp file suffix without strcat --- src/protocol.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/protocol.c b/src/protocol.c index d7447f4..2f413f2 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -107,7 +107,8 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) } else if (server->arg_file != NULL) { int fd = -1; - char *filePath = strcat(server->arg_file, "XXXXXX"); + char *filePath = xmalloc(strlen(server->arg_file) + 7); + sprintf(filePath, "%sXXXXXX", server->arg_file); if ((fd = mkstemp(filePath)) == -1) { lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno)); From 49f4831c9000e088325c5ae97aa5791e9eca7cc8 Mon Sep 17 00:00:00 2001 From: Vaishali Kumanan Date: Mon, 9 Aug 2021 14:08:01 -0400 Subject: [PATCH 5/6] use snprintf instead of sprintf --- src/protocol.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/protocol.c b/src/protocol.c index 2f413f2..8ba8b0f 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -107,8 +107,10 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) } else if (server->arg_file != NULL) { int fd = -1; - char *filePath = xmalloc(strlen(server->arg_file) + 7); - sprintf(filePath, "%sXXXXXX", server->arg_file); + // mkstemp requires the file path to have suffix XXXXXX (len 7) + int file_path_len = strlen(server->arg_file) + 7; + char *filePath = xmalloc(file_path_len); + snprintf(filePath, file_path_len, "%sXXXXXX", server->arg_file); if ((fd = mkstemp(filePath)) == -1) { lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno)); From ce9a8ec627c8b5286e12a1c660cba810318a6c16 Mon Sep 17 00:00:00 2001 From: Vaishali Kumanan Date: Mon, 16 Aug 2021 12:50:53 -0400 Subject: [PATCH 6/6] Reformat code --- src/protocol.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/protocol.c b/src/protocol.c index 8ba8b0f..809cac6 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -104,15 +104,13 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) for (i = 0; i < pss->argc; i++) { argv[n++] = pss->args[i]; } - } - else if (server->arg_file != NULL) { + } else if (server->arg_file != NULL) { int fd = -1; - // mkstemp requires the file path to have suffix XXXXXX (len 7) - int file_path_len = strlen(server->arg_file) + 7; + int file_path_len = strlen(server->arg_file) + 6 /*XXXXXX*/ + 1 /*null character*/; char *filePath = xmalloc(file_path_len); snprintf(filePath, file_path_len, "%sXXXXXX", server->arg_file); - if ((fd = mkstemp(filePath)) == -1) { + if ((fd = mkstemp(filePath)) != -1) { lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno)); return false; } @@ -124,8 +122,11 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) } } + if (close(fd) != 0) { + lwsl_err("Close temp file failed with error: %d (%s)\n", errno, strerror(errno)); + return false + } argv[n++] = filePath; - close(fd); } argv[n] = NULL;