Commit 5641051d authored by Arie Peterson's avatar Arie Peterson 🐚
Browse files

Merge branch '11-adding-ssh-keys-may-break-authorized_keys' into 'master'

Resolve "Adding ssh keys may break `authorized_keys`"

Closes #11

See merge request !7
parents 4937294c f1a9b2c5
Pipeline #9993 passed with stages
in 1 minute and 20 seconds
......@@ -19,8 +19,8 @@
int callback_ssh_keys_post(const struct _u_request * request,
struct _u_response * response, void * user_data)
{
// Open file with append mode
FILE * authorized_keys = fopen(AUTHORIZED_KEYS_PATH, "a");
// Open file with append+read mode.
FILE * authorized_keys = fopen(AUTHORIZED_KEYS_PATH, "a+");
// Check if that succeeded.
if (authorized_keys == NULL)
......@@ -46,8 +46,21 @@ int callback_ssh_keys_post(const struct _u_request * request,
char * ssh_key_with_command;
add_ssh_command(&ssh_key_with_command, ssh_key);
// Read the last character of the authorized_keys file.
fseek(authorized_keys, -1, SEEK_END);
int last_char = fgetc(authorized_keys);
// Write SSH key to file
fprintf(authorized_keys, "%s\n", ssh_key_with_command);
// Check if the authorized_keys file is empty or ends in a newline.
if (last_char == EOF || last_char == '\n')
{
fprintf(authorized_keys, "%s\n", ssh_key_with_command);
}
else
{
// If not, prepend an extra newline.
fprintf(authorized_keys, "\n%s\n", ssh_key_with_command);
}
fclose(authorized_keys);
free(ssh_key_with_command);
json_decref(json_input);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment