Improved credentials parsing scalability
This commit is contained in:
parent
e375231b8b
commit
7880fd95fb
@ -1 +1 @@
|
|||||||
probotic
|
username=probotic
|
54
src/parse.c
54
src/parse.c
@ -24,6 +24,20 @@
|
|||||||
#include "parse.h"
|
#include "parse.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
|
||||||
|
#define PARAMS_COUNT 2
|
||||||
|
|
||||||
|
enum cred_param_ids_e
|
||||||
|
{
|
||||||
|
USERNAME,
|
||||||
|
PASSWORD
|
||||||
|
};
|
||||||
|
|
||||||
|
/* TODO: move = to the handler */
|
||||||
|
char const * cred_param_names_g[] = {
|
||||||
|
"username=",
|
||||||
|
"password="
|
||||||
|
};
|
||||||
|
|
||||||
DELC void
|
DELC void
|
||||||
parse_command(char * cmd)
|
parse_command(char * cmd)
|
||||||
{
|
{
|
||||||
@ -71,21 +85,39 @@ parse_creds(creds_t * creds,
|
|||||||
if (stream == NULL)
|
if (stream == NULL)
|
||||||
{ PERROR(PROGN); }
|
{ PERROR(PROGN); }
|
||||||
|
|
||||||
if (getline(&(creds->username), &nread, stream) < 1)
|
char * values[PARAMS_COUNT];
|
||||||
|
memset(values, 0, sizeof(char *) * PARAMS_COUNT);
|
||||||
|
|
||||||
|
char * line = NULL;
|
||||||
|
while (getline(&line, &nread, stream) > 0)
|
||||||
{
|
{
|
||||||
ERRMSG("Cannot get username");
|
for (size_t i = 0; i < PARAMS_COUNT; i++)
|
||||||
goto fail;
|
{
|
||||||
|
size_t param_len = strlen(cred_param_names_g[i]);
|
||||||
|
|
||||||
|
if (!strncmp(cred_param_names_g[i], line, param_len))
|
||||||
|
{
|
||||||
|
/* Starts with the current parameter specifier */
|
||||||
|
char const * val = line + param_len;
|
||||||
|
size_t val_len = strlen(val);
|
||||||
|
/* Duplicates and gets rid of a newline */
|
||||||
|
values[i] = strndup(val, val[val_len - 1] == '\n' ? val_len - 1 : val_len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getline(&(creds->password), &nread, stream) < 1)
|
free(line);
|
||||||
{
|
line = NULL;
|
||||||
/* Bot credentials file with an empty password is a valid case.
|
nread = 0;
|
||||||
* Considering irc_connect api the pointer to the password should be NULL in that case.
|
}
|
||||||
*/
|
creds->username = values[USERNAME];
|
||||||
|
creds->password = values[PASSWORD];
|
||||||
|
|
||||||
/* Theoretically it can be allocated with an empty string */
|
/* Check empty but required paramters */
|
||||||
free(creds->password);
|
if (!creds->username)
|
||||||
creds->password = NULL;
|
{
|
||||||
|
ERRMSG("Could not parse bot login");
|
||||||
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
|
Reference in New Issue
Block a user