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