implement Ftpfs; include "sys.m"; include "draw.m"; include "bufio.m"; include "daytime.m"; include "string.m"; include "styxlib.m"; styxlib: Styxlib; Styxserver, Chan, Tmsg, Rmsg: import styxlib; Ftpfs: module { init: fn(nil: ref Draw->Context, argv: list of string); }; sys: Sys; time: Daytime; str: String; bufio: Bufio; Iobuf: import bufio; Tm: import time; FD: import Sys; FileIO: import Sys; Connection: import Sys; Rread: import Sys; Rwrite: import Sys; Dir: import Sys; # # File system node. Refers to parent and file structure. # Siblings are linked. The head is parent.children. # Node : adt { dir: Dir; uniq: int; parent: cyclic ref Node; sibs: cyclic ref Node; children: cyclic ref Node; file: cyclic ref File; depth: int; longname: string; cached: int; valid: int; extendpath: fn(parent: self ref Node, elem: string) : ref Node; fixsymbolic: fn(n: self ref Node); invalidate: fn(n: self ref Node); markcached: fn(n: self ref Node); uncache: fn(n: self ref Node); uncachedir: fn(parent: self ref Node, child: ref Node); convD2M: fn(n: self ref Node, x: int); qid: fn(n: self ref Node) : Sys->Qid; fileget: fn(n: self ref Node) : ref File; filefree: fn(n: self ref Node); fileclean: fn(n: self ref Node); fileisdirty: fn(n: self ref Node) : int; filedirty: fn(n: self ref Node); fileread: fn(n: self ref Node, b: array of byte, off, c: int) : int; filewrite: fn(n: self ref Node, b: array of byte, off, c: int) : int; action: fn(n: self ref Node, cmd: string) : int; createdir: fn(n: self ref Node) : int; createfile: fn(n: self ref Node) : int; changedir: fn(n: self ref Node) : int; docreate: fn(n: self ref Node) : int; mkunique: fn(parent: self ref Node, off: int) : int; pathname: fn(n: self ref Node) : string; readdir: fn(n: self ref Node) : int; readfile: fn(n: self ref Node) : int; removedir: fn(n: self ref Node) : int; removefile: fn(n: self ref Node) : int; }; # # Styx protocol file identifier. # Fid : adt { fid: int; node: ref Node; busy: int; }; # # Foreign file with cache. # File : adt { cache: array of byte; length: int; offset: int; fd: ref FD; inuse, dirty: int; atime: int; node: cyclic ref Node; tempname: string; createtmp: fn(f: self ref File) : ref FD; }; ftp: Connection; dfid: ref FD; dfidiob: ref Iobuf; buffresidue: int = 0; tbuff: array of byte; rbuff: array of byte; ccfd: ref FD; stdin, stderr: ref FD; fids: list of ref Fid; BSZ: con 8192; Chunk: con 1024; Nfiles: con 128; CHSYML: con 16r40000000; mountpoint: string = "/n/ftp"; user: string = nil; password: string; hostname: string = "kremvax"; anon: string = "anon"; firewall: string = "tcp!$PROXY!402"; myname: string = "anon"; myhost: string = "lucent.com"; proxyid: string; proxyhost: string; errstr: string; net: string; port: int; Enosuchfile: con "file does not exist"; Eftpproto: con "ftp protocol error"; Eshutdown: con "remote shutdown"; Eioerror: con "io error"; Enotadirectory: con "not a directory"; Eisadirectory: con "is a directory"; Epermission: con "permission denied"; Ebadoffset: con "bad offset"; Ebadlength: con "bad length"; Enowstat: con "wstat not implemented"; Emesgmismatch: con "message size mismatch"; remdir: ref Node; remroot: ref Node; remrootpath: string; ss: ref Styxserver; heartbeatpid, controlmanagerpid: int; # # FTP protocol codes are 3 digits >= 100. # The code type is obtained by dividing by 100. # Syserr: con -2; Syntax: con -1; Shutdown: con 0; Extra: con 1; Success: con 2; Incomplete: con 3; TempFail: con 4; PermFail: con 5; Impossible: con 6; Err: con 7; debug: int = 0; quiet: int = 0; active: int = 0; cdtoroot: int = 0; proxy: int = 0; mcon, scon: ref FileIO; mountfd: ref FD; styxlibfd: ref FD; usage() { sys->print("usage: ftpfs [-/dpq] [-m mountpoint] [-a password] ftphost\n"); exit; } # # Set up FDs for service. # connect() : string { pip := array[2] of ref Sys->FD; if(sys->pipe(pip) < 0) return sys->sprint("can't create pipe: %r"); mountfd = pip[0]; styxlibfd = pip[1]; return nil; } #shut(s: string) #{ # sys->print("ftpfs: %s shutdown\n", s); #} # # Mount server. Must be spawned because it does # an attach transaction. # mount(mountpoint: string) { if (sys->mount(mountfd, mountpoint, sys->MREPL | sys->MCREATE, nil) < 0) { sys->print("mount %s failed: %r\n", mountpoint); shutdown(); } mountfd = nil; } # # Keep the link alive. # beatquanta: con 10; beatlimit: con 10; beatcount: int; activity: int; transfer: int; heartbeat(pidc: chan of int) { pid := sys->pctl(0, nil); pidc <-= pid; for (;;) { sys->sleep(beatquanta * 1000); if (activity || transfer) { beatcount = 0; activity = 0; continue; } beatcount++; if (beatcount == beatlimit) { acquire(); if (sendrequest("NOOP", 0) == Success) getreply(0); release(); beatcount = 0; activity = 0; } } } # # Control lock. # grant: chan of int; notify: chan of int; controlmanager(pidc: chan of int) { pidc <-= sys->pctl(0, nil); for (;;) { <- grant; <- notify; } } acquire() { grant <- = 0; } release() { notify <- = 0; } # # Data formatting routines. # sendreply(r: ref Rmsg) { if (debug) sys->print("> %s\n", styxlib->rmsg2s(r)); ss.reply(r); } rerror(tag: int, s: string) { if (debug) sys->print("error: %s\n", s); sendreply(ref Rmsg.Error(tag, s)); } seterr(e: int, s: string) : int { case e { Syserr => errstr = Eioerror; Syntax => errstr = Eftpproto; Shutdown => errstr = Eshutdown; * => errstr = s; } return -1; } # # Node routines. # anode: Node; npath: int = 1; newnode(parent: ref Node, name: string) : ref Node { n := ref anode; n.dir.name = name; n.dir.atime = time->now(); n.children = nil; n.longname = name; if (parent != nil) { n.parent = parent; n.sibs = parent.children; parent.children = n; n.depth = parent.depth + 1; n.valid = 0; } else { n.parent = n; n.sibs = nil; n.depth = 0; n.valid = 1; n.dir.uid = anon; n.dir.gid = anon; n.dir.mtime = n.dir.atime; } n.file = nil; n.uniq = npath++; n.cached = 0; return n; } Node.extendpath(parent: self ref Node, elem: string) : ref Node { n: ref Node; for (n = parent.children; n != nil; n = n.sibs) if (n.dir.name == elem) return n; return newnode(parent, elem); } Node.markcached(n: self ref Node) { n.cached = 1; n.dir.atime = time->now(); } Node.uncache(n: self ref Node) { if (n.fileisdirty()) n.createfile(); n.filefree(); n.cached = 0; } Node.uncachedir(parent: self ref Node, child: ref Node) { sp: ref Node; if (parent == nil || parent == child) return; for (sp = parent.children; sp != nil; sp = sp.sibs) if (sp != child && sp.file != nil && !sp.file.dirty && sp.file.fd != nil) { sp.filefree(); sp.cached = 0; } } Node.invalidate(node: self ref Node) { n: ref Node; node.uncachedir(nil); for (n = node.children; n != nil; n = n.sibs) { n.cached = 0; n.invalidate(); n.valid = 0; } } Node.fixsymbolic(n: self ref Node) { if (n.changedir() == 0) { n.dir.mode |= Sys->CHDIR; n.uniq |= Sys->CHDIR; } n.dir.mode &= ~CHSYML; } Node.convD2M(n: self ref Node, x: int) { styxlib->convD2M(tbuff[x:], n.dir); } Node.qid(n: self ref Node) : Sys->Qid { return Sys->Qid(n.uniq, 0); } # # File routines. # ntmp: int; files: list of ref File; nfiles: int; afile: File; atime: int; # # Allocate a file structure for a node. If too many # are already allocated discard the oldest. # Node.fileget(n: self ref Node) : ref File { f, o: ref File; l: list of ref File; if (n.file != nil) return n.file; o = nil; for (l = files; l != nil; l = tl l) { f = hd l; if (f.inuse == 0) break; if (!f.dirty && (o == nil || o.atime > f.atime)) o = f; } if (l == nil) { if (nfiles == Nfiles && o != nil) { o.node.uncache(); f = o; } else { f = ref afile; files = f :: files; nfiles++; } } n.file = f; f.node = n; f.atime = atime++; f.inuse = 1; f.dirty = 0; f.length = 0; f.fd = nil; return f; } # # Create a temporary file for a local copy of a file. # If too many are open uncache parent. # File.createtmp(f: self ref File) : ref FD { t := "/tmp/ftp." + string time->now() + "." + string ntmp; if (ntmp >= 16) f.node.parent.uncachedir(f.node); f.fd = sys->create(t, Sys->ORDWR | Sys->ORCLOSE, 8r600); f.tempname = t; f.offset = 0; ntmp++; return f.fd; } # # Read 'c' bytes at offset 'off' from a file into buffer 'b'. # Node.fileread(n: self ref Node, b: array of byte, off, c: int) : int { f: ref File; t, i: int; f = n.file; if (off + c > f.length) c = f.length - off; for (t = 0; t < c; t += i) { if (off >= f.length) return t; if (off < Chunk) { i = c; if (off + i > Chunk) i = Chunk - off; b[t:] = f.cache[off: off + i]; } else { if (f.offset != off) { if (sys->seek(f.fd, off, Sys->SEEKSTART) < 0) { f.offset = -1; return seterr(Err, sys->sprint("seek temp failed: %r")); } } if (t == 0) i = sys->read(f.fd, b, c - t); else i = sys->read(f.fd, rbuff, c - t); if (i < 0) { f.offset = -1; return seterr(Err, sys->sprint("read temp failed: %r")); } if (i == 0) break; if (t > 0) b[t:] = rbuff[0: i]; f.offset = off + i; } off += i; } return t; } # # Write 'c' bytes at offset 'off' to a file from buffer 'b'. # Node.filewrite(n: self ref Node, b: array of byte, off, c: int) : int { f: ref File; t, i: int; f = n.fileget(); if (f.cache == nil) f.cache = array[Chunk] of byte; for (t = 0; t < c; t += i) { if (off < Chunk) { i = c; if (off + i > Chunk) i = Chunk - off; f.cache[off:] = b[t: t + i]; } else { if (f.fd == nil) { if (f.createtmp() == nil) return seterr(Err, sys->sprint("temp file: %r")); if (sys->write(f.fd, f.cache, Chunk) != Chunk) { f.offset = -1; return seterr(Err, sys->sprint("write temp failed: %r")); } f.offset = Chunk; f.length = Chunk; } if (f.offset != off) { if (off > f.length) { # extend the file with zeroes # sparse files may not be supported } if (sys->seek(f.fd, off, Sys->SEEKSTART) < 0) { f.offset = -1; return seterr(Err, sys->sprint("seek temp failed: %r")); } } i = sys->write(f.fd, b[t:len b], c - t); if (i != c - t) { f.offset = -1; return seterr(Err, sys->sprint("write temp failed: %r")); } } off += i; f.offset = off; } if (off > f.length) f.length = off; return t; } Node.filefree(n: self ref Node) { f: ref File; f = n.file; if (f == nil) return; if (f.fd != nil) { ntmp--; f.fd = nil; f.tempname = nil; } f.cache = nil; f.length = 0; f.inuse = 0; f.dirty = 0; n.file = nil; } Node.fileclean(n: self ref Node) { if (n.file != nil) n.file.dirty = 0; } Node.fileisdirty(n: self ref Node) : int { return n.file != nil && n.file.dirty; } Node.filedirty(n: self ref Node) { f: ref File; f = n.fileget(); f.dirty = 1; } # # Fid management. # afid: Fid; getfid(fid: int) : ref Fid { l: list of ref Fid; f, ff: ref Fid; ff = nil; for (l = fids; l != nil; l = tl l) { f = hd l; if (f.fid == fid) { if (f.busy) return f; else { ff = f; break; } } else if (ff == nil && !f.busy) ff = f; } if (ff == nil) { ff = ref afid; fids = ff :: fids; } ff.node = nil; ff.fid = fid; return ff; } # # FTP protocol. # fail(s: int, l: string) { case s { Syserr => sys->print("read fail: %r\n"); Syntax => sys->print("%s\n", Eftpproto); Shutdown => sys->print("%s\n", Eshutdown); * => sys->print("unexpected response: %s\n", l); } exit; } getfullreply(echo: int) : (int, int, string) { buffmulti: string; s: string; done: int; done = 0; buffmulti = nil; while (!done) { s = dfidiob.gets(int '\n'); if (s == nil || len s < 2) return (Shutdown, 0, nil); # process string if (s[len s - 1] == '\n') { if (s[len s - 2] == '\r') s = s[0: len s - 2]; else s = s[0: len s - 1]; } if (debug || echo) sys->print("%s\n", s); if (len s < 4) return (Syntax, 0, nil); if (buffmulti != nil) { if (s[3] == ' ' && s[0 : 3] == buffmulti) { done = 1; break; } } else if (s[3] == '-') buffmulti = s[0:3]; else { done = 1; break; } } # only way to here is if the last line of a multiline, or a single line response # has been found c := int s[0:3]; if (c < 100) return (Syntax, 0, nil); return (c / 100, c, s); } getreply(echo: int) : (int, string) { (c, code, s) := getfullreply(echo); return (c, s); } sendrequest2(req: string, echo: int, figleaf: string) : int { activity = 1; if (debug || echo) { if (figleaf == nil) figleaf = req; sys->print("%s\n", figleaf); } b := array of byte (req + "\r\n"); n := sys->write(dfid, b, len b); if (n < 0) return Syserr; if (n != len b) return Shutdown; return Success; } sendrequest(req: string, echo: int) : int { return sendrequest2(req, echo, req); } sendfail(s: int) { case s { Syserr => sys->print("write fail: %r\n"); Shutdown => sys->print("%s\n", Eshutdown); * => sys->print("internal error\n"); } exit; } dataport(l: list of string) : string { s := "tcp!" + hd l; l = tl l; s = s + "." + hd l; l = tl l; s = s + "." + hd l; l = tl l; s = s + "." + hd l; l = tl l; return s + "!" + string ((int hd l * 256) + (int hd tl l)); } commas(l: list of string) : string { s := hd l; l = tl l; while (l != nil) { s = s + "," + hd l; l = tl l; } return s; } third(cmd: string) : ref FD { acquire(); for (;;) { (n, data) := sys->dial(firewall, nil); if (n < 0) { if (debug) sys->print("dial %s failed: %r\n", firewall); break; } t := sys->sprint("\n%s!*\n\n%s\n%s\n1\n-1\n-1\n", proxyhost, myhost, myname); b := array of byte t; n = sys->write(data.dfd, b, len b); if (n < 0) { if (debug) sys->print("firewall write failed: %r\n"); break; } b = array[256] of byte; n = sys->read(data.dfd, b, len b); if (n < 0) { if (debug) sys->print("firewall read failed: %r\n"); break; } (c, k) := sys->tokenize(string b[:n], "\n"); if (c < 2) { if (debug) sys->print("bad response from firewall\n"); break; } if (hd k != "0") { if (debug) sys->print("firewall connect: %s\n", hd tl k); break; } p := hd tl k; if (debug) sys->print("portid %s\n", p); (c, k) = sys->tokenize(p, "!"); if (c < 3) { if (debug) sys->print("bad portid from firewall\n"); break; } n = int hd tl tl k; (c, k) = sys->tokenize(hd tl k, "."); if (c != 4) { if (debug) sys->print("bad portid ip address\n"); break; } t = sys->sprint("PORT %s,%d,%d", commas(k), n / 256, n & 255); r := sendrequest(t, 0); if (r != Success) break; (r, nil) = getreply(0); if (r != Success) break; r = sendrequest(cmd, 0); if (r != Success) break; (r, nil) = getreply(0); if (r != Extra) break; n = sys->read(data.dfd, b, len b); if (n < 0) { if (debug) sys->print("firewall read failed: %r\n"); break; } b = array of byte "0\n?\n"; n = sys->write(data.dfd, b, len b); if (n < 0) { if (debug) sys->print("firewall write failed: %r\n"); break; } release(); return data.dfd; } release(); return nil; } passive(cmd: string) : ref FD { acquire(); if (sendrequest("PASV", 0) != Success) { release(); return nil; } (r, m) := getreply(0); release(); if (r != Success) return nil; (nil, p) := str->splitl(m, "("); if (p == nil) str->splitl(m, "0-9"); else p = p[1:len p]; (c, l) := sys->tokenize(p, ","); if (c < 6) { sys->print("data: %s\n", m); return nil; } a := dataport(l); if (debug) sys->print("data dial %s\n", a); (s, d) := sys->dial(a, nil); if (s < 0) return nil; acquire(); r = sendrequest(cmd, 0); if (r != Success) { release(); return nil; } (r, m) = getreply(0); release(); if (r != Extra) return nil; return d.dfd; } getnet(dir: string): (string, int) { buf := array[50] of byte; n := dir + "/local"; lfd := sys->open(n, Sys->OREAD); if (lfd == nil) { if (debug) sys->fprint(stderr, "open %s: %r\n", n); return (nil, 0); } length := sys->read(lfd, buf, len buf); if (length < 0) { if (debug) sys->fprint(stderr, "read%s: %r\n", n); return (nil, 0); } (r, l) := sys->tokenize(string buf[0:length], "!"); if (r != 2) { if (debug) sys->fprint(stderr, "tokenize(%s) returned (%d)\n", string buf[0:length], r); return (nil, 0); } if (debug) sys->print("net is %s!%d\n", hd l, int hd tl l); return (hd l, int hd tl l); } activate(cmd: string) : ref FD { r: int; listenport, dataport: Connection; m: string; (r, listenport) = sys->announce("tcp!" + net + "!0"); if (r < 0) return nil; (x1, x2) := getnet(listenport.dir); (x3, x4) := sys->tokenize(x1, "."); t := sys->sprint("PORT %s,%d,%d", commas(x4), int x2 / 256, int x2&255); acquire(); r = sendrequest(t, 0); if (r != Success) { release(); return nil; } (r, m) = getreply(0); if (r != Success) { release(); return nil; } r = sendrequest(cmd, 0); if (r != Success) { release(); return nil; } (r, m) = getreply(0); release(); if (r != Extra) return nil; (r, dataport) = sys->listen(listenport); if (r < 0) { sys->fprint(stderr, "activate: listen failed: %r\n"); return nil; } fd := sys->open(dataport.dir + "/data", sys->ORDWR); if (debug) sys->print("activate: data connection on %s\n", dataport.dir); if (fd == nil) { sys->fprint(stderr, "activate: open of %s failed: %r\n", dataport.dir); return nil; } return fd; } data(cmd: string) : ref FD { if (proxy) return third(cmd); else if (active) return activate(cmd); else return passive(cmd); } # # File list cracking routines. # shorten(name: string, off: int) : string { l := len name; if (l < Sys->NAMELEN) return name; return name[0:Sys->NAMELEN - off - 1] + "*" + name[l - off:l]; } Node.mkunique(parent: self ref Node, off: int) : int { n, p: ref Node; change := 0; for (n = parent.children; n != nil; n = n.sibs) { for (p = n.sibs; p != nil; p = p.sibs) { if (n.dir.name != p.dir.name) continue; p.dir.name = shorten(p.longname, off); change = 1; } } return change; } fields(l: list of string, n: int) : array of string { a := array[n] of string; for (i := 0; i < n; i++) { a[i] = hd l; l = tl l; } return a; } now: ref Tm; months: con "janfebmaraprmayjunjulaugsepoctnovdec"; cracktime(month, day, year, hms: string) : int { tm: Tm; if (now == nil) now = time->local(time->now()); tm = *now; if (month[0] >= '0' && month[0] <= '9') { tm.mon = int month - 1; if (tm.mon < 0 || tm.mon > 11) tm.mon = 5; } else if (len month >= 3) { month = str->tolower(month[0:3]); for (i := 0; i < 36; i += 3) if (month == months[i:i+3]) { tm.mon = i / 3; break; } } tm.mday = int day; if (hms != nil) { (h, z) := str->splitl(hms, "apAP"); (a, b) := str->splitl(h, ":"); tm.hour = int a; if (b != nil) { (c, d) := str->splitl(b[1:len b], ":"); tm.min = int c; if (d != nil) tm.sec = int d[1:len d]; } if (z != nil && str->tolower(z)[0] == 'p') tm.hour += 12; } if (year != nil) { tm.year = int year; if (tm.year >= 1900) tm.year -= 1900; } else { if (tm.mon > now.mon || (tm.mon == now.mon && tm.mday > now.mday+1)) tm.year--; } return time->tm2epoch(ref tm); } crackmode(p: string) : int { flags := 0; case len p { 10 => # unix and new style plan 9 case p[0] { 'l' => return CHSYML | 0777; 'd' => flags = Sys->CHDIR; } p = p[1:10]; 11 => # old style plan 9 if (p[0] == 'l') flags = Sys->CHDIR; p = p[2:11]; * => return Sys->CHDIR | 0777; } mode := 0; n := 0; for (i := 0; i < 3; i++) { mode <<= 3; if (p[n] == 'r') mode |= 4; if (p[n+1] == 'w') mode |= 2; case p[n+2] { 'x' or 's' or 'S' => mode |= 1; } n += 3; } return mode | flags; } crackdir(p: string) : (string, Dir) { d: Dir; ln, a: string; (n, l) := sys->tokenize(p, " \t\r\n"); f := fields(l, n); if (n > 2 && f[n - 2] == "->") n -= 2; case n { 8 => # ls -l ln = f[7]; d.uid = f[2]; d.gid = f[2]; d.mode = crackmode(f[0]); d.length = int f[3]; (a, nil) = str->splitl(f[6], ":"); if (len a != len f[6]) d.atime = cracktime(f[4], f[5], nil, f[6]); else d.atime = cracktime(f[4], f[5], f[6], nil); 9 => # ls -lg ln = f[8]; d.uid = f[2]; d.gid = f[3]; d.mode = crackmode(f[0]); d.length = int f[4]; (a, nil) = str->splitl(f[7], ":"); if (len a != len f[7]) d.atime = cracktime(f[5], f[6], nil, f[7]); else d.atime = cracktime(f[5], f[6], f[7], nil); 10 => # plan 9 ln = f[9]; d.uid = f[3]; d.gid = f[4]; d.mode = crackmode(f[0]); d.length = int f[5]; (a, nil) = str->splitl(f[8], ":"); if (len a != len f[8]) d.atime = cracktime(f[6], f[7], nil, f[8]); else d.atime = cracktime(f[6], f[7], f[8], nil); 4 => # NT ln = f[3]; d.uid = anon; d.gid = anon; if (f[2] == "