Skip to content

Commit

Permalink
Added -m flag
Browse files Browse the repository at this point in the history
  • Loading branch information
palak-chaturvedi committed Apr 16, 2024
1 parent c6535f6 commit b62b785
Showing 1 changed file with 9 additions and 43 deletions.
52 changes: 9 additions & 43 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ usage(void)
printf(" pgagroal-admin [ -f FILE ] [ COMMAND ] \n");
printf("\n");
printf("Options:\n");
printf(" -c, --config CONFIG_FILE Set the path to the pgagroal.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_CONF_FILE);
printf(" -m, --master-key-file MASTER_KEY_FILE Set the path to the master.key file\n");
printf(" -f, --file FILE Set the path to a user file\n");
printf(" Defaults to %s\n", PGAGROAL_DEFAULT_USERS_FILE);
printf(" -U, --user USER Set the user name\n");
Expand Down Expand Up @@ -201,16 +200,13 @@ main(int argc, char** argv)
int option_index = 0;
size_t command_count = sizeof(command_table) / sizeof(struct pgagroal_command);
struct pgagroal_parsed_command parsed = {.cmd = NULL, .args = {0}};
char* configuration_path = NULL;
struct main_configuration* config = NULL;
int ret;
size_t size;
char* master_key_path = NULL;

while (1)
{
static struct option long_options[] =
{
{"config", required_argument, 0, 'c'},
{"master-key-file", required_argument, 0, 'm'},
{"user", required_argument, 0, 'U'},
{"password", required_argument, 0, 'P'},
{"file", required_argument, 0, 'f'},
Expand All @@ -220,7 +216,7 @@ main(int argc, char** argv)
{"help", no_argument, 0, '?'}
};

c = getopt_long(argc, argv, "gV?c:f:U:P:l:",
c = getopt_long(argc, argv, "gV?f:m:U:P:l:",
long_options, &option_index);

if (c == -1)
Expand All @@ -230,8 +226,8 @@ main(int argc, char** argv)

switch (c)
{
case 'c':
configuration_path = optarg;
case 'm':
master_key_path = optarg;
break;
case 'U':
username = optarg;
Expand Down Expand Up @@ -271,36 +267,6 @@ main(int argc, char** argv)
exit(1);
}

size = sizeof(struct main_configuration);
if (pgagroal_create_shared_memory(size, HUGEPAGE_OFF, &shmem))
{
errx(1, "Error creating shared memory");
}
pgagroal_init_configuration(shmem);

if (configuration_path != NULL)
{
ret = pgagroal_read_configuration(shmem, configuration_path, false);
if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND)
{
errx(1, "Configuration not found: <%s>", configuration_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
errx(1, "Too many sections in the configuration file <%s>", configuration_path);
}
config = (struct main_configuration*)shmem;
}
else
{
ret = pgagroal_read_configuration(shmem, PGAGROAL_DEFAULT_CONF_FILE, false);
if(ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND){
errx(1, "Configuration file not found");
}
config = (struct main_configuration*)shmem;

}

if (!parse_command(argc, argv, optind, &parsed, command_table, command_count))
{
usage();
Expand All @@ -321,21 +287,21 @@ main(int argc, char** argv)

if (parsed.cmd->action == ACTION_MASTER_KEY)
{
if (master_key(password, generate_pwd, pwd_length, config->master_key_file_location))
if (master_key(password, generate_pwd, pwd_length, master_key_path))
{
errx(1, "Cannot generate master key");
}
}
else if (parsed.cmd->action == ACTION_ADD_USER)
{
if (add_user(file_path, username, password, generate_pwd, pwd_length, config->master_key_file_location))
if (add_user(file_path, username, password, generate_pwd, pwd_length, master_key_path))
{
errx(1, "Error for <user add>");
}
}
else if (parsed.cmd->action == ACTION_UPDATE_USER)
{
if (update_user(file_path, username, password, generate_pwd, pwd_length, config->master_key_file_location))
if (update_user(file_path, username, password, generate_pwd, pwd_length, master_key_path))
{
errx(1, "Error for <user edit>");
}
Expand Down

0 comments on commit b62b785

Please sign in to comment.