diff --git a/src/api/ssh_keys_get.c b/src/api/ssh_keys_get.c index 255442174be3a3f72fa99ed6eb59b766a4865c42..1bd595839ac60afd9de3d1b52740e0ddce1f904c 100644 --- a/src/api/ssh_keys_get.c +++ b/src/api/ssh_keys_get.c @@ -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. diff --git a/src/auxiliary.c b/src/auxiliary.c index ae992df7c6f7dd052cd7835e3fc27ae2912ab9e8..18b5e0f96b0f9a88dce220065863af83b4c9c77d 100644 --- a/src/auxiliary.c +++ b/src/auxiliary.c @@ -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);