home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / tcp / Networking / TCP / Server / wu-ftpd / src / ftpshut.c < prev    next >
C/C++ Source or Header  |  1994-08-08  |  8KB  |  276 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. /* ftpshut 
  33.  * ======= 
  34.  * creates the ftpd shutdown file.
  35.  */
  36.  
  37. #include "config.h"
  38.  
  39. #include <errno.h>
  40. #include <pwd.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <time.h>
  44.  
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <sys/file.h>
  48. #include <sys/param.h>
  49.  
  50. #include "pathnames.h"
  51.  
  52. #define  WIDTH  70
  53.  
  54. int denyoffset = 10;            /* default deny time   */
  55. int discoffset = 5;             /* default disc time   */
  56. char *message = "System shutdown at %s";    /* default message     */
  57.  
  58. extern version[];
  59.  
  60. void
  61. massage(char *buf)
  62. {
  63.     char *sp = NULL;
  64.     char *ptr;
  65.     int i = 0;
  66.     int j = 0;
  67.  
  68.     ptr = buf;
  69.  
  70.     while (*ptr++ != '\0') {
  71.         ++i;
  72.  
  73.         /* if we have a space, keep track of where and at what "count" */
  74.  
  75.         if (*ptr == ' ') {
  76.             sp = ptr;
  77.             j = i;
  78.         }
  79.         /* magic cookies... */
  80.  
  81.         if (*ptr == '%') {
  82.             ++ptr;
  83.             switch (*ptr) {
  84.             case 'r':
  85.             case 's':
  86.             case 'd':
  87.             case 'T':
  88.                 i = i + 24;
  89.                 break;
  90.             case '\n':
  91.                 i = 0;
  92.                 break;
  93.             case 'C':
  94.             case 'R':
  95.             case 'L':
  96.             case 'U':
  97.                 i = i + 10;
  98.                 break;
  99.             case 'M':
  100.             case 'N':
  101.                 i = i + 3;
  102.                 break;
  103.             case '\0':
  104.                 return;
  105.                 break;
  106.             default:
  107.                 i = i + 1;
  108.                 break;
  109.             }
  110.         }
  111.         /* break up the long lines... */
  112.  
  113.         if ((i >= WIDTH) && (sp != NULL)) {
  114.             *sp = '\n';
  115.             sp = NULL;
  116.             i = i - j;
  117.         }
  118.     }
  119. }
  120.  
  121. int
  122. main(int argc, char **argv)
  123. {
  124.     time_t c_time;
  125.     struct tm *tp;
  126.  
  127.     char buf[BUFSIZ];
  128.  
  129.     int c;
  130.     extern int optind;
  131.     extern char *optarg;
  132.  
  133.     FILE *fp;
  134.     FILE *accessfile;
  135.     char *aclbuf,
  136.      *myaclbuf,
  137.      *crptr;
  138.     char *sp = NULL;
  139.     char linebuf[1024];
  140.     struct stat finfo;
  141.     int off = 0;
  142.  
  143.     while ((c = getopt(argc, argv, "vl:d:")) != EOF) {
  144.         switch (c) {
  145.         case 'l':
  146.             denyoffset = atoi(optarg);
  147.             break;
  148.         case 'd':
  149.             discoffset = atoi(optarg);
  150.             break;
  151.         case 'v':
  152.             fprintf(stderr, "%s\n", version);
  153.             exit(0);
  154.         default:
  155.             fprintf(stderr,
  156.                 "Usage: %s [-d min] [-l min] now [\"message\"]\n", argv[0]);
  157.             fprintf(stderr,
  158.                 "       %s [-d min] [-l min] +dd [\"message\"]\n", argv[0]);
  159.             fprintf(stderr,
  160.                "       %s [-d min] [-l min] HHMM [\"message\"]\n", argv[0]);
  161.             fprintf(stderr,
  162.                "       %s off\n", argv[0]);
  163.             exit(-1);
  164.         }
  165.     }
  166.  
  167.     if ((accessfile = fopen(_PATH_FTPACCESS, "r")) == NULL) {
  168.         if (errno != ENOENT)
  169.             perror("ftpshut: could not open() access file");
  170.         exit(1);
  171.     }
  172.     if (stat(_PATH_FTPACCESS, &finfo)) {
  173.         perror("ftpshut: could not stat() access file");
  174.         exit(1);
  175.     }
  176.     if (finfo.st_size == 0) {
  177.         printf("ftpshut: no service shutdown path defined\n");
  178.         exit(0);
  179.     } else {
  180.         if (!(aclbuf = (char *) malloc(finfo.st_size + 1))) {
  181.             perror("ftpcount: could not malloc aclbuf");
  182.             exit(1);
  183.         }
  184.         fread(aclbuf, finfo.st_size, 1, accessfile);
  185.         *(aclbuf + finfo.st_size) = '\0';
  186.     }
  187.  
  188.     myaclbuf = aclbuf;
  189.     while (*myaclbuf != NULL) {
  190.         if (strncasecmp(myaclbuf, "shutdown", 8) == 0) {
  191.             for (crptr = myaclbuf; *crptr++ != '\n';) ;
  192.             *--crptr = NULL;
  193.             strcpy(linebuf, myaclbuf);
  194.             *crptr = '\n';
  195.             (void) strtok(linebuf, " \t");  /* returns "shutdown" */
  196.             sp = strtok(NULL, " \t");   /* returns shutdown path */
  197.         }
  198.         while (*myaclbuf && *myaclbuf++ != '\n') ;
  199.     }
  200.  
  201.     /* three cases 
  202.      * -- now 
  203.      * -- +ddd 
  204.      * -- HHMM 
  205.      */
  206.  
  207.     c = -1;
  208.  
  209.     if (optind < argc) {
  210.         if (!strcasecmp(argv[optind], "off")) {
  211.             off = 1;
  212.         } else if (!strcasecmp(argv[optind], "now")) {
  213.             c_time = time(0);
  214.             tp = localtime(&c_time);
  215.         } else if ((*(argv[optind])) == '+') {
  216.             c_time = time(0);
  217.             c_time += 60 * atoi(++(argv[optind]));
  218.             tp = localtime(&c_time);
  219.         } else if ((c = atoi(argv[optind])) >= 0) {
  220.             c_time = time(0);
  221.             tp = localtime(&c_time);
  222.             tp->tm_hour = c / 100;
  223.             tp->tm_min = c % 100;
  224.  
  225.             if ((tp->tm_hour > 23) || (tp->tm_min > 59)) {
  226.                 fprintf(stderr, "Illegal time format.\n");
  227.                 return(1);
  228.             }
  229.         }
  230.     }
  231.     if (c_time <= 0) {
  232.         fprintf(stderr, "Usage: %s [-d min] [-l min] now [\"message\"]\n",
  233.                 argv[0]);
  234.         fprintf(stderr, "       %s [-d min] [-l min] +dd [\"message\"]\n",
  235.                 argv[0]);
  236.         fprintf(stderr, "       %s [-d min] [-l min] HHMM [\"message\"]\n",
  237.                 argv[0]);
  238.         fprintf(stderr, "       %s off\n",
  239.                 argv[0]);
  240.         return(1);
  241.     }
  242.     /* do we have a shutdown message? */
  243.  
  244.     if (++optind < argc)
  245.         strcpy(buf, argv[optind++]);
  246.     else
  247.         strcpy(buf, message);
  248.  
  249.     massage(buf);
  250.  
  251.     if ( sp == NULL ) {
  252.         fprintf(stderr, "No shutdown file defined in ftpaccess file.\n");
  253.         return(1);
  254.     }
  255.     
  256.     if (off) {
  257.         remove(sp);
  258.     } else {
  259.         if ( (fp = fopen(sp, "w")) == NULL )  {
  260.             perror("Couldn't open shutdown file");
  261.             return(1);
  262.         }
  263.  
  264.         fprintf(fp, "%.4d %.2d %.2d %.2d %.2d %.4d %.4d\n",
  265.                 (tp->tm_year) + 1900,
  266.                 tp->tm_mon,
  267.                 tp->tm_mday,
  268.                 tp->tm_hour,
  269.                 tp->tm_min,
  270.                 denyoffset,
  271.                 discoffset);
  272.         fprintf(fp, "%s\n", buf);
  273.         fclose(fp);
  274.     }
  275. }
  276.