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 / ftpcount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  9.7 KB  |  342 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. #include <signal.h>
  43. #include <time.h>
  44. #include <ctype.h>
  45.  
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #ifndef AMIGA
  49. #include <sys/file.h>
  50. #endif
  51. #include <sys/param.h>
  52.  
  53. #include "pathnames.h"
  54. #include "extensions.h"
  55.  
  56. #if defined(SVR4) || defined(ISC) || defined(AMIGA)
  57. #include <fcntl.h>
  58. #endif
  59.  
  60. struct c_list {
  61.     char *class;
  62.     struct c_list *next;
  63. };
  64.  
  65. extern char version[];
  66.  
  67. char *progname;
  68.  
  69. /*************************************************************************/
  70. /* FUNCTION  : parse_time                                                */
  71. /* PURPOSE   : Check a single valid-time-string against the current time */
  72. /*             and return whether or not a match occurs.                 */
  73. /* ARGUMENTS : a pointer to the time-string                              */
  74. /*************************************************************************/
  75.  
  76. int
  77. parsetime(char *whattime)
  78. {
  79.     static char *days[] =
  80.     {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Wk"};
  81.     time_t clock;
  82.     struct tm *curtime;
  83.     int wday,
  84.       start,
  85.       stop,
  86.       ltime,
  87.       validday,
  88.       loop,
  89.       match;
  90.  
  91.     (void) time(&clock);
  92.     curtime = localtime(&clock);
  93.     wday = curtime->tm_wday;
  94.     validday = 0;
  95.     match = 1;
  96.  
  97.     while (match && isalpha(*whattime) && isupper(*whattime)) {
  98.         match = 0;
  99.         for (loop = 0; loop < 8; loop++) {
  100.             if (strncmp(days[loop], whattime, 2) == NULL) {
  101.                 whattime += 2;
  102.                 match = 1;
  103.                 if ((wday == loop) | ((loop == 7) && wday && (wday < 6)))
  104.                     validday = 1;
  105.             }
  106.         }
  107.     }
  108.  
  109.     if (strncmp(whattime, "Any", 3) == NULL) {
  110.         validday = 1;
  111.         whattime += 3;
  112.     }
  113.     if (!validday)
  114.         return (0);
  115.  
  116.     if (sscanf(whattime, "%d-%d", &start, &stop) == 2) {
  117.         ltime = curtime->tm_min + 100 * curtime->tm_hour;
  118.         if ((start < stop) && ((ltime > start) && ltime < stop))
  119.             return (1);
  120.         if ((start > stop) && ((ltime > start) || ltime < stop))
  121.             return (1);
  122.     } else
  123.         return (1);
  124.  
  125.     return (0);
  126. }
  127.  
  128. /*************************************************************************/
  129. /* FUNCTION  : validtime                                                 */
  130. /* PURPOSE   : Break apart a set of valid time-strings and pass them to  */
  131. /*             parse_time, returning whether or not ANY matches occurred */
  132. /* ARGUMENTS : a pointer to the time-string                              */
  133. /*************************************************************************/
  134.  
  135. int
  136. validtime(char *ptr)
  137. {
  138.     char *nextptr;
  139.     int good;
  140.  
  141.     while (1) {
  142.         nextptr = strchr(ptr, '|');
  143.         if (strchr(ptr, '|') == NULL)
  144.             return (parsetime(ptr));
  145.         *nextptr = '\0';
  146.         good = parsetime(ptr);
  147.         *nextptr++ = '|';       /* gotta restore the | or things get skipped! */
  148.         if (good)
  149.             return (1);
  150.         ptr = nextptr;
  151.     }
  152. }
  153.  
  154. acl_getlimit(char *aclbuf, char *class)
  155. {
  156.     char *crptr,
  157.      *ptr,
  158.       linebuf[1024];
  159.     int limit;
  160.  
  161.     while (*aclbuf != NULL) {
  162.         if (strncasecmp(aclbuf, "limit", 5) == 0) {
  163.             for (crptr = aclbuf; *crptr++ != '\n';) ;
  164.             *--crptr = NULL;
  165.             strcpy(linebuf, aclbuf);
  166.             *crptr = '\n';
  167.             (void) strtok(linebuf, " \t");  /* returns "limit" */
  168.             if (strcmp(class, strtok(NULL, " \t")) == 0) {
  169.                 limit = atoi(strtok(NULL, " \t"));  /* returns limit <n> */
  170.                 if ((ptr = strtok(NULL, " \t")) && validtime(ptr))
  171.                     return (limit);
  172.             }
  173.         }
  174.         while (*aclbuf && *aclbuf++ != '\n') ;
  175.     }
  176.  
  177.     return (0);
  178.  
  179. }
  180.  
  181. acl_countusers(char *class)
  182. {
  183.     int pidfd,
  184.       count,
  185.       stat,
  186.       which;
  187.     char pidfile[1024];
  188.     char line[1024];
  189.     pid_t buf[MAXUSERS];
  190.     FILE *ZeFile;
  191. #if !defined(HAVE_FLOCK) && !defined(AMIGA)
  192. struct flock arg;
  193. #endif
  194.  
  195.     sprintf(pidfile, _PATH_PIDNAMES, class);
  196.     pidfd = open(pidfile, O_RDONLY, 0644);
  197.     if (pidfd == -1) {
  198.         return (0);
  199.     }
  200.     lseek(pidfd, 0, L_SET);
  201.  
  202.     count = 0;
  203.  
  204.     if (read(pidfd, buf, sizeof(buf)) == sizeof(buf)) {
  205.         for (which = 0; which < MAXUSERS; which++)
  206.         if (buf[which]) {
  207. #ifdef AMIGA
  208.             stat = 0;
  209. #else
  210.             stat = kill(buf[which], SIGCONT);
  211. #endif
  212.             if (((stat == -1) && (errno == EPERM)) || !stat) {
  213.                 if (strcmp(progname,"ftpcount")) {
  214. #if defined(SVR4)
  215.                     sprintf(line,"/bin/ps -l -p %d",buf[which]);
  216. #elif defined(M_UNIX)
  217.                     sprintf(line,"/bin/ps -f -p %d",buf[which]);
  218. #else
  219.                     sprintf(line,"/bin/ps %d",buf[which]);
  220. #endif
  221.                     ZeFile = popen(line, "r");
  222.                     rewind(ZeFile);
  223.                     fgets(line, 1024, ZeFile);
  224.                     fgets(line, 1024, ZeFile);
  225.                     printf("%s",line);
  226.                     pclose(ZeFile);
  227.                 }
  228.                 count++;
  229.             }
  230.         }
  231.     }
  232. #ifndef AMIGA
  233. #ifdef HAVE_FLOCK
  234.     flock(pidfd, LOCK_UN);
  235. #else
  236.     arg.l_type = F_UNLCK; arg.l_whence = arg.l_start = arg.l_len = 0;
  237.     fcntl(pidfd, F_SETLK, &arg);
  238. #endif
  239. #endif
  240.     close(pidfd);
  241.  
  242.     return (count);
  243. }
  244.  
  245. void
  246. new_list(struct c_list **list)
  247. {
  248.     (*list) = (struct c_list *) malloc(sizeof(struct c_list));
  249.  
  250.     (*list)->next = NULL;
  251. }
  252.  
  253. int
  254. add_list(char *class, struct c_list **list)
  255. {
  256.     struct c_list *cp;
  257.  
  258.     for (cp = (*list)->next; cp; cp = cp->next) {
  259.         if (!strcmp(cp->class, class))
  260.             return (-1);
  261.     }
  262.  
  263.     cp = (struct c_list *) malloc(sizeof(struct c_list));
  264.  
  265.     cp->class = (char *) malloc(strlen(class) + 1);
  266.     strcpy(cp->class, class);
  267.     cp->next = (*list)->next;
  268.     (*list)->next = cp;
  269.     return (1);
  270. }
  271.  
  272. main(int argc, char **argv)
  273. {
  274.     FILE *accessfile;
  275.     char class[80],
  276.       linebuf[1024],
  277.      *aclbuf,
  278.      *myaclbuf,
  279.      *crptr;
  280.     int limit;
  281.     struct stat finfo;
  282.     struct c_list *list;
  283.  
  284.     if (progname = strrchr(argv[0], '/'))  ++progname;
  285.     else  progname = argv[0];
  286.  
  287.     if (argc > 1) {
  288.         fprintf(stderr, "%s\n", version);
  289.         exit(0);
  290.     }
  291.  
  292.     if ((accessfile = fopen(_PATH_FTPACCESS, "r")) == NULL) {
  293.         if (errno != ENOENT)
  294.             perror("ftpcount: could not open() access file");
  295.         exit(1);
  296.     }
  297.     if (stat(_PATH_FTPACCESS, &finfo)) {
  298.         perror("ftpcount: could not stat() access file");
  299.         exit(1);
  300.     }
  301.     if (finfo.st_size == 0) {
  302.         printf("%s: no service classes defined, no usage count kept\n",progname);
  303.         exit(0);
  304.     } else {
  305.         if (!(aclbuf = (char *) malloc(finfo.st_size + 1))) {
  306.             perror("ftpcount: could not malloc aclbuf");
  307.             exit(1);
  308.         }
  309.         fread(aclbuf, finfo.st_size, 1, accessfile);
  310.         *(aclbuf + finfo.st_size) = '\0';
  311.     }
  312.  
  313.     (void) new_list(&list);
  314.     myaclbuf = aclbuf;
  315.     while (*myaclbuf != NULL) {
  316.         if (strncasecmp(myaclbuf, "class", 5) == 0) {
  317.             for (crptr = myaclbuf; *crptr++ != '\n';) ;
  318.             *--crptr = NULL;
  319.             strcpy(linebuf, myaclbuf);
  320.             *crptr = '\n';
  321.             (void) strtok(linebuf, " \t");  /* returns "class" */
  322.             strcpy(class, strtok(NULL, " \t")); /* returns class name */
  323.             if ((add_list(class, &list)) < 0) {
  324.                 /* we have a class with multiple "class..." lines so, only
  325.                  * display one count... */
  326.                 ;
  327.             } else {
  328.                 limit = acl_getlimit(myaclbuf, class);
  329.             if (strcmp(progname,"ftpcount")) {
  330.                 printf("Service class %s: \n", class);
  331.                 printf("   - %3d users (%3d maximum)\n\n",
  332.             acl_countusers(class), limit);
  333.         }
  334.         else
  335.                 printf("Service class %-20.20s - %3d users (%3d maximum)\n",
  336.                        class, acl_countusers(class), limit);
  337.             }
  338.         }
  339.         while (*myaclbuf && *myaclbuf++ != '\n') ;
  340.     }
  341. }
  342.