/*
    File:       ShibMysql.cpp
    Contains:
*/

#define __SHIB_DEBUG__ 1

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "StringParser.h"
#include "QTSSModuleUtils.h"

#include "ShibMysql.h"
#include "mysql.h" 

/* tcpwrapper code */
/*
#ifndef lint
static char sccsid[] = "@(#) tcpdmatch.c 1.5 96/02/11 17:01:36";
#endif
*/

/* System libraries. */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <syslog.h>
#include <setjmp.h>
#include <string.h>

extern void exit();
extern int optind;
extern char *optarg;

#ifndef	INADDR_NONE
#define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
#endif

#ifndef S_ISDIR
#define S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
#endif

/* Application-specific. */

//#include "tcpd.h"
/*
#include "inetcf.h"
#include "scaffold.h"
*/

//static void usage();
//static void tcpdmatch();

/* tcpwrapper code */


// Constructor
ShibMysql::ShibMysql(char* host, char* name, char* user, char* pass, char* port, char* socket) {

	// char* directory;
	// char* sessionkey;
	// char* ip;
	// char* dss;

	if (socket != NULL) {
		if (__SHIB_DEBUG__) qtss_printf("ShibMysql::ShibMysql using socket %s, overriding host and port\n", socket);
		host="localhost";
		port="0";
	} else {
		if (host==NULL) host="localhost";
		if (port==NULL) port="3306";
	}


	if (__SHIB_DEBUG__) {
		if (host != NULL) qtss_printf("ShibMysql::ShibMysql host: %s\n", host);
		if (socket != NULL) qtss_printf("ShibMysql::ShibMysql socket: %s\n", socket);
		if (port != NULL) qtss_printf("ShibMysql::ShibMysql port: %s\n", port);

		if (name != NULL) qtss_printf("ShibMysql::ShibMysql name: %s\n", name);
		if (user != NULL) qtss_printf("ShibMysql::ShibMysql user: %s\n", user);
		if (pass != NULL) qtss_printf("ShibMysql::ShibMysql pass: %s\n", pass);
	}


//	if (__SHIB_DEBUG__) qtss_printf("ShibMysql::ShibMysql making database connection in constructor\n");

	mysql_init(&db);
	if (mysql_real_connect(&db, host, user, pass, name, atoi(port), socket, 0) == NULL) qtss_printf("ShibMysql::ShibMysql successful connect!\n");

}

// Destructor
ShibMysql::~ShibMysql() {
	mysql_close(&db);
//	if (__SHIB_DEBUG__) qtss_printf("ShibMysql::~ShibMysql thank you for shopping at kwiki-mart! :)");
//	if (__SHIB_DEBUG__) qtss_printf("\n\n");
}

Bool16 ShibMysql::set(char* d, char* s, char* i, char* dss) {
	if (d != NULL) directory = d;
	if (s != NULL) sessionkey = s;
	if (i != NULL) ip = i;
	if (dss != NULL) darwin = dss;

//	if (__SHIB_DEBUG__) qtss_printf("ShibMysql::set(\n\tclient_ip: %s\n\tsession_key: %s\n\tdirectory_path:%s\n\tdarwin:%s\n)\n", ip, sessionkey, directory, darwin);
	return true;
}

Bool16 ShibMysql::check() {
	//::snprintf(query,512,"select COUNT(*) as numrows from shib_sessions where client_ip='%s' and session_key='%s' and directory_path like '%s%%'", ip, sessionkey, directory);
	::snprintf(query,512,"select COUNT(*) as numrows from shib_sessions where client_ip='%s' and session_key='%s'", ip, sessionkey);
	mysql_query(&db, query);

	if ( rows() ) {
		if (__SHIB_DEBUG__) qtss_printf("ShibMysql::check() matching record found %s %s\n", ip, sessionkey);
		if (! checkDarwinSession()) {
			if (__SHIB_DEBUG__) qtss_printf("ShibMysql::check() matching shib_darwin not found on initial insert, inserting.\n");
			snprintf(query, 512, "insert into shib_darwin(client_ip, darwin_session) values ('%s', '%s')", ip, darwin);
			if(mysql_query(&db, query) != 0) {
				if (__SHIB_DEBUG__) qtss_printf("ShibMysql::check() %s\n", mysql_error(&db));
				return false;
			}
		}
//		mysql_close(&db);
		return true;
	} else {
		if (__SHIB_DEBUG__) qtss_printf("ShibMysql::check() no matching record found for %s %s\n", ip, sessionkey);
		return false;
	}
}    

Bool16 ShibMysql::checkDarwinSession() {
	::snprintf(query,512,"select COUNT(*) as numrows from shib_darwin where client_ip='%s' and darwin_session='%s'", ip, darwin);
	mysql_query(&db, query);
	if ( rows() ) return true;
	return false;
}

int ShibMysql::rows() {
	MYSQL_RES* sqlres = mysql_store_result(&db);
	if(sqlres == NULL) {
		if (__SHIB_DEBUG__) qtss_printf("ShibMysql::rows() failed to store result: %s\n", mysql_error(&db));
		return 0;
	}
	MYSQL_ROW numrows = mysql_fetch_row(sqlres);
	return atoi(numrows[0]);
}

Bool16 ShibMysql::clearDarwinSessions() {
	::snprintf(query,512,"delete from shib_darwin");
	if(mysql_query(&db, query) != 0) {
		if (__SHIB_DEBUG__) qtss_printf("ShibMysql::clearDarwinSessions() had some error: %s\n", mysql_error(&db));
		return false;
	}
	else {
		if (__SHIB_DEBUG__) qtss_printf("ShibMysql::clearDarwinSessions() deleted shib_darwin sessions\n");
		return true;
	}
}
