]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/ip/tftpd.c
import E script from bell labs
[plan9front.git] / sys / src / cmd / ip / tftpd.c
1 /*
2  * tftpd - tftp service, see /lib/rfc/rfc783 (now rfc1350 + 234[789])
3  */
4 #include <u.h>
5 #include <libc.h>
6 #include <auth.h>
7 #include <bio.h>
8 #include <ip.h>
9 #include <ndb.h>
10
11 enum
12 {
13         Maxpath=        128,
14
15         Debug=          0,
16
17         Opsize=         sizeof(short),
18         Blksize=        sizeof(short),
19         Hdrsize=        Opsize + Blksize,
20
21         Ackerr=         -1,
22         Ackok=          0,
23         Ackrexmit=      1,
24
25         /* op codes */
26         Tftp_READ       = 1,
27         Tftp_WRITE      = 2,
28         Tftp_DATA       = 3,
29         Tftp_ACK        = 4,
30         Tftp_ERROR      = 5,
31         Tftp_OACK       = 6,            /* option acknowledge */
32
33         Errnotdef       = 0,            /* see textual error instead */
34         Errnotfound     = 1,
35         Errnoaccess     = 2,
36         Errdiskfull     = 3,
37         Errbadop        = 4,
38         Errbadtid       = 5,
39         Errexists       = 6,
40         Errnouser       = 7,
41         Errbadopt       = 8,            /* really bad option value */
42
43         Defsegsize      = 512,
44         Maxsegsize      = 65464,        /* from rfc2348 */
45
46         /*
47          * bandt (viaduct) tunnels use smaller mtu than ether's
48          * (1400 bytes for tcp mss of 1300 bytes).
49          */
50         Bandtmtu        = 1400,
51         /*
52          * maximum size of block's data content, excludes hdrs,
53          * notably IP/UDP and TFTP, using worst-case (IPv6) sizes.
54          */
55         Bandtblksz      = Bandtmtu - 40 - 8,
56         Bcavium         = 1432,         /* cavium's u-boot demands this size */
57         Bci20           = 1468,         /* ci20 u-boot */
58 };
59
60 typedef struct Opt Opt;
61 struct Opt {
62         char    *name;
63         int     *valp;          /* set to client's value if within bounds */
64         int     min;
65         int     max;
66 };
67
68 int     dbg;
69 int     restricted;
70 int     pid;
71
72 /* options */
73 int     blksize = Defsegsize;           /* excluding 4-byte header */
74 int     timeout = 5;                    /* seconds */
75 int     tsize;
76 static Opt option[] = {
77         "timeout",      &timeout,       1,      255,
78         /* see "hack" below */
79         "blksize",      &blksize,       8,      Maxsegsize,
80         "tsize",        &tsize,         0,      ~0UL >> 1,
81 };
82
83 void    sendfile(int, char*, char*, int);
84 void    recvfile(int, char*, char*);
85 void    nak(int, int, char*);
86 void    ack(int, ushort);
87 void    clrcon(void);
88 void    setuser(void);
89 char*   sunkernel(char*);
90 void    remoteaddr(char*, char*, int);
91 void    doserve(int);
92
93 char    bigbuf[32768];
94 char    raddr[64];
95
96 char    *dir = "/lib/tftpd";
97 char    *dirsl;
98 int     dirsllen;
99 char    flog[] = "ipboot";
100 char    net[Maxpath];
101
102 static char *opnames[] = {
103 [Tftp_READ]     "read",
104 [Tftp_WRITE]    "write",
105 [Tftp_DATA]     "data",
106 [Tftp_ACK]      "ack",
107 [Tftp_ERROR]    "error",
108 [Tftp_OACK]     "oack",
109 };
110
111 void
112 usage(void)
113 {
114         fprint(2, "usage: %s [-dr] [-h homedir] [-s svc] [-x netmtpt]\n",
115                 argv0);
116         exits("usage");
117 }
118
119 void
120 main(int argc, char **argv)
121 {
122         char buf[64];
123         char adir[64], ldir[64];
124         int cfd, lcfd, dfd;
125         char *svc = "69";
126
127         setnetmtpt(net, sizeof net, nil);
128         ARGBEGIN{
129         case 'd':
130                 dbg++;
131                 break;
132         case 'h':
133                 dir = EARGF(usage());
134                 break;
135         case 'r':
136                 restricted = 1;
137                 break;
138         case 's':
139                 svc = EARGF(usage());
140                 break;
141         case 'x':
142                 setnetmtpt(net, sizeof net, EARGF(usage()));
143                 break;
144         default:
145                 usage();
146         }ARGEND
147
148         snprint(buf, sizeof buf, "%s/", dir);
149         dirsl = strdup(buf);
150         dirsllen = strlen(dirsl);
151
152         fmtinstall('E', eipfmt);
153         fmtinstall('I', eipfmt);
154
155         /*
156          * setuser calls newns, and typical /lib/namespace files contain
157          * "cd /usr/$user", so call setuser before chdir.
158          */
159         setuser();
160         if(chdir(dir) < 0)
161                 sysfatal("can't get to directory %s: %r", dir);
162
163         if(!dbg)
164                 switch(rfork(RFNOTEG|RFPROC|RFFDG)) {
165                 case -1:
166                         sysfatal("fork: %r");
167                 case 0:
168                         break;
169                 default:
170                         exits(0);
171                 }
172
173         snprint(buf, sizeof buf, "%s/udp!*!%s", net, svc);
174         cfd = announce(buf, adir);
175         if (cfd < 0)
176                 sysfatal("announcing on %s: %r", buf);
177         syslog(dbg, flog, "tftpd started on %s dir %s", buf, adir);
178 //      setuser();
179         for(;;) {
180                 lcfd = listen(adir, ldir);
181                 if(lcfd < 0)
182                         sysfatal("listening on %s: %r", adir);
183
184                 switch(fork()) {
185                 case -1:
186                         sysfatal("fork: %r");
187                 case 0:
188                         dfd = accept(lcfd, ldir);
189                         if(dfd < 0)
190                                 exits(0);
191                         remoteaddr(ldir, raddr, sizeof(raddr));
192                         pid = getpid();
193                         syslog(0, flog, "tftp %d connection from %s dir %s",
194                                 pid, raddr, ldir);
195                         doserve(dfd);
196                         exits("done");
197                         break;
198                 default:
199                         close(lcfd);
200                         continue;
201                 }
202         }
203 }
204
205 static Opt *
206 handleopt(int fd, char *name, char *val)
207 {
208         int n;
209         Opt *op;
210
211         for (op = option; op < option + nelem(option); op++)
212                 if(cistrcmp(name, op->name) == 0) {
213                         n = strtol(val, nil, 10);
214                         if (n < op->min || n > op->max) {
215                                 nak(fd, Errbadopt, "option value out of range");
216                                 syslog(dbg, flog, "tftp bad option value from "
217                                         "client: %s %s", name, val);
218                                 sysfatal("bad option value from client: %s %s",
219                                         name, val);
220                         }
221                         *op->valp = n;
222                         /* incoming 0 for tsize is uninteresting */
223                         if(cistrcmp("tsize", op->name) != 0)
224                                 syslog(dbg, flog, "tftpd %d setting %s to client's %d",
225                                         pid, name, n);
226                         return op;
227                 }
228         return nil;
229 }
230
231 static vlong
232 filesize(char *file)
233 {
234         vlong size;
235         Dir *dp;
236
237         dp = dirstat(file);
238         if (dp == nil)
239                 return -1;
240         size = dp->length;
241         free(dp);
242         return size;
243 }
244
245 /* copy word into bp iff it fits before ep, returns bytes to advance bp. */
246 static int
247 emits(char *word, char *bp, char *ep)
248 {
249         int len;
250
251         len = strlen(word) + 1;
252         if (bp + len >= ep)
253                 return -1;
254         strcpy(bp, word);
255         return len;
256 }
257
258 /* format number into bp iff it fits before ep. */
259 static int
260 emitn(vlong n, char *bp, char *ep)
261 {
262         char numb[32];
263
264         snprint(numb, sizeof numb, "%lld", n);
265         return emits(numb, bp, ep);
266 }
267
268 /*
269  * send an OACK packet to respond to options.  bail early with -1 on error.
270  * p is the packet containing the options.
271  *
272  * hack: bandt (viaducts) uses smaller mtu than ether's
273  * (1400 bytes for tcp mss of 1300 bytes),
274  * so offer at most bandt's mtu minus headers,
275  * to avoid failure of pxe booting via viaduct.
276  * there's an exception for the cavium's u-boot.
277  */
278 static int
279 options(int fd, char *buf, int bufsz, char *file, ushort oper, char *p, int dlen)
280 {
281         int nmlen, vallen, olen, nopts;
282         vlong size;
283         char *val, *bp, *ep;
284         Opt *op;
285
286         buf[0] = 0;
287         buf[1] = Tftp_OACK;
288         bp = buf + Opsize;
289         ep = buf + bufsz;
290         nopts = 0;
291         for (; dlen > 0 && *p != '\0'; p = val + vallen, bp += olen) {
292                 nmlen = strlen(p) + 1;          /* include NUL */
293                 if (nmlen > dlen)
294                         break;
295                 dlen -= nmlen;
296                 val = p + nmlen;
297                 if (dlen <= 0 || *val == '\0')
298                         break;
299
300                 vallen = strlen(val) + 1;
301                 if (vallen > dlen)
302                         break;
303                 dlen -= vallen;
304
305                 olen = 0;
306                 op = handleopt(fd, p, val);
307                 if (op == nil)
308                         continue;
309
310                 nopts++;
311
312                 /* append OACK response to buf */
313                 nmlen = emits(p, bp, ep);       /* option name */
314                 if (nmlen < 0)
315                         return -1;
316                 bp += nmlen;
317
318                 if (oper == Tftp_READ && cistrcmp(p, "tsize") == 0) {
319                         size = filesize(file);
320                         if (size == -1) {
321                                 nak(fd, Errnotfound, "no such file");
322                                 syslog(dbg, flog, "tftpd tsize for "
323                                         "non-existent file %s", file);
324                                 // *op->valp = 0;
325                                 // olen = emits("0", bp, ep);
326                                 return -1;
327                         }
328                         *op->valp = size;
329                         olen = emitn(size, bp, ep);
330                         syslog(dbg, flog, "tftpd %d %s tsize is %,lld",
331                                 pid, file, size);
332                 } else if (oper == Tftp_READ && cistrcmp(p, "blksize") == 0 &&
333                     blksize > Bandtblksz && blksize != Bcavium && blksize != Bci20) {
334                         *op->valp = blksize = Bandtblksz;
335                         olen = emitn(blksize, bp, ep);
336                         syslog(dbg, flog, "tftpd %d overriding blksize to %d",
337                                 pid, blksize);
338                 } else
339                         olen = emits(val, bp, ep);  /* use requested value */
340         }
341         if (nopts == 0)
342                 return 0;               /* no options actually seen */
343
344         if (write(fd, buf, bp - buf) < bp - buf) {
345                 syslog(dbg, flog, "tftpd network write error on oack to %s: %r",
346                         raddr);
347                 sysfatal("tftpd: network write error: %r");
348         }
349         if(Debug)
350                 syslog(dbg, flog, "tftpd oack: options to %s", raddr);
351         return nopts;
352 }
353
354 static void
355 optlog(char *bytes, char *p, int dlen)
356 {
357         char *bp;
358
359         bp = bytes;
360         sprint(bp, "tftpd %d option bytes: ", dlen);
361         bp += strlen(bp);
362         for (; dlen > 0; dlen--, p++)
363                 *bp++ = *p? *p: ' ';
364         *bp = '\0';
365         syslog(dbg, flog, "%s", bytes);
366 }
367
368 /*
369  * replace one occurrence of %[ICE] with ip, cfgpxe name, or ether mac, resp.
370  * we can't easily use $ because u-boot has stranger quoting rules than sh.
371  */
372 char *
373 mapname(char *file)
374 {
375         int nf;
376         char *p, *newnm, *cur, *arpf, *ln, *remip, *bang;
377         char *fields[4];
378         Biobuf *arp;
379
380         p = strchr(file, '%');
381         if (p == nil || p[1] == '\0')
382                 return strdup(file);
383
384         remip = strdup(raddr);
385         newnm = mallocz(strlen(file) + Maxpath, 1);
386         if (remip == nil || newnm == nil)
387                 sysfatal("out of memory");
388
389         bang = strchr(remip, '!');
390         if (bang)
391                 *bang = '\0';                   /* remove !port */
392
393         memmove(newnm, file, p - file);         /* copy up to % */
394         cur = newnm + strlen(newnm);
395         switch(p[1]) {
396         case 'I':
397                 strcpy(cur, remip);             /* remote's IP */
398                 break;
399         case 'C':
400                 strcpy(cur, "/cfg/pxe/");
401                 cur += strlen(cur);
402                 /* fall through */
403         case 'E':
404                 /* look up remote's IP in /net/arp to get mac. */
405                 arpf = smprint("%s/arp", net);
406                 arp = Bopen(arpf, OREAD);
407                 free(arpf);
408                 if (arp == nil)
409                         break;
410                 /* read lines looking for remip in 3rd field of 4 */
411                 while ((ln = Brdline(arp, '\n')) != nil) {
412                         ln[Blinelen(arp)-1] = 0;
413                         nf = tokenize(ln, fields, nelem(fields));
414                         if (nf >= 4 && strcmp(fields[2], remip) == 0) {
415                                 strcpy(cur, fields[3]);
416                                 break;
417                         }
418                 }
419                 Bterm(arp);
420                 break;
421         }
422         strcat(newnm, p + 2);                   /* tail following %x */
423         free(remip);
424         return newnm;
425 }
426
427 void
428 doserve(int fd)
429 {
430         int dlen, opts;
431         char *mode, *p, *file;
432         short op;
433
434         dlen = read(fd, bigbuf, sizeof(bigbuf)-1);
435         if(dlen < 0)
436                 sysfatal("listen read: %r");
437
438         bigbuf[dlen] = '\0';
439         op = (bigbuf[0]<<8) | bigbuf[1];
440         dlen -= Opsize;
441         mode = file = bigbuf + Opsize;
442         while(*mode != '\0' && dlen--)
443                 mode++;
444         mode++;
445         p = mode;
446         while(*p && dlen--)
447                 p++;
448
449         file = mapname(file);   /* we don't free the result; minor leak */
450
451         if(dlen == 0) {
452                 nak(fd, 0, "bad tftpmode");
453                 close(fd);
454                 syslog(dbg, flog, "tftpd %d bad mode %s for file %s from %s",
455                         pid, mode, file, raddr);
456                 return;
457         }
458
459         if(op != Tftp_READ && op != Tftp_WRITE) {
460                 nak(fd, Errbadop, "Illegal TFTP operation");
461                 close(fd);
462                 syslog(dbg, flog, "tftpd %d bad request %d (%s) %s", pid, op,
463                         (op < nelem(opnames)? opnames[op]: "gok"), raddr);
464                 return;
465         }
466
467         if(restricted){
468                 if(file[0] == '#' || strncmp(file, "../", 3) == 0 ||
469                   strstr(file, "/../") != nil ||
470                   (file[0] == '/' && strncmp(file, dirsl, dirsllen) != 0)){
471                         nak(fd, Errnoaccess, "Permission denied");
472                         close(fd);
473                         syslog(dbg, flog, "tftpd %d bad request %d from %s file %s",
474                                 pid, op, raddr, file);
475                         return;
476                 }
477         }
478
479         /*
480          * options are supposed to be negotiated, but the cavium board's
481          * u-boot really wants us to use a block size of 1432 bytes and won't
482          * take `no' for an answer.
483          */
484         p++;                            /* skip NUL after mode */
485         dlen--;
486         opts = 0;
487         if(dlen > 0) {                  /* might have options */
488                 char bytes[32*1024];
489
490                 if(Debug)
491                         optlog(bytes, p, dlen);
492                 opts = options(fd, bytes, sizeof bytes, file, op, p, dlen);
493                 if (opts < 0)
494                         return;
495         }
496         if(op == Tftp_READ)
497                 sendfile(fd, file, mode, opts);
498         else
499                 recvfile(fd, file, mode);
500 }
501
502 void
503 catcher(void *junk, char *msg)
504 {
505         USED(junk);
506
507         if(strncmp(msg, "exit", 4) == 0)
508                 noted(NDFLT);
509         noted(NCONT);
510 }
511
512 static int
513 awaitack(int fd, int block)
514 {
515         int ackblock, al, rxl;
516         ushort op;
517         uchar ack[1024];
518
519         for(rxl = 0; rxl < 10; rxl++) {
520                 memset(ack, 0, Hdrsize);
521                 alarm(1000);
522                 al = read(fd, ack, sizeof(ack));
523                 alarm(0);
524                 if(al < 0) {
525                         if (Debug)
526                                 syslog(dbg, flog, "tftpd %d timed out "
527                                         "waiting for ack from %s", pid, raddr);
528                         return Ackrexmit;
529                 }
530                 op = ack[0]<<8|ack[1];
531                 if(op == Tftp_ERROR) {
532                         if (Debug)
533                                 syslog(dbg, flog, "tftpd %d got error "
534                                         "waiting for ack from %s", pid, raddr);
535                         return Ackerr;
536                 } else if(op != Tftp_ACK) {
537                         syslog(dbg, flog, "tftpd %d rcvd %s op from %s", pid,
538                                 (op < nelem(opnames)? opnames[op]: "gok"),
539                                 raddr);
540                         return Ackerr;
541                 }
542                 ackblock = ack[2]<<8|ack[3];
543                 if (Debug)
544                         syslog(dbg, flog, "tftpd %d read ack of %d bytes "
545                                 "for block %d", pid, al, ackblock);
546                 if(ackblock == block)
547                         return Ackok;           /* for block just sent */
548                 else if(ackblock == block + 1)  /* intel pxe eof bug */
549                         return Ackok;
550                 else if(ackblock == 0xffff)
551                         return Ackrexmit;
552                 else
553                         /* ack is for some other block; ignore it, try again */
554                         syslog(dbg, flog, "tftpd %d expected ack for block %d, "
555                                 "got %d", pid, block, ackblock);
556         }
557         return Ackrexmit;
558 }
559
560 void
561 sendfile(int fd, char *name, char *mode, int opts)
562 {
563         int file, block, ret, rexmit, n, txtry;
564         uchar buf[Maxsegsize+Hdrsize];
565         char errbuf[ERRMAX];
566
567         file = -1;
568         syslog(dbg, flog, "tftpd %d send file '%s' %s to %s",
569                 pid, name, mode, raddr);
570         name = sunkernel(name);
571         if(name == 0){
572                 nak(fd, 0, "not in our database");
573                 goto error;
574         }
575
576         notify(catcher);
577
578         file = open(name, OREAD);
579         if(file < 0) {
580                 errstr(errbuf, sizeof errbuf);
581                 nak(fd, 0, errbuf);
582                 goto error;
583         }
584         block = 0;
585         rexmit = Ackok;
586         n = 0;
587         /*
588          * if we sent an oack previously, wait for the client's ack or error.
589          * if we get no ack for our oack, it could be that we returned
590          * a tsize that the client can't handle, or it could be intel
591          * pxe just read-with-tsize to get size, couldn't be bothered to
592          * ack our oack and has just gone ahead and issued another read.
593          */
594         if(opts && awaitack(fd, 0) != Ackok)
595                 goto error;
596
597         for(txtry = 0; txtry < timeout;) {
598                 if(rexmit == Ackok) {
599                         block++;
600                         buf[0] = 0;
601                         buf[1] = Tftp_DATA;
602                         buf[2] = block>>8;
603                         buf[3] = block;
604                         n = read(file, buf+Hdrsize, blksize);
605                         if(n < 0) {
606                                 errstr(errbuf, sizeof errbuf);
607                                 nak(fd, 0, errbuf);
608                                 goto error;
609                         }
610                         txtry = 0;
611                 }
612                 else {
613                         syslog(dbg, flog, "tftpd %d rexmit %d %s:%d to %s",
614                                 pid, Hdrsize+n, name, block, raddr);
615                         txtry++;
616                 }
617
618                 ret = write(fd, buf, Hdrsize+n);
619                 if(ret < Hdrsize+n) {
620                         syslog(dbg, flog,
621                                 "tftpd network write error on %s to %s: %r",
622                                 name, raddr);
623                         sysfatal("tftpd: network write error: %r");
624                 }
625                 if (Debug)
626                         syslog(dbg, flog, "tftpd %d sent block %d", pid, block);
627
628                 rexmit = awaitack(fd, block);
629                 if (rexmit == Ackerr)
630                         break;
631                 if(ret != blksize+Hdrsize && rexmit == Ackok)
632                         break;
633         }
634         syslog(dbg, flog, "tftpd %d done sending file '%s' %s to %s",
635                 pid, name, mode, raddr);
636 error:
637         close(fd);
638         close(file);
639 }
640
641 void
642 recvfile(int fd, char *name, char *mode)
643 {
644         ushort op, block, inblock;
645         uchar buf[Maxsegsize+8];
646         char errbuf[ERRMAX];
647         int n, ret, file;
648
649         syslog(dbg, flog, "receive file '%s' %s from %s", name, mode, raddr);
650
651         file = create(name, OWRITE, 0666);
652         if(file < 0) {
653                 errstr(errbuf, sizeof errbuf);
654                 nak(fd, 0, errbuf);
655                 syslog(dbg, flog, "can't create %s: %s", name, errbuf);
656                 return;
657         }
658
659         block = 0;
660         ack(fd, block);
661         block++;
662
663         for (;;) {
664                 alarm(15000);
665                 n = read(fd, buf, blksize+8);
666                 alarm(0);
667                 if(n < 0) {
668                         syslog(dbg, flog, "tftpd: network error reading %s: %r",
669                                 name);
670                         goto error;
671                 }
672                 /*
673                  * NB: not `<='; just a header is legal and happens when
674                  * file being read is a multiple of segment-size bytes long.
675                  */
676                 if(n < Hdrsize) {
677                         syslog(dbg, flog,
678                                 "tftpd: short read from network, reading %s",
679                                 name);
680                         goto error;
681                 }
682                 op = buf[0]<<8|buf[1];
683                 if(op == Tftp_ERROR) {
684                         syslog(dbg, flog, "tftpd: tftp error reading %s", name);
685                         goto error;
686                 }
687
688                 n -= Hdrsize;
689                 inblock = buf[2]<<8|buf[3];
690                 if(op == Tftp_DATA) {
691                         if(inblock == block) {
692                                 ret = write(file, buf+Hdrsize, n);
693                                 if(ret != n) {
694                                         errstr(errbuf, sizeof errbuf);
695                                         nak(fd, 0, errbuf);
696                                         syslog(dbg, flog,
697                                             "tftpd: error writing %s: %s",
698                                                 name, errbuf);
699                                         goto error;
700                                 }
701                                 ack(fd, block);
702                                 block++;
703                         } else
704                                 ack(fd, 0xffff);        /* tell him to resend */
705                 }
706         }
707 error:
708         close(file);
709 }
710
711 void
712 ack(int fd, ushort block)
713 {
714         uchar ack[4];
715         int n;
716
717         ack[0] = 0;
718         ack[1] = Tftp_ACK;
719         ack[2] = block>>8;
720         ack[3] = block;
721
722         n = write(fd, ack, 4);
723         if(n < 4)
724                 sysfatal("network write: %r");
725 }
726
727 void
728 nak(int fd, int code, char *msg)
729 {
730         char buf[128];
731         int n;
732
733         n = 5 + strlen(msg);
734         if(n > sizeof(buf))
735                 n = sizeof(buf);
736         buf[0] = 0;
737         buf[1] = Tftp_ERROR;
738         buf[2] = 0;
739         buf[3] = code;
740         memmove(buf+4, msg, n - 5);
741         buf[n-1] = 0;
742         if(write(fd, buf, n) != n)
743                 sysfatal("write nak: %r");
744 }
745
746 void
747 setuser(void)
748 {
749         int fd;
750
751         fd = open("#c/user", OWRITE);
752         if(fd < 0 || write(fd, "none", strlen("none")) < 0)
753                 sysfatal("can't become none: %r");
754         close(fd);
755         if(newns("none", nil) < 0)
756                 sysfatal("can't build namespace: %r");
757 }
758
759 char*
760 lookup(char *sattr, char *sval, char *tattr, char *tval, int len)
761 {
762         static Ndb *db;
763         char *attrs[1];
764         Ndbtuple *t;
765
766         if(db == nil)
767                 db = ndbopen(0);
768         if(db == nil)
769                 return nil;
770
771         if(sattr == nil)
772                 sattr = ipattr(sval);
773
774         attrs[0] = tattr;
775         t = ndbipinfo(db, sattr, sval, attrs, 1);
776         if(t == nil)
777                 return nil;
778         strncpy(tval, t->val, len);
779         tval[len-1] = 0;
780         ndbfree(t);
781         return tval;
782 }
783
784 /*
785  *  for sun kernel boots, replace the requested file name with
786  *  a one from our database.  If the database doesn't specify a file,
787  *  don't answer.
788  */
789 char*
790 sunkernel(char *name)
791 {
792         ulong addr;
793         uchar v4[IPv4addrlen];
794         uchar v6[IPaddrlen];
795         char buf[256];
796         char ipbuf[128];
797         char *suffix;
798
799         addr = strtoul(name, &suffix, 16);
800         if(suffix-name != 8 || (strcmp(suffix, "") != 0 && strcmp(suffix, ".SUN") != 0))
801                 return name;
802
803         v4[0] = addr>>24;
804         v4[1] = addr>>16;
805         v4[2] = addr>>8;
806         v4[3] = addr;
807         v4tov6(v6, v4);
808         sprint(ipbuf, "%I", v6);
809         return lookup("ip", ipbuf, "bootf", buf, sizeof buf);
810 }
811
812 void
813 remoteaddr(char *dir, char *raddr, int len)
814 {
815         char buf[64];
816         int fd, n;
817
818         snprint(buf, sizeof(buf), "%s/remote", dir);
819         fd = open(buf, OREAD);
820         if(fd < 0){
821                 snprint(raddr, sizeof(raddr), "unknown");
822                 return;
823         }
824         n = read(fd, raddr, len-1);
825         close(fd);
826         if(n <= 0){
827                 snprint(raddr, sizeof(raddr), "unknown");
828                 return;
829         }
830         if(n > 0)
831                 n--;
832         raddr[n] = 0;
833 }