tftpd.c revision 1.15 1 1.15 explorer /* $NetBSD: tftpd.c,v 1.15 1998/09/20 04:44:55 explorer 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.15 explorer __RCSID("$NetBSD: tftpd.c,v 1.15 1998/09/20 04:44:55 explorer 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.14 lukem #include <grp.h>
70 1.1 cgd #include <netdb.h>
71 1.14 lukem #include <pwd.h>
72 1.1 cgd #include <setjmp.h>
73 1.9 mrg #include <signal.h>
74 1.1 cgd #include <stdio.h>
75 1.9 mrg #include <stdlib.h>
76 1.1 cgd #include <string.h>
77 1.9 mrg #include <syslog.h>
78 1.9 mrg #include <unistd.h>
79 1.9 mrg
80 1.9 mrg #include "tftpsubs.h"
81 1.1 cgd
82 1.14 lukem #define DEFAULTUSER "nobody"
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.13 mycroft static const 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.14 lukem syslog(LOG_ERR,
144 1.14 lukem "Usage: %s [-ln] [-u user] [-g group] [-s directory] [directory ...]",
145 1.14 lukem __progname);
146 1.5 cgd exit(1);
147 1.5 cgd }
148 1.5 cgd
149 1.9 mrg int
150 1.5 cgd main(argc, argv)
151 1.5 cgd int argc;
152 1.5 cgd char **argv;
153 1.1 cgd {
154 1.14 lukem struct passwd *pwent;
155 1.14 lukem struct group *grent;
156 1.14 lukem struct tftphdr *tp;
157 1.14 lukem int n = 0;
158 1.9 mrg int ch, on;
159 1.5 cgd int fd = 0;
160 1.9 mrg struct sockaddr_in sin;
161 1.15 explorer int len;
162 1.14 lukem char *tgtuser, *tgtgroup, *ep;
163 1.14 lukem uid_t curuid, tgtuid;
164 1.14 lukem gid_t curgid, tgtgid;
165 1.14 lukem long nid;
166 1.1 cgd
167 1.11 lukem openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
168 1.14 lukem tgtuser = DEFAULTUSER;
169 1.14 lukem tgtgroup = NULL;
170 1.14 lukem curuid = getuid();
171 1.14 lukem curgid = getgid();
172 1.5 cgd
173 1.14 lukem while ((ch = getopt(argc, argv, "g:lns:u:")) != -1)
174 1.9 mrg switch (ch) {
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.14 lukem syslog(LOG_DEBUG, "running as user `%s' (%d), group `%s' (%d)",
265 1.14 lukem tgtuser, tgtuid, tgtgroup ? tgtgroup : "(unspecified)" , tgtgid);
266 1.14 lukem if (curgid != tgtgid) {
267 1.14 lukem if (setgid(tgtgid)) {
268 1.14 lukem syslog(LOG_ERR, "setgid to %d: %m", (int)tgtgid);
269 1.14 lukem exit(1);
270 1.14 lukem }
271 1.14 lukem if (setgroups(0, NULL)) {
272 1.14 lukem syslog(LOG_ERR, "setgroups: %m");
273 1.14 lukem exit(1);
274 1.14 lukem }
275 1.8 mrg }
276 1.8 mrg
277 1.14 lukem if (curuid != tgtuid) {
278 1.14 lukem if (setuid(tgtuid)) {
279 1.14 lukem syslog(LOG_ERR, "setuid to %d: %m", (int)tgtuid);
280 1.14 lukem exit(1);
281 1.14 lukem }
282 1.4 mycroft }
283 1.5 cgd
284 1.9 mrg on = 1;
285 1.5 cgd if (ioctl(fd, FIONBIO, &on) < 0) {
286 1.14 lukem syslog(LOG_ERR, "ioctl(FIONBIO): %m");
287 1.1 cgd exit(1);
288 1.1 cgd }
289 1.1 cgd fromlen = sizeof (from);
290 1.5 cgd n = recvfrom(fd, buf, sizeof (buf), 0,
291 1.1 cgd (struct sockaddr *)&from, &fromlen);
292 1.1 cgd if (n < 0) {
293 1.14 lukem syslog(LOG_ERR, "recvfrom: %m");
294 1.1 cgd exit(1);
295 1.1 cgd }
296 1.1 cgd /*
297 1.1 cgd * Now that we have read the message out of the UDP
298 1.1 cgd * socket, we fork and exit. Thus, inetd will go back
299 1.1 cgd * to listening to the tftp port, and the next request
300 1.1 cgd * to come in will start up a new instance of tftpd.
301 1.1 cgd *
302 1.1 cgd * We do this so that inetd can run tftpd in "wait" mode.
303 1.1 cgd * The problem with tftpd running in "nowait" mode is that
304 1.1 cgd * inetd may get one or more successful "selects" on the
305 1.1 cgd * tftp port before we do our receive, so more than one
306 1.1 cgd * instance of tftpd may be started up. Worse, if tftpd
307 1.1 cgd * break before doing the above "recvfrom", inetd would
308 1.1 cgd * spawn endless instances, clogging the system.
309 1.1 cgd */
310 1.1 cgd {
311 1.1 cgd int pid;
312 1.1 cgd int i, j;
313 1.1 cgd
314 1.1 cgd for (i = 1; i < 20; i++) {
315 1.1 cgd pid = fork();
316 1.1 cgd if (pid < 0) {
317 1.1 cgd sleep(i);
318 1.1 cgd /*
319 1.1 cgd * flush out to most recently sent request.
320 1.1 cgd *
321 1.1 cgd * This may drop some request, but those
322 1.1 cgd * will be resent by the clients when
323 1.1 cgd * they timeout. The positive effect of
324 1.1 cgd * this flush is to (try to) prevent more
325 1.1 cgd * than one tftpd being started up to service
326 1.1 cgd * a single request from a single client.
327 1.1 cgd */
328 1.1 cgd j = sizeof from;
329 1.5 cgd i = recvfrom(fd, buf, sizeof (buf), 0,
330 1.1 cgd (struct sockaddr *)&from, &j);
331 1.1 cgd if (i > 0) {
332 1.1 cgd n = i;
333 1.1 cgd fromlen = j;
334 1.1 cgd }
335 1.1 cgd } else {
336 1.1 cgd break;
337 1.1 cgd }
338 1.1 cgd }
339 1.1 cgd if (pid < 0) {
340 1.14 lukem syslog(LOG_ERR, "fork: %m");
341 1.1 cgd exit(1);
342 1.1 cgd } else if (pid != 0) {
343 1.1 cgd exit(0);
344 1.1 cgd }
345 1.1 cgd }
346 1.15 explorer
347 1.6 mycroft from.sin_len = sizeof(struct sockaddr_in);
348 1.1 cgd from.sin_family = AF_INET;
349 1.15 explorer
350 1.15 explorer /*
351 1.15 explorer * remember what address this was sent to, so we can respond on the
352 1.15 explorer * same interface
353 1.15 explorer */
354 1.15 explorer len = sizeof(sin);
355 1.15 explorer if (getsockname(fd, (struct sockaddr *)&sin, &len) == 0)
356 1.15 explorer sin.sin_port = 0;
357 1.15 explorer else {
358 1.15 explorer memset(&sin, 0, sizeof(sin));
359 1.15 explorer sin.sin_family = AF_INET;
360 1.15 explorer }
361 1.15 explorer
362 1.1 cgd alarm(0);
363 1.5 cgd close(fd);
364 1.1 cgd close(1);
365 1.1 cgd peer = socket(AF_INET, SOCK_DGRAM, 0);
366 1.1 cgd if (peer < 0) {
367 1.14 lukem syslog(LOG_ERR, "socket: %m");
368 1.1 cgd exit(1);
369 1.1 cgd }
370 1.15 explorer if (bind(peer, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
371 1.14 lukem syslog(LOG_ERR, "bind: %m");
372 1.1 cgd exit(1);
373 1.1 cgd }
374 1.1 cgd if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
375 1.14 lukem syslog(LOG_ERR, "connect: %m");
376 1.1 cgd exit(1);
377 1.1 cgd }
378 1.1 cgd tp = (struct tftphdr *)buf;
379 1.1 cgd tp->th_opcode = ntohs(tp->th_opcode);
380 1.1 cgd if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
381 1.1 cgd tftp(tp, n);
382 1.1 cgd exit(1);
383 1.1 cgd }
384 1.1 cgd
385 1.1 cgd /*
386 1.1 cgd * Handle initial connection protocol.
387 1.1 cgd */
388 1.9 mrg static void
389 1.1 cgd tftp(tp, size)
390 1.1 cgd struct tftphdr *tp;
391 1.1 cgd int size;
392 1.1 cgd {
393 1.14 lukem char *cp;
394 1.1 cgd int first = 1, ecode;
395 1.14 lukem struct formats *pf;
396 1.9 mrg char *filename, *mode = NULL; /* XXX gcc */
397 1.1 cgd
398 1.1 cgd filename = cp = tp->th_stuff;
399 1.1 cgd again:
400 1.1 cgd while (cp < buf + size) {
401 1.1 cgd if (*cp == '\0')
402 1.1 cgd break;
403 1.1 cgd cp++;
404 1.1 cgd }
405 1.1 cgd if (*cp != '\0') {
406 1.1 cgd nak(EBADOP);
407 1.1 cgd exit(1);
408 1.1 cgd }
409 1.1 cgd if (first) {
410 1.1 cgd mode = ++cp;
411 1.1 cgd first = 0;
412 1.1 cgd goto again;
413 1.1 cgd }
414 1.1 cgd for (cp = mode; *cp; cp++)
415 1.1 cgd if (isupper(*cp))
416 1.1 cgd *cp = tolower(*cp);
417 1.1 cgd for (pf = formats; pf->f_mode; pf++)
418 1.1 cgd if (strcmp(pf->f_mode, mode) == 0)
419 1.1 cgd break;
420 1.1 cgd if (pf->f_mode == 0) {
421 1.1 cgd nak(EBADOP);
422 1.1 cgd exit(1);
423 1.1 cgd }
424 1.9 mrg ecode = (*pf->f_validate)(&filename, tp->th_opcode);
425 1.9 mrg if (logging) {
426 1.9 mrg syslog(LOG_INFO, "%s: %s request for %s: %s",
427 1.9 mrg verifyhost(&from),
428 1.9 mrg tp->th_opcode == WRQ ? "write" : "read",
429 1.9 mrg filename, errtomsg(ecode));
430 1.9 mrg }
431 1.1 cgd if (ecode) {
432 1.9 mrg /*
433 1.9 mrg * Avoid storms of naks to a RRQ broadcast for a relative
434 1.9 mrg * bootfile pathname from a diskless Sun.
435 1.9 mrg */
436 1.9 mrg if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
437 1.9 mrg exit(0);
438 1.1 cgd nak(ecode);
439 1.1 cgd exit(1);
440 1.1 cgd }
441 1.1 cgd if (tp->th_opcode == WRQ)
442 1.1 cgd (*pf->f_recv)(pf);
443 1.1 cgd else
444 1.1 cgd (*pf->f_send)(pf);
445 1.1 cgd exit(0);
446 1.1 cgd }
447 1.1 cgd
448 1.1 cgd
449 1.1 cgd FILE *file;
450 1.1 cgd
451 1.1 cgd /*
452 1.1 cgd * Validate file access. Since we
453 1.1 cgd * have no uid or gid, for now require
454 1.1 cgd * file to exist and be publicly
455 1.1 cgd * readable/writable.
456 1.1 cgd * If we were invoked with arguments
457 1.1 cgd * from inetd then the file must also be
458 1.1 cgd * in one of the given directory prefixes.
459 1.1 cgd * Note also, full path name must be
460 1.1 cgd * given as we have no login directory.
461 1.1 cgd */
462 1.9 mrg int
463 1.9 mrg validate_access(filep, mode)
464 1.9 mrg char **filep;
465 1.1 cgd int mode;
466 1.1 cgd {
467 1.1 cgd struct stat stbuf;
468 1.1 cgd int fd;
469 1.9 mrg struct dirlist *dirp;
470 1.9 mrg static char pathname[MAXPATHLEN];
471 1.9 mrg char *filename = *filep;
472 1.1 cgd
473 1.9 mrg /*
474 1.9 mrg * Prevent tricksters from getting around the directory restrictions
475 1.9 mrg */
476 1.9 mrg if (strstr(filename, "/../"))
477 1.9 mrg return (EACCESS);
478 1.9 mrg
479 1.9 mrg if (*filename == '/') {
480 1.4 mycroft /*
481 1.9 mrg * Allow the request if it's in one of the approved locations.
482 1.9 mrg * Special case: check the null prefix ("/") by looking
483 1.9 mrg * for length = 1 and relying on the arg. processing that
484 1.9 mrg * it's a /.
485 1.4 mycroft */
486 1.9 mrg for (dirp = dirs; dirp->name != NULL; dirp++) {
487 1.9 mrg if (dirp->len == 1 ||
488 1.9 mrg (!strncmp(filename, dirp->name, dirp->len) &&
489 1.9 mrg filename[dirp->len] == '/'))
490 1.9 mrg break;
491 1.9 mrg }
492 1.9 mrg /* If directory list is empty, allow access to any file */
493 1.9 mrg if (dirp->name == NULL && dirp != dirs)
494 1.1 cgd return (EACCESS);
495 1.9 mrg if (stat(filename, &stbuf) < 0)
496 1.9 mrg return (errno == ENOENT ? ENOTFOUND : EACCESS);
497 1.10 mycroft if (!S_ISREG(stbuf.st_mode))
498 1.9 mrg return (ENOTFOUND);
499 1.9 mrg if (mode == RRQ) {
500 1.9 mrg if ((stbuf.st_mode & S_IROTH) == 0)
501 1.9 mrg return (EACCESS);
502 1.9 mrg } else {
503 1.9 mrg if ((stbuf.st_mode & S_IWOTH) == 0)
504 1.9 mrg return (EACCESS);
505 1.9 mrg }
506 1.1 cgd } else {
507 1.9 mrg int err;
508 1.9 mrg
509 1.9 mrg /*
510 1.9 mrg * Relative file name: search the approved locations for it.
511 1.9 mrg * Don't allow write requests or ones that avoid directory
512 1.9 mrg * restrictions.
513 1.9 mrg */
514 1.9 mrg
515 1.9 mrg if (mode != RRQ || !strncmp(filename, "../", 3))
516 1.1 cgd return (EACCESS);
517 1.9 mrg
518 1.9 mrg /*
519 1.9 mrg * If the file exists in one of the directories and isn't
520 1.9 mrg * readable, continue looking. However, change the error code
521 1.9 mrg * to give an indication that the file exists.
522 1.9 mrg */
523 1.9 mrg err = ENOTFOUND;
524 1.9 mrg if (dirs[0].name != NULL) {
525 1.9 mrg for (dirp = dirs; dirp->name != NULL; dirp++) {
526 1.9 mrg snprintf(pathname, sizeof pathname, "%s/%s",
527 1.9 mrg dirp->name, filename);
528 1.9 mrg if (stat(pathname, &stbuf) == 0 &&
529 1.9 mrg (stbuf.st_mode & S_IFMT) == S_IFREG) {
530 1.9 mrg if ((stbuf.st_mode & S_IROTH) != 0)
531 1.9 mrg break;
532 1.9 mrg err = EACCESS;
533 1.9 mrg }
534 1.9 mrg }
535 1.9 mrg if (dirp->name == NULL)
536 1.9 mrg return (err);
537 1.9 mrg *filep = filename = pathname;
538 1.9 mrg } else
539 1.9 mrg *filep = filename;
540 1.1 cgd }
541 1.1 cgd fd = open(filename, mode == RRQ ? 0 : 1);
542 1.1 cgd if (fd < 0)
543 1.1 cgd return (errno + 100);
544 1.1 cgd file = fdopen(fd, (mode == RRQ)? "r":"w");
545 1.1 cgd if (file == NULL) {
546 1.1 cgd return errno+100;
547 1.1 cgd }
548 1.1 cgd return (0);
549 1.1 cgd }
550 1.1 cgd
551 1.1 cgd int timeout;
552 1.1 cgd jmp_buf timeoutbuf;
553 1.1 cgd
554 1.1 cgd void
555 1.9 mrg timer(dummy)
556 1.9 mrg int dummy;
557 1.1 cgd {
558 1.1 cgd
559 1.1 cgd timeout += rexmtval;
560 1.1 cgd if (timeout >= maxtimeout)
561 1.1 cgd exit(1);
562 1.1 cgd longjmp(timeoutbuf, 1);
563 1.1 cgd }
564 1.1 cgd
565 1.1 cgd /*
566 1.1 cgd * Send the requested file.
567 1.1 cgd */
568 1.9 mrg void
569 1.1 cgd sendfile(pf)
570 1.1 cgd struct formats *pf;
571 1.1 cgd {
572 1.9 mrg struct tftphdr *dp;
573 1.14 lukem struct tftphdr *ap; /* ack packet */
574 1.14 lukem int size, n;
575 1.9 mrg volatile int block;
576 1.1 cgd
577 1.1 cgd signal(SIGALRM, timer);
578 1.1 cgd dp = r_init();
579 1.1 cgd ap = (struct tftphdr *)ackbuf;
580 1.9 mrg block = 1;
581 1.1 cgd do {
582 1.1 cgd size = readit(file, &dp, pf->f_convert);
583 1.1 cgd if (size < 0) {
584 1.1 cgd nak(errno + 100);
585 1.1 cgd goto abort;
586 1.1 cgd }
587 1.1 cgd dp->th_opcode = htons((u_short)DATA);
588 1.1 cgd dp->th_block = htons((u_short)block);
589 1.1 cgd timeout = 0;
590 1.9 mrg (void)setjmp(timeoutbuf);
591 1.1 cgd
592 1.1 cgd send_data:
593 1.1 cgd if (send(peer, dp, size + 4, 0) != size + 4) {
594 1.14 lukem syslog(LOG_ERR, "tftpd: write: %m");
595 1.1 cgd goto abort;
596 1.1 cgd }
597 1.1 cgd read_ahead(file, pf->f_convert);
598 1.1 cgd for ( ; ; ) {
599 1.1 cgd alarm(rexmtval); /* read the ack */
600 1.1 cgd n = recv(peer, ackbuf, sizeof (ackbuf), 0);
601 1.1 cgd alarm(0);
602 1.1 cgd if (n < 0) {
603 1.14 lukem syslog(LOG_ERR, "tftpd: read: %m");
604 1.1 cgd goto abort;
605 1.1 cgd }
606 1.1 cgd ap->th_opcode = ntohs((u_short)ap->th_opcode);
607 1.1 cgd ap->th_block = ntohs((u_short)ap->th_block);
608 1.1 cgd
609 1.1 cgd if (ap->th_opcode == ERROR)
610 1.1 cgd goto abort;
611 1.9 mrg
612 1.1 cgd if (ap->th_opcode == ACK) {
613 1.9 mrg if (ap->th_block == block)
614 1.1 cgd break;
615 1.1 cgd /* Re-synchronize with the other side */
616 1.1 cgd (void) synchnet(peer);
617 1.9 mrg if (ap->th_block == (block -1))
618 1.1 cgd goto send_data;
619 1.1 cgd }
620 1.1 cgd
621 1.1 cgd }
622 1.1 cgd block++;
623 1.1 cgd } while (size == SEGSIZE);
624 1.1 cgd abort:
625 1.1 cgd (void) fclose(file);
626 1.1 cgd }
627 1.1 cgd
628 1.1 cgd void
629 1.9 mrg justquit(dummy)
630 1.9 mrg int dummy;
631 1.1 cgd {
632 1.1 cgd exit(0);
633 1.1 cgd }
634 1.1 cgd
635 1.1 cgd /*
636 1.1 cgd * Receive a file.
637 1.1 cgd */
638 1.9 mrg void
639 1.1 cgd recvfile(pf)
640 1.1 cgd struct formats *pf;
641 1.1 cgd {
642 1.9 mrg struct tftphdr *dp;
643 1.14 lukem struct tftphdr *ap; /* ack buffer */
644 1.14 lukem int n, size;
645 1.9 mrg volatile int block;
646 1.1 cgd
647 1.1 cgd signal(SIGALRM, timer);
648 1.1 cgd dp = w_init();
649 1.1 cgd ap = (struct tftphdr *)ackbuf;
650 1.9 mrg block = 0;
651 1.1 cgd do {
652 1.1 cgd timeout = 0;
653 1.1 cgd ap->th_opcode = htons((u_short)ACK);
654 1.1 cgd ap->th_block = htons((u_short)block);
655 1.1 cgd block++;
656 1.1 cgd (void) setjmp(timeoutbuf);
657 1.1 cgd send_ack:
658 1.1 cgd if (send(peer, ackbuf, 4, 0) != 4) {
659 1.14 lukem syslog(LOG_ERR, "tftpd: write: %m");
660 1.1 cgd goto abort;
661 1.1 cgd }
662 1.1 cgd write_behind(file, pf->f_convert);
663 1.1 cgd for ( ; ; ) {
664 1.1 cgd alarm(rexmtval);
665 1.1 cgd n = recv(peer, dp, PKTSIZE, 0);
666 1.1 cgd alarm(0);
667 1.1 cgd if (n < 0) { /* really? */
668 1.14 lukem syslog(LOG_ERR, "tftpd: read: %m");
669 1.1 cgd goto abort;
670 1.1 cgd }
671 1.1 cgd dp->th_opcode = ntohs((u_short)dp->th_opcode);
672 1.1 cgd dp->th_block = ntohs((u_short)dp->th_block);
673 1.1 cgd if (dp->th_opcode == ERROR)
674 1.1 cgd goto abort;
675 1.1 cgd if (dp->th_opcode == DATA) {
676 1.1 cgd if (dp->th_block == block) {
677 1.1 cgd break; /* normal */
678 1.1 cgd }
679 1.1 cgd /* Re-synchronize with the other side */
680 1.1 cgd (void) synchnet(peer);
681 1.1 cgd if (dp->th_block == (block-1))
682 1.1 cgd goto send_ack; /* rexmit */
683 1.1 cgd }
684 1.1 cgd }
685 1.1 cgd /* size = write(file, dp->th_data, n - 4); */
686 1.1 cgd size = writeit(file, &dp, n - 4, pf->f_convert);
687 1.1 cgd if (size != (n-4)) { /* ahem */
688 1.1 cgd if (size < 0) nak(errno + 100);
689 1.1 cgd else nak(ENOSPACE);
690 1.1 cgd goto abort;
691 1.1 cgd }
692 1.1 cgd } while (size == SEGSIZE);
693 1.1 cgd write_behind(file, pf->f_convert);
694 1.1 cgd (void) fclose(file); /* close data file */
695 1.1 cgd
696 1.1 cgd ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */
697 1.1 cgd ap->th_block = htons((u_short)(block));
698 1.1 cgd (void) send(peer, ackbuf, 4, 0);
699 1.1 cgd
700 1.1 cgd signal(SIGALRM, justquit); /* just quit on timeout */
701 1.1 cgd alarm(rexmtval);
702 1.1 cgd n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
703 1.1 cgd alarm(0);
704 1.1 cgd if (n >= 4 && /* if read some data */
705 1.1 cgd dp->th_opcode == DATA && /* and got a data block */
706 1.1 cgd block == dp->th_block) { /* then my last ack was lost */
707 1.1 cgd (void) send(peer, ackbuf, 4, 0); /* resend final ack */
708 1.1 cgd }
709 1.1 cgd abort:
710 1.1 cgd return;
711 1.1 cgd }
712 1.1 cgd
713 1.13 mycroft const struct errmsg {
714 1.1 cgd int e_code;
715 1.13 mycroft const char *e_msg;
716 1.1 cgd } errmsgs[] = {
717 1.1 cgd { EUNDEF, "Undefined error code" },
718 1.1 cgd { ENOTFOUND, "File not found" },
719 1.1 cgd { EACCESS, "Access violation" },
720 1.1 cgd { ENOSPACE, "Disk full or allocation exceeded" },
721 1.1 cgd { EBADOP, "Illegal TFTP operation" },
722 1.1 cgd { EBADID, "Unknown transfer ID" },
723 1.1 cgd { EEXISTS, "File already exists" },
724 1.1 cgd { ENOUSER, "No such user" },
725 1.1 cgd { -1, 0 }
726 1.1 cgd };
727 1.1 cgd
728 1.13 mycroft static const char *
729 1.9 mrg errtomsg(error)
730 1.9 mrg int error;
731 1.9 mrg {
732 1.9 mrg static char buf[20];
733 1.14 lukem const struct errmsg *pe;
734 1.9 mrg
735 1.9 mrg if (error == 0)
736 1.9 mrg return "success";
737 1.9 mrg for (pe = errmsgs; pe->e_code >= 0; pe++)
738 1.9 mrg if (pe->e_code == error)
739 1.9 mrg return pe->e_msg;
740 1.9 mrg sprintf(buf, "error %d", error);
741 1.9 mrg return buf;
742 1.9 mrg }
743 1.9 mrg
744 1.1 cgd /*
745 1.1 cgd * Send a nak packet (error message).
746 1.1 cgd * Error code passed in is one of the
747 1.1 cgd * standard TFTP codes, or a UNIX errno
748 1.1 cgd * offset by 100.
749 1.1 cgd */
750 1.9 mrg static void
751 1.1 cgd nak(error)
752 1.1 cgd int error;
753 1.1 cgd {
754 1.14 lukem struct tftphdr *tp;
755 1.1 cgd int length;
756 1.14 lukem const struct errmsg *pe;
757 1.1 cgd
758 1.1 cgd tp = (struct tftphdr *)buf;
759 1.1 cgd tp->th_opcode = htons((u_short)ERROR);
760 1.1 cgd for (pe = errmsgs; pe->e_code >= 0; pe++)
761 1.1 cgd if (pe->e_code == error)
762 1.1 cgd break;
763 1.1 cgd if (pe->e_code < 0) {
764 1.1 cgd tp->th_code = EUNDEF; /* set 'undef' errorcode */
765 1.13 mycroft strcpy(tp->th_msg, strerror(error - 100));
766 1.13 mycroft } else {
767 1.13 mycroft tp->th_code = htons((u_short)error);
768 1.13 mycroft strcpy(tp->th_msg, pe->e_msg);
769 1.1 cgd }
770 1.1 cgd length = strlen(pe->e_msg);
771 1.1 cgd tp->th_msg[length] = '\0';
772 1.1 cgd length += 5;
773 1.1 cgd if (send(peer, buf, length, 0) != length)
774 1.14 lukem syslog(LOG_ERR, "nak: %m");
775 1.9 mrg }
776 1.9 mrg
777 1.9 mrg static char *
778 1.9 mrg verifyhost(fromp)
779 1.9 mrg struct sockaddr_in *fromp;
780 1.9 mrg {
781 1.9 mrg struct hostent *hp;
782 1.9 mrg
783 1.9 mrg hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (fromp->sin_addr),
784 1.9 mrg fromp->sin_family);
785 1.9 mrg if (hp)
786 1.9 mrg return hp->h_name;
787 1.9 mrg else
788 1.9 mrg return inet_ntoa(fromp->sin_addr);
789 1.1 cgd }
790