From 8e52ba7e6a8abb503c0120bd3b234f4d0ca5fc21 Mon Sep 17 00:00:00 2001 From: Arindam Guha Date: Wed, 1 Jun 2022 15:16:08 +0200 Subject: [PATCH] =?UTF-8?q?gj=C3=B8r=20det=20mulig=20=C3=A5=20bruke=20host?= =?UTF-8?q?=20og=20user=20i=20.toml-fila?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql-admutils.h | 6 ++---- mysql-dbadm.c | 3 ++- pwfile.c | 45 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/mysql-admutils.h b/mysql-admutils.h index ed70f2b..71c2e34 100644 --- a/mysql-admutils.h +++ b/mysql-admutils.h @@ -31,8 +31,7 @@ extern char **get_group_names(int *numgroups); extern int version(void); -extern int -read_toml_file(void); +extern const char* read_toml_file(void); /* same as strcpy, but returns a pointer to the end of dest instead of start */ extern char *strmov(char *, const char *); @@ -41,8 +40,7 @@ extern int name_isclean(char*); #ifdef _mysql_h -extern int -dberror(MYSQL *pmysql, char *format, ...); +extern int dberror(MYSQL *pmysql, char *format, ...); extern int reload(MYSQL *pmysql); diff --git a/mysql-dbadm.c b/mysql-dbadm.c index 6221a80..a945a9a 100644 --- a/mysql-dbadm.c +++ b/mysql-dbadm.c @@ -6,7 +6,6 @@ */ #include "config.h" -#include "mysql-admutils.h" #include #include @@ -17,7 +16,9 @@ #include #include #include +#include #include +#include "mysql-admutils.h" char * strchr_whitespace(const char *s) diff --git a/pwfile.c b/pwfile.c index 98bff51..2b2ed14 100644 --- a/pwfile.c +++ b/pwfile.c @@ -16,8 +16,8 @@ #include "toml.h" /* defaults for configurable values */ -const char* db_user = "root"; -const char* db_server = "mysql.stud.ntnu.no"; +const char* db_server = NULL; +const char* db_user = NULL; const char* db_passwd = NULL ; const char* db_name = "mysql"; @@ -30,14 +30,14 @@ FILE* fp; toml_table_t* conf; toml_table_t* server; const char* raw; +char* host; +char* user; char* password; char errbuf[200]; static char *tomlfile = SYSCONFDIR "/mysql-admutils.toml"; - -int -read_toml_file(void) +const char* read_toml_file(void) { if (0 == (fp = fopen(tomlfile, "r"))) { perror("fopen"); @@ -47,7 +47,7 @@ read_toml_file(void) conf = toml_parse_file(fp, errbuf, sizeof(errbuf)); fclose(fp); if (0 == conf) { - fprintf(stderr, "ERROR: %s\n", errbuf); + fprintf(stderr, "ERROR: parsing %s\n", errbuf); exit(1); } @@ -57,24 +57,51 @@ read_toml_file(void) exit(1); } + 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); + } + if (0 == (raw = toml_raw_in(server, "password"))) { fprintf(stderr, "ERROR: missing 'password' in [server]\n"); toml_free(conf); exit(1); } if (toml_rtos(raw, &password)) { - fprintf(stderr, "ERROR: bad value in 'host'\n"); + fprintf(stderr, "ERROR: bad value in 'password'\n"); toml_free(conf); exit(1); } toml_free(conf); + db_server = host; + db_user = user; db_passwd = password; - + return db_server; + return db_user; return db_passwd; + free(host); + free(user); free(password); - + exit(1); }