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