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