-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
Remove remote connection part from the admin.c Added -m flag
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,8 @@ | |
#include <logging.h> | ||
#include <security.h> | ||
#include <utils.h> | ||
#include <shmem.h> | ||
#include <configuration.h> | ||
|
||
/* system */ | ||
#include <ctype.h> | ||
|
@@ -52,9 +54,9 @@ | |
#define ACTION_REMOVE_USER 4 | ||
#define ACTION_LIST_USERS 5 | ||
|
||
static int master_key(char* password, bool generate_pwd, int pwd_length); | ||
static int add_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length); | ||
static int update_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length); | ||
static int master_key(char* password, bool generate_pwd, int pwd_length, char* filename); | ||
static int add_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length, char* filename); | ||
static int update_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length, char* filename); | ||
static int remove_user(char* users_path, char* username); | ||
static int list_users(char* users_path); | ||
|
||
|
@@ -164,6 +166,7 @@ usage(void) | |
printf(" pgagroal-admin [ -f FILE ] [ COMMAND ] \n"); | ||
printf("\n"); | ||
printf("Options:\n"); | ||
printf(" -m, --master-key-file MASTER_KEY_FILE Set the path to the master.key file\n"); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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"); | ||
|
@@ -197,11 +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* master_key_path = NULL; | ||
|
||
while (1) | ||
{ | ||
static struct option long_options[] = | ||
{ | ||
{"master-key-file", required_argument, 0, 'm'}, | ||
This comment has been minimized.
Sorry, something went wrong.
fluca1978
Collaborator
|
||
{"user", required_argument, 0, 'U'}, | ||
{"password", required_argument, 0, 'P'}, | ||
{"file", required_argument, 0, 'f'}, | ||
|
@@ -211,7 +216,7 @@ main(int argc, char** argv) | |
{"help", no_argument, 0, '?'} | ||
}; | ||
|
||
c = getopt_long(argc, argv, "gV?f:U:P:l:", | ||
c = getopt_long(argc, argv, "gV?f:m:U:P:l:", | ||
long_options, &option_index); | ||
|
||
if (c == -1) | ||
|
@@ -221,6 +226,9 @@ main(int argc, char** argv) | |
|
||
switch (c) | ||
{ | ||
case 'm': | ||
master_key_path = optarg; | ||
break; | ||
case 'U': | ||
username = optarg; | ||
break; | ||
|
@@ -253,6 +261,12 @@ main(int argc, char** argv) | |
errx(1, "Using the root account is not allowed"); | ||
} | ||
|
||
if (argc <= 1) | ||
{ | ||
usage(); | ||
exit(1); | ||
} | ||
|
||
if (!parse_command(argc, argv, optind, &parsed, command_table, command_count)) | ||
{ | ||
usage(); | ||
|
@@ -273,21 +287,21 @@ main(int argc, char** argv) | |
|
||
if (parsed.cmd->action == ACTION_MASTER_KEY) | ||
{ | ||
if (master_key(password, generate_pwd, pwd_length)) | ||
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)) | ||
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)) | ||
if (update_user(file_path, username, password, generate_pwd, pwd_length, master_key_path)) | ||
{ | ||
errx(1, "Error for <user edit>"); | ||
} | ||
|
@@ -318,77 +332,90 @@ main(int argc, char** argv) | |
} | ||
|
||
static int | ||
master_key(char* password, bool generate_pwd, int pwd_length) | ||
master_key(char* password, bool generate_pwd, int pwd_length, char* filename) | ||
{ | ||
FILE* file = NULL; | ||
char buf[MISC_LENGTH]; | ||
char* encoded = NULL; | ||
struct stat st = {0}; | ||
bool do_free = true; | ||
|
||
if (pgagroal_get_home_directory() == NULL) | ||
if (filename == NULL) | ||
{ | ||
char* username = pgagroal_get_user_name(); | ||
if (pgagroal_get_home_directory() == NULL) | ||
{ | ||
char* username = pgagroal_get_user_name(); | ||
|
||
if (username != NULL) | ||
{ | ||
warnx("No home directory for user \'%s\'", username); | ||
} | ||
else | ||
{ | ||
warnx("No home directory for user running pgagroal"); | ||
} | ||
|
||
goto error; | ||
} | ||
|
||
if (username != NULL) | ||
memset(&buf, 0, sizeof(buf)); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
snprintf(&buf[0], sizeof(buf), "%s/.pgagroal", pgagroal_get_home_directory()); | ||
|
||
if (stat(&buf[0], &st) == -1) | ||
{ | ||
warnx("No home directory for user \'%s\'", username); | ||
mkdir(&buf[0], S_IRWXU); | ||
} | ||
else | ||
{ | ||
warnx("No home directory for user running pgagroal"); | ||
if (S_ISDIR(st.st_mode) && st.st_mode & S_IRWXU && !(st.st_mode & S_IRWXG) && !(st.st_mode & S_IRWXO)) | ||
{ | ||
/* Ok */ | ||
} | ||
else | ||
{ | ||
warnx("Wrong permissions for directory <%s> (must be 0700)", &buf[0]); | ||
goto error; | ||
} | ||
} | ||
|
||
goto error; | ||
} | ||
memset(&buf, 0, sizeof(buf)); | ||
snprintf(&buf[0], sizeof(buf), "%s/.pgagroal/master.key", pgagroal_get_home_directory()); | ||
|
||
memset(&buf, 0, sizeof(buf)); | ||
snprintf(&buf[0], sizeof(buf), "%s/.pgagroal", pgagroal_get_home_directory()); | ||
if (pgagroal_exists(&buf[0])) | ||
{ | ||
warnx("The file %s already exists, cannot continue", &buf[0]); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
goto error; | ||
} | ||
|
||
if (stat(&buf[0], &st) == -1) | ||
{ | ||
mkdir(&buf[0], S_IRWXU); | ||
} | ||
else | ||
{ | ||
if (S_ISDIR(st.st_mode) && st.st_mode & S_IRWXU && !(st.st_mode & S_IRWXG) && !(st.st_mode & S_IRWXO)) | ||
if (stat(&buf[0], &st) == -1) | ||
{ | ||
/* Ok */ | ||
} | ||
else | ||
{ | ||
warnx("Wrong permissions for directory <%s> (must be 0700)", &buf[0]); | ||
goto error; | ||
if (S_ISREG(st.st_mode) && st.st_mode & (S_IRUSR | S_IWUSR) && !(st.st_mode & S_IRWXG) && !(st.st_mode & S_IRWXO)) | ||
{ | ||
/* Ok */ | ||
} | ||
else | ||
{ | ||
warnx("Wrong permissions for file <%s> (must be 0600)", &buf[0]); | ||
This comment has been minimized.
Sorry, something went wrong.
fluca1978
Collaborator
|
||
goto error; | ||
} | ||
} | ||
} | ||
|
||
memset(&buf, 0, sizeof(buf)); | ||
snprintf(&buf[0], sizeof(buf), "%s/.pgagroal/master.key", pgagroal_get_home_directory()); | ||
|
||
if (pgagroal_exists(&buf[0])) | ||
{ | ||
warnx("The file %s already exists, cannot continue", &buf[0]); | ||
goto error; | ||
file = fopen(&buf[0], "w+"); | ||
} | ||
|
||
if (stat(&buf[0], &st) == -1) | ||
{ | ||
/* Ok */ | ||
} | ||
else | ||
{ | ||
if (S_ISREG(st.st_mode) && st.st_mode & (S_IRUSR | S_IWUSR) && !(st.st_mode & S_IRWXG) && !(st.st_mode & S_IRWXO)) | ||
{ | ||
/* Ok */ | ||
} | ||
else | ||
if (pgagroal_exists(filename)) | ||
{ | ||
warnx("Wrong permissions for file <%s> (must be 0600)", &buf[0]); | ||
warnx("The file %s already exists, cannot continue", filename); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
goto error; | ||
} | ||
file = fopen(filename, "w+"); | ||
} | ||
|
||
file = fopen(&buf[0], "w+"); | ||
if (file == NULL) | ||
{ | ||
warnx("Could not write to master key file <%s>", &buf[0]); | ||
|
@@ -439,7 +466,14 @@ master_key(char* password, bool generate_pwd, int pwd_length) | |
fclose(file); | ||
|
||
chmod(&buf[0], S_IRUSR | S_IWUSR); | ||
printf("Master Key stored into %s\n", &buf[0]); | ||
if (filename == NULL) | ||
{ | ||
printf("Master Key stored into %s\n", &buf[0]); | ||
} | ||
else | ||
{ | ||
printf("Master Key stored into %s\n", filename); | ||
} | ||
return 0; | ||
|
||
error: | ||
|
@@ -460,7 +494,7 @@ master_key(char* password, bool generate_pwd, int pwd_length) | |
} | ||
|
||
static int | ||
add_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length) | ||
add_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length, char* filename) | ||
{ | ||
FILE* users_file = NULL; | ||
char line[MISC_LENGTH]; | ||
|
@@ -476,7 +510,7 @@ add_user(char* users_path, char* username, char* password, bool generate_pwd, in | |
char* verify = NULL; | ||
bool do_free = true; | ||
|
||
if (pgagroal_get_master_key(&master_key)) | ||
if (pgagroal_get_master_key(&master_key, filename)) | ||
{ | ||
warnx("Invalid master key"); | ||
goto error; | ||
|
@@ -635,7 +669,7 @@ add_user(char* users_path, char* username, char* password, bool generate_pwd, in | |
} | ||
|
||
static int | ||
update_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length) | ||
update_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length, char* filename) | ||
{ | ||
FILE* users_file = NULL; | ||
FILE* users_file_tmp = NULL; | ||
|
@@ -656,7 +690,7 @@ update_user(char* users_path, char* username, char* password, bool generate_pwd, | |
|
||
memset(&tmpfilename, 0, sizeof(tmpfilename)); | ||
|
||
if (pgagroal_get_master_key(&master_key)) | ||
if (pgagroal_get_master_key(&master_key, filename)) | ||
{ | ||
warnx("Invalid master key"); | ||
goto error; | ||
|
Description could be something like "Sets the full path to the master key file", and on a new line "Defaults to $home/%s" with
PGAGROAL_DEFAULT_MASTER_KEY_FILE_NAME =
master.key`.