File: ftpshut.c

package info (click to toggle)
proftpd-dfsg 1.3.4a-5%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 27,820 kB
  • sloc: perl: 154,169; ansic: 128,582; sh: 13,564; php: 11,586; makefile: 2,156
file content (184 lines) | stat: -rw-r--r-- 4,836 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * ProFTPD - FTP server daemon
 * Copyright (c) 1997, 1998 Public Flood Software
 * Copyright (c) 2001-2011 The ProFTPD Project team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
 * and other respective copyright holders give permission to link this program
 * with OpenSSL, and distribute the resulting executable, without including
 * the source code for OpenSSL in the source distribution.
 */

/* Simple utility to create the proftpd shutdown message file, allowing
 * an admin to configure the shutdown, deny, disconnect times and messages.
 *
 * $Id: ftpshut.c,v 1.9 2011/05/23 20:46:20 castaglia Exp $
 */

#include "conf.h"

static void show_usage(char *progname) {
  printf("usage: %s [ -R ] [ -l min ] [ -d min ] time "
    " [ warning-message ... ]\n", progname);

  exit(1);
}

static int isnumeric(char *str) {
  while (str && isspace((int) *str))
    str++;

  if (!str || !*str)
    return 0;

  for (; str && *str; str++) {
    if (!isdigit((int) *str))
      return 0;
  }

  return 1;
}

int main(int argc, char *argv[]) {
  int deny = 10,disc = 5,c;
  FILE *outf;
  char *shut,*msg,*progname = argv[0];
  time_t now;
  struct tm *tm;
  int mn = 0,hr = 0;

  opterr = 0;

  while ((c = getopt(argc, argv, "Rl:d:")) != -1) {
    switch (c) {
      case 'R':
        if (unlink(PR_SHUTMSG_PATH) < 0) {
          fprintf(stderr, "%s: error removing '" PR_SHUTMSG_PATH "': %s\n",
            progname, strerror(errno));
          exit(1);
        }
        fprintf(stdout, "%s: " PR_SHUTMSG_PATH " removed\n", progname);
        exit(0);

      case 'l':
      case 'd':
        if (!optarg) {
          fprintf(stderr, "%s: -%c requires an argument\n", progname, c);
          show_usage(progname);
        }

        if (!isnumeric(optarg)) {
	  fprintf(stderr, "%s: -%c requires a numeric argument\n", progname, c);
	  show_usage(progname);
        }

        if (c == 'd')
	  disc = atoi(optarg);

        else if (c == 'l')
	  deny = atoi(optarg);

        break;

      case '?':
        fprintf(stderr, "%s: unknown option '%c'\n", progname, (char)optopt);

      case 'h':
      default:
        show_usage(progname);
    }
  }

  /* Everything left on the command line is the message */
  if (optind >= argc)
    show_usage(progname);

  shut = argv[optind++];

  if(optind < argc)
    msg = argv[optind];
  else
    msg = "going down at %s";

  time(&now);
  tm = localtime(&now);

  /* shut must be either 'now', '+number' or 'HHMM' */
  if(strcasecmp(shut,"now") != 0) {
    if(*shut == '+') {
      shut++;
      while (shut && *shut && isspace((int) *shut)) shut++;

      if(!isnumeric(shut)) {
	fprintf(stderr, "%s: Invalid time interval specified.\n", progname);
	show_usage(progname);
      }

      now += (60 * atoi(shut));
      tm = localtime(&now);
    } else {
      if((strlen(shut) != 4 && strlen(shut) != 2) || !isnumeric(shut)) {
	fprintf(stderr, "%s: Invalid time interval specified.\n", progname);
	show_usage(progname);
      }

      if(strlen(shut) > 2) {
        mn = atoi((shut + strlen(shut) - 2));

	if(mn > 59) {
	  fprintf(stderr, "%s: Invalid time interval specified.\n",
		  progname);
	  show_usage(progname);
	}

        *(shut + strlen(shut) - 2) = '\0';
      }

      hr = atoi(shut);

      if(hr > 23) {
	fprintf(stderr, "%s: Invalid time interval specified.\n",
		progname);
	show_usage(progname);
      }

      if(hr < tm->tm_hour || (hr == tm->tm_hour && mn <= tm->tm_min)) {
        now += 86400;		/* one day forward */
        tm = localtime(&now);
      }
      tm->tm_hour = hr;
      tm->tm_min = mn;
    }
  }

  umask(022);
  if ((outf = fopen(PR_SHUTMSG_PATH, "w")) == NULL) {
    fprintf(stderr,"%s: error opening '" PR_SHUTMSG_PATH "': %s\n", progname,
      strerror(errno));
    exit(1);
  }

  fprintf(outf,"%d %d %d %d %d %d",
          tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
          tm->tm_min, tm->tm_sec);
  fprintf(outf," %02d%02d %02d%02d\n",
          (deny / 60),(deny % 60),
          (disc / 60),(disc % 60));
  fprintf(outf,"%s\n",msg);
  fclose(outf);
  return 0;
}