tftpd.c revision 1.27 1 1.27 briggs /* $NetBSD: tftpd.c,v 1.27 2004/01/06 14:30:10 briggs 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.26 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.9 mrg #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.9 mrg __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
35 1.9 mrg The Regents of the University of California. All rights reserved.\n");
36 1.9 mrg #if 0
37 1.9 mrg static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
38 1.9 mrg #else
39 1.27 briggs __RCSID("$NetBSD: tftpd.c,v 1.27 2004/01/06 14:30:10 briggs Exp $");
40 1.9 mrg #endif
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd /*
44 1.1 cgd * Trivial file transfer protocol server.
45 1.1 cgd *
46 1.9 mrg * This version includes many modifications by Jim Guyton
47 1.9 mrg * <guyton@rand-unix>.
48 1.1 cgd */
49 1.1 cgd
50 1.9 mrg #include <sys/param.h>
51 1.1 cgd #include <sys/ioctl.h>
52 1.1 cgd #include <sys/stat.h>
53 1.9 mrg #include <sys/socket.h>
54 1.9 mrg
55 1.1 cgd #include <netinet/in.h>
56 1.1 cgd #include <arpa/tftp.h>
57 1.9 mrg #include <arpa/inet.h>
58 1.9 mrg
59 1.9 mrg #include <ctype.h>
60 1.9 mrg #include <errno.h>
61 1.9 mrg #include <fcntl.h>
62 1.14 lukem #include <grp.h>
63 1.1 cgd #include <netdb.h>
64 1.14 lukem #include <pwd.h>
65 1.1 cgd #include <setjmp.h>
66 1.9 mrg #include <signal.h>
67 1.1 cgd #include <stdio.h>
68 1.9 mrg #include <stdlib.h>
69 1.1 cgd #include <string.h>
70 1.9 mrg #include <syslog.h>
71 1.9 mrg #include <unistd.h>
72 1.9 mrg
73 1.9 mrg #include "tftpsubs.h"
74 1.1 cgd
75 1.14 lukem #define DEFAULTUSER "nobody"
76 1.7 jtc
77 1.1 cgd #define TIMEOUT 5
78 1.1 cgd
79 1.1 cgd int peer;
80 1.1 cgd int rexmtval = TIMEOUT;
81 1.1 cgd int maxtimeout = 5*TIMEOUT;
82 1.1 cgd
83 1.25 briggs char buf[MAXPKTSIZE];
84 1.1 cgd char ackbuf[PKTSIZE];
85 1.25 briggs char oackbuf[PKTSIZE];
86 1.18 itojun struct sockaddr_storage from;
87 1.1 cgd int fromlen;
88 1.24 christos int debug;
89 1.1 cgd
90 1.25 briggs int tftp_opt_tsize = 0;
91 1.25 briggs int tftp_blksize = SEGSIZE;
92 1.25 briggs int tftp_tsize = 0;
93 1.25 briggs
94 1.9 mrg /*
95 1.9 mrg * Null-terminated directory prefix list for absolute pathname requests and
96 1.9 mrg * search list for relative pathname requests.
97 1.9 mrg *
98 1.9 mrg * MAXDIRS should be at least as large as the number of arguments that
99 1.9 mrg * inetd allows (currently 20).
100 1.9 mrg */
101 1.9 mrg #define MAXDIRS 20
102 1.9 mrg static struct dirlist {
103 1.9 mrg char *name;
104 1.9 mrg int len;
105 1.9 mrg } dirs[MAXDIRS+1];
106 1.9 mrg static int suppress_naks;
107 1.9 mrg static int logging;
108 1.9 mrg static int secure;
109 1.9 mrg static char *securedir;
110 1.9 mrg
111 1.9 mrg struct formats;
112 1.9 mrg
113 1.22 lukem static const char *errtomsg(int);
114 1.22 lukem static void nak(int);
115 1.22 lukem static void tftp(struct tftphdr *, int);
116 1.22 lukem static void usage(void);
117 1.22 lukem static char *verifyhost(struct sockaddr *);
118 1.22 lukem void justquit(int);
119 1.22 lukem int main(int, char **);
120 1.25 briggs void recvfile(struct formats *, int, int);
121 1.25 briggs void sendfile(struct formats *, int, int);
122 1.22 lukem void timer(int);
123 1.24 christos static const char *opcode(int);
124 1.22 lukem int validate_access(char **, int);
125 1.1 cgd
126 1.9 mrg struct formats {
127 1.22 lukem const char *f_mode;
128 1.22 lukem int (*f_validate)(char **, int);
129 1.25 briggs void (*f_send)(struct formats *, int, int);
130 1.25 briggs void (*f_recv)(struct formats *, int, int);
131 1.22 lukem int f_convert;
132 1.9 mrg } formats[] = {
133 1.9 mrg { "netascii", validate_access, sendfile, recvfile, 1 },
134 1.9 mrg { "octet", validate_access, sendfile, recvfile, 0 },
135 1.9 mrg { 0 }
136 1.9 mrg };
137 1.4 mycroft
138 1.5 cgd static void
139 1.22 lukem usage(void)
140 1.5 cgd {
141 1.22 lukem
142 1.14 lukem syslog(LOG_ERR,
143 1.24 christos "Usage: %s [-dln] [-u user] [-g group] [-s directory] [directory ...]",
144 1.23 cgd getprogname());
145 1.5 cgd exit(1);
146 1.5 cgd }
147 1.5 cgd
148 1.9 mrg int
149 1.22 lukem main(int argc, char *argv[])
150 1.1 cgd {
151 1.18 itojun struct sockaddr_storage me;
152 1.22 lukem struct passwd *pwent;
153 1.22 lukem struct group *grent;
154 1.22 lukem struct tftphdr *tp;
155 1.22 lukem char *tgtuser, *tgtgroup, *ep;
156 1.22 lukem int n, ch, on, fd;
157 1.25 briggs int len, soopt;
158 1.22 lukem uid_t curuid, tgtuid;
159 1.22 lukem gid_t curgid, tgtgid;
160 1.22 lukem long nid;
161 1.1 cgd
162 1.22 lukem n = 0;
163 1.22 lukem fd = 0;
164 1.11 lukem openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
165 1.14 lukem tgtuser = DEFAULTUSER;
166 1.14 lukem tgtgroup = NULL;
167 1.14 lukem curuid = getuid();
168 1.14 lukem curgid = getgid();
169 1.5 cgd
170 1.24 christos while ((ch = getopt(argc, argv, "dg:lns:u:")) != -1)
171 1.9 mrg switch (ch) {
172 1.24 christos case 'd':
173 1.24 christos debug++;
174 1.24 christos break;
175 1.14 lukem
176 1.14 lukem case 'g':
177 1.14 lukem tgtgroup = optarg;
178 1.14 lukem break;
179 1.14 lukem
180 1.9 mrg case 'l':
181 1.9 mrg logging = 1;
182 1.9 mrg break;
183 1.9 mrg
184 1.9 mrg case 'n':
185 1.9 mrg suppress_naks = 1;
186 1.9 mrg break;
187 1.9 mrg
188 1.5 cgd case 's':
189 1.5 cgd secure = 1;
190 1.9 mrg securedir = optarg;
191 1.5 cgd break;
192 1.5 cgd
193 1.14 lukem case 'u':
194 1.14 lukem tgtuser = optarg;
195 1.14 lukem break;
196 1.14 lukem
197 1.5 cgd default:
198 1.5 cgd usage();
199 1.5 cgd break;
200 1.5 cgd }
201 1.5 cgd
202 1.9 mrg if (optind < argc) {
203 1.9 mrg struct dirlist *dirp;
204 1.9 mrg
205 1.9 mrg /* Get list of directory prefixes. Skip relative pathnames. */
206 1.9 mrg for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
207 1.9 mrg optind++) {
208 1.9 mrg if (argv[optind][0] == '/') {
209 1.9 mrg dirp->name = argv[optind];
210 1.9 mrg dirp->len = strlen(dirp->name);
211 1.9 mrg dirp++;
212 1.9 mrg }
213 1.9 mrg }
214 1.9 mrg }
215 1.9 mrg
216 1.14 lukem if (*tgtuser == '\0' || (tgtgroup != NULL && *tgtgroup == '\0'))
217 1.14 lukem usage();
218 1.14 lukem
219 1.14 lukem nid = (strtol(tgtuser, &ep, 10));
220 1.14 lukem if (*ep == '\0') {
221 1.14 lukem if (nid > UID_MAX) {
222 1.14 lukem syslog(LOG_ERR, "uid %ld is too large", nid);
223 1.14 lukem exit(1);
224 1.14 lukem }
225 1.14 lukem pwent = getpwuid((uid_t)nid);
226 1.14 lukem } else
227 1.14 lukem pwent = getpwnam(tgtuser);
228 1.14 lukem if (pwent == NULL) {
229 1.14 lukem syslog(LOG_ERR, "unknown user `%s'", tgtuser);
230 1.14 lukem exit(1);
231 1.14 lukem }
232 1.14 lukem tgtuid = pwent->pw_uid;
233 1.14 lukem tgtgid = pwent->pw_gid;
234 1.14 lukem
235 1.14 lukem if (tgtgroup != NULL) {
236 1.14 lukem nid = (strtol(tgtgroup, &ep, 10));
237 1.14 lukem if (*ep == '\0') {
238 1.14 lukem if (nid > GID_MAX) {
239 1.14 lukem syslog(LOG_ERR, "gid %ld is too large", nid);
240 1.14 lukem exit(1);
241 1.14 lukem }
242 1.14 lukem grent = getgrgid((gid_t)nid);
243 1.14 lukem } else
244 1.14 lukem grent = getgrnam(tgtgroup);
245 1.14 lukem if (grent != NULL)
246 1.14 lukem tgtgid = grent->gr_gid;
247 1.14 lukem else {
248 1.14 lukem syslog(LOG_ERR, "unknown group `%s'", tgtgroup);
249 1.14 lukem exit(1);
250 1.14 lukem }
251 1.14 lukem }
252 1.14 lukem
253 1.9 mrg if (secure) {
254 1.9 mrg if (chdir(securedir) < 0) {
255 1.9 mrg syslog(LOG_ERR, "chdir %s: %m", securedir);
256 1.9 mrg exit(1);
257 1.4 mycroft }
258 1.9 mrg if (chroot(".")) {
259 1.14 lukem syslog(LOG_ERR, "chroot: %m");
260 1.4 mycroft exit(1);
261 1.4 mycroft }
262 1.4 mycroft }
263 1.5 cgd
264 1.22 lukem if (logging)
265 1.22 lukem syslog(LOG_DEBUG, "running as user `%s' (%d), group `%s' (%d)",
266 1.22 lukem tgtuser, tgtuid, tgtgroup ? tgtgroup : "(unspecified)",
267 1.22 lukem tgtgid);
268 1.14 lukem if (curgid != tgtgid) {
269 1.14 lukem if (setgid(tgtgid)) {
270 1.14 lukem syslog(LOG_ERR, "setgid to %d: %m", (int)tgtgid);
271 1.14 lukem exit(1);
272 1.14 lukem }
273 1.14 lukem if (setgroups(0, NULL)) {
274 1.14 lukem syslog(LOG_ERR, "setgroups: %m");
275 1.14 lukem exit(1);
276 1.14 lukem }
277 1.8 mrg }
278 1.8 mrg
279 1.14 lukem if (curuid != tgtuid) {
280 1.14 lukem if (setuid(tgtuid)) {
281 1.14 lukem syslog(LOG_ERR, "setuid to %d: %m", (int)tgtuid);
282 1.14 lukem exit(1);
283 1.14 lukem }
284 1.4 mycroft }
285 1.5 cgd
286 1.9 mrg on = 1;
287 1.5 cgd if (ioctl(fd, FIONBIO, &on) < 0) {
288 1.14 lukem syslog(LOG_ERR, "ioctl(FIONBIO): %m");
289 1.1 cgd exit(1);
290 1.1 cgd }
291 1.1 cgd fromlen = sizeof (from);
292 1.5 cgd n = recvfrom(fd, buf, sizeof (buf), 0,
293 1.1 cgd (struct sockaddr *)&from, &fromlen);
294 1.1 cgd if (n < 0) {
295 1.14 lukem syslog(LOG_ERR, "recvfrom: %m");
296 1.1 cgd exit(1);
297 1.1 cgd }
298 1.1 cgd /*
299 1.1 cgd * Now that we have read the message out of the UDP
300 1.1 cgd * socket, we fork and exit. Thus, inetd will go back
301 1.1 cgd * to listening to the tftp port, and the next request
302 1.1 cgd * to come in will start up a new instance of tftpd.
303 1.1 cgd *
304 1.1 cgd * We do this so that inetd can run tftpd in "wait" mode.
305 1.1 cgd * The problem with tftpd running in "nowait" mode is that
306 1.1 cgd * inetd may get one or more successful "selects" on the
307 1.1 cgd * tftp port before we do our receive, so more than one
308 1.1 cgd * instance of tftpd may be started up. Worse, if tftpd
309 1.1 cgd * break before doing the above "recvfrom", inetd would
310 1.1 cgd * spawn endless instances, clogging the system.
311 1.1 cgd */
312 1.1 cgd {
313 1.1 cgd int pid;
314 1.1 cgd int i, j;
315 1.1 cgd
316 1.1 cgd for (i = 1; i < 20; i++) {
317 1.1 cgd pid = fork();
318 1.1 cgd if (pid < 0) {
319 1.1 cgd sleep(i);
320 1.1 cgd /*
321 1.1 cgd * flush out to most recently sent request.
322 1.1 cgd *
323 1.1 cgd * This may drop some request, but those
324 1.1 cgd * will be resent by the clients when
325 1.1 cgd * they timeout. The positive effect of
326 1.1 cgd * this flush is to (try to) prevent more
327 1.1 cgd * than one tftpd being started up to service
328 1.1 cgd * a single request from a single client.
329 1.1 cgd */
330 1.1 cgd j = sizeof from;
331 1.5 cgd i = recvfrom(fd, buf, sizeof (buf), 0,
332 1.1 cgd (struct sockaddr *)&from, &j);
333 1.1 cgd if (i > 0) {
334 1.1 cgd n = i;
335 1.1 cgd fromlen = j;
336 1.1 cgd }
337 1.1 cgd } else {
338 1.1 cgd break;
339 1.1 cgd }
340 1.1 cgd }
341 1.1 cgd if (pid < 0) {
342 1.14 lukem syslog(LOG_ERR, "fork: %m");
343 1.1 cgd exit(1);
344 1.1 cgd } else if (pid != 0) {
345 1.1 cgd exit(0);
346 1.1 cgd }
347 1.1 cgd }
348 1.15 explorer
349 1.15 explorer /*
350 1.15 explorer * remember what address this was sent to, so we can respond on the
351 1.15 explorer * same interface
352 1.15 explorer */
353 1.18 itojun len = sizeof(me);
354 1.18 itojun if (getsockname(fd, (struct sockaddr *)&me, &len) == 0) {
355 1.18 itojun switch (me.ss_family) {
356 1.18 itojun case AF_INET:
357 1.18 itojun ((struct sockaddr_in *)&me)->sin_port = 0;
358 1.18 itojun break;
359 1.18 itojun case AF_INET6:
360 1.18 itojun ((struct sockaddr_in6 *)&me)->sin6_port = 0;
361 1.18 itojun break;
362 1.18 itojun default:
363 1.18 itojun /* unsupported */
364 1.18 itojun break;
365 1.18 itojun }
366 1.18 itojun } else {
367 1.18 itojun memset(&me, 0, sizeof(me));
368 1.18 itojun me.ss_family = from.ss_family;
369 1.18 itojun me.ss_len = from.ss_len;
370 1.15 explorer }
371 1.15 explorer
372 1.1 cgd alarm(0);
373 1.5 cgd close(fd);
374 1.1 cgd close(1);
375 1.18 itojun peer = socket(from.ss_family, SOCK_DGRAM, 0);
376 1.1 cgd if (peer < 0) {
377 1.14 lukem syslog(LOG_ERR, "socket: %m");
378 1.1 cgd exit(1);
379 1.1 cgd }
380 1.18 itojun if (bind(peer, (struct sockaddr *)&me, me.ss_len) < 0) {
381 1.14 lukem syslog(LOG_ERR, "bind: %m");
382 1.1 cgd exit(1);
383 1.1 cgd }
384 1.18 itojun if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) {
385 1.14 lukem syslog(LOG_ERR, "connect: %m");
386 1.1 cgd exit(1);
387 1.1 cgd }
388 1.25 briggs soopt = 65536; /* larger than we'll ever need */
389 1.25 briggs if (setsockopt(peer, SOL_SOCKET, SO_SNDBUF, (void *) &soopt, sizeof(soopt)) < 0) {
390 1.25 briggs syslog(LOG_ERR, "set SNDBUF: %m");
391 1.25 briggs exit(1);
392 1.25 briggs }
393 1.25 briggs if (setsockopt(peer, SOL_SOCKET, SO_RCVBUF, (void *) &soopt, sizeof(soopt)) < 0) {
394 1.25 briggs syslog(LOG_ERR, "set RCVBUF: %m");
395 1.25 briggs exit(1);
396 1.25 briggs }
397 1.25 briggs
398 1.1 cgd tp = (struct tftphdr *)buf;
399 1.1 cgd tp->th_opcode = ntohs(tp->th_opcode);
400 1.1 cgd if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
401 1.1 cgd tftp(tp, n);
402 1.1 cgd exit(1);
403 1.1 cgd }
404 1.1 cgd
405 1.25 briggs static int
406 1.25 briggs blk_handler(struct tftphdr *tp, char *opt, char *val, char *ack,
407 1.25 briggs int *ackl, int *ec)
408 1.25 briggs {
409 1.25 briggs unsigned long bsize;
410 1.25 briggs char *endp;
411 1.25 briggs int l;
412 1.25 briggs
413 1.25 briggs /*
414 1.25 briggs * On these failures, we could just ignore the blocksize option.
415 1.25 briggs * Perhaps that should be a command-line option.
416 1.25 briggs */
417 1.25 briggs errno = 0;
418 1.25 briggs bsize = strtoul(val, &endp, 10);
419 1.25 briggs if ((bsize == ULONG_MAX && errno == ERANGE) || *endp) {
420 1.25 briggs syslog(LOG_NOTICE, "%s: %s request for %s: "
421 1.25 briggs "illegal value %s for blksize option",
422 1.25 briggs verifyhost((struct sockaddr *)&from),
423 1.25 briggs tp->th_opcode == WRQ ? "write" : "read",
424 1.25 briggs tp->th_stuff, val);
425 1.25 briggs return 0;
426 1.25 briggs }
427 1.25 briggs if (bsize < 8 || bsize > 65464) {
428 1.25 briggs syslog(LOG_NOTICE, "%s: %s request for %s: "
429 1.25 briggs "out of range value %s for blksize option",
430 1.25 briggs verifyhost((struct sockaddr *)&from),
431 1.25 briggs tp->th_opcode == WRQ ? "write" : "read",
432 1.25 briggs tp->th_stuff, val);
433 1.25 briggs return 0;
434 1.25 briggs }
435 1.25 briggs
436 1.25 briggs tftp_blksize = bsize;
437 1.25 briggs strcpy(ack + *ackl, "blksize");
438 1.25 briggs *ackl += 8;
439 1.25 briggs l = sprintf(ack + *ackl, "%lu", bsize);
440 1.25 briggs *ackl += l + 1;
441 1.25 briggs
442 1.25 briggs return 0;
443 1.25 briggs }
444 1.25 briggs
445 1.25 briggs static int
446 1.25 briggs timeout_handler(struct tftphdr *tp, char *opt, char *val, char *ack,
447 1.25 briggs int *ackl, int *ec)
448 1.25 briggs {
449 1.25 briggs unsigned long tout;
450 1.25 briggs char *endp;
451 1.25 briggs int l;
452 1.25 briggs
453 1.25 briggs errno = 0;
454 1.25 briggs tout = strtoul(val, &endp, 10);
455 1.25 briggs if ((tout == ULONG_MAX && errno == ERANGE) || *endp) {
456 1.25 briggs syslog(LOG_NOTICE, "%s: %s request for %s: "
457 1.25 briggs "illegal value %s for timeout option",
458 1.25 briggs verifyhost((struct sockaddr *)&from),
459 1.25 briggs tp->th_opcode == WRQ ? "write" : "read",
460 1.25 briggs tp->th_stuff, val);
461 1.25 briggs return 0;
462 1.25 briggs }
463 1.25 briggs if (tout < 1 || tout > 255) {
464 1.25 briggs syslog(LOG_NOTICE, "%s: %s request for %s: "
465 1.25 briggs "out of range value %s for timeout option",
466 1.25 briggs verifyhost((struct sockaddr *)&from),
467 1.25 briggs tp->th_opcode == WRQ ? "write" : "read",
468 1.25 briggs tp->th_stuff, val);
469 1.25 briggs return 0;
470 1.25 briggs }
471 1.25 briggs
472 1.25 briggs rexmtval = tout;
473 1.25 briggs strcpy(ack + *ackl, "timeout");
474 1.25 briggs *ackl += 8;
475 1.25 briggs l = sprintf(ack + *ackl, "%lu", tout);
476 1.25 briggs *ackl += l + 1;
477 1.25 briggs
478 1.25 briggs /*
479 1.25 briggs * Arbitrarily pick a maximum timeout on a request to 3
480 1.25 briggs * retransmissions if the interval timeout is more than
481 1.25 briggs * one minute. Longest possible timeout is therefore
482 1.25 briggs * 3 * 255 - 1, or 764 seconds.
483 1.25 briggs */
484 1.25 briggs if (rexmtval > 60) {
485 1.25 briggs maxtimeout = rexmtval * 3;
486 1.25 briggs } else {
487 1.25 briggs maxtimeout = rexmtval * 5;
488 1.25 briggs }
489 1.25 briggs
490 1.25 briggs return 0;
491 1.25 briggs }
492 1.25 briggs
493 1.25 briggs static int
494 1.25 briggs tsize_handler(struct tftphdr *tp, char *opt, char *val, char *ack,
495 1.25 briggs int *ackl, int *ec)
496 1.25 briggs {
497 1.25 briggs unsigned long fsize;
498 1.25 briggs char *endp;
499 1.25 briggs
500 1.25 briggs /*
501 1.25 briggs * Maximum file even with extended tftp is 65535 blocks of
502 1.25 briggs * length 65464, or 4290183240 octets (4784056 less than 2^32).
503 1.25 briggs * unsigned long is at least 32 bits on all NetBSD archs.
504 1.25 briggs */
505 1.25 briggs
506 1.25 briggs errno = 0;
507 1.25 briggs fsize = strtoul(val, &endp, 10);
508 1.25 briggs if ((fsize == ULONG_MAX && errno == ERANGE) || *endp) {
509 1.25 briggs syslog(LOG_NOTICE, "%s: %s request for %s: "
510 1.25 briggs "illegal value %s for tsize option",
511 1.25 briggs verifyhost((struct sockaddr *)&from),
512 1.25 briggs tp->th_opcode == WRQ ? "write" : "read",
513 1.25 briggs tp->th_stuff, val);
514 1.25 briggs return 0;
515 1.25 briggs }
516 1.25 briggs if (fsize > (unsigned long) 65535 * 65464) {
517 1.25 briggs syslog(LOG_NOTICE, "%s: %s request for %s: "
518 1.25 briggs "out of range value %s for tsize option",
519 1.25 briggs verifyhost((struct sockaddr *)&from),
520 1.25 briggs tp->th_opcode == WRQ ? "write" : "read",
521 1.25 briggs tp->th_stuff, val);
522 1.25 briggs return 0;
523 1.25 briggs }
524 1.25 briggs
525 1.25 briggs tftp_opt_tsize = 1;
526 1.25 briggs tftp_tsize = fsize;
527 1.25 briggs /*
528 1.25 briggs * We will report this later -- either replying with the fsize (WRQ)
529 1.25 briggs * or replying with the actual filesize (RRQ).
530 1.25 briggs */
531 1.25 briggs
532 1.25 briggs return 0;
533 1.25 briggs }
534 1.25 briggs
535 1.25 briggs struct tftp_options {
536 1.25 briggs char *o_name;
537 1.25 briggs int (*o_handler)(struct tftphdr *, char *, char *, char *,
538 1.25 briggs int *, int *);
539 1.25 briggs } options[] = {
540 1.25 briggs { "blksize", blk_handler },
541 1.25 briggs { "timeout", timeout_handler },
542 1.25 briggs { "tsize", tsize_handler },
543 1.25 briggs { NULL, NULL }
544 1.25 briggs };
545 1.25 briggs
546 1.25 briggs /*
547 1.25 briggs * Get options for an extended tftp session. Stuff the ones we
548 1.25 briggs * recognize in oackbuf.
549 1.25 briggs */
550 1.25 briggs static int
551 1.25 briggs get_options(struct tftphdr *tp, char *cp, int size, char *ackb,
552 1.25 briggs int *alen, int *err)
553 1.25 briggs {
554 1.25 briggs struct tftp_options *op;
555 1.25 briggs char *option, *value, *endp;
556 1.25 briggs int r, rv=0, ec=0;
557 1.25 briggs
558 1.25 briggs endp = cp + size;
559 1.25 briggs while (cp < endp) {
560 1.25 briggs option = cp;
561 1.25 briggs while (*cp && cp < endp) {
562 1.25 briggs *cp = tolower(*cp);
563 1.25 briggs cp++;
564 1.25 briggs }
565 1.25 briggs if (*cp) {
566 1.25 briggs /* if we have garbage at the end, just ignore it */
567 1.25 briggs break;
568 1.25 briggs }
569 1.25 briggs cp++; /* skip over NUL */
570 1.25 briggs value = cp;
571 1.25 briggs while (*cp && cp < endp) {
572 1.25 briggs cp++;
573 1.25 briggs }
574 1.25 briggs if (*cp) {
575 1.25 briggs /* if we have garbage at the end, just ignore it */
576 1.25 briggs break;
577 1.25 briggs }
578 1.25 briggs cp++;
579 1.25 briggs for (op = options; op->o_name; op++) {
580 1.25 briggs if (strcmp(op->o_name, option) == 0)
581 1.25 briggs break;
582 1.25 briggs }
583 1.25 briggs if (op->o_name) {
584 1.25 briggs r = op->o_handler(tp, option, value, ackb, alen, &ec);
585 1.25 briggs if (r < 0) {
586 1.25 briggs rv = -1;
587 1.25 briggs break;
588 1.25 briggs }
589 1.25 briggs rv++;
590 1.25 briggs } /* else ignore unknown options */
591 1.25 briggs }
592 1.25 briggs
593 1.25 briggs if (rv < 0)
594 1.25 briggs *err = ec;
595 1.25 briggs
596 1.25 briggs return rv;
597 1.25 briggs }
598 1.25 briggs
599 1.1 cgd /*
600 1.1 cgd * Handle initial connection protocol.
601 1.1 cgd */
602 1.9 mrg static void
603 1.22 lukem tftp(struct tftphdr *tp, int size)
604 1.1 cgd {
605 1.14 lukem struct formats *pf;
606 1.22 lukem char *cp;
607 1.22 lukem char *filename, *mode;
608 1.25 briggs int first, ecode, alen, etftp=0, r;
609 1.22 lukem
610 1.22 lukem first = 1;
611 1.22 lukem mode = NULL;
612 1.1 cgd
613 1.1 cgd filename = cp = tp->th_stuff;
614 1.1 cgd again:
615 1.1 cgd while (cp < buf + size) {
616 1.1 cgd if (*cp == '\0')
617 1.1 cgd break;
618 1.1 cgd cp++;
619 1.1 cgd }
620 1.1 cgd if (*cp != '\0') {
621 1.1 cgd nak(EBADOP);
622 1.1 cgd exit(1);
623 1.1 cgd }
624 1.1 cgd if (first) {
625 1.1 cgd mode = ++cp;
626 1.1 cgd first = 0;
627 1.1 cgd goto again;
628 1.1 cgd }
629 1.1 cgd for (cp = mode; *cp; cp++)
630 1.1 cgd if (isupper(*cp))
631 1.1 cgd *cp = tolower(*cp);
632 1.1 cgd for (pf = formats; pf->f_mode; pf++)
633 1.1 cgd if (strcmp(pf->f_mode, mode) == 0)
634 1.1 cgd break;
635 1.1 cgd if (pf->f_mode == 0) {
636 1.1 cgd nak(EBADOP);
637 1.1 cgd exit(1);
638 1.1 cgd }
639 1.25 briggs /*
640 1.25 briggs * cp currently points to the NUL byte following the mode.
641 1.25 briggs *
642 1.25 briggs * If we have some valid options, then let's assume that we're
643 1.25 briggs * now dealing with an extended tftp session. Note that if we
644 1.25 briggs * don't get any options, then we *must* assume that we do not
645 1.25 briggs * have an extended tftp session. If we get options, we fill
646 1.25 briggs * in the ack buf to acknowledge them. If we skip that, then
647 1.25 briggs * the client *must* assume that we are not using an extended
648 1.25 briggs * session.
649 1.25 briggs */
650 1.25 briggs size -= (++cp - (char *) tp);
651 1.25 briggs if (size > 0 && *cp) {
652 1.25 briggs alen = 2; /* Skip over opcode */
653 1.25 briggs r = get_options(tp, cp, size, oackbuf, &alen, &ecode);
654 1.25 briggs if (r > 0) {
655 1.25 briggs etftp = 1;
656 1.25 briggs } else if (r < 0) {
657 1.25 briggs nak(ecode);
658 1.25 briggs exit(1);
659 1.25 briggs }
660 1.25 briggs }
661 1.9 mrg ecode = (*pf->f_validate)(&filename, tp->th_opcode);
662 1.9 mrg if (logging) {
663 1.9 mrg syslog(LOG_INFO, "%s: %s request for %s: %s",
664 1.18 itojun verifyhost((struct sockaddr *)&from),
665 1.9 mrg tp->th_opcode == WRQ ? "write" : "read",
666 1.9 mrg filename, errtomsg(ecode));
667 1.9 mrg }
668 1.1 cgd if (ecode) {
669 1.9 mrg /*
670 1.9 mrg * Avoid storms of naks to a RRQ broadcast for a relative
671 1.9 mrg * bootfile pathname from a diskless Sun.
672 1.9 mrg */
673 1.9 mrg if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
674 1.9 mrg exit(0);
675 1.1 cgd nak(ecode);
676 1.1 cgd exit(1);
677 1.1 cgd }
678 1.25 briggs
679 1.25 briggs if (etftp) {
680 1.25 briggs struct tftphdr *oack_h;
681 1.25 briggs
682 1.25 briggs if (tftp_opt_tsize) {
683 1.25 briggs int l;
684 1.25 briggs
685 1.25 briggs strcpy(oackbuf + alen, "tsize");
686 1.25 briggs alen += 6;
687 1.25 briggs l = sprintf(oackbuf + alen, "%u", tftp_tsize);
688 1.25 briggs alen += l + 1;
689 1.25 briggs }
690 1.25 briggs oack_h = (struct tftphdr *) oackbuf;
691 1.25 briggs oack_h->th_opcode = htons(OACK);
692 1.25 briggs }
693 1.25 briggs
694 1.1 cgd if (tp->th_opcode == WRQ)
695 1.25 briggs (*pf->f_recv)(pf, etftp, alen);
696 1.1 cgd else
697 1.25 briggs (*pf->f_send)(pf, etftp, alen);
698 1.1 cgd exit(0);
699 1.1 cgd }
700 1.1 cgd
701 1.1 cgd
702 1.1 cgd FILE *file;
703 1.1 cgd
704 1.1 cgd /*
705 1.1 cgd * Validate file access. Since we
706 1.1 cgd * have no uid or gid, for now require
707 1.1 cgd * file to exist and be publicly
708 1.1 cgd * readable/writable.
709 1.1 cgd * If we were invoked with arguments
710 1.1 cgd * from inetd then the file must also be
711 1.1 cgd * in one of the given directory prefixes.
712 1.1 cgd */
713 1.9 mrg int
714 1.22 lukem validate_access(char **filep, int mode)
715 1.22 lukem {
716 1.22 lukem struct stat stbuf;
717 1.22 lukem struct dirlist *dirp;
718 1.22 lukem static char pathname[MAXPATHLEN];
719 1.22 lukem char *filename;
720 1.22 lukem int fd;
721 1.22 lukem
722 1.22 lukem filename = *filep;
723 1.1 cgd
724 1.9 mrg /*
725 1.9 mrg * Prevent tricksters from getting around the directory restrictions
726 1.9 mrg */
727 1.9 mrg if (strstr(filename, "/../"))
728 1.9 mrg return (EACCESS);
729 1.9 mrg
730 1.9 mrg if (*filename == '/') {
731 1.4 mycroft /*
732 1.9 mrg * Allow the request if it's in one of the approved locations.
733 1.9 mrg * Special case: check the null prefix ("/") by looking
734 1.9 mrg * for length = 1 and relying on the arg. processing that
735 1.9 mrg * it's a /.
736 1.4 mycroft */
737 1.9 mrg for (dirp = dirs; dirp->name != NULL; dirp++) {
738 1.9 mrg if (dirp->len == 1 ||
739 1.9 mrg (!strncmp(filename, dirp->name, dirp->len) &&
740 1.9 mrg filename[dirp->len] == '/'))
741 1.9 mrg break;
742 1.9 mrg }
743 1.9 mrg /* If directory list is empty, allow access to any file */
744 1.9 mrg if (dirp->name == NULL && dirp != dirs)
745 1.1 cgd return (EACCESS);
746 1.9 mrg if (stat(filename, &stbuf) < 0)
747 1.9 mrg return (errno == ENOENT ? ENOTFOUND : EACCESS);
748 1.10 mycroft if (!S_ISREG(stbuf.st_mode))
749 1.9 mrg return (ENOTFOUND);
750 1.9 mrg if (mode == RRQ) {
751 1.9 mrg if ((stbuf.st_mode & S_IROTH) == 0)
752 1.9 mrg return (EACCESS);
753 1.9 mrg } else {
754 1.9 mrg if ((stbuf.st_mode & S_IWOTH) == 0)
755 1.9 mrg return (EACCESS);
756 1.9 mrg }
757 1.1 cgd } else {
758 1.9 mrg /*
759 1.9 mrg * Relative file name: search the approved locations for it.
760 1.9 mrg */
761 1.9 mrg
762 1.16 aidan if (!strncmp(filename, "../", 3))
763 1.1 cgd return (EACCESS);
764 1.9 mrg
765 1.9 mrg /*
766 1.16 aidan * Find the first file that exists in any of the directories,
767 1.16 aidan * check access on it.
768 1.9 mrg */
769 1.9 mrg if (dirs[0].name != NULL) {
770 1.9 mrg for (dirp = dirs; dirp->name != NULL; dirp++) {
771 1.9 mrg snprintf(pathname, sizeof pathname, "%s/%s",
772 1.9 mrg dirp->name, filename);
773 1.9 mrg if (stat(pathname, &stbuf) == 0 &&
774 1.9 mrg (stbuf.st_mode & S_IFMT) == S_IFREG) {
775 1.16 aidan break;
776 1.9 mrg }
777 1.9 mrg }
778 1.9 mrg if (dirp->name == NULL)
779 1.16 aidan return (ENOTFOUND);
780 1.16 aidan if (mode == RRQ && !(stbuf.st_mode & S_IROTH))
781 1.16 aidan return (EACCESS);
782 1.16 aidan if (mode == WRQ && !(stbuf.st_mode & S_IWOTH))
783 1.16 aidan return (EACCESS);
784 1.9 mrg *filep = filename = pathname;
785 1.16 aidan } else {
786 1.16 aidan /*
787 1.16 aidan * If there's no directory list, take our cue from the
788 1.16 aidan * absolute file request check above (*filename == '/'),
789 1.16 aidan * and allow access to anything.
790 1.16 aidan */
791 1.16 aidan if (stat(filename, &stbuf) < 0)
792 1.16 aidan return (errno == ENOENT ? ENOTFOUND : EACCESS);
793 1.16 aidan if (!S_ISREG(stbuf.st_mode))
794 1.16 aidan return (ENOTFOUND);
795 1.16 aidan if (mode == RRQ) {
796 1.16 aidan if ((stbuf.st_mode & S_IROTH) == 0)
797 1.16 aidan return (EACCESS);
798 1.16 aidan } else {
799 1.16 aidan if ((stbuf.st_mode & S_IWOTH) == 0)
800 1.16 aidan return (EACCESS);
801 1.16 aidan }
802 1.9 mrg *filep = filename;
803 1.16 aidan }
804 1.1 cgd }
805 1.25 briggs
806 1.25 briggs if (tftp_opt_tsize && mode == RRQ)
807 1.25 briggs tftp_tsize = (unsigned long) stbuf.st_size;
808 1.25 briggs
809 1.17 carrel fd = open(filename, mode == RRQ ? O_RDONLY : O_WRONLY | O_TRUNC);
810 1.1 cgd if (fd < 0)
811 1.1 cgd return (errno + 100);
812 1.1 cgd file = fdopen(fd, (mode == RRQ)? "r":"w");
813 1.1 cgd if (file == NULL) {
814 1.16 aidan close(fd);
815 1.22 lukem return (errno + 100);
816 1.1 cgd }
817 1.1 cgd return (0);
818 1.1 cgd }
819 1.1 cgd
820 1.1 cgd int timeout;
821 1.1 cgd jmp_buf timeoutbuf;
822 1.1 cgd
823 1.1 cgd void
824 1.22 lukem timer(int dummy)
825 1.1 cgd {
826 1.1 cgd
827 1.1 cgd timeout += rexmtval;
828 1.1 cgd if (timeout >= maxtimeout)
829 1.1 cgd exit(1);
830 1.1 cgd longjmp(timeoutbuf, 1);
831 1.1 cgd }
832 1.1 cgd
833 1.24 christos static const char *
834 1.24 christos opcode(int code)
835 1.24 christos {
836 1.24 christos static char buf[64];
837 1.24 christos
838 1.24 christos switch (code) {
839 1.24 christos case RRQ:
840 1.24 christos return "RRQ";
841 1.24 christos case WRQ:
842 1.24 christos return "WRQ";
843 1.24 christos case DATA:
844 1.24 christos return "DATA";
845 1.24 christos case ACK:
846 1.24 christos return "ACK";
847 1.24 christos case ERROR:
848 1.24 christos return "ERROR";
849 1.25 briggs case OACK:
850 1.25 briggs return "OACK";
851 1.24 christos default:
852 1.24 christos (void)snprintf(buf, sizeof(buf), "*code %d*", code);
853 1.24 christos return buf;
854 1.24 christos }
855 1.24 christos }
856 1.24 christos
857 1.1 cgd /*
858 1.1 cgd * Send the requested file.
859 1.1 cgd */
860 1.9 mrg void
861 1.25 briggs sendfile(struct formats *pf, int etftp, int acklength)
862 1.1 cgd {
863 1.19 dogcow volatile unsigned int block;
864 1.22 lukem struct tftphdr *dp;
865 1.22 lukem struct tftphdr *ap; /* ack packet */
866 1.22 lukem int size, n;
867 1.1 cgd
868 1.1 cgd signal(SIGALRM, timer);
869 1.1 cgd ap = (struct tftphdr *)ackbuf;
870 1.25 briggs if (etftp) {
871 1.25 briggs dp = (struct tftphdr *)oackbuf;
872 1.25 briggs size = acklength - 4;
873 1.25 briggs block = 0;
874 1.25 briggs } else {
875 1.25 briggs dp = r_init();
876 1.25 briggs size = 0;
877 1.25 briggs block = 1;
878 1.25 briggs }
879 1.25 briggs
880 1.1 cgd do {
881 1.25 briggs if (block > 0) {
882 1.25 briggs size = readit(file, &dp, tftp_blksize, pf->f_convert);
883 1.25 briggs if (size < 0) {
884 1.25 briggs nak(errno + 100);
885 1.25 briggs goto abort;
886 1.25 briggs }
887 1.25 briggs dp->th_opcode = htons((u_short)DATA);
888 1.25 briggs dp->th_block = htons((u_short)block);
889 1.1 cgd }
890 1.1 cgd timeout = 0;
891 1.9 mrg (void)setjmp(timeoutbuf);
892 1.1 cgd
893 1.1 cgd send_data:
894 1.25 briggs if (!etftp && debug)
895 1.24 christos syslog(LOG_DEBUG, "Send DATA %u", block);
896 1.25 briggs if ((n = send(peer, dp, size + 4, 0)) != size + 4) {
897 1.14 lukem syslog(LOG_ERR, "tftpd: write: %m");
898 1.1 cgd goto abort;
899 1.1 cgd }
900 1.25 briggs if (block)
901 1.25 briggs read_ahead(file, tftp_blksize, pf->f_convert);
902 1.1 cgd for ( ; ; ) {
903 1.1 cgd alarm(rexmtval); /* read the ack */
904 1.25 briggs n = recv(peer, ackbuf, tftp_blksize, 0);
905 1.1 cgd alarm(0);
906 1.1 cgd if (n < 0) {
907 1.14 lukem syslog(LOG_ERR, "tftpd: read: %m");
908 1.1 cgd goto abort;
909 1.1 cgd }
910 1.1 cgd ap->th_opcode = ntohs((u_short)ap->th_opcode);
911 1.1 cgd ap->th_block = ntohs((u_short)ap->th_block);
912 1.24 christos switch (ap->th_opcode) {
913 1.24 christos case ERROR:
914 1.1 cgd goto abort;
915 1.9 mrg
916 1.24 christos case ACK:
917 1.25 briggs if (ap->th_block == 0) {
918 1.25 briggs etftp = 0;
919 1.25 briggs acklength = 0;
920 1.25 briggs dp = r_init();
921 1.25 briggs goto done;
922 1.25 briggs }
923 1.9 mrg if (ap->th_block == block)
924 1.24 christos goto done;
925 1.24 christos if (debug)
926 1.24 christos syslog(LOG_DEBUG, "Resync ACK %u != %u",
927 1.24 christos (unsigned int)ap->th_block, block);
928 1.1 cgd /* Re-synchronize with the other side */
929 1.25 briggs (void) synchnet(peer, tftp_blksize);
930 1.9 mrg if (ap->th_block == (block -1))
931 1.1 cgd goto send_data;
932 1.24 christos default:
933 1.24 christos syslog(LOG_INFO, "Received %s in sendfile\n",
934 1.24 christos opcode(dp->th_opcode));
935 1.1 cgd }
936 1.1 cgd
937 1.1 cgd }
938 1.24 christos done:
939 1.24 christos if (debug)
940 1.24 christos syslog(LOG_DEBUG, "Received ACK for block %u", block);
941 1.1 cgd block++;
942 1.25 briggs } while (size == tftp_blksize || block == 1);
943 1.1 cgd abort:
944 1.1 cgd (void) fclose(file);
945 1.1 cgd }
946 1.1 cgd
947 1.1 cgd void
948 1.22 lukem justquit(int dummy)
949 1.1 cgd {
950 1.22 lukem
951 1.1 cgd exit(0);
952 1.1 cgd }
953 1.1 cgd
954 1.1 cgd /*
955 1.1 cgd * Receive a file.
956 1.1 cgd */
957 1.9 mrg void
958 1.25 briggs recvfile(struct formats *pf, int etftp, int acklength)
959 1.1 cgd {
960 1.19 dogcow volatile unsigned int block;
961 1.22 lukem struct tftphdr *dp;
962 1.22 lukem struct tftphdr *ap; /* ack buffer */
963 1.22 lukem int n, size;
964 1.1 cgd
965 1.1 cgd signal(SIGALRM, timer);
966 1.1 cgd dp = w_init();
967 1.25 briggs ap = (struct tftphdr *)oackbuf;
968 1.9 mrg block = 0;
969 1.1 cgd do {
970 1.1 cgd timeout = 0;
971 1.25 briggs if (etftp == 0) {
972 1.25 briggs ap = (struct tftphdr *)ackbuf;
973 1.25 briggs ap->th_opcode = htons((u_short)ACK);
974 1.25 briggs ap->th_block = htons((u_short)block);
975 1.25 briggs acklength = 4;
976 1.25 briggs }
977 1.24 christos if (debug)
978 1.24 christos syslog(LOG_DEBUG, "Sending ACK for block %u\n", block);
979 1.1 cgd block++;
980 1.1 cgd (void) setjmp(timeoutbuf);
981 1.1 cgd send_ack:
982 1.25 briggs if (send(peer, ap, acklength, 0) != acklength) {
983 1.14 lukem syslog(LOG_ERR, "tftpd: write: %m");
984 1.1 cgd goto abort;
985 1.1 cgd }
986 1.1 cgd write_behind(file, pf->f_convert);
987 1.1 cgd for ( ; ; ) {
988 1.1 cgd alarm(rexmtval);
989 1.25 briggs n = recv(peer, dp, tftp_blksize + 4, 0);
990 1.1 cgd alarm(0);
991 1.1 cgd if (n < 0) { /* really? */
992 1.14 lukem syslog(LOG_ERR, "tftpd: read: %m");
993 1.1 cgd goto abort;
994 1.1 cgd }
995 1.25 briggs etftp = 0;
996 1.1 cgd dp->th_opcode = ntohs((u_short)dp->th_opcode);
997 1.1 cgd dp->th_block = ntohs((u_short)dp->th_block);
998 1.24 christos if (debug)
999 1.24 christos syslog(LOG_DEBUG, "Received %s for block %u",
1000 1.24 christos opcode(dp->th_opcode),
1001 1.24 christos (unsigned int)dp->th_block);
1002 1.24 christos
1003 1.24 christos switch (dp->th_opcode) {
1004 1.24 christos case ERROR:
1005 1.1 cgd goto abort;
1006 1.24 christos case DATA:
1007 1.24 christos if (dp->th_block == block)
1008 1.24 christos goto done; /* normal */
1009 1.24 christos if (debug)
1010 1.24 christos syslog(LOG_DEBUG, "Resync %u != %u",
1011 1.24 christos (unsigned int)dp->th_block, block);
1012 1.1 cgd /* Re-synchronize with the other side */
1013 1.25 briggs (void) synchnet(peer, tftp_blksize);
1014 1.1 cgd if (dp->th_block == (block-1))
1015 1.1 cgd goto send_ack; /* rexmit */
1016 1.24 christos break;
1017 1.24 christos default:
1018 1.24 christos syslog(LOG_INFO, "Received %s in recvfile\n",
1019 1.24 christos opcode(dp->th_opcode));
1020 1.24 christos break;
1021 1.1 cgd }
1022 1.1 cgd }
1023 1.24 christos done:
1024 1.24 christos if (debug)
1025 1.24 christos syslog(LOG_DEBUG, "Got block %u", block);
1026 1.1 cgd /* size = write(file, dp->th_data, n - 4); */
1027 1.1 cgd size = writeit(file, &dp, n - 4, pf->f_convert);
1028 1.1 cgd if (size != (n-4)) { /* ahem */
1029 1.1 cgd if (size < 0) nak(errno + 100);
1030 1.1 cgd else nak(ENOSPACE);
1031 1.1 cgd goto abort;
1032 1.1 cgd }
1033 1.25 briggs } while (size == tftp_blksize);
1034 1.1 cgd write_behind(file, pf->f_convert);
1035 1.1 cgd (void) fclose(file); /* close data file */
1036 1.1 cgd
1037 1.1 cgd ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */
1038 1.1 cgd ap->th_block = htons((u_short)(block));
1039 1.24 christos if (debug)
1040 1.24 christos syslog(LOG_DEBUG, "Send final ACK %u", block);
1041 1.1 cgd (void) send(peer, ackbuf, 4, 0);
1042 1.1 cgd
1043 1.1 cgd signal(SIGALRM, justquit); /* just quit on timeout */
1044 1.1 cgd alarm(rexmtval);
1045 1.1 cgd n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
1046 1.1 cgd alarm(0);
1047 1.1 cgd if (n >= 4 && /* if read some data */
1048 1.1 cgd dp->th_opcode == DATA && /* and got a data block */
1049 1.1 cgd block == dp->th_block) { /* then my last ack was lost */
1050 1.1 cgd (void) send(peer, ackbuf, 4, 0); /* resend final ack */
1051 1.1 cgd }
1052 1.1 cgd abort:
1053 1.1 cgd return;
1054 1.1 cgd }
1055 1.1 cgd
1056 1.13 mycroft const struct errmsg {
1057 1.22 lukem int e_code;
1058 1.22 lukem const char *e_msg;
1059 1.1 cgd } errmsgs[] = {
1060 1.1 cgd { EUNDEF, "Undefined error code" },
1061 1.1 cgd { ENOTFOUND, "File not found" },
1062 1.1 cgd { EACCESS, "Access violation" },
1063 1.1 cgd { ENOSPACE, "Disk full or allocation exceeded" },
1064 1.1 cgd { EBADOP, "Illegal TFTP operation" },
1065 1.1 cgd { EBADID, "Unknown transfer ID" },
1066 1.1 cgd { EEXISTS, "File already exists" },
1067 1.1 cgd { ENOUSER, "No such user" },
1068 1.25 briggs { EOPTNEG, "Option negotiation failed" },
1069 1.1 cgd { -1, 0 }
1070 1.1 cgd };
1071 1.1 cgd
1072 1.13 mycroft static const char *
1073 1.22 lukem errtomsg(int error)
1074 1.9 mrg {
1075 1.22 lukem static char ebuf[20];
1076 1.14 lukem const struct errmsg *pe;
1077 1.9 mrg
1078 1.9 mrg if (error == 0)
1079 1.22 lukem return ("success");
1080 1.9 mrg for (pe = errmsgs; pe->e_code >= 0; pe++)
1081 1.9 mrg if (pe->e_code == error)
1082 1.22 lukem return (pe->e_msg);
1083 1.22 lukem snprintf(ebuf, sizeof(ebuf), "error %d", error);
1084 1.22 lukem return (ebuf);
1085 1.9 mrg }
1086 1.9 mrg
1087 1.1 cgd /*
1088 1.1 cgd * Send a nak packet (error message).
1089 1.1 cgd * Error code passed in is one of the
1090 1.1 cgd * standard TFTP codes, or a UNIX errno
1091 1.1 cgd * offset by 100.
1092 1.1 cgd */
1093 1.9 mrg static void
1094 1.22 lukem nak(int error)
1095 1.1 cgd {
1096 1.22 lukem const struct errmsg *pe;
1097 1.14 lukem struct tftphdr *tp;
1098 1.22 lukem int length;
1099 1.22 lukem size_t msglen;
1100 1.1 cgd
1101 1.1 cgd tp = (struct tftphdr *)buf;
1102 1.1 cgd tp->th_opcode = htons((u_short)ERROR);
1103 1.21 itojun msglen = sizeof(buf) - (&tp->th_msg[0] - buf);
1104 1.1 cgd for (pe = errmsgs; pe->e_code >= 0; pe++)
1105 1.1 cgd if (pe->e_code == error)
1106 1.1 cgd break;
1107 1.1 cgd if (pe->e_code < 0) {
1108 1.1 cgd tp->th_code = EUNDEF; /* set 'undef' errorcode */
1109 1.21 itojun strlcpy(tp->th_msg, strerror(error - 100), msglen);
1110 1.13 mycroft } else {
1111 1.13 mycroft tp->th_code = htons((u_short)error);
1112 1.21 itojun strlcpy(tp->th_msg, pe->e_msg, msglen);
1113 1.1 cgd }
1114 1.24 christos if (debug)
1115 1.24 christos syslog(LOG_DEBUG, "Send NACK %s", tp->th_msg);
1116 1.21 itojun length = strlen(tp->th_msg);
1117 1.21 itojun msglen = &tp->th_msg[length + 1] - buf;
1118 1.21 itojun if (send(peer, buf, msglen, 0) != msglen)
1119 1.14 lukem syslog(LOG_ERR, "nak: %m");
1120 1.9 mrg }
1121 1.9 mrg
1122 1.9 mrg static char *
1123 1.22 lukem verifyhost(struct sockaddr *fromp)
1124 1.9 mrg {
1125 1.18 itojun static char hbuf[MAXHOSTNAMELEN];
1126 1.9 mrg
1127 1.20 itojun if (getnameinfo(fromp, fromp->sa_len, hbuf, sizeof(hbuf), NULL, 0, 0))
1128 1.20 itojun strlcpy(hbuf, "?", sizeof(hbuf));
1129 1.22 lukem return (hbuf);
1130 1.1 cgd }
1131