home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / tcpip / amitcp-support / wustl-ftpdaemon / src / acl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  5.9 KB  |  172 lines

  1. /* Copyright (c) 1993, 1994  Washington University in Saint Louis
  2.  * All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are
  6.  * met: 1. Redistributions of source code must retain the above copyright
  7.  * notice, this list of conditions and the following disclaimer. 2.
  8.  * Redistributions in binary form must reproduce the above copyright notice,
  9.  * this list of conditions and the following disclaimer in the documentation
  10.  * and/or other materials provided with the distribution. 3. All advertising
  11.  * materials mentioning features or use of this software must display the
  12.  * following acknowledgement: This product includes software developed by the
  13.  * Washington University in Saint Louis and its contributors. 4. Neither the
  14.  * name of the University nor the names of its contributors may be used to
  15.  * endorse or promote products derived from this software without specific
  16.  * prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY WASHINGTON UNIVERSITY AND CONTRIBUTORS
  19.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASHINGTON
  22.  * UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. #include "config.h"
  33.  
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #ifdef SYSSYSLOG
  38. #include <sys/syslog.h>
  39. #else
  40. #include <syslog.h>
  41. #endif
  42.  
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #ifndef AMIGA
  46. #include <sys/file.h>
  47. #endif
  48.  
  49. #include "pathnames.h"
  50. #include "extensions.h"
  51.  
  52. char *aclbuf = NULL;
  53. static struct aclmember *aclmembers;
  54.  
  55. /*************************************************************************/
  56. /* FUNCTION  : getaclentry                                               */
  57. /* PURPOSE   : Retrieve a named entry from the ACL                       */
  58. /* ARGUMENTS : pointer to the keyword and a handle to the acl members    */
  59. /* RETURNS   : pointer to the acl member containing the keyword or NULL  */
  60. /*************************************************************************/
  61.  
  62. struct aclmember *
  63. getaclentry(char *keyword, struct aclmember **next)
  64. {
  65.     do {
  66.         if (!*next)
  67.             *next = aclmembers;
  68.         else
  69.             *next = (*next)->next;
  70.     } while (*next && strcmp((*next)->keyword, keyword));
  71.  
  72.     return (*next);
  73. }
  74.  
  75. /*************************************************************************/
  76. /* FUNCTION  : parseacl                                                  */
  77. /* PURPOSE   : Parse the acl buffer into its components                  */
  78. /* ARGUMENTS : A pointer to the acl file                                 */
  79. /* RETURNS   : nothing                                                   */
  80. /*************************************************************************/
  81.  
  82. void
  83. parseacl(void)
  84. {
  85.     char *ptr,
  86.      *aclptr = aclbuf,
  87.      *line;
  88.     int cnt;
  89.     struct aclmember *member,
  90.      *acltail;
  91.  
  92.     if (!aclbuf || !(*aclbuf))
  93.         return;
  94.  
  95.     aclmembers = (struct aclmember *) NULL;
  96.     acltail = (struct aclmember *) NULL;
  97.  
  98.     while (*aclptr != NULL) {
  99.         line = aclptr;
  100.         while (*aclptr && *aclptr != '\n')
  101.             aclptr++;
  102.         *aclptr++ = (char) NULL;
  103.  
  104.         /* deal with comments */
  105.         if ((ptr = strchr(line, '#')) != NULL)
  106.             /* allowed escaped '#' chars for path-filter (DiB) */
  107.             if (*(ptr-1) != '\\')
  108.                 *ptr = NULL;
  109.  
  110.         ptr = strtok(line, " \t");
  111.         if (ptr) {
  112.             member = (struct aclmember *) calloc(1, sizeof(struct aclmember));
  113.  
  114.             (void) strcpy(member->keyword, ptr);
  115.             cnt = 0;
  116.             while ((ptr = strtok(NULL, " \t")) != NULL)
  117.                 member->arg[cnt++] = ptr;
  118.             if (acltail)
  119.                 acltail->next = member;
  120.             acltail = member;
  121.             if (!aclmembers)
  122.                 aclmembers = member;
  123.         }
  124.     }
  125. }
  126.  
  127. /*************************************************************************/
  128. /* FUNCTION  : readacl                                                   */
  129. /* PURPOSE   : Read the acl into memory                                  */
  130. /* ARGUMENTS : The pathname of the acl                                   */
  131. /* RETURNS   : 0 if error, 1 if no error                                 */
  132. /*************************************************************************/
  133.  
  134. int
  135. readacl(char *aclpath)
  136. {
  137.     FILE *aclfile;
  138.     struct stat finfo;
  139.     extern int use_accessfile;
  140.  
  141.     if (!use_accessfile)
  142.         return (0);
  143.  
  144.     if (stat(aclpath, &finfo) != 0) {
  145.         syslog(LOG_ERR, "cannot stat access file %s: %s", aclpath,
  146.                strerror(errno));
  147.         return (0);
  148.     }
  149.     if ((aclfile = fopen(aclpath, "r")) == NULL) {
  150.         if (errno != ENOENT)
  151.             syslog(LOG_ERR, "cannot open access file %s: %s",
  152.                    aclpath, strerror(errno));
  153.         return (0);
  154.     }
  155.     if (finfo.st_size == 0) {
  156.         aclbuf = (char *) calloc(1, 1);
  157.     } else {
  158.         if (!(aclbuf = malloc((unsigned) finfo.st_size + 1))) {
  159.             syslog(LOG_ERR, "could not malloc aclbuf (%d bytes)", finfo.st_size + 1);
  160.             return (0);
  161.         }
  162.         if (!fread(aclbuf, (size_t) finfo.st_size, 1, aclfile)) {
  163.             syslog(LOG_ERR, "error reading acl file %s: %s", aclpath,
  164.                    strerror(errno));
  165.             aclbuf = NULL;
  166.             return (0);
  167.         }
  168.         *(aclbuf + finfo.st_size) = '\0';
  169.     }
  170.     return (1);
  171. }
  172.