/*
	Auther:		Satoshi Yasuda	7m3tjz/ad6gz
*/

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<sys/socket.h>
#include	<netinet/in.h>
#include	<netinet/ip.h>
#include	"trust.h"

void	config(int argc, char **argv)
{
	char	buff[128];
	char	*delmi = "=\n\r\t\0";
	char	*pnt;
	FILE	*config_file;
	int	len;

	if (argc == 2)
	{
		config_file = fopen (argv[1], "r");
		if (!config_file)
		{
			logfile = fopen (LOGFILE, "a");
			now = time(NULL);
			fprintf (logfile, "%24.24s config file not found (%s)\n", ctime(&now), argv[1]);
			fclose (logfile);
			exit (1);
		}
	} 
	else
	{
		config_file = fopen ("/etc/dstrust.conf","r");
		if (!config_file)
		{
                        logfile = fopen (LOGFILE, "a");
                        now = time(NULL);
			fprintf (logfile, "%24.24s config file not found (/etc/dstrust.conf)\n", ctime(&now));
			fclose (logfile);
			exit (1);
		}
	}

	while (fgets (buff, 127, config_file))
	{
		if (buff[0] != '#')
		{
			pnt = strtok(buff, delmi);
			pnt = strtok(NULL,delmi);

			if (!memcmp (buff, "CMD_MODE", 8))
			{
				/* 1 stdin */
				/* 0 fifo  */
				cmdmode = atoi (pnt);
			}
			else if (!memcmp (buff, "TRUST_PORT", 10))
			{
				AuthPort = htons(atoi(pnt));
			}
			else if (!memcmp (buff, "TRUST_DB_IN_PORT", 16))
			{
				dbInPort = htons(atoi(pnt));
			}
			else if (!memcmp (buff, "TRUST_DB_OUT_PORT", 17))
			{
				dbOutPort = htons(atoi(pnt));
			}
                        else if (!memcmp (buff, "MONITOR_PORT", 12))
                        {
                                MonitorPort = htons(atoi(pnt));
                        }
                        else if (!memcmp (buff, "MONITOR_DOMAIN_NAME", 19))
                        {
                                memset (MonitorDomainName, 0x00, 128);
                                memcpy (MonitorDomainName, pnt, strlen(pnt));
                        }
                        else if (!memcmp (buff, "TRUST_NAME", 10))
                        {
				len = strlen(pnt);
				if (len > 16) len = 16;
                                memset (TrustName, 0x20, 16);
                                memcpy (TrustName, pnt, len);
                        }
                        else if (!memcmp (buff, "TRUST_DB_DOMAIN_NAME", 20))
                        {
                                memset (TrustDbDomainName, 0x00, 128);
                                memcpy (TrustDbDomainName, pnt, strlen(pnt));
                        }
		}
	}
	fclose (config_file);
}
