From 164f792b86a9e87976ce30b510958708b238f3a7 Mon Sep 17 00:00:00 2001 From: Yaroslav Nikonorov Date: Wed, 23 Sep 2020 14:50:01 +0300 Subject: [PATCH] GUACAMOLE-1174: Remove option use-exec, add snprintf result validation, fix code formatting. --- src/protocols/kubernetes/kubernetes.c | 1 - src/protocols/kubernetes/settings.c | 15 ++----------- src/protocols/kubernetes/settings.h | 14 ++---------- src/protocols/kubernetes/url.c | 32 ++++++++++++++++++--------- src/protocols/kubernetes/url.h | 11 ++++----- 5 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/protocols/kubernetes/kubernetes.c b/src/protocols/kubernetes/kubernetes.c index 263d99fc..c793ad46 100644 --- a/src/protocols/kubernetes/kubernetes.c +++ b/src/protocols/kubernetes/kubernetes.c @@ -217,7 +217,6 @@ void* guac_kubernetes_client_thread(void* data) { settings->kubernetes_namespace, settings->kubernetes_pod, settings->kubernetes_container, - settings->use_exec, settings->exec_command)) { guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Unable to generate path for Kubernetes API endpoint: " diff --git a/src/protocols/kubernetes/settings.c b/src/protocols/kubernetes/settings.c index 74370d4b..9de7a859 100644 --- a/src/protocols/kubernetes/settings.c +++ b/src/protocols/kubernetes/settings.c @@ -31,7 +31,6 @@ const char* GUAC_KUBERNETES_CLIENT_ARGS[] = { "namespace", "pod", "container", - "use-exec", "exec-command", "use-ssl", "client-cert", @@ -89,12 +88,7 @@ enum KUBERNETES_ARGS_IDX { IDX_CONTAINER, /** - * Whether exec call should be used. If omitted, attach call will be used. - */ - IDX_USE_EXEC, - - /** - * The command used by exec call. + * The command used by exec call. If omitted, attach call will be used. */ IDX_EXEC_COMMAND, @@ -287,15 +281,10 @@ guac_kubernetes_settings* guac_kubernetes_parse_args(guac_user* user, guac_user_parse_args_string(user, GUAC_KUBERNETES_CLIENT_ARGS, argv, IDX_CONTAINER, NULL); - /* Parse whether exec call should be used */ - settings->use_exec = - guac_user_parse_args_boolean(user, GUAC_KUBERNETES_CLIENT_ARGS, argv, - IDX_USE_EXEC, false); - /* Read exec command (optional) */ settings->exec_command = guac_user_parse_args_string(user, GUAC_KUBERNETES_CLIENT_ARGS, argv, - IDX_EXEC_COMMAND, GUAC_KUBERNETES_DEFAULT_EXEC_COMMAND); + IDX_EXEC_COMMAND, NULL); /* Parse whether SSL should be used */ settings->use_ssl = diff --git a/src/protocols/kubernetes/settings.h b/src/protocols/kubernetes/settings.h index 597ebd53..1ad58058 100644 --- a/src/protocols/kubernetes/settings.h +++ b/src/protocols/kubernetes/settings.h @@ -47,12 +47,6 @@ */ #define GUAC_KUBERNETES_DEFAULT_NAMESPACE "default" -/** - * The command that should be used by default for exec call if no - * specific command is provided. - */ -#define GUAC_KUBERNETES_DEFAULT_EXEC_COMMAND "/bin/sh" - /** * The filename to use for the typescript, if not specified. */ @@ -104,12 +98,8 @@ typedef struct guac_kubernetes_settings { char* kubernetes_container; /** - * Whether exec call should be used, default attach. - */ - bool use_exec; - - /** - * Exec command, default /bin/sh. + * The command to generate api endpoint for call exec. + * If omitted call attach will be used. */ char* exec_command; diff --git a/src/protocols/kubernetes/url.c b/src/protocols/kubernetes/url.c index 97156269..9b8c55ac 100644 --- a/src/protocols/kubernetes/url.c +++ b/src/protocols/kubernetes/url.c @@ -91,7 +91,7 @@ int guac_kubernetes_escape_url_component(char* output, int length, int guac_kubernetes_endpoint_uri(char* buffer, int length, const char* kubernetes_namespace, const char* kubernetes_pod, - const char* kubernetes_container, int use_exec, const char* exec_command) { + const char* kubernetes_container, const char* exec_command) { int written; @@ -111,26 +111,32 @@ int guac_kubernetes_endpoint_uri(char* buffer, int length, return 1; /* Generate endpoint path depending on the call type */ - char* call="attach"; - if (use_exec) + char* call = "attach"; + if (exec_command != NULL) call = "exec"; char endpoint_path[GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH]; - snprintf(endpoint_path, GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH*3, - "/api/v1/namespaces/%s/pods/%s/%s", escaped_namespace, escaped_pod, call); + + written = snprintf(endpoint_path, sizeof(endpoint_path), + "/api/v1/namespaces/%s/pods/%s/%s", escaped_namespace, escaped_pod, call); + + if (written < 0 || written >= sizeof(endpoint_path)) + return 1; /* Generate endpoint params */ char endpoint_params[GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH]=""; - int param_length=0; - if(use_exec){ + if (exec_command != NULL) { /* Escape exec command */ if (guac_kubernetes_escape_url_component(escaped_exec_command, sizeof(escaped_exec_command), exec_command)) return 1; - param_length += snprintf(endpoint_params, GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH, - "command=%s&", escaped_exec_command); + written = snprintf(endpoint_params, sizeof(endpoint_params), + "command=%s&", escaped_exec_command); + + if (written < 0 || written >= sizeof(endpoint_params)) + return 1; } if (kubernetes_container != NULL) { @@ -139,13 +145,17 @@ int guac_kubernetes_endpoint_uri(char* buffer, int length, sizeof(escaped_container), kubernetes_container)) return 1; - snprintf(endpoint_params+param_length, GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH-param_length, + written = snprintf(endpoint_params, sizeof(endpoint_params), "container=%s&", escaped_container); + + if (written < 0 || written >= sizeof(endpoint_params)) + return 1; } /* Combine path and params to uri */ written = snprintf(buffer, length, "%s?%sstdin=true&stdout=true&tty=true", - endpoint_path, endpoint_params); + endpoint_path, endpoint_params); + /* Endpoint URL was successfully generated if it was written to the given * buffer without truncation */ return !(written < length - 1); diff --git a/src/protocols/kubernetes/url.h b/src/protocols/kubernetes/url.h index 8827e7ec..4d5e92e0 100644 --- a/src/protocols/kubernetes/url.h +++ b/src/protocols/kubernetes/url.h @@ -72,13 +72,10 @@ int guac_kubernetes_escape_url_component(char* output, int length, * @param kubernetes_container * The name of the container to attach to, or NULL to arbitrarily attach * to the first container in the pod. - * - * @param use_exec - * Whether use call exec. - * Execute a command in a container and attach to it instead of main container process. - * + * * @param exec_command - * The command used in conjunction with exec call. + * The command used to run a new process and attach to it, + * instead of the main container process. * * @return * Zero if the endpoint path was successfully written to the provided @@ -86,7 +83,7 @@ int guac_kubernetes_escape_url_component(char* output, int length, */ int guac_kubernetes_endpoint_uri(char* buffer, int length, const char* kubernetes_namespace, const char* kubernetes_pod, - const char* kubernetes_container, int use_exec, const char* exec_command); + const char* kubernetes_container, const char* exec_command); #endif