Annotation of libwww/Library/src/HTRules.c, revision 2.23

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

Webmaster