/*
 Module Name: Key
Programed By: Lynden Sherriff
Created Date: 8/1/99
	 Version: 1.0c
*/
/*
	Valid Defines:
		LYNDENS_REGO	- Used in DFax, WebIMAP, WebMail. The check key routine.

Modified by Matt to compile as stand-alone adt.
#define OLD_KEY_ADT to make compatible with original version

These are the functions you will probably want to use if not using OLD_KEY_ADT:

	int key_decode(pKEY key, char *skey, char *host, char *version);
	char *key_create_temp(pKEY key, long int users, int product_id, int flags, char *vrsn);
	char *key_make(pKEY key, int months, long int users, int product_id, int reg_num, int flags, char *host, char *vrsn);

*/


#ifndef KEY_HEADER
#	define KEY_HEADER



/* License types user limits and prices */
#	define DPOP_U1	500
#	define DPOP_P1	485 
#	define DPOP_U2	50
#	define DPOP_P2	285 
#	define DPOP_U3	5
#	define DPOP_P3	0 
#	define DPOP_U4	-1
#	define DPOP_P4	165
#	define DPOP_U5	0
#	define DPOP_P5	985 
#	define DPOP_U6	0
#	define DPOP_P6	3295 

/* currently defined flag values for keys */
#	define NO_KEY_TYPE			-1
#	define NONE_KEY				0x00

#	define NO_IPNUM				0x01
#	define VERSION_SPECIFIC		0x02
#	define DEALER_KEY			0x04
#	define FTEMP_KEY			0x08
#ifdef KEY_CODE			/* Only Key.c requires this */
#		define DMAILWEB_KEY			0x10
#		define CWMAIL_KEY			0x20
#		define JEEVES_KEY			0x40
#		define WEBIMAP_KEY			0x80

/* Below are stored in the last 4 bits of the month */
#		define WEBNEWS_KEY			0x100
#		define NETAUTH_KEY			0x200
#		define DFAX_KEY				0x400
#		define WAP_KEY				0x800
#endif



/* PRODUCT IDS */
	enum{
		ID_NONE, ID_DMAIL, ID_DMAILWEB, ID_CWMAIL, ID_DFAX, ID_WEBIMAP, ID_WEBMAIL, ID_WEBNEWS,
		ID_WAPMAIL, ID_NETAUTH, ID_JEEVES,
		ID_DBANNER, ID_DBANNER2, ID_DBABBLE,
		ID_SURGEFTP, ID_DNOTICE, ID_DNOTICE2, ID_WEBSHAREIT,ID_DNOTICE3,
		ID_SURGEMAIL,

		/* add more here 0-255 - must match entries in product array below */
		ID_LAST
	};

#ifdef KEY_CODE			/* Only Key.c requires this */
	typedef struct PRODUCT {
		int id;
		char *code;
		char *name;

		int old_key_mask;
	} product_rec;

	product_rec product[]={
		{ ID_NONE,		"none",		"Undefined Product",	NONE_KEY},		/* no product id - ie old style keys */
		{ ID_DMAIL,		"dm1",		"DMail",				NONE_KEY},		/* dsmtp and dpop license */
		{ ID_DMAILWEB,	"dm7",		"DMailWeb",				DMAILWEB_KEY},	/*  */
		{ ID_CWMAIL,	"cw1",		"CWMail",				CWMAIL_KEY},	/*  */
		{ ID_DFAX,		"df1",		"DFax",					DFAX_KEY},		/*  */
		{ ID_WEBIMAP,	"wi1",		"WebIMAP",				WEBIMAP_KEY},	/*  */
		{ ID_WEBMAIL,	"wm1",		"WebMail",				WEBIMAP_KEY},	/*  */
		{ ID_WEBNEWS,	"wn1",		"WebNews",				WEBNEWS_KEY},	/*  */
		{ ID_WAPMAIL,	"wap1",		"WAPMail",				WAP_KEY},		/*  */
		{ ID_NETAUTH,	"na1",		"Netauth",				NETAUTH_KEY},	/*  */
		{ ID_JEEVES,	"j1",		"Jeeves",				JEEVES_KEY},	/*  */
		{ ID_DBANNER,	"dbn1",		"DBanner",				NONE_KEY},		/* dbanner normal licencse */
		{ ID_DBANNER2,	"dbn2",		"DBanner",				NONE_KEY},		/* some special dbanner license */
		{ ID_DBABBLE,	"dbb1",		"DBabble",				NONE_KEY},		/* dbabble - may need more than 1 ? */
		{ ID_SURGEFTP,	"fp1",		"SurgeFTP",				NONE_KEY},		/* dbabble - may need more than 1 ? */
		{ ID_DNOTICE,	"dnt1",		"DNotice1",				NONE_KEY},		/*  */
		{ ID_DNOTICE2,	"dnt2",		"DNotice2",				NONE_KEY},		/*  */
		{ ID_WEBSHAREIT,"wsi1",		"webshareit",			NONE_KEY},		/*  */
		{ ID_DNOTICE2,	"dnt3",		"DNotice3",				NONE_KEY},		/*  */
		{ ID_SURGEMAIL,	"dsm1",		"SurgeMail",				NONE_KEY},		/*  */

		/* add more here  0-255 */
		{ ID_LAST,		NULL,		NULL,					NONE_KEY}		/* END OF LIST */
	};
#endif



/* Key info constants returned by new_test in reason */
/* KEY MASKS */
#	define	KEY_NONE_ENTERED		0x0000

#	define	KEY_POSSIBLE			0x0001
#	define	KEY_ERROR_MASK			0x0ff0

#	define	KEY_INVALID_TYPE		0x0010
#	define	KEY_WRONG_VERSION		0x0100

#	define	KEY_EXPIRED				0x0020
#	define	KEY_INVALID_TIME		0x0200

#	define	KEY_INVALID				0x0040
#	define	KEY_WRONG_PC			0x0400


#	define	KEY_FROM_FILE			0x4000
#	define	KEY_TEMPORARY			0x8000

	typedef struct key 
	{
		char expires[512];
		int users, flags;
		int key_info;

		int check_key;

		int product_id;
		int reg_num;
	} KEY;

	typedef struct key *pKEY;

	/******************************************************************/
#ifdef LYNDENS_REGO
#	include "vini.h"
	int check_rego(pINI_VIRTUAL ini, char *file, int product_id, char *version);
#else
#ifdef OLD_KEY_ADT
#	include "static.h"
#endif
#endif
	/******************************************************************/

	char *strtobin(char *s, int *xlen);
	char *bintostr(char *s, int len);
	int pc_encode(char *pcname);
	char *text_encrypt(char *s, int key);
	char *bin_encrypt(char *xs, int *nout);
	char *x_hide(char *key, char *pass, int day);
	char *x_unhide(char *key, char *hex, int day);

	/******************************************************************/
#ifndef OLD_KEY_ADT
	char *host_details(void);
#endif

#ifdef OLD_KEY_ADT
	int key_save_file(char *key, char *domain, char *path, char *filename);
	STATIC_LIST key_load_file(char *path, char *domain, char *file);

	int key_check(pKEY key, char *skey, char *path, char *filename, char *version, int product_id);
#endif
	int key_recheck_key(pKEY key);

	char *key_get_expires(pKEY key);
	int key_get_users(pKEY key);
	int key_get_flags(pKEY key);

	int key_get_product_id(pKEY key);
	int key_get_reg_num(pKEY key);


	int key_decode(pKEY key, char *skey, char *host, char *version);
	char *key_create_temp(pKEY key, int users, int product_id, int flags, char *vrsn);
	char *key_make(pKEY key, int months, int users, int product_id, int reg_num, int flags, char *host, char *vrsn);

	char *lib_unhide(char *key, char *hex, int day, char *result);
	char *lib_hide(char *key, char *pass, int day, char *output);

#endif
