conf.c revision 1.47 1 1.47 enami /* $NetBSD: conf.c,v 1.47 2002/05/30 00:24:47 enami Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.41 lukem * Copyright (c) 1997-2001 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Simon Burge and Luke Mewburn.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the NetBSD
21 1.1 lukem * Foundation, Inc. and its contributors.
22 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 lukem * contributors may be used to endorse or promote products derived
24 1.1 lukem * from this software without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1 lukem */
38 1.1 lukem
39 1.2 christos #include <sys/cdefs.h>
40 1.1 lukem #ifndef lint
41 1.47 enami __RCSID("$NetBSD: conf.c,v 1.47 2002/05/30 00:24:47 enami Exp $");
42 1.1 lukem #endif /* not lint */
43 1.1 lukem
44 1.1 lukem #include <sys/types.h>
45 1.1 lukem #include <sys/param.h>
46 1.37 lukem #include <sys/socket.h>
47 1.1 lukem #include <sys/stat.h>
48 1.1 lukem
49 1.24 lukem #include <ctype.h>
50 1.1 lukem #include <errno.h>
51 1.25 lukem #include <fcntl.h>
52 1.1 lukem #include <glob.h>
53 1.37 lukem #include <netdb.h>
54 1.28 lukem #include <setjmp.h>
55 1.25 lukem #include <signal.h>
56 1.1 lukem #include <stdio.h>
57 1.2 christos #include <stdlib.h>
58 1.1 lukem #include <string.h>
59 1.1 lukem #include <stringlist.h>
60 1.1 lukem #include <syslog.h>
61 1.23 lukem #include <time.h>
62 1.23 lukem #include <unistd.h>
63 1.23 lukem #include <util.h>
64 1.18 explorer
65 1.18 explorer #ifdef KERBEROS5
66 1.21 christos #include <krb5/krb5.h>
67 1.18 explorer #endif
68 1.1 lukem
69 1.1 lukem #include "extern.h"
70 1.1 lukem #include "pathnames.h"
71 1.1 lukem
72 1.30 lukem static char *strend(const char *, char *);
73 1.30 lukem static int filetypematch(char *, int);
74 1.15 lukem
75 1.1 lukem
76 1.37 lukem /* class defaults */
77 1.37 lukem #define DEFAULT_LIMIT -1 /* unlimited connections */
78 1.37 lukem #define DEFAULT_MAXFILESIZE -1 /* unlimited file size */
79 1.37 lukem #define DEFAULT_MAXTIMEOUT 7200 /* 2 hours */
80 1.37 lukem #define DEFAULT_TIMEOUT 900 /* 15 minutes */
81 1.37 lukem #define DEFAULT_UMASK 027 /* 15 minutes */
82 1.37 lukem
83 1.1 lukem /*
84 1.25 lukem * Initialise curclass to an `empty' state
85 1.1 lukem */
86 1.1 lukem void
87 1.30 lukem init_curclass(void)
88 1.1 lukem {
89 1.1 lukem struct ftpconv *conv, *cnext;
90 1.1 lukem
91 1.23 lukem for (conv = curclass.conversions; conv != NULL; conv = cnext) {
92 1.1 lukem REASSIGN(conv->suffix, NULL);
93 1.1 lukem REASSIGN(conv->types, NULL);
94 1.1 lukem REASSIGN(conv->disable, NULL);
95 1.1 lukem REASSIGN(conv->command, NULL);
96 1.1 lukem cnext = conv->next;
97 1.1 lukem free(conv);
98 1.1 lukem }
99 1.28 lukem
100 1.37 lukem memset((char *)&curclass.advertise, 0, sizeof(curclass.advertise));
101 1.37 lukem curclass.advertise.su_len = 0; /* `not used' */
102 1.33 lukem REASSIGN(curclass.chroot, NULL);
103 1.25 lukem REASSIGN(curclass.classname, NULL);
104 1.1 lukem curclass.conversions = NULL;
105 1.1 lukem REASSIGN(curclass.display, NULL);
106 1.33 lukem REASSIGN(curclass.homedir, NULL);
107 1.37 lukem curclass.limit = DEFAULT_LIMIT;
108 1.25 lukem REASSIGN(curclass.limitfile, NULL);
109 1.37 lukem curclass.maxfilesize = DEFAULT_MAXFILESIZE;
110 1.24 lukem curclass.maxrateget = 0;
111 1.24 lukem curclass.maxrateput = 0;
112 1.37 lukem curclass.maxtimeout = DEFAULT_MAXTIMEOUT;
113 1.24 lukem REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
114 1.1 lukem REASSIGN(curclass.notify, NULL);
115 1.28 lukem curclass.portmin = 0;
116 1.28 lukem curclass.portmax = 0;
117 1.24 lukem curclass.rateget = 0;
118 1.24 lukem curclass.rateput = 0;
119 1.37 lukem curclass.timeout = DEFAULT_TIMEOUT;
120 1.33 lukem /* curclass.type is set elsewhere */
121 1.37 lukem curclass.umask = DEFAULT_UMASK;
122 1.47 enami curclass.mmapsize = 0;
123 1.47 enami curclass.readsize = 0;
124 1.47 enami curclass.writesize = 0;
125 1.47 enami curclass.sendbufsize = 0;
126 1.47 enami curclass.sendlowat = 0;
127 1.36 lukem
128 1.36 lukem CURCLASS_FLAGS_SET(checkportcmd);
129 1.46 lukem CURCLASS_FLAGS_CLR(denyquick);
130 1.36 lukem CURCLASS_FLAGS_SET(modify);
131 1.36 lukem CURCLASS_FLAGS_SET(passive);
132 1.46 lukem CURCLASS_FLAGS_CLR(private);
133 1.36 lukem CURCLASS_FLAGS_CLR(sanenames);
134 1.36 lukem CURCLASS_FLAGS_SET(upload);
135 1.25 lukem }
136 1.25 lukem
137 1.25 lukem /*
138 1.25 lukem * Parse the configuration file, looking for the named class, and
139 1.25 lukem * define curclass to contain the appropriate settings.
140 1.25 lukem */
141 1.25 lukem void
142 1.30 lukem parse_conf(const char *findclass)
143 1.25 lukem {
144 1.25 lukem FILE *f;
145 1.25 lukem char *buf, *p;
146 1.25 lukem size_t len;
147 1.36 lukem LLT llval;
148 1.36 lukem int none, match;
149 1.25 lukem char *endp;
150 1.26 lukem char *class, *word, *arg, *template;
151 1.25 lukem const char *infile;
152 1.25 lukem size_t line;
153 1.25 lukem unsigned int timeout;
154 1.25 lukem struct ftpconv *conv, *cnext;
155 1.25 lukem
156 1.25 lukem init_curclass();
157 1.25 lukem REASSIGN(curclass.classname, xstrdup(findclass));
158 1.37 lukem /* set more guest defaults */
159 1.1 lukem if (strcasecmp(findclass, "guest") == 0) {
160 1.36 lukem CURCLASS_FLAGS_CLR(modify);
161 1.1 lukem curclass.umask = 0707;
162 1.1 lukem }
163 1.1 lukem
164 1.6 lukem infile = conffilename(_PATH_FTPDCONF);
165 1.1 lukem if ((f = fopen(infile, "r")) == NULL)
166 1.1 lukem return;
167 1.1 lukem
168 1.1 lukem line = 0;
169 1.26 lukem template = NULL;
170 1.23 lukem for (;
171 1.23 lukem (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
172 1.23 lukem FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
173 1.23 lukem free(buf)) {
174 1.1 lukem none = match = 0;
175 1.23 lukem p = buf;
176 1.5 lukem if (len < 1)
177 1.5 lukem continue;
178 1.23 lukem if (p[len - 1] == '\n')
179 1.23 lukem p[--len] = '\0';
180 1.23 lukem if (EMPTYSTR(p))
181 1.1 lukem continue;
182 1.1 lukem
183 1.23 lukem NEXTWORD(p, word);
184 1.23 lukem NEXTWORD(p, class);
185 1.23 lukem NEXTWORD(p, arg);
186 1.1 lukem if (EMPTYSTR(word) || EMPTYSTR(class))
187 1.1 lukem continue;
188 1.1 lukem if (strcasecmp(class, "none") == 0)
189 1.1 lukem none = 1;
190 1.27 lukem if (! (strcasecmp(class, findclass) == 0 ||
191 1.27 lukem (template != NULL && strcasecmp(class, template) == 0) ||
192 1.27 lukem none ||
193 1.27 lukem strcasecmp(class, "all") == 0) )
194 1.1 lukem continue;
195 1.1 lukem
196 1.36 lukem #define CONF_FLAG(x) \
197 1.36 lukem do { \
198 1.36 lukem if (none || \
199 1.36 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) \
200 1.36 lukem CURCLASS_FLAGS_CLR(x); \
201 1.36 lukem else \
202 1.36 lukem CURCLASS_FLAGS_SET(x); \
203 1.36 lukem } while (0)
204 1.36 lukem
205 1.36 lukem #define CONF_STRING(x) \
206 1.36 lukem do { \
207 1.36 lukem if (none || EMPTYSTR(arg)) \
208 1.36 lukem arg = NULL; \
209 1.36 lukem else \
210 1.36 lukem arg = xstrdup(arg); \
211 1.36 lukem REASSIGN(curclass.x, arg); \
212 1.36 lukem } while (0)
213 1.36 lukem
214 1.37 lukem
215 1.37 lukem if (0) {
216 1.37 lukem /* no-op */
217 1.37 lukem
218 1.43 lukem } else if ((strcasecmp(word, "advertise") == 0)
219 1.43 lukem || (strcasecmp(word, "advertize") == 0)) {
220 1.37 lukem struct addrinfo hints, *res;
221 1.37 lukem int error;
222 1.37 lukem
223 1.37 lukem memset((char *)&curclass.advertise, 0,
224 1.37 lukem sizeof(curclass.advertise));
225 1.37 lukem curclass.advertise.su_len = 0;
226 1.37 lukem if (none || EMPTYSTR(arg))
227 1.37 lukem continue;
228 1.37 lukem res = NULL;
229 1.37 lukem memset(&hints, 0, sizeof(hints));
230 1.37 lukem /*
231 1.37 lukem * only get addresses of the family
232 1.37 lukem * that we're listening on
233 1.37 lukem */
234 1.37 lukem hints.ai_family = ctrl_addr.su_family;
235 1.37 lukem hints.ai_socktype = SOCK_STREAM;
236 1.37 lukem error = getaddrinfo(arg, "0", &hints, &res);
237 1.37 lukem if (error) {
238 1.37 lukem syslog(LOG_WARNING, "%s line %d: %s",
239 1.37 lukem infile, (int)line, gai_strerror(error));
240 1.37 lukem advertiseparsefail:
241 1.37 lukem if (res)
242 1.37 lukem freeaddrinfo(res);
243 1.37 lukem continue;
244 1.37 lukem }
245 1.37 lukem if (res->ai_next) {
246 1.37 lukem syslog(LOG_WARNING,
247 1.37 lukem "%s line %d: multiple addresses returned for `%s'; please be more specific",
248 1.37 lukem infile, (int)line, arg);
249 1.37 lukem goto advertiseparsefail;
250 1.37 lukem }
251 1.37 lukem if (sizeof(curclass.advertise) < res->ai_addrlen || (
252 1.37 lukem #ifdef INET6
253 1.37 lukem res->ai_family != AF_INET6 &&
254 1.37 lukem #endif
255 1.37 lukem res->ai_family != AF_INET)) {
256 1.37 lukem syslog(LOG_WARNING,
257 1.37 lukem "%s line %d: unsupported protocol %d for `%s'",
258 1.37 lukem infile, (int)line, res->ai_family, arg);
259 1.37 lukem goto advertiseparsefail;
260 1.37 lukem }
261 1.37 lukem memcpy(&curclass.advertise, res->ai_addr,
262 1.37 lukem res->ai_addrlen);
263 1.37 lukem curclass.advertise.su_len = res->ai_addrlen;
264 1.37 lukem freeaddrinfo(res);
265 1.37 lukem
266 1.37 lukem } else if (strcasecmp(word, "checkportcmd") == 0) {
267 1.36 lukem CONF_FLAG(checkportcmd);
268 1.23 lukem
269 1.33 lukem } else if (strcasecmp(word, "chroot") == 0) {
270 1.36 lukem CONF_STRING(chroot);
271 1.33 lukem
272 1.24 lukem } else if (strcasecmp(word, "classtype") == 0) {
273 1.24 lukem if (!none && !EMPTYSTR(arg)) {
274 1.24 lukem if (strcasecmp(arg, "GUEST") == 0)
275 1.24 lukem curclass.type = CLASS_GUEST;
276 1.24 lukem else if (strcasecmp(arg, "CHROOT") == 0)
277 1.24 lukem curclass.type = CLASS_CHROOT;
278 1.24 lukem else if (strcasecmp(arg, "REAL") == 0)
279 1.24 lukem curclass.type = CLASS_REAL;
280 1.24 lukem else {
281 1.24 lukem syslog(LOG_WARNING,
282 1.24 lukem "%s line %d: unknown class type `%s'",
283 1.24 lukem infile, (int)line, arg);
284 1.24 lukem continue;
285 1.24 lukem }
286 1.24 lukem }
287 1.24 lukem
288 1.9 lukem } else if (strcasecmp(word, "conversion") == 0) {
289 1.5 lukem char *suffix, *types, *disable, *convcmd;
290 1.5 lukem
291 1.1 lukem if (EMPTYSTR(arg)) {
292 1.1 lukem syslog(LOG_WARNING,
293 1.1 lukem "%s line %d: %s requires a suffix",
294 1.23 lukem infile, (int)line, word);
295 1.1 lukem continue; /* need a suffix */
296 1.1 lukem }
297 1.23 lukem NEXTWORD(p, types);
298 1.23 lukem NEXTWORD(p, disable);
299 1.23 lukem convcmd = p;
300 1.1 lukem if (convcmd)
301 1.1 lukem convcmd += strspn(convcmd, " \t");
302 1.23 lukem suffix = xstrdup(arg);
303 1.1 lukem if (none || EMPTYSTR(types) ||
304 1.1 lukem EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
305 1.1 lukem types = NULL;
306 1.1 lukem disable = NULL;
307 1.1 lukem convcmd = NULL;
308 1.1 lukem } else {
309 1.23 lukem types = xstrdup(types);
310 1.23 lukem disable = xstrdup(disable);
311 1.23 lukem convcmd = xstrdup(convcmd);
312 1.1 lukem }
313 1.1 lukem for (conv = curclass.conversions; conv != NULL;
314 1.1 lukem conv = conv->next) {
315 1.5 lukem if (strcmp(conv->suffix, suffix) == 0)
316 1.1 lukem break;
317 1.1 lukem }
318 1.1 lukem if (conv == NULL) {
319 1.1 lukem conv = (struct ftpconv *)
320 1.1 lukem calloc(1, sizeof(struct ftpconv));
321 1.1 lukem if (conv == NULL) {
322 1.1 lukem syslog(LOG_WARNING, "can't malloc");
323 1.1 lukem continue;
324 1.1 lukem }
325 1.23 lukem conv->next = NULL;
326 1.23 lukem for (cnext = curclass.conversions;
327 1.23 lukem cnext != NULL; cnext = cnext->next)
328 1.23 lukem if (cnext->next == NULL)
329 1.23 lukem break;
330 1.23 lukem if (cnext != NULL)
331 1.23 lukem cnext->next = conv;
332 1.23 lukem else
333 1.23 lukem curclass.conversions = conv;
334 1.1 lukem }
335 1.5 lukem REASSIGN(conv->suffix, suffix);
336 1.1 lukem REASSIGN(conv->types, types);
337 1.1 lukem REASSIGN(conv->disable, disable);
338 1.1 lukem REASSIGN(conv->command, convcmd);
339 1.23 lukem
340 1.46 lukem } else if (strcasecmp(word, "denyquick") == 0) {
341 1.46 lukem CONF_FLAG(denyquick);
342 1.46 lukem
343 1.1 lukem } else if (strcasecmp(word, "display") == 0) {
344 1.36 lukem CONF_STRING(display);
345 1.23 lukem
346 1.33 lukem } else if (strcasecmp(word, "homedir") == 0) {
347 1.36 lukem CONF_STRING(homedir);
348 1.36 lukem
349 1.25 lukem } else if (strcasecmp(word, "limit") == 0) {
350 1.25 lukem int limit;
351 1.25 lukem
352 1.37 lukem curclass.limit = DEFAULT_LIMIT;
353 1.37 lukem REASSIGN(curclass.limitfile, NULL);
354 1.25 lukem if (none || EMPTYSTR(arg))
355 1.25 lukem continue;
356 1.25 lukem limit = (int)strtol(arg, &endp, 10);
357 1.25 lukem if (*endp != 0) {
358 1.25 lukem syslog(LOG_WARNING,
359 1.25 lukem "%s line %d: invalid limit %s",
360 1.25 lukem infile, (int)line, arg);
361 1.25 lukem continue;
362 1.25 lukem }
363 1.25 lukem curclass.limit = limit;
364 1.26 lukem REASSIGN(curclass.limitfile,
365 1.26 lukem EMPTYSTR(p) ? NULL : xstrdup(p));
366 1.25 lukem
367 1.37 lukem } else if (strcasecmp(word, "maxfilesize") == 0) {
368 1.37 lukem curclass.maxfilesize = DEFAULT_MAXFILESIZE;
369 1.37 lukem if (none || EMPTYSTR(arg))
370 1.37 lukem continue;
371 1.37 lukem llval = strsuftoll(arg);
372 1.37 lukem if (llval == -1) {
373 1.37 lukem syslog(LOG_WARNING,
374 1.37 lukem "%s line %d: invalid maxfilesize %s",
375 1.37 lukem infile, (int)line, arg);
376 1.37 lukem continue;
377 1.37 lukem }
378 1.37 lukem curclass.maxfilesize = llval;
379 1.37 lukem
380 1.1 lukem } else if (strcasecmp(word, "maxtimeout") == 0) {
381 1.37 lukem curclass.maxtimeout = DEFAULT_MAXTIMEOUT;
382 1.1 lukem if (none || EMPTYSTR(arg))
383 1.1 lukem continue;
384 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
385 1.1 lukem if (*endp != 0) {
386 1.1 lukem syslog(LOG_WARNING,
387 1.1 lukem "%s line %d: invalid maxtimeout %s",
388 1.23 lukem infile, (int)line, arg);
389 1.1 lukem continue;
390 1.1 lukem }
391 1.1 lukem if (timeout < 30) {
392 1.1 lukem syslog(LOG_WARNING,
393 1.1 lukem "%s line %d: maxtimeout %d < 30 seconds",
394 1.23 lukem infile, (int)line, timeout);
395 1.1 lukem continue;
396 1.1 lukem }
397 1.1 lukem if (timeout < curclass.timeout) {
398 1.1 lukem syslog(LOG_WARNING,
399 1.1 lukem "%s line %d: maxtimeout %d < timeout (%d)",
400 1.23 lukem infile, (int)line, timeout,
401 1.23 lukem curclass.timeout);
402 1.1 lukem continue;
403 1.1 lukem }
404 1.1 lukem curclass.maxtimeout = timeout;
405 1.47 enami
406 1.47 enami } else if (strcasecmp(word, "mmapsize") == 0) {
407 1.47 enami curclass.mmapsize = 0;
408 1.47 enami if (none || EMPTYSTR(arg))
409 1.47 enami continue;
410 1.47 enami llval = strsuftoll(arg);
411 1.47 enami if (llval == -1) {
412 1.47 enami syslog(LOG_WARNING,
413 1.47 enami "%s line %d: invalid mmapsize %s",
414 1.47 enami infile, (int)line, arg);
415 1.47 enami continue;
416 1.47 enami }
417 1.47 enami curclass.mmapsize = llval;
418 1.47 enami
419 1.47 enami } else if (strcasecmp(word, "readsize") == 0) {
420 1.47 enami curclass.readsize = 0;
421 1.47 enami if (none || EMPTYSTR(arg))
422 1.47 enami continue;
423 1.47 enami llval = strsuftoll(arg);
424 1.47 enami if (llval == -1) {
425 1.47 enami syslog(LOG_WARNING,
426 1.47 enami "%s line %d: invalid readsize %s",
427 1.47 enami infile, (int)line, arg);
428 1.47 enami continue;
429 1.47 enami }
430 1.47 enami curclass.readsize = llval;
431 1.47 enami
432 1.47 enami } else if (strcasecmp(word, "writesize") == 0) {
433 1.47 enami curclass.writesize = 0;
434 1.47 enami if (none || EMPTYSTR(arg))
435 1.47 enami continue;
436 1.47 enami llval = strsuftoll(arg);
437 1.47 enami if (llval == -1) {
438 1.47 enami syslog(LOG_WARNING,
439 1.47 enami "%s line %d: invalid writesize %s",
440 1.47 enami infile, (int)line, arg);
441 1.47 enami continue;
442 1.47 enami }
443 1.47 enami curclass.writesize = llval;
444 1.47 enami
445 1.47 enami } else if (strcasecmp(word, "sendbufsize") == 0) {
446 1.47 enami curclass.sendbufsize = 0;
447 1.47 enami if (none || EMPTYSTR(arg))
448 1.47 enami continue;
449 1.47 enami llval = strsuftoll(arg);
450 1.47 enami if (llval == -1) {
451 1.47 enami syslog(LOG_WARNING,
452 1.47 enami "%s line %d: invalid sendbufsize %s",
453 1.47 enami infile, (int)line, arg);
454 1.47 enami continue;
455 1.47 enami }
456 1.47 enami curclass.sendbufsize = llval;
457 1.47 enami
458 1.47 enami } else if (strcasecmp(word, "sendlowat") == 0) {
459 1.47 enami curclass.sendlowat = 0;
460 1.47 enami if (none || EMPTYSTR(arg))
461 1.47 enami continue;
462 1.47 enami llval = strsuftoll(arg);
463 1.47 enami if (llval == -1) {
464 1.47 enami syslog(LOG_WARNING,
465 1.47 enami "%s line %d: invalid sendlowat %s",
466 1.47 enami infile, (int)line, arg);
467 1.47 enami continue;
468 1.47 enami }
469 1.47 enami curclass.sendlowat = llval;
470 1.23 lukem
471 1.1 lukem } else if (strcasecmp(word, "modify") == 0) {
472 1.36 lukem CONF_FLAG(modify);
473 1.23 lukem
474 1.24 lukem } else if (strcasecmp(word, "motd") == 0) {
475 1.36 lukem CONF_STRING(motd);
476 1.24 lukem
477 1.1 lukem } else if (strcasecmp(word, "notify") == 0) {
478 1.36 lukem CONF_STRING(notify);
479 1.23 lukem
480 1.14 tv } else if (strcasecmp(word, "passive") == 0) {
481 1.36 lukem CONF_FLAG(passive);
482 1.28 lukem
483 1.28 lukem } else if (strcasecmp(word, "portrange") == 0) {
484 1.28 lukem int minport, maxport;
485 1.28 lukem char *min, *max;
486 1.28 lukem
487 1.37 lukem curclass.portmin = 0;
488 1.37 lukem curclass.portmax = 0;
489 1.37 lukem if (none || EMPTYSTR(arg))
490 1.28 lukem continue;
491 1.28 lukem min = arg;
492 1.28 lukem NEXTWORD(p, max);
493 1.28 lukem if (EMPTYSTR(max)) {
494 1.28 lukem syslog(LOG_WARNING,
495 1.28 lukem "%s line %d: missing maxport argument",
496 1.28 lukem infile, (int)line);
497 1.28 lukem continue;
498 1.28 lukem }
499 1.28 lukem minport = (int)strtol(min, &endp, 10);
500 1.28 lukem if (*endp != 0 || minport < IPPORT_RESERVED ||
501 1.28 lukem minport > IPPORT_ANONMAX) {
502 1.28 lukem syslog(LOG_WARNING,
503 1.28 lukem "%s line %d: invalid minport %s",
504 1.28 lukem infile, (int)line, min);
505 1.28 lukem continue;
506 1.28 lukem }
507 1.28 lukem maxport = (int)strtol(max, &endp, 10);
508 1.28 lukem if (*endp != 0 || maxport < IPPORT_RESERVED ||
509 1.28 lukem maxport > IPPORT_ANONMAX) {
510 1.28 lukem syslog(LOG_WARNING,
511 1.28 lukem "%s line %d: invalid maxport %s",
512 1.28 lukem infile, (int)line, max);
513 1.28 lukem continue;
514 1.28 lukem }
515 1.28 lukem if (minport >= maxport) {
516 1.28 lukem syslog(LOG_WARNING,
517 1.28 lukem "%s line %d: minport %d >= maxport %d",
518 1.28 lukem infile, (int)line, minport, maxport);
519 1.28 lukem continue;
520 1.28 lukem }
521 1.28 lukem curclass.portmin = minport;
522 1.28 lukem curclass.portmax = maxport;
523 1.46 lukem
524 1.46 lukem } else if (strcasecmp(word, "private") == 0) {
525 1.46 lukem CONF_FLAG(private);
526 1.23 lukem
527 1.24 lukem } else if (strcasecmp(word, "rateget") == 0) {
528 1.37 lukem curclass.maxrateget = 0;
529 1.37 lukem curclass.rateget = 0;
530 1.24 lukem if (none || EMPTYSTR(arg))
531 1.24 lukem continue;
532 1.36 lukem llval = strsuftoll(arg);
533 1.36 lukem if (llval == -1) {
534 1.24 lukem syslog(LOG_WARNING,
535 1.24 lukem "%s line %d: invalid rateget %s",
536 1.24 lukem infile, (int)line, arg);
537 1.24 lukem continue;
538 1.24 lukem }
539 1.36 lukem curclass.maxrateget = llval;
540 1.36 lukem curclass.rateget = llval;
541 1.24 lukem
542 1.24 lukem } else if (strcasecmp(word, "rateput") == 0) {
543 1.37 lukem curclass.maxrateput = 0;
544 1.37 lukem curclass.rateput = 0;
545 1.24 lukem if (none || EMPTYSTR(arg))
546 1.24 lukem continue;
547 1.36 lukem llval = strsuftoll(arg);
548 1.36 lukem if (llval == -1) {
549 1.24 lukem syslog(LOG_WARNING,
550 1.24 lukem "%s line %d: invalid rateput %s",
551 1.24 lukem infile, (int)line, arg);
552 1.24 lukem continue;
553 1.24 lukem }
554 1.36 lukem curclass.maxrateput = llval;
555 1.36 lukem curclass.rateput = llval;
556 1.36 lukem
557 1.36 lukem } else if (strcasecmp(word, "sanenames") == 0) {
558 1.36 lukem CONF_FLAG(sanenames);
559 1.24 lukem
560 1.1 lukem } else if (strcasecmp(word, "timeout") == 0) {
561 1.37 lukem curclass.timeout = DEFAULT_TIMEOUT;
562 1.1 lukem if (none || EMPTYSTR(arg))
563 1.1 lukem continue;
564 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
565 1.1 lukem if (*endp != 0) {
566 1.1 lukem syslog(LOG_WARNING,
567 1.1 lukem "%s line %d: invalid timeout %s",
568 1.23 lukem infile, (int)line, arg);
569 1.1 lukem continue;
570 1.1 lukem }
571 1.1 lukem if (timeout < 30) {
572 1.1 lukem syslog(LOG_WARNING,
573 1.1 lukem "%s line %d: timeout %d < 30 seconds",
574 1.23 lukem infile, (int)line, timeout);
575 1.1 lukem continue;
576 1.1 lukem }
577 1.1 lukem if (timeout > curclass.maxtimeout) {
578 1.1 lukem syslog(LOG_WARNING,
579 1.1 lukem "%s line %d: timeout %d > maxtimeout (%d)",
580 1.23 lukem infile, (int)line, timeout,
581 1.23 lukem curclass.maxtimeout);
582 1.1 lukem continue;
583 1.1 lukem }
584 1.1 lukem curclass.timeout = timeout;
585 1.23 lukem
586 1.26 lukem } else if (strcasecmp(word, "template") == 0) {
587 1.26 lukem if (none)
588 1.26 lukem continue;
589 1.26 lukem REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
590 1.26 lukem
591 1.1 lukem } else if (strcasecmp(word, "umask") == 0) {
592 1.45 lukem mode_t fumask;
593 1.1 lukem
594 1.37 lukem curclass.umask = DEFAULT_UMASK;
595 1.1 lukem if (none || EMPTYSTR(arg))
596 1.1 lukem continue;
597 1.45 lukem fumask = (mode_t)strtoul(arg, &endp, 8);
598 1.45 lukem if (*endp != 0 || fumask > 0777) {
599 1.1 lukem syslog(LOG_WARNING,
600 1.1 lukem "%s line %d: invalid umask %s",
601 1.23 lukem infile, (int)line, arg);
602 1.1 lukem continue;
603 1.1 lukem }
604 1.45 lukem curclass.umask = fumask;
605 1.23 lukem
606 1.24 lukem } else if (strcasecmp(word, "upload") == 0) {
607 1.36 lukem CONF_FLAG(upload);
608 1.36 lukem if (! CURCLASS_FLAGS_ISSET(upload))
609 1.36 lukem CURCLASS_FLAGS_CLR(modify);
610 1.24 lukem
611 1.1 lukem } else {
612 1.1 lukem syslog(LOG_WARNING,
613 1.1 lukem "%s line %d: unknown directive '%s'",
614 1.23 lukem infile, (int)line, word);
615 1.1 lukem continue;
616 1.1 lukem }
617 1.1 lukem }
618 1.26 lukem REASSIGN(template, NULL);
619 1.1 lukem fclose(f);
620 1.1 lukem }
621 1.1 lukem
622 1.1 lukem /*
623 1.1 lukem * Show file listed in curclass.display first time in, and list all the
624 1.37 lukem * files named in curclass.notify in the current directory.
625 1.37 lukem * Send back responses with the prefix `code' + "-".
626 1.37 lukem * If code == -1, flush the internal cache of directory names and return.
627 1.1 lukem */
628 1.1 lukem void
629 1.30 lukem show_chdir_messages(int code)
630 1.1 lukem {
631 1.1 lukem static StringList *slist = NULL;
632 1.1 lukem
633 1.1 lukem struct stat st;
634 1.1 lukem struct tm *t;
635 1.1 lukem glob_t gl;
636 1.1 lukem time_t now, then;
637 1.1 lukem int age;
638 1.45 lukem char curwd[MAXPATHLEN];
639 1.1 lukem char *cp, **rlist;
640 1.1 lukem
641 1.37 lukem if (code == -1) {
642 1.37 lukem if (slist != NULL)
643 1.37 lukem sl_free(slist, 1);
644 1.37 lukem slist = NULL;
645 1.37 lukem return;
646 1.37 lukem }
647 1.37 lukem
648 1.29 lukem if (quietmessages)
649 1.29 lukem return;
650 1.29 lukem
651 1.1 lukem /* Setup list for directory cache */
652 1.1 lukem if (slist == NULL)
653 1.1 lukem slist = sl_init();
654 1.22 lukem if (slist == NULL) {
655 1.22 lukem syslog(LOG_WARNING, "can't allocate memory for stringlist");
656 1.22 lukem return;
657 1.22 lukem }
658 1.1 lukem
659 1.1 lukem /* Check if this directory has already been visited */
660 1.45 lukem if (getcwd(curwd, sizeof(curwd) - 1) == NULL) {
661 1.13 mouse syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
662 1.1 lukem return;
663 1.1 lukem }
664 1.45 lukem if (sl_find(slist, curwd) != NULL)
665 1.1 lukem return;
666 1.1 lukem
667 1.45 lukem cp = xstrdup(curwd);
668 1.22 lukem if (sl_add(slist, cp) == -1)
669 1.22 lukem syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
670 1.1 lukem
671 1.1 lukem /* First check for a display file */
672 1.33 lukem (void)display_file(curclass.display, code);
673 1.1 lukem
674 1.1 lukem /* Now see if there are any notify files */
675 1.24 lukem if (EMPTYSTR(curclass.notify))
676 1.1 lukem return;
677 1.1 lukem
678 1.44 lukem memset(&gl, 0, sizeof(gl));
679 1.39 christos if (glob(curclass.notify, GLOB_LIMIT, NULL, &gl) != 0
680 1.40 christos || gl.gl_matchc == 0) {
681 1.40 christos globfree(&gl);
682 1.1 lukem return;
683 1.40 christos }
684 1.1 lukem time(&now);
685 1.1 lukem for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
686 1.1 lukem if (stat(*rlist, &st) != 0)
687 1.1 lukem continue;
688 1.7 mycroft if (!S_ISREG(st.st_mode))
689 1.1 lukem continue;
690 1.1 lukem then = st.st_mtime;
691 1.20 lukem if (code != 0) {
692 1.32 sommerfe reply(-code, "%s", "");
693 1.20 lukem code = 0;
694 1.20 lukem }
695 1.31 lukem reply(-code, "Please read the file %s", *rlist);
696 1.1 lukem t = localtime(&now);
697 1.1 lukem age = 365 * t->tm_year + t->tm_yday;
698 1.1 lukem t = localtime(&then);
699 1.1 lukem age -= 365 * t->tm_year + t->tm_yday;
700 1.31 lukem reply(-code, " it was last modified on %.24s - %d day%s ago",
701 1.19 lukem ctime(&then), age, PLURAL(age));
702 1.1 lukem }
703 1.1 lukem globfree(&gl);
704 1.1 lukem }
705 1.1 lukem
706 1.24 lukem int
707 1.33 lukem display_file(const char *file, int code)
708 1.24 lukem {
709 1.24 lukem FILE *f;
710 1.42 kristerw char *buf, *p;
711 1.45 lukem char curwd[MAXPATHLEN];
712 1.24 lukem size_t len;
713 1.36 lukem off_t lastnum;
714 1.24 lukem time_t now;
715 1.29 lukem
716 1.36 lukem lastnum = 0;
717 1.29 lukem if (quietmessages)
718 1.29 lukem return (0);
719 1.24 lukem
720 1.24 lukem if (EMPTYSTR(file))
721 1.24 lukem return(0);
722 1.24 lukem if ((f = fopen(file, "r")) == NULL)
723 1.24 lukem return (0);
724 1.32 sommerfe reply(-code, "%s", "");
725 1.24 lukem
726 1.24 lukem for (;
727 1.24 lukem (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
728 1.24 lukem if (len > 0)
729 1.24 lukem if (buf[len - 1] == '\n')
730 1.24 lukem buf[--len] = '\0';
731 1.31 lukem cprintf(stdout, " ");
732 1.24 lukem
733 1.24 lukem for (p = buf; *p; p++) {
734 1.24 lukem if (*p == '%') {
735 1.24 lukem p++;
736 1.24 lukem switch (*p) {
737 1.25 lukem
738 1.25 lukem case 'c':
739 1.31 lukem cprintf(stdout, "%s",
740 1.25 lukem curclass.classname ?
741 1.25 lukem curclass.classname : "<unknown>");
742 1.25 lukem break;
743 1.25 lukem
744 1.24 lukem case 'C':
745 1.45 lukem if (getcwd(curwd, sizeof(curwd)-1)
746 1.45 lukem == NULL){
747 1.24 lukem syslog(LOG_WARNING,
748 1.24 lukem "can't getcwd: %s",
749 1.24 lukem strerror(errno));
750 1.24 lukem continue;
751 1.24 lukem }
752 1.45 lukem cprintf(stdout, "%s", curwd);
753 1.24 lukem break;
754 1.25 lukem
755 1.24 lukem case 'E':
756 1.36 lukem if (! EMPTYSTR(emailaddr))
757 1.36 lukem cprintf(stdout, "%s",
758 1.36 lukem emailaddr);
759 1.24 lukem break;
760 1.25 lukem
761 1.24 lukem case 'L':
762 1.31 lukem cprintf(stdout, "%s", hostname);
763 1.24 lukem break;
764 1.25 lukem
765 1.25 lukem case 'M':
766 1.36 lukem if (curclass.limit == -1) {
767 1.31 lukem cprintf(stdout, "unlimited");
768 1.36 lukem lastnum = 0;
769 1.36 lukem } else {
770 1.31 lukem cprintf(stdout, "%d",
771 1.25 lukem curclass.limit);
772 1.36 lukem lastnum = curclass.limit;
773 1.36 lukem }
774 1.25 lukem break;
775 1.25 lukem
776 1.25 lukem case 'N':
777 1.36 lukem cprintf(stdout, "%d", connections);
778 1.36 lukem lastnum = connections;
779 1.25 lukem break;
780 1.25 lukem
781 1.24 lukem case 'R':
782 1.31 lukem cprintf(stdout, "%s", remotehost);
783 1.24 lukem break;
784 1.25 lukem
785 1.36 lukem case 's':
786 1.36 lukem if (lastnum != 1)
787 1.36 lukem cprintf(stdout, "s");
788 1.36 lukem break;
789 1.36 lukem
790 1.36 lukem case 'S':
791 1.36 lukem if (lastnum != 1)
792 1.36 lukem cprintf(stdout, "S");
793 1.36 lukem break;
794 1.36 lukem
795 1.24 lukem case 'T':
796 1.24 lukem now = time(NULL);
797 1.31 lukem cprintf(stdout, "%.24s", ctime(&now));
798 1.24 lukem break;
799 1.25 lukem
800 1.24 lukem case 'U':
801 1.31 lukem cprintf(stdout, "%s",
802 1.24 lukem pw ? pw->pw_name : "<unknown>");
803 1.24 lukem break;
804 1.25 lukem
805 1.24 lukem case '%':
806 1.31 lukem CPUTC('%', stdout);
807 1.24 lukem break;
808 1.25 lukem
809 1.24 lukem }
810 1.31 lukem } else
811 1.31 lukem CPUTC(*p, stdout);
812 1.24 lukem }
813 1.31 lukem cprintf(stdout, "\r\n");
814 1.24 lukem }
815 1.24 lukem
816 1.24 lukem (void)fflush(stdout);
817 1.24 lukem (void)fclose(f);
818 1.24 lukem return (1);
819 1.33 lukem }
820 1.33 lukem
821 1.33 lukem /*
822 1.33 lukem * Parse src, expanding '%' escapes, into dst (which must be at least
823 1.33 lukem * MAXPATHLEN long).
824 1.33 lukem */
825 1.33 lukem void
826 1.33 lukem format_path(char *dst, const char *src)
827 1.33 lukem {
828 1.33 lukem size_t len;
829 1.33 lukem const char *p;
830 1.33 lukem
831 1.33 lukem dst[0] = '\0';
832 1.33 lukem len = 0;
833 1.33 lukem if (src == NULL)
834 1.33 lukem return;
835 1.33 lukem for (p = src; *p && len < MAXPATHLEN; p++) {
836 1.33 lukem if (*p == '%') {
837 1.33 lukem p++;
838 1.33 lukem switch (*p) {
839 1.33 lukem
840 1.33 lukem case 'c':
841 1.33 lukem len += strlcpy(dst + len, curclass.classname,
842 1.33 lukem MAXPATHLEN - len);
843 1.33 lukem break;
844 1.33 lukem
845 1.33 lukem case 'd':
846 1.33 lukem len += strlcpy(dst + len, pw->pw_dir,
847 1.33 lukem MAXPATHLEN - len);
848 1.33 lukem break;
849 1.33 lukem
850 1.33 lukem case 'u':
851 1.33 lukem len += strlcpy(dst + len, pw->pw_name,
852 1.33 lukem MAXPATHLEN - len);
853 1.33 lukem break;
854 1.33 lukem
855 1.33 lukem case '%':
856 1.33 lukem dst[len++] = '%';
857 1.33 lukem break;
858 1.33 lukem
859 1.33 lukem }
860 1.33 lukem } else
861 1.33 lukem dst[len++] = *p;
862 1.33 lukem }
863 1.33 lukem if (len < MAXPATHLEN)
864 1.33 lukem dst[len] = '\0';
865 1.33 lukem dst[MAXPATHLEN - 1] = '\0';
866 1.24 lukem }
867 1.24 lukem
868 1.1 lukem /*
869 1.23 lukem * Find s2 at the end of s1. If found, return a string up to (but
870 1.1 lukem * not including) s2, otherwise returns NULL.
871 1.1 lukem */
872 1.1 lukem static char *
873 1.30 lukem strend(const char *s1, char *s2)
874 1.1 lukem {
875 1.25 lukem static char buf[MAXPATHLEN];
876 1.1 lukem
877 1.1 lukem char *start;
878 1.1 lukem size_t l1, l2;
879 1.1 lukem
880 1.1 lukem l1 = strlen(s1);
881 1.1 lukem l2 = strlen(s2);
882 1.1 lukem
883 1.44 lukem if (l2 >= l1 || l1 >= sizeof(buf))
884 1.1 lukem return(NULL);
885 1.1 lukem
886 1.24 lukem strlcpy(buf, s1, sizeof(buf));
887 1.1 lukem start = buf + (l1 - l2);
888 1.1 lukem
889 1.1 lukem if (strcmp(start, s2) == 0) {
890 1.1 lukem *start = '\0';
891 1.1 lukem return(buf);
892 1.1 lukem } else
893 1.1 lukem return(NULL);
894 1.1 lukem }
895 1.1 lukem
896 1.1 lukem static int
897 1.30 lukem filetypematch(char *types, int mode)
898 1.1 lukem {
899 1.1 lukem for ( ; types[0] != '\0'; types++)
900 1.1 lukem switch (*types) {
901 1.1 lukem case 'd':
902 1.1 lukem if (S_ISDIR(mode))
903 1.1 lukem return(1);
904 1.1 lukem break;
905 1.1 lukem case 'f':
906 1.1 lukem if (S_ISREG(mode))
907 1.1 lukem return(1);
908 1.1 lukem break;
909 1.1 lukem }
910 1.1 lukem return(0);
911 1.1 lukem }
912 1.1 lukem
913 1.1 lukem /*
914 1.1 lukem * Look for a conversion. If we succeed, return a pointer to the
915 1.1 lukem * command to execute for the conversion.
916 1.1 lukem *
917 1.1 lukem * The command is stored in a static array so there's no memory
918 1.1 lukem * leak problems, and not too much to change in ftpd.c. This
919 1.1 lukem * routine doesn't need to be re-entrant unless we start using a
920 1.1 lukem * multi-threaded ftpd, and that's not likely for a while...
921 1.1 lukem */
922 1.23 lukem char **
923 1.30 lukem do_conversion(const char *fname)
924 1.1 lukem {
925 1.1 lukem struct ftpconv *cp;
926 1.1 lukem struct stat st;
927 1.1 lukem int o_errno;
928 1.3 christos char *base = NULL;
929 1.23 lukem char *cmd, *p, *lp, **argv;
930 1.23 lukem StringList *sl;
931 1.1 lukem
932 1.1 lukem o_errno = errno;
933 1.23 lukem sl = NULL;
934 1.23 lukem cmd = NULL;
935 1.1 lukem for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
936 1.5 lukem if (cp->suffix == NULL) {
937 1.5 lukem syslog(LOG_WARNING,
938 1.5 lukem "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
939 1.5 lukem continue;
940 1.5 lukem }
941 1.1 lukem if ((base = strend(fname, cp->suffix)) == NULL)
942 1.1 lukem continue;
943 1.5 lukem if (cp->types == NULL || cp->disable == NULL ||
944 1.1 lukem cp->command == NULL)
945 1.1 lukem continue;
946 1.1 lukem /* Is it enabled? */
947 1.1 lukem if (strcmp(cp->disable, ".") != 0 &&
948 1.1 lukem stat(cp->disable, &st) == 0)
949 1.1 lukem continue;
950 1.1 lukem /* Does the base exist? */
951 1.1 lukem if (stat(base, &st) < 0)
952 1.1 lukem continue;
953 1.1 lukem /* Is the file type ok */
954 1.1 lukem if (!filetypematch(cp->types, st.st_mode))
955 1.1 lukem continue;
956 1.1 lukem break; /* "We have a winner!" */
957 1.1 lukem }
958 1.1 lukem
959 1.1 lukem /* If we got through the list, no conversion */
960 1.23 lukem if (cp == NULL)
961 1.23 lukem goto cleanup_do_conv;
962 1.23 lukem
963 1.23 lukem /* Split up command into an argv */
964 1.23 lukem if ((sl = sl_init()) == NULL)
965 1.23 lukem goto cleanup_do_conv;
966 1.23 lukem cmd = xstrdup(cp->command);
967 1.23 lukem p = cmd;
968 1.23 lukem while (p) {
969 1.23 lukem NEXTWORD(p, lp);
970 1.23 lukem if (strcmp(lp, "%s") == 0)
971 1.23 lukem lp = base;
972 1.23 lukem if (sl_add(sl, xstrdup(lp)) == -1)
973 1.23 lukem goto cleanup_do_conv;
974 1.1 lukem }
975 1.1 lukem
976 1.23 lukem if (sl_add(sl, NULL) == -1)
977 1.23 lukem goto cleanup_do_conv;
978 1.23 lukem argv = sl->sl_str;
979 1.23 lukem free(cmd);
980 1.23 lukem free(sl);
981 1.23 lukem return(argv);
982 1.23 lukem
983 1.23 lukem cleanup_do_conv:
984 1.23 lukem if (sl)
985 1.23 lukem sl_free(sl, 1);
986 1.23 lukem free(cmd);
987 1.23 lukem errno = o_errno;
988 1.23 lukem return(NULL);
989 1.24 lukem }
990 1.24 lukem
991 1.24 lukem /*
992 1.36 lukem * Convert the string `arg' to a long long, which may have an optional SI suffix
993 1.36 lukem * (`b', `k', `m', `g', `t'). Returns the number for success, -1 otherwise.
994 1.24 lukem */
995 1.36 lukem LLT
996 1.36 lukem strsuftoll(const char *arg)
997 1.24 lukem {
998 1.24 lukem char *cp;
999 1.36 lukem LLT val;
1000 1.24 lukem
1001 1.24 lukem if (!isdigit((unsigned char)arg[0]))
1002 1.24 lukem return (-1);
1003 1.24 lukem
1004 1.36 lukem val = STRTOLL(arg, &cp, 10);
1005 1.24 lukem if (cp != NULL) {
1006 1.24 lukem if (cp[0] != '\0' && cp[1] != '\0')
1007 1.24 lukem return (-1);
1008 1.24 lukem switch (tolower((unsigned char)cp[0])) {
1009 1.24 lukem case '\0':
1010 1.24 lukem case 'b':
1011 1.24 lukem break;
1012 1.24 lukem case 'k':
1013 1.24 lukem val <<= 10;
1014 1.24 lukem break;
1015 1.24 lukem case 'm':
1016 1.24 lukem val <<= 20;
1017 1.24 lukem break;
1018 1.24 lukem case 'g':
1019 1.24 lukem val <<= 30;
1020 1.24 lukem break;
1021 1.38 lukem #ifndef NO_LONG_LONG
1022 1.36 lukem case 't':
1023 1.36 lukem val <<= 40;
1024 1.36 lukem break;
1025 1.38 lukem #endif
1026 1.24 lukem default:
1027 1.24 lukem return (-1);
1028 1.24 lukem }
1029 1.24 lukem }
1030 1.36 lukem if (val < 0)
1031 1.24 lukem return (-1);
1032 1.24 lukem
1033 1.24 lukem return (val);
1034 1.25 lukem }
1035 1.25 lukem
1036 1.26 lukem /*
1037 1.26 lukem * Count the number of current connections, reading from
1038 1.26 lukem * /var/run/ftpd.pids-<class>
1039 1.26 lukem * Does a kill -0 on each pid in that file, and only counts
1040 1.26 lukem * processes that exist (or frees the slot if it doesn't).
1041 1.26 lukem * Adds getpid() to the first free slot. Truncates the file
1042 1.26 lukem * if possible.
1043 1.26 lukem */
1044 1.25 lukem void
1045 1.30 lukem count_users(void)
1046 1.25 lukem {
1047 1.25 lukem char fn[MAXPATHLEN];
1048 1.25 lukem int fd, i, last;
1049 1.25 lukem size_t count;
1050 1.25 lukem pid_t *pids, mypid;
1051 1.25 lukem struct stat sb;
1052 1.25 lukem
1053 1.25 lukem (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
1054 1.25 lukem (void)strlcat(fn, curclass.classname, sizeof(fn));
1055 1.25 lukem pids = NULL;
1056 1.25 lukem connections = 1;
1057 1.25 lukem
1058 1.35 lukem if ((fd = open(fn, O_RDWR | O_CREAT, 0600)) == -1)
1059 1.25 lukem return;
1060 1.35 lukem if (lockf(fd, F_TLOCK, 0) == -1)
1061 1.35 lukem goto cleanup_count;
1062 1.25 lukem if (fstat(fd, &sb) == -1)
1063 1.25 lukem goto cleanup_count;
1064 1.25 lukem if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
1065 1.25 lukem goto cleanup_count;
1066 1.25 lukem count = read(fd, pids, sb.st_size);
1067 1.25 lukem if (count < 0 || count != sb.st_size)
1068 1.25 lukem goto cleanup_count;
1069 1.25 lukem count /= sizeof(pid_t);
1070 1.25 lukem mypid = getpid();
1071 1.25 lukem last = 0;
1072 1.25 lukem for (i = 0; i < count; i++) {
1073 1.25 lukem if (pids[i] == 0)
1074 1.25 lukem continue;
1075 1.25 lukem if (kill(pids[i], 0) == -1 && errno != EPERM) {
1076 1.25 lukem if (mypid != 0) {
1077 1.25 lukem pids[i] = mypid;
1078 1.25 lukem mypid = 0;
1079 1.25 lukem last = i;
1080 1.25 lukem }
1081 1.25 lukem } else {
1082 1.25 lukem connections++;
1083 1.25 lukem last = i;
1084 1.25 lukem }
1085 1.25 lukem }
1086 1.25 lukem if (mypid != 0) {
1087 1.25 lukem if (pids[last] != 0)
1088 1.25 lukem last++;
1089 1.25 lukem pids[last] = mypid;
1090 1.25 lukem }
1091 1.25 lukem count = (last + 1) * sizeof(pid_t);
1092 1.25 lukem if (lseek(fd, 0, SEEK_SET) == -1)
1093 1.25 lukem goto cleanup_count;
1094 1.25 lukem if (write(fd, pids, count) == -1)
1095 1.25 lukem goto cleanup_count;
1096 1.25 lukem (void)ftruncate(fd, count);
1097 1.25 lukem
1098 1.25 lukem cleanup_count:
1099 1.35 lukem if (lseek(fd, 0, SEEK_SET) != -1)
1100 1.35 lukem (void)lockf(fd, F_ULOCK, 0);
1101 1.25 lukem close(fd);
1102 1.25 lukem REASSIGN(pids, NULL);
1103 1.1 lukem }
1104