HTAAUtil.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:10k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*       HTAAUtil.c
  2. ** COMMON PARTS OF ACCESS AUTHORIZATION MODULE
  3. ** FOR BOTH SERVER AND BROWSER
  4. **
  5. ** (c) COPYRIGHT MIT 1995.
  6. ** Please first read the full copyright statement in the file COPYRIGH.
  7. **
  8. ** IMPORTANT:
  9. ** Routines in this module use dynamic allocation, but free
  10. ** automatically all the memory reserved by them.
  11. **
  12. ** Therefore the caller never has to (and never should)
  13. ** free() any object returned by these functions.
  14. **
  15. ** Therefore also all the strings returned by this package
  16. ** are only valid until the next call to the same function
  17. ** is made. This approach is selected, because of the nature
  18. ** of access authorization: no string returned by the package
  19. ** needs to be valid longer than until the next call.
  20. **
  21. ** This also makes it easy to plug the AA package in:
  22. ** you don't have to ponder whether to free() something
  23. ** here or is it done somewhere else (because it is always
  24. ** done somewhere else).
  25. **
  26. ** The strings that the package needs to store are copied
  27. ** so the original strings given as parameters to AA
  28. ** functions may be freed or modified with no side effects.
  29. **
  30. ** The AA package does not free() anything else than what
  31. ** it has itself allocated.
  32. **
  33. ** AA (Access Authorization) package means modules which
  34. ** names start with HTAA.
  35. **
  36. ** AUTHORS:
  37. ** AL Ari Luotonen luotonen@dxcern.cern.ch
  38. ** MD  Mark Donszelmann    duns@vxdeop.cern.ch
  39. **
  40. ** HISTORY:
  41. **  8 Nov 93  MD (VMS only) Added case insensitive comparison
  42. ** in HTAA_templateCaseMatch
  43. **
  44. **
  45. ** BUGS:
  46. **
  47. **
  48. */
  49. /* Library include files */
  50. #include "tcp.h"
  51. #include "HTUtils.h"
  52. #include "HTString.h"
  53. #include "HTAAUtil.h" /* Implemented here */
  54. #include "HTAssoc.h" /* Assoc list */
  55. /* PUBLIC HTAAScheme_enum()
  56. ** TRANSLATE SCHEME NAME INTO
  57. ** A SCHEME ENUMERATION
  58. **
  59. ** ON ENTRY:
  60. ** name is a string representing the scheme name.
  61. **
  62. ** ON EXIT:
  63. ** returns the enumerated constant for that scheme.
  64. */
  65. PUBLIC HTAAScheme HTAAScheme_enum (CONST char* name)
  66. {
  67.     static char *upcased = NULL;
  68.     char *cur;
  69.     if (!name) return HTAA_UNKNOWN;
  70.     StrAllocCopy(upcased, name);
  71.     cur = upcased;
  72.     while (*cur) {
  73. *cur = TOUPPER(*cur);
  74. cur++;
  75.     }
  76.     
  77.     if      (!strncmp(upcased, "NONE", 4))    return HTAA_NONE;
  78.     else if (!strncmp(upcased, "BASIC", 5))    return HTAA_BASIC;
  79.     else if (!strncmp(upcased, "PUBKEY", 6))    return HTAA_PUBKEY;
  80.     else if (!strncmp(upcased, "KERBEROSV4", 10))  return HTAA_KERBEROS_V4;
  81.     else if (!strncmp(upcased, "KERBEROSV5", 10))  return HTAA_KERBEROS_V5;
  82.     else    return HTAA_UNKNOWN;
  83. }
  84. /* PUBLIC HTAAScheme_name()
  85. ** GET THE NAME OF A GIVEN SCHEME
  86. ** ON ENTRY:
  87. ** scheme is one of the scheme enum values:
  88. ** HTAA_NONE, HTAA_BASIC, HTAA_PUBKEY, ...
  89. **
  90. ** ON EXIT:
  91. ** returns the name of the scheme, i.e.
  92. ** "None", "Basic", "Pubkey", ...
  93. */
  94. PUBLIC char *HTAAScheme_name (HTAAScheme scheme)
  95. {
  96.     switch (scheme) {
  97.       case HTAA_NONE: return "None";          break;
  98.       case HTAA_BASIC: return "Basic";         break;
  99.       case HTAA_PUBKEY: return "Pubkey";        break;
  100.       case HTAA_KERBEROS_V4: return "KerberosV4"; break;
  101.       case HTAA_KERBEROS_V5: return "KerberosV5"; break;
  102.       case HTAA_UNKNOWN: return "UNKNOWN";       break;
  103.       default: return "THIS-IS-A-BUG";
  104.     }
  105. }
  106. /* PUBLIC HTAA_templateMatch()
  107. ** STRING COMPARISON FUNCTION FOR FILE NAMES
  108. **    WITH ONE WILDCARD * IN THE TEMPLATE
  109. ** NOTE:
  110. ** This is essentially the same code as in HTRules.c, but it
  111. ** cannot be used because it is embedded in between other code.
  112. ** (In fact, HTRules.c should use this routine, but then this
  113. **  routine would have to be more sophisticated... why is life
  114. **  sometimes so hard...)
  115. **
  116. ** ON ENTRY:
  117. ** template is a template string to match the file name
  118. ** agaist, may contain a single wildcard
  119. ** character * which matches zero or more
  120. ** arbitrary characters.
  121. ** filename is the filename (or pathname) to be matched
  122. ** agaist the template.
  123. **
  124. ** ON EXIT:
  125. ** returns YES, if filename matches the template.
  126. ** NO, otherwise.
  127. */
  128. PUBLIC BOOL HTAA_templateMatch (CONST char * tmplate, 
  129.      CONST char * filename)
  130. {
  131.     CONST char *p = tmplate;
  132.     CONST char *q = filename;
  133.     int m;
  134.     if (!tmplate || !filename) {
  135. if (PROT_TRACE)
  136.     TTYPrint(TDEST, "HTAA_templateMatch: invalid param: %s is NULL!!n",
  137.     (tmplate ? "filename" : "template"));
  138. return NO;
  139.     }
  140.     for( ; *p  &&  *q  &&  *p == *q; p++, q++) /* Find first mismatch */
  141. ; /* do nothing else */
  142.     if (!*p && !*q) return YES; /* Equally long equal strings */
  143.     else if ('*' == *p) { /* Wildcard */
  144. p++; /* Skip wildcard character */
  145. m = strlen(q) - strlen(p); /* Amount to match to wildcard */
  146. if (m < 0) return NO; /* No match, filename too short */
  147. else { /* Skip the matched characters and compare */
  148.     if (strcmp(p, q+m)) return NO; /* Tail mismatch */
  149.     else                return YES; /* Tail match */
  150. }
  151.     } /* if wildcard */
  152.     else return NO; /* Length or character mismatch */
  153. }    
  154. /* PUBLIC HTAA_templateCaseMatch()
  155. ** STRING COMPARISON FUNCTION FOR FILE NAMES
  156. **    WITH ONE WILDCARD * IN THE TEMPLATE (Case Insensitive)
  157. ** NOTE:
  158. ** This is essentially the same code as in HTAA_templateMatch, but
  159. ** it compares case insensitive (for VMS). Reason for this routine
  160. ** is that HTAA_templateMatch gets called from several places, also 
  161. ** there where a case sensitive match is needed, so one cannot just
  162. ** change the HTAA_templateMatch routine for VMS.
  163. **
  164. ** ON ENTRY:
  165. ** tmplate is a template string to match the file name
  166. ** agaist, may contain a single wildcard
  167. ** character * which matches zero or more
  168. ** arbitrary characters.
  169. ** filename is the filename (or pathname) to be matched
  170. ** agaist the template.
  171. **
  172. ** ON EXIT:
  173. ** returns YES, if filename matches the template.
  174. ** NO, otherwise.
  175. */
  176. PUBLIC BOOL HTAA_templateCaseMatch (CONST char * tmplate, 
  177.           CONST char * filename)
  178. {
  179.     CONST char *p = tmplate;
  180.     CONST char *q = filename;
  181.     int m;
  182.     if (!tmplate || !filename) {
  183. if (PROT_TRACE)
  184.     TTYPrint(TDEST,
  185.     "HTAA_templateCaseMatch: invalid param: %s is NULL!!n",
  186.     (tmplate ? "filename" : "template"));
  187. return NO;
  188.     }
  189.     for( ; *p  &&  *q  &&  TOUPPER(*p) == TOUPPER(*q); p++, q++) /* Find first mismatch */
  190. ; /* do nothing else */
  191.     if (!*p && !*q) return YES; /* Equally long equal strings */
  192.     else if ('*' == *p) { /* Wildcard */
  193. p++; /* Skip wildcard character */
  194. m = strlen(q) - strlen(p); /* Amount to match to wildcard */
  195. if (m < 0) return NO; /* No match, filename too short */
  196. else { /* Skip the matched characters and compare */
  197.     if (strcasecomp(p, q+m)) return NO; /* Tail mismatch */
  198.     else                return YES; /* Tail match */
  199. }
  200.     } /* if wildcard */
  201.     else return NO; /* Length or character mismatch */
  202. }    
  203. /* PUBLIC HTAA_makeProtectionTemplate()
  204. ** CREATE A PROTECTION TEMPLATE FOR THE FILES
  205. ** IN THE SAME DIRECTORY AS THE GIVEN FILE
  206. ** (Used by server if there is no fancier way for
  207. ** it to tell the client, and by browser if server
  208. ** didn't send WWW-ProtectionTemplate: field)
  209. ** ON ENTRY:
  210. ** docname is the document pathname (from URL).
  211. **
  212. ** ON EXIT:
  213. ** returns a template matching docname, and other files
  214. ** files in that directory.
  215. **
  216. ** E.g.  /foo/bar/x.html  =>  /foo/bar/ *
  217. **     ^
  218. ** Space only to prevent it from
  219. ** being a comment marker here,
  220. ** there really isn't any space.
  221. */
  222. PUBLIC char *HTAA_makeProtectionTemplate (CONST char * docname)
  223. {
  224.     char *tmplate = NULL;
  225.     char *slash = NULL;
  226.     if (docname) {
  227. StrAllocCopy(tmplate, docname);
  228. slash = strrchr(tmplate, '/');
  229. if (slash) slash++;
  230. else slash = tmplate;
  231. *slash = (char)0;
  232. StrAllocCat(tmplate, "*");
  233.     }
  234.     else StrAllocCopy(tmplate, "*");
  235.     if (PROT_TRACE)
  236. TTYPrint(TDEST, "make_template: made template `%s' for file `%s'n",
  237. tmplate, docname);
  238.     return tmplate;
  239. }
  240. /*
  241. ** Skip leading whitespace from *s forward
  242. */
  243. #define SKIPWS(s) while (*s==' ' || *s=='t') s++;
  244. /*
  245. ** Kill trailing whitespace starting from *(s-1) backwords
  246. */
  247. #define KILLWS(s) {char *c=s-1; while (*c==' ' || *c=='t') *(c--)=(char)0;}
  248. /* PUBLIC HTAA_parseArgList()
  249. ** PARSE AN ARGUMENT LIST GIVEN IN A HEADER FIELD
  250. ** ON ENTRY:
  251. ** str is a comma-separated list:
  252. **
  253. ** item, item, item
  254. ** where
  255. ** item ::= value
  256. **        | name=value
  257. **        | name="value"
  258. **
  259. ** Leading and trailing whitespace is ignored
  260. ** everywhere except inside quotes, so the following
  261. ** examples are equal:
  262. **
  263. ** name=value,foo=bar
  264. **  name="value",foo="bar"
  265. **   name = value ,  foo = bar
  266. **    name = "value" ,  foo = "bar"
  267. **
  268. ** ON EXIT:
  269. ** returns a list of name-value pairs (actually HTAssocList*).
  270. ** For items with no name, just value, the name is
  271. ** the number of order number of that item. E.g.
  272. ** "1" for the first, etc.
  273. */
  274. PUBLIC HTAssocList *HTAA_parseArgList (char * str)
  275. {
  276.     HTAssocList *assoc_list = HTAssocList_new();
  277.     char *cur = NULL;
  278.     char *name = NULL;
  279.     int index = 0;
  280.     if (!str) return assoc_list;
  281.     while (*str) {
  282. SKIPWS(str); /* Skip leading whitespace */
  283. cur = str;
  284. index++;
  285. while (*cur  &&  *cur != '='  &&  *cur != ',')
  286.     cur++; /* Find end of name (or lonely value without a name) */
  287. KILLWS(cur); /* Kill trailing whitespace */
  288. if (*cur == '=') { /* Name followed by a value */
  289.     *(cur++) = (char)0; /* Terminate name */
  290.     StrAllocCopy(name, str);
  291.     SKIPWS(cur); /* Skip WS leading the value */
  292.     str = cur;
  293.     if (*str == '"') { /* Quoted value */
  294. str++;
  295. cur = str;
  296. while (*cur  &&  *cur != '"') cur++;
  297. if (*cur == '"')
  298.     *(cur++) = (char)0; /* Terminate value */
  299. /* else it is lacking terminating quote */
  300. SKIPWS(cur); /* Skip WS leading comma */
  301. if (*cur == ',') cur++; /* Skip separating colon */
  302.     }
  303.     else { /* Unquoted value */
  304. while (*cur  &&  *cur != ',') cur++;
  305. KILLWS(cur); /* Kill trailing whitespace */
  306. if (*cur == ',')
  307.     *(cur++) = (char)0;
  308. /* else *cur already NULL */
  309.     }
  310. }
  311. else { /* No name, just a value */
  312.     if (*cur == ',') 
  313. *(cur++) = (char)0; /* Terminate value */
  314.     /* else last value on line (already terminated by NULL) */
  315.     StrAllocCopy(name, "nnn"); /* Room for item order number */
  316.     sprintf(name, "%d", index); /* Item order number for name */
  317. }
  318. HTAssocList_add(assoc_list, name, str);
  319. str = cur;
  320.     } /* while *str */
  321.     HT_FREE(name);                                         /* Henrik 14/03-94 */
  322.     return assoc_list;
  323. }