📄 htaautil.c
字号:
/* MODULE HTAAUtil.c** COMMON PARTS OF ACCESS AUTHORIZATION MODULE** FOR BOTH SERVER AND BROWSER**** IMPORTANT:** Routines in this module use dynamic allocation, but free** automatically all the memory reserved by them.**** Therefore the caller never has to (and never should)** free() any object returned by these functions.**** Therefore also all the strings returned by this package** are only valid until the next call to the same function** is made. This approach is selected, because of the nature** of access authorization: no string returned by the package** needs to be valid longer than until the next call.**** This also makes it easy to plug the AA package in:** you don't have to ponder whether to free() something** here or is it done somewhere else (because it is always** done somewhere else).**** The strings that the package needs to store are copied** so the original strings given as parameters to AA** functions may be freed or modified with no side effects.**** The AA package does not free() anything else than what** it has itself allocated.**** AA (Access Authorization) package means modules which** names start with HTAA.**** AUTHORS:** AL Ari Luotonen luotonen@dxcern.cern.ch** MD Mark Donszelmann duns@vxdeop.cern.ch**** HISTORY:** 8 Nov 93 MD (VMS only) Added case insensitive comparison in HTAA_templateCaseMatch****** BUGS:*****/#include <HTUtils.h>#include <HTAAUtil.h> /* Implemented here */#include <HTAssoc.h> /* Assoc list */#include <HTTCP.h>#include <HTTP.h>#include <LYStrings.h>#include <LYLeaks.h>/* PUBLIC HTAAScheme_enum()** TRANSLATE SCHEME NAME INTO** A SCHEME ENUMERATION**** ON ENTRY:** name is a string representing the scheme name.**** ON EXIT:** returns the enumerated constant for that scheme.*/PUBLIC HTAAScheme HTAAScheme_enum ARGS1(CONST char*, name){ char *upcased = NULL; if (!name) return HTAA_UNKNOWN; StrAllocCopy(upcased, name); LYUpperCase(upcased); if (!strncmp(upcased, "NONE", 4)) { FREE(upcased); return HTAA_NONE; } else if (!strncmp(upcased, "BASIC", 5)) { FREE(upcased); return HTAA_BASIC; } else if (!strncmp(upcased, "PUBKEY", 6)) { FREE(upcased); return HTAA_PUBKEY; } else if (!strncmp(upcased, "KERBEROSV4", 10)) { FREE(upcased); return HTAA_KERBEROS_V4; } else if (!strncmp(upcased, "KERBEROSV5", 10)) { FREE(upcased); return HTAA_KERBEROS_V5; } else { FREE(upcased); return HTAA_UNKNOWN; }}/* PUBLIC HTAAScheme_name()** GET THE NAME OF A GIVEN SCHEME** ON ENTRY:** scheme is one of the scheme enum values:** HTAA_NONE, HTAA_BASIC, HTAA_PUBKEY, ...**** ON EXIT:** returns the name of the scheme, i.e.** "None", "Basic", "Pubkey", ...*/PUBLIC char *HTAAScheme_name ARGS1(HTAAScheme, scheme){ switch (scheme) { case HTAA_NONE: return "None"; case HTAA_BASIC: return "Basic"; case HTAA_PUBKEY: return "Pubkey"; case HTAA_KERBEROS_V4: return "KerberosV4"; case HTAA_KERBEROS_V5: return "KerberosV5"; case HTAA_UNKNOWN: return "UNKNOWN"; default: return "THIS-IS-A-BUG"; }}/* PUBLIC HTAAMethod_enum()** TRANSLATE METHOD NAME INTO AN ENUMERATED VALUE** ON ENTRY:** name is the method name to translate.**** ON EXIT:** returns HTAAMethod enumerated value corresponding** to the given name.*/PUBLIC HTAAMethod HTAAMethod_enum ARGS1(CONST char *, name){ if (!name) return METHOD_UNKNOWN; if (0==strcasecomp(name, "GET")) return METHOD_GET; else if (0==strcasecomp(name, "PUT")) return METHOD_PUT; else return METHOD_UNKNOWN;}/* PUBLIC HTAAMethod_name()** GET THE NAME OF A GIVEN METHOD** ON ENTRY:** method is one of the method enum values:** METHOD_GET, METHOD_PUT, ...**** ON EXIT:** returns the name of the scheme, i.e.** "GET", "PUT", ...*/PUBLIC char *HTAAMethod_name ARGS1(HTAAMethod, method){ switch (method) { case METHOD_GET: return "GET"; case METHOD_PUT: return "PUT"; case METHOD_UNKNOWN: return "UNKNOWN"; default: return "THIS-IS-A-BUG"; }}/* PUBLIC HTAAMethod_inList()** IS A METHOD IN A LIST OF METHOD NAMES** ON ENTRY:** method is the method to look for.** list is a list of method names.**** ON EXIT:** returns YES, if method was found.** NO, if not found.*/PUBLIC BOOL HTAAMethod_inList ARGS2(HTAAMethod, method, HTList *, list){ HTList *cur = list; char *item; while (NULL != (item = (char*)HTList_nextObject(cur))) { CTRACE((tfp, " %s", item)); if (method == HTAAMethod_enum(item)) return YES; } return NO; /* Not found */}/* PUBLIC HTAA_templateMatch()** STRING COMPARISON FUNCTION FOR FILE NAMES** WITH ONE WILDCARD * IN THE TEMPLATE** NOTE:** This is essentially the same code as in HTRules.c, but it** cannot be used because it is embedded in between other code.** (In fact, HTRules.c should use this routine, but then this** routine would have to be more sophisticated... why is life** sometimes so hard...)**** ON ENTRY:** template is a template string to match the file name** against, may contain a single wildcard** character * which matches zero or more** arbitrary characters.** filename is the filename (or pathname) to be matched** against the template.**** ON EXIT:** returns YES, if filename matches the template.** NO, otherwise.*/PUBLIC BOOL HTAA_templateMatch ARGS2(CONST char *, template, CONST char *, filename){ CONST char *p = template; CONST char *q = filename; int m; for (; *p && *q && *p == *q; p++, q++) /* Find first mismatch */ ; /* do nothing else */ if (!*p && !*q) return YES; /* Equally long equal strings */ else if ('*' == *p) { /* Wildcard */ p++; /* Skip wildcard character */ m = strlen(q) - strlen(p); /* Amount to match to wildcard */ if (m < 0) return NO; /* No match, filename too short */ else { /* Skip the matched characters and compare */ if (strcmp(p, q+m)) return NO; /* Tail mismatch */ else return YES; /* Tail match */ } } /* if wildcard */ else return NO; /* Length or character mismatch */}/* PUBLIC HTAA_templateCaseMatch()** STRING COMPARISON FUNCTION FOR FILE NAMES** WITH ONE WILDCARD * IN THE TEMPLATE (Case Insensitive)** NOTE:** This is essentially the same code as in HTAA_templateMatch, but** it compares case insensitive (for VMS). Reason for this routine** is that HTAA_templateMatch gets called from several places, also** there where a case sensitive match is needed, so one cannot just** change the HTAA_templateMatch routine for VMS.**** ON ENTRY:** template is a template string to match the file name** against, may contain a single wildcard** character * which matches zero or more** arbitrary characters.** filename is the filename (or pathname) to be matched** against the template.**** ON EXIT:** returns YES, if filename matches the template.** NO, otherwise.*/PUBLIC BOOL HTAA_templateCaseMatch ARGS2(CONST char *, template, CONST char *, filename){ CONST char *p = template; CONST char *q = filename; int m; /* Find first mismatch */ for (; *p && *q && TOUPPER(*p) == TOUPPER(*q); p++, q++) ; /* do nothing else */ if (!*p && !*q) return YES; /* Equally long equal strings */ else if ('*' == *p) { /* Wildcard */ p++; /* Skip wildcard character */ m = strlen(q) - strlen(p); /* Amount to match to wildcard */ if (m < 0) return NO; /* No match, filename too short */ else { /* Skip the matched characters and compare */ if (strcasecomp(p, q+m)) return NO; /* Tail mismatch */ else return YES; /* Tail match */ } } /* if wildcard */ else return NO; /* Length or character mismatch */}/* PUBLIC HTAA_makeProtectionTemplate()** CREATE A PROTECTION TEMPLATE FOR THE FILES** IN THE SAME DIRECTORY AS THE GIVEN FILE** (Used by server if there is no fancier way for** it to tell the client, and by browser if server** didn't send WWW-ProtectionTemplate: field)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -