#ifndef __CAPTIVE_H__
#define __CAPTIVE_H__


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>

#include <gdbm.h>


#define EVENT_LOGIN_SUCCESS			1
#define EVENT_LOGIN_FAIL_BAD_USERPASS		2
#define EVENT_LOGIN_FAIL_ACCOUNT_DISABLED	3
#define EVENT_LOGIN_FAIL_NO_IP			4
#define EVENT_LOGIN_FAIL_NO_MAC_IN_LEASE_FILE	5
#define EVENT_LOGOFF_TIMED			6
#define EVENT_LOGOFF_FORCED			7
#define EVENT_DATABASE_ERROR			8
#define EVENT_SYSTEM_FLUSH			9
#define EVENT_USER_ADD				10
#define EVENT_USER_DEL				11
#define EVENT_USER_MODIFY			12
#define EVENT_LOGOFF_ELECTIVE			13

#define MAX_NUMBER_OF_ACCOUNTS			1000   /* shouldn't be more than INT_MAX */


#define USERNAME_LEN      15
#define PASSWORD_LEN      15
#define MIN_PASSWORD_LEN   6
#define IP_LEN            20
#define MAC_LEN           20


#define IPTABLES_INS 1
#define IPTABLES_DEL 2

typedef struct {
        char password[PASSWORD_LEN];
        long login_dur_in_seconds;
        int enabled;

	int logged_in_now;
	char ip[IP_LEN];
	char mac[IP_LEN];
	long login_expires_at;
} userinfo;



int post_to_log( int event_code, char *account_name, char *ip, char *mac, long time_stamp );

void print_event_history( char *username );
void print_nmap_history( char *username );



int get_mac_from_dhcp_lease( char *ip, char *mac );

void iptables_rule( int action, char *ip, char *mac );

int printfile( char *filename );
char *timeleftstring( long secondsleft );
int valid_up( char *username, char *password, long *secondsleft );
int parse_post_data( char *buffer, char *username, char *password );

int nmap_portscan( char *username, char *ip );

void userinfoswap( userinfo *u1, userinfo *u2 );

#endif

