Unified · Split
| @@ -1,231 +0,0 @@ | ||
| 1 | −implement Mount; | |
| 2 | − | |
| 3 | −include "sys.m"; | |
| 4 | − sys: Sys; | |
| 5 | −include "draw.m"; | |
| 6 | −include "keyring.m"; | |
| 7 | −include "security.m"; | |
| 8 | −include "arg.m"; | |
| 9 | −include "sh.m"; | |
| 10 | −include "styxconv.m"; | |
| 11 | − styxconv: Styxconv; | |
| 12 | − | |
| 13 | −Mount: module | |
| 14 | −{ | |
| 15 | − init: fn(nil: ref Draw->Context, nil: list of string); | |
| 16 | −}; | |
| 17 | − | |
| 18 | −vflag := 0; | |
| 19 | −doauth := 1; | |
| 20 | − | |
| 21 | −usage() | |
| 22 | −{ | |
| 23 | − sys->fprint(sys->fildes(2), "Usage: mount [-a|-b] [-cA] [-C cryptoalg] [-f keyfile] net!addr|file|{command} mountpoint [spec]\n"); | |
| 24 | − raise "fail:usage"; | |
| 25 | −} | |
| 26 | − | |
| 27 | −fail(status, msg: string) | |
| 28 | −{ | |
| 29 | − sys->fprint(sys->fildes(2), "mount: %s\n", msg); | |
| 30 | − raise "fail:"+status; | |
| 31 | −} | |
| 32 | − | |
| 33 | −nomod(mod: string) | |
| 34 | −{ | |
| 35 | − fail("load", sys->sprint("can't load %s: %r", mod)); | |
| 36 | −} | |
| 37 | − | |
| 38 | −init(ctxt: ref Draw->Context, args: list of string) | |
| 39 | −{ | |
| 40 | − sys = load Sys Sys->PATH; | |
| 41 | − | |
| 42 | − vflag = 0; | |
| 43 | − unauth := 0; | |
| 44 | − alg := "none"; | |
| 45 | − keyfile: string; | |
| 46 | − spec: string; | |
| 47 | − | |
| 48 | − arg := load Arg Arg->PATH; | |
| 49 | − if(arg == nil) | |
| 50 | − nomod(Arg->PATH); | |
| 51 | − styxconv = load Styxconv Styxconv->PATH; | |
| 52 | − if(styxconv == nil) | |
| 53 | − nomod(Styxconv->PATH); | |
| 54 | − | |
| 55 | − arg->init(args); | |
| 56 | − styxconv->init(); | |
| 57 | − | |
| 58 | − flags := 0; | |
| 59 | − while((o := arg->opt()) != 0) | |
| 60 | − case o { | |
| 61 | − 'a' => | |
| 62 | − flags |= Sys->MAFTER; | |
| 63 | − 'b' => | |
| 64 | − flags |= Sys->MBEFORE; | |
| 65 | − 'c' => | |
| 66 | − flags |= Sys->MCREATE; | |
| 67 | − 'C' => | |
| 68 | − alg = arg->arg(); | |
| 69 | − if(alg == nil) | |
| 70 | − usage(); | |
| 71 | − 'f' => | |
| 72 | − keyfile = arg->arg(); | |
| 73 | − if(keyfile == nil) | |
| 74 | − usage(); | |
| 75 | − 'A' => | |
| 76 | − doauth = 0; | |
| 77 | − 'v' => | |
| 78 | − vflag = 1; | |
| 79 | − 'u' => | |
| 80 | − unauth = 1; # temporary, undocumented option for testing | |
| 81 | − * => | |
| 82 | − usage(); | |
| 83 | − } | |
| 84 | − args = arg->argv(); | |
| 85 | − arg = nil; | |
| 86 | − if(len args != 2){ | |
| 87 | − if(len args != 3) | |
| 88 | − usage(); | |
| 89 | − spec = hd tl tl args; | |
| 90 | − } | |
| 91 | − addr := hd args; | |
| 92 | − mountpoint := hd tl args; | |
| 93 | − | |
| 94 | − # open stream | |
| 95 | − fd := do_connect(ctxt, addr); | |
| 96 | − | |
| 97 | − # authenticate if necessary | |
| 98 | − if (doauth) | |
| 99 | − fd = do_auth(keyfile, alg, fd, addr, unauth); | |
| 100 | − | |
| 101 | − p := array[2] of ref Sys->FD; | |
| 102 | − if(sys->pipe(p) < 0) | |
| 103 | − fail("can't create pipe", sys->sprint("can't create pipe: %r")); | |
| 104 | − pidch := chan of int; | |
| 105 | − spawn styxconv->styxconv(p[1], fd, pidch); | |
| 106 | − p[1] = nil; | |
| 107 | − <- pidch; | |
| 108 | − ok := sys->mount(p[0], nil, mountpoint, flags, spec); | |
| 109 | − p[0] = nil; | |
| 110 | − if(ok < 0) | |
| 111 | − fail("mount failed", sys->sprint("mount failed: %r")); | |
| 112 | − | |
| 113 | −} | |
| 114 | − | |
| 115 | −# either make network connection or open file | |
| 116 | −do_connect(ctxt: ref Draw->Context, dest: string): ref Sys->FD | |
| 117 | −{ | |
| 118 | − if(dest != nil && dest[0] == '{' && dest[len dest - 1] == '}'){ | |
| 119 | − doauth = 0; | |
| 120 | − return popen(ctxt, dest :: nil); | |
| 121 | − } | |
| 122 | − (n, nil) := sys->tokenize(dest, "!"); | |
| 123 | − if(n == 1){ | |
| 124 | − fd := sys->open(dest, Sys->ORDWR); | |
| 125 | − if(fd != nil) | |
| 126 | − return fd; | |
| 127 | − if(dest[0] == '/') | |
| 128 | − fail("open failed", sys->sprint("can't open %s: %r", dest)); | |
| 129 | − } | |
| 130 | − (ok, c) := sys->dial(netmkaddr(dest, "net", "styx"), nil); | |
| 131 | − if(ok < 0) | |
| 132 | − fail("dial failed", sys->sprint("can't dial %s: %r", dest)); | |
| 133 | − return c.dfd; | |
| 134 | −} | |
| 135 | − | |
| 136 | −popen(ctxt: ref Draw->Context, argv: list of string): ref Sys->FD | |
| 137 | −{ | |
| 138 | − sh := load Sh Sh->PATH; | |
| 139 | − if(sh == nil) | |
| 140 | − nomod(Sh->PATH); | |
| 141 | − sync := chan of int; | |
| 142 | − fds := array[2] of ref Sys->FD; | |
| 143 | − sys->pipe(fds); | |
| 144 | − spawn runcmd(sh, ctxt, argv, fds[0], sync); | |
| 145 | − <-sync; | |
| 146 | − return fds[1]; | |
| 147 | −} | |
| 148 | − | |
| 149 | −runcmd(sh: Sh, ctxt: ref Draw->Context, argv: list of string, stdin: ref Sys->FD, | |
| 150 | − sync: chan of int) | |
| 151 | −{ | |
| 152 | − sys->pctl(Sys->FORKFD, nil); | |
| 153 | − sys->dup(stdin.fd, 0); | |
| 154 | − stdin = nil; | |
| 155 | − sync <-= 0; | |
| 156 | − sh->run(ctxt, argv); | |
| 157 | −} | |
| 158 | − | |
| 159 | −# authenticate if necessary | |
| 160 | −do_auth(keyfile, alg: string, dfd: ref Sys->FD, addr: string, unauth: int): ref Sys->FD | |
| 161 | −{ | |
| 162 | − cert : string; | |
| 163 | − | |
| 164 | − kr := load Keyring Keyring->PATH; | |
| 165 | − if(kr == nil) | |
| 166 | − nomod(Keyring->PATH); | |
| 167 | − | |
| 168 | − kd := "/usr/" + user() + "/keyring/"; | |
| 169 | − if (keyfile == nil) { | |
| 170 | − cert = kd + netmkaddr(addr, "tcp", ""); | |
| 171 | − (ok, nil) := sys->stat(cert); | |
| 172 | − if (ok < 0) | |
| 173 | − cert = kd + "default"; | |
| 174 | − } | |
| 175 | − else if (len keyfile > 0 && keyfile[0] != '/') | |
| 176 | − cert = kd + keyfile; | |
| 177 | − else | |
| 178 | − cert = keyfile; | |
| 179 | − ai := kr->readauthinfo(cert); | |
| 180 | − if (ai == nil){ | |
| 181 | − if(!unauth) | |
| 182 | − fail("readauthinfo failed", sys->sprint("cannot read %s: %r", cert)); | |
| 183 | − sys->fprint(sys->fildes(2), "mount: can't read %s (%r): trying mount as `nobody'\n", cert); | |
| 184 | − } | |
| 185 | − | |
| 186 | − au := load Auth Auth->PATH; | |
| 187 | − if(au == nil) | |
| 188 | − nomod(Auth->PATH); | |
| 189 | − | |
| 190 | − err := au->init(); | |
| 191 | − if(err != nil) | |
| 192 | − fail("auth init failed", sys->sprint("cannot init Auth: %s", err)); | |
| 193 | − | |
| 194 | − fd: ref Sys->FD; | |
| 195 | − (fd, err) = au->client(alg, ai, dfd); | |
| 196 | − if(fd == nil) | |
| 197 | − fail("auth failed", sys->sprint("authentication failed: %s", err)); | |
| 198 | − if(vflag) | |
| 199 | − sys->print("remote username is %s\n", err); | |
| 200 | − | |
| 201 | − return fd; | |
| 202 | −} | |
| 203 | − | |
| 204 | −user(): string | |
| 205 | −{ | |
| 206 | − fd := sys->open("/dev/user", sys->OREAD); | |
| 207 | − if(fd == nil) | |
| 208 | − return ""; | |
| 209 | − | |
| 210 | − buf := array[Sys->NAMEMAX] of byte; | |
| 211 | − n := sys->read(fd, buf, len buf); | |
| 212 | − if(n < 0) | |
| 213 | − return ""; | |
| 214 | − | |
| 215 | − return string buf[0:n]; | |
| 216 | −} | |
| 217 | − | |
| 218 | −netmkaddr(addr, net, svc: string): string | |
| 219 | −{ | |
| 220 | − if(net == nil) | |
| 221 | − net = "net"; | |
| 222 | − (n, nil) := sys->tokenize(addr, "!"); | |
| 223 | − if(n <= 1){ | |
| 224 | − if(svc== nil) | |
| 225 | − return sys->sprint("%s!%s", net, addr); | |
| 226 | − return sys->sprint("%s!%s!%s", net, addr, svc); | |
| 227 | − } | |
| 228 | − if(svc == nil || n > 2) | |
| 229 | − return addr; | |
| 230 | − return sys->sprint("%s!%s", addr, svc); | |
| 231 | −} | |
Binary file not shown.
Binary file not shown.