Debian Codegraph @

ftpd/ftpcmd.y

raw
1 /*
2 Copyright (C) 1994-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1985, 1988, 1993, 1994
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * Grammar for FTP commands:
50 *
51 * See RFC 959, RFC 1639, RFC 2389, RFC 2428,
52 * and RFC 3659 (MDTM, REST, SIZE).
53 *
54 * Security related details:
55 *
56 * PORT, EPRT, and LPRT are only allowed for
57 * target ports greater than IPPORT_RESERVED.
58 * In addition, the network address of the data
59 * connection must be identical to the address
60 * of the standing control connection. These
61 * have bearing on RFC 2577, sections 3 and 4.
62
63 * TODO: Update with RFC 3659 (MLST, MLSD).
64 *
65 * TODO: RFC 2428 (EPSV ALL).
66 *
67 * FIXME: Rewrite with GNU standard formatting. Legacy code is changed!
68 */
69
70 %{
71
72 #include <config.h>
73
74 #include <sys/types.h>
75 #include <sys/param.h>
76 #include <sys/socket.h>
77 #include <sys/stat.h>
78
79 #include <netinet/in.h>
80 #include <netdb.h>
81 #include <arpa/ftp.h>
82
83 #include <ctype.h>
84 #include <errno.h>
85 #include <pwd.h>
86 #include <setjmp.h>
87 #include <signal.h>
88 #ifdef HAVE_INTTYPES_H
89 # include <inttypes.h> /* strtoimax */
90 #elif defined HAVE_STDINT_H
91 # include <stdint.h>
92 #endif
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <syslog.h>
97 #include <sys/time.h>
98 #include <time.h>
99 #include <unistd.h>
100 #include <limits.h>
101 #include <sys/utsname.h>
102 /* Include glob.h last, because it may define "const" which breaks
103 system headers on some platforms. */
104 #include <glob.h>
105
106 #include "extern.h"
107
108 off_t restart_point;
109
110 static char cbuf[512]; /* Command Buffer. */
111 static char *fromname;
112 static int cmd_type;
113 static int cmd_form;
114 static int cmd_bytesz;
115
116 struct tab
117 {
118 const char *name;
119 short token;
120 short state;
121 short implemented; /* 1 if command is implemented */
122 const char *help;
123 };
124
125 static struct tab cmdtab[];
126 static struct tab sitetab[];
127 static char *extlist[];
128 static char *copy (char *);
129 static void help (struct tab *, char *);
130 static struct tab *lookup (struct tab *, char *);
131 static void sizecmd (char *);
132 static int yylex (void);
133 static void yyerror (const char *s);
134
135 %}
136
137 %union {
138 intmax_t i;
139 char *s;
140 }
141
142 %token
143 A B C E F I
144 L N P R S T
145
146 SP CRLF COMMA
147
148 USER PASS ACCT REIN QUIT PORT
149 PASV TYPE STRU MODE RETR STOR
150 APPE MLFL MAIL MSND MSOM MSAM
151 MRSQ MRCP ALLO REST RNFR RNTO
152 ABOR DELE CWD LIST NLST SITE
153 STAT HELP NOOP MKD RMD PWD
154 CDUP STOU SMNT SYST SIZE MDTM
155
156 FEAT OPTS
157
158 EPRT EPSV LPRT LPSV
159
160 ADAT AUTH CCC CONF ENC MIC
161 PBSZ PROT
162
163 UMASK IDLE CHMOD
164
165 LEXERR
166
167 %token <s> STRING
168 %token <i> NUMBER CHAR
169
170 %type <i> check_login octal_number byte_size
171 %type <i> struct_code mode_code type_code form_code
172 %type <s> pathstring pathname password username
173 %type <i> host_port net_proto tcp_port long_host_port
174 %type <s> net_addr
175
176 %start cmd_list
177
178 %%
179
180 cmd_list
181 : /* empty */
182 | cmd_list cmd
183 {
184 free (fromname);
185 fromname = (char *) 0;
186 restart_point = (off_t) 0;
187 }
188 | cmd_list rcmd
189 ;
190
191 cmd
192 : USER SP username CRLF
193 {
194 user ($3);
195 free ($3);
196 }
197 | PASS SP password CRLF
198 {
199 pass ($3);
200 memset ($3, 0, strlen ($3));
201 free ($3);
202 }
203 | PORT check_login SP host_port CRLF
204 {
205 if ($2)
206 {
207 if ($4
208 && ((his_addr.ss_family == AF_INET
209 && memcmp (&((struct sockaddr_in *) &his_addr)->sin_addr,
210 &((struct sockaddr_in *) &data_dest)->sin_addr,
211 sizeof (struct in_addr))
212 == 0
213 && ntohs (((struct sockaddr_in *) &data_dest)->sin_port)
214 > IPPORT_RESERVED)
215 ||
216 (his_addr.ss_family == AF_INET6
217 && memcmp (&((struct sockaddr_in6 *) &his_addr)->sin6_addr,
218 &((struct sockaddr_in6 *) &data_dest)->sin6_addr,
219 sizeof (struct in6_addr))
220 == 0
221 && ntohs (((struct sockaddr_in6 *) &data_dest)->sin6_port)
222 > IPPORT_RESERVED)
223 )
224 )
225 {
226 usedefault = 0;
227 if (pdata >= 0)
228 {
229 close (pdata);
230 pdata = -1;
231 }
232 reply (200, "PORT command successful.");
233 }
234 else
235 {
236 usedefault = 1;
237 memset (&data_dest, 0, sizeof (data_dest));
238 reply (500, "Illegal PORT Command");
239 }
240 }
241 }
242 | PASV check_login CRLF
243 {
244 if ($2)
245 passive (PASSIVE_PASV, AF_INET);
246 }
247 | TYPE SP type_code CRLF
248 {
249 switch (cmd_type)
250 {
251 case TYPE_A:
252 if (cmd_form == FORM_N)
253 {
254 reply (200, "Type set to A.");
255 type = cmd_type;
256 form = cmd_form;
257 }
258 else
259 reply (504, "Form must be N.");
260 break;
261
262 case TYPE_E:
263 reply (504, "Type E not implemented.");
264 break;
265
266 case TYPE_I:
267 reply (200, "Type set to I.");
268 type = cmd_type;
269 break;
270
271 case TYPE_L:
272 if (cmd_bytesz == 8)
273 {
274 reply (200, "Type set to L (byte size 8).");
275 type = cmd_type;
276 }
277 else
278 reply (504, "Byte size must be 8.");
279 }
280 }
281 | STRU SP struct_code CRLF
282 {
283 switch ($3)
284 {
285 case STRU_F:
286 reply (200, "STRU F ok.");
287 break;
288
289 default:
290 reply (504, "Unimplemented STRU type.");
291 }
292 }
293 | MODE SP mode_code CRLF
294 {
295 switch ($3)
296 {
297 case MODE_S:
298 reply (200, "MODE S ok.");
299 break;
300
301 default:
302 reply (502, "Unimplemented MODE type.");
303 }
304 }
305 | ALLO SP NUMBER CRLF
306 {
307 reply (202, "ALLO command ignored.");
308 }
309 | ALLO SP NUMBER SP R SP NUMBER CRLF
310 {
311 reply (202, "ALLO command ignored.");
312 }
313 | RETR check_login SP pathname CRLF
314 {
315 if ($2 && $4 != NULL)
316 retrieve ((char *) 0, $4);
317 free ($4);
318 }
319 | STOR check_login SP pathname CRLF
320 {
321 if ($2 && $4 != NULL)
322 store ($4, "w", 0);
323 free ($4);
324 }
325 | APPE check_login SP pathname CRLF
326 {
327 if ($2 && $4 != NULL)
328 store ($4, "a", 0);
329 free ($4);
330 }
331 | NLST check_login CRLF
332 {
333 if ($2)
334 send_file_list (".");
335 }
336 | NLST check_login SP STRING CRLF
337 {
338 if ($2 && $4 != NULL)
339 send_file_list ($4);
340 free ($4);
341 }
342 | LIST check_login CRLF
343 {
344 if ($2)
345 retrieve ("/bin/ls -lgA", "");
346 }
347 | LIST check_login SP pathname CRLF
348 {
349 if ($2 && $4 != NULL)
350 retrieve ("/bin/ls -lgA %s", $4);
351 free ($4);
352 }
353 | STAT check_login SP pathname CRLF
354 {
355 if ($2 && $4 != NULL)
356 statfilecmd ($4);
357 free ($4);
358 }
359 | STAT CRLF
360 {
361 statcmd ();
362 }
363 | DELE check_login SP pathname CRLF
364 {
365 if ($2 && $4 != NULL)
366 delete ($4);
367 free ($4);
368 }
369 | RNTO check_login SP pathname CRLF
370 {
371 if ($2)
372 {
373 if (fromname)
374 {
375 renamecmd (fromname, $4);
376 free (fromname);
377 fromname = (char *) 0;
378 }
379 else
380 reply (503, "Bad sequence of commands.");
381 }
382 free ($4);
383 }
384 | ABOR CRLF
385 {
386 reply (225, "ABOR command successful.");
387 }
388 | CWD check_login CRLF
389 {
390 if ($2)
391 cwd (cred.homedir);
392 }
393 | CWD check_login SP pathname CRLF
394 {
395 if ($2 && $4 != NULL)
396 cwd ($4);
397 free ($4);
398 }
399 | HELP CRLF
400 {
401 help (cmdtab, (char *) 0);
402 }
403 | HELP SP STRING CRLF
404 {
405 char *cp = $3;
406
407 if (strncasecmp (cp, "SITE", 4) == 0)
408 {
409 cp = $3 + 4;
410 if (*cp == ' ')
411 cp++;
412 if (*cp)
413 help (sitetab, cp);
414 else
415 help (sitetab, (char *) 0);
416 }
417 else
418 help (cmdtab, $3);
419 free ($3);
420 }
421 | NOOP CRLF
422 {
423 reply (200, "NOOP command successful.");
424 }
425 | MKD check_login SP pathname CRLF
426 {
427 if ($2 && $4 != NULL)
428 makedir ($4);
429 free ($4);
430 }
431 | RMD check_login SP pathname CRLF
432 {
433 if ($2 && $4 != NULL)
434 removedir ($4);
435 free ($4);
436 }
437 | PWD check_login CRLF
438 {
439 if ($2)
440 pwd ();
441 }
442 | CDUP check_login CRLF
443 {
444 if ($2)
445 cwd ("..");
446 }
447 | FEAT check_login CRLF
448 {
449 if ($2)
450 {
451 char **name;
452
453 lreply (211, "Supported extensions:");
454 for (name = extlist; *name; name++)
455 printf (" %s\r\n", *name);
456 reply (211, "End");
457 }
458 }
459 /* Catch all variants of the above FEAT, and reject them. */
460 | FEAT check_login SP STRING CRLF
461 {
462 if ($2)
463 {
464 reply (501, "Not accepting arguments.");
465 free ($4);
466 }
467 }
468 /* Changeable behaviour is yet to be implemented,
469 * so OPTS is a no-op for the time being. It is
470 * mandatory by RFC 2389, since FEAT now exists.
471 */
472 | OPTS check_login CRLF
473 {
474 if ($2)
475 {
476 reply (501, "Must have an argument.");
477 }
478 }
479 | OPTS check_login SP STRING CRLF
480 {
481 if ($2)
482 {
483 reply (501, "No options are available.");
484 free ($4);
485 }
486 }
487 | SITE SP HELP CRLF
488 {
489 help (sitetab, (char *) 0);
490 }
491 | SITE SP HELP SP STRING CRLF
492 {
493 help (sitetab, $5);
494 free ($5);
495 }
496 | SITE SP UMASK check_login CRLF
497 {
498 int oldmask;
499
500 if ($4)
501 {
502 oldmask = umask (0);
503 umask (oldmask);
504 reply (200, "Current UMASK is %03o", oldmask);
505 }
506 }
507 | SITE SP UMASK check_login SP octal_number CRLF
508 {
509 int oldmask;
510
511 if ($4)
512 {
513 if (($6 == -1) || ($6 > 0777))
514 reply (501, "Bad UMASK value");
515 else
516 {
517 oldmask = umask ($6);
518 reply (200, "UMASK set to %03o (was %03o)",
519 $6, oldmask);
520 }
521 }
522 }
523 | SITE SP CHMOD check_login SP octal_number SP pathname CRLF
524 {
525 if ($4 && ($8 != NULL))
526 {
527 if ($6 > 0777)
528 reply (501,
529 "CHMOD: Mode value must be between 0 and 0777");
530 else if (chmod ($8, $6) < 0)
531 perror_reply (550, $8);
532 else
533 reply (200, "CHMOD command successful.");
534 }
535 free ($8);
536 }
537 | SITE SP IDLE CRLF
538 {
539 reply (200,
540 "Current IDLE time limit is %d seconds; max %d",
541 timeout, maxtimeout);
542 }
543 | SITE SP IDLE check_login SP NUMBER CRLF
544 {
545 if ($4)
546 {
547 if ($6 < 30 || $6 > maxtimeout)
548 reply (501,
549 "Maximum IDLE time must be between 30 and %d seconds",
550 maxtimeout);
551 else
552 {
553 timeout = $6;
554 alarm ((unsigned) timeout);
555 reply (200,
556 "Maximum IDLE time set to %d seconds",
557 timeout);
558 }
559 }
560 }
561 | STOU check_login SP pathname CRLF
562 {
563 if ($2 && $4 != NULL)
564 store ($4, "w", 1);
565 free ($4);
566 }
567 | SYST CRLF
568 {
569 const char *sys_type; /* Official rfc-defined os type. */
570 char *version = 0; /* A more specific type. */
571
572 #ifdef HAVE_UNAME
573 struct utsname u;
574
575 if (uname (&u) >= 0)
576 {
577 version = malloc (strlen (u.sysname) + 1
578 + strlen (u.release) + 1);
579 if (version)
580 sprintf (version, "%s %s", u.sysname, u.release);
581 }
582 #else /* !HAVE_UNAME */
583 # ifdef BSD
584 version = "BSD";
585 # endif /* BSD */
586 #endif /* !HAVE_UNAME */
587
588 #if defined unix || defined __unix || defined __unix__
589 sys_type = "UNIX";
590 #else
591 sys_type = "UNKNOWN";
592 #endif
593
594 if (!no_version && version)
595 reply (215, "%s Type: L%d Version: %s",
596 sys_type, CHAR_BIT, version);
597 else
598 reply (215, "%s Type: L%d", sys_type, CHAR_BIT);
599
600 #ifdef HAVE_UNAME
601 free (version);
602 #endif
603 }
604
605 /*
606 * SIZE is in RFC 3659.
607 *
608 * Return size of file in a format suitable for
609 * using with RESTART (we just count bytes).
610 */
611 | SIZE check_login SP pathname CRLF
612 {
613 if ($2 && $4 != NULL)
614 sizecmd ($4);
615 free ($4);
616 }
617
618 /*
619 * MDTM is in RFC 3659.
620 *
621 * Return modification time of file as an ISO 3307
622 * style time. E.g. YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx
623 * where xxx is the fractional second (of any precision,
624 * not necessarily 3 digits)
625 */
626 | MDTM check_login SP pathname CRLF
627 {
628 if ($2 && $4 != NULL)
629 {
630 struct stat stbuf;
631
632 if (stat ($4, &stbuf) < 0)
633 reply (550, "%s: %s", $4, strerror (errno));
634 else if (!S_ISREG (stbuf.st_mode))
635 reply (550, "%s: not a plain file.", $4);
636 else
637 {
638 struct tm *t;
639
640 t = gmtime (&stbuf.st_mtime);
641 reply (213,
642 "%04d%02d%02d%02d%02d%02d",
643 1900 + t->tm_year, t->tm_mon+1,
644 t->tm_mday, t->tm_hour,
645 t->tm_min, t->tm_sec);
646 }
647 }
648 free ($4);
649 }
650
651 /*
652 * EPRT is in RFC 2428.
653 */
654 | EPRT check_login SP CHAR net_proto CHAR net_addr CHAR tcp_port CHAR CRLF
655 {
656 usedefault = 0;
657 if (pdata >= 0)
658 {
659 close (pdata);
660 pdata = -1;
661 }
662 /* A first sanity check. */
663 if ($2 /* valid login */
664 && ($5 > 0) /* valid protocols */
665 && ($4 > 32 && $4 < 127) /* legal first delimiter */
666 /* identical delimiters */
667 && ($4 == $6 && $4 == $8 && $4 == $10))
668 {
669 /* We only accept connections using
670 * the same address family as is
671 * currently in use, unless we
672 * detect IPv4-mapped-to-IPv6.
673 */
674 if (his_addr.ss_family == $5
675 || ($5 == AF_INET6
676 && his_addr.ss_family == AF_INET)
677 || ($5 == AF_INET
678 && his_addr.ss_family == AF_INET6))
679 {
680 int err;
681 char p[8];
682 struct addrinfo hints, *res;
683
684 memset (&hints, 0, sizeof (hints));
685 snprintf (p, sizeof (p), "%jd", $9 & 0xffff);
686 hints.ai_family = $5;
687 hints.ai_socktype = SOCK_STREAM;
688 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
689
690 err = getaddrinfo ($7, p, &hints, &res);
691 if (err)
692 reply (500, "Illegal EPRT Command");
693 else if (/* sanity check */
694 (his_addr.ss_family == AF_INET
695 && memcmp (&((struct sockaddr_in *) &his_addr)->sin_addr,
696 &((struct sockaddr_in *) res->ai_addr)->sin_addr,
697 sizeof (struct in_addr))
698 == 0
699 && ntohs (((struct sockaddr_in *) res->ai_addr)->sin_port)
700 > IPPORT_RESERVED
701 )
702 ||
703 (his_addr.ss_family == AF_INET6
704 && memcmp (&((struct sockaddr_in6 *) &his_addr)->sin6_addr,
705 &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr,
706 sizeof (struct in6_addr))
707 == 0
708 && ntohs (((struct sockaddr_in6 *) res->ai_addr)->sin6_port)
709 > IPPORT_RESERVED
710 )
711 ||
712 (his_addr.ss_family == AF_INET
713 && res->ai_family == AF_INET6
714 && IN6_IS_ADDR_V4MAPPED (&((struct sockaddr_in6 *) res->ai_addr)->sin6_addr)
715 && memcmp (&((struct sockaddr_in *) &his_addr)->sin_addr,
716 &((struct in_addr *) &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr)[3],
717 sizeof (struct in_addr))
718 == 0
719 && ntohs (((struct sockaddr_in6 *) res->ai_addr)->sin6_port)
720 > IPPORT_RESERVED
721 )
722 ||
723 (his_addr.ss_family == AF_INET6
724 && res->ai_family == AF_INET
725 && IN6_IS_ADDR_V4MAPPED (&((struct sockaddr_in6 *) &his_addr)->sin6_addr)
726 && memcmp (&((struct in_addr *) &((struct sockaddr_in6 *) &his_addr)->sin6_addr)[3],
727 &((struct sockaddr_in *) res->ai_addr)->sin_addr,
728 sizeof (struct in_addr))
729 == 0
730 && ntohs (((struct sockaddr_in *) res->ai_addr)->sin_port)
731 > IPPORT_RESERVED
732 )
733 )
734 {
735 /* In the case of IPv4 mapped as IPv6,
736 * the addresses were proven to coincide,
737 * only the extraction remains.
738 * Since non-mapped is the standard,
739 * test that situation first.
740 */
741 if (his_addr.ss_family == res->ai_family)
742 {
743 memcpy (&data_dest, res->ai_addr,
744 res->ai_addrlen);
745 data_dest_len = res->ai_addrlen;
746 }
747 else if (his_addr.ss_family == AF_INET
748 && res->ai_family == AF_INET6)
749 {
750 /* `his_addr' contains the reduced
751 * IPv4 address.
752 */
753 memcpy (&data_dest, &his_addr,
754 sizeof (struct sockaddr_in));
755 data_dest_len =
756 sizeof (struct sockaddr_in);
757 ((struct sockaddr_in *) &data_dest)->sin_port =
758 ((struct sockaddr_in6 *) res->ai_addr)->sin6_port;
759 }
760 else
761 {
762 /* `res->ai_addr' contains the reduced
763 * IPv4 address, but the connection
764 * stands on `his_addr', which is
765 * an IPv4-to-IPv6-mapped address.
766 */
767 memcpy (&data_dest, &his_addr,
768 sizeof (struct sockaddr_in6));
769 data_dest_len =
770 sizeof (struct sockaddr_in6);
771 ((struct sockaddr_in6 *) &data_dest)->sin6_port =
772 ((struct sockaddr_in *) res->ai_addr)->sin_port;
773 }
774
775 freeaddrinfo (res);
776 reply (200, "EPRT command successful.");
777 }
778 else
779 {
780 /* failed identity check */
781 if (res)
782 freeaddrinfo (res);
783 reply (500, "Illegal EPRT Command");
784 }
785 }
786 else
787 /* Not fit for established connection. */
788 reply (522,
789 "Network protocol not supported, use (%d)",
790 ($5 == 1) ? 2 : 1);
791 }
792 else if ($2 && ($5 <= 0))
793 reply (522,
794 "Network protocol not supported, use (1,2)");
795 else if ($2)
796 /* Incorrect delimiters detected,
797 * the other conditions are met.
798 */
799 reply (500, "Illegal EPRT Command");
800 }
801
802 /*
803 * EPSV is in RFC 2428.
804 */
805 | EPSV check_login CRLF
806 {
807 if ($2)
808 passive (PASSIVE_EPSV, AF_UNSPEC);
809 }
810 | EPSV check_login SP net_proto CRLF
811 {
812 if ($2)
813 {
814 if ($4 > 0)
815 passive (PASSIVE_EPSV, $4);
816 else
817 reply (522,
818 "Network protocol not supported, use (1,2)");
819 }
820 }
821
822 /*
823 * LPRT is in RFC 1639.
824 */
825 | LPRT check_login SP long_host_port CRLF
826 {
827 if ($2)
828 {
829 if ($4 &&
830 ((his_addr.ss_family == AF_INET
831 && memcmp (&((struct sockaddr_in *) &his_addr)->sin_addr,
832 &((struct sockaddr_in *) &data_dest)->sin_addr,
833 sizeof (struct in_addr)) == 0
834 && ntohs (((struct sockaddr_in *) &data_dest)->sin_port)
835 > IPPORT_RESERVED)
836 ||
837 (his_addr.ss_family == AF_INET6
838 && memcmp (&((struct sockaddr_in6 *) &his_addr)->sin6_addr,
839 &((struct sockaddr_in6 *) &data_dest)->sin6_addr,
840 sizeof (struct in6_addr)) == 0
841 && ntohs (((struct sockaddr_in6 *) &data_dest)->sin6_port)
842 > IPPORT_RESERVED)
843 )
844 )
845 {
846 usedefault = 0;
847 if (pdata >= 0)
848 {
849 close (pdata);
850 pdata = -1;
851 }
852 reply (200, "LPRT command successful.");
853 }
854 else
855 {
856 usedefault = 1;
857 memset (&data_dest, 0, sizeof (data_dest));
858 reply (500, "Illegal LPRT Command");
859 }
860 } /* check_login */
861 }
862
863 /*
864 * LPSV is in RFC 1639.
865 */
866 | LPSV check_login CRLF
867 {
868 if ($2)
869 passive (PASSIVE_LPSV, 0 /* not used */);
870 }
871
872 | QUIT CRLF
873 {
874 reply (221, "Goodbye.");
875 dologout (0);
876 }
877 | error CRLF
878 {
879 yyerrok;
880 }
881 ;
882 rcmd
883 : RNFR check_login SP pathname CRLF
884 {
885 restart_point = (off_t) 0;
886 if ($2 && $4)
887 {
888 free (fromname);
889 fromname = renamefrom ($4);
890 }
891 if (fromname == (char *) 0 && $4)
892 free ($4);
893 }
894
895 /*
896 * REST is in RFC 3659.
897 */
898 | REST SP byte_size CRLF
899 {
900 free (fromname);
901 fromname = (char *) 0;
902 restart_point = $3;
903 reply (350, "Restarting at %jd. %s",
904 (intmax_t) restart_point,
905 "Send STORE or RETRIEVE to initiate transfer.");
906 }
907 ;
908
909 username
910 : STRING
911 ;
912
913 password
914 : /* empty */
915 {
916 $$ = (char *) calloc (1, sizeof (char));
917 }
918 | STRING
919 ;
920
921 byte_size
922 : NUMBER
923 ;
924
925 net_proto
926 : NUMBER
927 {
928 /* Rewrite as valid address family. */
929 if ($1 == 1)
930 $$ = AF_INET;
931 else if ($1 == 2)
932 $$ = AF_INET6;
933 else
934 $$ = -1; /* Invalid protocol. */
935 }
936 ;
937
938 tcp_port
939 : NUMBER
940 ;
941
942 net_addr
943 : STRING
944 ;
945
946 host_port
947 : NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
948 NUMBER COMMA NUMBER
949 {
950 int err;
951 char a[INET6_ADDRSTRLEN], p[8];
952 struct addrinfo hints, *res;
953
954 snprintf (a, sizeof (a), "%jd.%jd.%jd.%jd",
955 $1 & 0xff, $3 & 0xff,
956 $5 & 0xff, $7 & 0xff);
957 snprintf (p, sizeof (p), "%jd",
958 (($9 & 0xff) << 8) + ($11 & 0xff));
959 memset (&hints, 0, sizeof (hints));
960 hints.ai_family = his_addr.ss_family;
961 hints.ai_socktype = SOCK_STREAM;
962 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
963
964 if (his_addr.ss_family == AF_INET6)
965 {
966 /* IPv4 mapped to IPv6. */
967 hints.ai_family = AF_INET6;
968 #ifdef AI_V4MAPPED
969 hints.ai_flags |= AI_V4MAPPED;
970 #endif
971 snprintf (a, sizeof (a),
972 "::ffff:%jd.%jd.%jd.%jd",
973 $1 & 0xff, $3 & 0xff,
974 $5 & 0xff, $7 & 0xff);
975 }
976
977 err = getaddrinfo (a, p, &hints, &res);
978 if (err)
979 {
980 reply (550, "Address failure: %s,%s", a, p);
981 memset (&data_dest, 0, sizeof (data_dest));
982 data_dest_len = 0;
983 $$ = 0;
984 }
985 else
986 {
987 memcpy (&data_dest, res->ai_addr, res->ai_addrlen);
988 data_dest_len = res->ai_addrlen;
989 freeaddrinfo (res);
990 $$ = 1;
991 }
992 }
993 ;
994
995 long_host_port
996 : NUMBER COMMA NUMBER COMMA /* af, hal */
997 NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA /* h */
998 NUMBER COMMA NUMBER COMMA NUMBER /* pal, p */
999 {
1000 int err;
1001 char a[INET6_ADDRSTRLEN], p[8];
1002 struct addrinfo hints, *res;
1003
1004 /* Well formed input for IPv4? */
1005 if ($1 != 4 || $3 != 4 || $13 != 2
1006 || $5 < 0 || $5 > 255 || $7 < 0 || $7 > 255
1007 || $9 < 0 || $9 > 255 || $11 < 0 || $11 > 255
1008 || $15 < 0 || $15 > 255
1009 || $17 < 0 || $17 > 255)
1010 {
1011 reply (500, "Invalid address.");
1012 memset (&data_dest, 0, sizeof (data_dest));
1013 data_dest_len = 0;
1014 $$ = 0;
1015 }
1016 else
1017 {
1018 snprintf (a, sizeof (a), "%jd.%jd.%jd.%jd",
1019 $5, $7, $9, $11);
1020 snprintf (p, sizeof (p), "%jd", ($15 << 8) + $17);
1021
1022 memset (&hints, 0, sizeof (hints));
1023 hints.ai_family = his_addr.ss_family;
1024 hints.ai_socktype = SOCK_STREAM;
1025 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
1026
1027 if (his_addr.ss_family == AF_INET6)
1028 {
1029 /* IPv4 mapped to IPv6. */
1030 hints.ai_family = AF_INET6;
1031 #ifdef AI_V4MAPPED
1032 hints.ai_flags |= AI_V4MAPPED;
1033 #endif
1034 snprintf (a, sizeof (a),
1035 "::ffff:%jd.%jd.%jd.%jd",
1036 $5, $7, $9, $11);
1037 }
1038
1039 err = getaddrinfo (a, p, &hints, &res);
1040 if (err)
1041 {
1042 reply (550, "LPRT address failure: %s,%s",
1043 a, p);
1044 memset (&data_dest, 0, sizeof (data_dest));
1045 data_dest_len = 0;
1046 $$ = 0;
1047 }
1048 else
1049 {
1050 memcpy (&data_dest, res->ai_addr,
1051 res->ai_addrlen);
1052 data_dest_len = res->ai_addrlen;
1053 freeaddrinfo (res);
1054 $$ = 1;
1055 }
1056 }
1057 }
1058 | NUMBER COMMA NUMBER COMMA /* af, hal */
1059 NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA /* h */
1060 NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
1061 NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
1062 NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
1063 NUMBER COMMA NUMBER COMMA NUMBER /* pal, p */
1064 {
1065 int err;
1066 char a[INET6_ADDRSTRLEN], p[8];
1067 struct addrinfo hints, *res;
1068
1069 /* Well formed input for IPv6? */
1070 if ($1 != 6 || $3 != 16 || $37 != 2
1071 || $5 < 0 || $5 > 255 || $7 < 0 || $7 > 255
1072 || $9 < 0 || $9 > 255 || $11 < 0 || $11 > 255
1073 || $13 < 0 || $13 > 255 || $15 < 0 || $15 > 255
1074 || $17 < 0 || $17 > 255 || $19 < 0 || $19 > 255
1075 || $21 < 0 || $21 > 255 || $23 < 0 || $23 > 255
1076 || $25 < 0 || $25 > 255 || $27 < 0 || $27 > 255
1077 || $29 < 0 || $29 > 255 || $31 < 0 || $31 > 255
1078 || $33 < 0 || $33 > 255 || $35 < 0 || $35 > 255
1079 || $39 < 0 || $39 > 255 || $41 < 0 || $41 > 255)
1080 {
1081 reply (500, "Invalid address.");
1082 memset (&data_dest, 0, sizeof (data_dest));
1083 data_dest_len = 0;
1084 $$ = 0;
1085 }
1086 else
1087 {
1088 snprintf (a, sizeof (a),
1089 "%02jx%02jx:%02jx%02jx:"
1090 "%02jx%02jx:%02jx%02jx:"
1091 "%02jx%02jx:%02jx%02jx:"
1092 "%02jx%02jx:%02jx%02jx",
1093 $5, $7, $9, $11,
1094 $13, $15, $17, $19,
1095 $21, $23, $25, $27,
1096 $29, $31, $33, $35);
1097 snprintf (p, sizeof (p), "%jd",
1098 ($39 << 8) + $41);
1099
1100 memset (&hints, 0, sizeof (hints));
1101 hints.ai_family = his_addr.ss_family;
1102 hints.ai_socktype = SOCK_STREAM;
1103 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
1104
1105 err = getaddrinfo (a, p, &hints, &res);
1106 if (err)
1107 {
1108 reply (550, "LPRT address failure: %s,%s",
1109 a, p);
1110 memset (&data_dest, 0, sizeof (data_dest));
1111 data_dest_len = 0;
1112 $$ = 0;
1113 }
1114 else
1115 {
1116 memcpy (&data_dest, res->ai_addr,
1117 res->ai_addrlen);
1118 data_dest_len = res->ai_addrlen;
1119 freeaddrinfo (res);
1120 $$ = 1;
1121 }
1122 }
1123 }
1124 ;
1125
1126 form_code
1127 : N
1128 {
1129 $$ = FORM_N;
1130 }
1131 | T
1132 {
1133 $$ = FORM_T;
1134 }
1135 | C
1136 {
1137 $$ = FORM_C;
1138 }
1139 ;
1140
1141 type_code
1142 : A
1143 {
1144 cmd_type = TYPE_A;
1145 cmd_form = FORM_N;
1146 }
1147 | A SP form_code
1148 {
1149 cmd_type = TYPE_A;
1150 cmd_form = $3;
1151 }
1152 | E
1153 {
1154 cmd_type = TYPE_E;
1155 cmd_form = FORM_N;
1156 }
1157 | E SP form_code
1158 {
1159 cmd_type = TYPE_E;
1160 cmd_form = $3;
1161 }
1162 | I
1163 {
1164 cmd_type = TYPE_I;
1165 }
1166 | L
1167 {
1168 cmd_type = TYPE_L;
1169 cmd_bytesz = CHAR_BIT;
1170 }
1171 | L SP byte_size
1172 {
1173 cmd_type = TYPE_L;
1174 cmd_bytesz = $3;
1175 }
1176 /* this is for a bug in the BBN ftp */
1177 | L byte_size
1178 {
1179 cmd_type = TYPE_L;
1180 cmd_bytesz = $2;
1181 }
1182 ;
1183
1184 struct_code
1185 : F
1186 {
1187 $$ = STRU_F;
1188 }
1189 | R
1190 {
1191 $$ = STRU_R;
1192 }
1193 | P
1194 {
1195 $$ = STRU_P;
1196 }
1197 ;
1198
1199 mode_code
1200 : S
1201 {
1202 $$ = MODE_S;
1203 }
1204 | B
1205 {
1206 $$ = MODE_B;
1207 }
1208 | C
1209 {
1210 $$ = MODE_C;
1211 }
1212 ;
1213
1214 pathname
1215 : pathstring
1216 {
1217 /*
1218 * Problem: this production is used for all pathname
1219 * processing, but only gives a 550 error reply.
1220 * This is a valid reply in some cases but not in others.
1221 */
1222 if (cred.logged_in && $1 && *$1 == '~')
1223 {
1224 glob_t gl;
1225 int flags = GLOB_NOCHECK;
1226
1227 #ifdef GLOB_BRACE
1228 flags |= GLOB_BRACE;
1229 #endif
1230 #ifdef GLOB_QUOTE
1231 flags |= GLOB_QUOTE;
1232 #endif
1233 #ifdef GLOB_TILDE
1234 flags |= GLOB_TILDE;
1235 #endif
1236
1237 memset (&gl, 0, sizeof (gl));
1238 if (glob ($1, flags, NULL, &gl)
1239 || gl.gl_pathc == 0)
1240 {
1241 reply (550, "not found");
1242 $$ = NULL;
1243 }
1244 else
1245 $$ = strdup (gl.gl_pathv[0]);
1246
1247 globfree (&gl);
1248 free ($1);
1249 }
1250 else
1251 $$ = $1;
1252 }
1253 ;
1254
1255 pathstring
1256 : STRING
1257 ;
1258
1259 octal_number
1260 : NUMBER
1261 {
1262 int ret, dec, multby, digit;
1263
1264 /*
1265 * Convert a number that was read as decimal number
1266 * to what it would be if it had been read as octal.
1267 */
1268 dec = $1;
1269 multby = 1;
1270 ret = 0;
1271 while (dec)
1272 {
1273 digit = dec % 10;
1274 if (digit > 7)
1275 {
1276 ret = -1;
1277 break;
1278 }
1279 ret += digit * multby;
1280 multby *= 8;
1281 dec /= 10;
1282 }
1283 $$ = ret;
1284 }
1285 ;
1286
1287 check_login
1288 : /* empty */
1289 {
1290 if (cred.logged_in)
1291 $$ = 1;
1292 else
1293 {
1294 reply (530, "Please login with USER and PASS.");
1295 $$ = 0;
1296 }
1297 }
1298 ;
1299
1300 %%
1301
1302 #define CMD 0 /* beginning of command */
1303 #define ARGS 1 /* expect miscellaneous arguments */
1304 #define STR1 2 /* expect SP followed by STRING */
1305 #define STR2 3 /* expect STRING (must be STR2 + 1)*/
1306 #define OSTR 4 /* optional SP then STRING */
1307 #define ZSTR1 5 /* SP then optional STRING */
1308 #define ZSTR2 6 /* optional STRING after SP (must be ZSTR1 + 1) */
1309 #define SITECMD 7 /* SITE command */
1310 #define NSTR 8 /* Number followed by a string */
1311 #define DLIST 9 /* SP and delimited list for EPRT/EPSV */
1312
1313 static struct tab cmdtab[] = {
1314 /* In the order defined by RFC 959. See also RFC 1123. */
1315 /* Access control commands. */
1316 { "USER", USER, STR1, 1, "<sp> username" },
1317 { "PASS", PASS, ZSTR1, 1, "<sp> password" },
1318 { "ACCT", ACCT, STR1, 0, "(specify account)" },
1319 { "CWD", CWD, OSTR, 1, "[ <sp> directory-name ]" },
1320 { "CDUP", CDUP, ARGS, 1, "(change to parent directory)" },
1321 { "SMNT", SMNT, ARGS, 0, "(structure mount)" },
1322 { "REIN", REIN, ARGS, 0, "(reinitialize server state)" },
1323 { "QUIT", QUIT, ARGS, 1, "(terminate service)", },
1324 /* Transfer parameter commands. */
1325 { "PORT", PORT, ARGS, 1, "<sp> b0, b1, b2, b3, b4" },
1326 { "PASV", PASV, ARGS, 1, "(set server in passive mode)" },
1327 { "TYPE", TYPE, ARGS, 1, "<sp> [ A | E | I | L ]" },
1328 { "STRU", STRU, ARGS, 1, "(specify file structure)" },
1329 { "MODE", MODE, ARGS, 1, "(specify transfer mode)" },
1330 /* FTP service commands. */
1331 { "RETR", RETR, STR1, 1, "<sp> file-name" },
1332 { "STOR", STOR, STR1, 1, "<sp> file-name" },
1333 { "STOU", STOU, STR1, 1, "<sp> file-name" },
1334 { "APPE", APPE, STR1, 1, "<sp> file-name" },
1335 { "ALLO", ALLO, ARGS, 1, "allocate storage (vacuously)" },
1336 { "REST", REST, ARGS, 1, "<sp> offset (restart command)" },
1337 { "RNFR", RNFR, STR1, 1, "<sp> file-name" },
1338 { "RNTO", RNTO, STR1, 1, "<sp> file-name" },
1339 { "ABOR", ABOR, ARGS, 1, "(abort operation)" },
1340 { "DELE", DELE, STR1, 1, "<sp> file-name" },
1341 { "RMD", RMD, STR1, 1, "<sp> path-name" },
1342 { "MKD", MKD, STR1, 1, "<sp> path-name" },
1343 { "PWD", PWD, ARGS, 1, "(return current directory)" },
1344 { "LIST", LIST, OSTR, 1, "[ <sp> path-name ]" },
1345 { "NLST", NLST, OSTR, 1, "[ <sp> path-name ]" },
1346 { "SITE", SITE, SITECMD, 1, "site-cmd [ <sp> arguments ]" },
1347 { "SYST", SYST, ARGS, 1, "(get type of operating system)" },
1348 { "STAT", STAT, OSTR, 1, "[ <sp> path-name ]" },
1349 { "HELP", HELP, OSTR, 1, "[ <sp> <string> ]" },
1350 { "NOOP", NOOP, ARGS, 1, "" },
1351 /* Experimental commands, as mentioned in RFC 1123. Now obsolete. */
1352 { "XMKD", MKD, STR1, 1, "<sp> path-name" },
1353 { "XRMD", RMD, STR1, 1, "<sp> path-name" },
1354 { "XPWD", PWD, ARGS, 1, "(return current directory)" },
1355 { "XCUP", CDUP, ARGS, 1, "(change to parent directory)" },
1356 { "XCWD", CWD, OSTR, 1, "[ <sp> directory-name ]" },
1357 /* Commands in RFC 2389. */
1358 { "FEAT", FEAT, OSTR, 1, "(display command extensions)" },
1359 /* XXX: Replace OSTR once some functionality exists. */
1360 { "OPTS", OPTS, OSTR, 1, "<sp> cmd-name [ <sp> options ]" },
1361 /* Commands in RFC 3659. */
1362 { "SIZE", SIZE, OSTR, 1, "<sp> path-name" },
1363 { "MDTM", MDTM, OSTR, 1, "<sp> path-name" },
1364 /* Unimplemented, but reserved in RFC ???. */
1365 { "MLFL", MLFL, OSTR, 0, "(mail file)" },
1366 { "MAIL", MAIL, OSTR, 0, "(mail to user)" },
1367 { "MSND", MSND, OSTR, 0, "(mail send to terminal)" },
1368 { "MSOM", MSOM, OSTR, 0, "(mail send to terminal or mailbox)" },
1369 { "MSAM", MSAM, OSTR, 0, "(mail send to terminal and mailbox)" },
1370 { "MRSQ", MRSQ, OSTR, 0, "(mail recipient scheme question)" },
1371 { "MRCP", MRCP, STR1, 0, "(mail recipient)" },
1372 /* Extended addressing in RFC 2428. */
1373 { "EPRT", EPRT, DLIST, 1, "<sp> <d> proto <d> addr <d> port <d>" },
1374 { "EPSV", EPSV, ARGS, 1, "[ <sp> af ]" },
1375 /* Long addressing in RFC 1639. Obsoleted in RFC 5797. */
1376 { "LPRT", LPRT, ARGS, 1, "<sp> af,hal,h0..hn,2,p0,p1" },
1377 { "LPSV", LPSV, ARGS, 1, "(set server in long passive mode)" },
1378 /* Security extensions in RFC 2228. */
1379 { "ADAT", ADAT, OSTR, 0, "<sp> security-data" },
1380 { "AUTH", AUTH, OSTR, 0, "<sp> mechanism" },
1381 { "CCC", CCC, ARGS, 0, "(clear command channel)" },
1382 { "CONF", CONF, OSTR, 0, "<sp> confidential-msg" },
1383 { "ENC", ENC, OSTR, 0, "<sp> private-message" },
1384 { "MIC", MIC, OSTR, 0, "<sp> safe-message" },
1385 { "PBSZ", PBSZ, OSTR, 0, "<sp> buf-size" },
1386 { "PROT", PROT, OSTR, 0, "<sp> char" },
1387 /* End of list. */
1388 { NULL, 0, 0, 0, NULL }
1389 };
1390
1391 static struct tab sitetab[] = {
1392 { "CHMOD", CHMOD, NSTR, 1, "<sp> mode <sp> file-name" },
1393 { "HELP", HELP, OSTR, 1, "[ <sp> <string> ]" },
1394 { "IDLE", IDLE, ARGS, 1, "[ <sp> maximum-idle-time ]" },
1395 { "UMASK", UMASK, ARGS, 1, "[ <sp> umask ]" },
1396 { NULL, 0, 0, 0, NULL }
1397 };
1398
1399 /* Extensions beyond RFC 959 and RFC 2389. Ordered as implemented. */
1400 static char *extlist[] = {
1401 "MDTM", "SIZE", "REST STREAM",
1402 "EPRT", "EPSV", "LPRT", "LPSV",
1403 NULL };
1404
1405 static struct tab *
1406 lookup (struct tab *p, char *cmd)
1407 {
1408 for (; p->name != NULL; p++)
1409 if (strcmp (cmd, p->name) == 0)
1410 return (p);
1411 return (0);
1412 }
1413
1414 #include <arpa/telnet.h>
1415
1416 /*
1417 * getline - a hacked up version of fgets to ignore TELNET escape codes.
1418 */
1419 char *
1420 telnet_fgets (char *s, int n, FILE *iop)
1421 {
1422 int c;
1423 register char *cs;
1424
1425 cs = s;
1426 /* tmpline may contain saved command from urgent mode interruption */
1427 for (c = 0; tmpline[c] != '\0' && --n > 0; ++c)
1428 {
1429 *cs++ = tmpline[c];
1430 if (tmpline[c] == '\n')
1431 {
1432 *cs++ = '\0';
1433 if (debug)
1434 syslog (LOG_DEBUG, "command: %s", s);
1435 tmpline[0] = '\0';
1436 return (s);
1437 }
1438
1439 if (c == 0)
1440 tmpline[0] = '\0';
1441 }
1442
1443 while ((c = getc (iop)) != EOF)
1444 {
1445 c &= 0377;
1446 if (c == IAC)
1447 {
1448 c = getc (iop);
1449 if (c != EOF)
1450 {
1451 c &= 0377;
1452 switch (c)
1453 {
1454 case WILL:
1455 case WONT:
1456 c = getc (iop);
1457 printf ("%c%c%c", IAC, DONT, 0377 & c);
1458 fflush (stdout);
1459 continue;
1460
1461 case DO:
1462 case DONT:
1463 c = getc (iop);
1464 printf ("%c%c%c", IAC, WONT, 0377 & c);
1465 fflush (stdout);
1466 continue;
1467
1468 case IAC:
1469 break;
1470
1471 default:
1472 continue; /* ignore command */
1473 }
1474 }
1475 }
1476
1477 *cs++ = c;
1478 if (--n <= 0 || c == '\n')
1479 break;
1480 }
1481
1482 if (c == EOF && cs == s)
1483 return (NULL);
1484
1485 *cs++ = '\0';
1486
1487 if (debug)
1488 {
1489 if (!cred.guest && strncasecmp ("pass ", s, 5) == 0)
1490 {
1491 /* Don't syslog passwords. */
1492 syslog (LOG_DEBUG, "command: %.5s ???", s);
1493 }
1494 else
1495 {
1496 register char *cp;
1497 register int len;
1498
1499 /* Don't syslog trailing CR-LF. */
1500 len = strlen (s);
1501 cp = s + len - 1;
1502
1503 while (cp >= s && (*cp == '\n' || *cp == '\r'))
1504 {
1505 --cp;
1506 --len;
1507 }
1508
1509 syslog (LOG_DEBUG, "command: %.*s", len, s);
1510 }
1511 }
1512 return (s);
1513 }
1514
1515 void
1516 toolong (int signo)
1517 {
1518 (void) signo;
1519 reply (421, "Timeout (%d seconds): closing control connection.",
1520 timeout);
1521 if (logging)
1522 syslog (LOG_INFO, "User %s timed out after %d seconds",
1523 (cred.name ? cred.name : "unknown"), timeout);
1524 dologout (1);
1525 }
1526
1527 static int
1528 yylex (void)
1529 {
1530 static int cpos, state;
1531 char *cp, *cp2;
1532 struct tab *p;
1533 int n;
1534 char c;
1535
1536 for (;;)
1537 {
1538 switch (state)
1539 {
1540 case CMD:
1541 signal (SIGALRM, toolong);
1542 alarm ((unsigned) timeout);
1543 if (telnet_fgets (cbuf, sizeof (cbuf)-1, stdin) == NULL)
1544 {
1545 reply (221, "You could at least say goodbye.");
1546 dologout (0);
1547 }
1548 alarm (0);
1549
1550 #ifdef HAVE_SETPROCTITLE
1551 if (strncasecmp (cbuf, "PASS", 4) != 0)
1552 setproctitle ("%s: %s", proctitle, cbuf);
1553 #endif /* HAVE_SETPROCTITLE */
1554
1555 cp = strchr (cbuf, '\r');
1556 if (cp)
1557 {
1558 *cp++ = '\n';
1559 *cp = '\0';
1560 }
1561
1562 cp = strpbrk (cbuf, " \n");
1563 if (cp)
1564 cpos = cp - cbuf;
1565
1566 if (cpos == 0)
1567 cpos = 4;
1568
1569 c = cbuf[cpos];
1570 cbuf[cpos] = '\0';
1571 upper (cbuf);
1572 p = lookup (cmdtab, cbuf);
1573 cbuf[cpos] = c;
1574
1575 if (p != 0)
1576 {
1577 if (p->implemented == 0)
1578 {
1579 nack (p->name);
1580 longjmp (errcatch, 0);
1581 /* NOTREACHED */
1582 }
1583 state = p->state;
1584 yylval.s = (char*) p->name;
1585 return (p->token);
1586 }
1587 break; /* Command not known. */
1588
1589 case SITECMD:
1590 if (cbuf[cpos] == ' ')
1591 {
1592 cpos++;
1593 return (SP);
1594 }
1595 cp = &cbuf[cpos];
1596
1597 cp2 = strpbrk (cp, " \n");
1598 if (cp2)
1599 cpos = cp2 - cbuf;
1600
1601 c = cbuf[cpos];
1602 cbuf[cpos] = '\0';
1603 upper (cp);
1604 p = lookup (sitetab, cp);
1605 cbuf[cpos] = c;
1606
1607 if (p != 0)
1608 {
1609 if (p->implemented == 0)
1610 {
1611 state = CMD;
1612 nack (p->name);
1613 longjmp (errcatch, 0);
1614 /* NOTREACHED */
1615 }
1616
1617 state = p->state;
1618 yylval.s = (char*) p->name;
1619 return (p->token);
1620 }
1621 state = CMD;
1622 break; /* Command not known. */
1623
1624 case OSTR:
1625 if (cbuf[cpos] == '\n')
1626 {
1627 state = CMD;
1628 return (CRLF);
1629 }
1630
1631 case STR1:
1632 case ZSTR1:
1633 dostr1:
1634 if (cbuf[cpos] == ' ')
1635 {
1636 cpos++;
1637 if (state == OSTR)
1638 state = STR2;
1639 else
1640 ++state;
1641
1642 return (SP);
1643 }
1644 /* Intentional continuation. */
1645
1646 case ZSTR2:
1647 if (cbuf[cpos] == '\n')
1648 {
1649 state = CMD;
1650 return (CRLF);
1651 }
1652
1653 case STR2:
1654 cp = &cbuf[cpos];
1655 n = strlen (cp);
1656 cpos += n - 1;
1657 /*
1658 * Make sure the string is nonempty and newline terminated.
1659 */
1660 if (n > 1 && cbuf[cpos] == '\n')
1661 {
1662 cbuf[cpos] = '\0';
1663 yylval.s = copy (cp);
1664 cbuf[cpos] = '\n';
1665 state = ARGS;
1666 return (STRING);
1667 }
1668 break; /* Empty string, missing NL. */
1669
1670 case NSTR:
1671 if (cbuf[cpos] == ' ')
1672 {
1673 cpos++;
1674 return (SP);
1675 }
1676 if (isdigit (cbuf[cpos]))
1677 {
1678 cp = &cbuf[cpos];
1679 while (isdigit (cbuf[++cpos]))
1680 ;
1681
1682 c = cbuf[cpos];
1683 cbuf[cpos] = '\0';
1684 yylval.i = atoi (cp);
1685 cbuf[cpos] = c;
1686 state = STR1;
1687 return (NUMBER);
1688 }
1689 state = STR1;
1690 goto dostr1;
1691
1692 case DLIST:
1693 /* Either numerical strings or
1694 * address strings for IPv4 and IPv6.
1695 * The consist of hexadecimal chars,
1696 * colon and periods. A period can
1697 * not begin a valid address. */
1698 if (isxdigit (cbuf[cpos]) || cbuf[cpos] == ':')
1699 {
1700 int is_num = 1; /* Only to turn off. */
1701
1702 cp = &cbuf[cpos];
1703 while (isxdigit (cbuf[cpos])
1704 || cbuf[cpos] == ':'
1705 || cbuf[cpos] == '.')
1706 {
1707 if (!isdigit (cbuf[cpos]))
1708 is_num = 0;
1709 cpos++;
1710 }
1711
1712 c = cbuf[cpos];
1713 cbuf[cpos] = '\0';
1714 if (is_num)
1715 {
1716 yylval.i = atoi (cp);
1717 cbuf[cpos] = c;
1718 return (NUMBER);
1719 }
1720 else
1721 {
1722 yylval.s = copy (cp);
1723 cbuf[cpos] = c;
1724 return (STRING);
1725 }
1726 }
1727
1728 c = cbuf[cpos++];
1729 switch (c)
1730 {
1731 case ' ':
1732 return (SP);
1733
1734 case '\n':
1735 state = CMD;
1736 return (CRLF);
1737
1738 default:
1739 yylval.i = c;
1740 return (CHAR);
1741 }
1742 break; /* Not reachable. */
1743
1744 case ARGS:
1745 if (isdigit (cbuf[cpos]))
1746 {
1747 cp = &cbuf[cpos];
1748 while (isdigit (cbuf[++cpos]))
1749 ;
1750
1751 c = cbuf[cpos];
1752 cbuf[cpos] = '\0';
1753 yylval.i = strtoimax (cp, NULL, 10); /* off_t */
1754 cbuf[cpos] = c;
1755 return (NUMBER);
1756 }
1757
1758 switch (cbuf[cpos++])
1759 {
1760 case '\n':
1761 state = CMD;
1762 return (CRLF);
1763
1764 case ' ':
1765 return (SP);
1766
1767 case ',':
1768 return (COMMA);
1769
1770 case 'A':
1771 case 'a':
1772 return (A);
1773
1774 case 'B':
1775 case 'b':
1776 return (B);
1777
1778 case 'C':
1779 case 'c':
1780 return (C);
1781
1782 case 'E':
1783 case 'e':
1784 return (E);
1785
1786 case 'F':
1787 case 'f':
1788 return (F);
1789
1790 case 'I':
1791 case 'i':
1792 return (I);
1793
1794 case 'L':
1795 case 'l':
1796 return (L);
1797
1798 case 'N':
1799 case 'n':
1800 return (N);
1801
1802 case 'P':
1803 case 'p':
1804 return (P);
1805
1806 case 'R':
1807 case 'r':
1808 return (R);
1809
1810 case 'S':
1811 case 's':
1812 return (S);
1813
1814 case 'T':
1815 case 't':
1816 return (T);
1817 }
1818 break; /* No number, not in [\n ,aAbBcCeEfFiIlLnNpPrRsSttT] */
1819
1820 default:
1821 fatal ("Unknown state in scanner.");
1822 }
1823
1824 /*
1825 * Analysis: Cases when this point is reached.
1826 *
1827 * CMD: command not known
1828 * SITECMD: site command not known (state changed to CMD)
1829 *
1830 * OSTR, STR1, ZSTR1, STR2, ZSTR2, NSTR:
1831 * empty string or string without NL
1832 *
1833 * ARGS: not a number, not a special character
1834 */
1835
1836 /*
1837 * Issue a new error message only if the parser has not
1838 * yet reported a complaint. Without this precaution
1839 * two messages would be directed to the client, thus
1840 * upsetting all following exchange.
1841 */
1842 if (!yynerrs)
1843 yyerror ("command not recognized");
1844
1845 state = CMD;
1846 longjmp (errcatch, 0);
1847 } /* for (;;) */
1848 }
1849
1850 void
1851 upper (char *s)
1852 {
1853 while (*s != '\0')
1854 {
1855 if (islower (*s))
1856 *s = toupper (*s);
1857 s++;
1858 }
1859 }
1860
1861 static char *
1862 copy (char *s)
1863 {
1864 char *p;
1865
1866 p = malloc (strlen (s) + 1);
1867 if (p == NULL)
1868 fatal ("Ran out of memory.");
1869
1870 strcpy (p, s);
1871 return (p);
1872 }
1873
1874 static void
1875 help (struct tab *ctab, char *s)
1876 {
1877 struct tab *c;
1878 int width, NCMDS;
1879 const char *help_type;
1880
1881 if (ctab == sitetab)
1882 help_type = "SITE ";
1883 else
1884 help_type = "";
1885
1886 width = 0, NCMDS = 0;
1887 for (c = ctab; c->name != NULL; c++)
1888 {
1889 int len = strlen (c->name);
1890
1891 if (len > width)
1892 width = len;
1893
1894 NCMDS++;
1895 }
1896
1897 width = (width + 8) &~ 7;
1898
1899 if (s == 0)
1900 {
1901 int i, j, w;
1902 int columns, lines;
1903
1904 lreply (214, "The following %scommands are recognized %s.",
1905 help_type, "(* =>'s unimplemented)");
1906
1907 columns = 76 / width;
1908 if (columns == 0)
1909 columns = 1;
1910
1911 lines = (NCMDS + columns - 1) / columns;
1912
1913 for (i = 0; i < lines; i++)
1914 {
1915 printf (" ");
1916 for (j = 0; j < columns; j++)
1917 {
1918 c = ctab + j * lines + i;
1919 printf ("%s%c", c->name, c->implemented ? ' ' : '*');
1920
1921 if (c + lines >= &ctab[NCMDS])
1922 break;
1923
1924 w = strlen (c->name) + 1;
1925 while (w < width)
1926 {
1927 putchar (' ');
1928 w++;
1929 }
1930 }
1931 printf ("\r\n");
1932 }
1933 fflush (stdout);
1934 reply (214, "Direct comments to ftp-bugs@%s.", hostname);
1935 return;
1936 }
1937
1938 upper (s);
1939
1940 c = lookup (ctab, s);
1941 if (c == (struct tab *) 0)
1942 {
1943 reply (502, "Unknown command %s.", s);
1944 return;
1945 }
1946
1947 if (c->implemented)
1948 reply (214, "Syntax: %s%s %s", help_type, c->name, c->help);
1949 else
1950 reply (214, "%s%-*s\t%s; unimplemented.", help_type,
1951 width, c->name, c->help);
1952 }
1953
1954 static void
1955 sizecmd (char *filename)
1956 {
1957 switch (type)
1958 {
1959 case TYPE_L:
1960 case TYPE_I:
1961 {
1962 struct stat stbuf;
1963
1964 if (stat (filename, &stbuf) < 0 || !S_ISREG (stbuf.st_mode))
1965 reply (550, "%s: not a plain file.", filename);
1966 else
1967 reply (213, "%ju", (uintmax_t) stbuf.st_size);
1968 break;
1969 }
1970
1971 case TYPE_A:
1972 {
1973 FILE *fin;
1974 int c;
1975 off_t count;
1976 struct stat stbuf;
1977
1978 fin = fopen (filename, "r");
1979 if (fin == NULL)
1980 {
1981 perror_reply (550, filename);
1982 return;
1983 }
1984
1985 if (fstat (fileno (fin), &stbuf) < 0 || !S_ISREG (stbuf.st_mode))
1986 {
1987 reply (550, "%s: not a plain file.", filename);
1988 fclose (fin);
1989 return;
1990 }
1991
1992 count = 0;
1993 while ((c = getc (fin)) != EOF)
1994 {
1995 if (c == '\n') /* will get expanded to \r\n */
1996 count++;
1997 count++;
1998 }
1999 fclose (fin);
2000
2001 reply (213, "%jd", (intmax_t) count);
2002 break;
2003 }
2004
2005 default:
2006 reply (504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
2007 }
2008 }
2009
2010 static void
2011 yyerror (const char *s)
2012 {
2013 char *cp;
2014
2015 cp = strchr (cbuf, '\n');
2016 if (cp != NULL)
2017 *cp = '\0';
2018
2019 reply (500, "'%s': %s", cbuf, (s ? s : "command not understood."));
2020 }