Added admin list to credentials

This commit is contained in:
fall-leaf 2023-08-04 11:09:15 +03:00
parent 51a06fdd2c
commit db3c80c7d3
2 changed files with 24 additions and 28 deletions

View File

@ -16,7 +16,7 @@ DECL char * raw(char const * const sql);
DECL char * remind(char * who); DECL char * remind(char * who);
DECL char * slurp(char const * fn); DECL char * slurp(char const * fn);
DECL int is_admin(char const * user); DECL int is_admin(char const * user);
DECL int parse_admin_list(char const * admin_list_file); DECL void parse_admins(char * admin_string);
DECL int parse_pair(char const * buf, size_t const len); DECL int parse_pair(char const * buf, size_t const len);
DECL void clean_admin_list(); DECL void clean_admin_list();
DECL void creds_free_password(void); DECL void creds_free_password(void);

View File

@ -26,7 +26,7 @@
#include "help.h" #include "help.h"
#include "parse.h" #include "parse.h"
#define PARAMS_COUNT 5 #define PARAMS_COUNT 6
enum cred_param_ids_e enum cred_param_ids_e
{ {
@ -34,7 +34,8 @@ enum cred_param_ids_e
PASSWORD, PASSWORD,
CHANNEL, CHANNEL,
SERVER, SERVER,
PORT PORT,
ADMINS
}; };
char const * cred_names[] = char const * cred_names[] =
@ -43,7 +44,8 @@ char const * cred_names[] =
"password", "password",
"channel", "channel",
"server", "server",
"port" "port",
"admins"
}; };
size_t const cred_names_len[] = size_t const cred_names_len[] =
@ -52,7 +54,8 @@ size_t const cred_names_len[] =
8, 8,
7, 7,
6, 6,
4 4,
6
}; };
creds_t creds = {0}; creds_t creds = {0};
@ -189,6 +192,10 @@ parse_pair(char const * buf, size_t len)
case CHANNEL: creds.channel = strndup(buf,x); break; case CHANNEL: creds.channel = strndup(buf,x); break;
case SERVER: creds.server = strndup(buf,x); break; case SERVER: creds.server = strndup(buf,x); break;
case PORT: creds.port = atoi(buf); break; case PORT: creds.port = atoi(buf); break;
case ADMINS:
parse_admins(strndup(buf,x));
break;
} }
if (x + 2 < len) if (x + 2 < len)
{ buf += x + 1; } { buf += x + 1; }
@ -203,48 +210,37 @@ parse_pair(char const * buf, size_t len)
return 0; return 0;
} }
DECL int /* WARNING: admin_string WILL be changed */
parse_admin_list(char const * admin_list_path) DECL void
parse_admins(char * admin_string)
{ {
#define ADMIN_LIST_INIT_SIZE 8 #define ADMIN_LIST_INIT_SIZE 8
/* prealloc with 8 */ /* prealloc with 8 */
size_t current_array_size = ADMIN_LIST_INIT_SIZE; size_t current_array_size = ADMIN_LIST_INIT_SIZE;
FILE * stream; size_t i = 0;
size_t lines_read = 0; char * token = NULL;
char * line = NULL;
size_t nread = 0;
stream = fopen(admin_list_path, "r");
if (stream == NULL)
{
/* Pretty valid case I guess? No admin file = no admins,
maybe all configuration is performed locally */
return 0;
}
admins = calloc(ADMIN_LIST_INIT_SIZE, sizeof(char *)); admins = calloc(ADMIN_LIST_INIT_SIZE, sizeof(char *));
while (getline(&line, &nread, stream) > 0) while ((token = strtok(admin_string, " ")))
{ {
if (++lines_read > current_array_size) if (++i > current_array_size)
{ {
/* double the space */ /* double the space */
current_array_size *= 2; current_array_size *= 2;
realloc(admins, current_array_size); admins = realloc(admins, current_array_size);
} }
admins[lines_read - 1] = line; admins[i - 1] = strdup(token);
} }
/* set up array end marker for proper clean-up later */ /* set up array end marker for proper clean-up later */
if (lines_read + 1 > current_array_size) if (i + 1 > current_array_size)
{ {
realloc(admins, current_array_size + 1); admins = realloc(admins, current_array_size + 1);
} }
admins[lines_read] = NULL; admins[i] = NULL;
return 0;
} }
DECL int DECL int