2001-11-25 01:41:16 +01:00
|
|
|
/*
|
2007-02-27 15:10:08 +01:00
|
|
|
* @($) $Id: pwfile.c,v 1.3 2007-02-27 14:10:08 geirha Exp $
|
2001-11-25 01:41:16 +01:00
|
|
|
*
|
|
|
|
* functions for parsing the config file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2007-02-27 15:10:08 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-11-25 01:41:16 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include "mysql-admutils.h"
|
2018-10-26 16:53:47 +02:00
|
|
|
// #include "pwyacc.h"
|
|
|
|
#include "toml.h"
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
/* defaults for configurable values */
|
2022-06-01 15:16:08 +02:00
|
|
|
const char* db_server = NULL;
|
|
|
|
const char* db_user = NULL;
|
2018-10-26 16:53:47 +02:00
|
|
|
const char* db_passwd = NULL ;
|
2001-11-25 01:41:16 +01:00
|
|
|
const char* db_name = "mysql";
|
|
|
|
|
|
|
|
extern int yyparse(void);
|
|
|
|
extern FILE *yyin;
|
|
|
|
|
|
|
|
int config_line = 1;
|
|
|
|
|
2018-10-26 16:53:47 +02:00
|
|
|
FILE* fp;
|
|
|
|
toml_table_t* conf;
|
|
|
|
toml_table_t* server;
|
|
|
|
const char* raw;
|
2022-06-01 15:16:08 +02:00
|
|
|
char* host;
|
|
|
|
char* user;
|
2018-10-26 16:53:47 +02:00
|
|
|
char* password;
|
|
|
|
char errbuf[200];
|
2001-11-25 01:41:16 +01:00
|
|
|
|
2018-10-26 16:53:47 +02:00
|
|
|
static char *tomlfile = SYSCONFDIR "/mysql-admutils.toml";
|
2001-11-25 01:41:16 +01:00
|
|
|
|
2022-06-01 15:16:08 +02:00
|
|
|
const char* read_toml_file(void)
|
2001-11-25 01:41:16 +01:00
|
|
|
{
|
2018-10-26 16:53:47 +02:00
|
|
|
if (0 == (fp = fopen(tomlfile, "r"))) {
|
|
|
|
perror("fopen");
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-11-25 01:41:16 +01:00
|
|
|
|
2018-10-26 16:53:47 +02:00
|
|
|
conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
|
|
|
|
fclose(fp);
|
|
|
|
if (0 == conf) {
|
2022-06-01 15:16:08 +02:00
|
|
|
fprintf(stderr, "ERROR: parsing %s\n", errbuf);
|
2018-10-26 16:53:47 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2001-11-25 01:41:16 +01:00
|
|
|
|
2018-10-26 16:53:47 +02:00
|
|
|
if (0 == (server = toml_table_in(conf, "server"))) {
|
|
|
|
fprintf(stderr, "ERROR: missing [server]\n");
|
|
|
|
toml_free(conf);
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-11-25 01:41:16 +01:00
|
|
|
|
2022-06-01 15:16:08 +02:00
|
|
|
if (0 == (raw = toml_raw_in(server, "host"))) {
|
|
|
|
fprintf(stderr, "ERROR: missing 'host' in [server]\n");
|
|
|
|
toml_free(conf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (toml_rtos(raw, &host)) {
|
|
|
|
fprintf(stderr, "ERROR: bad value in 'host'\n");
|
|
|
|
toml_free(conf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == (raw = toml_raw_in(server, "user"))) {
|
|
|
|
fprintf(stderr, "ERROR: missing 'user' in [server]\n");
|
|
|
|
toml_free(conf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (toml_rtos(raw, &user)) {
|
|
|
|
fprintf(stderr, "ERROR: bad value in 'user'\n");
|
|
|
|
toml_free(conf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-10-26 16:53:47 +02:00
|
|
|
if (0 == (raw = toml_raw_in(server, "password"))) {
|
|
|
|
fprintf(stderr, "ERROR: missing 'password' in [server]\n");
|
|
|
|
toml_free(conf);
|
|
|
|
exit(1);
|
2001-11-25 01:41:16 +01:00
|
|
|
}
|
2018-10-26 16:53:47 +02:00
|
|
|
if (toml_rtos(raw, &password)) {
|
2022-06-01 15:16:08 +02:00
|
|
|
fprintf(stderr, "ERROR: bad value in 'password'\n");
|
2018-10-26 16:53:47 +02:00
|
|
|
toml_free(conf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
toml_free(conf);
|
|
|
|
|
2022-06-01 15:16:08 +02:00
|
|
|
db_server = host;
|
|
|
|
db_user = user;
|
2018-10-26 16:53:47 +02:00
|
|
|
db_passwd = password;
|
|
|
|
|
2022-06-01 15:16:08 +02:00
|
|
|
return db_server;
|
|
|
|
return db_user;
|
2018-10-26 16:53:47 +02:00
|
|
|
return db_passwd;
|
|
|
|
|
2022-06-01 15:16:08 +02:00
|
|
|
free(host);
|
|
|
|
free(user);
|
2018-10-26 16:53:47 +02:00
|
|
|
free(password);
|
2001-11-25 01:41:16 +01:00
|
|
|
}
|