Skip to content

Commit

Permalink
Add functionality to write to a temp file through url arguments (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaishalikumanan authored and kahing committed Aug 26, 2021
1 parent 3b174da commit a559366
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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
Expand Down
3 changes: 3 additions & 0 deletions man/ttyd.1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ 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
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
Expand Down
3 changes: 3 additions & 0 deletions man/ttyd.man.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

Expand Down
32 changes: 29 additions & 3 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,35 @@ static int spawn_process(struct pss_tty *pss) {
for (i = 0; i < server->argc; i++) {
argv[n++] = server->argv[i];
}
for (i = 0; i < proc->argc; i++) {
argv[n++] = proc->args[i];
if (server->url_arg) {
for (i = 0; i < proc->argc; i++) {
argv[n++] = proc->args[i];
}
} else if (server->arg_file != NULL) {
int fd = -1;
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) {
lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno));
return false;
}

for (i = 0; i < proc->argc; i++) {
if (dprintf(fd, "%s\n", proc->args[i]) < 0) {
lwsl_err("Write to temp file failed with error: %d (%s)\n", errno, strerror(errno));
return false;
}
}

if (close(fd) != 0) {
lwsl_err("Close temp file failed with error: %d (%s)\n", errno, strerror(errno));
return false
}
argv[n++] = filePath;
}

argv[n] = NULL;

uv_signal_start(&server->watcher, child_cb, SIGCHLD);
Expand Down Expand Up @@ -270,7 +296,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
proc->state = STATE_INIT;
uv_pipe_init(server->loop, &proc->pipe, 0);

if (server->url_arg) {
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) {
proc->args = xrealloc(proc->args, (proc->argc + 1) * sizeof(char *));
Expand Down
15 changes: 12 additions & 3 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static const struct option options[] = {
{"ssl-key", required_argument, NULL, 'K'},
{"ssl-ca", required_argument, NULL, 'A'},
{"url-arg", no_argument, NULL, 'a'},
{"arg-file", required_argument, NULL, 'f'},
{"readonly", no_argument, NULL, 'R'},
{"terminal-type", required_argument, NULL, 'T'},
{"client-option", required_argument, NULL, 't'},
Expand All @@ -86,10 +87,10 @@ static const struct option options[] = {
{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: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:6aSC: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() {
Expand All @@ -107,6 +108,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 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"
Expand Down Expand Up @@ -184,6 +186,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);
Expand Down Expand Up @@ -318,6 +321,11 @@ int main(int argc, char **argv) {
break;
case 'a':
server->url_arg = true;
server->arg_file = NULL;
break;
case 'f':
server->arg_file = strdup(optarg);
server->url_arg = false;
break;
case 'R':
server->readonly = true;
Expand Down Expand Up @@ -534,7 +542,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 != 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);
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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
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
Expand Down

0 comments on commit a559366

Please sign in to comment.