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