diff --git a/src/api/ssh_keys_post.c b/src/api/ssh_keys_post.c index 58c2f933cd34e767770e3c05eb3fccfd952f34eb..08fd28f2628463ca8530e238beb4f3f434974e24 100644 --- a/src/api/ssh_keys_post.c +++ b/src/api/ssh_keys_post.c @@ -41,23 +41,11 @@ int callback_ssh_keys_post(const struct _u_request * request, return send_simple_response(response, 400, "error", "missing ssh-key"); } - // Call sed to append the command correctly: - char * command = NULL; - asprintf(&command, "echo '%s' | sed -rf '%s'", ssh_key, RESTRICT_COMMAND_PATH); - FILE * sed_output = popen(command, "r"); + char * ssh_key_with_command; - if (!sed_output) - { - return send_simple_response(response, 500, "error", "Internal error while handling ssh-key"); - } - - // Get the output from sed - char * ssh_key_with_command = read_from_file(sed_output); + add_ssh_command(&ssh_key_with_command, ssh_key); - if(!ssh_key_with_command) - { - return send_simple_response(response, 500, "error", "Internal error while converting ssh-key"); - } + asprintf(&ssh_key_with_command, "%s\n", ssh_key_with_command); // Write SSH key to file fprintf(authorized_keys, ssh_key_with_command); @@ -65,4 +53,3 @@ int callback_ssh_keys_post(const struct _u_request * request, return send_simple_response(response, 200, "status", "ok"); } - diff --git a/src/auxiliary.c b/src/auxiliary.c index c05da344af7476fe4e9bc36b8dcef94d062585ca..2a38fb0fb65c3efa4911b6cb7bed4a893898fdf9 100644 --- a/src/auxiliary.c +++ b/src/auxiliary.c @@ -191,3 +191,17 @@ int parse_int(const char * input, int * result) *result = l; return 0; } + +/** + * Add the SSH_COMMAND string in front of ssh_key unless it's already there + * because people have seen it being used in ssh_keys_list. + * @param[in] ssh_key a valid ssh key string + */ +int add_ssh_command(char ** ssh_key_with_command, const char * ssh_key) +{ + if(strncmp(SSH_COMMAND, ssh_key, strlen(SSH_COMMAND)) != 0) + asprintf(ssh_key_with_command, "%s %s", SSH_COMMAND, ssh_key); + else + asprintf(ssh_key_with_command, "%s", ssh_key); + return 0; +} diff --git a/src/includes/settings.h b/src/includes/settings.h index 287d675bfab72fd72f97ec217634c39d3e77c92a..85dc499d5c461f44dfd0f16e89aad0a5426b44e7 100644 --- a/src/includes/settings.h +++ b/src/includes/settings.h @@ -15,4 +15,4 @@ #define AUTHORIZED_KEYS_DIR "/root/.ssh" #define AUTHORIZED_KEYS_PATH AUTHORIZED_KEYS_DIR "/authorized_keys" #define SSH_HOST_KEY_DIR "/dropbear" -#define RESTRICT_COMMAND_PATH "/etc/cryptops-api/restrict_command.sed" +#define SSH_COMMAND "command=\"cd / && /usr/bin/cryptops-client\""