Skip to content
Snippets Groups Projects
Commit e6879b57 authored by Maarten de Waard's avatar Maarten de Waard :angel:
Browse files

print only ssh keys with IDs, fix PUT behaviour

parent 7d81aa2e
No related branches found
No related tags found
No related merge requests found
...@@ -23,11 +23,15 @@ json_t * readAuthorizedKeysToJson() ...@@ -23,11 +23,15 @@ json_t * readAuthorizedKeysToJson()
ssize_t read; ssize_t read;
while ((read = getline(&line, &line_length, authorized_keys)) != -1) while ((read = getline(&line, &line_length, authorized_keys)) != -1)
{ {
asprintf(&field, "%d", index); // Ignore empty lines, they do have IDs though.
// Remove trailing newline. if(strlen(line) > 1)
line[strcspn(line, "\n")] = 0; {
json_object_set(keys, field, json_string(line)); asprintf(&field, "%d", index);
++index; // Remove trailing newline.
line[strcspn(line, "\n")] = 0;
json_object_set(keys, field, json_string(line));
}
index++;
} }
// Close file and clean up. // Close file and clean up.
......
...@@ -223,52 +223,53 @@ int replace_ssh_key(int id, const char * ssh_key) ...@@ -223,52 +223,53 @@ int replace_ssh_key(int id, const char * ssh_key)
return -2; return -2;
} }
int line_number = 1;
char ch;
// Loop through all the characters in the input file
do {
ch = getc(authorized_keys_in);
if (ch == '\n') // Initialise with any non-EOF character
line_number++; char ch;
int line_number = 1;
// Loop through all the lines in the input file
do
{
if (line_number != id) if (line_number != id)
{ {
// Copy all lines that don't have id as line number // Copy 1 line
putc(ch, authorized_keys_out); ch = getc(authorized_keys_in);
while(ch != EOF && ch != '\n')
{
// Copy all lines that don't have id as line number
putc(ch, authorized_keys_out);
ch = getc(authorized_keys_in);
}
} }
else else
{ {
// Insert the new line first // Insert an ssh key instead of a line
putc(ch, authorized_keys_out); ch = *ssh_key++;
while(ch != '\0')
// Copy ssh_key to the line that has id as line number
// Some magic happens here, where *ssh_key++ returns the current
// index and moves the pointer to the next index of ssh_key
// TODO: prepend ssh_key with command= line
while(*ssh_key)
{ {
printf("inserting %c\n", *ssh_key); // New line in ssh key is not allowed, because that will screw
if(*ssh_key != '\n' && *ssh_key != '\r') // up the indices
{ if(ch != '\n')
putc(*ssh_key++, authorized_keys_out); putc(ch, authorized_keys_out);
} ch = *ssh_key++;
} }
printf("inserting new line %c\n", *ssh_key);
putc('\n', authorized_keys_out); // ignore replaced line
// Read characters until the next newline do
do { {
printf("not inserting %c\n", ch);
ch = getc(authorized_keys_in); ch = getc(authorized_keys_in);
} while (ch != EOF && ch != '\n'); } while (ch != EOF && ch != '\n');
printf("increasing line number to %d\n", line_number + 1);
// Increment line_number because the next loop will read a new
// character
line_number++;
} }
} while (ch != EOF);
if(ch != EOF)
{
// Insert newline
putc('\n', authorized_keys_out);
// Increment line number
line_number++;
}
} while(ch != EOF);
fclose(authorized_keys_in); fclose(authorized_keys_in);
fclose(authorized_keys_out); fclose(authorized_keys_out);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment