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()
ssize_t read;
while ((read = getline(&line, &line_length, authorized_keys)) != -1)
{
asprintf(&field, "%d", index);
// Remove trailing newline.
line[strcspn(line, "\n")] = 0;
json_object_set(keys, field, json_string(line));
++index;
// Ignore empty lines, they do have IDs though.
if(strlen(line) > 1)
{
asprintf(&field, "%d", index);
// Remove trailing newline.
line[strcspn(line, "\n")] = 0;
json_object_set(keys, field, json_string(line));
}
index++;
}
// Close file and clean up.
......
......@@ -223,52 +223,53 @@ int replace_ssh_key(int id, const char * ssh_key)
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')
line_number++;
// Initialise with any non-EOF character
char ch;
int line_number = 1;
// Loop through all the lines in the input file
do
{
if (line_number != id)
{
// Copy all lines that don't have id as line number
putc(ch, authorized_keys_out);
// Copy 1 line
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
{
// Insert the new line first
putc(ch, authorized_keys_out);
// 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)
// Insert an ssh key instead of a line
ch = *ssh_key++;
while(ch != '\0')
{
printf("inserting %c\n", *ssh_key);
if(*ssh_key != '\n' && *ssh_key != '\r')
{
putc(*ssh_key++, authorized_keys_out);
}
// New line in ssh key is not allowed, because that will screw
// up the indices
if(ch != '\n')
putc(ch, authorized_keys_out);
ch = *ssh_key++;
}
printf("inserting new line %c\n", *ssh_key);
putc('\n', authorized_keys_out);
// Read characters until the next newline
do {
printf("not inserting %c\n", ch);
// ignore replaced line
do
{
ch = getc(authorized_keys_in);
} 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_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