Annotation of libwww/Library/src/HTRules.c, revision 2.22
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.1 timbl 177: for(r = rules; r; r = r->next) {
178: char * p = r->pattern;
2.11 luotonen 179: int m=0; /* Number of characters matched against wildcard */
2.1 timbl 180: CONST char * q = current;
181: for(;*p && *q; p++, q++) { /* Find first mismatch */
182: if (*p!=*q) break;
183: }
184:
185: if (*p == '*') { /* Match up to wildcard */
186: m = strlen(q) - strlen(p+1); /* Amount to match to wildcard */
187: if(m<0) continue; /* tail is too short to match */
188: if (0!=strcmp(q+m, p+1)) continue; /* Tail mismatch */
189: } else /* Not wildcard */
190: if (*p != *q) continue; /* plain mismatch: go to next rule */
191:
192: switch (r->op) { /* Perform operation */
2.11 luotonen 193:
2.1 timbl 194: case HT_Pass: /* Authorised */
195: if (!r->equiv) {
2.17 luotonen 196: CTRACE(stderr, "HTRule: Pass `%s'\n", current);
2.20 luotonen 197: return current;
2.1 timbl 198: }
2.11 luotonen 199: /* Else fall through ...to map and pass */
2.1 timbl 200:
201: case HT_Map:
202: if (*p == *q) { /* End of both strings, no wildcard */
2.17 luotonen 203: CTRACE(stderr, "For `%s' using `%s'\n", current, r->equiv);
204: StrAllocCopy(current, r->equiv); /* use entire translation */
2.1 timbl 205: } else {
206: char * ins = strchr(r->equiv, '*'); /* Insertion point */
207: if (ins) { /* Consistent rule!!! */
208: char * temp = (char *)malloc(
209: strlen(r->equiv)-1 + m + 1);
2.9 secret 210: if (temp==NULL)
211: outofmem(__FILE__, "HTTranslate"); /* NT & AS */
2.1 timbl 212: strncpy(temp, r->equiv, ins-r->equiv);
213: /* Note: temp may be unterminated now! */
214: strncpy(temp+(ins-r->equiv), q, m); /* Matched bit */
215: strcpy (temp+(ins-r->equiv)+m, ins+1); /* Last bit */
2.17 luotonen 216: CTRACE(stderr, "For `%s' using `%s'\n",
2.1 timbl 217: current, temp);
218: free(current);
219: current = temp; /* Use this */
220:
221: } else { /* No insertion point */
222: char * temp = (char *)malloc(strlen(r->equiv)+1);
2.9 secret 223: if (temp==NULL)
224: outofmem(__FILE__, "HTTranslate"); /* NT & AS */
2.1 timbl 225: strcpy(temp, r->equiv);
2.17 luotonen 226: CTRACE(stderr, "For `%s' using `%s'\n", current, temp);
2.1 timbl 227: free(current);
228: current = temp; /* Use this */
229: } /* If no insertion point exists */
230: }
231: if (r->op == HT_Pass) {
2.17 luotonen 232: CTRACE(stderr, "HTRule: ...and pass `%s'\n", current);
2.20 luotonen 233: return current;
2.1 timbl 234: }
235: break;
236:
2.11 luotonen 237: case HT_Fail: /* Unauthorised */
2.15 luotonen 238: default:
2.17 luotonen 239: CTRACE(stderr,"HTRule: *** FAIL `%s'\n", current);
240: return (char *)0;
2.1 timbl 241:
2.11 luotonen 242: } /* if tail matches ... switch operation */
2.1 timbl 243:
244: } /* loop over rules */
245:
2.22 ! luotonen 246: return current; /* For clients default is to pass */
2.15 luotonen 247: }
248:
249:
250:
2.7 timbl 251: /* Load one line of configuration
252: ** ------------------------------
253: **
254: ** Call this, for example, to load a X resource with config info.
255: **
256: ** returns 0 OK, < 0 syntax error.
257: */
2.15 luotonen 258: PUBLIC int HTSetConfiguration ARGS1(CONST char *, config)
2.7 timbl 259: {
260: HTRuleOp op;
261: char * line = NULL;
262: char * pointer = line;
263: char *word1, *word2, *word3;
264: float quality, secs, secs_per_byte;
265: int status;
266:
267: StrAllocCopy(line, config);
268: {
269: char * p = strchr(line, '#'); /* Chop off comments */
270: if (p) *p = 0;
271: }
272: pointer = line;
273: word1 = HTNextField(&pointer);
274: if (!word1) {
275: free(line);
276: return 0;
277: } ; /* Comment only or blank */
2.11 luotonen 278:
2.7 timbl 279: word2 = HTNextField(&pointer);
2.19 luotonen 280: word3 = HTNextField(&pointer);
2.11 luotonen 281:
2.7 timbl 282: if (!word2) {
283: fprintf(stderr, "HTRule: Insufficient operands: %s\n", line);
284: free(line);
285: return -2; /*syntax error */
286: }
287:
2.17 luotonen 288: if (0==strcasecomp(word1, "suffix") ||
289: 0==strcasecomp(word1, "addtype")) {
2.8 timbl 290: char * encoding = HTNextField(&pointer);
291: if (pointer) status = sscanf(pointer, "%f", &quality);
292: else status = 0;
2.17 luotonen 293: HTAddType(word2, word3,
2.8 timbl 294: encoding ? encoding : "binary",
295: status >= 1? quality : 1.0);
2.7 timbl 296:
2.17 luotonen 297: } else if (0==strcasecomp(word1, "addencoding") ||
298: 0==strcasecomp(word1, "encoding")) {
299: if (pointer)
300: status = sscanf(pointer, "%f", &quality);
301: else status = 0;
302: HTAddEncoding(word2, word3,
303: status >= 1 ? quality : 1.0);
304:
305: } else if (0==strcasecomp(word1, "addlanguage") ||
306: 0==strcasecomp(word1, "language")) {
307: if (pointer)
308: status = sscanf(pointer, "%f", &quality);
309: else status = 0;
310: HTAddLanguage(word2, word3,
311: status >= 1 ? quality : 1.0);
312:
2.7 timbl 313: } else if (0==strcasecomp(word1, "presentation")) {
2.8 timbl 314: if (pointer) status = sscanf(pointer, "%f%f%f",
315: &quality, &secs, &secs_per_byte);
316: else status = 0;
2.14 timbl 317: if (!HTConversions) HTConversions = HTList_new();
318: HTSetPresentation(HTConversions, word2, word3,
2.7 timbl 319: status >= 1? quality : 1.0,
2.11 luotonen 320: status >= 2 ? secs : 0.0,
2.7 timbl 321: status >= 3 ? secs_per_byte : 0.0 );
2.12 luotonen 322:
2.7 timbl 323: } else {
324: op = 0==strcasecomp(word1, "map") ? HT_Map
325: : 0==strcasecomp(word1, "pass") ? HT_Pass
326: : 0==strcasecomp(word1, "fail") ? HT_Fail
2.19 luotonen 327: : HT_Invalid;
2.7 timbl 328: if (op==HT_Invalid) {
2.17 luotonen 329: CTRACE(stderr, "HTRule: Bad rule `%s'\n", config);
2.7 timbl 330: } else {
331: HTAddRule(op, word2, word3);
332: }
333: }
334: free(line);
335: return 0;
336: }
337:
2.1 timbl 338:
2.11 luotonen 339: /* Load the rules from a file HTLoadRules()
2.1 timbl 340: ** --------------------------
341: **
342: ** On entry,
343: ** Rules can be in any state
344: ** On exit,
345: ** Any existing rules will have been kept.
2.7 timbl 346: ** Any new rules will have been loaded.
347: ** Returns 0 if no error, 0 if error!
2.1 timbl 348: **
349: ** Bugs:
350: ** The strings may not contain spaces.
351: */
352:
353: int HTLoadRules ARGS1(CONST char *, filename)
354: {
355: FILE * fp = fopen(filename, "r");
356: char line[LINE_LENGTH+1];
357:
358: if (!fp) {
2.17 luotonen 359: CTRACE(stderr, "HTRules: Can't open rules file %s\n", filename);
2.1 timbl 360: return -1; /* File open error */
361: }
362: for(;;) {
363: if (!fgets(line, LINE_LENGTH+1, fp)) break; /* EOF or error */
2.7 timbl 364: (void) HTSetConfiguration(line);
2.1 timbl 365: }
366: fclose(fp);
2.7 timbl 367: return 0; /* No error or syntax errors ignored */
2.1 timbl 368: }
2.11 luotonen 369:
Webmaster