conf.c revision 1.35 1 /* $NetBSD: conf.c,v 1.35 2000/11/15 02:32:30 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.35 2000/11/15 02:32:30 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(const char *, char *);
71 static int filetypematch(char *, int);
72
73
74 /*
75 * Initialise curclass to an `empty' state
76 */
77 void
78 init_curclass(void)
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 = 1;
92 REASSIGN(curclass.chroot, NULL);
93 REASSIGN(curclass.classname, NULL);
94 curclass.conversions = NULL;
95 REASSIGN(curclass.display, NULL);
96 REASSIGN(curclass.homedir, NULL);
97 curclass.limit = -1; /* unlimited connections */
98 REASSIGN(curclass.limitfile, NULL);
99 curclass.maxrateget = 0;
100 curclass.maxrateput = 0;
101 curclass.maxtimeout = 7200; /* 2 hours */
102 curclass.modify = 1;
103 REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
104 REASSIGN(curclass.notify, NULL);
105 curclass.passive = 1;
106 curclass.portmin = 0;
107 curclass.portmax = 0;
108 curclass.rateget = 0;
109 curclass.rateput = 0;
110 curclass.timeout = 900; /* 15 minutes */
111 /* curclass.type is set elsewhere */
112 curclass.umask = 027;
113 curclass.upload = 1;
114 }
115
116 /*
117 * Parse the configuration file, looking for the named class, and
118 * define curclass to contain the appropriate settings.
119 */
120 void
121 parse_conf(const char *findclass)
122 {
123 FILE *f;
124 char *buf, *p;
125 size_t len;
126 int none, match, rate;
127 char *endp;
128 char *class, *word, *arg, *template;
129 const char *infile;
130 size_t line;
131 unsigned int timeout;
132 struct ftpconv *conv, *cnext;
133
134 init_curclass();
135 REASSIGN(curclass.classname, xstrdup(findclass));
136 if (strcasecmp(findclass, "guest") == 0) {
137 curclass.modify = 0;
138 curclass.umask = 0707;
139 }
140
141 infile = conffilename(_PATH_FTPDCONF);
142 if ((f = fopen(infile, "r")) == NULL)
143 return;
144
145 line = 0;
146 template = NULL;
147 for (;
148 (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
149 FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
150 free(buf)) {
151 none = match = 0;
152 p = buf;
153 if (len < 1)
154 continue;
155 if (p[len - 1] == '\n')
156 p[--len] = '\0';
157 if (EMPTYSTR(p))
158 continue;
159
160 NEXTWORD(p, word);
161 NEXTWORD(p, class);
162 NEXTWORD(p, arg);
163 if (EMPTYSTR(word) || EMPTYSTR(class))
164 continue;
165 if (strcasecmp(class, "none") == 0)
166 none = 1;
167 if (! (strcasecmp(class, findclass) == 0 ||
168 (template != NULL && strcasecmp(class, template) == 0) ||
169 none ||
170 strcasecmp(class, "all") == 0) )
171 continue;
172
173 if (strcasecmp(word, "checkportcmd") == 0) {
174 if (none ||
175 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
176 curclass.checkportcmd = 0;
177 else
178 curclass.checkportcmd = 1;
179
180 } else if (strcasecmp(word, "chroot") == 0) {
181 if (none || EMPTYSTR(arg))
182 arg = NULL;
183 else
184 arg = xstrdup(arg);
185 REASSIGN(curclass.chroot, arg);
186
187 } else if (strcasecmp(word, "classtype") == 0) {
188 if (!none && !EMPTYSTR(arg)) {
189 if (strcasecmp(arg, "GUEST") == 0)
190 curclass.type = CLASS_GUEST;
191 else if (strcasecmp(arg, "CHROOT") == 0)
192 curclass.type = CLASS_CHROOT;
193 else if (strcasecmp(arg, "REAL") == 0)
194 curclass.type = CLASS_REAL;
195 else {
196 syslog(LOG_WARNING,
197 "%s line %d: unknown class type `%s'",
198 infile, (int)line, arg);
199 continue;
200 }
201 }
202
203 } else if (strcasecmp(word, "conversion") == 0) {
204 char *suffix, *types, *disable, *convcmd;
205
206 if (EMPTYSTR(arg)) {
207 syslog(LOG_WARNING,
208 "%s line %d: %s requires a suffix",
209 infile, (int)line, word);
210 continue; /* need a suffix */
211 }
212 NEXTWORD(p, types);
213 NEXTWORD(p, disable);
214 convcmd = p;
215 if (convcmd)
216 convcmd += strspn(convcmd, " \t");
217 suffix = xstrdup(arg);
218 if (none || EMPTYSTR(types) ||
219 EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
220 types = NULL;
221 disable = NULL;
222 convcmd = NULL;
223 } else {
224 types = xstrdup(types);
225 disable = xstrdup(disable);
226 convcmd = xstrdup(convcmd);
227 }
228 for (conv = curclass.conversions; conv != NULL;
229 conv = conv->next) {
230 if (strcmp(conv->suffix, suffix) == 0)
231 break;
232 }
233 if (conv == NULL) {
234 conv = (struct ftpconv *)
235 calloc(1, sizeof(struct ftpconv));
236 if (conv == NULL) {
237 syslog(LOG_WARNING, "can't malloc");
238 continue;
239 }
240 conv->next = NULL;
241 for (cnext = curclass.conversions;
242 cnext != NULL; cnext = cnext->next)
243 if (cnext->next == NULL)
244 break;
245 if (cnext != NULL)
246 cnext->next = conv;
247 else
248 curclass.conversions = conv;
249 }
250 REASSIGN(conv->suffix, suffix);
251 REASSIGN(conv->types, types);
252 REASSIGN(conv->disable, disable);
253 REASSIGN(conv->command, convcmd);
254
255 } else if (strcasecmp(word, "display") == 0) {
256 if (none || EMPTYSTR(arg))
257 arg = NULL;
258 else
259 arg = xstrdup(arg);
260 REASSIGN(curclass.display, arg);
261
262 } else if (strcasecmp(word, "homedir") == 0) {
263 if (none || EMPTYSTR(arg))
264 arg = NULL;
265 else
266 arg = xstrdup(arg);
267 REASSIGN(curclass.homedir, arg);
268
269 } else if (strcasecmp(word, "limit") == 0) {
270 int limit;
271
272 if (none || EMPTYSTR(arg))
273 continue;
274 limit = (int)strtol(arg, &endp, 10);
275 if (*endp != 0) {
276 syslog(LOG_WARNING,
277 "%s line %d: invalid limit %s",
278 infile, (int)line, arg);
279 continue;
280 }
281 curclass.limit = limit;
282 REASSIGN(curclass.limitfile,
283 EMPTYSTR(p) ? NULL : xstrdup(p));
284
285 } else if (strcasecmp(word, "maxtimeout") == 0) {
286 if (none || EMPTYSTR(arg))
287 continue;
288 timeout = (unsigned int)strtoul(arg, &endp, 10);
289 if (*endp != 0) {
290 syslog(LOG_WARNING,
291 "%s line %d: invalid maxtimeout %s",
292 infile, (int)line, arg);
293 continue;
294 }
295 if (timeout < 30) {
296 syslog(LOG_WARNING,
297 "%s line %d: maxtimeout %d < 30 seconds",
298 infile, (int)line, timeout);
299 continue;
300 }
301 if (timeout < curclass.timeout) {
302 syslog(LOG_WARNING,
303 "%s line %d: maxtimeout %d < timeout (%d)",
304 infile, (int)line, timeout,
305 curclass.timeout);
306 continue;
307 }
308 curclass.maxtimeout = timeout;
309
310 } else if (strcasecmp(word, "modify") == 0) {
311 if (none ||
312 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
313 curclass.modify = 0;
314 else
315 curclass.modify = 1;
316
317 } else if (strcasecmp(word, "motd") == 0) {
318 if (none || EMPTYSTR(arg))
319 arg = NULL;
320 else
321 arg = xstrdup(arg);
322 REASSIGN(curclass.motd, arg);
323
324
325 } else if (strcasecmp(word, "notify") == 0) {
326 if (none || EMPTYSTR(arg))
327 arg = NULL;
328 else
329 arg = xstrdup(arg);
330 REASSIGN(curclass.notify, arg);
331
332 } else if (strcasecmp(word, "passive") == 0) {
333 if (none ||
334 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
335 curclass.passive = 0;
336 else
337 curclass.passive = 1;
338
339 } else if (strcasecmp(word, "portrange") == 0) {
340 int minport, maxport;
341 char *min, *max;
342
343 if (none) {
344 curclass.portmin = 0;
345 curclass.portmax = 0;
346 continue;
347 }
348 if (EMPTYSTR(arg))
349 continue;
350 min = arg;
351 NEXTWORD(p, max);
352 if (EMPTYSTR(max)) {
353 syslog(LOG_WARNING,
354 "%s line %d: missing maxport argument",
355 infile, (int)line);
356 continue;
357 }
358 minport = (int)strtol(min, &endp, 10);
359 if (*endp != 0 || minport < IPPORT_RESERVED ||
360 minport > IPPORT_ANONMAX) {
361 syslog(LOG_WARNING,
362 "%s line %d: invalid minport %s",
363 infile, (int)line, min);
364 continue;
365 }
366 maxport = (int)strtol(max, &endp, 10);
367 if (*endp != 0 || maxport < IPPORT_RESERVED ||
368 maxport > IPPORT_ANONMAX) {
369 syslog(LOG_WARNING,
370 "%s line %d: invalid maxport %s",
371 infile, (int)line, max);
372 continue;
373 }
374 if (minport >= maxport) {
375 syslog(LOG_WARNING,
376 "%s line %d: minport %d >= maxport %d",
377 infile, (int)line, minport, maxport);
378 continue;
379 }
380 curclass.portmin = minport;
381 curclass.portmax = maxport;
382
383 } else if (strcasecmp(word, "rateget") == 0) {
384 if (none || EMPTYSTR(arg))
385 continue;
386 rate = strsuftoi(arg);
387 if (rate == -1) {
388 syslog(LOG_WARNING,
389 "%s line %d: invalid rateget %s",
390 infile, (int)line, arg);
391 continue;
392 }
393 curclass.maxrateget = rate;
394 curclass.rateget = rate;
395
396 } else if (strcasecmp(word, "rateput") == 0) {
397 if (none || EMPTYSTR(arg))
398 continue;
399 rate = strsuftoi(arg);
400 if (rate == -1) {
401 syslog(LOG_WARNING,
402 "%s line %d: invalid rateput %s",
403 infile, (int)line, arg);
404 continue;
405 }
406 curclass.maxrateput = rate;
407 curclass.rateput = rate;
408
409 } else if (strcasecmp(word, "timeout") == 0) {
410 if (none || EMPTYSTR(arg))
411 continue;
412 timeout = (unsigned int)strtoul(arg, &endp, 10);
413 if (*endp != 0) {
414 syslog(LOG_WARNING,
415 "%s line %d: invalid timeout %s",
416 infile, (int)line, arg);
417 continue;
418 }
419 if (timeout < 30) {
420 syslog(LOG_WARNING,
421 "%s line %d: timeout %d < 30 seconds",
422 infile, (int)line, timeout);
423 continue;
424 }
425 if (timeout > curclass.maxtimeout) {
426 syslog(LOG_WARNING,
427 "%s line %d: timeout %d > maxtimeout (%d)",
428 infile, (int)line, timeout,
429 curclass.maxtimeout);
430 continue;
431 }
432 curclass.timeout = timeout;
433
434 } else if (strcasecmp(word, "template") == 0) {
435 if (none)
436 continue;
437 REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
438
439 } else if (strcasecmp(word, "umask") == 0) {
440 mode_t umask;
441
442 if (none || EMPTYSTR(arg))
443 continue;
444 umask = (mode_t)strtoul(arg, &endp, 8);
445 if (*endp != 0 || umask > 0777) {
446 syslog(LOG_WARNING,
447 "%s line %d: invalid umask %s",
448 infile, (int)line, arg);
449 continue;
450 }
451 curclass.umask = umask;
452
453 } else if (strcasecmp(word, "upload") == 0) {
454 if (none ||
455 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
456 curclass.modify = 0;
457 curclass.upload = 0;
458 } else
459 curclass.upload = 1;
460
461 } else {
462 syslog(LOG_WARNING,
463 "%s line %d: unknown directive '%s'",
464 infile, (int)line, word);
465 continue;
466 }
467 }
468 REASSIGN(template, NULL);
469 fclose(f);
470 }
471
472 /*
473 * Show file listed in curclass.display first time in, and list all the
474 * files named in curclass.notify in the current directory. Send back
475 * responses with the prefix `code' + "-".
476 */
477 void
478 show_chdir_messages(int code)
479 {
480 static StringList *slist = NULL;
481
482 struct stat st;
483 struct tm *t;
484 glob_t gl;
485 time_t now, then;
486 int age;
487 char cwd[MAXPATHLEN];
488 char *cp, **rlist;
489
490 if (quietmessages)
491 return;
492
493 /* Setup list for directory cache */
494 if (slist == NULL)
495 slist = sl_init();
496 if (slist == NULL) {
497 syslog(LOG_WARNING, "can't allocate memory for stringlist");
498 return;
499 }
500
501 /* Check if this directory has already been visited */
502 if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
503 syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
504 return;
505 }
506 if (sl_find(slist, cwd) != NULL)
507 return;
508
509 cp = xstrdup(cwd);
510 if (sl_add(slist, cp) == -1)
511 syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
512
513 /* First check for a display file */
514 (void)display_file(curclass.display, code);
515
516 /* Now see if there are any notify files */
517 if (EMPTYSTR(curclass.notify))
518 return;
519
520 if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
521 return;
522 time(&now);
523 for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
524 if (stat(*rlist, &st) != 0)
525 continue;
526 if (!S_ISREG(st.st_mode))
527 continue;
528 then = st.st_mtime;
529 if (code != 0) {
530 reply(-code, "%s", "");
531 code = 0;
532 }
533 reply(-code, "Please read the file %s", *rlist);
534 t = localtime(&now);
535 age = 365 * t->tm_year + t->tm_yday;
536 t = localtime(&then);
537 age -= 365 * t->tm_year + t->tm_yday;
538 reply(-code, " it was last modified on %.24s - %d day%s ago",
539 ctime(&then), age, PLURAL(age));
540 }
541 globfree(&gl);
542 }
543
544 int
545 display_file(const char *file, int code)
546 {
547 FILE *f;
548 char *buf, *p, *cwd;
549 size_t len;
550 time_t now;
551
552 if (quietmessages)
553 return (0);
554
555 if (EMPTYSTR(file))
556 return(0);
557 if ((f = fopen(file, "r")) == NULL)
558 return (0);
559 reply(-code, "%s", "");
560
561 for (;
562 (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
563 if (len > 0)
564 if (buf[len - 1] == '\n')
565 buf[--len] = '\0';
566 cprintf(stdout, " ");
567
568 for (p = buf; *p; p++) {
569 if (*p == '%') {
570 p++;
571 switch (*p) {
572
573 case 'c':
574 cprintf(stdout, "%s",
575 curclass.classname ?
576 curclass.classname : "<unknown>");
577 break;
578
579 case 'C':
580 if (getcwd(cwd, sizeof(cwd)-1) == NULL){
581 syslog(LOG_WARNING,
582 "can't getcwd: %s",
583 strerror(errno));
584 continue;
585 }
586 cprintf(stdout, "%s", cwd);
587 break;
588
589 case 'E':
590 /* XXXX email address */
591 break;
592
593 case 'L':
594 cprintf(stdout, "%s", hostname);
595 break;
596
597 case 'M':
598 if (curclass.limit == -1)
599 cprintf(stdout, "unlimited");
600 else
601 cprintf(stdout, "%d",
602 curclass.limit);
603 break;
604
605 case 'N':
606 if (connections > 0)
607 cprintf(stdout, "%d",
608 connections);
609 break;
610
611 case 'R':
612 cprintf(stdout, "%s", remotehost);
613 break;
614
615 case 'T':
616 now = time(NULL);
617 cprintf(stdout, "%.24s", ctime(&now));
618 break;
619
620 case 'U':
621 cprintf(stdout, "%s",
622 pw ? pw->pw_name : "<unknown>");
623 break;
624
625 case '%':
626 CPUTC('%', stdout);
627 break;
628
629 }
630 } else
631 CPUTC(*p, stdout);
632 }
633 cprintf(stdout, "\r\n");
634 }
635
636 (void)fflush(stdout);
637 (void)fclose(f);
638 return (1);
639 }
640
641 /*
642 * Parse src, expanding '%' escapes, into dst (which must be at least
643 * MAXPATHLEN long).
644 */
645 void
646 format_path(char *dst, const char *src)
647 {
648 size_t len;
649 const char *p;
650
651 dst[0] = '\0';
652 len = 0;
653 if (src == NULL)
654 return;
655
656 for (p = src; *p && len < MAXPATHLEN; p++) {
657 if (*p == '%') {
658 p++;
659 switch (*p) {
660
661 case 'c':
662 len += strlcpy(dst + len, curclass.classname,
663 MAXPATHLEN - len);
664 break;
665
666 case 'd':
667 len += strlcpy(dst + len, pw->pw_dir,
668 MAXPATHLEN - len);
669 break;
670
671 case 'u':
672 len += strlcpy(dst + len, pw->pw_name,
673 MAXPATHLEN - len);
674 break;
675
676 case '%':
677 dst[len++] = '%';
678 break;
679
680 }
681 } else
682 dst[len++] = *p;
683 }
684 if (len < MAXPATHLEN)
685 dst[len] = '\0';
686 dst[MAXPATHLEN - 1] = '\0';
687 }
688
689 /*
690 * Find s2 at the end of s1. If found, return a string up to (but
691 * not including) s2, otherwise returns NULL.
692 */
693 static char *
694 strend(const char *s1, char *s2)
695 {
696 static char buf[MAXPATHLEN];
697
698 char *start;
699 size_t l1, l2;
700
701 l1 = strlen(s1);
702 l2 = strlen(s2);
703
704 if (l2 >= l1)
705 return(NULL);
706
707 strlcpy(buf, s1, sizeof(buf));
708 start = buf + (l1 - l2);
709
710 if (strcmp(start, s2) == 0) {
711 *start = '\0';
712 return(buf);
713 } else
714 return(NULL);
715 }
716
717 static int
718 filetypematch(char *types, int mode)
719 {
720 for ( ; types[0] != '\0'; types++)
721 switch (*types) {
722 case 'd':
723 if (S_ISDIR(mode))
724 return(1);
725 break;
726 case 'f':
727 if (S_ISREG(mode))
728 return(1);
729 break;
730 }
731 return(0);
732 }
733
734 /*
735 * Look for a conversion. If we succeed, return a pointer to the
736 * command to execute for the conversion.
737 *
738 * The command is stored in a static array so there's no memory
739 * leak problems, and not too much to change in ftpd.c. This
740 * routine doesn't need to be re-entrant unless we start using a
741 * multi-threaded ftpd, and that's not likely for a while...
742 */
743 char **
744 do_conversion(const char *fname)
745 {
746 struct ftpconv *cp;
747 struct stat st;
748 int o_errno;
749 char *base = NULL;
750 char *cmd, *p, *lp, **argv;
751 StringList *sl;
752
753 o_errno = errno;
754 sl = NULL;
755 cmd = NULL;
756 for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
757 if (cp->suffix == NULL) {
758 syslog(LOG_WARNING,
759 "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
760 continue;
761 }
762 if ((base = strend(fname, cp->suffix)) == NULL)
763 continue;
764 if (cp->types == NULL || cp->disable == NULL ||
765 cp->command == NULL)
766 continue;
767 /* Is it enabled? */
768 if (strcmp(cp->disable, ".") != 0 &&
769 stat(cp->disable, &st) == 0)
770 continue;
771 /* Does the base exist? */
772 if (stat(base, &st) < 0)
773 continue;
774 /* Is the file type ok */
775 if (!filetypematch(cp->types, st.st_mode))
776 continue;
777 break; /* "We have a winner!" */
778 }
779
780 /* If we got through the list, no conversion */
781 if (cp == NULL)
782 goto cleanup_do_conv;
783
784 /* Split up command into an argv */
785 if ((sl = sl_init()) == NULL)
786 goto cleanup_do_conv;
787 cmd = xstrdup(cp->command);
788 p = cmd;
789 while (p) {
790 NEXTWORD(p, lp);
791 if (strcmp(lp, "%s") == 0)
792 lp = base;
793 if (sl_add(sl, xstrdup(lp)) == -1)
794 goto cleanup_do_conv;
795 }
796
797 if (sl_add(sl, NULL) == -1)
798 goto cleanup_do_conv;
799 argv = sl->sl_str;
800 free(cmd);
801 free(sl);
802 return(argv);
803
804 cleanup_do_conv:
805 if (sl)
806 sl_free(sl, 1);
807 free(cmd);
808 errno = o_errno;
809 return(NULL);
810 }
811
812 /*
813 * Convert the string `arg' to an int, which may have an optional SI suffix
814 * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
815 */
816 int
817 strsuftoi(const char *arg)
818 {
819 char *cp;
820 long val;
821
822 if (!isdigit((unsigned char)arg[0]))
823 return (-1);
824
825 val = strtol(arg, &cp, 10);
826 if (cp != NULL) {
827 if (cp[0] != '\0' && cp[1] != '\0')
828 return (-1);
829 switch (tolower((unsigned char)cp[0])) {
830 case '\0':
831 case 'b':
832 break;
833 case 'k':
834 val <<= 10;
835 break;
836 case 'm':
837 val <<= 20;
838 break;
839 case 'g':
840 val <<= 30;
841 break;
842 default:
843 return (-1);
844 }
845 }
846 if (val < 0 || val > INT_MAX)
847 return (-1);
848
849 return (val);
850 }
851
852 /*
853 * Count the number of current connections, reading from
854 * /var/run/ftpd.pids-<class>
855 * Does a kill -0 on each pid in that file, and only counts
856 * processes that exist (or frees the slot if it doesn't).
857 * Adds getpid() to the first free slot. Truncates the file
858 * if possible.
859 */
860 void
861 count_users(void)
862 {
863 char fn[MAXPATHLEN];
864 int fd, i, last;
865 size_t count;
866 pid_t *pids, mypid;
867 struct stat sb;
868
869 (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
870 (void)strlcat(fn, curclass.classname, sizeof(fn));
871 pids = NULL;
872 connections = 1;
873
874 if ((fd = open(fn, O_RDWR | O_CREAT, 0600)) == -1)
875 return;
876 if (lockf(fd, F_TLOCK, 0) == -1)
877 goto cleanup_count;
878 if (fstat(fd, &sb) == -1)
879 goto cleanup_count;
880 if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
881 goto cleanup_count;
882 count = read(fd, pids, sb.st_size);
883 if (count < 0 || count != sb.st_size)
884 goto cleanup_count;
885 count /= sizeof(pid_t);
886 mypid = getpid();
887 last = 0;
888 for (i = 0; i < count; i++) {
889 if (pids[i] == 0)
890 continue;
891 if (kill(pids[i], 0) == -1 && errno != EPERM) {
892 if (mypid != 0) {
893 pids[i] = mypid;
894 mypid = 0;
895 last = i;
896 }
897 } else {
898 connections++;
899 last = i;
900 }
901 }
902 if (mypid != 0) {
903 if (pids[last] != 0)
904 last++;
905 pids[last] = mypid;
906 }
907 count = (last + 1) * sizeof(pid_t);
908 if (lseek(fd, 0, SEEK_SET) == -1)
909 goto cleanup_count;
910 if (write(fd, pids, count) == -1)
911 goto cleanup_count;
912 (void)ftruncate(fd, count);
913
914 cleanup_count:
915 if (lseek(fd, 0, SEEK_SET) != -1)
916 (void)lockf(fd, F_ULOCK, 0);
917 close(fd);
918 REASSIGN(pids, NULL);
919 }
920