/*
**	BPALogin v1.6 - lightweight portable BIDS2 login client
**	Copyright (c) 1999  Shane Hyde (shyde@trontech.com.au)
** 
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
** 
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
** 
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/ 

#include "bpalogin.h"

struct session s;
int debug_level = DEFAULT_DEBUG;

extern int errno;

int parse_parms(struct session *,char * conffile);
void usage();

void onconnected(int i)
{
}

void ondisconnected()
{
}

void critical(char *s)
{
	syslog(LOG_CRIT,"Critical error: %s\n",s);
	exit(1);
}

void debug(int l,char *s,...)
{
	va_list ap;
	va_start(ap,s);
	if(debug_level > l)
	{
		int pri;
		char buf[256];

		switch(l)
		{
		case 0:
			pri = LOG_INFO;
			break;
		case 1:
			pri = LOG_INFO;
			break;
		case 2:
		case 3:
		default:
			pri = LOG_DEBUG;
			break;
		}
		vsprintf(buf,s,ap);
		syslog(pri,"%s",buf);
	}
	va_end(ap);
}

void noncritical(char *s,...)
{
	char buf[256];

	va_list ap;
	va_start(ap,s);
	vsprintf(buf,s,ap);
	syslog(LOG_CRIT,buf);
	va_end(ap);
}

void onsignal(int i)
{
	debug(1,"Sigint caught\n");
	logout(0,&s);
	closelog();
	exit(1);
}

int main(int argc,char* argv[])
{
	int makedaemon = 1;
	char conffile[256];

	int c;

	signal(SIGINT,onsignal);
	signal(SIGHUP,onsignal);
	signal(SIGTERM,onsignal);

	strcpy(s.authserver,DEFAULT_AUTHSERVER);
	s.authport = DEFAULT_AUTHPORT;
	strcpy(s.username,"");
	strcpy(s.password,"");
	strcpy(s.loginprog,"");
	strcpy(conffile,DEFAULT_CONFFILE);
	strcpy(s.localaddress,"");

	while(1)
	{
		c = getopt(argc,argv,"c:u:p:a:n:Dd:l:");
		if(c == -1)
			break;
		switch(c)
		{
		case 'c':
			strncpy(conffile,optarg,MAXCONFFILE);
			break;
		case '?':
			usage();
			exit(1);
			break;
		}
	}

	if(!parse_parms(&s,conffile))
	{
		usage();
		exit(1);
	}

	optind = 1;
	while(1)	
	{
		c = getopt(argc,argv,"c:u:p:a:n:Dd:l:");
		if(c == -1)
			break;
		switch(c)
		{
		case 'D':
			makedaemon = 0;
			break;
		case 'c':
			break;
		case 'u':
			strncpy(s.username,optarg,MAXUSERNAME);
			break;
		case 'p':
			strncpy(s.password,optarg,MAXPASSWORD);
			break;
		case 'l':
			strncpy(s.localaddress,optarg,MAXLOCALADDRESS);
			break;
		case 'a':
			strncpy(s.authserver,optarg,MAXAUTHSERVER);
			break;
		case 'n':
			s.authport = atoi(optarg);
			break;
		case 'd':
			debug_level = atoi(optarg);
			break;
		case '?':
			break;
		case ':':
			break;
		}
	}

	if(makedaemon && fork())
		exit(0);

	openlog("bpalogin",LOG_PID,LOG_DAEMON);

	syslog(LOG_INFO,"BPALogin v1.6 - lightweight portable BIDS2 login client\n");

	if(!strcmp(s.username,""))
	{
		critical("Username has not been set");
		exit(1);
	}
	if(!strcmp(s.password,""))
	{
		critical("Password has not been set");
		exit(1);
	}
	s.debug = debug;
	s.critical = critical;
	s.noncritical = noncritical;
	s.onconnected = onconnected;
	s.ondisconnected = ondisconnected;

	while(mainloop(&s));

	exit(0);
}

int parse_parms(struct session *s,char * conffile)
{
	char buf[512];
	FILE * f;

	f = fopen(conffile,"rt");
	if(!f)
	{
		debug(0,"Cannot open conf file\n");
		return FALSE;
	}

	while(fgets(buf,400,f) != NULL)
	{
		char parm[100];
		char value[100];

		if(buf[0] == '#')
			continue;

		sscanf(buf,"%s %s",parm,value);	
		debug(1,"Parameter %s set to %s\n",parm,value);

		if(!strcasecmp(parm,"username"))
		{
			strcpy(s->username,value);
		}
		else if(!strcasecmp(parm,"password"))
		{
			strcpy(s->password,value);
		}
		else if(!strcasecmp(parm,"authserver"))
		{
			strcpy(s->authserver,value);
		}
		else if(!strcasecmp(parm,"localaddress"))
		{
			strcpy(s->localaddress,value);
		}
		else if(!strcasecmp(parm,"debuglevel"))
		{
			int v = atoi(value);
			debug_level = v;	
		}
		else if(!strcasecmp(parm,"loginprog"))
		{
			strcpy(s->loginprog,value);
		}
	}
	fclose(f);
	return TRUE;
}

void usage()
{
	printf("BPALogin v1.6 - lightweight portable BIDS2 login client\n");
	printf("Copyright (c) 1999 Shane Hyde (shyde@trontech.com.au)\n");
	printf("\nThis program is *not* a product of Big Pond Advance\n");
	printf("\nUsage: bpalogin [options], where options are:\n\n");
	printf(" -a authserver        Specifies the name of the authserver\n");
	printf(" -c conffile          Specifies the config file to read option\n");
	printf("                      settings from (default is /etc/bpalogin.conf)\n");
	printf(" -l localaddress      Specifies the local address to bind to\n");
	printf(" -p password          Specifies the password\n");
	printf(" -u username          Specifies the username\n");
	printf(" -n port              Specifies the authserver port\n");
	printf(" -D                   Dont run bpalogin as a daemon (run in foreground)\n");
	printf("\nNote that command line options override the values in the conffile\n");
	
}

int closesocket(int s)
{
	return close(s);
}

void socketerror(struct session *s, const char * str)
{
	char buf[200];
	sprintf(buf,"%s - %s",str,strerror(errno));
	s->noncritical(buf);
}
