⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rulefile.cpp

📁 JK Proxy Project - Version 0.1 ------------------------------ This was going to be a proxy serve
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
+-----------------------------------------------------------------------------+
|                                                                             |
|   rulefile.cpp                                                              |
|   Last change: yyyy-mm-dd                                                   |
|                                                                             |
|   Author: Jan Krumsiek                                                      |
|   eMail:  proxy@krumsiek.com                                                |
|   Web:    http://www.krumsiek.com/proxy                                     |
|                                                                             |
|   Copyright 2003 - Jan Krumsiek                                             |
|   This source code can freely be modified and redistributed. No liability   |
|   whatsoever is taken by the author for any use of this software.           |
|                                                                             |
|   Description:                                                              |
|   This module loads a rulefile (i.e. a file that contains rules which are   |
|   used to indicate if a message is blocked or may be passed) and creates a  |
|   RULEFILE struct which can then be used by the rule processor.             |
|                                                                             |
+-----------------------------------------------------------------------------+
*/

#include "proxymain.h"
#include "filesystem.h"
#include "cfgfiles.h"
#include "rulefile.h"
#include "patternmatch.h"

#include "iostream.h"
#include <map>

// prototypes
RULE_COMMAND StrToCommand(CHARPTR command);
UINT AddRule(CHARPTR* parameters, RULESETPTR ruleset);
UINT AddList(CHARPTR* parameters, LISTMAPPTR listmap);
void SetFunctPointer(RULE* rule, RULE_TYPE type, bool insensitive);
int strstrcaps (const char* x, const char* y);
int stristrcaps (const char* x, const char* y);
int strpatterncaps (const char* x, const char* y);
RULE_TYPE StrToRuletype(CHARPTR command);
char* stristr(const char *szSrc,const char *szFind );

CHARPTR curfile;		// Filename of current file
CHARPTR curfiledir;		// Directory of current file
UINT curline;			// Current line
LISTMAPPTR vlistmap;	// Listmap must be globally accessible

RULEFILE* LoadRules(CHARPTR file) // exported
// loads a set of rules from a file and returns a pointer to the set
{
	COMMAND cur;
	RULE_COMMAND cur_cmd;

	// create map of lists
	vlistmap = new LISTMAP;
	// create new ruleset
	RULESETPTR vruleset = new RULESET;
	// ruleset and map of lists are combined in the RULEFILE struct
	RULEFILE* vreturn = new RULEFILE;
	// save directory of this config file
	curfiledir = ExtractDir(file);

	UINT retval;
	

	// first of all open the file
	int ret = OpenConfigFile(file);
	
	// error?
	if (ret == 0) return 0;

	// now iterate through all lines
	cur = GetNextFullCommand();

	curfile = file;

	while (cur.command != 0)
	{

		// find out which command this is (convert the string to an integer)
		cur_cmd = StrToCommand(cur.command);

		// save current line
		curline = cur.line;

		// which command is it?
		switch (cur_cmd)
		{
		case C_RULE:
			// it is a rule, call AddRule()
			retval = AddRule(cur.paras,vruleset);
			if (retval != 0)
			{
				// if return value of AddRule != -> something went wrong
				// return 0 to indicate error
				return 0;
			}
			break;

		case C_LIST:
			// A list to be loaded, call AddList()
			AddList(cur.paras,vlistmap);
			break;

		case C_EMPTY:
			// empty line, go on
			break;

		default:
			// invalid command
			cout << endl << "Error in " << curfile << "(" << curline << ")"
				 << ": Unknown command '" << cur.command << "'";
			// return zero to indicate error
			return 0;
		}
		
 
		// release memory allocated for this command
		ReleaseCommand(&cur);
		// get next command
		cur = GetNextFullCommand();
	}
	


	// release file (= deallocate memory)
	ReleaseConfigFile();

	// set return value
	vreturn->rules = vruleset;
	vreturn->list  = vlistmap;


	return vreturn;
}

void ReleaseRuleFile(RULEFILE* rulefile)
// dellocates all memory that has been allocated for a RULEFILE struct
{
	UINT i,n;
	LIST* curlist;
	RULE* currule;

	// first of all delete lists

	// declare iterator
	std::map<std::string, LIST*>::const_iterator iter;
	// iterate throug map
	for (iter = rulefile->list->begin(); iter != rulefile->list->end(); iter++)
	{
		// get current list
		curlist = iter->second;

		// iterate through list and delete each item
		for (i=0;i<curlist->count;i++)
			delete curlist->items[i];

		// now delete array pointer of list
		delete curlist->items;

		// delete list pointer itsself
		delete curlist;
	}
	// delete list-map
	delete rulefile->list;


	// delete rule structs
	n = rulefile->rules->size();

	for( i=0; i<n ; i++ )
	{
		// get rule from vector
		currule = (*(rulefile->rules))[i];

		// only 'fieldname' is a pointer that has to be deleted
		delete currule->fieldname;

		// now delete rule struct itself
		delete currule;

	}

	// delete vector
	delete rulefile->rules;


	// finished... delete RULEFILE struct
	delete rulefile;

	// done
}

UINT AddRule(CHARPTR* parameters, RULESETPTR ruleset)
// adds a single rule to the ruleset 'ruleset'
// return 0 for success
{
	RULE* newrule = new RULE;
	RULE_TYPE ruletype;
	bool issensitive;
	int i_conv;

	memset(newrule,0,sizeof(RULE));

	// now check each parameter of this line, if something is wrong
	// return 1 (= non-zero) and output error msg

	// 1. parameter: field (header or body)
	// must be 'header' or 'body'
	if (stricmp(parameters[0],"header") == 0)
		// header rule
		newrule->field = F_HEADER;
	else if (stricmp(parameters[0],"body") == 0)
		// body rule
		newrule->field = F_BODY;
	else if (stricmp(parameters[0],"email") == 0)
		// email rules
	{
		// internally we store "email" as HEADER field with field name [char value 255]
		newrule->field = F_HEADER;
		newrule->fieldname = (CHARPTR)CopyAndPtr("\xFF",1);
	}
	else if (stricmp(parameters[0],"size") == 0)
		// size rule
		
		newrule->field = F_SIZE;
	else
	{
		// wrong parameter, output error
		cout << endl << "Error in " << curfile << "(" << curline << ")"
			 << ": 1. parameter must be 'header', 'body' or 'email'";
		return 1;
	}

	// now check if it is a 'size' rule. (size rules take completely differen parameters)

	if (newrule->field != F_SIZE)
	{
		// *** NO SIZE RULE

		// 2. parameter: name of header field
		// if field = F_HEADER and 2. para empty and newrule->fieldname[0] != 255 -> error (actually 255 is -1 -> signed char)
		if ((strlen(parameters[1]) == 0) && (newrule->field == F_HEADER) && (newrule->fieldname[0] != -1))
		{
			cout << endl << "Error in " << curfile << "(" << curline << ")"
				 << ": 2. parameter may not be empty if field = 'header'";

			return 1;
		}
		else
		{
			// don't copy if newrule->fieldname already set (-> email field)
			if (newrule->fieldname == 0)
			{
				// copy string
				newrule->fieldname = (CHARPTR)CopyAndPtr(parameters[1],strlen(parameters[1]));
				// lowercase (for string comparison)
				strlwr(newrule->fieldname);
			}
		}
		
		// 3. parameter: rule type (contains, equals ... etc.)

		// convert from string to integer
		ruletype = StrToRuletype(parameters[2]);

		if (ruletype == R_UNKNOWN)
		{
			// unknown type -> error
			cout << endl << "Error in " << curfile << "(" << curline << ")"
				 << ": 3. parameter must be 'equals', 'notequals', 'contains', 'notcontains', 'pattern' or 'notpattern'";

			return 1;
		}

		// 4. parameter: case sensitivity
		// C for sensitive, I for insensitive
		if (stricmp(parameters[3],"C") == 0)
			issensitive = true;
		else if (stricmp(parameters[3],"I") == 0)
			issensitive = false;
		else
		{
			// error
			cout << endl << "Error in " << curfile << "(" << curline << ")"
				 << ": 4. parameter must be 'C' (case-sensitive) or 'I' (case-insensitive)";
			return 1;
			
		}

		// Now combine parameter 3 and 4 to get the pointer to the
		// comparison function and the negation value 
		// (read comment in SetFunctPointer() for detailled description of "negation" value)
		SetFunctPointer(newrule,ruletype,issensitive);

		// 5. parameter: value
		// if first byte is a @ -> pointer to list
		if (*parameters[4] == '@')
		{
			// is list

			// check if list already defined before
			LIST* check;
			check = (*vlistmap)[&(parameters[4])[1]];
			// if check == 0 then list doesn't exist
			if (check != 0)
			{
				// list exits. set a pointer to it
				newrule->islist = true;
				newrule->value = check;

				LIST* ts = (LIST*) newrule->value;
				ts = ts;
			}
			else
			{
				// list is not defined yet -> error
				cout << endl << "Error in " << curfile << "(" << curline << ")"
					 << ": 5. parameter - list '" << &(parameters[4])[1] << "' does not exist or is not loaded yet";
				return 1;
				
			}


		}
		// no list, must be a value in quotes ""
		else if	(*parameters[4] == '"')
		{
			// Find closing quote

			// Point to last byte
			CHARPTR search = parameters[4] + strlen(parameters[4]) - 1;

			// Go backwards to find closing quite
			while (*search != '\"') 
				search--;
			search--;

			// Copy
			newrule->islist = false;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -