From 83734d597f8348add88d80a2feddc768f3a58e96 Mon Sep 17 00:00:00 2001
From: Aslak Raanes <aslakr@ntnu.no>
Date: Thu, 25 Oct 2018 15:50:16 +0200
Subject: [PATCH 1/3] la til submodule tomlc99 for parsing av toml

---
 .gitmodules | 3 +++
 tomlc99     | 1 +
 2 files changed, 4 insertions(+)
 create mode 100644 .gitmodules
 create mode 160000 tomlc99

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..d07db8b
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "tomlc99"]
+	path = tomlc99
+	url = https://github.com/cktan/tomlc99.git
diff --git a/tomlc99 b/tomlc99
new file mode 160000
index 0000000..f06bf3a
--- /dev/null
+++ b/tomlc99
@@ -0,0 +1 @@
+Subproject commit f06bf3a5ccdf60271a8cb25a066776297af8868a

From 2b815b30d3364df12b7649342d47bd5503cb6c4b Mon Sep 17 00:00:00 2001
From: Aslak Raanes <aslakr@ntnu.no>
Date: Fri, 26 Oct 2018 16:51:30 +0200
Subject: [PATCH 2/3] Tester bruk av tomlc99 istedenfor bruk av yacc/flex/bison

---
 toml.c | 1 +
 toml.h | 1 +
 2 files changed, 2 insertions(+)
 create mode 120000 toml.c
 create mode 120000 toml.h

diff --git a/toml.c b/toml.c
new file mode 120000
index 0000000..213424f
--- /dev/null
+++ b/toml.c
@@ -0,0 +1 @@
+tomlc99/toml.c
\ No newline at end of file
diff --git a/toml.h b/toml.h
new file mode 120000
index 0000000..d2d9814
--- /dev/null
+++ b/toml.h
@@ -0,0 +1 @@
+tomlc99/toml.h
\ No newline at end of file

From ef4add6a89462bc14189fc0607e4c90c7a446d86 Mon Sep 17 00:00:00 2001
From: Aslak Raanes <aslakr@ntnu.no>
Date: Fri, 26 Oct 2018 16:53:47 +0200
Subject: [PATCH 3/3] Fjerner filer som ikke lenger trengs

---
 Makefile.am      |   3 +-
 mysql-admutils.h |   3 +-
 mysql-dbadm.c    |   2 +-
 mysql-useradm.c  |   1 +
 pwfile.c         | 124 ++++++++++++++++++-----------------------------
 pwlex.l          |  47 ------------------
 pwyacc.y         |  33 -------------
 7 files changed, 53 insertions(+), 160 deletions(-)
 delete mode 100644 pwlex.l
 delete mode 100644 pwyacc.y

diff --git a/Makefile.am b/Makefile.am
index b65669f..8ee0389 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,8 +1,7 @@
 bin_PROGRAMS = mysql-dbadm mysql-useradm
-mysql_admutils_SOURCE = common.c mysql-admutils.h pwyacc.y pwlex.l pwfile.c
+mysql_admutils_SOURCE = common.c mysql-admutils.h toml.c pwfile.c
 mysql_dbadm_SOURCES = mysql-dbadm.c $(mysql_admutils_SOURCE)
 mysql_useradm_SOURCES = mysql-useradm.c $(mysql_admutils_SOURCE)
-BUILT_SOURCES = pwyacc.c pwyacc.h pwlex.c
 AM_YFLAGS = -d
 AM_CPPFLAGS = @MYSQL_INCLUDE@ -DSYSCONFDIR=\"$(sysconfdir)\"
 LDADD = @MYSQL_LIBS@ @LEXLIB@
diff --git a/mysql-admutils.h b/mysql-admutils.h
index 1ee0cdc..ed70f2b 100644
--- a/mysql-admutils.h
+++ b/mysql-admutils.h
@@ -14,6 +14,7 @@ extern const char *db_user;
 extern const char *db_passwd;
 extern const char *db_name;
 
+
 extern int
 wrong_use(char *format, ...);
 
@@ -31,7 +32,7 @@ extern int
 version(void);
 
 extern int
-read_config_file(void);
+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 *);
diff --git a/mysql-dbadm.c b/mysql-dbadm.c
index ffe32a0..6221a80 100644
--- a/mysql-dbadm.c
+++ b/mysql-dbadm.c
@@ -619,7 +619,7 @@ main(int argc, char *argv[])
   if ((command != c_show) && (argc < 3))
     return wrong_use(NULL);
 
-/*  read_config_file(); */
+  read_toml_file();
 
   /* connect to the database server and select the mysql database */
   if (!mysql_real_connect(&mysql, db_server, db_user, db_passwd, db_name, 0, NULL, 0))
diff --git a/mysql-useradm.c b/mysql-useradm.c
index f7d3e9a..258b82a 100644
--- a/mysql-useradm.c
+++ b/mysql-useradm.c
@@ -278,6 +278,7 @@ main(int argc, char *argv[])
     return wrong_use(NULL);
 
 /*  read_config_file(); */
+  read_toml_file();
 
   /* connect to the database server and select the mysql database */
   if (!mysql_real_connect(&mysql, db_server, db_user, db_passwd, db_name, 0, NULL, 0))
diff --git a/pwfile.c b/pwfile.c
index 433dd13..98bff51 100644
--- a/pwfile.c
+++ b/pwfile.c
@@ -12,15 +12,13 @@
 #include <unistd.h>
 #include <sys/resource.h>
 #include "mysql-admutils.h"
-#include "pwyacc.h"
-
-/* the MySQL maximum */
-#define MAXPWCHARS 16
+// #include "pwyacc.h"
+#include "toml.h"
 
 /* defaults for configurable values */
 const char* db_user = "root";
 const char* db_server = "mysql.stud.ntnu.no";
-const char* db_passwd = MYSQLPW ;
+const char* db_passwd = NULL ;
 const char* db_name = "mysql";
 
 extern int yyparse(void);
@@ -28,81 +26,55 @@ extern FILE *yyin;
 
 int config_line = 1;
 
-static FILE *pwfile;
-static char *filename = SYSCONFDIR "/mysql-admutils.conf";
+FILE* fp;
+toml_table_t* conf;
+toml_table_t* server;
+const char* raw;
+char* password;
+char errbuf[200];
 
-int
-read_config_file(void)
-{
-  struct rlimit rlim;
-  int rc; /* return code */
-
-  /* to stop the user from obtaining the password from a core dump. */
-  if (getrlimit(RLIMIT_CORE, &rlim) == -1) {
-    perror("getrlimit");
-    exit(1);
-  }
-  rlim.rlim_max = 0;
-  if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
-    perror("setrlimit");
-    exit(1);
-  }
-
-  pwfile = fopen(filename, "r");
-  if (!pwfile) {
-    fatal_error("cannot open configuration file %s", filename);
-    exit(1);
-  }
-
-  /* we don't need to be SUID anymore. */
-  if (seteuid(getuid()) != 0)
-    perror("seteuid");
-
-  yyin = pwfile;
-  rc = yyparse();
-  fclose(pwfile);
-
-  return rc;
-}
+static char *tomlfile = SYSCONFDIR "/mysql-admutils.toml";
 
 
 int
-yywrap(void)
+read_toml_file(void)
 {
-  return 1;
-}
-
-
-int
-yyerror(const char *msg)
-{
-  fprintf(stderr, "%s:%d: %s\n", filename, config_line, msg);
-  return 0;
-}
-
-
-int
-config_set_string_var(int var, const char *value)
-{
-  assert(value);
-  switch (var) {
-  case USER:
-    db_user = value;
-    break;
-  case HOST:
-    db_server = value;
-    break;
-  case PASSWORD:
-    if (strlen(value) > MAXPWCHARS) {
-      fprintf(stderr, "%s:%d: password is too long (%d chars > %d chars)\n", 
-	      filename, config_line, strlen(value), MAXPWCHARS);
-      fclose(pwfile);
-      exit(1);
+    if (0 == (fp = fopen(tomlfile, "r"))) {
+        perror("fopen");
+        exit(1);
     }
-    db_passwd = value;
-    break; 
-  default:
-    assert(!"We should never get here.");
-  }
-  return 0;
+
+    conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
+    fclose(fp);
+    if (0 == conf) {
+        fprintf(stderr, "ERROR: %s\n", errbuf);
+        exit(1);
+    }
+
+    if (0 == (server = toml_table_in(conf, "server"))) {
+        fprintf(stderr, "ERROR: missing [server]\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");
+          toml_free(conf);
+          exit(1);
+    }
+
+    toml_free(conf);
+
+    db_passwd = password;
+
+
+    return db_passwd;
+
+    free(password);
+
 }
diff --git a/pwlex.l b/pwlex.l
deleted file mode 100644
index 457ef74..0000000
--- a/pwlex.l
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * @(#) $Id: pwlex.l,v 1.1.1.1 2001-11-25 00:41:16 lkarsten Exp $
- *
- * lex source for the configuration file
- *
- */
-
-%{
-#include <stdio.h>
-#include <string.h>
-#include "pwyacc.h"
-
-extern int config_line;
-extern int yylval;
-%}
-
-%%
-
-#.*\n		config_line++;
-
-set		{ yylval = SET; return(SET); }
-host		{ yylval = HOST; return(HOST); };
-user		{ yylval = USER; return(USER); };
-password	{ yylval = PASSWORD; return(PASSWORD); }
-
-\"[^"]*\"	{
-		if (yytext[yyleng - 1] == '\\')
-			yymore();
-		else
-                        yytext[yyleng - 1] = '\0';
-                        yylval = (int)strdup(yytext + 1);
-			return(STRING);
-		};
-
-;		return(';');
-
-[^ \t\n\;]*	{
-                yylval = (int)strdup(yytext);
-                return(STRING);
-                }
-
-[ \t]		;
-
-\n		config_line++;
-
-%%
-
diff --git a/pwyacc.y b/pwyacc.y
deleted file mode 100644
index 1a39886..0000000
--- a/pwyacc.y
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * @(#) $Id: pwyacc.y,v 1.1.1.1 2001-11-25 00:41:16 lkarsten Exp $
- *
- * yacc source for parsing the configuration file
- *
- */
-
-%{
-int yyerror(const char *msg);
-int yylex(void);
-int config_set_string_var(int var, const char *value);
-%}
-
-%token SET HOST USER PASSWORD STRING
-
-%%
-
-input: ;
-input: statement input ;
-
-statement: SET variable STRING ';' 
-	{
-		$$ = config_set_string_var($2, (const char *)$3);
-	}
-
-;
-
-variable: USER | PASSWORD | HOST 
-	{ $$ = $1; }
-	;
-
-%%
-