tftpd.c revision 1.12 1 1.12 mrg /* $NetBSD: tftpd.c,v 1.12 1998/07/03 11:50:51 mrg Exp $ */
2 1.9 mrg
3 1.1 cgd /*
4 1.9 mrg * Copyright (c) 1983, 1993
5 1.9 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.9 mrg #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.9 mrg __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
39 1.9 mrg The Regents of the University of California. All rights reserved.\n");
40 1.9 mrg #if 0
41 1.9 mrg static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
42 1.9 mrg #else
43 1.12 mrg __RCSID("$NetBSD: tftpd.c,v 1.12 1998/07/03 11:50:51 mrg Exp $");
44 1.9 mrg #endif
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd /*
48 1.1 cgd * Trivial file transfer protocol server.
49 1.1 cgd *
50 1.9 mrg * This version includes many modifications by Jim Guyton
51 1.9 mrg * <guyton@rand-unix>.
52 1.1 cgd */
53 1.1 cgd
54 1.9 mrg #include <sys/param.h>
55 1.1 cgd #include <sys/ioctl.h>
56 1.1 cgd #include <sys/stat.h>
57 1.9 mrg #include <sys/socket.h>
58 1.9 mrg
59 1.1 cgd #include <signal.h>
60 1.1 cgd #include <fcntl.h>
61 1.1 cgd
62 1.1 cgd #include <netinet/in.h>
63 1.1 cgd #include <arpa/tftp.h>
64 1.9 mrg #include <arpa/inet.h>
65 1.9 mrg
66 1.9 mrg #include <ctype.h>
67 1.9 mrg #include <errno.h>
68 1.9 mrg #include <fcntl.h>
69 1.1 cgd #include <netdb.h>
70 1.1 cgd #include <setjmp.h>
71 1.9 mrg #include <signal.h>
72 1.1 cgd #include <stdio.h>
73 1.9 mrg #include <stdlib.h>
74 1.1 cgd #include <string.h>
75 1.9 mrg #include <syslog.h>
76 1.9 mrg #include <unistd.h>
77 1.9 mrg
78 1.9 mrg #include "tftpsubs.h"
79 1.1 cgd
80 1.7 jtc /* XXX svr4 defines UID_NOBODY and GID_NOBODY constants in <sys/param.h> */
81 1.7 jtc #define UID_NOBODY 32767
82 1.7 jtc #define GID_NOBODY 32766
83 1.7 jtc
84 1.1 cgd #define TIMEOUT 5
85 1.1 cgd
86 1.5 cgd extern char *__progname;
87 1.1 cgd int peer;
88 1.1 cgd int rexmtval = TIMEOUT;
89 1.1 cgd int maxtimeout = 5*TIMEOUT;
90 1.1 cgd
91 1.1 cgd #define PKTSIZE SEGSIZE+4
92 1.1 cgd char buf[PKTSIZE];
93 1.1 cgd char ackbuf[PKTSIZE];
94 1.1 cgd struct sockaddr_in from;
95 1.1 cgd int fromlen;
96 1.1 cgd
97 1.9 mrg /*
98 1.9 mrg * Null-terminated directory prefix list for absolute pathname requests and
99 1.9 mrg * search list for relative pathname requests.
100 1.9 mrg *
101 1.9 mrg * MAXDIRS should be at least as large as the number of arguments that
102 1.9 mrg * inetd allows (currently 20).
103 1.9 mrg */
104 1.9 mrg #define MAXDIRS 20
105 1.9 mrg static struct dirlist {
106 1.9 mrg char *name;
107 1.9 mrg int len;
108 1.9 mrg } dirs[MAXDIRS+1];
109 1.9 mrg static int suppress_naks;
110 1.9 mrg static int logging;
111 1.9 mrg static int secure;
112 1.9 mrg static char *securedir;
113 1.9 mrg
114 1.9 mrg struct formats;
115 1.9 mrg
116 1.9 mrg static void tftp __P((struct tftphdr *, int));
117 1.9 mrg static char *errtomsg __P((int));
118 1.9 mrg static void nak __P((int));
119 1.9 mrg static char *verifyhost __P((struct sockaddr_in *));
120 1.9 mrg static void usage __P((void));
121 1.9 mrg void timer __P((int));
122 1.9 mrg void sendfile __P((struct formats *));
123 1.9 mrg void recvfile __P((struct formats *));
124 1.9 mrg void justquit __P((int));
125 1.9 mrg int validate_access __P((char **, int));
126 1.9 mrg int main __P((int, char **));
127 1.1 cgd
128 1.9 mrg struct formats {
129 1.9 mrg char *f_mode;
130 1.9 mrg int (*f_validate) __P((char **, int));
131 1.9 mrg void (*f_send) __P((struct formats *));
132 1.9 mrg void (*f_recv) __P((struct formats *));
133 1.9 mrg int f_convert;
134 1.9 mrg } formats[] = {
135 1.9 mrg { "netascii", validate_access, sendfile, recvfile, 1 },
136 1.9 mrg { "octet", validate_access, sendfile, recvfile, 0 },
137 1.9 mrg { 0 }
138 1.9 mrg };
139 1.4 mycroft
140 1.5 cgd static void
141 1.5 cgd usage()
142 1.5 cgd {
143 1.5 cgd syslog(LOG_ERR, "Usage: %s [-s] [directory ...]\n", __progname);
144 1.5 cgd exit(1);
145 1.5 cgd }
146 1.5 cgd
147 1.9 mrg int
148 1.5 cgd main(argc, argv)
149 1.5 cgd int argc;
150 1.5 cgd char **argv;
151 1.1 cgd {
152 1.1 cgd register struct tftphdr *tp;
153 1.1 cgd register int n = 0;
154 1.9 mrg int ch, on;
155 1.5 cgd int fd = 0;
156 1.9 mrg struct sockaddr_in sin;
157 1.1 cgd
158 1.11 lukem openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
159 1.5 cgd
160 1.9 mrg while ((ch = getopt(argc, argv, "lns:")) != -1)
161 1.9 mrg switch (ch) {
162 1.9 mrg case 'l':
163 1.9 mrg logging = 1;
164 1.9 mrg break;
165 1.9 mrg
166 1.9 mrg case 'n':
167 1.9 mrg suppress_naks = 1;
168 1.9 mrg break;
169 1.9 mrg
170 1.5 cgd case 's':
171 1.5 cgd secure = 1;
172 1.9 mrg securedir = optarg;
173 1.5 cgd break;
174 1.5 cgd
175 1.5 cgd default:
176 1.5 cgd usage();
177 1.5 cgd break;
178 1.5 cgd }
179 1.5 cgd
180 1.9 mrg if (optind < argc) {
181 1.9 mrg struct dirlist *dirp;
182 1.9 mrg
183 1.9 mrg /* Get list of directory prefixes. Skip relative pathnames. */
184 1.9 mrg for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
185 1.9 mrg optind++) {
186 1.9 mrg if (argv[optind][0] == '/') {
187 1.9 mrg dirp->name = argv[optind];
188 1.9 mrg dirp->len = strlen(dirp->name);
189 1.9 mrg dirp++;
190 1.9 mrg }
191 1.9 mrg }
192 1.9 mrg }
193 1.9 mrg
194 1.9 mrg if (secure) {
195 1.9 mrg if (chdir(securedir) < 0) {
196 1.9 mrg syslog(LOG_ERR, "chdir %s: %m", securedir);
197 1.9 mrg exit(1);
198 1.4 mycroft }
199 1.9 mrg if (chroot(".")) {
200 1.9 mrg syslog(LOG_ERR, "chroot: %m\n");
201 1.4 mycroft exit(1);
202 1.4 mycroft }
203 1.4 mycroft }
204 1.5 cgd
205 1.7 jtc if (setgid(GID_NOBODY)) {
206 1.7 jtc syslog(LOG_ERR, "setgid: %m");
207 1.8 mrg exit(1);
208 1.8 mrg }
209 1.8 mrg
210 1.8 mrg if (setgroups(0, NULL)) {
211 1.8 mrg syslog(LOG_ERR, "setgroups: %m");
212 1.7 jtc exit(1);
213 1.7 jtc }
214 1.7 jtc
215 1.7 jtc if (setuid(UID_NOBODY)) {
216 1.7 jtc syslog(LOG_ERR, "setuid: %m");
217 1.4 mycroft exit(1);
218 1.4 mycroft }
219 1.5 cgd
220 1.9 mrg on = 1;
221 1.5 cgd if (ioctl(fd, FIONBIO, &on) < 0) {
222 1.1 cgd syslog(LOG_ERR, "ioctl(FIONBIO): %m\n");
223 1.1 cgd exit(1);
224 1.1 cgd }
225 1.1 cgd fromlen = sizeof (from);
226 1.5 cgd n = recvfrom(fd, buf, sizeof (buf), 0,
227 1.1 cgd (struct sockaddr *)&from, &fromlen);
228 1.1 cgd if (n < 0) {
229 1.1 cgd syslog(LOG_ERR, "recvfrom: %m\n");
230 1.1 cgd exit(1);
231 1.1 cgd }
232 1.1 cgd /*
233 1.1 cgd * Now that we have read the message out of the UDP
234 1.1 cgd * socket, we fork and exit. Thus, inetd will go back
235 1.1 cgd * to listening to the tftp port, and the next request
236 1.1 cgd * to come in will start up a new instance of tftpd.
237 1.1 cgd *
238 1.1 cgd * We do this so that inetd can run tftpd in "wait" mode.
239 1.1 cgd * The problem with tftpd running in "nowait" mode is that
240 1.1 cgd * inetd may get one or more successful "selects" on the
241 1.1 cgd * tftp port before we do our receive, so more than one
242 1.1 cgd * instance of tftpd may be started up. Worse, if tftpd
243 1.1 cgd * break before doing the above "recvfrom", inetd would
244 1.1 cgd * spawn endless instances, clogging the system.
245 1.1 cgd */
246 1.1 cgd {
247 1.1 cgd int pid;
248 1.1 cgd int i, j;
249 1.1 cgd
250 1.1 cgd for (i = 1; i < 20; i++) {
251 1.1 cgd pid = fork();
252 1.1 cgd if (pid < 0) {
253 1.1 cgd sleep(i);
254 1.1 cgd /*
255 1.1 cgd * flush out to most recently sent request.
256 1.1 cgd *
257 1.1 cgd * This may drop some request, but those
258 1.1 cgd * will be resent by the clients when
259 1.1 cgd * they timeout. The positive effect of
260 1.1 cgd * this flush is to (try to) prevent more
261 1.1 cgd * than one tftpd being started up to service
262 1.1 cgd * a single request from a single client.
263 1.1 cgd */
264 1.1 cgd j = sizeof from;
265 1.5 cgd i = recvfrom(fd, buf, sizeof (buf), 0,
266 1.1 cgd (struct sockaddr *)&from, &j);
267 1.1 cgd if (i > 0) {
268 1.1 cgd n = i;
269 1.1 cgd fromlen = j;
270 1.1 cgd }
271 1.1 cgd } else {
272 1.1 cgd break;
273 1.1 cgd }
274 1.1 cgd }
275 1.1 cgd if (pid < 0) {
276 1.1 cgd syslog(LOG_ERR, "fork: %m\n");
277 1.1 cgd exit(1);
278 1.1 cgd } else if (pid != 0) {
279 1.1 cgd exit(0);
280 1.1 cgd }
281 1.1 cgd }
282 1.6 mycroft from.sin_len = sizeof(struct sockaddr_in);
283 1.1 cgd from.sin_family = AF_INET;
284 1.1 cgd alarm(0);
285 1.5 cgd close(fd);
286 1.1 cgd close(1);
287 1.1 cgd peer = socket(AF_INET, SOCK_DGRAM, 0);
288 1.1 cgd if (peer < 0) {
289 1.1 cgd syslog(LOG_ERR, "socket: %m\n");
290 1.1 cgd exit(1);
291 1.1 cgd }
292 1.9 mrg memset(&sin, 0, sizeof(sin));
293 1.9 mrg sin.sin_family = AF_INET;
294 1.9 mrg if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
295 1.1 cgd syslog(LOG_ERR, "bind: %m\n");
296 1.1 cgd exit(1);
297 1.1 cgd }
298 1.1 cgd if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
299 1.1 cgd syslog(LOG_ERR, "connect: %m\n");
300 1.1 cgd exit(1);
301 1.1 cgd }
302 1.1 cgd tp = (struct tftphdr *)buf;
303 1.1 cgd tp->th_opcode = ntohs(tp->th_opcode);
304 1.1 cgd if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
305 1.1 cgd tftp(tp, n);
306 1.1 cgd exit(1);
307 1.1 cgd }
308 1.1 cgd
309 1.1 cgd /*
310 1.1 cgd * Handle initial connection protocol.
311 1.1 cgd */
312 1.9 mrg static void
313 1.1 cgd tftp(tp, size)
314 1.1 cgd struct tftphdr *tp;
315 1.1 cgd int size;
316 1.1 cgd {
317 1.1 cgd register char *cp;
318 1.1 cgd int first = 1, ecode;
319 1.1 cgd register struct formats *pf;
320 1.9 mrg char *filename, *mode = NULL; /* XXX gcc */
321 1.1 cgd
322 1.1 cgd filename = cp = tp->th_stuff;
323 1.1 cgd again:
324 1.1 cgd while (cp < buf + size) {
325 1.1 cgd if (*cp == '\0')
326 1.1 cgd break;
327 1.1 cgd cp++;
328 1.1 cgd }
329 1.1 cgd if (*cp != '\0') {
330 1.1 cgd nak(EBADOP);
331 1.1 cgd exit(1);
332 1.1 cgd }
333 1.1 cgd if (first) {
334 1.1 cgd mode = ++cp;
335 1.1 cgd first = 0;
336 1.1 cgd goto again;
337 1.1 cgd }
338 1.1 cgd for (cp = mode; *cp; cp++)
339 1.1 cgd if (isupper(*cp))
340 1.1 cgd *cp = tolower(*cp);
341 1.1 cgd for (pf = formats; pf->f_mode; pf++)
342 1.1 cgd if (strcmp(pf->f_mode, mode) == 0)
343 1.1 cgd break;
344 1.1 cgd if (pf->f_mode == 0) {
345 1.1 cgd nak(EBADOP);
346 1.1 cgd exit(1);
347 1.1 cgd }
348 1.9 mrg ecode = (*pf->f_validate)(&filename, tp->th_opcode);
349 1.9 mrg if (logging) {
350 1.9 mrg syslog(LOG_INFO, "%s: %s request for %s: %s",
351 1.9 mrg verifyhost(&from),
352 1.9 mrg tp->th_opcode == WRQ ? "write" : "read",
353 1.9 mrg filename, errtomsg(ecode));
354 1.9 mrg }
355 1.1 cgd if (ecode) {
356 1.9 mrg /*
357 1.9 mrg * Avoid storms of naks to a RRQ broadcast for a relative
358 1.9 mrg * bootfile pathname from a diskless Sun.
359 1.9 mrg */
360 1.9 mrg if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
361 1.9 mrg exit(0);
362 1.1 cgd nak(ecode);
363 1.1 cgd exit(1);
364 1.1 cgd }
365 1.1 cgd if (tp->th_opcode == WRQ)
366 1.1 cgd (*pf->f_recv)(pf);
367 1.1 cgd else
368 1.1 cgd (*pf->f_send)(pf);
369 1.1 cgd exit(0);
370 1.1 cgd }
371 1.1 cgd
372 1.1 cgd
373 1.1 cgd FILE *file;
374 1.1 cgd
375 1.1 cgd /*
376 1.1 cgd * Validate file access. Since we
377 1.1 cgd * have no uid or gid, for now require
378 1.1 cgd * file to exist and be publicly
379 1.1 cgd * readable/writable.
380 1.1 cgd * If we were invoked with arguments
381 1.1 cgd * from inetd then the file must also be
382 1.1 cgd * in one of the given directory prefixes.
383 1.1 cgd * Note also, full path name must be
384 1.1 cgd * given as we have no login directory.
385 1.1 cgd */
386 1.9 mrg int
387 1.9 mrg validate_access(filep, mode)
388 1.9 mrg char **filep;
389 1.1 cgd int mode;
390 1.1 cgd {
391 1.1 cgd struct stat stbuf;
392 1.1 cgd int fd;
393 1.9 mrg struct dirlist *dirp;
394 1.9 mrg static char pathname[MAXPATHLEN];
395 1.9 mrg char *filename = *filep;
396 1.1 cgd
397 1.9 mrg /*
398 1.9 mrg * Prevent tricksters from getting around the directory restrictions
399 1.9 mrg */
400 1.9 mrg if (strstr(filename, "/../"))
401 1.9 mrg return (EACCESS);
402 1.9 mrg
403 1.9 mrg if (*filename == '/') {
404 1.4 mycroft /*
405 1.9 mrg * Allow the request if it's in one of the approved locations.
406 1.9 mrg * Special case: check the null prefix ("/") by looking
407 1.9 mrg * for length = 1 and relying on the arg. processing that
408 1.9 mrg * it's a /.
409 1.4 mycroft */
410 1.9 mrg for (dirp = dirs; dirp->name != NULL; dirp++) {
411 1.9 mrg if (dirp->len == 1 ||
412 1.9 mrg (!strncmp(filename, dirp->name, dirp->len) &&
413 1.9 mrg filename[dirp->len] == '/'))
414 1.9 mrg break;
415 1.9 mrg }
416 1.9 mrg /* If directory list is empty, allow access to any file */
417 1.9 mrg if (dirp->name == NULL && dirp != dirs)
418 1.1 cgd return (EACCESS);
419 1.9 mrg if (stat(filename, &stbuf) < 0)
420 1.9 mrg return (errno == ENOENT ? ENOTFOUND : EACCESS);
421 1.10 mycroft if (!S_ISREG(stbuf.st_mode))
422 1.9 mrg return (ENOTFOUND);
423 1.9 mrg if (mode == RRQ) {
424 1.9 mrg if ((stbuf.st_mode & S_IROTH) == 0)
425 1.9 mrg return (EACCESS);
426 1.9 mrg } else {
427 1.9 mrg if ((stbuf.st_mode & S_IWOTH) == 0)
428 1.9 mrg return (EACCESS);
429 1.9 mrg }
430 1.1 cgd } else {
431 1.9 mrg int err;
432 1.9 mrg
433 1.9 mrg /*
434 1.9 mrg * Relative file name: search the approved locations for it.
435 1.9 mrg * Don't allow write requests or ones that avoid directory
436 1.9 mrg * restrictions.
437 1.9 mrg */
438 1.9 mrg
439 1.9 mrg if (mode != RRQ || !strncmp(filename, "../", 3))
440 1.1 cgd return (EACCESS);
441 1.9 mrg
442 1.9 mrg /*
443 1.9 mrg * If the file exists in one of the directories and isn't
444 1.9 mrg * readable, continue looking. However, change the error code
445 1.9 mrg * to give an indication that the file exists.
446 1.9 mrg */
447 1.9 mrg err = ENOTFOUND;
448 1.9 mrg if (dirs[0].name != NULL) {
449 1.9 mrg for (dirp = dirs; dirp->name != NULL; dirp++) {
450 1.9 mrg snprintf(pathname, sizeof pathname, "%s/%s",
451 1.9 mrg dirp->name, filename);
452 1.9 mrg if (stat(pathname, &stbuf) == 0 &&
453 1.9 mrg (stbuf.st_mode & S_IFMT) == S_IFREG) {
454 1.9 mrg if ((stbuf.st_mode & S_IROTH) != 0)
455 1.9 mrg break;
456 1.9 mrg err = EACCESS;
457 1.9 mrg }
458 1.9 mrg }
459 1.9 mrg if (dirp->name == NULL)
460 1.9 mrg return (err);
461 1.9 mrg *filep = filename = pathname;
462 1.9 mrg } else
463 1.9 mrg *filep = filename;
464 1.1 cgd }
465 1.1 cgd fd = open(filename, mode == RRQ ? 0 : 1);
466 1.1 cgd if (fd < 0)
467 1.1 cgd return (errno + 100);
468 1.1 cgd file = fdopen(fd, (mode == RRQ)? "r":"w");
469 1.1 cgd if (file == NULL) {
470 1.1 cgd return errno+100;
471 1.1 cgd }
472 1.1 cgd return (0);
473 1.1 cgd }
474 1.1 cgd
475 1.1 cgd int timeout;
476 1.1 cgd jmp_buf timeoutbuf;
477 1.1 cgd
478 1.1 cgd void
479 1.9 mrg timer(dummy)
480 1.9 mrg int dummy;
481 1.1 cgd {
482 1.1 cgd
483 1.1 cgd timeout += rexmtval;
484 1.1 cgd if (timeout >= maxtimeout)
485 1.1 cgd exit(1);
486 1.1 cgd longjmp(timeoutbuf, 1);
487 1.1 cgd }
488 1.1 cgd
489 1.1 cgd /*
490 1.1 cgd * Send the requested file.
491 1.1 cgd */
492 1.9 mrg void
493 1.1 cgd sendfile(pf)
494 1.1 cgd struct formats *pf;
495 1.1 cgd {
496 1.9 mrg struct tftphdr *dp;
497 1.1 cgd register struct tftphdr *ap; /* ack packet */
498 1.9 mrg register int size, n;
499 1.9 mrg volatile int block;
500 1.1 cgd
501 1.1 cgd signal(SIGALRM, timer);
502 1.1 cgd dp = r_init();
503 1.1 cgd ap = (struct tftphdr *)ackbuf;
504 1.9 mrg block = 1;
505 1.1 cgd do {
506 1.1 cgd size = readit(file, &dp, pf->f_convert);
507 1.1 cgd if (size < 0) {
508 1.1 cgd nak(errno + 100);
509 1.1 cgd goto abort;
510 1.1 cgd }
511 1.1 cgd dp->th_opcode = htons((u_short)DATA);
512 1.1 cgd dp->th_block = htons((u_short)block);
513 1.1 cgd timeout = 0;
514 1.9 mrg (void)setjmp(timeoutbuf);
515 1.1 cgd
516 1.1 cgd send_data:
517 1.1 cgd if (send(peer, dp, size + 4, 0) != size + 4) {
518 1.1 cgd syslog(LOG_ERR, "tftpd: write: %m\n");
519 1.1 cgd goto abort;
520 1.1 cgd }
521 1.1 cgd read_ahead(file, pf->f_convert);
522 1.1 cgd for ( ; ; ) {
523 1.1 cgd alarm(rexmtval); /* read the ack */
524 1.1 cgd n = recv(peer, ackbuf, sizeof (ackbuf), 0);
525 1.1 cgd alarm(0);
526 1.1 cgd if (n < 0) {
527 1.1 cgd syslog(LOG_ERR, "tftpd: read: %m\n");
528 1.1 cgd goto abort;
529 1.1 cgd }
530 1.1 cgd ap->th_opcode = ntohs((u_short)ap->th_opcode);
531 1.1 cgd ap->th_block = ntohs((u_short)ap->th_block);
532 1.1 cgd
533 1.1 cgd if (ap->th_opcode == ERROR)
534 1.1 cgd goto abort;
535 1.9 mrg
536 1.1 cgd if (ap->th_opcode == ACK) {
537 1.9 mrg if (ap->th_block == block)
538 1.1 cgd break;
539 1.1 cgd /* Re-synchronize with the other side */
540 1.1 cgd (void) synchnet(peer);
541 1.9 mrg if (ap->th_block == (block -1))
542 1.1 cgd goto send_data;
543 1.1 cgd }
544 1.1 cgd
545 1.1 cgd }
546 1.1 cgd block++;
547 1.1 cgd } while (size == SEGSIZE);
548 1.1 cgd abort:
549 1.1 cgd (void) fclose(file);
550 1.1 cgd }
551 1.1 cgd
552 1.1 cgd void
553 1.9 mrg justquit(dummy)
554 1.9 mrg int dummy;
555 1.1 cgd {
556 1.1 cgd exit(0);
557 1.1 cgd }
558 1.1 cgd
559 1.1 cgd /*
560 1.1 cgd * Receive a file.
561 1.1 cgd */
562 1.9 mrg void
563 1.1 cgd recvfile(pf)
564 1.1 cgd struct formats *pf;
565 1.1 cgd {
566 1.9 mrg struct tftphdr *dp;
567 1.1 cgd register struct tftphdr *ap; /* ack buffer */
568 1.9 mrg register int n, size;
569 1.9 mrg volatile int block;
570 1.1 cgd
571 1.1 cgd signal(SIGALRM, timer);
572 1.1 cgd dp = w_init();
573 1.1 cgd ap = (struct tftphdr *)ackbuf;
574 1.9 mrg block = 0;
575 1.1 cgd do {
576 1.1 cgd timeout = 0;
577 1.1 cgd ap->th_opcode = htons((u_short)ACK);
578 1.1 cgd ap->th_block = htons((u_short)block);
579 1.1 cgd block++;
580 1.1 cgd (void) setjmp(timeoutbuf);
581 1.1 cgd send_ack:
582 1.1 cgd if (send(peer, ackbuf, 4, 0) != 4) {
583 1.1 cgd syslog(LOG_ERR, "tftpd: write: %m\n");
584 1.1 cgd goto abort;
585 1.1 cgd }
586 1.1 cgd write_behind(file, pf->f_convert);
587 1.1 cgd for ( ; ; ) {
588 1.1 cgd alarm(rexmtval);
589 1.1 cgd n = recv(peer, dp, PKTSIZE, 0);
590 1.1 cgd alarm(0);
591 1.1 cgd if (n < 0) { /* really? */
592 1.1 cgd syslog(LOG_ERR, "tftpd: read: %m\n");
593 1.1 cgd goto abort;
594 1.1 cgd }
595 1.1 cgd dp->th_opcode = ntohs((u_short)dp->th_opcode);
596 1.1 cgd dp->th_block = ntohs((u_short)dp->th_block);
597 1.1 cgd if (dp->th_opcode == ERROR)
598 1.1 cgd goto abort;
599 1.1 cgd if (dp->th_opcode == DATA) {
600 1.1 cgd if (dp->th_block == block) {
601 1.1 cgd break; /* normal */
602 1.1 cgd }
603 1.1 cgd /* Re-synchronize with the other side */
604 1.1 cgd (void) synchnet(peer);
605 1.1 cgd if (dp->th_block == (block-1))
606 1.1 cgd goto send_ack; /* rexmit */
607 1.1 cgd }
608 1.1 cgd }
609 1.1 cgd /* size = write(file, dp->th_data, n - 4); */
610 1.1 cgd size = writeit(file, &dp, n - 4, pf->f_convert);
611 1.1 cgd if (size != (n-4)) { /* ahem */
612 1.1 cgd if (size < 0) nak(errno + 100);
613 1.1 cgd else nak(ENOSPACE);
614 1.1 cgd goto abort;
615 1.1 cgd }
616 1.1 cgd } while (size == SEGSIZE);
617 1.1 cgd write_behind(file, pf->f_convert);
618 1.1 cgd (void) fclose(file); /* close data file */
619 1.1 cgd
620 1.1 cgd ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */
621 1.1 cgd ap->th_block = htons((u_short)(block));
622 1.1 cgd (void) send(peer, ackbuf, 4, 0);
623 1.1 cgd
624 1.1 cgd signal(SIGALRM, justquit); /* just quit on timeout */
625 1.1 cgd alarm(rexmtval);
626 1.1 cgd n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
627 1.1 cgd alarm(0);
628 1.1 cgd if (n >= 4 && /* if read some data */
629 1.1 cgd dp->th_opcode == DATA && /* and got a data block */
630 1.1 cgd block == dp->th_block) { /* then my last ack was lost */
631 1.1 cgd (void) send(peer, ackbuf, 4, 0); /* resend final ack */
632 1.1 cgd }
633 1.1 cgd abort:
634 1.1 cgd return;
635 1.1 cgd }
636 1.1 cgd
637 1.1 cgd struct errmsg {
638 1.1 cgd int e_code;
639 1.1 cgd char *e_msg;
640 1.1 cgd } errmsgs[] = {
641 1.1 cgd { EUNDEF, "Undefined error code" },
642 1.1 cgd { ENOTFOUND, "File not found" },
643 1.1 cgd { EACCESS, "Access violation" },
644 1.1 cgd { ENOSPACE, "Disk full or allocation exceeded" },
645 1.1 cgd { EBADOP, "Illegal TFTP operation" },
646 1.1 cgd { EBADID, "Unknown transfer ID" },
647 1.1 cgd { EEXISTS, "File already exists" },
648 1.1 cgd { ENOUSER, "No such user" },
649 1.1 cgd { -1, 0 }
650 1.1 cgd };
651 1.1 cgd
652 1.9 mrg static char *
653 1.9 mrg errtomsg(error)
654 1.9 mrg int error;
655 1.9 mrg {
656 1.9 mrg static char buf[20];
657 1.9 mrg register struct errmsg *pe;
658 1.9 mrg
659 1.9 mrg if (error == 0)
660 1.9 mrg return "success";
661 1.9 mrg for (pe = errmsgs; pe->e_code >= 0; pe++)
662 1.9 mrg if (pe->e_code == error)
663 1.9 mrg return pe->e_msg;
664 1.9 mrg sprintf(buf, "error %d", error);
665 1.9 mrg return buf;
666 1.9 mrg }
667 1.9 mrg
668 1.1 cgd /*
669 1.1 cgd * Send a nak packet (error message).
670 1.1 cgd * Error code passed in is one of the
671 1.1 cgd * standard TFTP codes, or a UNIX errno
672 1.1 cgd * offset by 100.
673 1.1 cgd */
674 1.9 mrg static void
675 1.1 cgd nak(error)
676 1.1 cgd int error;
677 1.1 cgd {
678 1.1 cgd register struct tftphdr *tp;
679 1.1 cgd int length;
680 1.1 cgd register struct errmsg *pe;
681 1.1 cgd
682 1.1 cgd tp = (struct tftphdr *)buf;
683 1.1 cgd tp->th_opcode = htons((u_short)ERROR);
684 1.1 cgd tp->th_code = htons((u_short)error);
685 1.1 cgd for (pe = errmsgs; pe->e_code >= 0; pe++)
686 1.1 cgd if (pe->e_code == error)
687 1.1 cgd break;
688 1.1 cgd if (pe->e_code < 0) {
689 1.1 cgd pe->e_msg = strerror(error - 100);
690 1.1 cgd tp->th_code = EUNDEF; /* set 'undef' errorcode */
691 1.1 cgd }
692 1.1 cgd strcpy(tp->th_msg, pe->e_msg);
693 1.1 cgd length = strlen(pe->e_msg);
694 1.1 cgd tp->th_msg[length] = '\0';
695 1.1 cgd length += 5;
696 1.1 cgd if (send(peer, buf, length, 0) != length)
697 1.1 cgd syslog(LOG_ERR, "nak: %m\n");
698 1.9 mrg }
699 1.9 mrg
700 1.9 mrg static char *
701 1.9 mrg verifyhost(fromp)
702 1.9 mrg struct sockaddr_in *fromp;
703 1.9 mrg {
704 1.9 mrg struct hostent *hp;
705 1.9 mrg
706 1.9 mrg hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (fromp->sin_addr),
707 1.9 mrg fromp->sin_family);
708 1.9 mrg if (hp)
709 1.9 mrg return hp->h_name;
710 1.9 mrg else
711 1.9 mrg return inet_ntoa(fromp->sin_addr);
712 1.1 cgd }
713