diff options
author | 2021-05-09 14:20:58 -0400 | |
---|---|---|
committer | 2021-05-09 14:20:58 -0400 | |
commit | 5fd83771641d15c418f747bd343ba6738d3875f7 (patch) | |
tree | 5abf0f78f680d9837dbd93d4d4c3933bb7509599 /remote_cmds/tftp.tproj | |
download | apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.gz apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.zst apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.zip |
Import macOS userland
adv_cmds-176
basic_cmds-55
bootstrap_cmds-116.100.1
developer_cmds-66
diskdev_cmds-667.40.1
doc_cmds-53.60.1
file_cmds-321.40.3
mail_cmds-35
misc_cmds-34
network_cmds-606.40.1
patch_cmds-17
remote_cmds-63
shell_cmds-216.60.1
system_cmds-880.60.2
text_cmds-106
Diffstat (limited to 'remote_cmds/tftp.tproj')
-rw-r--r-- | remote_cmds/tftp.tproj/Makefile | 12 | ||||
-rw-r--r-- | remote_cmds/tftp.tproj/extern.h | 38 | ||||
-rw-r--r-- | remote_cmds/tftp.tproj/main.c | 920 | ||||
-rw-r--r-- | remote_cmds/tftp.tproj/tftp.1 | 243 | ||||
-rw-r--r-- | remote_cmds/tftp.tproj/tftp.c | 742 | ||||
-rw-r--r-- | remote_cmds/tftp.tproj/tftpsubs.c | 287 | ||||
-rw-r--r-- | remote_cmds/tftp.tproj/tftpsubs.h | 54 |
7 files changed, 2296 insertions, 0 deletions
diff --git a/remote_cmds/tftp.tproj/Makefile b/remote_cmds/tftp.tproj/Makefile new file mode 100644 index 0000000..6ed62a0 --- /dev/null +++ b/remote_cmds/tftp.tproj/Makefile @@ -0,0 +1,12 @@ +Project = tftp + +HFILES = extern.h tftpsubs.h +CFILES = main.c tftp.c tftpsubs.c +MANPAGES = tftp.1 + +Extra_CC_Flags = -Wall -Werror -fPIE +Extra_CC_Flags += -D__FBSDID=__RCSID +Extra_LD_Flags = -dead_strip -pie +Extra_LD_Flags += -ledit + +include $(MAKEFILEPATH)/CoreOS/ReleaseControl/BSDCommon.make diff --git a/remote_cmds/tftp.tproj/extern.h b/remote_cmds/tftp.tproj/extern.h new file mode 100644 index 0000000..fdf3d31 --- /dev/null +++ b/remote_cmds/tftp.tproj/extern.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)extern.h 8.1 (Berkeley) 6/6/93 + * $FreeBSD: src/usr.bin/tftp/extern.h,v 1.3 2002/03/22 01:42:33 imp Exp $ + */ + +void recvfile(int, char *, char *); +void xmitfile(int, char *, char *); diff --git a/remote_cmds/tftp.tproj/main.c b/remote_cmds/tftp.tproj/main.c new file mode 100644 index 0000000..f223726 --- /dev/null +++ b/remote_cmds/tftp.tproj/main.c @@ -0,0 +1,920 @@ +/* $NetBSD: main.c,v 1.19 2003/10/02 23:31:52 itojun Exp $ */ + +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +__attribute__((__used__)) +static const char copyright[] = +"@(#) Copyright (c) 1983, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif + +#if 0 +#ifndef lint +static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; +#endif +#endif + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD: src/usr.bin/tftp/main.c,v 1.22 2005/10/19 15:37:42 stefanf Exp $"); + +/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ + +/* + * TFTP User Program -- Command Interface. + */ +#include <sys/param.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/file.h> +#include <sys/param.h> + +#include <netinet/in.h> + +#include <arpa/inet.h> +#include <arpa/tftp.h> + +#include <ctype.h> +#include <fcntl.h> +#include <err.h> +#include <histedit.h> +#include <netdb.h> +#include <setjmp.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "extern.h" + +#define MAXLINE 200 +#define TIMEOUT 5 /* secs between rexmt's */ +#define MAXSEGSIZE 65464 +struct sockaddr_storage peeraddr; +int f; +int trace; +int verbose; +int tsize=0; +int tout=0; +int def_blksize=SEGSIZE; +int blksize=SEGSIZE; +int connected; +char mode[32]; +char line[MAXLINE]; +int margc; +#define MAX_MARGV 20 +char *margv[MAX_MARGV]; +jmp_buf toplevel; +volatile int txrx_error; + +void get(int, char **); +void help(int, char **); +void intr(int); +void modecmd(int, char **); +void put(int, char **); +void quit(int, char **); +void setascii(int, char **); +void setbinary(int, char **); +void setpeer0(char *, const char *); +void setpeer(int, char **); +void setrexmt(int, char **); +void settimeout(int, char **); +void settrace(int, char **); +void setverbose(int, char **); +#ifdef __APPLE__ +void setblksize(int, char **); +void settsize(int, char **); +void settimeoutopt(int, char **); +#endif +void status(int, char **); +#ifdef __APPLE__ +char *tail(char *); +int main(int, char *[]); +void intr(int); +struct cmd *getcmd(char *); +#endif + +static void command(void) __dead2; +static const char *command_prompt(void); + +static void getusage(char *); +static void makeargv(void); +static void putusage(char *); +static void settftpmode(const char *); + +char *tail(char *); +struct cmd *getcmd(char *); + +#define HELPINDENT (sizeof("connect")) + +struct cmd { + const char *name; + char *help; + void (*handler)(int, char **); +}; + +char vhelp[] = "toggle verbose mode"; +char thelp[] = "toggle packet tracing"; +#ifdef __APPLE__ +char tshelp[] = "toggle extended tsize option"; +char tohelp[] = "toggle extended timeout option"; +char blhelp[] = "set an alternative blocksize (def. 512)"; +#endif +char chelp[] = "connect to remote tftp"; +char qhelp[] = "exit tftp"; +char hhelp[] = "print help information"; +char shelp[] = "send file"; +char rhelp[] = "receive file"; +char mhelp[] = "set file transfer mode"; +char sthelp[] = "show current status"; +char xhelp[] = "set per-packet retransmission timeout"; +char ihelp[] = "set total retransmission timeout"; +char ashelp[] = "set mode to netascii"; +char bnhelp[] = "set mode to octet"; + +struct cmd cmdtab[] = { + { "connect", chelp, setpeer }, + { "mode", mhelp, modecmd }, + { "put", shelp, put }, + { "get", rhelp, get }, + { "quit", qhelp, quit }, + { "verbose", vhelp, setverbose }, +#ifdef __APPLE__ + { "blksize", blhelp, setblksize }, + { "tsize", tshelp, settsize }, +#endif + { "trace", thelp, settrace }, + { "status", sthelp, status }, + { "binary", bnhelp, setbinary }, + { "ascii", ashelp, setascii }, + { "rexmt", xhelp, setrexmt }, + { "timeout", ihelp, settimeout }, +#ifdef __APPLE__ + { "tout", tohelp, settimeoutopt }, +#endif + { "?", hhelp, help }, + { NULL, NULL, NULL } +}; + +int +main(argc, argv) + int argc; + char *argv[]; +{ + int c; + + f = -1; + strcpy(mode, "netascii"); + signal(SIGINT, intr); + + setprogname(argv[0]); + while ((c = getopt(argc, argv, "e")) != -1) { + switch (c) { + case 'e': + blksize = MAXSEGSIZE; + strcpy(mode, "octet"); + tsize = 1; + tout = 1; + break; + default: + printf("usage: %s [-e] host-name [port]\n", + getprogname()); + exit(1); + } + } + argc -= optind; + argv += optind; + + if (argc >= 1) { + if (setjmp(toplevel) != 0) + exit(txrx_error); + argc++; + argv--; + setpeer(argc, argv); + } + if (setjmp(toplevel) != 0) + (void)putchar('\n'); + command(); + return (0); +} + +char hostname[MAXHOSTNAMELEN]; + +void +setpeer0(host, port) + char *host; + const char *port; +{ + struct addrinfo hints, *res0, *res; + int error, soopt; + struct sockaddr_storage ss; + const char *cause = "unknown"; + + if (connected) { + close(f); + f = -1; + } + connected = 0; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_protocol = IPPROTO_UDP; + hints.ai_flags = AI_CANONNAME; + if (!port) + port = "tftp"; + error = getaddrinfo(host, port, &hints, &res0); + if (error) { + warnx("%s", gai_strerror(error)); + return; + } + + for (res = res0; res; res = res->ai_next) { + if (res->ai_addrlen > sizeof(peeraddr)) + continue; + f = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (f < 0) { + cause = "socket"; + continue; + } + + memset(&ss, 0, sizeof(ss)); + ss.ss_family = res->ai_family; + ss.ss_len = res->ai_addrlen; + if (bind(f, (struct sockaddr *)&ss, ss.ss_len) < 0) { + cause = "bind"; + close(f); + f = -1; + continue; + } + + break; + } + + if (f >= 0) { + soopt = 65536; + if (setsockopt(f, SOL_SOCKET, SO_SNDBUF, &soopt, sizeof(soopt)) + < 0) { + close(f); + f = -1; + cause = "setsockopt SNDBUF"; + } + if (setsockopt(f, SOL_SOCKET, SO_RCVBUF, &soopt, sizeof(soopt)) + < 0) { + close(f); + f = -1; + cause = "setsockopt RCVBUF"; + } + } + + if (f < 0) + warn("%s", cause); + else { + /* res->ai_addr <= sizeof(peeraddr) is guaranteed */ + memcpy(&peeraddr, res->ai_addr, res->ai_addrlen); + if (res->ai_canonname) { + (void) strlcpy(hostname, res->ai_canonname, + sizeof(hostname)); + } else + (void) strlcpy(hostname, host, sizeof(hostname)); + connected = 1; + } + + freeaddrinfo(res0); +} + +void +setpeer(argc, argv) + int argc; + char *argv[]; +{ + + if (argc < 2) { + strcpy(line, "Connect "); + printf("(to) "); + fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); + makeargv(); + argc = margc; + argv = margv; + } + if ((argc < 2) || (argc > 3)) { + printf("usage: %s [-e] host-name [port]\n", getprogname()); + return; + } + if (argc == 3) + setpeer0(argv[1], argv[2]); + else + setpeer0(argv[1], NULL); +} + +struct modes { + const char *m_name; + const char *m_mode; +} modes[] = { + { "ascii", "netascii" }, + { "netascii", "netascii" }, + { "binary", "octet" }, + { "image", "octet" }, + { "octet", "octet" }, +/* { "mail", "mail" }, */ + { 0, 0 } +}; + +void +modecmd(argc, argv) + int argc; + char *argv[]; +{ + struct modes *p; + const char *sep; + + if (argc < 2) { + printf("Using %s mode to transfer files.\n", mode); + return; + } + if (argc == 2) { + for (p = modes; p->m_name; p++) + if (strcmp(argv[1], p->m_name) == 0) + break; + if (p->m_name) { + settftpmode(p->m_mode); + return; + } + printf("%s: unknown mode\n", argv[1]); + /* drop through and print usage message */ + } + + printf("usage: %s [", argv[0]); + sep = " "; + for (p = modes; p->m_name; p++) { + printf("%s%s", sep, p->m_name); + if (*sep == ' ') + sep = " | "; + } + printf(" ]\n"); + return; +} + +void +setbinary(argc, argv) + int argc __unused; + char *argv[] __unused; +{ + + settftpmode("octet"); +} + +void +setascii(argc, argv) + int argc __unused; + char *argv[] __unused; +{ + + settftpmode("netascii"); +} + +static void +settftpmode(newmode) + const char *newmode; +{ + strcpy(mode, newmode); + if (verbose) + printf("mode set to %s\n", mode); +} + + +/* + * Send file(s). + */ +void +put(argc, argv) + int argc; + char *argv[]; +{ + int fd; + int n; + char *cp, *targ; +#ifdef __APPLE__ + char targbuf[PATH_MAX]; +#endif /* __APPLE__ */ + + if (argc < 2) { + strcpy(line, "send "); + printf("(file) "); + fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); + makeargv(); + argc = margc; + argv = margv; + } + if (argc < 2) { + putusage(argv[0]); + return; + } + targ = argv[argc - 1]; + if (rindex(argv[argc - 1], ':')) { + char *lcp; + + for (n = 1; n < argc - 1; n++) + if (index(argv[n], ':')) { + putusage(argv[0]); + return; + } + lcp = argv[argc - 1]; + targ = rindex(lcp, ':'); + *targ++ = 0; + if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') { + lcp[strlen(lcp) - 1] = '\0'; + lcp++; + } + setpeer0(lcp, NULL); + } + if (!connected) { + printf("No target machine specified.\n"); + return; + } + if (argc < 4) { + cp = argc == 2 ? tail(targ) : argv[1]; + fd = open(cp, O_RDONLY); + if (fd < 0) { + warn("%s", cp); + return; + } + if (verbose) + printf("putting %s to %s:%s [%s]\n", + cp, hostname, targ, mode); + xmitfile(fd, targ, mode); + return; + } + /* this assumes the target is a directory */ + /* on a remote unix system. hmmmm. */ +#ifdef __APPLE__ + snprintf(targbuf, sizeof(targbuf), "%s/", targ); + cp = targbuf + strlen(targbuf); +#else /* !__APPLE__ */ + cp = index(targ, '\0'); + *cp++ = '/'; +#endif /* __APPLE__ */ + for (n = 1; n < argc - 1; n++) { +#ifdef __APPLE__ + strlcpy(cp, tail(argv[n]), sizeof(targbuf) - (cp - targbuf)); +#else /* !__APPLE__ */ + strcpy(cp, tail(argv[n])); +#endif /* __APPLE__ */ + fd = open(argv[n], O_RDONLY); + if (fd < 0) { + warn("%s", argv[n]); + continue; + } + if (verbose) + printf("putting %s to %s:%s [%s]\n", +#ifdef __APPLE__ + argv[n], hostname, targbuf, mode); +#else /* !__APPLE__ */ + argv[n], hostname, targ, mode); +#endif /* __APPLE__ */ + xmitfile(fd, targ, mode); + } +} + +static void +putusage(s) + char *s; +{ + printf("usage: %s file ... host:target, or\n", s); + printf(" %s file ... target (when already connected)\n", s); +} + +/* + * Receive file(s). + */ +void +get(argc, argv) + int argc; + char *argv[]; +{ + int fd; + int n; + char *cp; + char *src; + + if (argc < 2) { + strcpy(line, "get "); + printf("(files) "); + fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); + makeargv(); + argc = margc; + argv = margv; + } + if (argc < 2) { + getusage(argv[0]); + return; + } + if (!connected) { + for (n = 1; n < argc ; n++) + if (rindex(argv[n], ':') == 0) { + getusage(argv[0]); + return; + } + } + for (n = 1; n < argc ; n++) { + src = rindex(argv[n], ':'); + if (src == NULL) + src = argv[n]; + else { + char *lcp; + + *src++ = 0; + lcp = argv[n]; + if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') { + lcp[strlen(lcp) - 1] = '\0'; + lcp++; + } + setpeer0(lcp, NULL); + if (!connected) + continue; + } + if (argc < 4) { + cp = argc == 3 ? argv[2] : tail(src); + fd = creat(cp, 0644); + if (fd < 0) { + warn("%s", cp); + return; + } + if (verbose) + printf("getting from %s:%s to %s [%s]\n", + hostname, src, cp, mode); + recvfile(fd, src, mode); + break; + } + cp = tail(src); /* new .. jdg */ + fd = creat(cp, 0644); + if (fd < 0) { + warn("%s", cp); + continue; + } + if (verbose) + printf("getting from %s:%s to %s [%s]\n", + hostname, src, cp, mode); + recvfile(fd, src, mode); + } +} + +static void +getusage(s) + char *s; +{ + printf("usage: %s host:file host:file ... file, or\n", s); + printf(" %s file file ... file if connected\n", s); +} + +void +setblksize(argc, argv) + int argc; + char *argv[]; +{ + int t; + + if (argc < 2) { + strcpy(line, "blksize "); + printf("(blksize) "); + fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); + makeargv(); + argc = margc; + argv = margv; + } + if (argc != 2) { + printf("usage: %s value\n", argv[0]); + return; + } + t = atoi(argv[1]); + if (t < 8 || t > 65464) + printf("%s: bad value\n", argv[1]); + else + blksize = t; +} + +int def_rexmtval = TIMEOUT; +int rexmtval = TIMEOUT; + +void +setrexmt(argc, argv) + int argc; + char *argv[]; +{ + int t; + + if (argc < 2) { + strcpy(line, "Rexmt-timeout "); + printf("(value) "); + fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); + makeargv(); + argc = margc; + argv = margv; + } + if (argc != 2) { + printf("usage: %s value\n", argv[0]); + return; + } + t = atoi(argv[1]); + if (t < 0) + printf("%s: bad value\n", argv[1]); + else + rexmtval = t; +} + +int maxtimeout = 5 * TIMEOUT; + +void +settimeout(argc, argv) + int argc; + char *argv[]; +{ + int t; + + if (argc < 2) { + strcpy(line, "Maximum-timeout "); + printf("(value) "); + fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); + makeargv(); + argc = margc; + argv = margv; + } + if (argc != 2) { + printf("usage: %s value\n", argv[0]); + return; + } + t = atoi(argv[1]); + if (t < 0) + printf("%s: bad value\n", argv[1]); + else + maxtimeout = t; +} + +void +status(argc, argv) + int argc __unused; + char *argv[] __unused; +{ + if (connected) + printf("Connected to %s.\n", hostname); + else + printf("Not connected.\n"); + printf("Mode: %s Verbose: %s Tracing: %s\n", mode, + verbose ? "on" : "off", trace ? "on" : "off"); + printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n", + rexmtval, maxtimeout); +} + +void +intr(dummy) + int dummy __unused; +{ + + signal(SIGALRM, SIG_IGN); + alarm(0); + longjmp(toplevel, -1); +} + +char * +tail(filename) + char *filename; +{ + char *s; + + while (*filename) { + s = rindex(filename, '/'); + if (s == NULL) + break; + if (s[1]) + return (s + 1); + *s = '\0'; + } + return (filename); +} + +static const char * +command_prompt() +{ + + return ("tftp> "); +} + +/* + * Command parser. + */ +static void +command() +{ + HistEvent he; + struct cmd *c; + static EditLine *el; + static History *hist; + const char *bp; + char *cp; + int len, num, vrbose; + + vrbose = isatty(0); + if (vrbose) { + el = el_init("tftp", stdin, stdout, stderr); + hist = history_init(); + history(hist, &he, H_SETSIZE, 100); + el_set(el, EL_HIST, history, hist); + el_set(el, EL_EDITOR, "emacs"); + el_set(el, EL_PROMPT, command_prompt); + el_set(el, EL_SIGNAL, 1); + el_source(el, NULL); + } + for (;;) { + if (vrbose) { + if ((bp = el_gets(el, &num)) == NULL || num == 0) + exit(0); + len = (num > MAXLINE) ? MAXLINE : num; + memcpy(line, bp, len); + line[len] = '\0'; + history(hist, &he, H_ENTER, bp); + } else { + if (fgets(line, sizeof line , stdin) == 0) { + if (feof(stdin)) { + exit(txrx_error); + } else { + continue; + } + } + } + if ((cp = strchr(line, '\n'))) + *cp = '\0'; + if (line[0] == 0) + continue; + makeargv(); + if (margc == 0) + continue; + c = getcmd(margv[0]); + if (c == (struct cmd *)-1) { + printf("?Ambiguous command\n"); + continue; + } + if (c == 0) { + printf("?Invalid command\n"); + continue; + } + (*c->handler)(margc, margv); + } +} + +struct cmd * +getcmd(name) + char *name; +{ + const char *p, *q; + struct cmd *c, *found; + int nmatches, longest; + + longest = 0; + nmatches = 0; + found = 0; + for (c = cmdtab; (p = c->name) != NULL; c++) { + for (q = name; *q == *p++; q++) + if (*q == 0) /* exact match? */ + return (c); + if (!*q) { /* the name was a prefix */ + if (q - name > longest) { + longest = q - name; + nmatches = 1; + found = c; + } else if (q - name == longest) + nmatches++; + } + } + if (nmatches > 1) + return ((struct cmd *)-1); + return (found); +} + +/* + * Slice a string up into argc/argv. + */ +static void +makeargv() +{ + char *cp; + char **argp = margv; + + margc = 0; + if ((cp = strchr(line, '\n'))) + *cp = '\0'; + for (cp = line; margc < MAX_MARGV - 1 && *cp;) { + while (isspace((unsigned char)*cp)) + cp++; + if (*cp == '\0') + break; + *argp++ = cp; + margc += 1; + while (*cp != '\0' && !isspace((unsigned char)*cp)) + cp++; + if (*cp == '\0') + break; + *cp++ = '\0'; + } + *argp++ = 0; +} + +void +quit(argc, argv) + int argc __unused; + char *argv[] __unused; +{ + exit(txrx_error); +} + +/* + * Help command. + */ +void +help(argc, argv) + int argc; + char *argv[]; +{ + struct cmd *c; + + if (argc == 1) { + printf("Commands may be abbreviated. Commands are:\n\n"); + for (c = cmdtab; c->name; c++) + printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help); + return; + } + while (--argc > 0) { + char *arg; + arg = *++argv; + c = getcmd(arg); + if (c == (struct cmd *)-1) + printf("?Ambiguous help command %s\n", arg); + else if (c == (struct cmd *)0) + printf("?Invalid help command %s\n", arg); + else + printf("%s\n", c->help); + } +} + +void +settrace(argc, argv) + int argc __unused; + char **argv __unused; +{ + trace = !trace; + printf("Packet tracing %s.\n", trace ? "on" : "off"); +} + +void +setverbose(argc, argv) + int argc __unused; + char **argv __unused; +{ + verbose = !verbose; + printf("Verbose mode %s.\n", verbose ? "on" : "off"); +} + +void +settsize(argc, argv) + int argc __unused; + char **argv __unused; +{ + tsize = !tsize; + printf("Tsize mode %s.\n", tsize ? "on" : "off"); +} + +void +settimeoutopt(argc, argv) + int argc __unused; + char **argv __unused; +{ + tout = !tout; + printf("Timeout option %s.\n", tout ? "on" : "off"); +} diff --git a/remote_cmds/tftp.tproj/tftp.1 b/remote_cmds/tftp.tproj/tftp.1 new file mode 100644 index 0000000..fe867ca --- /dev/null +++ b/remote_cmds/tftp.tproj/tftp.1 @@ -0,0 +1,243 @@ +.\" $NetBSD: tftp.1,v 1.18 2003/08/07 11:16:14 agc Exp $ +.\" +.\" Copyright (c) 1990, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)tftp.1 8.2 (Berkeley) 4/18/94 +.\" $FreeBSD: src/usr.bin/tftp/tftp.1,v 1.20 2007/11/07 07:56:57 ru Exp $ +.\" +.Dd June 11, 2003 +.Dt TFTP 1 +.Os +.Sh NAME +.Nm tftp +.Nd "trivial file transfer program" +.Sh SYNOPSIS +.Nm +.Op Fl e +.Op Ar host +.Op Ar port +.Sh DESCRIPTION +The +.Nm +utility is the user interface to the Internet +.Tn TFTP +(Trivial File Transfer Protocol), +which allows users to transfer files to and from a remote machine. +The remote +.Ar host +(and optional +.Ar port ) +may be specified on the command line, in which case +.Nm +uses +.Ar host +(and +.Ar port ) +as the default for future transfers (see the +.Cm connect +command below). +.Pp +The optional +.Fl e +argument sets a binary transfer mode as well as setting the extended options +as if +.Cm tout , +.Cm tsize , +and +.Cm blksize 65464 , +had been given. +.Sh COMMANDS +Once +.Nm +is running, it issues the prompt +.Ql tftp\*[Gt] +and recognizes the following commands: +.Pp +.Bl -tag -width verbose -compact +.It Cm \&? Ar command-name ... +Print help information. +.Pp +.It Cm ascii +Shorthand for +.Ic mode Cm ascii . +.Pp +.It Cm binary +Shorthand for +.Ic mode Cm binary . +.Pp +.It Cm blksize Ar blk-size +Set the tftp blksize option to +.Ar blk-size +octets (8-bit bytes). Since the number of blocks in a tftp +.Cm get +or +.Cm put +is 65535, the default block size of 512 bytes only allows a maximum of +just under 32 megabytes to be transferred. The value given for +.Ar blk-size +must be between 8 and 65464, inclusive. +Note that many servers will not respect this option. +.Pp +.It Cm connect Ar host-name Op Ar port +Set the +.Ar host +(and optionally +.Ar port ) +for transfers. +Note that the +.Tn TFTP +protocol, unlike the +.Tn FTP +protocol, +does not maintain connections between transfers; thus, the +.Cm connect +command does not actually create a connection, +but merely remembers what host is to be used for transfers. +You do not have to use the +.Cm connect +command; the remote host can be specified as part of the +.Cm get +or +.Cm put +commands. +.Pp +.It Cm get Ar filename +.It Cm get Ar remotename localname +.It Cm get Ar file1 file2 ... fileN +Get one or more files from the remote host. +When using the +.Ar host +argument, the +.Ar host +will be used as default host for future transfers. +If +.Ar localname +is specified, the file is stored locally as +.Ar localname , +otherwise the original filename is used. +Note that it is not possible to download two files at a time, only +one, three, or more than three files, at a time. +.Pp +To specify an IPv6 numeric address for a host, wrap it using square +brackets like +.Dq Li [3ffe:2900:e00c:ffee::1234] : Ns Ar file +to disambiguate the +colons used in the IPv6 address from the colon separating the host and +the filename. +.Pp +.It Cm mode Ar transfer-mode +Set the mode for transfers; +.Ar transfer-mode +may be one of +.Cm ascii +or +.Cm binary . +The default is +.Cm ascii . +.Pp +.It Cm put Ar file +.It Cm put Ar localfile remotefile +.It Cm put Ar file1 file2 ... fileN remote-directory +Put a file or set of files to the specified +remote file or directory. +The destination +can be in one of two forms: +a filename on the remote host, if the host has already been specified, +or a string of the form +.Ar hosts:filename +to specify both a host and filename at the same time. +If the latter form is used, +the hostname specified becomes the default for future transfers. +When +.Ar remotename +is specified, the file is stored remotely as +.Ar remotename , +otherwise the original filename is used. +If the +.Ar remote-directory +argument is used, the remote host is assumed to be a +.Ux +machine. +To specify an IPv6 numeric address for a +.Ar host , +see the example under the +.Cm get +command. +.Pp +.It Cm quit +Exit +.Nm . +An end of file also exits. +.Pp +.It Cm rexmt Ar retransmission-timeout +Set the per-packet retransmission timeout, in seconds. +.Pp +.It Cm status +Show current status. +.Pp +.It Cm timeout Ar total-transmission-timeout +Set the total transmission timeout, in seconds. +.Pp +.It Cm tout +Toggle the tftp "timeout" option. If enabled, the client will pass its +.Ar retransmission-timeout +to the server. +Note that many servers will not respect this option. +.Pp +.It Cm trace +Toggle packet tracing. +.Pp +.It Cm tsize +Toggle the tftp "tsize" option. If enabled, the client will pass and +request the filesize of a file at the beginning of a file transfer. +Note that many servers will not respect this option. +.Pp +.It Cm verbose +Toggle verbose mode. +.El +.Sh HISTORY +The +.Nm +command appeared in +.Bx 4.3 . +IPv6 support was implemented by WIDE/KAME project in 1999. +TFTP options were implemented by Wasabi Systems, Inc., in 2003, +and first appeared in +.Nx 2.0 . +.Sh SECURITY CONSIDERATIONS +Because there is no user-login or validation within +the +.Tn TFTP +protocol, the remote site will probably have some +sort of file-access restrictions in place. +The +exact methods are specific to each site and therefore +difficult to document here. +.Pp +Files larger than 33488896 octets (65535 blocks) cannot be transferred +without client and server supporting blocksize negotiation (RFC1783). diff --git a/remote_cmds/tftp.tproj/tftp.c b/remote_cmds/tftp.tproj/tftp.c new file mode 100644 index 0000000..56c9750 --- /dev/null +++ b/remote_cmds/tftp.tproj/tftp.c @@ -0,0 +1,742 @@ +/* $NetBSD: tftp.c,v 1.18 2003/08/07 11:16:14 agc Exp $ */ + +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if 0 +#ifndef lint +static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; +#endif /* not lint */ +#endif + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD: src/usr.bin/tftp/tftp.c,v 1.13 2006/09/28 21:22:21 matteo Exp $"); + +/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ + +/* + * TFTP User Program -- Protocol Machines + */ +#include <sys/types.h> +#include <sys/param.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/time.h> + +#include <netinet/in.h> + +#include <arpa/inet.h> +#include <arpa/tftp.h> + +#include <err.h> +#include <errno.h> +#include <setjmp.h> +#include <signal.h> +#ifdef __APPLE__ +#include <stdlib.h> +#endif +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <netdb.h> + +#include "extern.h" +#include "tftpsubs.h" + +extern struct sockaddr_storage peeraddr; /* filled in by main */ +extern int f; /* the opened socket */ +extern int trace; +extern int verbose; +extern int def_rexmtval; +extern int rexmtval; +extern int maxtimeout; +extern volatile int txrx_error; +#ifdef __APPLE__ +extern int tsize; +extern int tout; +extern int def_blksize; +extern int blksize; +#endif + +char ackbuf[PKTSIZE]; +int timeout; +extern jmp_buf toplevel; +jmp_buf timeoutbuf; + +static void nak(int, struct sockaddr *); +#ifdef __APPLE__ +static int makerequest(int, const char *, struct tftphdr *, const char *, off_t); +#else +static int makerequest(int, const char *, struct tftphdr *, const char *); +#endif +static void printstats(const char *, unsigned long); +static void startclock(void); +static void stopclock(void); +static void timer(int); +static void tpacket(const char *, struct tftphdr *, int); +static int sockaddrcmp(struct sockaddr *, struct sockaddr *); + +static void get_options(struct tftphdr *, int); + +static void +get_options(struct tftphdr *ap, int size) +{ + unsigned long val; + char *opt, *endp, *nextopt, *valp; + int l; + + size -= 2; /* skip over opcode */ + opt = ap->th_stuff; + endp = opt + size - 1; + *endp = '\0'; + + while (opt < endp) { + l = strlen(opt) + 1; + valp = opt + l; + if (valp < endp) { + val = strtoul(valp, NULL, 10); + l = strlen(valp) + 1; + nextopt = valp + l; + if (val == ULONG_MAX && errno == ERANGE) { + /* Report illegal value */ + opt = nextopt; + continue; + } + } else { + /* Badly formed OACK */ + break; + } + if (strcmp(opt, "tsize") == 0) { + /* cool, but we'll ignore it */ + } else if (strcmp(opt, "timeout") == 0) { + if (val >= 1 && val <= 255) { + rexmtval = val; + } else { + /* Report error? */ + } + } else if (strcmp(opt, "blksize") == 0) { + if (val >= 8 && val <= MAXSEGSIZE) { + blksize = val; + } else { + /* Report error? */ + } + } else { + /* unknown option */ + } + opt = nextopt; + } +} + +/* + * Send the requested file. + */ +void +xmitfile(fd, name, mode) + int fd; + char *name; + char *mode; +{ + struct tftphdr *ap; /* data and ack packets */ + struct tftphdr *dp; + int n; + volatile unsigned int block; + volatile int size, convert; + volatile unsigned long amount; + struct sockaddr_storage from; + struct stat sbuf; + off_t filesize=0; + socklen_t fromlen; + FILE *file; + struct sockaddr_storage peer; + struct sockaddr_storage serv; /* valid server port number */ + + startclock(); /* start stat's clock */ + dp = r_init(); /* reset fillbuf/read-ahead code */ + ap = (struct tftphdr *)ackbuf; + if (tsize) { + if (fstat(fd, &sbuf) == 0) { + filesize = sbuf.st_size; + } else { + filesize = -1ULL; + } + } + file = fdopen(fd, "r"); + convert = !strcmp(mode, "netascii"); + block = 0; + amount = 0; + memcpy(&peer, &peeraddr, peeraddr.ss_len); + memset(&serv, 0, sizeof(serv)); + + signal(SIGALRM, timer); + do { + if (block == 0) + size = makerequest(WRQ, name, dp, mode, filesize) - 4; + else { + /* size = read(fd, dp->th_data, SEGSIZE); */ + size = readit(file, &dp, blksize, convert); + if (size < 0) { + nak(errno + 100, (struct sockaddr *)&peer); + break; + } + dp->th_opcode = htons((u_short)DATA); + dp->th_block = htons((u_short)block); + } + timeout = 0; + (void) setjmp(timeoutbuf); +send_data: + if (trace) + tpacket("sent", dp, size + 4); + n = sendto(f, dp, size + 4, 0, + (struct sockaddr *)&peer, peer.ss_len); + if (n != size + 4) { + warn("sendto"); + goto abort; + } + if (block) + read_ahead(file, blksize, convert); + for ( ; ; ) { + alarm(rexmtval); + do { + fromlen = sizeof(from); + n = recvfrom(f, ackbuf, sizeof(ackbuf), 0, + (struct sockaddr *)&from, &fromlen); + } while (n <= 0); + alarm(0); + if (n < 0) { + warn("recvfrom"); + goto abort; + } + if (!serv.ss_family) + serv = from; + else if (!sockaddrcmp((struct sockaddr *)&serv, + (struct sockaddr *)&from)) { + warn("server address/port mismatch"); + goto abort; + } + peer = from; + if (trace) + tpacket("received", ap, n); + /* should verify packet came from server */ + ap->th_opcode = ntohs(ap->th_opcode); + if (ap->th_opcode == ERROR) { + printf("Error code %d: %s\n", ap->th_code, + ap->th_msg); + txrx_error = 1; + goto abort; + } + if (ap->th_opcode == ACK) { + int j; + + ap->th_block = ntohs(ap->th_block); + if (ap->th_block == 0 && block == 0) { + /* + * If the extended options are enabled, + * the server just refused 'em all. + * The only one that _really_ + * matters is blksize, but we'll + * clear timeout, too. + */ + blksize = def_blksize; + rexmtval = def_rexmtval; + } + if (ap->th_block == (u_short)block) { + break; + } + /* On an error, try to synchronize + * both sides. + */ + j = synchnet(f); + if (j && trace) { + printf("discarded %d packets\n", + j); + } + if (ap->th_block == (u_short)(block-1)) { + goto send_data; + } + } + if (ap->th_opcode == OACK) { + if (block == 0) { + blksize = def_blksize; + rexmtval = def_rexmtval; + get_options(ap, n); + break; + } + } + } + if (block > 0) + amount += size; + block++; + } while (size == blksize || block == 1); +abort: + fclose(file); + stopclock(); + if (amount > 0) + printstats("Sent", amount); + txrx_error = 1; +} + +/* + * Receive a file. + */ +void +recvfile(fd, name, mode) + int fd; + char *name; + char *mode; +{ + struct tftphdr *ap; + struct tftphdr *dp; + int n, oack=0; + volatile unsigned int block; + volatile int size, firsttrip; + volatile unsigned long amount; + struct sockaddr_storage from; + socklen_t fromlen; + int readlen; + FILE *file; + volatile int convert; /* true if converting crlf -> lf */ + struct sockaddr_storage peer; + struct sockaddr_storage serv; /* valid server port number */ + + startclock(); + dp = w_init(); + ap = (struct tftphdr *)ackbuf; + file = fdopen(fd, "w"); + convert = !strcmp(mode, "netascii"); + block = 1; + firsttrip = 1; + amount = 0; + memcpy(&peer, &peeraddr, peeraddr.ss_len); + memset(&serv, 0, sizeof(serv)); + + signal(SIGALRM, timer); + do { + short rx_opcode; + + if (firsttrip) { + size = makerequest(RRQ, name, ap, mode, 0); + readlen = PKTSIZE; + firsttrip = 0; + } else { + ap->th_opcode = htons((u_short)ACK); + ap->th_block = htons((u_short)(block)); + readlen = blksize+4; + size = 4; + block++; + } + timeout = 0; + (void) setjmp(timeoutbuf); +send_ack: + if (trace) + tpacket("sent", ap, size); + if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peer, + peer.ss_len) != size) { + alarm(0); + warn("sendto"); + goto abort; + } + write_behind(file, convert); + for ( ; ; ) { + int j; + unsigned short rx_block; + + alarm(rexmtval); + do { + fromlen = sizeof(from); + n = recvfrom(f, dp, readlen, 0, + (struct sockaddr *)&from, &fromlen); + } while (n <= 0); + alarm(0); + if (n < 0) { + warn("recvfrom"); + goto abort; + } + if (serv.ss_family != AF_UNSPEC + && !sockaddrcmp((struct sockaddr *)&serv, + (struct sockaddr *)&from)) { + /* ignore this packet */ + if (trace && verbose) + tpacket("ignored", dp, n); + continue; + } + if (trace) + tpacket("received", dp, n); + /* should verify client address */ + rx_opcode = ntohs(dp->th_opcode); + switch (rx_opcode) { + case ERROR: + printf("Error code %d: %s\n", dp->th_code, + dp->th_msg); + txrx_error = 1; + if (serv.ss_family == AF_UNSPEC) { + peer = from; + } + goto abort; + case DATA: + rx_block = ntohs(dp->th_block); + if (rx_block == (u_short)block) { + if (block == 1 && !oack) { + /* no OACK, revert to defaults */ + blksize = def_blksize; + rexmtval = def_rexmtval; + } + if (serv.ss_family == AF_UNSPEC) { + peer = serv = from; + } + goto next_packet; + } + /* On an error, try to synchronize + * both sides. + */ + j = synchnet(f); + if (j && trace) { + printf("discarded %d packets\n", j); + } + if (rx_block == (u_short)(block - 1) + && serv.ss_family != AF_UNSPEC) { + goto send_ack; /* resend ack */ + } + break; + case OACK: + if (block == 1) { + if (serv.ss_family == AF_UNSPEC) { + serv = peer = from; + } + oack = 1; + blksize = def_blksize; + rexmtval = def_rexmtval; + get_options(dp, n); + ap->th_opcode = htons(ACK); + ap->th_block = 0; + readlen = blksize+4; + size = 4; + goto send_ack; + } + break; + default: + break; + } + } +next_packet: + /* size = write(fd, dp->th_data, n - 4); */ + size = writeit(file, &dp, n - 4, convert); + if (size < 0) { + nak(errno + 100, (struct sockaddr *)&peer); + break; + } + amount += size; + } while (size == blksize); +abort: /* ok to ack, since user */ + ap->th_opcode = htons((u_short)ACK); /* has seen err msg */ + ap->th_block = htons((u_short)block); + (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peer, + peer.ss_len); + write_behind(file, convert); /* flush last buffer */ + fclose(file); + stopclock(); + if (amount > 0) + printstats("Received", amount); + txrx_error = 1; +} + +static int +#ifdef __APPLE__ +makerequest(request, name, tp, mode, filesize) +#else +makerequest(request, name, tp, mode) +#endif + int request; + const char *name; + struct tftphdr *tp; + const char *mode; +#ifdef __APPLE__ + off_t filesize; +#endif +{ + char *cp; + + tp->th_opcode = htons((u_short)request); + cp = tp->th_stuff; + strcpy(cp, name); + cp += strlen(name); + *cp++ = '\0'; + strcpy(cp, mode); + cp += strlen(mode); + *cp++ = '\0'; +#ifdef __APPLE__ + if (tsize) { + strcpy(cp, "tsize"); + cp += strlen(cp); + *cp++ = '\0'; + sprintf(cp, "%lu", (unsigned long) filesize); + cp += strlen(cp); + *cp++ = '\0'; + } + if (tout) { + strcpy(cp, "timeout"); + cp += strlen(cp); + *cp++ = '\0'; + sprintf(cp, "%d", rexmtval); + cp += strlen(cp); + *cp++ = '\0'; + } + if (blksize != SEGSIZE) { + strcpy(cp, "blksize"); + cp += strlen(cp); + *cp++ = '\0'; + sprintf(cp, "%d", blksize); + cp += strlen(cp); + *cp++ = '\0'; + } +#endif + return (cp - (char *)tp); +} + +struct errmsg { + int e_code; + const char *e_msg; +} errmsgs[] = { + { EUNDEF, "Undefined error code" }, + { ENOTFOUND, "File not found" }, + { EACCESS, "Access violation" }, + { ENOSPACE, "Disk full or allocation exceeded" }, + { EBADOP, "Illegal TFTP operation" }, + { EBADID, "Unknown transfer ID" }, + { EEXISTS, "File already exists" }, + { ENOUSER, "No such user" }, + { EOPTNEG, "Option negotiation failed" }, + { -1, 0 } +}; + +/* + * Send a nak packet (error message). + * Error code passed in is one of the + * standard TFTP codes, or a UNIX errno + * offset by 100. + */ +static void +nak(error, peer) + int error; + struct sockaddr *peer; +{ + struct errmsg *pe; + struct tftphdr *tp; + int length; + size_t msglen; + + tp = (struct tftphdr *)ackbuf; + tp->th_opcode = htons((u_short)ERROR); + msglen = sizeof(ackbuf) - (&tp->th_msg[0] - ackbuf); + for (pe = errmsgs; pe->e_code >= 0; pe++) + if (pe->e_code == error) + break; + if (pe->e_code < 0) { + tp->th_code = EUNDEF; + strlcpy(tp->th_msg, strerror(error - 100), msglen); + } else { + tp->th_code = htons((u_short)error); + strlcpy(tp->th_msg, pe->e_msg, msglen); + } + length = strlen(tp->th_msg); + msglen = &tp->th_msg[length + 1] - ackbuf; + if (trace) + tpacket("sent", tp, (int)msglen); + if (sendto(f, ackbuf, msglen, 0, peer, peer->sa_len) != msglen) + warn("nak"); +} + +static void +tpacket(s, tp, n) + const char *s; + struct tftphdr *tp; + int n; +{ + static const char *opcodes[] = + { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" }; + char *cp, *file, *endp, *opt = NULL, *spc; + u_short op = ntohs(tp->th_opcode); + int i, o; + + if (op < RRQ || op > OACK) + printf("%s opcode=%x ", s, op); + else + printf("%s %s ", s, opcodes[op]); + switch (op) { + + case RRQ: + case WRQ: + n -= 2; + cp = tp->th_stuff; + endp = cp + n - 1; + if (*endp != '\0') { /* Shouldn't happen, but... */ + *endp = '\0'; + } + file = cp; + cp = strchr(cp, '\0') + 1; + printf("<file=%s, mode=%s", file, cp); + cp = strchr(cp, '\0') + 1; + o = 0; + while (cp < endp) { + i = strlen(cp) + 1; + if (o) { + printf(", %s=%s", opt, cp); + } else { + opt = cp; + } + o = (o+1) % 2; + cp += i; + } + printf(">\n"); + break; + + case DATA: + printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4); + break; + + case ACK: + printf("<block=%d>\n", ntohs(tp->th_block)); + break; + + case ERROR: + printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg); + break; + + case OACK: + o = 0; + n -= 2; + cp = tp->th_stuff; + endp = cp + n - 1; + if (*endp != '\0') { /* Shouldn't happen, but... */ + *endp = '\0'; + } + printf("<"); + spc = ""; + while (cp < endp) { + i = strlen(cp) + 1; + if (o) { + printf("%s%s=%s", spc, opt, cp); + spc = ", "; + } else { + opt = cp; + } + o = (o+1) % 2; + cp += i; + } + printf(">\n"); + break; + } +} + +struct timeval tstart; +struct timeval tstop; + +static void +startclock() +{ + + (void)gettimeofday(&tstart, NULL); +} + +static void +stopclock() +{ + + (void)gettimeofday(&tstop, NULL); +} + +static void +printstats(direction, amount) + const char *direction; + unsigned long amount; +{ + double delta; + /* compute delta in 1/10's second units */ + delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) - + ((tstart.tv_sec*10.)+(tstart.tv_usec/100000)); + delta = delta/10.; /* back to seconds */ + printf("%s %ld bytes in %.1f seconds", direction, amount, delta); + if (verbose) + printf(" [%.0f bits/sec]", (amount*8.)/delta); + putchar('\n'); +} + +static void +timer(sig) + int sig __unused; +{ + + timeout += rexmtval; + if (timeout >= maxtimeout) { + printf("Transfer timed out.\n"); + longjmp(toplevel, -1); + } + txrx_error = 1; + longjmp(timeoutbuf, 1); +} + +typedef union { + struct sockaddr * addr; + struct sockaddr_in * in; + struct sockaddr_in6 * in6; +} sockaddr_union; + +static int +sockaddrcmp(struct sockaddr * sa, struct sockaddr * sb) +{ + sockaddr_union a; + sockaddr_union b; + + if (sa->sa_len != sb->sa_len + || sa->sa_family != sb->sa_family) { + return 0; + } + a.addr = sa; + b.addr = sb; + switch (sa->sa_family) { + case AF_INET: + if (a.in->sin_port != b.in->sin_port + || a.in->sin_addr.s_addr != b.in->sin_addr.s_addr) { + return 0; + } + break; + case AF_INET6: + if (a.in6->sin6_port != b.in6->sin6_port + || !IN6_ARE_ADDR_EQUAL(&a.in6->sin6_addr, + &b.in6->sin6_addr)) { + return 0; + } + break; + default: + if (bcmp(sa, sb, sa->sa_len) != 0) { + return 0; + } + break; + } + return 1; +} diff --git a/remote_cmds/tftp.tproj/tftpsubs.c b/remote_cmds/tftp.tproj/tftpsubs.c new file mode 100644 index 0000000..5dbaa87 --- /dev/null +++ b/remote_cmds/tftp.tproj/tftpsubs.c @@ -0,0 +1,287 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> + +__FBSDID("$FreeBSD: src/usr.bin/tftp/tftpsubs.c,v 1.6 2005/02/14 17:42:58 stefanf Exp $"); + +#ifndef lint +__attribute__((__used__)) +static const char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93"; +#endif + +/* Simple minded read-ahead/write-behind subroutines for tftp user and + server. Written originally with multiple buffers in mind, but current + implementation has two buffer logic wired in. + + Todo: add some sort of final error check so when the write-buffer + is finally flushed, the caller can detect if the disk filled up + (or had an i/o error) and return a nak to the other side. + + Jim Guyton 10/85 + */ + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <netinet/in.h> +#include <arpa/tftp.h> + +#include <stdio.h> +#include <unistd.h> + +#include "tftpsubs.h" + +#ifdef __APPLE__ +struct bf { + int counter; /* size of data in buffer, or flag */ + char buf[MAXPKTSIZE]; /* room for data packet */ +} bfs[2]; +#else +#define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */ + +struct bf { + int counter; /* size of data in buffer, or flag */ + char buf[PKTSIZE]; /* room for data packet */ +} bfs[2]; +#endif + + /* Values for bf.counter */ +#define BF_ALLOC -3 /* alloc'd but not yet filled */ +#define BF_FREE -2 /* free */ +/* [-1 .. SEGSIZE] = size of data in the data buffer */ + +static int nextone; /* index of next buffer to use */ +static int current; /* index of buffer in use */ + + /* control flags for crlf conversions */ +int newline = 0; /* fillbuf: in middle of newline expansion */ +int prevchar = -1; /* putbuf: previous char (cr check) */ + +static struct tftphdr *rw_init(int); + +struct tftphdr *w_init(void) { return rw_init(0); } /* write-behind */ +struct tftphdr *r_init(void) { return rw_init(1); } /* read-ahead */ + +static struct tftphdr * +rw_init(x) /* init for either read-ahead or write-behind */ + int x; /* zero for write-behind, one for read-head */ +{ + newline = 0; /* init crlf flag */ + prevchar = -1; + bfs[0].counter = BF_ALLOC; /* pass out the first buffer */ + current = 0; + bfs[1].counter = BF_FREE; + nextone = x; /* ahead or behind? */ + return (struct tftphdr *)bfs[0].buf; +} + + +/* Have emptied current buffer by sending to net and getting ack. + Free it and return next buffer filled with data. + */ +int +readit(file, dpp, amt, convert) + FILE *file; /* file opened for read */ + struct tftphdr **dpp; + int amt; + int convert; /* if true, convert to ascii */ +{ + struct bf *b; + + bfs[current].counter = BF_FREE; /* free old one */ + current = !current; /* "incr" current */ + + b = &bfs[current]; /* look at new buffer */ + if (b->counter == BF_FREE) /* if it's empty */ + read_ahead(file, amt, convert); /* fill it */ +/* assert(b->counter != BF_FREE);*//* check */ + *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */ + return b->counter; +} + +/* + * fill the input buffer, doing ascii conversions if requested + * conversions are lf -> cr,lf and cr -> cr, nul + */ +void +read_ahead(file, amt, convert) + FILE *file; /* file opened for read */ + int amt; /* number of bytes to read */ + int convert; /* if true, convert to ascii */ +{ + int i; + char *p; + int c; + struct bf *b; + struct tftphdr *dp; + + b = &bfs[nextone]; /* look at "next" buffer */ + if (b->counter != BF_FREE) /* nop if not free */ + return; + nextone = !nextone; /* "incr" next buffer ptr */ + + dp = (struct tftphdr *)b->buf; + + if (convert == 0) { + b->counter = read(fileno(file), dp->th_data, amt); + return; + } + + p = dp->th_data; + for (i = 0 ; i < amt; i++) { + if (newline) { + if (prevchar == '\n') + c = '\n'; /* lf to cr,lf */ + else c = '\0'; /* cr to cr,nul */ + newline = 0; + } + else { + c = getc(file); + if (c == EOF) break; + if (c == '\n' || c == '\r') { + prevchar = c; + c = '\r'; + newline = 1; + } + } + *p++ = c; + } + b->counter = (int)(p - dp->th_data); +} + +/* Update count associated with the buffer, get new buffer + from the queue. Calls write_behind only if next buffer not + available. + */ +int +writeit(file, dpp, ct, convert) + FILE *file; + struct tftphdr **dpp; + int ct, convert; +{ + bfs[current].counter = ct; /* set size of data to write */ + current = !current; /* switch to other buffer */ + if (bfs[current].counter != BF_FREE) /* if not free */ + (void)write_behind(file, convert); /* flush it */ + bfs[current].counter = BF_ALLOC; /* mark as alloc'd */ + *dpp = (struct tftphdr *)bfs[current].buf; + return ct; /* this is a lie of course */ +} + +/* + * Output a buffer to a file, converting from netascii if requested. + * CR,NUL -> CR and CR,LF => LF. + * Note spec is undefined if we get CR as last byte of file or a + * CR followed by anything else. In this case we leave it alone. + */ +int +write_behind(file, convert) + FILE *file; + int convert; +{ + char *buf; + int count; + int ct; + char *p; + int c; /* current character */ + struct bf *b; + struct tftphdr *dp; + + b = &bfs[nextone]; + if (b->counter < -1) /* anything to flush? */ + return 0; /* just nop if nothing to do */ + + count = b->counter; /* remember byte count */ + b->counter = BF_FREE; /* reset flag */ + dp = (struct tftphdr *)b->buf; + nextone = !nextone; /* incr for next time */ + buf = dp->th_data; + + if (count <= 0) return -1; /* nak logic? */ + + if (convert == 0) + return write(fileno(file), buf, count); + + p = buf; + ct = count; + while (ct--) { /* loop over the buffer */ + c = *p++; /* pick up a character */ + if (prevchar == '\r') { /* if prev char was cr */ + if (c == '\n') /* if have cr,lf then just */ + fseek(file, -1, 1); /* smash lf on top of the cr */ + else + if (c == '\0') /* if have cr,nul then */ + goto skipit; /* just skip over the putc */ + /* else just fall through and allow it */ + } + putc(c, file); +skipit: + prevchar = c; + } + return count; +} + + +/* When an error has occurred, it is possible that the two sides + * are out of synch. Ie: that what I think is the other side's + * response to packet N is really their response to packet N-1. + * + * So, to try to prevent that, we flush all the input queued up + * for us on the network connection on our host. + * + * We return the number of packets we flushed (mostly for reporting + * when trace is active). + */ + +int +synchnet(f) + int f; /* socket to flush */ +{ + int i, j = 0; + char rbuf[PKTSIZE]; + struct sockaddr_storage from; + socklen_t fromlen; + + while (1) { + (void) ioctl(f, FIONREAD, &i); + if (i) { + j++; + fromlen = sizeof from; + (void) recvfrom(f, rbuf, sizeof (rbuf), 0, + (struct sockaddr *)&from, &fromlen); + } else { + return(j); + } + } +} diff --git a/remote_cmds/tftp.tproj/tftpsubs.h b/remote_cmds/tftp.tproj/tftpsubs.h new file mode 100644 index 0000000..808acb2 --- /dev/null +++ b/remote_cmds/tftp.tproj/tftpsubs.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)tftpsubs.h 8.1 (Berkeley) 6/6/93 + * $FreeBSD: src/usr.bin/tftp/tftpsubs.h,v 1.2 2002/03/22 01:42:34 imp Exp $ + */ + +/* + * Prototypes for read-ahead/write-behind subroutines for tftp user and + * server. + */ +struct tftphdr *r_init(void); +#ifdef __APPLE__ +void read_ahead(FILE *, int, int); +int readit(FILE *, struct tftphdr **, int, int); +#else +void read_ahead(FILE *, int); +int readit(FILE *, struct tftphdr **, int); +#endif + +int synchnet(int); + +struct tftphdr *w_init(void); +int write_behind(FILE *, int); +int writeit(FILE *, struct tftphdr **, int, int); |