Move to new version of tftpd (changes from <guyton@rand-unix>).
[unix-history] / usr / src / libexec / tftpd / tftpd.c
CommitLineData
8c5eec2f
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
ce4fd43b 13#ifndef lint
076ae92c 14static char sccsid[] = "@(#)tftpd.c 5.2 (Berkeley) %G%";
8c5eec2f 15#endif not lint
f8675e8e
SL
16
17/*
18 * Trivial file transfer protocol server.
19 */
20#include <sys/types.h>
f8675e8e 21#include <sys/socket.h>
f8675e8e 22#include <sys/ioctl.h>
ce4fd43b
SL
23#include <sys/wait.h>
24#include <sys/stat.h>
de3b21e8
SL
25
26#include <netinet/in.h>
27
533343f1
SL
28#include <arpa/tftp.h>
29
de3b21e8 30#include <signal.h>
f8675e8e 31#include <stdio.h>
f8675e8e
SL
32#include <errno.h>
33#include <ctype.h>
4d5e33bd 34#include <netdb.h>
103499bc 35#include <setjmp.h>
3f99c0f7 36#include <syslog.h>
103499bc 37
103499bc 38#define TIMEOUT 5
de3b21e8 39
f8675e8e 40extern int errno;
4d5e33bd 41struct sockaddr_in sin = { AF_INET };
bb933cc2 42int peer;
103499bc
SL
43int rexmtval = TIMEOUT;
44int maxtimeout = 5*TIMEOUT;
f8675e8e 45char buf[BUFSIZ];
bb933cc2
MK
46struct sockaddr_in from;
47int fromlen;
f8675e8e 48
bb933cc2 49main()
f8675e8e 50{
f8675e8e
SL
51 register struct tftphdr *tp;
52 register int n;
53
076ae92c 54 openlog("tftpd", LOG_PID, LOG_DAEMON);
bb933cc2
MK
55 alarm(10);
56 fromlen = sizeof (from);
57 n = recvfrom(0, buf, sizeof (buf), 0,
58 (caddr_t)&from, &fromlen);
59 if (n < 0) {
60 perror("tftpd: recvfrom");
4d5e33bd
SL
61 exit(1);
62 }
bb933cc2
MK
63 from.sin_family = AF_INET;
64 alarm(0);
bb933cc2
MK
65 close(0);
66 close(1);
67 peer = socket(AF_INET, SOCK_DGRAM, 0);
68 if (peer < 0) {
3f99c0f7 69 syslog(LOG_ERR, "socket: %m");
bb933cc2
MK
70 exit(1);
71 }
72 if (bind(peer, (caddr_t)&sin, sizeof (sin)) < 0) {
3f99c0f7 73 syslog(LOG_ERR, "bind: %m");
bb933cc2 74 exit(1);
f8675e8e 75 }
bb933cc2 76 if (connect(peer, (caddr_t)&from, sizeof(from)) < 0) {
3f99c0f7 77 syslog(LOG_ERR, "connect: %m");
bb933cc2 78 exit(1);
f8675e8e 79 }
bb933cc2
MK
80 tp = (struct tftphdr *)buf;
81 tp->th_opcode = ntohs(tp->th_opcode);
82 if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
83 tftp(tp, n);
84 exit(1);
103499bc
SL
85}
86
f8675e8e
SL
87int validate_access();
88int sendfile(), recvfile();
89
90struct formats {
91 char *f_mode;
92 int (*f_validate)();
93 int (*f_send)();
94 int (*f_recv)();
95} formats[] = {
96 { "netascii", validate_access, sendfile, recvfile },
97 { "octet", validate_access, sendfile, recvfile },
98#ifdef notdef
99 { "mail", validate_user, sendmail, recvmail },
100#endif
101 { 0 }
102};
103
f8675e8e
SL
104/*
105 * Handle initial connection protocol.
106 */
bb933cc2 107tftp(tp, size)
f8675e8e
SL
108 struct tftphdr *tp;
109 int size;
110{
111 register char *cp;
112 int first = 1, ecode;
113 register struct formats *pf;
114 char *filename, *mode;
115
f8675e8e
SL
116 filename = cp = tp->th_stuff;
117again:
118 while (cp < buf + size) {
119 if (*cp == '\0')
120 break;
121 cp++;
122 }
123 if (*cp != '\0') {
124 nak(EBADOP);
125 exit(1);
126 }
127 if (first) {
128 mode = ++cp;
129 first = 0;
130 goto again;
131 }
132 for (cp = mode; *cp; cp++)
133 if (isupper(*cp))
134 *cp = tolower(*cp);
135 for (pf = formats; pf->f_mode; pf++)
136 if (strcmp(pf->f_mode, mode) == 0)
137 break;
138 if (pf->f_mode == 0) {
139 nak(EBADOP);
140 exit(1);
141 }
bb933cc2 142 ecode = (*pf->f_validate)(filename, tp->th_opcode);
f8675e8e
SL
143 if (ecode) {
144 nak(ecode);
145 exit(1);
146 }
147 if (tp->th_opcode == WRQ)
148 (*pf->f_recv)(pf);
149 else
150 (*pf->f_send)(pf);
151 exit(0);
152}
153
bb933cc2
MK
154int fd;
155
f8675e8e
SL
156/*
157 * Validate file access. Since we
158 * have no uid or gid, for now require
159 * file to exist and be publicly
160 * readable/writable.
161 * Note also, full path name must be
162 * given as we have no login directory.
163 */
bb933cc2 164validate_access(file, mode)
f8675e8e 165 char *file;
f8675e8e
SL
166 int mode;
167{
168 struct stat stbuf;
169
170 if (*file != '/')
171 return (EACCESS);
172 if (stat(file, &stbuf) < 0)
173 return (errno == ENOENT ? ENOTFOUND : EACCESS);
174 if (mode == RRQ) {
175 if ((stbuf.st_mode&(S_IREAD >> 6)) == 0)
176 return (EACCESS);
177 } else {
178 if ((stbuf.st_mode&(S_IWRITE >> 6)) == 0)
179 return (EACCESS);
180 }
181 fd = open(file, mode == RRQ ? 0 : 1);
182 if (fd < 0)
183 return (errno + 100);
184 return (0);
185}
186
103499bc
SL
187int timeout;
188jmp_buf timeoutbuf;
f8675e8e
SL
189
190timer()
191{
103499bc
SL
192
193 timeout += rexmtval;
194 if (timeout >= maxtimeout)
f8675e8e 195 exit(1);
103499bc 196 longjmp(timeoutbuf, 1);
f8675e8e
SL
197}
198
199/*
200 * Send the requested file.
201 */
202sendfile(pf)
203 struct format *pf;
204{
205 register struct tftphdr *tp;
206 register int block = 1, size, n;
207
103499bc 208 signal(SIGALRM, timer);
f8675e8e
SL
209 tp = (struct tftphdr *)buf;
210 do {
211 size = read(fd, tp->th_data, SEGSIZE);
212 if (size < 0) {
213 nak(errno + 100);
bb933cc2 214 return;
f8675e8e
SL
215 }
216 tp->th_opcode = htons((u_short)DATA);
217 tp->th_block = htons((u_short)block);
218 timeout = 0;
103499bc 219 (void) setjmp(timeoutbuf);
bb933cc2
MK
220 if (send(peer, buf, size + 4, 0) != size + 4) {
221 perror("tftpd: send");
222 return;
f8675e8e 223 }
103499bc
SL
224 do {
225 alarm(rexmtval);
bb933cc2 226 n = recv(peer, buf, sizeof (buf), 0);
f8675e8e 227 alarm(0);
103499bc 228 if (n < 0) {
bb933cc2
MK
229 perror("tftpd: recv");
230 return;
103499bc
SL
231 }
232 tp->th_opcode = ntohs((u_short)tp->th_opcode);
233 tp->th_block = ntohs((u_short)tp->th_block);
234 if (tp->th_opcode == ERROR)
bb933cc2 235 return;
103499bc 236 } while (tp->th_opcode != ACK || tp->th_block != block);
f8675e8e
SL
237 block++;
238 } while (size == SEGSIZE);
f8675e8e
SL
239}
240
241/*
242 * Receive a file.
243 */
244recvfile(pf)
245 struct format *pf;
246{
247 register struct tftphdr *tp;
248 register int block = 0, n, size;
249
103499bc 250 signal(SIGALRM, timer);
f8675e8e
SL
251 tp = (struct tftphdr *)buf;
252 do {
253 timeout = 0;
f8675e8e
SL
254 tp->th_opcode = htons((u_short)ACK);
255 tp->th_block = htons((u_short)block);
256 block++;
103499bc 257 (void) setjmp(timeoutbuf);
bb933cc2
MK
258 if (send(peer, buf, 4, 0) != 4) {
259 perror("tftpd: send");
103499bc 260 goto abort;
f8675e8e 261 }
103499bc
SL
262 do {
263 alarm(rexmtval);
bb933cc2 264 n = recv(peer, buf, sizeof (buf), 0);
f8675e8e 265 alarm(0);
103499bc 266 if (n < 0) {
bb933cc2 267 perror("tftpd: recv");
103499bc
SL
268 goto abort;
269 }
270 tp->th_opcode = ntohs((u_short)tp->th_opcode);
271 tp->th_block = ntohs((u_short)tp->th_block);
272 if (tp->th_opcode == ERROR)
273 goto abort;
274 } while (tp->th_opcode != DATA || block != tp->th_block);
f8675e8e
SL
275 size = write(fd, tp->th_data, n - 4);
276 if (size < 0) {
277 nak(errno + 100);
103499bc 278 goto abort;
f8675e8e
SL
279 }
280 } while (size == SEGSIZE);
103499bc 281abort:
f8675e8e
SL
282 tp->th_opcode = htons((u_short)ACK);
283 tp->th_block = htons((u_short)(block));
bb933cc2 284 (void) send(peer, buf, 4, 0);
f8675e8e
SL
285}
286
287struct errmsg {
288 int e_code;
289 char *e_msg;
290} errmsgs[] = {
291 { EUNDEF, "Undefined error code" },
292 { ENOTFOUND, "File not found" },
293 { EACCESS, "Access violation" },
294 { ENOSPACE, "Disk full or allocation exceeded" },
295 { EBADOP, "Illegal TFTP operation" },
296 { EBADID, "Unknown transfer ID" },
297 { EEXISTS, "File already exists" },
298 { ENOUSER, "No such user" },
299 { -1, 0 }
300};
301
302/*
303 * Send a nak packet (error message).
304 * Error code passed in is one of the
305 * standard TFTP codes, or a UNIX errno
306 * offset by 100.
307 */
308nak(error)
309 int error;
310{
311 register struct tftphdr *tp;
312 int length;
313 register struct errmsg *pe;
314 extern char *sys_errlist[];
315
316 tp = (struct tftphdr *)buf;
317 tp->th_opcode = htons((u_short)ERROR);
318 tp->th_code = htons((u_short)error);
319 for (pe = errmsgs; pe->e_code >= 0; pe++)
320 if (pe->e_code == error)
321 break;
322 if (pe->e_code < 0)
323 pe->e_msg = sys_errlist[error - 100];
324 strcpy(tp->th_msg, pe->e_msg);
325 length = strlen(pe->e_msg);
326 tp->th_msg[length] = '\0';
327 length += 5;
bb933cc2 328 if (send(peer, buf, length, 0) != length)
f8675e8e 329 perror("nak");
bb933cc2 330 exit(1);
f8675e8e 331}