home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Networking / wu-ftpd-2.4.2b13-MIHS / src / acl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-03  |  6.5 KB  |  199 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. #ifndef lint
  33. char * rcsid = "$Id: acl.c,v 1.5 1997/01/14 22:45:48 sob Exp $";
  34. #endif
  35. #include "config.h"
  36.  
  37. #include <stdio.h>
  38. #include <errno.h>
  39. #include <string.h>
  40. #ifdef SYSSYSLOG
  41. #include <sys/syslog.h>
  42. #else
  43. #include <syslog.h>
  44. #endif
  45.  
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #include <sys/file.h>
  49.  
  50. #include "pathnames.h"
  51. #include "extensions.h"
  52.  
  53. char *aclbuf = NULL;
  54. static struct aclmember *aclmembers;
  55.  
  56. /*************************************************************************/
  57. /* FUNCTION  : getaclentry                                               */
  58. /* PURPOSE   : Retrieve a named entry from the ACL                       */
  59. /* ARGUMENTS : pointer to the keyword and a handle to the acl members    */
  60. /* RETURNS   : pointer to the acl member containing the keyword or NULL  */
  61. /*************************************************************************/
  62.  
  63. struct aclmember *
  64. #ifdef __STDC__
  65. getaclentry(char *keyword, struct aclmember **next)
  66. #else
  67. getaclentry(keyword,next)
  68. char *keyword;
  69. struct aclmember **next;
  70. #endif
  71. {
  72.     do {
  73.         if (!*next)
  74.             *next = aclmembers;
  75.         else
  76.             *next = (*next)->next;
  77.     } while (*next && strcmp((*next)->keyword, keyword));
  78.  
  79.     return (*next);
  80. }
  81.  
  82. /*************************************************************************/
  83. /* FUNCTION  : parseacl                                                  */
  84. /* PURPOSE   : Parse the acl buffer into its components                  */
  85. /* ARGUMENTS : A pointer to the acl file                                 */
  86. /* RETURNS   : nothing                                                   */
  87. /*************************************************************************/
  88.  
  89. void
  90. #ifdef __STDC__
  91. parseacl(void)
  92. #else
  93. parseacl()
  94. #endif
  95. {
  96.     char *ptr,
  97.      *aclptr = aclbuf,
  98.      *line;
  99.     int cnt;
  100.     struct aclmember *member,
  101.      *acltail;
  102.  
  103.     if (!aclbuf || !(*aclbuf))
  104.         return;
  105.  
  106.     aclmembers = (struct aclmember *) NULL;
  107.     acltail = (struct aclmember *) NULL;
  108.  
  109.     while (*aclptr != '\0') {
  110.         line = aclptr;
  111.         while (*aclptr && *aclptr != '\n')
  112.             aclptr++;
  113.         *aclptr++ = (char) NULL;
  114.  
  115.         /* deal with comments */
  116.         if ((ptr = strchr(line, '#')) != NULL)
  117.             /* allowed escaped '#' chars for path-filter (DiB) */
  118.             if (*(ptr-1) != '\\')
  119.                 *ptr = '\0';
  120.  
  121.         ptr = strtok(line, " \t");
  122.         if (ptr) {
  123.             member = (struct aclmember *) calloc(1, sizeof(struct aclmember));
  124.  
  125.             (void) strcpy(member->keyword, ptr);
  126.             cnt = 0;
  127.             while ((ptr = strtok(NULL, " \t")) != NULL) {
  128.         if (cnt >= MAXARGS) {
  129.             syslog(LOG_ERR,
  130.             "Too many args (>%d) in ftpaccess: %s %s %s %s %s ...",
  131.             MAXARGS - 1, member->keyword, member->arg[0],
  132.             member->arg[1], member->arg[2], member->arg[3]);
  133.             break;
  134.         }
  135.                 member->arg[cnt++] = ptr;
  136.         }
  137.             if (acltail)
  138.                 acltail->next = member;
  139.             acltail = member;
  140.             if (!aclmembers)
  141.                 aclmembers = member;
  142.         }
  143.     }
  144. }
  145.  
  146. /*************************************************************************/
  147. /* FUNCTION  : readacl                                                   */
  148. /* PURPOSE   : Read the acl into memory                                  */
  149. /* ARGUMENTS : The pathname of the acl                                   */
  150. /* RETURNS   : 0 if error, 1 if no error                                 */
  151. /*************************************************************************/
  152.  
  153. int
  154. #ifdef __STDC__
  155. readacl(char *aclpath)
  156. #else
  157. readacl(aclpath)
  158. char *aclpath;
  159. #endif
  160. {
  161.     FILE *aclfile;
  162.     struct stat finfo;
  163.     extern int use_accessfile;
  164.  
  165.     if (!use_accessfile)
  166.         return (0);
  167.  
  168.     if ((aclfile = fopen(aclpath, "r")) == NULL) {
  169.         syslog(LOG_ERR, "cannot open access file %s: %s", aclpath,
  170.                strerror(errno));
  171.         return (0);
  172.     }
  173.     if (fstat(fileno(aclfile), &finfo) != 0) {
  174.         syslog(LOG_ERR, "cannot fstat access file %s: %s", aclpath,
  175.                strerror(errno));
  176.         (void) fclose(aclfile);
  177.         return (0);
  178.     }
  179.     if (finfo.st_size == 0) {
  180.         aclbuf = (char *) calloc(1, 1);
  181.     } else {
  182.         if (!(aclbuf = (char *)malloc((unsigned) finfo.st_size + 1))) {
  183.             syslog(LOG_ERR, "could not malloc aclbuf (%d bytes)", finfo.st_size + 1);
  184.             (void) fclose(aclfile);
  185.             return (0);
  186.         }
  187.         if (!fread(aclbuf, (size_t) finfo.st_size, 1, aclfile)) {
  188.             syslog(LOG_ERR, "error reading acl file %s: %s", aclpath,
  189.                    strerror(errno));
  190.             aclbuf = NULL;
  191.             (void) fclose(aclfile);
  192.             return (0);
  193.         }
  194.         *(aclbuf + finfo.st_size) = '\0';
  195.     }
  196.     (void) fclose(aclfile);
  197.     return (1);
  198. }
  199.