135 lines
4.0 KiB
C
135 lines
4.0 KiB
C
|
/* OpenVPN - Squid Load Balancer*
|
||
|
*------------------------------*
|
||
|
* Version 1.0 *
|
||
|
* Written by Christoph Haas *
|
||
|
* License: LGPL *
|
||
|
* 12.10.2010 *
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#include <stdarg.h>
|
||
|
#include <mysql/mysql.h>
|
||
|
#include <regex.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define __MAX_LINE__ 100
|
||
|
#define __MAX_FIELD__ 255
|
||
|
#define false 0
|
||
|
#define true 1
|
||
|
|
||
|
|
||
|
MYSQL mysql;
|
||
|
|
||
|
int connect_db(char *config)
|
||
|
{
|
||
|
FILE *db_data = NULL;
|
||
|
regex_t reg;
|
||
|
char user[__MAX_LINE__] = "";
|
||
|
char passwd[__MAX_LINE__] = "";
|
||
|
char database[__MAX_LINE__] = "";
|
||
|
char host[__MAX_LINE__] = "";
|
||
|
int port = 0;
|
||
|
char tmp[__MAX_LINE__];
|
||
|
char buf[__MAX_LINE__];
|
||
|
int a, b;
|
||
|
int length;
|
||
|
|
||
|
if((db_data = fopen(config, "r")) == NULL)
|
||
|
{
|
||
|
fprintf(stderr, "Can't open %s for reading.\n", config);
|
||
|
return false;
|
||
|
}
|
||
|
regcomp(®, "^([a-zA-Z0-9:]+)[ ][a-zA-Z0-9]+$", REG_EXTENDED | REG_NEWLINE);
|
||
|
while(fgets(buf, __MAX_LINE__, db_data))
|
||
|
{
|
||
|
length = strlen(buf);
|
||
|
if(length < 3)continue;
|
||
|
if(length >= __MAX_LINE__)
|
||
|
{
|
||
|
fclose(db_data);
|
||
|
regfree(®);
|
||
|
fprintf(stderr, "To long line in config file.\n");
|
||
|
return false;
|
||
|
}
|
||
|
if(buf[0] == '#')continue;
|
||
|
if(regexec(®, buf, 0, 0, 0))
|
||
|
{
|
||
|
fclose(db_data);
|
||
|
regfree(®);
|
||
|
fprintf(stderr, "Syntax error in config file.\n");
|
||
|
return false;
|
||
|
}
|
||
|
for(a = 0, b = 0; a < strlen(buf); a++)
|
||
|
{
|
||
|
if(buf[a] == ' ')b++;
|
||
|
}
|
||
|
if(b != 1)
|
||
|
{
|
||
|
fclose(db_data);
|
||
|
regfree(®);
|
||
|
fprintf(stderr, "Syntax error in config file.\n");
|
||
|
return false;
|
||
|
}
|
||
|
if((strncmp(buf, "user: ", 6)) == 0)sscanf(buf, "%s %s", tmp, user);
|
||
|
if((strncmp(buf, "passwd: ", 8)) == 0)sscanf(buf, "%s %s", tmp, passwd);
|
||
|
if((strncmp(buf, "database: ", 10)) == 0)sscanf(buf, "%s %s", tmp, database);
|
||
|
if((strncmp(buf, "host: ", 6)) == 0)sscanf(buf, "%s %s", tmp, host);
|
||
|
if((strncmp(buf, "port: ", 6)) == 0)sscanf(buf, "%s %d", tmp, &port);
|
||
|
}
|
||
|
fclose(db_data);
|
||
|
regfree(®);
|
||
|
if((strlen(user) < 1) || (strlen(passwd) < 1) ||
|
||
|
(strlen(database) < 1) || (strlen(host) < 1))
|
||
|
{
|
||
|
fprintf(stderr, "One value for MySQL connection isn't set. \
|
||
|
Please set user, passwd, database and host.\n");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
|
mysql_init(&mysql);
|
||
|
if((mysql_real_connect(&mysql, host, user, passwd, database, port, NULL, 0)) == NULL)
|
||
|
{
|
||
|
fprintf(stderr, "%s\n", mysql_error(&mysql));
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char **argv)
|
||
|
{
|
||
|
MYSQL_RES *result;
|
||
|
MYSQL_ROW row;
|
||
|
char Query[200]= "SELECT * FROM userdata WHERE VPNId = '\0";
|
||
|
|
||
|
if(argc == 2)
|
||
|
{
|
||
|
/*iID = atoi(argv[1]);
|
||
|
printf("Int:%d\n", iID);
|
||
|
sprintf(ID,"%-#10x",iID);
|
||
|
printf("String:%s\n", ID);*/
|
||
|
|
||
|
connect_db("/home/christoph/squid_dynamic/db.conf"); // Datenbank Verbindung aufbauen
|
||
|
|
||
|
strcat(Query, argv[1]); // Query builden
|
||
|
strcat(Query,"'"); //
|
||
|
|
||
|
mysql_query(&mysql, Query); // Query abschicken
|
||
|
result = mysql_store_result(&mysql); // Result speichern
|
||
|
|
||
|
while ((row = mysql_fetch_row(result))) // Alle Datensätze auslesen (in dem Fall eh nur einer)
|
||
|
{
|
||
|
printf("%s:%s\n", row[9], row[10]); // Feld 9 und 10 (login/pass) aus dem datensatz ausgeben
|
||
|
}
|
||
|
|
||
|
mysql_free_result(result); // Result wieder löschen
|
||
|
mysql_close(&mysql); // Datenbank Verbindung schliesen
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
printf("USAGE: sqidy xx | xx = VPNId\n"); // bei flascheingabe der Parameter
|
||
|
}
|
||
|
}
|