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