123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635 |
- /* already in plan9.h #include <sys/types.h> *//* for struct passwd, struct group, struct stat ... */
- #include <sys/stat.h> /* for stat, umask */
- #include <stdlib.h> /* for malloc */
- #include <string.h> /* for strcpy, memmove */
- #include <pwd.h> /* for getpwnam, getpwuid */
- #include <grp.h> /* for getgrnam, getgrgid */
- #include <unistd.h> /* for gethostname, pread, pwrite, read, write */
- #include <utime.h> /* for utime */
- #include <dirent.h> /* for readdir */
- #include <errno.h> /* for errno */
- #include <stdio.h> /* for remove [sic] */
- #include <fcntl.h> /* for O_RDONLY, etc. */
- #include <sys/socket.h> /* various networking crud */
- #include <netinet/in.h>
- #include <netdb.h>
- #include <plan9.h>
- #include <fcall.h>
- #include <oldfcall.h>
- #include <u9fs.h>
- /* #ifndef because can be given in makefile */
- #ifndef DEFAULTLOG
- #define DEFAULTLOG "/tmp/u9fs.log"
- #endif
- char *logfile = DEFAULTLOG;
- #define S_ISSPECIAL(m) (S_ISCHR(m) || S_ISBLK(m) || S_ISFIFO(m))
- enum {
- Tdot = 1,
- Tdotdot
- };
- enum {
- P9P1,
- P9P2000
- };
- typedef struct User User;
- struct User {
- int id;
- gid_t defaultgid;
- char *name;
- char **mem; /* group members */
- int nmem;
- User *next;
- };
- typedef struct Fid Fid;
- struct Fid {
- int fid;
- char *path;
- struct stat st;
- User *u;
- int omode;
- DIR *dir;
- int diroffset;
- int fd;
- struct dirent *dirent;
- Fid *next;
- Fid *prev;
- };
- void* emalloc(size_t);
- void* erealloc(void*, size_t);
- char* estrdup(char*);
- char* estrpath(char*, char*);
- void sysfatal(char*, ...);
- int okuser(char*);
- void rversion(Fcall*, Fcall*);
- void rauth(Fcall*, Fcall*);
- void rattach(Fcall*, Fcall*);
- void rflush(Fcall*, Fcall*);
- void rclone(Fcall*, Fcall*);
- void rwalk(Fcall*, Fcall*);
- void ropen(Fcall*, Fcall*);
- void rcreate(Fcall*, Fcall*);
- void rread(Fcall*, Fcall*);
- void rwrite(Fcall*, Fcall*);
- void rclunk(Fcall*, Fcall*);
- void rstat(Fcall*, Fcall*);
- void rwstat(Fcall*, Fcall*);
- void rclwalk(Fcall*, Fcall*);
- void rremove(Fcall*, Fcall*);
- User* uname2user(char*);
- User* gname2user(char*);
- User* uid2user(int);
- User* gid2user(int);
- Fid* newfid(int, char**);
- Fid* oldfid(int, char**);
- int fidstat(Fid*, char**);
- void freefid(Fid*);
- int userchange(User*, char**);
- int userwalk(User*, char**, char*, Qid*, char**);
- int useropen(Fid*, int, char**);
- int usercreate(Fid*, char*, int, long, char**);
- int userremove(Fid*, char**);
- int userperm(User*, char*, int, int);
- int useringroup(User*, User*);
- Qid stat2qid(struct stat*);
- void getfcallold(int, Fcall*, int);
- void putfcallold(int, Fcall*);
- char Eauth[] = "authentication failed";
- char Ebadfid[] = "fid unknown or out of range";
- char Ebadoffset[] = "bad offset in directory read";
- char Ebadusefid[] = "bad use of fid";
- char Edirchange[] = "wstat can't convert between files and directories";
- char Eexist[] = "file or directory already exists";
- char Efidactive[] = "fid already in use";
- char Enotdir[] = "not a directory";
- char Enotingroup[] = "not a member of proposed group";
- char Enotowner[] = "only owner can change group in wstat";
- char Eperm[] = "permission denied";
- char Especial0[] = "already attached without access to special files";
- char Especial1[] = "already attached with access to special files";
- char Especial[] = "no access to special file";
- char Etoolarge[] = "i/o count too large";
- char Eunknowngroup[] = "unknown group";
- char Eunknownuser[] = "unknown user";
- char Ewstatbuffer[] = "bogus wstat buffer";
- ulong msize = IOHDRSZ+8192;
- uchar* rxbuf;
- uchar* txbuf;
- void* databuf;
- int connected;
- int devallowed;
- char* autharg;
- char* defaultuser;
- char hostname[256];
- char remotehostname[256];
- int chatty9p = 0;
- int network = 1;
- int old9p = -1;
- int authed;
- User* none;
- Auth *authmethods[] = { /* first is default */
- &authrhosts,
- &authnone,
- };
- Auth *auth;
- void
- getfcallnew(int fd, Fcall *fc, int have)
- {
- int len;
- if(have > BIT32SZ)
- sysfatal("cannot happen");
- if(have < BIT32SZ && readn(fd, rxbuf+have, BIT32SZ-have) != BIT32SZ-have)
- sysfatal("couldn't read message");
- len = GBIT32(rxbuf);
- if(len <= BIT32SZ)
- sysfatal("bogus message");
- len -= BIT32SZ;
- if(readn(fd, rxbuf+BIT32SZ, len) != len)
- sysfatal("short message");
- if(convM2S(rxbuf, len+BIT32SZ, fc) != len+BIT32SZ)
- sysfatal("badly sized message type %d", rxbuf[0]);
- }
- void
- getfcallold(int fd, Fcall *fc, int have)
- {
- int len, n;
- if(have > 3)
- sysfatal("cannot happen");
- if(have < 3 && readn(fd, rxbuf, 3-have) != 3-have)
- sysfatal("couldn't read message");
- len = oldhdrsize(rxbuf[0]);
- if(len < 3)
- sysfatal("bad message %d", rxbuf[0]);
- if(len > 3 && readn(fd, rxbuf+3, len-3) != len-3)
- sysfatal("couldn't read message");
- n = iosize(rxbuf);
- if(readn(fd, rxbuf+len, n) != n)
- sysfatal("couldn't read message");
- len += n;
- if(convM2Sold(rxbuf, len, fc) != len)
- sysfatal("badly sized message type %d", rxbuf[0]);
- }
- void
- putfcallnew(int wfd, Fcall *tx)
- {
- uint n;
- if((n = convS2M(tx, txbuf, msize)) == 0)
- sysfatal("couldn't format message type %d", tx->type);
- if(write(wfd, txbuf, n) != n)
- sysfatal("couldn't send message");
- }
- void
- putfcallold(int wfd, Fcall *tx)
- {
- uint n;
- if((n = convS2Mold(tx, txbuf, msize)) == 0)
- sysfatal("couldn't format message type %d", tx->type);
- if(write(wfd, txbuf, n) != n)
- sysfatal("couldn't send message");
- }
- void
- getfcall(int fd, Fcall *fc)
- {
- if(old9p == 1){
- getfcallold(fd, fc, 0);
- return;
- }
- if(old9p == 0){
- getfcallnew(fd, fc, 0);
- return;
- }
- /* auto-detect */
- if(readn(fd, rxbuf, 3) != 3)
- sysfatal("couldn't read message");
- /* is it an old (9P1) message? */
- if(50 <= rxbuf[0] && rxbuf[0] <= 87 && (rxbuf[0]&1)==0 && GBIT16(rxbuf+1) == 0xFFFF){
- old9p = 1;
- getfcallold(fd, fc, 3);
- return;
- }
- getfcallnew(fd, fc, 3);
- old9p = 0;
- }
- void
- seterror(Fcall *f, char *error)
- {
- f->type = Rerror;
- f->ename = error ? error : "programmer error";
- }
- int
- isowner(User *u, Fid *f)
- {
- return u->id == f->st.st_uid;
- }
- void
- serve(int rfd, int wfd)
- {
- Fcall rx, tx;
- for(;;){
- getfcall(rfd, &rx);
- if(chatty9p)
- fprint(2, "<- %F\n", &rx);
- memset(&tx, 0, sizeof tx);
- tx.type = rx.type+1;
- tx.tag = rx.tag;
- switch(rx.type){
- case Tflush:
- break;
- case Tversion:
- rversion(&rx, &tx);
- break;
- case Tauth:
- rauth(&rx, &tx);
- break;
- case Tattach:
- rattach(&rx, &tx);
- break;
- case Twalk:
- rwalk(&rx, &tx);
- break;
- case Tstat:
- tx.stat = databuf;
- rstat(&rx, &tx);
- break;
- case Twstat:
- rwstat(&rx, &tx);
- break;
- case Topen:
- ropen(&rx, &tx);
- break;
- case Tcreate:
- rcreate(&rx, &tx);
- break;
- case Tread:
- tx.data = databuf;
- rread(&rx, &tx);
- break;
- case Twrite:
- rwrite(&rx, &tx);
- break;
- case Tclunk:
- rclunk(&rx, &tx);
- break;
- case Tremove:
- rremove(&rx, &tx);
- break;
- default:
- fprint(2, "unknown message %F\n", &rx);
- seterror(&tx, "bad message");
- break;
- }
- if(chatty9p)
- fprint(2, "-> %F\n", &tx);
- (old9p ? putfcallold : putfcallnew)(wfd, &tx);
- }
- }
- void
- rversion(Fcall *rx, Fcall *tx)
- {
- if(msize > rx->msize)
- msize = rx->msize;
- tx->msize = msize;
- if(strncmp(rx->version, "9P", 2) != 0)
- tx->version = "unknown";
- else
- tx->version = "9P2000";
- }
- void
- rauth(Fcall *rx, Fcall *tx)
- {
- char *e;
- if((e = auth->auth(rx, tx)) != nil)
- seterror(tx, e);
- }
- void
- rattach(Fcall *rx, Fcall *tx)
- {
- char *e;
- Fid *fid;
- User *u;
- if(rx->aname == nil)
- rx->aname = "";
- if(strcmp(rx->aname, "device") == 0){
- if(connected && !devallowed){
- seterror(tx, Especial0);
- return;
- }
- devallowed = 1;
- }else{
- if(connected && devallowed){
- seterror(tx, Especial1);
- return;
- }
- }
- if(strcmp(rx->uname, "none") == 0){
- if(authed == 0){
- seterror(tx, Eauth);
- return;
- }
- } else {
- if((e = auth->attach(rx, tx)) != nil){
- seterror(tx, e);
- return;
- }
- authed++;
- }
- if((fid = newfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- fid->path = estrdup("/");
- if(fidstat(fid, &e) < 0){
- seterror(tx, e);
- freefid(fid);
- return;
- }
- if(defaultuser)
- rx->uname = defaultuser;
- if((u = uname2user(rx->uname)) == nil || u->id == 0){
- /* we don't know anyone named root... */
- seterror(tx, Eunknownuser);
- freefid(fid);
- return;
- }
- fid->u = u;
- tx->qid = stat2qid(&fid->st);
- return;
- }
- void
- rwalk(Fcall *rx, Fcall *tx)
- {
- int i;
- char *path, *e;
- Fid *fid, *nfid;
- e = nil;
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(fid->omode != -1){
- seterror(tx, Ebadusefid);
- return;
- }
- if(fidstat(fid, &e) < 0){
- seterror(tx, e);
- return;
- }
- if(!S_ISDIR(fid->st.st_mode) && rx->nwname){
- seterror(tx, Enotdir);
- return;
- }
- nfid = nil;
- if(rx->newfid != rx->fid && (nfid = newfid(rx->newfid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- path = estrdup(fid->path);
- e = nil;
- for(i=0; i<rx->nwname; i++)
- if(userwalk(fid->u, &path, rx->wname[i], &tx->wqid[i], &e) < 0)
- break;
- if(i == rx->nwname){ /* successful clone or walk */
- tx->nwqid = i;
- if(nfid){
- nfid->path = path;
- nfid->u = fid->u;
- }else{
- free(fid->path);
- fid->path = path;
- }
- }else{
- if(i > 0) /* partial walk? */
- tx->nwqid = i;
- else
- seterror(tx, e);
- if(nfid) /* clone implicit new fid */
- freefid(nfid);
- free(path);
- }
- return;
- }
- void
- ropen(Fcall *rx, Fcall *tx)
- {
- char *e;
- Fid *fid;
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(fid->omode != -1){
- seterror(tx, Ebadusefid);
- return;
- }
- if(fidstat(fid, &e) < 0){
- seterror(tx, e);
- return;
- }
- if(!devallowed && S_ISSPECIAL(fid->st.st_mode)){
- seterror(tx, Especial);
- return;
- }
- if(useropen(fid, rx->mode, &e) < 0){
- seterror(tx, e);
- return;
- }
- tx->iounit = 0;
- tx->qid = stat2qid(&fid->st);
- }
- void
- rcreate(Fcall *rx, Fcall *tx)
- {
- char *e;
- Fid *fid;
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(fid->omode != -1){
- seterror(tx, Ebadusefid);
- return;
- }
- if(fidstat(fid, &e) < 0){
- seterror(tx, e);
- return;
- }
- if(!S_ISDIR(fid->st.st_mode)){
- seterror(tx, Enotdir);
- return;
- }
- if(usercreate(fid, rx->name, rx->mode, rx->perm, &e) < 0){
- seterror(tx, e);
- return;
- }
- if(fidstat(fid, &e) < 0){
- seterror(tx, e);
- return;
- }
- tx->iounit = 0;
- tx->qid = stat2qid(&fid->st);
- }
- uchar
- modebyte(struct stat *st)
- {
- uchar b;
- b = 0;
- if(S_ISDIR(st->st_mode))
- b |= QTDIR;
- /* no way to test append-only */
- /* no real way to test exclusive use, but mark devices as such */
- if(S_ISSPECIAL(st->st_mode))
- b |= QTEXCL;
- return b;
- }
- ulong
- plan9mode(struct stat *st)
- {
- return ((ulong)modebyte(st)<<24) | (st->st_mode & 0777);
- }
- /*
- * this is for chmod, so don't worry about S_IFDIR
- */
- mode_t
- unixmode(Dir *d)
- {
- return (mode_t)(d->mode&0777);
- }
- Qid
- stat2qid(struct stat *st)
- {
- uchar *p, *ep, *q;
- Qid qid;
- /*
- * For now, ignore the device number.
- */
- qid.path = 0;
- p = (uchar*)&qid.path;
- ep = p+sizeof(qid.path);
- q = p+sizeof(ino_t);
- if(q > ep){
- fprint(2, "warning: inode number too big\n");
- q = ep;
- }
- memmove(p, &st->st_ino, q-p);
- q = q+sizeof(dev_t);
- if(q > ep){
- /* fprint(2, "warning: inode number + device number too big %d+%d\n", sizeof(ino_t), sizeof(dev_t)); */
- q = ep - sizeof(dev_t);
- if(q < p)
- fprint(2, "warning: device number too big by itself\n");
- else
- *(dev_t*)q ^= st->st_dev;
- }
- qid.vers = st->st_mtime ^ (st->st_size << 8);
- qid.type = modebyte(st);
- return qid;
- }
- void
- stat2dir(char *path, struct stat *st, Dir *d)
- {
- User *u;
- char *q;
- memset(d, 0, sizeof(*d));
- d->qid = stat2qid(st);
- d->mode = plan9mode(st);
- d->atime = st->st_atime;
- d->mtime = st->st_mtime;
- d->length = st->st_size;
- d->uid = (u = uid2user(st->st_uid)) ? u->name : "???";
- d->gid = (u = gid2user(st->st_gid)) ? u->name : "???";
- d->muid = "";
- if((q = strrchr(path, '/')) != nil)
- d->name = q+1;
- else
- d->name = path;
- }
- void
- rread(Fcall *rx, Fcall *tx)
- {
- char *e, *path;
- uchar *p, *ep;
- int n;
- Fid *fid;
- Dir d;
- struct stat st;
- if(rx->count > msize-IOHDRSZ){
- seterror(tx, Etoolarge);
- return;
- }
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(fid->omode == -1 || (fid->omode&3) == OWRITE){
- seterror(tx, Ebadusefid);
- return;
- }
- if(fid->dir){
- if(rx->offset != fid->diroffset){
- if(rx->offset != 0){
- seterror(tx, Ebadoffset);
- return;
- }
- rewinddir(fid->dir);
- fid->diroffset = 0;
- }
- p = (uchar*)tx->data;
- ep = (uchar*)tx->data+rx->count;
- for(;;){
- if(p+BIT16SZ >= ep)
- break;
- if(fid->dirent == nil) /* one entry cache for when convD2M fails */
- if((fid->dirent = readdir(fid->dir)) == nil)
- break;
- if(strcmp(fid->dirent->d_name, ".") == 0
- || strcmp(fid->dirent->d_name, "..") == 0){
- fid->dirent = nil;
- continue;
- }
- path = estrpath(fid->path, fid->dirent->d_name);
- memset(&st, 0, sizeof st);
- if(stat(path, &st) < 0){
- fprint(2, "dirread: stat(%s) failed: %s\n", path, strerror(errno));
- fid->dirent = nil;
- free(path);
- continue;
- }
- free(path);
- stat2dir(fid->dirent->d_name, &st, &d);
- if((n=(old9p ? convD2Mold : convD2M)(&d, p, ep-p)) <= BIT16SZ)
- break;
- p += n;
- fid->dirent = nil;
- }
- tx->count = p - (uchar*)tx->data;
- fid->diroffset += tx->count;
- }else{
- if((n = pread(fid->fd, tx->data, rx->count, rx->offset)) < 0){
- seterror(tx, strerror(errno));
- return;
- }
- tx->count = n;
- }
- }
- void
- rwrite(Fcall *rx, Fcall *tx)
- {
- char *e;
- Fid *fid;
- int n;
- if(rx->count > msize-IOHDRSZ){
- seterror(tx, Etoolarge);
- return;
- }
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(fid->omode == -1 || (fid->omode&3) == OREAD || (fid->omode&3) == OEXEC){
- seterror(tx, Ebadusefid);
- return;
- }
- if((n = pwrite(fid->fd, rx->data, rx->count, rx->offset)) < 0){
- seterror(tx, strerror(errno));
- return;
- }
- tx->count = n;
- }
- void
- rclunk(Fcall *rx, Fcall *tx)
- {
- char *e;
- Fid *fid;
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(fid->omode != -1 && fid->omode&ORCLOSE)
- remove(fid->path);
- freefid(fid);
- }
- void
- rremove(Fcall *rx, Fcall *tx)
- {
- char *e;
- Fid *fid;
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(userremove(fid, &e) < 0)
- seterror(tx, e);
- freefid(fid);
- }
- void
- rstat(Fcall *rx, Fcall *tx)
- {
- char *e;
- Fid *fid;
- Dir d;
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- if(fidstat(fid, &e) < 0){
- seterror(tx, e);
- return;
- }
- stat2dir(fid->path, &fid->st, &d);
- if((tx->nstat=(old9p ? convD2Mold : convD2M)(&d, tx->stat, msize)) <= BIT16SZ)
- seterror(tx, "convD2M fails");
- }
- void
- rwstat(Fcall *rx, Fcall *tx)
- {
- char *e;
- char *p, *old, *new, *dir;
- gid_t gid;
- Dir d;
- Fid *fid;
- if((fid = oldfid(rx->fid, &e)) == nil){
- seterror(tx, e);
- return;
- }
- /*
- * wstat is supposed to be atomic.
- * we check all the things we can before trying anything.
- * still, if we are told to truncate a file and rename it and only
- * one works, we're screwed. in such cases we leave things
- * half broken and return an error. it's hardly perfect.
- */
- if((old9p ? convM2Dold : convM2D)(rx->stat, rx->nstat, &d, (char*)rx->stat) <= BIT16SZ){
- seterror(tx, Ewstatbuffer);
- return;
- }
- if(fidstat(fid, &e) < 0){
- seterror(tx, e);
- return;
- }
- /*
- * The casting is necessary because d.mode is ulong and might,
- * on some systems, be 64 bits. We only want to compare the
- * bottom 32 bits, since that's all that gets sent in the protocol.
- *
- * Same situation for d.mtime and d.length (although that last check
- * is admittedly superfluous, given the current lack of 128-bit machines).
- */
- gid = (gid_t)-1;
- if(d.gid[0] != '\0'){
- User *g;
- g = gname2user(d.gid);
- if(g == nil){
- seterror(tx, Eunknowngroup);
- return;
- }
- gid = (gid_t)g->id;
- if(groupchange(fid->u, gid2user(gid), &e) < 0){
- seterror(tx, e);
- return;
- }
- }
- if((u32int)d.mode != (u32int)~0 && (((d.mode&DMDIR)!=0) ^ (S_ISDIR(fid->st.st_mode)!=0))){
- seterror(tx, Edirchange);
- return;
- }
- if(strcmp(fid->path, "/") == 0){
- seterror(tx, "no wstat of root");
- return;
- }
- /*
- * try things in increasing order of harm to the file.
- * mtime should come after truncate so that if you
- * do both the mtime actually takes effect, but i'd rather
- * leave truncate until last.
- * (see above comment about atomicity).
- */
- if((u32int)d.mode != (u32int)~0 && chmod(fid->path, unixmode(&d)) < 0){
- if(chatty9p)
- fprint(2, "chmod(%s, 0%luo) failed\n", fid->path, unixmode(&d));
- seterror(tx, strerror(errno));
- return;
- }
- if((u32int)d.mtime != (u32int)~0){
- struct utimbuf t;
- t.actime = 0;
- t.modtime = d.mtime;
- if(utime(fid->path, &t) < 0){
- if(chatty9p)
- fprint(2, "utime(%s) failed\n", fid->path);
- seterror(tx, strerror(errno));
- return;
- }
- }
- if(gid != (gid_t)-1 && gid != fid->st.st_gid){
- if(chown(fid->path, (uid_t)-1, gid) < 0){
- if(chatty9p)
- fprint(2, "chgrp(%s, %d) failed\n", fid->path, gid);
- seterror(tx, strerror(errno));
- return;
- }
- }
- if(d.name[0]){
- old = fid->path;
- dir = estrdup(fid->path);
- if((p = strrchr(dir, '/')) > dir)
- *p = '\0';
- else{
- seterror(tx, "whoops: can't happen in u9fs");
- return;
- }
-
- new = estrpath(dir, d.name);
- if(strcmp(old, new) != 0 && rename(old, new) < 0){
- if(chatty9p)
- fprint(2, "rename(%s, %s) failed\n", old, new);
- seterror(tx, strerror(errno));
- free(new);
- free(dir);
- return;
- }
- fid->path = new;
- free(old);
- free(dir);
- }
- if((u64int)d.length != (u64int)~0 && truncate(fid->path, d.length) < 0){
- fprint(2, "truncate(%s, %lld) failed\n", fid->path, d.length);
- seterror(tx, strerror(errno));
- return;
- }
- }
- /*
- * we keep a table by numeric id. by name lookups happen infrequently
- * while by-number lookups happen once for every directory entry read
- * and every stat request.
- */
- User *utab[64];
- User *gtab[64];
- User*
- adduser(struct passwd *p)
- {
- User *u;
- u = emalloc(sizeof(*u));
- u->id = p->pw_uid;
- u->name = estrdup(p->pw_name);
- u->next = utab[p->pw_uid%nelem(utab)];
- u->defaultgid = p->pw_gid;
- utab[p->pw_uid%nelem(utab)] = u;
- return u;
- }
- int
- useringroup(User *u, User *g)
- {
- int i;
- for(i=0; i<g->nmem; i++)
- if(strcmp(g->mem[i], u->name) == 0)
- return 1;
- /*
- * Hack around common Unix problem that everyone has
- * default group "user" but /etc/group lists no members.
- */
- if(u->defaultgid == g->id)
- return 1;
- return 0;
- }
- User*
- addgroup(struct group *g)
- {
- User *u;
- char **p;
- int n;
- u = emalloc(sizeof(*u));
- n = 0;
- for(p=g->gr_mem; *p; p++)
- n++;
- u->mem = emalloc(sizeof(u->mem[0])*n);
- n = 0;
- for(p=g->gr_mem; *p; p++)
- u->mem[n++] = estrdup(*p);
- u->nmem = n;
- u->id = g->gr_gid;
- u->name = estrdup(g->gr_name);
- u->next = gtab[g->gr_gid%nelem(gtab)];
- gtab[g->gr_gid%nelem(gtab)] = u;
- return u;
- }
- User*
- uname2user(char *name)
- {
- int i;
- User *u;
- struct passwd *p;
- for(i=0; i<nelem(utab); i++)
- for(u=utab[i]; u; u=u->next)
- if(strcmp(u->name, name) == 0)
- return u;
- if((p = getpwnam(name)) == nil)
- return nil;
- return adduser(p);
- }
- User*
- uid2user(int id)
- {
- User *u;
- struct passwd *p;
- for(u=utab[id%nelem(utab)]; u; u=u->next)
- if(u->id == id)
- return u;
- if((p = getpwuid(id)) == nil)
- return nil;
- return adduser(p);
- }
- User*
- gname2user(char *name)
- {
- int i;
- User *u;
- struct group *g;
- for(i=0; i<nelem(gtab); i++)
- for(u=gtab[i]; u; u=u->next)
- if(strcmp(u->name, name) == 0)
- return u;
- if((g = getgrnam(name)) == nil)
- return nil;
- return addgroup(g);
- }
- User*
- gid2user(int id)
- {
- User *u;
- struct group *g;
- for(u=gtab[id%nelem(gtab)]; u; u=u->next)
- if(u->id == id)
- return u;
- if((g = getgrgid(id)) == nil)
- return nil;
- return addgroup(g);
- }
- void
- sysfatal(char *fmt, ...)
- {
- char buf[1024];
- va_list va;
- va_start(va, fmt);
- doprint(buf, buf+sizeof buf, fmt, va);
- va_end(va);
- fprint(2, "u9fs: %s\n", buf);
- fprint(2, "last unix error: %s\n", strerror(errno));
- exit(1);
- }
- void*
- emalloc(size_t n)
- {
- void *p;
- if(n == 0)
- n = 1;
- p = malloc(n);
- if(p == 0)
- sysfatal("malloc(%ld) fails", (long)n);
- memset(p, 0, n);
- return p;
- }
- void*
- erealloc(void *p, size_t n)
- {
- if(p == 0)
- p = malloc(n);
- else
- p = realloc(p, n);
- if(p == 0)
- sysfatal("realloc(..., %ld) fails", (long)n);
- return p;
- }
- char*
- estrdup(char *p)
- {
- p = strdup(p);
- if(p == 0)
- sysfatal("strdup(%.20s) fails", p);
- return p;
- }
- char*
- estrpath(char *p, char *q)
- {
- char *r, *s;
- if(strcmp(q, "..") == 0){
- r = estrdup(p);
- if((s = strrchr(r, '/')) && s > r)
- *s = '\0';
- else if(s == r)
- s[1] = '\0';
- return r;
- }
- r = emalloc(strlen(p)+1+strlen(q)+1);
- strcpy(r, p);
- if(r[0]=='\0' || r[strlen(r)-1] != '/')
- strcat(r, "/");
- strcat(r, q);
- return r;
- }
- Fid *newfid(int, char**);
- Fid *oldfid(int, char**);
- int fidstat(Fid*, char**);
- void freefid(Fid*);
- Fid *fidtab[1];
- Fid*
- lookupfid(int fid)
- {
- Fid *f;
- for(f=fidtab[fid%nelem(fidtab)]; f; f=f->next)
- if(f->fid == fid)
- return f;
- return nil;
- }
- Fid*
- newfid(int fid, char **ep)
- {
- Fid *f;
- if(lookupfid(fid) != nil){
- *ep = Efidactive;
- return nil;
- }
- f = emalloc(sizeof(*f));
- f->next = fidtab[fid%nelem(fidtab)];
- if(f->next)
- f->next->prev = f;
- fidtab[fid%nelem(fidtab)] = f;
- f->fid = fid;
- f->fd = -1;
- f->omode = -1;
- return f;
- }
- Fid*
- oldfid(int fid, char **ep)
- {
- Fid *f;
- if((f = lookupfid(fid)) == nil){
- *ep = Ebadfid;
- return nil;
- }
- if(userchange(f->u, ep) < 0)
- return nil;
- return f;
- }
- void
- freefid(Fid *f)
- {
- if(f->prev)
- f->prev->next = f->next;
- else
- fidtab[f->fid%nelem(fidtab)] = f->next;
- if(f->next)
- f->next->prev = f->prev;
- if(f->dir)
- closedir(f->dir);
- if(f->fd)
- close(f->fd);
- free(f->path);
- free(f);
- }
- int
- fidstat(Fid *fid, char **ep)
- {
- if(stat(fid->path, &fid->st) < 0){
- fprint(2, "fidstat(%s) failed\n", fid->path);
- if(ep)
- *ep = strerror(errno);
- return -1;
- }
- if(S_ISDIR(fid->st.st_mode))
- fid->st.st_size = 0;
- return 0;
- }
- int
- userchange(User *u, char **ep)
- {
- if(defaultuser)
- return 0;
- if(setreuid(0, 0) < 0){
- fprint(2, "setreuid(0, 0) failed\n");
- *ep = "cannot setuid back to root";
- return -1;
- }
- /*
- * Initgroups does not appear to be SUSV standard.
- * But it exists on SGI and on Linux, which makes me
- * think it's standard enough. We have to do something
- * like this, and the closest other function I can find is
- * setgroups (which initgroups eventually calls).
- * Setgroups is the same as far as standardization though,
- * so we're stuck using a non-SUSV call. Sigh.
- */
- if(initgroups(u->name, u->defaultgid) < 0)
- fprint(2, "initgroups(%s) failed: %s\n", u->name, strerror(errno));
- if(setreuid(-1, u->id) < 0){
- fprint(2, "setreuid(-1, %s) failed\n", u->name);
- *ep = strerror(errno);
- return -1;
- }
- return 0;
- }
- /*
- * We do our own checking here, then switch to root temporarily
- * to set our gid. In a perfect world, you'd be allowed to set your
- * egid to any of the supplemental groups of your euid, but this
- * is not the case on Linux 2.2.14 (and perhaps others).
- *
- * This is a race, of course, but it's a race against processes
- * that can edit the group lists. If you can do that, you can
- * change your own group without our help.
- */
- int
- groupchange(User *u, User *g, char **ep)
- {
- if(!useringroup(u, g)){
- if(chatty9p)
- fprint(2, "%s not in group %s\n", u->name, g->name);
- *ep = Enotingroup;
- return -1;
- }
- setreuid(0,0);
- if(setregid(-1, g->id) < 0){
- fprint(2, "setegid(%s/%d) failed in groupchange\n", g->name, g->id);
- *ep = strerror(errno);
- return -1;
- }
- if(userchange(u, ep) < 0)
- return -1;
- return 0;
- }
- /*
- * An attempt to enforce permissions by looking at the
- * file system. Separation of checking permission and
- * actually performing the action is a terrible idea, of
- * course, so we use setreuid for most of the permission
- * enforcement. This is here only so we can give errors
- * on open(ORCLOSE) in some cases.
- */
- int
- userperm(User *u, char *path, int type, int need)
- {
- char *p, *q;
- int i, have;
- struct stat st;
- User *g;
- switch(type){
- default:
- fprint(2, "bad type %d in userperm\n", type);
- return -1;
- case Tdot:
- if(stat(path, &st) < 0){
- fprint(2, "userperm: stat(%s) failed\n", path);
- return -1;
- }
- break;
- case Tdotdot:
- p = estrdup(path);
- if((q = strrchr(p, '/'))==nil){
- fprint(2, "userperm(%s, ..): bad path\n", p);
- free(p);
- return -1;
- }
- if(q > p)
- *q = '\0';
- else
- *(q+1) = '\0';
- if(stat(p, &st) < 0){
- fprint(2, "userperm: stat(%s) (dotdot of %s) failed\n",
- p, path);
- free(p);
- return -1;
- }
- free(p);
- break;
- }
- if(u == none){
- fprint(2, "userperm: none wants %d in 0%luo\n", need, st.st_mode);
- have = st.st_mode&7;
- if((have&need)==need)
- return 0;
- return -1;
- }
- have = st.st_mode&7;
- if((uid_t)u->id == st.st_uid)
- have |= (st.st_mode>>6)&7;
- if((have&need)==need)
- return 0;
- if(((have|((st.st_mode>>3)&7))&need) != need) /* group won't help */
- return -1;
- g = gid2user(st.st_gid);
- for(i=0; i<g->nmem; i++){
- if(strcmp(g->mem[i], u->name) == 0){
- have |= (st.st_mode>>3)&7;
- break;
- }
- }
- if((have&need)==need)
- return 0;
- return -1;
- }
- int
- userwalk(User *u, char **path, char *elem, Qid *qid, char **ep)
- {
- char *npath;
- struct stat st;
- npath = estrpath(*path, elem);
- if(stat(npath, &st) < 0){
- free(npath);
- *ep = strerror(errno);
- return -1;
- }
- *qid = stat2qid(&st);
- free(*path);
- *path = npath;
- return 0;
- }
- int
- useropen(Fid *fid, int omode, char **ep)
- {
- int a, o;
- /*
- * Check this anyway, to try to head off problems later.
- */
- if((omode&ORCLOSE) && userperm(fid->u, fid->path, Tdotdot, W_OK) < 0){
- *ep = Eperm;
- return -1;
- }
- switch(omode&3){
- default:
- *ep = "programmer error";
- return -1;
- case OREAD:
- a = R_OK;
- o = O_RDONLY;
- break;
- case ORDWR:
- a = R_OK|W_OK;
- o = O_RDWR;
- break;
- case OWRITE:
- a = R_OK;
- o = O_WRONLY;
- break;
- case OEXEC:
- a = X_OK;
- o = O_RDONLY;
- break;
- }
- if(omode & OTRUNC){
- a |= W_OK;
- o |= O_TRUNC;
- }
- if(S_ISDIR(fid->st.st_mode)){
- if(a != R_OK){
- fprint(2, "attempt by %s to open dir %d\n", fid->u->name, omode);
- *ep = Eperm;
- return -1;
- }
- if((fid->dir = opendir(fid->path)) == nil){
- *ep = strerror(errno);
- return -1;
- }
- }else{
- /*
- * This is wrong because access used the real uid
- * and not the effective uid. Let the open sort it out.
- *
- if(access(fid->path, a) < 0){
- *ep = strerror(errno);
- return -1;
- }
- *
- */
- if((fid->fd = open(fid->path, o)) < 0){
- *ep = strerror(errno);
- return -1;
- }
- }
- fid->omode = omode;
- return 0;
- }
- int
- usercreate(Fid *fid, char *elem, int omode, long perm, char **ep)
- {
- int o, m;
- char *opath, *npath;
- struct stat st, parent;
- if(stat(fid->path, &parent) < 0){
- *ep = strerror(errno);
- return -1;
- }
- /*
- * Change group so that created file has expected group
- * by Plan 9 semantics. If that fails, might as well go
- * with the user's default group.
- */
- if(groupchange(fid->u, gid2user(parent.st_gid), ep) < 0
- && groupchange(fid->u, gid2user(fid->u->defaultgid), ep) < 0)
- return -1;
- m = (perm & DMDIR) ? 0777 : 0666;
- perm = perm & (~m | (fid->st.st_mode & m));
- npath = estrpath(fid->path, elem);
- if(perm & DMDIR){
- if((omode&~ORCLOSE) != OREAD){
- *ep = Eperm;
- free(npath);
- return -1;
- }
- if(stat(npath, &st) >= 0 || errno != ENOENT){
- *ep = Eexist;
- free(npath);
- return -1;
- }
- /* race */
- if(mkdir(npath, perm&0777) < 0){
- *ep = strerror(errno);
- free(npath);
- return -1;
- }
- if((fid->dir = opendir(npath)) == nil){
- *ep = strerror(errno);
- remove(npath); /* race */
- free(npath);
- return -1;
- }
- }else{
- o = O_CREAT|O_EXCL;
- switch(omode&3){
- default:
- *ep = "programmer error";
- return -1;
- case OREAD:
- case OEXEC:
- o |= O_RDONLY;
- break;
- case ORDWR:
- o |= O_RDWR;
- break;
- case OWRITE:
- o |= O_WRONLY;
- break;
- }
- if(omode & OTRUNC)
- o |= O_TRUNC;
- if((fid->fd = open(npath, o, perm&0777)) < 0){
- if(chatty9p)
- fprint(2, "create(%s, 0x%x, 0%o) failed\n", npath, o, perm&0777);
- *ep = strerror(errno);
- free(npath);
- return -1;
- }
- }
- opath = fid->path;
- fid->path = npath;
- if(fidstat(fid, ep) < 0){
- fprint(2, "stat after create on %s failed\n", npath);
- remove(npath); /* race */
- free(npath);
- fid->path = opath;
- if(fid->fd >= 0){
- close(fid->fd);
- fid->fd = -1;
- }else{
- closedir(fid->dir);
- fid->dir = nil;
- }
- return -1;
- }
- fid->omode = omode;
- free(opath);
- return 0;
- }
- int
- userremove(Fid *fid, char **ep)
- {
- if(remove(fid->path) < 0){
- *ep = strerror(errno);
- return -1;
- }
- return 0;
- }
- void
- usage(void)
- {
- fprint(2, "usage: u9fs [-Dnz] [-a authmethod] [-m msize] [-u user] [root]\n");
- exit(1);
- }
- int
- main(int argc, char **argv)
- {
- char *authtype;
- int i;
- int fd;
- int logflag;
- auth = authmethods[0];
- logflag = O_WRONLY|O_APPEND|O_CREAT;
- ARGBEGIN{
- case 'D':
- chatty9p = 1;
- break;
- case 'a':
- authtype = EARGF(usage());
- auth = nil;
- for(i=0; i<nelem(authmethods); i++)
- if(strcmp(authmethods[i]->name, authtype)==0)
- auth = authmethods[i];
- if(auth == nil)
- sysfatal("unknown auth type '%s'", authtype);
- break;
- case 'A':
- autharg = EARGF(usage());
- break;
- case 'l':
- logfile = EARGF(usage());
- break;
- case 'm':
- msize = strtol(EARGF(usage()), 0, 0);
- break;
- case 'n':
- network = 0;
- break;
- case 'u':
- defaultuser = EARGF(usage());
- break;
- case 'z':
- logflag |= O_TRUNC;
- }ARGEND
- if(argc > 1)
- usage();
- fd = open(logfile, logflag, 0666);
- if(fd < 0)
- sysfatal("cannot open log '%s'", logfile);
- if(dup2(fd, 2) < 0)
- sysfatal("cannot dup fd onto stderr");
- fprint(2, "u9fs\nkill %d\n", (int)getpid());
- fmtinstall('F', fcallconv);
- fmtinstall('D', dirconv);
- fmtinstall('M', dirmodeconv);
- rxbuf = emalloc(msize);
- txbuf = emalloc(msize);
- databuf = emalloc(msize);
- if(auth->init)
- auth->init();
- if(network)
- getremotehostname(remotehostname, sizeof remotehostname);
- if(gethostname(hostname, sizeof hostname) < 0)
- strcpy(hostname, "gnot");
- umask(0);
- if(argc == 1)
- if(chroot(argv[0]) < 0)
- sysfatal("chroot '%s' failed", argv[0]);
- none = uname2user("none");
- serve(0, 1);
- return 0;
- }
|