Annotation of libwww/Library/src/HTRules.c, revision 2.24
2.19 luotonen 1: /* Configuration manager for Clients HTRules.c
2: ** =================================
2.1 timbl 3: **
2.19 luotonen 4: ** This module is replaced by HTConfig.c in httpd.
2.1 timbl 5: **
6: ** History:
7: ** 3 Jun 91 Written TBL
8: ** 10 Aug 91 Authorisation added after Daniel Martin (pass, fail)
9: ** Rule order in file changed
10: ** Comments allowed with # on 1st char of rule line
11: ** 17 Jun 92 Bug fix: pass and fail failed if didn't contain '*' TBL
2.9 secret 12: ** 1 Sep 93 Bug fix: no memory check - Nathan Torkington
13: ** BYTE_ADDRESSING removed - Arthur Secret
2.12 luotonen 14: ** 11 Sep 93 MD Changed %i into %d in debug printf.
2.10 duns 15: ** VMS does not recognize %i.
16: ** Bug Fix: in case of PASS, only one parameter to printf.
2.12 luotonen 17: ** 19 Sep 93 AL Added Access Authorization stuff.
18: ** 1 Nov 93 AL Added htbin.
2.15 luotonen 19: ** 30 Nov 93 AL Added HTTranslateReq().
2.19 luotonen 20: ** 4 Feb 94 AL Took away all the daemon-specific stuff.
2.11 luotonen 21: **
2.1 timbl 22: */
23:
24: /* (c) CERN WorldWideWeb project 1990,91. See Copyright.html for details */
25: #include "HTRules.h"
26:
27: #include "HTFile.h"
2.17 luotonen 28: #include "HTParse.h" /* HTParse() */
2.18 luotonen 29: #include "HTAAUtil.h"
30:
31: #ifndef VMS
32: #include <pwd.h> /* Unix password file routine: getpwnam() */
33: #endif /* not VMS */
34:
2.1 timbl 35:
36: #define LINE_LENGTH 256
37:
38:
39: typedef struct _rule {
40: struct _rule * next;
41: HTRuleOp op;
42: char * pattern;
43: char * equiv;
44: } rule;
45:
2.13 luotonen 46:
2.1 timbl 47: /* Module-wide variables
48: ** ---------------------
49: */
50:
51: PRIVATE rule * rules = 0; /* Pointer to first on list */
52: #ifndef PUT_ON_HEAD
53: PRIVATE rule * rule_tail = 0; /* Pointer to last on list */
54: #endif
55:
2.11 luotonen 56:
2.1 timbl 57: /* Add rule to the list HTAddRule()
58: ** --------------------
59: **
60: ** On entry,
61: ** pattern points to 0-terminated string containing a single "*"
62: ** equiv points to the equivalent string with * for the
63: ** place where the text matched by * goes.
64: ** On exit,
65: ** returns 0 if success, -1 if error.
66: */
2.9 secret 67:
2.15 luotonen 68: PUBLIC int HTAddRule ARGS3(HTRuleOp, op,
69: CONST char *, pattern,
70: CONST char *, equiv)
2.9 secret 71: { /* BYTE_ADDRESSING removed and memory check - AS - 1 Sep 93 */
72: rule * temp;
73: char * pPattern;
74:
75: temp = (rule *)malloc(sizeof(*temp));
76: if (temp==NULL)
77: outofmem(__FILE__, "HTAddRule");
78: pPattern = (char *)malloc(strlen(pattern)+1);
79: if (pPattern==NULL)
80: outofmem(__FILE__, "HTAddRule");
2.1 timbl 81: if (equiv) { /* Two operands */
82: char * pEquiv = (char *)malloc(strlen(equiv)+1);
2.9 secret 83: if (pEquiv==NULL)
84: outofmem(__FILE__, "HTAddRule");
2.1 timbl 85: temp->equiv = pEquiv;
86: strcpy(pEquiv, equiv);
87: } else {
88: temp->equiv = 0;
89: }
90: temp->pattern = pPattern;
91: temp->op = op;
92:
93: strcpy(pPattern, pattern);
2.10 duns 94: if (TRACE) {
95: if (equiv)
2.17 luotonen 96: fprintf(stderr, "Rule: For `%s' op %d `%s'\n", pattern, op, equiv);
2.10 duns 97: else
2.17 luotonen 98: fprintf(stderr, "Rule: For `%s' op %d\n", pattern, op);
2.10 duns 99: }
2.1 timbl 100:
101: #ifdef PUT_ON_HEAD
102: temp->next = rules;
103: rules = temp;
104: #else
105: temp->next = 0;
106: if (rule_tail) rule_tail->next = temp;
107: else rules = temp;
108: rule_tail = temp;
109: #endif
110:
111:
112: return 0;
113: }
114:
115:
116: /* Clear all rules HTClearRules()
117: ** ---------------
118: **
119: ** On exit,
120: ** There are no rules
121: ** returns 0 if success, -1 if error.
122: **
123: ** See also
124: ** HTAddRule()
125: */
2.15 luotonen 126: PUBLIC int HTClearRules NOARGS
2.1 timbl 127: {
128: while (rules) {
129: rule * temp = rules;
130: rules = temp->next;
131: free(temp->pattern);
132: free(temp->equiv);
133: free(temp);
134: }
135: #ifndef PUT_ON_HEAD
136: rule_tail = 0;
137: #endif
138:
139: return 0;
140: }
141:
142:
2.18 luotonen 143:
2.1 timbl 144: /* Translate by rules HTTranslate()
145: ** ------------------
146: **
2.15 luotonen 147: ** The most recently defined rules are applied last.
2.1 timbl 148: **
149: ** On entry,
150: ** required points to a string whose equivalent value is neeed
151: ** On exit,
152: ** returns the address of the equivalent string allocated from
153: ** the heap which the CALLER MUST FREE. If no translation
154: ** occured, then it is a copy of te original.
155: */
2.15 luotonen 156: PUBLIC char * HTTranslate ARGS1(CONST char *, required)
2.1 timbl 157: {
158: rule * r;
2.11 luotonen 159: char *current = NULL;
160: StrAllocCopy(current, required);
161:
2.1 timbl 162: for(r = rules; r; r = r->next) {
163: char * p = r->pattern;
2.11 luotonen 164: int m=0; /* Number of characters matched against wildcard */
2.1 timbl 165: CONST char * q = current;
166: for(;*p && *q; p++, q++) { /* Find first mismatch */
167: if (*p!=*q) break;
168: }
169:
170: if (*p == '*') { /* Match up to wildcard */
171: m = strlen(q) - strlen(p+1); /* Amount to match to wildcard */
172: if(m<0) continue; /* tail is too short to match */
173: if (0!=strcmp(q+m, p+1)) continue; /* Tail mismatch */
174: } else /* Not wildcard */
175: if (*p != *q) continue; /* plain mismatch: go to next rule */
176:
177: switch (r->op) { /* Perform operation */
2.11 luotonen 178:
2.1 timbl 179: case HT_Pass: /* Authorised */
180: if (!r->equiv) {
2.17 luotonen 181: CTRACE(stderr, "HTRule: Pass `%s'\n", current);
2.20 luotonen 182: return current;
2.1 timbl 183: }
2.11 luotonen 184: /* Else fall through ...to map and pass */
2.1 timbl 185:
186: case HT_Map:
187: if (*p == *q) { /* End of both strings, no wildcard */
2.17 luotonen 188: CTRACE(stderr, "For `%s' using `%s'\n", current, r->equiv);
189: StrAllocCopy(current, r->equiv); /* use entire translation */
2.1 timbl 190: } else {
191: char * ins = strchr(r->equiv, '*'); /* Insertion point */
192: if (ins) { /* Consistent rule!!! */
193: char * temp = (char *)malloc(
194: strlen(r->equiv)-1 + m + 1);
2.9 secret 195: if (temp==NULL)
196: outofmem(__FILE__, "HTTranslate"); /* NT & AS */
2.1 timbl 197: strncpy(temp, r->equiv, ins-r->equiv);
198: /* Note: temp may be unterminated now! */
199: strncpy(temp+(ins-r->equiv), q, m); /* Matched bit */
200: strcpy (temp+(ins-r->equiv)+m, ins+1); /* Last bit */
2.17 luotonen 201: CTRACE(stderr, "For `%s' using `%s'\n",
2.1 timbl 202: current, temp);
203: free(current);
204: current = temp; /* Use this */
205:
206: } else { /* No insertion point */
207: char * temp = (char *)malloc(strlen(r->equiv)+1);
2.9 secret 208: if (temp==NULL)
209: outofmem(__FILE__, "HTTranslate"); /* NT & AS */
2.1 timbl 210: strcpy(temp, r->equiv);
2.17 luotonen 211: CTRACE(stderr, "For `%s' using `%s'\n", current, temp);
2.1 timbl 212: free(current);
213: current = temp; /* Use this */
214: } /* If no insertion point exists */
215: }
216: if (r->op == HT_Pass) {
2.17 luotonen 217: CTRACE(stderr, "HTRule: ...and pass `%s'\n", current);
2.20 luotonen 218: return current;
2.1 timbl 219: }
220: break;
221:
2.11 luotonen 222: case HT_Fail: /* Unauthorised */
2.15 luotonen 223: default:
2.17 luotonen 224: CTRACE(stderr,"HTRule: *** FAIL `%s'\n", current);
225: return (char *)0;
2.1 timbl 226:
2.11 luotonen 227: } /* if tail matches ... switch operation */
2.1 timbl 228:
229: } /* loop over rules */
230:
2.22 luotonen 231: return current; /* For clients default is to pass */
2.15 luotonen 232: }
233:
234:
235:
2.7 timbl 236: /* Load one line of configuration
237: ** ------------------------------
238: **
239: ** Call this, for example, to load a X resource with config info.
240: **
241: ** returns 0 OK, < 0 syntax error.
242: */
2.15 luotonen 243: PUBLIC int HTSetConfiguration ARGS1(CONST char *, config)
2.7 timbl 244: {
245: HTRuleOp op;
246: char * line = NULL;
247: char * pointer = line;
248: char *word1, *word2, *word3;
249: float quality, secs, secs_per_byte;
250: int status;
251:
252: StrAllocCopy(line, config);
253: {
254: char * p = strchr(line, '#'); /* Chop off comments */
255: if (p) *p = 0;
256: }
257: pointer = line;
258: word1 = HTNextField(&pointer);
259: if (!word1) {
260: free(line);
261: return 0;
262: } ; /* Comment only or blank */
2.11 luotonen 263:
2.7 timbl 264: word2 = HTNextField(&pointer);
2.19 luotonen 265: word3 = HTNextField(&pointer);
2.11 luotonen 266:
2.7 timbl 267: if (!word2) {
268: fprintf(stderr, "HTRule: Insufficient operands: %s\n", line);
269: free(line);
270: return -2; /*syntax error */
271: }
272:
2.17 luotonen 273: if (0==strcasecomp(word1, "suffix") ||
274: 0==strcasecomp(word1, "addtype")) {
2.8 timbl 275: char * encoding = HTNextField(&pointer);
276: if (pointer) status = sscanf(pointer, "%f", &quality);
277: else status = 0;
2.17 luotonen 278: HTAddType(word2, word3,
2.8 timbl 279: encoding ? encoding : "binary",
280: status >= 1? quality : 1.0);
2.7 timbl 281:
2.23 luotonen 282: } else if (0==strcasecomp(word1, "addencoding")) {
2.17 luotonen 283: if (pointer)
284: status = sscanf(pointer, "%f", &quality);
285: else status = 0;
286: HTAddEncoding(word2, word3,
287: status >= 1 ? quality : 1.0);
288:
2.23 luotonen 289: } else if (0==strcasecomp(word1, "addlanguage")) {
2.17 luotonen 290: if (pointer)
291: status = sscanf(pointer, "%f", &quality);
292: else status = 0;
293: HTAddLanguage(word2, word3,
294: status >= 1 ? quality : 1.0);
295:
2.7 timbl 296: } else if (0==strcasecomp(word1, "presentation")) {
2.8 timbl 297: if (pointer) status = sscanf(pointer, "%f%f%f",
298: &quality, &secs, &secs_per_byte);
299: else status = 0;
2.14 timbl 300: if (!HTConversions) HTConversions = HTList_new();
301: HTSetPresentation(HTConversions, word2, word3,
2.7 timbl 302: status >= 1? quality : 1.0,
2.11 luotonen 303: status >= 2 ? secs : 0.0,
2.7 timbl 304: status >= 3 ? secs_per_byte : 0.0 );
2.12 luotonen 305:
2.7 timbl 306: } else {
307: op = 0==strcasecomp(word1, "map") ? HT_Map
308: : 0==strcasecomp(word1, "pass") ? HT_Pass
309: : 0==strcasecomp(word1, "fail") ? HT_Fail
2.19 luotonen 310: : HT_Invalid;
2.7 timbl 311: if (op==HT_Invalid) {
2.17 luotonen 312: CTRACE(stderr, "HTRule: Bad rule `%s'\n", config);
2.7 timbl 313: } else {
314: HTAddRule(op, word2, word3);
315: }
316: }
317: free(line);
318: return 0;
319: }
320:
2.1 timbl 321:
2.11 luotonen 322: /* Load the rules from a file HTLoadRules()
2.1 timbl 323: ** --------------------------
324: **
325: ** On entry,
326: ** Rules can be in any state
327: ** On exit,
328: ** Any existing rules will have been kept.
2.7 timbl 329: ** Any new rules will have been loaded.
330: ** Returns 0 if no error, 0 if error!
2.1 timbl 331: **
332: ** Bugs:
333: ** The strings may not contain spaces.
334: */
335:
336: int HTLoadRules ARGS1(CONST char *, filename)
337: {
338: FILE * fp = fopen(filename, "r");
339: char line[LINE_LENGTH+1];
340:
341: if (!fp) {
2.17 luotonen 342: CTRACE(stderr, "HTRules: Can't open rules file %s\n", filename);
2.1 timbl 343: return -1; /* File open error */
344: }
345: for(;;) {
346: if (!fgets(line, LINE_LENGTH+1, fp)) break; /* EOF or error */
2.7 timbl 347: (void) HTSetConfiguration(line);
2.1 timbl 348: }
349: fclose(fp);
2.7 timbl 350: return 0; /* No error or syntax errors ignored */
2.1 timbl 351: }
2.11 luotonen 352:
Webmaster