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