Skip to content

Commit

Permalink
system-plugin: check for opened files when storing key data
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateo Cindrić committed Aug 6, 2022
1 parent e1b8427 commit 1658bad
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/system/api/authentication/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,52 +263,53 @@ int system_authentication_store_user_authorized_key(system_ctx_t *ctx, const cha
char ssh_path_buffer[PATH_MAX] = {0};
char key_path_buffer[PATH_MAX] = {0};
DIR *ssh_dir = NULL;
FILE *key_file = NULL;
bool key_file_opened = false;

error = snprintf(ssh_path_buffer, sizeof(ssh_path_buffer), "/home/%s/.ssh", user);
if (error < 0) {
SRPLG_LOG_ERR(PLUGIN_NAME, "snprintf() failed");
goto error_out;
}

ssh_dir = opendir(ssh_path_buffer);
if (ssh_dir) {
LL_FOREACH(head, iter)
{
char ssh_data_buffer[PATH_MAX] = {0};
if (snprintf(key_path_buffer, sizeof(key_path_buffer), "%s/%s", ssh_path_buffer, iter->key.name) < 0) {
goto error_out;
}
if (snprintf(ssh_data_buffer, sizeof(ssh_data_buffer), "%s %s", iter->key.algorithm, iter->key.data) < 0) {
goto error_out;
}
FILE *destFile = fopen(key_path_buffer, "w");
fwrite(ssh_data_buffer, sizeof(char), sizeof(ssh_data_buffer), destFile);
fclose(destFile);
}
} else if (errno == ENOENT) {
if (errno == ENOENT) {
// create directory
error = mkdir(ssh_path_buffer, 0700);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "mkdir() error (%d)", error);
goto error_out;
}
LL_FOREACH(head, iter)
{
char ssh_data_buffer[PATH_MAX] = {0};
if (snprintf(key_path_buffer, sizeof(key_path_buffer), "%s/%s", ssh_path_buffer, iter->key.name) < 0) {
goto error_out;
}
if (snprintf(ssh_data_buffer, sizeof(ssh_data_buffer), "%s %s", iter->key.algorithm, iter->key.data) < 0) {
goto error_out;
}
FILE *destFile = fopen(key_path_buffer, "w");
fwrite(ssh_data_buffer, sizeof(char), sizeof(ssh_data_buffer), destFile);
fclose(destFile);
}
} else {
SRPLG_LOG_ERR(PLUGIN_NAME, "opendir() failed");
goto error_out;
}

// create key files
LL_FOREACH(head, iter)
{
if ((error = snprintf(key_path_buffer, sizeof(key_path_buffer), "%s/%s", ssh_path_buffer, iter->key.name)) < 0) {
SRPLG_LOG_ERR(PLUGIN_NAME, "snprintf() error (%d)", error);
goto error_out;
}

key_file = fopen(key_path_buffer, "w");
if (!key_file) {
SRPLG_LOG_ERR(PLUGIN_NAME, "fopen() failed");
goto error_out;
}
key_file_opened = true;

error = fprintf(key_file, "%s %s", iter->key.algorithm, iter->key.data);
if (error < 0) {
SRPLG_LOG_ERR(PLUGIN_NAME, "fprintf() error (%d)", error);
goto error_out;
}

fclose(key_file);
key_file_opened = false;
}

goto out;

error_out:
Expand All @@ -318,6 +319,11 @@ int system_authentication_store_user_authorized_key(system_ctx_t *ctx, const cha
if (ssh_dir) {
closedir(ssh_dir);
}

if (key_file_opened) {
fclose(key_file);
}

return error;
}

Expand Down

0 comments on commit 1658bad

Please sign in to comment.