📄 exportfs.c
字号:
/* * exportfs - Export a plan 9 name space across a network */#include <u.h>#include <libc.h>#include <auth.h>#include <fcall.h>#include <libsec.h>#define Extern#include "exportfs.h"#define QIDPATH ((1LL<<48)-1)vlong newqid = 0;enum { Encnone, Encssl, Enctls,};void (*fcalls[])(Fsrpc*) ={ [Tversion] Xversion, [Tauth] Xauth, [Tflush] Xflush, [Tattach] Xattach, [Twalk] Xwalk, [Topen] slave, [Tcreate] Xcreate, [Tclunk] Xclunk, [Tread] slave, [Twrite] slave, [Tremove] Xremove, [Tstat] Xstat, [Twstat] Xwstat,};/* accounting and debugging counters */int filecnt;int freecnt;int qidcnt;int qfreecnt;int ncollision;int netfd;int srvfd = -1;int nonone = 1;char *filterp;char *ealgs = "rc4_256 sha1";char *aanfilter = "/bin/aan";int encproto = Encnone;int readonly;static void mksecret(char *, uchar *);static int localread9pmsg(int, void *, uint, ulong *);static char *anstring = "tcp!*!0";int filter(int, char *);voidusage(void){ fprint(2, "usage: %s [-adnsR] [-f dbgfile] [-m msize] [-r root] [-S srvfile] [-e 'crypt hash'] [-P exclusion-file] [-A announce-string] [-B address]\n", argv0); fatal("usage");}voidmain(int argc, char **argv){ char buf[ERRMAX], ebuf[ERRMAX], *srvfdfile; Fsrpc *r; int doauth, n, fd; char *dbfile, *srv, *na, *nsfile, *keyspec; AuthInfo *ai; ulong initial; dbfile = "/tmp/exportdb"; srv = nil; srvfd = -1; srvfdfile = nil; na = nil; nsfile = nil; keyspec = ""; doauth = 0; ai = nil; ARGBEGIN{ case 'a': doauth = 1; break; case 'd': dbg++; break; case 'e': ealgs = EARGF(usage()); if(*ealgs == 0 || strcmp(ealgs, "clear") == 0) ealgs = nil; break; case 'f': dbfile = EARGF(usage()); break; case 'k': keyspec = EARGF(usage()); break; case 'm': messagesize = strtoul(EARGF(usage()), nil, 0); break; case 'n': nonone = 0; break; case 'r': srv = EARGF(usage()); break; case 's': srv = "/"; break; case 'A': anstring = EARGF(usage()); break; case 'B': na = EARGF(usage()); break; case 'F': /* accepted but ignored, for backwards compatibility */ break; case 'N': nsfile = EARGF(usage()); break; case 'P': patternfile = EARGF(usage()); break; case 'R': readonly = 1; break; case 'S': if(srvfdfile) usage(); srvfdfile = EARGF(usage()); break; default: usage(); }ARGEND USED(argc, argv); if(doauth){ /* * We use p9any so we don't have to visit this code again, with the * cost that this code is incompatible with the old world, which * requires p9sk2. (The two differ in who talks first, so compatibility * is awkward.) */ ai = auth_proxy(0, auth_getkey, "proto=p9any role=server %s", keyspec); if(ai == nil) fatal("auth_proxy: %r"); if(nonone && strcmp(ai->cuid, "none") == 0) fatal("exportfs by none disallowed"); if(auth_chuid(ai, nsfile) < 0) fatal("auth_chuid: %r"); putenv("service", "exportfs"); } if(srvfdfile){ if((srvfd = open(srvfdfile, ORDWR)) < 0) sysfatal("open '%s': %r", srvfdfile); } if(na){ if(srv == nil) sysfatal("-B requires -s"); if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0) sysfatal("can't dial %s: %r", na); ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec); if(ai == nil) sysfatal("%r: %s", na); dup(fd, 0); dup(fd, 1); close(fd); } exclusions(); if(dbg) { n = create(dbfile, OWRITE|OTRUNC, 0666); dup(n, DFD); close(n); } if(srvfd >= 0 && srv){ fprint(2, "exportfs: -S cannot be used with -r or -s\n"); usage(); } DEBUG(DFD, "exportfs: started\n"); rfork(RFNOTEG); if(messagesize == 0){ messagesize = iounit(netfd); if(messagesize == 0) messagesize = 8192+IOHDRSZ; } Workq = emallocz(sizeof(Fsrpc)*Nr_workbufs);// for(i=0; i<Nr_workbufs; i++)// Workq[i].buf = emallocz(messagesize); fhash = emallocz(sizeof(Fid*)*FHASHSIZE); fmtinstall('F', fcallfmt); /* * Get tree to serve from network connection, * check we can get there and ack the connection */ if(srvfd != -1) { /* do nothing */ } else if(srv) { chdir(srv); DEBUG(DFD, "invoked as server for %s", srv); strncpy(buf, srv, sizeof buf); } else { buf[0] = 0; n = read(0, buf, sizeof(buf)-1); if(n < 0) { errstr(buf, sizeof buf); fprint(0, "read(0): %s", buf); DEBUG(DFD, "read(0): %s", buf); exits(buf); } buf[n] = 0; if(chdir(buf) < 0) { errstr(ebuf, sizeof ebuf); fprint(0, "chdir(%d:\"%s\"): %s", n, buf, ebuf); DEBUG(DFD, "chdir(%d:\"%s\"): %s", n, buf, ebuf); exits(ebuf); } } DEBUG(DFD, "\niniting root\n"); initroot(); DEBUG(DFD, "exportfs: %s\n", buf); if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2) fatal("open ack write"); if (readn(netfd, &initial, sizeof(ulong)) < sizeof(ulong)) fatal("can't read initial string: %r\n"); if (!strncmp((char *)&initial, "impo", sizeof(ulong))) { char buf[128], *p, *args[3]; // New import. Read import's parameters... initial = 0; p = buf; while (p - buf < sizeof buf) { if ((n = read(netfd, p, 1)) < 0) fatal("can't read impo arguments: %r\n"); if (n == 0) fatal("connection closed while reading arguments\n"); if (*p == '\n') *p = '\0'; if (*p++ == '\0') break; } if (tokenize(buf, args, nelem(args)) != 2) fatal("impo arguments invalid: impo%s...\n", buf); if (!strcmp(args[0], "aan")) filterp = aanfilter; else if (strcmp(args[0], "nofilter")) fatal("import filter argument unsupported: %s\n", args[0]); if (!strcmp(args[1], "ssl")) encproto = Encssl; else if (!strcmp(args[1], "tls")) encproto = Enctls; else if (strcmp(args[1], "clear")) fatal("import encryption proto unsupported: %s\n", args[1]); if (encproto == Enctls) sysfatal("%s: tls has not yet been implemented\n", argv[0]); } if (encproto != Encnone && ealgs && ai) { uchar key[16]; uchar digest[SHA1dlen]; char fromclientsecret[21]; char fromserversecret[21]; int i; memmove(key+4, ai->secret, ai->nsecret); /* exchange random numbers */ srand(truerand()); for(i = 0; i < 4; i++) key[i+12] = rand(); if (initial) fatal("Protocol botch: old import\n"); if(readn(netfd, key, 4) != 4) fatal("can't read key part; %r\n"); if(write(netfd, key+12, 4) != 4) fatal("can't write key part; %r\n"); /* scramble into two secrets */ sha1(key, sizeof(key), digest, nil); mksecret(fromclientsecret, digest); mksecret(fromserversecret, digest+10); if (filterp) netfd = filter(netfd, filterp); switch (encproto) { case Encssl: netfd = pushssl(netfd, ealgs, fromserversecret, fromclientsecret, nil); break; case Enctls: default: fatal("Unsupported encryption protocol\n"); } if(netfd < 0) fatal("can't establish ssl connection: %r"); } else if (filterp) { if (initial) fatal("Protocol botch: don't know how to deal with this\n"); netfd = filter(netfd, filterp); } /* * Start serving file requests from the network */ for(;;) { r = getsbuf(); if(r == 0) fatal("Out of service buffers"); n = localread9pmsg(netfd, r->buf, messagesize, &initial); if(n <= 0) fatal(nil); if(convM2S(r->buf, n, &r->work) == 0) fatal("convM2S format error"); DEBUG(DFD, "%F\n", &r->work); (fcalls[r->work.type])(r); }}// WARNING: Replace this with the original version as soon as all // _old_ imports have been replaced with negotiating imports. Also// cpu relies on this (which needs to be fixed!) -- pb.static intlocalread9pmsg(int fd, void *abuf, uint n, ulong *initial){ int m, len; uchar *buf; buf = abuf; /* read count */ assert(BIT32SZ == sizeof(ulong)); if (*initial) { memcpy(buf, initial, BIT32SZ); *initial = 0; } else { m = readn(fd, buf, BIT32SZ); if(m != BIT32SZ){ if(m < 0) return -1; return 0; } } len = GBIT32(buf); if(len <= BIT32SZ || len > n){ werrstr("bad length in 9P2000 message header"); return -1; } len -= BIT32SZ; m = readn(fd, buf+BIT32SZ, len); if(m < len) return 0; return BIT32SZ+m;}voidreply(Fcall *r, Fcall *t, char *err){ uchar *data; int n; t->tag = r->tag; t->fid = r->fid; if(err) { t->type = Rerror; t->ename = err; } else t->type = r->type + 1; DEBUG(DFD, "\t%F\n", t); data = malloc(messagesize); /* not mallocz; no need to clear */ if(data == nil) fatal(Enomem); n = convS2M(t, data, messagesize); if(write(netfd, data, n)!=n){syslog(0, "exportfs", "short write: %r"); fatal("mount write");} free(data);}Fid *getfid(int nr){ Fid *f; for(f = fidhash(nr); f; f = f->next) if(f->nr == nr) return f; return 0;}intfreefid(int nr){ Fid *f, **l;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -