Debian Codegraph @

WWW/Library/Implementation/HTRules.c

raw
1 /*
2 * $LynxId: HTRules.c,v 1.49 2025/01/07 15:44:34 tom Exp $
3 *
4 * Configuration manager for Hypertext Daemon HTRules.c
5 * ==========================================
6 *
7 *
8 * History:
9 * 3 Jun 91 Written TBL
10 * 10 Aug 91 Authorisation added after Daniel Martin (pass, fail)
11 * Rule order in file changed
12 * Comments allowed with # on 1st char of rule line
13 * 17 Jun 92 Bug fix: pass and fail failed if didn't contain '*' TBL
14 * 1 Sep 93 Bug fix: no memory check - Nathan Torkington
15 * BYTE_ADDRESSING removed - Arthur Secret
16 * 11 Sep 93 MD Changed %i into %d in debug printf.
17 * VMS does not recognize %i.
18 * Bug Fix: in case of PASS, only one parameter to printf.
19 * 19 Sep 93 AL Added Access Authorization stuff.
20 * 1 Nov 93 AL Added htbin.
21 * 25 May 99 KW Added redirect for lynx.
22 *
23 */
24
25 #include <HTUtils.h>
26
27 /* (c) CERN WorldWideWeb project 1990,91. See Copyright.html for details */
28 #include <HTRules.h>
29
30 #include <HTFile.h>
31 #include <LYLeaks.h>
32 #include <HTAAProt.h>
33
34 #define LINE_LENGTH 256
35
36 typedef struct _rule {
37 struct _rule *next;
38 HTRuleOp op;
39 char *pattern;
40 char *equiv;
41 char *condition_op; /* as strings - may be inefficient, */
42 char *condition; /* but this is not for a server - kw */
43 } rule;
44
45 #ifndef NO_RULES
46
47 #include <HTTP.h> /* for redirecting_url, indirectly HTPermitRedir - kw */
48 #include <LYGlobalDefs.h> /* for LYUserSpecifiedURL - kw */
49 #include <LYStrings.h> /* for LYscanFloat */
50 #include <LYUtils.h> /* for LYFixCursesOn - kw */
51 #include <HTAlert.h>
52
53 /* Global variables
54 * ----------------
55 */
56 char *HTBinDir = NULL; /* Physical /htbin directory path. */
57
58 /* In future this should not be global. */
59 char *HTSearchScript = NULL; /* Search script name. */
60
61 /* Module-wide variables
62 * ---------------------
63 */
64
65 static rule *rules = NULL; /* Pointer to first on list */
66
67 #ifndef PUT_ON_HEAD
68 static rule *rule_tail = NULL; /* Pointer to last on list */
69 #endif
70
71 /* Add rule to the list HTAddRule()
72 * --------------------
73 *
74 * On entry,
75 * pattern points to 0-terminated string containing a single "*"
76 * equiv points to the equivalent string with * for the
77 * place where the text matched by * goes.
78 * On exit,
79 * returns 0 if success, -1 if error.
80 */
81
82 int HTAddRule(HTRuleOp op, const char *pattern,
83 const char *equiv,
84 const char *cond_op,
85 const char *cond)
86 { /* BYTE_ADDRESSING removed and memory check - AS - 1 Sep 93 */
87 rule *temp;
88 char *pPattern = NULL;
89
90 temp = typecalloc(rule);
91 if (temp == NULL)
92 outofmem(__FILE__, "HTAddRule");
93
94 if (equiv) { /* Two operands */
95 char *pEquiv = NULL;
96
97 StrAllocCopy(pEquiv, equiv);
98 temp->equiv = pEquiv;
99 } else {
100 temp->equiv = NULL;
101 }
102 if (cond_op) {
103 StrAllocCopy(temp->condition_op, cond_op);
104 StrAllocCopy(temp->condition, cond);
105 }
106 StrAllocCopy(pPattern, pattern);
107 temp->pattern = pPattern;
108 temp->op = op;
109
110 if (equiv) {
111 CTRACE((tfp, "Rule: For `%s' op %d `%s'", pattern, (int) op, equiv));
112 } else {
113 CTRACE((tfp, "Rule: For `%s' op %d", pattern, (int) op));
114 }
115 if (cond_op) {
116 CTRACE((tfp, "\t%s %s\n", cond_op, NONNULL(cond)));
117 } else {
118 CTRACE((tfp, "\n"));
119 }
120
121 if (!rules) {
122 #ifdef LY_FIND_LEAKS
123 atexit(HTClearRules);
124 #endif
125 }
126 #ifdef PUT_ON_HEAD
127 temp->next = rules;
128 rules = temp;
129 #else
130 temp->next = NULL;
131 if (rule_tail)
132 rule_tail->next = temp;
133 else
134 rules = temp;
135 rule_tail = temp;
136 #endif
137
138 return 0;
139 }
140
141 /* Clear all rules HTClearRules()
142 * ---------------
143 *
144 * On exit,
145 * There are no rules
146 *
147 * See also
148 * HTAddRule()
149 */
150 void HTClearRules(void)
151 {
152 while (rules) {
153 rule *temp = rules;
154
155 rules = temp->next;
156 FREE(temp->pattern);
157 FREE(temp->equiv);
158 FREE(temp->condition_op);
159 FREE(temp->condition);
160 FREE(temp);
161 }
162 #ifndef PUT_ON_HEAD
163 rule_tail = NULL;
164 #endif
165 }
166
167 static BOOL rule_cond_ok(rule * r)
168 {
169 BOOL result;
170
171 if (!r->condition_op)
172 return YES;
173 if (strcmp(r->condition_op, "if") && strcmp(r->condition_op, "unless")) {
174 CTRACE((tfp, "....... rule ignored, unrecognized `%s'!\n",
175 r->condition_op));
176 return NO;
177 }
178 if (!strcmp(r->condition, "redirected"))
179 result = (BOOL) (redirection_attempts > 0);
180 else if (!strcmp(r->condition, "userspec"))
181 result = LYUserSpecifiedURL;
182 else {
183 CTRACE((tfp, "....... rule ignored, unrecognized `%s %s'!\n",
184 r->condition_op, NONNULL(r->condition)));
185 return NO;
186 }
187 if (!strcmp(r->condition_op, "if"))
188 return result;
189 else
190 return (BOOL) (!result);
191
192 }
193
194 /* Translate by rules HTTranslate()
195 * ------------------
196 *
197 * The most recently defined rules are applied first.
198 *
199 * On entry,
200 * required points to a string whose equivalent value is needed
201 * On exit,
202 * returns the address of the equivalent string allocated from
203 * the heap which the CALLER MUST FREE. If no translation
204 * occurred, then it is a copy of the original.
205 * NEW FEATURES:
206 * When a "protect" or "defprot" rule is matched,
207 * a call to HTAA_setCurrentProtection() or
208 * HTAA_setDefaultProtection() is made to notify
209 * the Access Authorization module that the file is
210 * protected, and so it knows how to handle it.
211 * -- AL
212 */
213 char *HTTranslate(const char *required)
214 {
215 rule *r;
216 char *current = NULL;
217 char *msgtmp = NULL;
218 const char *pMsg;
219 int proxy_none_flag = 0;
220 int permitredir_flag = 0;
221
222 StrAllocCopy(current, required);
223
224 HTAA_clearProtections(); /* Reset from previous call -- AL */
225
226 for (r = rules; r; r = r->next) {
227 char *p = r->pattern;
228 int m = 0; /* Number of characters matched against wildcard */
229 const char *q = current;
230
231 for (; *p && *q; p++, q++) { /* Find first mismatch */
232 if (*p != *q)
233 break;
234 }
235
236 if (*p == '*') { /* Match up to wildcard */
237 m = (int) strlen(q) - (int) strlen(p + 1); /* Amount to match to wildcard */
238 if (m < 0)
239 continue; /* tail is too short to match */
240 if (0 != strcmp(q + m, p + 1))
241 continue; /* Tail mismatch */
242 } else
243 /* Not wildcard */ if (*p != *q)
244 continue; /* plain mismatch: go to next rule */
245
246 if (!rule_cond_ok(r)) /* check condition, next rule if false - kw */
247 continue;
248
249 switch (r->op) { /* Perform operation */
250
251 case HT_DefProt:
252 case HT_Protect:
253 #ifdef ACCESS_AUTH
254 {
255 char *local_copy = NULL;
256 char *p2;
257 char *eff_ids = NULL;
258 char *prot_file = NULL;
259
260 CTRACE((tfp, "HTRule: `%s' matched %s %s: `%s'\n",
261 current,
262 (r->op == HT_Protect ? "Protect" : "DefProt"),
263 "rule, setup",
264 (r->equiv ? r->equiv :
265 (r->op == HT_Protect ? "DEFAULT" : "NULL!!"))));
266
267 if (r->equiv) {
268 StrAllocCopy(local_copy, r->equiv);
269 p2 = local_copy;
270 prot_file = HTNextField(&p2);
271 eff_ids = HTNextField(&p2);
272 }
273
274 if (r->op == HT_Protect)
275 HTAA_setCurrentProtection(current, prot_file, eff_ids);
276 else
277 HTAA_setDefaultProtection(current, prot_file, eff_ids);
278
279 FREE(local_copy);
280
281 /* continue translating rules */
282 }
283 #endif /* ACCESS_AUTH */
284 break;
285
286 case HT_UserMsg: /* Produce message immediately */
287 LYFixCursesOn("show rule message:");
288 HTUserMsg2((r->equiv ? r->equiv : "Rule: %s"), current);
289 break;
290 case HT_InfoMsg: /* Produce messages immediately */
291 case HT_Progress:
292 case HT_Alert:
293 LYFixCursesOn("show rule message:"); /* and fall through */
294 /* FALLTHRU */
295 case HT_AlwaysAlert:
296 HTSprintf0(&msgtmp,
297 HT_FMT("%s", (r->equiv
298 ? r->equiv
299 : (r->op == HT_AlwaysAlert)
300 ? "%s"
301 : "Rule: %s")),
302 current);
303 pMsg = msgtmp;
304 switch (r->op) { /* Actually produce message */
305 case HT_InfoMsg:
306 HTInfoMsg(pMsg);
307 break;
308 case HT_Progress:
309 HTProgress(pMsg);
310 break;
311 case HT_Alert:
312 HTAlert(pMsg);
313 break;
314 case HT_AlwaysAlert:
315 HTAlwaysAlert("Rule alert:", pMsg);
316 break;
317 default:
318 break;
319 }
320 FREE(msgtmp);
321 break;
322
323 case HT_PermitRedir: /* Set special flag */
324 permitredir_flag = 1;
325 CTRACE((tfp, "HTRule: Mark for redirection permitted\n"));
326 break;
327
328 case HT_Pass: /* Authorised */
329 if (!r->equiv) {
330 if (proxy_none_flag) {
331 char *temp = NULL;
332
333 StrAllocCopy(temp, "NoProxy=");
334 StrAllocCat(temp, current);
335 FREE(current);
336 current = temp;
337 }
338 CTRACE((tfp, "HTRule: Pass `%s'\n", current));
339 return current;
340 }
341 /* FALLTHRU */
342
343 case HT_Map:
344 case HT_Redirect:
345 case HT_RedirectPerm:
346 if (*p == *q) { /* End of both strings, no wildcard */
347 CTRACE((tfp, "For `%s' using `%s'\n", current, r->equiv));
348 StrAllocCopy(current, r->equiv); /* use entire translation */
349 } else {
350 char *ins = StrChr(r->equiv, '*'); /* Insertion point */
351
352 if (ins) { /* Consistent rule!!! */
353 char *temp = NULL;
354
355 HTSprintf0(&temp, "%.*s%.*s%s",
356 (int) (ins - r->equiv),
357 r->equiv,
358 m,
359 q,
360 ins + 1);
361 CTRACE((tfp, "For `%s' using `%s'\n",
362 current, temp));
363 FREE(current);
364 current = temp; /* Use this */
365
366 } else { /* No insertion point */
367 char *temp = NULL;
368
369 StrAllocCopy(temp, r->equiv);
370 CTRACE((tfp, "For `%s' using `%s'\n",
371 current, temp));
372 FREE(current);
373 current = temp; /* Use this */
374 } /* If no insertion point exists */
375 }
376 if (r->op == HT_Pass) {
377 if (proxy_none_flag) {
378 char *temp = NULL;
379
380 StrAllocCopy(temp, "NoProxy=");
381 StrAllocCat(temp, current);
382 FREE(current);
383 current = temp;
384 }
385 CTRACE((tfp, "HTRule: ...and pass `%s'\n",
386 current));
387 return current;
388 } else if (r->op == HT_Redirect) {
389 CTRACE((tfp, "HTRule: ...and redirect to `%s'\n",
390 current));
391 redirecting_url = current;
392 HTPermitRedir = (BOOL) (permitredir_flag == 1);
393 return (char *) 0;
394 } else if (r->op == HT_RedirectPerm) {
395 CTRACE((tfp, "HTRule: ...and redirect like 301 to `%s'\n",
396 current));
397 redirecting_url = current;
398 permanent_redirection = TRUE;
399 HTPermitRedir = (BOOL) (permitredir_flag == 1);
400 return (char *) 0;
401 }
402 break;
403
404 case HT_UseProxy:
405 if (r->equiv && 0 == strcasecomp(r->equiv, "none")) {
406 CTRACE((tfp, "For `%s' will not use proxy\n", current));
407 proxy_none_flag = 1;
408 } else if (proxy_none_flag) {
409 CTRACE((tfp, "For `%s' proxy server ignored: %s\n",
410 current,
411 NONNULL(r->equiv)));
412 } else {
413 char *temp = NULL;
414
415 StrAllocCopy(temp, "Proxied=");
416 StrAllocCat(temp, r->equiv);
417 StrAllocCat(temp, current);
418 CTRACE((tfp, "HTRule: proxy server found: %s\n",
419 NONNULL(r->equiv)));
420 FREE(current);
421 return temp;
422 }
423 break;
424
425 case HT_Invalid:
426 case HT_Fail: /* Unauthorised */
427 CTRACE((tfp, "HTRule: *** FAIL `%s'\n", current));
428 FREE(current);
429 return (char *) 0;
430 } /* if tail matches ... switch operation */
431
432 } /* loop over rules */
433
434 if (proxy_none_flag) {
435 char *temp = NULL;
436
437 StrAllocCopy(temp, "NoProxy=");
438 StrAllocCat(temp, current);
439 FREE(current);
440 return temp;
441 }
442
443 return current;
444 }
445
446 /* Load one line of configuration
447 * ------------------------------
448 *
449 * Call this, for example, to load a X resource with config info.
450 *
451 * returns 0 OK, < 0 syntax error.
452 */
453 int HTSetConfiguration(char *config)
454 {
455 HTRuleOp op;
456 char *line = NULL;
457 char *pointer = NULL;
458 char *word1;
459 const char *word2;
460 const char *word3;
461 const char *cond_op = NULL;
462 const char *cond = NULL;
463 float quality, secs, secs_per_byte;
464 long maxbytes;
465 int status;
466
467 StrAllocCopy(line, config);
468 if (line != NULL) {
469 char *p = line;
470
471 /* Chop off comments */
472 while ((p = StrChr(p, '#'))) {
473 if (p == line || isspace(UCH(*(p - 1)))) {
474 *p = 0;
475 break;
476 } else {
477 p++;
478 }
479 }
480 }
481 pointer = line;
482 word1 = HTNextField(&pointer);
483 if (!word1) {
484 FREE(line);
485 return 0;
486 }; /* Comment only or blank */
487
488 word2 = HTNextField(&pointer);
489
490 if (0 == strcasecomp(word1, "defprot") ||
491 0 == strcasecomp(word1, "protect"))
492 word3 = pointer; /* The rest of the line to be parsed by AA module */
493 else
494 word3 = HTNextField(&pointer); /* Just the next word */
495
496 if (!word2) {
497 fprintf(stderr, "HTRule: %s %s\n", RULE_NEEDS_DATA, line);
498 FREE(line);
499 return -2; /*syntax error */
500 }
501
502 if (0 == strcasecomp(word1, "suffix")) {
503 char *encoding = HTNextField(&pointer);
504
505 status = 0;
506 if (pointer)
507 status = LYscanFloat(pointer, &quality);
508
509 HTSetSuffix(word2, word3,
510 encoding ? encoding : "binary",
511 status >= 1 ? quality : (float) 1.0);
512
513 } else if (0 == strcasecomp(word1, "presentation")) {
514 status = 0;
515 if (pointer) {
516 const char *temp = pointer;
517
518 if (LYscanFloat2(&temp, &quality)) {
519 status = 1;
520 if (LYscanFloat2(&temp, &secs)) {
521 status = 2;
522 if (LYscanFloat2(&temp, &secs_per_byte)) {
523 status = 3;
524 if (sscanf(temp, "%ld", &maxbytes)) {
525 status = 4;
526 }
527 }
528 }
529 }
530 }
531
532 HTSetPresentation(word2, word3, NULL,
533 status >= 1 ? quality : 1.0,
534 status >= 2 ? secs : 0.0,
535 status >= 3 ? secs_per_byte : 0.0,
536 status >= 4 ? maxbytes : 0,
537 mediaCFG);
538
539 } else if (0 == strncasecomp(word1, "htbin", 5) ||
540 0 == strncasecomp(word1, "bindir", 6)) {
541 StrAllocCopy(HTBinDir, word2); /* Physical /htbin location */
542
543 } else if (0 == strncasecomp(word1, "search", 6)) {
544 StrAllocCopy(HTSearchScript, word2); /* Search script name */
545
546 } else {
547 op = 0 == strcasecomp(word1, "map") ? HT_Map
548 : 0 == strcasecomp(word1, "pass") ? HT_Pass
549 : 0 == strcasecomp(word1, "fail") ? HT_Fail
550 : 0 == strcasecomp(word1, "redirect") ? HT_Redirect
551 : 0 == strncasecomp(word1, "redirectperm", 12) ? HT_RedirectPerm
552 : 0 == strcasecomp(word1, "redirecttemp") ? HT_Redirect
553 : 0 == strcasecomp(word1, "permitredirection") ? HT_PermitRedir
554 : 0 == strcasecomp(word1, "useproxy") ? HT_UseProxy
555 : 0 == strcasecomp(word1, "alert") ? HT_Alert
556 : 0 == strcasecomp(word1, "alwaysalert") ? HT_AlwaysAlert
557 : 0 == strcasecomp(word1, "progress") ? HT_Progress
558 : 0 == strcasecomp(word1, "usermsg") ? HT_UserMsg
559 : 0 == strcasecomp(word1, "infomsg") ? HT_InfoMsg
560 : 0 == strcasecomp(word1, "defprot") ? HT_DefProt
561 : 0 == strcasecomp(word1, "protect") ? HT_Protect
562 : HT_Invalid;
563 if (op == HT_Invalid) {
564 fprintf(stderr, "HTRule: %s '%s'\n", RULE_INCORRECT, config);
565 } else {
566 switch (op) {
567 case HT_Fail: /* never a or other 2nd parameter */
568 case HT_PermitRedir:
569 cond_op = word3;
570 if (cond_op && *cond_op) {
571 word3 = NULL;
572 cond = HTNextField(&pointer);
573 }
574 break;
575
576 case HT_Pass: /* possibly a URL2 */
577 if (word3 && (!strcasecomp(word3, "if") ||
578 !strcasecomp(word3, "unless"))) {
579 cond_op = word3;
580 word3 = NULL;
581 cond = HTNextField(&pointer);
582 break;
583 }
584 /* else fall through */
585 case HT_Map: /* always a URL2 (or other 2nd parameter) */
586 case HT_Redirect:
587 case HT_RedirectPerm:
588 case HT_UseProxy:
589 cond_op = HTNextField(&pointer);
590 /* check for extra status word in "Redirect" */
591 if (op == HT_Redirect && 0 == strcasecomp(word1, "redirect") &&
592 cond_op &&
593 strcasecomp(cond_op, "if") &&
594 strcasecomp(cond_op, "unless")) {
595 if (0 == strcmp(word2, "301") ||
596 0 == strcasecomp(word2, "permanent")) {
597 op = HT_RedirectPerm;
598 } else if (!(0 == strcmp(word2, "302") ||
599 0 == strcmp(word2, "303") ||
600 0 == strcasecomp(word2, "temp") ||
601 0 == strcasecomp(word2, "seeother"))) {
602 CTRACE((tfp, "Rule: Ignoring `%s' in Redirect\n", word2));
603 }
604 word2 = word3;
605 word3 = cond_op; /* cond_op isn't condition op after all */
606 cond_op = HTNextField(&pointer);
607 }
608 if (cond_op && *cond_op)
609 cond = HTNextField(&pointer);
610 break;
611
612 case HT_Progress:
613 case HT_InfoMsg:
614 case HT_UserMsg:
615 case HT_Alert:
616 case HT_AlwaysAlert:
617 cond_op = HTNextField(&pointer);
618 if (cond_op && *cond_op)
619 cond = HTNextField(&pointer);
620 if (word3) { /* Fix string with too may %s - kw */
621 const char *cp = word3;
622 char *cp1, *cp2;
623
624 while ((cp1 = StrChr(cp, '%'))) {
625 if (cp1[1] == '\0') {
626 *cp1 = '\0';
627 break;
628 } else if (cp1[1] == '%') {
629 cp = cp1 + 2;
630 continue;
631 } else
632 while ((cp2 = StrChr(cp1 + 2, '%'))) {
633 if (cp2[1] == '\0') {
634 *cp2 = '\0';
635 break;
636 } else if (cp2[1] == '%') {
637 cp1 = cp2;
638 } else {
639 *cp2 = '?'; /* replace bad % */
640 cp1 = cp2;
641 }
642 }
643 break;
644 }
645 }
646 break;
647
648 default:
649 break;
650 }
651 if (cond_op && cond && *cond && !strcasecomp(cond_op, "unless")) {
652 cond_op = "unless";
653 } else if (cond_op && cond && *cond &&
654 !strcasecomp(cond_op, "if")) {
655 cond_op = "if";
656 } else if (cond_op || cond) {
657 fprintf(stderr, "HTRule: %s '%s'\n", RULE_INCORRECT, config);
658 FREE(line); /* syntax error, condition is a mess - kw */
659 return -2; /* NB unrecognized cond passes here - kw */
660 }
661 if (cond && !strncasecomp(cond, "redirected", (int) strlen(cond))) {
662 cond = "redirected"; /* recognized, canonical case - kw */
663 } else if (cond && strlen(cond) >= 8 &&
664 !strncasecomp(cond, "userspecified", (int) strlen(cond))) {
665 cond = "userspec"; /* also allow abbreviation - kw */
666 }
667 HTAddRule(op, word2, word3, cond_op, cond);
668 }
669 }
670 FREE(line);
671 return 0;
672 }
673
674 /* Load the rules from a file HTLoadRules()
675 * --------------------------
676 *
677 * On entry,
678 * Rules can be in any state
679 * On exit,
680 * Any existing rules will have been kept.
681 * Any new rules will have been loaded.
682 * Returns 0 if no error, 0 if error!
683 *
684 * Bugs:
685 * The strings may not contain spaces.
686 */
687
688 int HTLoadRules(const char *filename)
689 {
690 FILE *fp = fopen(filename, TXT_R);
691 char line[LINE_LENGTH + 1];
692
693 if (!fp) {
694 CTRACE((tfp, "HTRules: Can't open rules file %s\n", filename));
695 return -1; /* File open error */
696 }
697 for (;;) {
698 if (!fgets(line, LINE_LENGTH + 1, fp))
699 break; /* EOF or error */
700 (void) HTSetConfiguration(line);
701 }
702 fclose(fp);
703 return 0; /* No error or syntax errors ignored */
704 }
705
706 #endif /* NO_RULES */