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

📄 htrules.c

📁 用于linux和其他unix下面的
💻 C
📖 第 1 页 / 共 2 页
字号:
/*	Configuration manager for Hypertext Daemon		HTRules.c**	==========================================****** History:**	 3 Jun 91	Written TBL**	10 Aug 91	Authorisation added after Daniel Martin (pass, fail)**			Rule order in file changed**			Comments allowed with # on 1st char of rule line**	17 Jun 92	Bug fix: pass and fail failed if didn't contain '*' TBL**	 1 Sep 93	Bug fix: no memory check - Nathan Torkington**			BYTE_ADDRESSING removed - Arthur Secret**	11 Sep 93  MD	Changed %i into %d in debug printf.**			VMS does not recognize %i.**			Bug Fix: in case of PASS, only one parameter to printf.**	19 Sep 93  AL	Added Access Authorization stuff.**	 1 Nov 93  AL	Added htbin.**	25 May 99  KW	Added redirect for lynx.***/#include <HTUtils.h>/* (c) CERN WorldWideWeb project 1990,91. See Copyright.html for details */#include <HTRules.h>#include <HTFile.h>#include <LYLeaks.h>#include <HTAAProt.h>#define LINE_LENGTH 256typedef struct _rule {	struct _rule *	next;	HTRuleOp	op;	char *		pattern;	char *		equiv;	char *		condition_op; /* as strings - may be inefficient, */	char *		condition;    /* but this is not for a server - kw */} rule;#ifndef NO_RULES#include <HTTP.h> /* for redirecting_url, indirectly HTPermitRedir - kw */#include <LYGlobalDefs.h> /* for LYUserSpecifiedURL - kw */#include <LYUtils.h>		/* for LYFixCursesOn - kw */#include <HTAlert.h>/*	Global variables**	----------------*/PUBLIC char *HTBinDir = NULL;	/* Physical /htbin directory path.	*/				/* In future this should not be global. */PUBLIC char *HTSearchScript = NULL;	/* Search script name.		*//*	Module-wide variables**	---------------------*/PRIVATE rule * rules = 0;	/* Pointer to first on list */#ifndef PUT_ON_HEADPRIVATE rule * rule_tail = 0;	/* Pointer to last on list */#endif/*	Add rule to the list					HTAddRule()**	--------------------****  On entry,**	pattern		points to 0-terminated string containing a single "*"**	equiv		points to the equivalent string with * for the**			place where the text matched by * goes.**  On exit,**	returns		0 if success, -1 if error.*/PUBLIC int HTAddRule ARGS5(    HTRuleOp,		op,    CONST char *,	pattern,    CONST char *,	equiv,    CONST char *,	cond_op,    CONST char *,	cond){ /* BYTE_ADDRESSING removed and memory check - AS - 1 Sep 93 */    rule *	temp;    char *	pPattern = NULL;    temp = typecalloc(rule);    if (temp==NULL)	outofmem(__FILE__, "HTAddRule");    if (equiv) {		/* Two operands */	char *	pEquiv = NULL;	StrAllocCopy(pEquiv, equiv);	temp->equiv = pEquiv;    } else {	temp->equiv = 0;    }    if (cond_op) {	StrAllocCopy(temp->condition_op, cond_op);	StrAllocCopy(temp->condition, cond);    }    StrAllocCopy(pPattern, pattern);    temp->pattern = pPattern;    temp->op = op;    if (equiv) {	CTRACE((tfp, "Rule: For `%s' op %d `%s'", pattern, op, equiv));    } else {	CTRACE((tfp, "Rule: For `%s' op %d", pattern, op));    }    if (cond_op) {	CTRACE((tfp, "\t%s %s\n", cond_op, NONNULL(cond)));    } else {	CTRACE((tfp, "\n"));    }    if (!rules) {#ifdef LY_FIND_LEAKS	atexit(HTClearRules);#endif    }#ifdef PUT_ON_HEAD    temp->next = rules;    rules = temp;#else    temp->next = 0;    if (rule_tail) rule_tail->next = temp;    else rules = temp;    rule_tail = temp;#endif    return 0;}/*	Clear all rules						HTClearRules()**	---------------**** On exit,**	There are no rules**** See also**	HTAddRule()*/void HTClearRules NOARGS{    while (rules) {	rule * temp = rules;	rules = temp->next;	FREE(temp->pattern);	FREE(temp->equiv);	FREE(temp->condition_op);	FREE(temp->condition);	FREE(temp);    }#ifndef PUT_ON_HEAD    rule_tail = 0;#endif}PRIVATE BOOL rule_cond_ok ARGS1(    rule *,	 r){    BOOL result;    if (!r->condition_op)	return YES;    if (strcmp(r->condition_op, "if") && strcmp(r->condition_op, "unless")) {	CTRACE((tfp, "....... rule ignored, unrecognized `%s'!\n",	       r->condition_op));	return NO;    }    if (!strcmp(r->condition, "redirected"))	result = (BOOL) (redirection_attempts > 0);    else if (!strcmp(r->condition, "userspec"))	result = LYUserSpecifiedURL;    else {	CTRACE((tfp, "....... rule ignored, unrecognized `%s %s'!\n",	       r->condition_op, NONNULL(r->condition)));	return NO;    }    if (!strcmp(r->condition_op, "if"))	return result;    else	return (BOOL) (!result);}/*	Translate by rules					HTTranslate()**	------------------****	The most recently defined rules are applied first.**** On entry,**	required	points to a string whose equivalent value is neeed** On exit,**	returns		the address of the equivalent string allocated from**			the heap which the CALLER MUST FREE. If no translation**			occured, then it is a copy of te original.** NEW FEATURES:**			When a "protect" or "defprot" rule is mathed,**			a call to HTAA_setCurrentProtection() or**			HTAA_setDefaultProtection() is made to notify**			the Access Authorization module that the file is**			protected, and so it knows how to handle it.**								-- AL*/char * HTTranslate ARGS1(    CONST char *,	required){    rule * r;    char *current = NULL;    char *msgtmp = NULL, *pMsg;    int proxy_none_flag = 0;    int permitredir_flag = 0;    StrAllocCopy(current, required);    HTAA_clearProtections();	/* Reset from previous call -- AL */    for(r = rules; r; r = r->next) {	char * p = r->pattern;	int m=0;   /* Number of characters matched against wildcard */	CONST char * q = current;	for(;*p && *q; p++, q++) {   /* Find first mismatch */	    if (*p!=*q) break;	}	if (*p == '*') {		/* Match up to wildcard */	    m = strlen(q) - strlen(p+1); /* Amount to match to wildcard */	    if(m<0) continue;		/* tail is too short to match */	    if (0!=strcmp(q+m, p+1)) continue;	/* Tail mismatch */	} else				/* Not wildcard */	    if (*p != *q) continue;	/* plain mismatch: go to next rule */	if (!rule_cond_ok(r))	/* check condition, next rule if false - kw */	    continue;	switch (r->op) {		/* Perform operation */#ifdef ACCESS_AUTH	case HT_DefProt:	case HT_Protect:	    {		char *local_copy = NULL;		char *p2;		char *eff_ids = NULL;		char *prot_file = NULL;		CTRACE((tfp, "HTRule: `%s' matched %s %s: `%s'\n",			    current,			    (r->op==HT_Protect ? "Protect" : "DefProt"),			    "rule, setup",			    (r->equiv ? r->equiv :			     (r->op==HT_Protect ?"DEFAULT" :"NULL!!"))));		if (r->equiv) {		    StrAllocCopy(local_copy, r->equiv);		    p2 = local_copy;		    prot_file = HTNextField(&p2);		    eff_ids = HTNextField(&p2);		}		if (r->op == HT_Protect)		    HTAA_setCurrentProtection(current, prot_file, eff_ids);		else		    HTAA_setDefaultProtection(current, prot_file, eff_ids);		FREE(local_copy);		/* continue translating rules */	    }	    break;#endif /* ACCESS_AUTH */	case HT_UserMsg:		/* Produce message immediately */	    LYFixCursesOn("show rule message:");	    HTUserMsg2((r->equiv ? r->equiv : "Rule: %s"), current);	    break;	case HT_InfoMsg:		/* Produce messages immediately */	case HT_Progress:	case HT_Alert:	    LYFixCursesOn("show rule message:"); /* and fall through */	case HT_AlwaysAlert:	    pMsg = r->equiv ? r->equiv :		(r->op==HT_AlwaysAlert) ? "%s" : "Rule: %s";	    if (strchr(pMsg, '%')) {		HTSprintf0(&msgtmp, pMsg, current);		pMsg = msgtmp;	    }	    switch (r->op) {		/* Actually produce message */	    case HT_InfoMsg:	HTInfoMsg(pMsg);	break;	    case HT_Progress:	HTProgress(pMsg);	break;	    case HT_Alert:	HTAlert(pMsg);		break;	    case HT_AlwaysAlert: HTAlwaysAlert("Rule alert:", pMsg);	break;	    default: break;	    }	    FREE(msgtmp);	    break;	case HT_PermitRedir:			/* Set special flag */	    permitredir_flag = 1;	    CTRACE((tfp, "HTRule: Mark for redirection permitted\n"));	    break;	case HT_Pass:				/* Authorised */	    if (!r->equiv) {		if (proxy_none_flag) {		    char * temp = NULL;		    StrAllocCopy(temp, "NoProxy=");		    StrAllocCat(temp, current);		    FREE(current);		    current = temp;		}		CTRACE((tfp, "HTRule: Pass `%s'\n", current));		return current;	    }	    /* Else fall through ...to map and pass */	case HT_Map:	case HT_Redirect:	case HT_RedirectPerm:	    if (*p == *q) { /* End of both strings, no wildcard */		  CTRACE((tfp, "For `%s' using `%s'\n", current, r->equiv));		  StrAllocCopy(current, r->equiv); /* use entire translation */

⌨️ 快捷键说明

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