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

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

Webmaster