conf.c revision 1.28 1 /* $NetBSD: conf.c,v 1.28 2000/01/12 22:39:27 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge and Luke Mewburn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: conf.c,v 1.28 2000/01/12 22:39:27 lukem Exp $");
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <ctype.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <glob.h>
52 #include <setjmp.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stringlist.h>
58 #include <syslog.h>
59 #include <time.h>
60 #include <unistd.h>
61 #include <util.h>
62
63 #ifdef KERBEROS5
64 #include <krb5/krb5.h>
65 #endif
66
67 #include "extern.h"
68 #include "pathnames.h"
69
70 static char *strend __P((const char *, char *));
71 static int filetypematch __P((char *, int));
72
73
74 /*
75 * Initialise curclass to an `empty' state
76 */
77 void
78 init_curclass()
79 {
80 struct ftpconv *conv, *cnext;
81
82 for (conv = curclass.conversions; conv != NULL; conv = cnext) {
83 REASSIGN(conv->suffix, NULL);
84 REASSIGN(conv->types, NULL);
85 REASSIGN(conv->disable, NULL);
86 REASSIGN(conv->command, NULL);
87 cnext = conv->next;
88 free(conv);
89 }
90
91 curclass.checkportcmd = 0;
92 REASSIGN(curclass.classname, NULL);
93 curclass.conversions = NULL;
94 REASSIGN(curclass.display, NULL);
95 curclass.limit = -1; /* unlimited connections */
96 REASSIGN(curclass.limitfile, NULL);
97 curclass.maxrateget = 0;
98 curclass.maxrateput = 0;
99 curclass.maxtimeout = 7200; /* 2 hours */
100 curclass.modify = 1;
101 REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
102 REASSIGN(curclass.notify, NULL);
103 curclass.passive = 1;
104 curclass.portmin = 0;
105 curclass.portmax = 0;
106 curclass.rateget = 0;
107 curclass.rateput = 0;
108 curclass.timeout = 900; /* 15 minutes */
109 curclass.umask = 027;
110 curclass.upload = 1;
111 }
112
113 /*
114 * Parse the configuration file, looking for the named class, and
115 * define curclass to contain the appropriate settings.
116 */
117 void
118 parse_conf(findclass)
119 char *findclass;
120 {
121 FILE *f;
122 char *buf, *p;
123 size_t len;
124 int none, match, rate;
125 char *endp;
126 char *class, *word, *arg, *template;
127 const char *infile;
128 size_t line;
129 unsigned int timeout;
130 struct ftpconv *conv, *cnext;
131
132 init_curclass();
133 REASSIGN(curclass.classname, xstrdup(findclass));
134 if (strcasecmp(findclass, "guest") == 0) {
135 curclass.modify = 0;
136 curclass.umask = 0707;
137 }
138
139 infile = conffilename(_PATH_FTPDCONF);
140 if ((f = fopen(infile, "r")) == NULL)
141 return;
142
143 line = 0;
144 template = NULL;
145 for (;
146 (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
147 FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
148 free(buf)) {
149 none = match = 0;
150 p = buf;
151 if (len < 1)
152 continue;
153 if (p[len - 1] == '\n')
154 p[--len] = '\0';
155 if (EMPTYSTR(p))
156 continue;
157
158 NEXTWORD(p, word);
159 NEXTWORD(p, class);
160 NEXTWORD(p, arg);
161 if (EMPTYSTR(word) || EMPTYSTR(class))
162 continue;
163 if (strcasecmp(class, "none") == 0)
164 none = 1;
165 if (! (strcasecmp(class, findclass) == 0 ||
166 (template != NULL && strcasecmp(class, template) == 0) ||
167 none ||
168 strcasecmp(class, "all") == 0) )
169 continue;
170
171 if (strcasecmp(word, "checkportcmd") == 0) {
172 if (none ||
173 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
174 curclass.checkportcmd = 0;
175 else
176 curclass.checkportcmd = 1;
177
178 } else if (strcasecmp(word, "classtype") == 0) {
179 if (!none && !EMPTYSTR(arg)) {
180 if (strcasecmp(arg, "GUEST") == 0)
181 curclass.type = CLASS_GUEST;
182 else if (strcasecmp(arg, "CHROOT") == 0)
183 curclass.type = CLASS_CHROOT;
184 else if (strcasecmp(arg, "REAL") == 0)
185 curclass.type = CLASS_REAL;
186 else {
187 syslog(LOG_WARNING,
188 "%s line %d: unknown class type `%s'",
189 infile, (int)line, arg);
190 continue;
191 }
192 }
193
194 } else if (strcasecmp(word, "conversion") == 0) {
195 char *suffix, *types, *disable, *convcmd;
196
197 if (EMPTYSTR(arg)) {
198 syslog(LOG_WARNING,
199 "%s line %d: %s requires a suffix",
200 infile, (int)line, word);
201 continue; /* need a suffix */
202 }
203 NEXTWORD(p, types);
204 NEXTWORD(p, disable);
205 convcmd = p;
206 if (convcmd)
207 convcmd += strspn(convcmd, " \t");
208 suffix = xstrdup(arg);
209 if (none || EMPTYSTR(types) ||
210 EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
211 types = NULL;
212 disable = NULL;
213 convcmd = NULL;
214 } else {
215 types = xstrdup(types);
216 disable = xstrdup(disable);
217 convcmd = xstrdup(convcmd);
218 }
219 for (conv = curclass.conversions; conv != NULL;
220 conv = conv->next) {
221 if (strcmp(conv->suffix, suffix) == 0)
222 break;
223 }
224 if (conv == NULL) {
225 conv = (struct ftpconv *)
226 calloc(1, sizeof(struct ftpconv));
227 if (conv == NULL) {
228 syslog(LOG_WARNING, "can't malloc");
229 continue;
230 }
231 conv->next = NULL;
232 for (cnext = curclass.conversions;
233 cnext != NULL; cnext = cnext->next)
234 if (cnext->next == NULL)
235 break;
236 if (cnext != NULL)
237 cnext->next = conv;
238 else
239 curclass.conversions = conv;
240 }
241 REASSIGN(conv->suffix, suffix);
242 REASSIGN(conv->types, types);
243 REASSIGN(conv->disable, disable);
244 REASSIGN(conv->command, convcmd);
245
246 } else if (strcasecmp(word, "display") == 0) {
247 if (none || EMPTYSTR(arg))
248 arg = NULL;
249 else
250 arg = xstrdup(arg);
251 REASSIGN(curclass.display, arg);
252
253 } else if (strcasecmp(word, "limit") == 0) {
254 int limit;
255
256 if (none || EMPTYSTR(arg))
257 continue;
258 limit = (int)strtol(arg, &endp, 10);
259 if (*endp != 0) {
260 syslog(LOG_WARNING,
261 "%s line %d: invalid limit %s",
262 infile, (int)line, arg);
263 continue;
264 }
265 curclass.limit = limit;
266 REASSIGN(curclass.limitfile,
267 EMPTYSTR(p) ? NULL : xstrdup(p));
268
269 } else if (strcasecmp(word, "maxtimeout") == 0) {
270 if (none || EMPTYSTR(arg))
271 continue;
272 timeout = (unsigned int)strtoul(arg, &endp, 10);
273 if (*endp != 0) {
274 syslog(LOG_WARNING,
275 "%s line %d: invalid maxtimeout %s",
276 infile, (int)line, arg);
277 continue;
278 }
279 if (timeout < 30) {
280 syslog(LOG_WARNING,
281 "%s line %d: maxtimeout %d < 30 seconds",
282 infile, (int)line, timeout);
283 continue;
284 }
285 if (timeout < curclass.timeout) {
286 syslog(LOG_WARNING,
287 "%s line %d: maxtimeout %d < timeout (%d)",
288 infile, (int)line, timeout,
289 curclass.timeout);
290 continue;
291 }
292 curclass.maxtimeout = timeout;
293
294 } else if (strcasecmp(word, "modify") == 0) {
295 if (none ||
296 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
297 curclass.modify = 0;
298 else
299 curclass.modify = 1;
300
301 } else if (strcasecmp(word, "motd") == 0) {
302 if (none || EMPTYSTR(arg))
303 arg = NULL;
304 else
305 arg = xstrdup(arg);
306 REASSIGN(curclass.motd, arg);
307
308
309 } else if (strcasecmp(word, "notify") == 0) {
310 if (none || EMPTYSTR(arg))
311 arg = NULL;
312 else
313 arg = xstrdup(arg);
314 REASSIGN(curclass.notify, arg);
315
316 } else if (strcasecmp(word, "passive") == 0) {
317 if (none ||
318 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
319 curclass.passive = 0;
320 else
321 curclass.passive = 1;
322
323 } else if (strcasecmp(word, "portrange") == 0) {
324 int minport, maxport;
325 char *min, *max;
326
327 if (none) {
328 curclass.portmin = 0;
329 curclass.portmax = 0;
330 continue;
331 }
332 if (EMPTYSTR(arg))
333 continue;
334 min = arg;
335 NEXTWORD(p, max);
336 if (EMPTYSTR(max)) {
337 syslog(LOG_WARNING,
338 "%s line %d: missing maxport argument",
339 infile, (int)line);
340 continue;
341 }
342 minport = (int)strtol(min, &endp, 10);
343 if (*endp != 0 || minport < IPPORT_RESERVED ||
344 minport > IPPORT_ANONMAX) {
345 syslog(LOG_WARNING,
346 "%s line %d: invalid minport %s",
347 infile, (int)line, min);
348 continue;
349 }
350 maxport = (int)strtol(max, &endp, 10);
351 if (*endp != 0 || maxport < IPPORT_RESERVED ||
352 maxport > IPPORT_ANONMAX) {
353 syslog(LOG_WARNING,
354 "%s line %d: invalid maxport %s",
355 infile, (int)line, max);
356 continue;
357 }
358 if (minport >= maxport) {
359 syslog(LOG_WARNING,
360 "%s line %d: minport %d >= maxport %d",
361 infile, (int)line, minport, maxport);
362 continue;
363 }
364 curclass.portmin = minport;
365 curclass.portmax = maxport;
366
367 } else if (strcasecmp(word, "rateget") == 0) {
368 if (none || EMPTYSTR(arg))
369 continue;
370 rate = strsuftoi(arg);
371 if (rate == -1) {
372 syslog(LOG_WARNING,
373 "%s line %d: invalid rateget %s",
374 infile, (int)line, arg);
375 continue;
376 }
377 curclass.maxrateget = rate;
378 curclass.rateget = rate;
379
380 } else if (strcasecmp(word, "rateput") == 0) {
381 if (none || EMPTYSTR(arg))
382 continue;
383 rate = strsuftoi(arg);
384 if (rate == -1) {
385 syslog(LOG_WARNING,
386 "%s line %d: invalid rateput %s",
387 infile, (int)line, arg);
388 continue;
389 }
390 curclass.maxrateput = rate;
391 curclass.rateput = rate;
392
393 } else if (strcasecmp(word, "timeout") == 0) {
394 if (none || EMPTYSTR(arg))
395 continue;
396 timeout = (unsigned int)strtoul(arg, &endp, 10);
397 if (*endp != 0) {
398 syslog(LOG_WARNING,
399 "%s line %d: invalid timeout %s",
400 infile, (int)line, arg);
401 continue;
402 }
403 if (timeout < 30) {
404 syslog(LOG_WARNING,
405 "%s line %d: timeout %d < 30 seconds",
406 infile, (int)line, timeout);
407 continue;
408 }
409 if (timeout > curclass.maxtimeout) {
410 syslog(LOG_WARNING,
411 "%s line %d: timeout %d > maxtimeout (%d)",
412 infile, (int)line, timeout,
413 curclass.maxtimeout);
414 continue;
415 }
416 curclass.timeout = timeout;
417
418 } else if (strcasecmp(word, "template") == 0) {
419 if (none)
420 continue;
421 REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
422
423 } else if (strcasecmp(word, "umask") == 0) {
424 mode_t umask;
425
426 if (none || EMPTYSTR(arg))
427 continue;
428 umask = (mode_t)strtoul(arg, &endp, 8);
429 if (*endp != 0 || umask > 0777) {
430 syslog(LOG_WARNING,
431 "%s line %d: invalid umask %s",
432 infile, (int)line, arg);
433 continue;
434 }
435 curclass.umask = umask;
436
437 } else if (strcasecmp(word, "upload") == 0) {
438 if (none ||
439 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
440 curclass.modify = 0;
441 curclass.upload = 0;
442 } else
443 curclass.upload = 1;
444
445 } else {
446 syslog(LOG_WARNING,
447 "%s line %d: unknown directive '%s'",
448 infile, (int)line, word);
449 continue;
450 }
451 }
452 REASSIGN(template, NULL);
453 fclose(f);
454 }
455
456 /*
457 * Show file listed in curclass.display first time in, and list all the
458 * files named in curclass.notify in the current directory. Send back
459 * responses with the prefix `code' + "-".
460 */
461 void
462 show_chdir_messages(code)
463 int code;
464 {
465 static StringList *slist = NULL;
466
467 struct stat st;
468 struct tm *t;
469 glob_t gl;
470 time_t now, then;
471 int age;
472 char cwd[MAXPATHLEN];
473 char *cp, **rlist;
474
475 /* Setup list for directory cache */
476 if (slist == NULL)
477 slist = sl_init();
478 if (slist == NULL) {
479 syslog(LOG_WARNING, "can't allocate memory for stringlist");
480 return;
481 }
482
483 /* Check if this directory has already been visited */
484 if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
485 syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
486 return;
487 }
488 if (sl_find(slist, cwd) != NULL)
489 return;
490
491 cp = xstrdup(cwd);
492 if (sl_add(slist, cp) == -1)
493 syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
494
495 /* First check for a display file */
496 (void)format_file(curclass.display, code);
497
498 /* Now see if there are any notify files */
499 if (EMPTYSTR(curclass.notify))
500 return;
501
502 if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
503 return;
504 time(&now);
505 for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
506 if (stat(*rlist, &st) != 0)
507 continue;
508 if (!S_ISREG(st.st_mode))
509 continue;
510 then = st.st_mtime;
511 if (code != 0) {
512 lreply(code, "");
513 code = 0;
514 }
515 lreply(code, "Please read the file %s", *rlist);
516 t = localtime(&now);
517 age = 365 * t->tm_year + t->tm_yday;
518 t = localtime(&then);
519 age -= 365 * t->tm_year + t->tm_yday;
520 lreply(code, " it was last modified on %.24s - %d day%s ago",
521 ctime(&then), age, PLURAL(age));
522 }
523 globfree(&gl);
524 }
525
526 int
527 format_file(file, code)
528 const char *file;
529 int code;
530 {
531 FILE *f;
532 char *buf, *p, *cwd;
533 size_t len;
534 off_t b;
535 time_t now;
536
537 #define PUTC(x) putchar(x), b++
538
539 if (EMPTYSTR(file))
540 return(0);
541 if ((f = fopen(file, "r")) == NULL)
542 return (0);
543 lreply(code, "");
544
545 b = 0;
546 for (;
547 (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
548 if (len > 0)
549 if (buf[len - 1] == '\n')
550 buf[--len] = '\0';
551 b += printf(" ");
552
553 for (p = buf; *p; p++) {
554 if (*p == '%') {
555 p++;
556 switch (*p) {
557
558 case 'c':
559 b += printf("%s",
560 curclass.classname ?
561 curclass.classname : "<unknown>");
562 break;
563
564 case 'C':
565 if (getcwd(cwd, sizeof(cwd)-1) == NULL){
566 syslog(LOG_WARNING,
567 "can't getcwd: %s",
568 strerror(errno));
569 continue;
570 }
571 b += printf("%s", cwd);
572 break;
573
574 case 'E':
575 /* XXXX email address */
576 break;
577
578 case 'L':
579 b += printf("%s", hostname);
580 break;
581
582 case 'M':
583 if (curclass.limit == -1)
584 b += printf("unlimited");
585 else
586 b += printf("%d",
587 curclass.limit);
588 break;
589
590 case 'N':
591 if (connections > 0)
592 b += printf("%d", connections);
593 break;
594
595 case 'R':
596 b += printf("%s", remotehost);
597 break;
598
599 case 'T':
600 now = time(NULL);
601 b += printf("%.24s", ctime(&now));
602 break;
603
604 case 'U':
605 b += printf("%s",
606 pw ? pw->pw_name : "<unknown>");
607 break;
608
609 case '%':
610 PUTC('%');
611 break;
612
613 }
614 } else {
615 PUTC(*p);
616 }
617 }
618 PUTC('\r');
619 PUTC('\n');
620 }
621
622 total_bytes += b;
623 total_bytes_out += b;
624 (void)fflush(stdout);
625 (void)fclose(f);
626 return (1);
627 }
628
629 /*
630 * Find s2 at the end of s1. If found, return a string up to (but
631 * not including) s2, otherwise returns NULL.
632 */
633 static char *
634 strend(s1, s2)
635 const char *s1;
636 char *s2;
637 {
638 static char buf[MAXPATHLEN];
639
640 char *start;
641 size_t l1, l2;
642
643 l1 = strlen(s1);
644 l2 = strlen(s2);
645
646 if (l2 >= l1)
647 return(NULL);
648
649 strlcpy(buf, s1, sizeof(buf));
650 start = buf + (l1 - l2);
651
652 if (strcmp(start, s2) == 0) {
653 *start = '\0';
654 return(buf);
655 } else
656 return(NULL);
657 }
658
659 static int
660 filetypematch(types, mode)
661 char *types;
662 int mode;
663 {
664 for ( ; types[0] != '\0'; types++)
665 switch (*types) {
666 case 'd':
667 if (S_ISDIR(mode))
668 return(1);
669 break;
670 case 'f':
671 if (S_ISREG(mode))
672 return(1);
673 break;
674 }
675 return(0);
676 }
677
678 /*
679 * Look for a conversion. If we succeed, return a pointer to the
680 * command to execute for the conversion.
681 *
682 * The command is stored in a static array so there's no memory
683 * leak problems, and not too much to change in ftpd.c. This
684 * routine doesn't need to be re-entrant unless we start using a
685 * multi-threaded ftpd, and that's not likely for a while...
686 */
687 char **
688 do_conversion(fname)
689 const char *fname;
690 {
691 struct ftpconv *cp;
692 struct stat st;
693 int o_errno;
694 char *base = NULL;
695 char *cmd, *p, *lp, **argv;
696 StringList *sl;
697
698 o_errno = errno;
699 sl = NULL;
700 cmd = NULL;
701 for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
702 if (cp->suffix == NULL) {
703 syslog(LOG_WARNING,
704 "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
705 continue;
706 }
707 if ((base = strend(fname, cp->suffix)) == NULL)
708 continue;
709 if (cp->types == NULL || cp->disable == NULL ||
710 cp->command == NULL)
711 continue;
712 /* Is it enabled? */
713 if (strcmp(cp->disable, ".") != 0 &&
714 stat(cp->disable, &st) == 0)
715 continue;
716 /* Does the base exist? */
717 if (stat(base, &st) < 0)
718 continue;
719 /* Is the file type ok */
720 if (!filetypematch(cp->types, st.st_mode))
721 continue;
722 break; /* "We have a winner!" */
723 }
724
725 /* If we got through the list, no conversion */
726 if (cp == NULL)
727 goto cleanup_do_conv;
728
729 /* Split up command into an argv */
730 if ((sl = sl_init()) == NULL)
731 goto cleanup_do_conv;
732 cmd = xstrdup(cp->command);
733 p = cmd;
734 while (p) {
735 NEXTWORD(p, lp);
736 if (strcmp(lp, "%s") == 0)
737 lp = base;
738 if (sl_add(sl, xstrdup(lp)) == -1)
739 goto cleanup_do_conv;
740 }
741
742 if (sl_add(sl, NULL) == -1)
743 goto cleanup_do_conv;
744 argv = sl->sl_str;
745 free(cmd);
746 free(sl);
747 return(argv);
748
749 cleanup_do_conv:
750 if (sl)
751 sl_free(sl, 1);
752 free(cmd);
753 errno = o_errno;
754 return(NULL);
755 }
756
757 /*
758 * Convert the string `arg' to an int, which may have an optional SI suffix
759 * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
760 */
761 int
762 strsuftoi(arg)
763 const char *arg;
764 {
765 char *cp;
766 long val;
767
768 if (!isdigit((unsigned char)arg[0]))
769 return (-1);
770
771 val = strtol(arg, &cp, 10);
772 if (cp != NULL) {
773 if (cp[0] != '\0' && cp[1] != '\0')
774 return (-1);
775 switch (tolower((unsigned char)cp[0])) {
776 case '\0':
777 case 'b':
778 break;
779 case 'k':
780 val <<= 10;
781 break;
782 case 'm':
783 val <<= 20;
784 break;
785 case 'g':
786 val <<= 30;
787 break;
788 default:
789 return (-1);
790 }
791 }
792 if (val < 0 || val > INT_MAX)
793 return (-1);
794
795 return (val);
796 }
797
798 /*
799 * Count the number of current connections, reading from
800 * /var/run/ftpd.pids-<class>
801 * Does a kill -0 on each pid in that file, and only counts
802 * processes that exist (or frees the slot if it doesn't).
803 * Adds getpid() to the first free slot. Truncates the file
804 * if possible.
805 */
806 void
807 count_users()
808 {
809 char fn[MAXPATHLEN];
810 int fd, i, last;
811 size_t count;
812 pid_t *pids, mypid;
813 struct stat sb;
814
815 (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
816 (void)strlcat(fn, curclass.classname, sizeof(fn));
817 pids = NULL;
818 connections = 1;
819
820 if ((fd = open(fn, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
821 return;
822 if (fstat(fd, &sb) == -1)
823 goto cleanup_count;
824 if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
825 goto cleanup_count;
826 count = read(fd, pids, sb.st_size);
827 if (count < 0 || count != sb.st_size)
828 goto cleanup_count;
829 count /= sizeof(pid_t);
830 mypid = getpid();
831 last = 0;
832 for (i = 0; i < count; i++) {
833 if (pids[i] == 0)
834 continue;
835 if (kill(pids[i], 0) == -1 && errno != EPERM) {
836 if (mypid != 0) {
837 pids[i] = mypid;
838 mypid = 0;
839 last = i;
840 }
841 } else {
842 connections++;
843 last = i;
844 }
845 }
846 if (mypid != 0) {
847 if (pids[last] != 0)
848 last++;
849 pids[last] = mypid;
850 }
851 count = (last + 1) * sizeof(pid_t);
852 if (lseek(fd, 0, SEEK_SET) == -1)
853 goto cleanup_count;
854 if (write(fd, pids, count) == -1)
855 goto cleanup_count;
856 (void)ftruncate(fd, count);
857
858 cleanup_count:
859 (void)flock(fd, LOCK_UN);
860 close(fd);
861 REASSIGN(pids, NULL);
862 }
863