home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTACL.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  5.9 KB  |  218 lines

  1.  
  2. /* MODULE                            HTACL.c
  3. **        ACCESS CONTROL LIST ROUTINES
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **    MD     Mark Donszelmann    duns@vxdeop.cern.ch
  8. **
  9. ** HISTORY:
  10. **     8 Nov 93  MD    (VMS only) case insensitive compare reading acl entry, filename
  11. **
  12. **
  13. ** BUGS:
  14. **
  15. **
  16. */
  17.  
  18.  
  19. #include "HTUtils.h"
  20.  
  21. /*#include <stdio.h> included by HTUtils.h -- FM *//* FILE */
  22. #include <string.h>
  23.  
  24. #include "HTAAFile.h"    /* File routines    */
  25. #include "HTGroup.h"    /* GroupDef        */
  26. #include "HTACL.h"    /* Implemented here    */
  27.  
  28. #include "LYLeaks.h"
  29.  
  30. /* PUBLIC                        HTAA_getAclFilename()
  31. **        RESOLVE THE FULL PATHNAME OF ACL FILE FOR A GIVEN FILE
  32. ** ON ENTRY:
  33. **    path    is the pathname of the file for which to
  34. **        ACL file should be found.
  35. **
  36. **        ACL filename is computed by replacing
  37. **        the filename by .www_acl in the pathname
  38. **        (this is done to a local copy, of course).
  39. **
  40. ** ON EXIT:
  41. **    returns    the absolute pathname of ACL file
  42. **        (which is automatically freed next time
  43. **        this fuction is called).
  44. */
  45. PUBLIC char *HTAA_getAclFilename ARGS1(CONST char *, pathname)
  46. {
  47.     static char * local_copy = NULL;
  48.     static char * acl_path = NULL;
  49.     char * directory = NULL;
  50.     char * filename = NULL;
  51.  
  52.     StrAllocCopy(local_copy, pathname);    /* Also frees local_copy */
  53.                                         /* from previous call.   */
  54.  
  55.     directory = local_copy;
  56.     filename = strrchr(directory, '/');
  57.     if (!filename) {        /* No path in front of filename */
  58.     directory = ".";    /* So use current directory */
  59.     filename = local_copy;    /* and the pathname itself is the filename */
  60.     }
  61.     else {
  62.     *filename = '\0'; /* Truncate filename off from directory path */
  63.     filename++;      /* and the filename begins from the next character */
  64.     }
  65.     
  66.     StrAllocCopy(acl_path, directory);    /* Also frees acl_path */
  67.                                         /* from previous call. */
  68.     StrAllocCat(acl_path, "/");
  69.     StrAllocCat(acl_path, ACL_FILE_NAME);
  70.  
  71.     return acl_path;
  72. }
  73.  
  74.  
  75. /* PUBLIC                        HTAA_openAcl()
  76. **        OPEN THE ACL FILE FOR THE GIVEN DOCUMENT
  77. ** ON ENTRY:
  78. **    pathname    is the absolute pathname of
  79. **            the file to be accessed.
  80. **
  81. ** ON EXIT:
  82. **    returns        the FILE* to open ACL.
  83. **            NULL, if ACL not found.
  84. */
  85. PUBLIC FILE *HTAA_openAcl ARGS1(CONST char *, pathname)
  86. {
  87.     return fopen(HTAA_getAclFilename(pathname), "r");
  88. }
  89.  
  90.  
  91. /* PUBLIC                        HTAA_closeAcl()
  92. **            CLOSE ACL FILE
  93. ** ON ENTRY:
  94. **    acl_file is Access Control List file to close.
  95. **
  96. ** ON EXIT:
  97. **    returns    nothing.
  98. */
  99. PUBLIC void HTAA_closeAcl ARGS1(FILE *, acl_file)
  100. {
  101.     if (acl_file)  fclose(acl_file);
  102. }
  103.  
  104.  
  105. /* PUBLIC                        HTAA_getAclEntry()
  106. **            CONSULT THE ACCESS CONTROL LIST AND
  107. **            GIVE A LIST OF GROUPS (AND USERS)
  108. **            AUTHORIZED TO ACCESS A GIVEN FILE
  109. ** ON ENTRY:
  110. **    acl_file    is an open ACL file.
  111. **    pathname    is the absolute pathname of
  112. **            the file to be accessed.
  113. **    method        is the method for which access is wanted.
  114. **
  115. ** ALC FILE FORMAT:
  116. **
  117. **    template : method, method, ... : group@addr, user, group, ...
  118. **
  119. **    The last item is in fact in exactly the same format as
  120. **    group definition in group file, i.e. everything that
  121. **    follows the 'groupname:' part,
  122. **    e.g.
  123. **        user, group, user@address, group@address,
  124. **        (user,group,...)@(address, address, ...)
  125. **
  126. ** ON EXIT:
  127. **    returns        NULL, if there is no entry for the file in the ACL,
  128. **            or ACL doesn't exist.
  129. **            If there is, a GroupDef object containing the
  130. **            group and user names allowed to access the file
  131. **            is returned (this is automatically freed
  132. **            next time this function is called).
  133. ** IMPORTANT:
  134. **    Returns the first entry with matching template and
  135. **    method. This function should be called multiple times
  136. **    to process all the valid entries (until it returns NULL).
  137. **    This is because there can be multiple entries like:
  138. **
  139. **        *.html : get,put : ari,timbl,robert
  140. **        *.html : get     : jim,james,jonathan,jojo
  141. **
  142. ** NOTE:
  143. **    The returned group definition may well contain references
  144. **    to groups defined in group file. Therefore these references
  145. **    must be resolved according to that rule file by function
  146. **    HTAA_resolveGroupReferences() (group file is read in by
  147. **    HTAA_readGroupFile()) and after that access authorization
  148. **    can be checked with function HTAA_userAndInetGroup().
  149. */
  150. PUBLIC GroupDef *HTAA_getAclEntry ARGS3(FILE *,        acl_file,
  151.                     CONST char *,    pathname,
  152.                     HTAAMethod,    method)
  153. {
  154.     static GroupDef * group_def = NULL;
  155.     CONST char * filename;
  156.     int len;
  157.     char *buf;
  158.  
  159.     if (!acl_file) return NULL;        /* ACL doesn't exist */
  160.     
  161.     if (group_def) {
  162.     GroupDef_delete(group_def);    /* From previous call */
  163.     group_def = NULL;
  164.     }
  165.  
  166.     if (!(filename = strrchr(pathname, '/')))
  167.     filename = pathname;
  168.     else filename++;    /* Skip slash */
  169.  
  170.     len = strlen(filename);
  171.  
  172.     if (!(buf = (char*)malloc((strlen(filename)+2)*sizeof(char))))
  173.     outofmem(__FILE__, "HTAA_getAuthorizedGroups");
  174.     
  175.     while (EOF != HTAAFile_readField(acl_file, buf, len+1)) {
  176. #ifdef VMS
  177.     if (HTAA_templateCaseMatch(buf, filename)) {
  178. #else /* not VMS */
  179.     if (HTAA_templateMatch(buf, filename)) {
  180. #endif /* not VMS */
  181.         HTList *methods = HTList_new();
  182.         HTAAFile_readList(acl_file, methods, MAX_METHODNAME_LEN);
  183.         if (TRACE) {
  184.         fprintf(stderr,
  185.             "Filename '%s' matched template '%s', allowed methods:",
  186.             filename, buf);
  187.         }    
  188.         if (HTAAMethod_inList(method, methods)) {    /* right method? */
  189.         if (TRACE) fprintf(stderr, " METHOD OK\n");
  190.         HTList_delete(methods);
  191.         free(buf);
  192.         group_def = HTAA_parseGroupDef(acl_file);
  193.         /*
  194.         ** HTAA_parseGroupDef() already reads the record
  195.         ** separator so we don't call HTAAFile_nextRec().
  196.         */
  197.         return group_def;
  198.         }
  199.         else if (TRACE) fprintf(stderr, " METHOD NOT FOUND\n");
  200.         HTList_delete(methods);
  201.     }    /* if template match */
  202.     else {
  203.         if (TRACE) {
  204.         fprintf(stderr,
  205.             "Filename '%s' didn't match template '%s'\n",
  206.             filename, buf);
  207.         }
  208.     }
  209.  
  210.     HTAAFile_nextRec(acl_file);
  211.     }    /* while not eof */
  212.     free(buf);
  213.  
  214.     return NULL;    /* No entry for requested file */
  215.                         /* (or an empty entry).        */
  216. }
  217.  
  218.