Merge pull request #5 in APPDRIFT/mysql-admutils from feature/toml to develop
* commit 'ef4add6a89462bc14189fc0607e4c90c7a446d86': Fjerner filer som ikke lenger trengs Tester bruk av tomlc99 istedenfor bruk av yacc/flex/bison la til submodule tomlc99 for parsing av toml
This commit is contained in:
commit
09975dc0e0
|
@ -0,0 +1,3 @@
|
|||
[submodule "tomlc99"]
|
||||
path = tomlc99
|
||||
url = https://github.com/cktan/tomlc99.git
|
|
@ -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@
|
||||
|
|
|
@ -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 *);
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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))
|
||||
|
|
124
pwfile.c
124
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);
|
||||
|
||||
}
|
||||
|
|
47
pwlex.l
47
pwlex.l
|
@ -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++;
|
||||
|
||||
%%
|
||||
|
33
pwyacc.y
33
pwyacc.y
|
@ -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; }
|
||||
;
|
||||
|
||||
%%
|
||||
|
|
@ -0,0 +1 @@
|
|||
Subproject commit f06bf3a5ccdf60271a8cb25a066776297af8868a
|
Loading…
Reference in New Issue