u9fs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. /* already in plan9.h #include <sys/types.h> *//* for struct passwd, struct group, struct stat ... */
  2. #include <sys/stat.h> /* for stat, umask */
  3. #include <stdlib.h> /* for malloc */
  4. #include <string.h> /* for strcpy, memmove */
  5. #include <pwd.h> /* for getpwnam, getpwuid */
  6. #include <grp.h> /* for getgrnam, getgrgid */
  7. #include <unistd.h> /* for gethostname, pread, pwrite, read, write */
  8. #include <utime.h> /* for utime */
  9. #include <dirent.h> /* for readdir */
  10. #include <errno.h> /* for errno */
  11. #include <stdio.h> /* for remove [sic] */
  12. #include <fcntl.h> /* for O_RDONLY, etc. */
  13. #include <sys/socket.h> /* various networking crud */
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16. #include <plan9.h>
  17. #include <fcall.h>
  18. #include <oldfcall.h>
  19. #include <u9fs.h>
  20. /* #ifndef because can be given in makefile */
  21. #ifndef DEFAULTLOG
  22. #define DEFAULTLOG "/tmp/u9fs.log"
  23. #endif
  24. char *logfile = DEFAULTLOG;
  25. #define S_ISSPECIAL(m) (S_ISCHR(m) || S_ISBLK(m) || S_ISFIFO(m))
  26. enum {
  27. Tdot = 1,
  28. Tdotdot
  29. };
  30. enum {
  31. P9P1,
  32. P9P2000
  33. };
  34. typedef struct User User;
  35. struct User {
  36. int id;
  37. gid_t defaultgid;
  38. char *name;
  39. char **mem; /* group members */
  40. int nmem;
  41. User *next;
  42. };
  43. typedef struct Fid Fid;
  44. struct Fid {
  45. int fid;
  46. char *path;
  47. struct stat st;
  48. User *u;
  49. int omode;
  50. DIR *dir;
  51. int diroffset;
  52. int fd;
  53. struct dirent *dirent;
  54. Fid *next;
  55. Fid *prev;
  56. };
  57. void* emalloc(size_t);
  58. void* erealloc(void*, size_t);
  59. char* estrdup(char*);
  60. char* estrpath(char*, char*);
  61. void sysfatal(char*, ...);
  62. int okuser(char*);
  63. void rversion(Fcall*, Fcall*);
  64. void rauth(Fcall*, Fcall*);
  65. void rattach(Fcall*, Fcall*);
  66. void rflush(Fcall*, Fcall*);
  67. void rclone(Fcall*, Fcall*);
  68. void rwalk(Fcall*, Fcall*);
  69. void ropen(Fcall*, Fcall*);
  70. void rcreate(Fcall*, Fcall*);
  71. void rread(Fcall*, Fcall*);
  72. void rwrite(Fcall*, Fcall*);
  73. void rclunk(Fcall*, Fcall*);
  74. void rstat(Fcall*, Fcall*);
  75. void rwstat(Fcall*, Fcall*);
  76. void rclwalk(Fcall*, Fcall*);
  77. void rremove(Fcall*, Fcall*);
  78. User* uname2user(char*);
  79. User* gname2user(char*);
  80. User* uid2user(int);
  81. User* gid2user(int);
  82. Fid* newfid(int, char**);
  83. Fid* oldfid(int, char**);
  84. int fidstat(Fid*, char**);
  85. void freefid(Fid*);
  86. int userchange(User*, char**);
  87. int userwalk(User*, char**, char*, Qid*, char**);
  88. int useropen(Fid*, int, char**);
  89. int usercreate(Fid*, char*, int, long, char**);
  90. int userremove(Fid*, char**);
  91. int userperm(User*, char*, int, int);
  92. int useringroup(User*, User*);
  93. Qid stat2qid(struct stat*);
  94. void getfcallold(int, Fcall*, int);
  95. void putfcallold(int, Fcall*);
  96. char Eauth[] = "authentication failed";
  97. char Ebadfid[] = "fid unknown or out of range";
  98. char Ebadoffset[] = "bad offset in directory read";
  99. char Ebadusefid[] = "bad use of fid";
  100. char Edirchange[] = "wstat can't convert between files and directories";
  101. char Eexist[] = "file or directory already exists";
  102. char Efidactive[] = "fid already in use";
  103. char Enotdir[] = "not a directory";
  104. char Enotingroup[] = "not a member of proposed group";
  105. char Enotowner[] = "only owner can change group in wstat";
  106. char Eperm[] = "permission denied";
  107. char Especial0[] = "already attached without access to special files";
  108. char Especial1[] = "already attached with access to special files";
  109. char Especial[] = "no access to special file";
  110. char Etoolarge[] = "i/o count too large";
  111. char Eunknowngroup[] = "unknown group";
  112. char Eunknownuser[] = "unknown user";
  113. char Ewstatbuffer[] = "bogus wstat buffer";
  114. ulong msize = IOHDRSZ+8192;
  115. uchar* rxbuf;
  116. uchar* txbuf;
  117. void* databuf;
  118. int connected;
  119. int devallowed;
  120. char* autharg;
  121. char* defaultuser;
  122. char hostname[256];
  123. char remotehostname[256];
  124. int chatty9p = 0;
  125. int network = 1;
  126. int old9p = -1;
  127. int authed;
  128. User* none;
  129. Auth *authmethods[] = { /* first is default */
  130. &authrhosts,
  131. &authnone,
  132. };
  133. Auth *auth;
  134. void
  135. getfcallnew(int fd, Fcall *fc, int have)
  136. {
  137. int len;
  138. if(have > BIT32SZ)
  139. sysfatal("cannot happen");
  140. if(have < BIT32SZ && readn(fd, rxbuf+have, BIT32SZ-have) != BIT32SZ-have)
  141. sysfatal("couldn't read message");
  142. len = GBIT32(rxbuf);
  143. if(len <= BIT32SZ)
  144. sysfatal("bogus message");
  145. len -= BIT32SZ;
  146. if(readn(fd, rxbuf+BIT32SZ, len) != len)
  147. sysfatal("short message");
  148. if(convM2S(rxbuf, len+BIT32SZ, fc) != len+BIT32SZ)
  149. sysfatal("badly sized message type %d", rxbuf[0]);
  150. }
  151. void
  152. getfcallold(int fd, Fcall *fc, int have)
  153. {
  154. int len, n;
  155. if(have > 3)
  156. sysfatal("cannot happen");
  157. if(have < 3 && readn(fd, rxbuf, 3-have) != 3-have)
  158. sysfatal("couldn't read message");
  159. len = oldhdrsize(rxbuf[0]);
  160. if(len < 3)
  161. sysfatal("bad message %d", rxbuf[0]);
  162. if(len > 3 && readn(fd, rxbuf+3, len-3) != len-3)
  163. sysfatal("couldn't read message");
  164. n = iosize(rxbuf);
  165. if(readn(fd, rxbuf+len, n) != n)
  166. sysfatal("couldn't read message");
  167. len += n;
  168. if(convM2Sold(rxbuf, len, fc) != len)
  169. sysfatal("badly sized message type %d", rxbuf[0]);
  170. }
  171. void
  172. putfcallnew(int wfd, Fcall *tx)
  173. {
  174. uint n;
  175. if((n = convS2M(tx, txbuf, msize)) == 0)
  176. sysfatal("couldn't format message type %d", tx->type);
  177. if(write(wfd, txbuf, n) != n)
  178. sysfatal("couldn't send message");
  179. }
  180. void
  181. putfcallold(int wfd, Fcall *tx)
  182. {
  183. uint n;
  184. if((n = convS2Mold(tx, txbuf, msize)) == 0)
  185. sysfatal("couldn't format message type %d", tx->type);
  186. if(write(wfd, txbuf, n) != n)
  187. sysfatal("couldn't send message");
  188. }
  189. void
  190. getfcall(int fd, Fcall *fc)
  191. {
  192. if(old9p == 1){
  193. getfcallold(fd, fc, 0);
  194. return;
  195. }
  196. if(old9p == 0){
  197. getfcallnew(fd, fc, 0);
  198. return;
  199. }
  200. /* auto-detect */
  201. if(readn(fd, rxbuf, 3) != 3)
  202. sysfatal("couldn't read message");
  203. /* is it an old (9P1) message? */
  204. if(50 <= rxbuf[0] && rxbuf[0] <= 87 && (rxbuf[0]&1)==0 && GBIT16(rxbuf+1) == 0xFFFF){
  205. old9p = 1;
  206. getfcallold(fd, fc, 3);
  207. return;
  208. }
  209. getfcallnew(fd, fc, 3);
  210. old9p = 0;
  211. }
  212. void
  213. seterror(Fcall *f, char *error)
  214. {
  215. f->type = Rerror;
  216. f->ename = error ? error : "programmer error";
  217. }
  218. int
  219. isowner(User *u, Fid *f)
  220. {
  221. return u->id == f->st.st_uid;
  222. }
  223. void
  224. serve(int rfd, int wfd)
  225. {
  226. Fcall rx, tx;
  227. for(;;){
  228. getfcall(rfd, &rx);
  229. if(chatty9p)
  230. fprint(2, "<- %F\n", &rx);
  231. memset(&tx, 0, sizeof tx);
  232. tx.type = rx.type+1;
  233. tx.tag = rx.tag;
  234. switch(rx.type){
  235. case Tflush:
  236. break;
  237. case Tversion:
  238. rversion(&rx, &tx);
  239. break;
  240. case Tauth:
  241. rauth(&rx, &tx);
  242. break;
  243. case Tattach:
  244. rattach(&rx, &tx);
  245. break;
  246. case Twalk:
  247. rwalk(&rx, &tx);
  248. break;
  249. case Tstat:
  250. tx.stat = databuf;
  251. rstat(&rx, &tx);
  252. break;
  253. case Twstat:
  254. rwstat(&rx, &tx);
  255. break;
  256. case Topen:
  257. ropen(&rx, &tx);
  258. break;
  259. case Tcreate:
  260. rcreate(&rx, &tx);
  261. break;
  262. case Tread:
  263. tx.data = databuf;
  264. rread(&rx, &tx);
  265. break;
  266. case Twrite:
  267. rwrite(&rx, &tx);
  268. break;
  269. case Tclunk:
  270. rclunk(&rx, &tx);
  271. break;
  272. case Tremove:
  273. rremove(&rx, &tx);
  274. break;
  275. default:
  276. fprint(2, "unknown message %F\n", &rx);
  277. seterror(&tx, "bad message");
  278. break;
  279. }
  280. if(chatty9p)
  281. fprint(2, "-> %F\n", &tx);
  282. (old9p ? putfcallold : putfcallnew)(wfd, &tx);
  283. }
  284. }
  285. void
  286. rversion(Fcall *rx, Fcall *tx)
  287. {
  288. if(msize > rx->msize)
  289. msize = rx->msize;
  290. tx->msize = msize;
  291. if(strncmp(rx->version, "9P", 2) != 0)
  292. tx->version = "unknown";
  293. else
  294. tx->version = "9P2000";
  295. }
  296. void
  297. rauth(Fcall *rx, Fcall *tx)
  298. {
  299. char *e;
  300. if((e = auth->auth(rx, tx)) != nil)
  301. seterror(tx, e);
  302. }
  303. void
  304. rattach(Fcall *rx, Fcall *tx)
  305. {
  306. char *e;
  307. Fid *fid;
  308. User *u;
  309. if(rx->aname == nil)
  310. rx->aname = "";
  311. if(strcmp(rx->aname, "device") == 0){
  312. if(connected && !devallowed){
  313. seterror(tx, Especial0);
  314. return;
  315. }
  316. devallowed = 1;
  317. }else{
  318. if(connected && devallowed){
  319. seterror(tx, Especial1);
  320. return;
  321. }
  322. }
  323. if(strcmp(rx->uname, "none") == 0){
  324. if(authed == 0){
  325. seterror(tx, Eauth);
  326. return;
  327. }
  328. } else {
  329. if((e = auth->attach(rx, tx)) != nil){
  330. seterror(tx, e);
  331. return;
  332. }
  333. authed++;
  334. }
  335. if((fid = newfid(rx->fid, &e)) == nil){
  336. seterror(tx, e);
  337. return;
  338. }
  339. fid->path = estrdup("/");
  340. if(fidstat(fid, &e) < 0){
  341. seterror(tx, e);
  342. freefid(fid);
  343. return;
  344. }
  345. if(defaultuser)
  346. rx->uname = defaultuser;
  347. if((u = uname2user(rx->uname)) == nil || u->id == 0){
  348. /* we don't know anyone named root... */
  349. seterror(tx, Eunknownuser);
  350. freefid(fid);
  351. return;
  352. }
  353. fid->u = u;
  354. tx->qid = stat2qid(&fid->st);
  355. return;
  356. }
  357. void
  358. rwalk(Fcall *rx, Fcall *tx)
  359. {
  360. int i;
  361. char *path, *e;
  362. Fid *fid, *nfid;
  363. e = nil;
  364. if((fid = oldfid(rx->fid, &e)) == nil){
  365. seterror(tx, e);
  366. return;
  367. }
  368. if(fid->omode != -1){
  369. seterror(tx, Ebadusefid);
  370. return;
  371. }
  372. if(fidstat(fid, &e) < 0){
  373. seterror(tx, e);
  374. return;
  375. }
  376. if(!S_ISDIR(fid->st.st_mode) && rx->nwname){
  377. seterror(tx, Enotdir);
  378. return;
  379. }
  380. nfid = nil;
  381. if(rx->newfid != rx->fid && (nfid = newfid(rx->newfid, &e)) == nil){
  382. seterror(tx, e);
  383. return;
  384. }
  385. path = estrdup(fid->path);
  386. e = nil;
  387. for(i=0; i<rx->nwname; i++)
  388. if(userwalk(fid->u, &path, rx->wname[i], &tx->wqid[i], &e) < 0)
  389. break;
  390. if(i == rx->nwname){ /* successful clone or walk */
  391. tx->nwqid = i;
  392. if(nfid){
  393. nfid->path = path;
  394. nfid->u = fid->u;
  395. }else{
  396. free(fid->path);
  397. fid->path = path;
  398. }
  399. }else{
  400. if(i > 0) /* partial walk? */
  401. tx->nwqid = i;
  402. else
  403. seterror(tx, e);
  404. if(nfid) /* clone implicit new fid */
  405. freefid(nfid);
  406. free(path);
  407. }
  408. return;
  409. }
  410. void
  411. ropen(Fcall *rx, Fcall *tx)
  412. {
  413. char *e;
  414. Fid *fid;
  415. if((fid = oldfid(rx->fid, &e)) == nil){
  416. seterror(tx, e);
  417. return;
  418. }
  419. if(fid->omode != -1){
  420. seterror(tx, Ebadusefid);
  421. return;
  422. }
  423. if(fidstat(fid, &e) < 0){
  424. seterror(tx, e);
  425. return;
  426. }
  427. if(!devallowed && S_ISSPECIAL(fid->st.st_mode)){
  428. seterror(tx, Especial);
  429. return;
  430. }
  431. if(useropen(fid, rx->mode, &e) < 0){
  432. seterror(tx, e);
  433. return;
  434. }
  435. tx->iounit = 0;
  436. tx->qid = stat2qid(&fid->st);
  437. }
  438. void
  439. rcreate(Fcall *rx, Fcall *tx)
  440. {
  441. char *e;
  442. Fid *fid;
  443. if((fid = oldfid(rx->fid, &e)) == nil){
  444. seterror(tx, e);
  445. return;
  446. }
  447. if(fid->omode != -1){
  448. seterror(tx, Ebadusefid);
  449. return;
  450. }
  451. if(fidstat(fid, &e) < 0){
  452. seterror(tx, e);
  453. return;
  454. }
  455. if(!S_ISDIR(fid->st.st_mode)){
  456. seterror(tx, Enotdir);
  457. return;
  458. }
  459. if(usercreate(fid, rx->name, rx->mode, rx->perm, &e) < 0){
  460. seterror(tx, e);
  461. return;
  462. }
  463. if(fidstat(fid, &e) < 0){
  464. seterror(tx, e);
  465. return;
  466. }
  467. tx->iounit = 0;
  468. tx->qid = stat2qid(&fid->st);
  469. }
  470. uchar
  471. modebyte(struct stat *st)
  472. {
  473. uchar b;
  474. b = 0;
  475. if(S_ISDIR(st->st_mode))
  476. b |= QTDIR;
  477. /* no way to test append-only */
  478. /* no real way to test exclusive use, but mark devices as such */
  479. if(S_ISSPECIAL(st->st_mode))
  480. b |= QTEXCL;
  481. return b;
  482. }
  483. ulong
  484. plan9mode(struct stat *st)
  485. {
  486. return ((ulong)modebyte(st)<<24) | (st->st_mode & 0777);
  487. }
  488. /*
  489. * this is for chmod, so don't worry about S_IFDIR
  490. */
  491. mode_t
  492. unixmode(Dir *d)
  493. {
  494. return (mode_t)(d->mode&0777);
  495. }
  496. Qid
  497. stat2qid(struct stat *st)
  498. {
  499. uchar *p, *ep, *q;
  500. Qid qid;
  501. /*
  502. * For now, ignore the device number.
  503. */
  504. qid.path = 0;
  505. p = (uchar*)&qid.path;
  506. ep = p+sizeof(qid.path);
  507. q = p+sizeof(ino_t);
  508. if(q > ep){
  509. fprint(2, "warning: inode number too big\n");
  510. q = ep;
  511. }
  512. memmove(p, &st->st_ino, q-p);
  513. q = q+sizeof(dev_t);
  514. if(q > ep){
  515. /* fprint(2, "warning: inode number + device number too big %d+%d\n", sizeof(ino_t), sizeof(dev_t)); */
  516. q = ep - sizeof(dev_t);
  517. if(q < p)
  518. fprint(2, "warning: device number too big by itself\n");
  519. else
  520. *(dev_t*)q ^= st->st_dev;
  521. }
  522. qid.vers = st->st_mtime ^ (st->st_size << 8);
  523. qid.type = modebyte(st);
  524. return qid;
  525. }
  526. void
  527. stat2dir(char *path, struct stat *st, Dir *d)
  528. {
  529. User *u;
  530. char *q;
  531. memset(d, 0, sizeof(*d));
  532. d->qid = stat2qid(st);
  533. d->mode = plan9mode(st);
  534. d->atime = st->st_atime;
  535. d->mtime = st->st_mtime;
  536. d->length = st->st_size;
  537. d->uid = (u = uid2user(st->st_uid)) ? u->name : "???";
  538. d->gid = (u = gid2user(st->st_gid)) ? u->name : "???";
  539. d->muid = "";
  540. if((q = strrchr(path, '/')) != nil)
  541. d->name = q+1;
  542. else
  543. d->name = path;
  544. }
  545. void
  546. rread(Fcall *rx, Fcall *tx)
  547. {
  548. char *e, *path;
  549. uchar *p, *ep;
  550. int n;
  551. Fid *fid;
  552. Dir d;
  553. struct stat st;
  554. if(rx->count > msize-IOHDRSZ){
  555. seterror(tx, Etoolarge);
  556. return;
  557. }
  558. if((fid = oldfid(rx->fid, &e)) == nil){
  559. seterror(tx, e);
  560. return;
  561. }
  562. if(fid->omode == -1 || (fid->omode&3) == OWRITE){
  563. seterror(tx, Ebadusefid);
  564. return;
  565. }
  566. if(fid->dir){
  567. if(rx->offset != fid->diroffset){
  568. if(rx->offset != 0){
  569. seterror(tx, Ebadoffset);
  570. return;
  571. }
  572. rewinddir(fid->dir);
  573. fid->diroffset = 0;
  574. }
  575. p = (uchar*)tx->data;
  576. ep = (uchar*)tx->data+rx->count;
  577. for(;;){
  578. if(p+BIT16SZ >= ep)
  579. break;
  580. if(fid->dirent == nil) /* one entry cache for when convD2M fails */
  581. if((fid->dirent = readdir(fid->dir)) == nil)
  582. break;
  583. if(strcmp(fid->dirent->d_name, ".") == 0
  584. || strcmp(fid->dirent->d_name, "..") == 0){
  585. fid->dirent = nil;
  586. continue;
  587. }
  588. path = estrpath(fid->path, fid->dirent->d_name);
  589. memset(&st, 0, sizeof st);
  590. if(stat(path, &st) < 0){
  591. fprint(2, "dirread: stat(%s) failed: %s\n", path, strerror(errno));
  592. fid->dirent = nil;
  593. free(path);
  594. continue;
  595. }
  596. free(path);
  597. stat2dir(fid->dirent->d_name, &st, &d);
  598. if((n=(old9p ? convD2Mold : convD2M)(&d, p, ep-p)) <= BIT16SZ)
  599. break;
  600. p += n;
  601. fid->dirent = nil;
  602. }
  603. tx->count = p - (uchar*)tx->data;
  604. fid->diroffset += tx->count;
  605. }else{
  606. if((n = pread(fid->fd, tx->data, rx->count, rx->offset)) < 0){
  607. seterror(tx, strerror(errno));
  608. return;
  609. }
  610. tx->count = n;
  611. }
  612. }
  613. void
  614. rwrite(Fcall *rx, Fcall *tx)
  615. {
  616. char *e;
  617. Fid *fid;
  618. int n;
  619. if(rx->count > msize-IOHDRSZ){
  620. seterror(tx, Etoolarge);
  621. return;
  622. }
  623. if((fid = oldfid(rx->fid, &e)) == nil){
  624. seterror(tx, e);
  625. return;
  626. }
  627. if(fid->omode == -1 || (fid->omode&3) == OREAD || (fid->omode&3) == OEXEC){
  628. seterror(tx, Ebadusefid);
  629. return;
  630. }
  631. if((n = pwrite(fid->fd, rx->data, rx->count, rx->offset)) < 0){
  632. seterror(tx, strerror(errno));
  633. return;
  634. }
  635. tx->count = n;
  636. }
  637. void
  638. rclunk(Fcall *rx, Fcall *tx)
  639. {
  640. char *e;
  641. Fid *fid;
  642. if((fid = oldfid(rx->fid, &e)) == nil){
  643. seterror(tx, e);
  644. return;
  645. }
  646. if(fid->omode != -1 && fid->omode&ORCLOSE)
  647. remove(fid->path);
  648. freefid(fid);
  649. }
  650. void
  651. rremove(Fcall *rx, Fcall *tx)
  652. {
  653. char *e;
  654. Fid *fid;
  655. if((fid = oldfid(rx->fid, &e)) == nil){
  656. seterror(tx, e);
  657. return;
  658. }
  659. if(userremove(fid, &e) < 0)
  660. seterror(tx, e);
  661. freefid(fid);
  662. }
  663. void
  664. rstat(Fcall *rx, Fcall *tx)
  665. {
  666. char *e;
  667. Fid *fid;
  668. Dir d;
  669. if((fid = oldfid(rx->fid, &e)) == nil){
  670. seterror(tx, e);
  671. return;
  672. }
  673. if(fidstat(fid, &e) < 0){
  674. seterror(tx, e);
  675. return;
  676. }
  677. stat2dir(fid->path, &fid->st, &d);
  678. if((tx->nstat=(old9p ? convD2Mold : convD2M)(&d, tx->stat, msize)) <= BIT16SZ)
  679. seterror(tx, "convD2M fails");
  680. }
  681. void
  682. rwstat(Fcall *rx, Fcall *tx)
  683. {
  684. char *e;
  685. char *p, *old, *new, *dir;
  686. gid_t gid;
  687. Dir d;
  688. Fid *fid;
  689. if((fid = oldfid(rx->fid, &e)) == nil){
  690. seterror(tx, e);
  691. return;
  692. }
  693. /*
  694. * wstat is supposed to be atomic.
  695. * we check all the things we can before trying anything.
  696. * still, if we are told to truncate a file and rename it and only
  697. * one works, we're screwed. in such cases we leave things
  698. * half broken and return an error. it's hardly perfect.
  699. */
  700. if((old9p ? convM2Dold : convM2D)(rx->stat, rx->nstat, &d, (char*)rx->stat) <= BIT16SZ){
  701. seterror(tx, Ewstatbuffer);
  702. return;
  703. }
  704. if(fidstat(fid, &e) < 0){
  705. seterror(tx, e);
  706. return;
  707. }
  708. /*
  709. * The casting is necessary because d.mode is ulong and might,
  710. * on some systems, be 64 bits. We only want to compare the
  711. * bottom 32 bits, since that's all that gets sent in the protocol.
  712. *
  713. * Same situation for d.mtime and d.length (although that last check
  714. * is admittedly superfluous, given the current lack of 128-bit machines).
  715. */
  716. gid = (gid_t)-1;
  717. if(d.gid[0] != '\0'){
  718. User *g;
  719. g = gname2user(d.gid);
  720. if(g == nil){
  721. seterror(tx, Eunknowngroup);
  722. return;
  723. }
  724. gid = (gid_t)g->id;
  725. if(groupchange(fid->u, gid2user(gid), &e) < 0){
  726. seterror(tx, e);
  727. return;
  728. }
  729. }
  730. if((u32int)d.mode != (u32int)~0 && (((d.mode&DMDIR)!=0) ^ (S_ISDIR(fid->st.st_mode)!=0))){
  731. seterror(tx, Edirchange);
  732. return;
  733. }
  734. if(strcmp(fid->path, "/") == 0){
  735. seterror(tx, "no wstat of root");
  736. return;
  737. }
  738. /*
  739. * try things in increasing order of harm to the file.
  740. * mtime should come after truncate so that if you
  741. * do both the mtime actually takes effect, but i'd rather
  742. * leave truncate until last.
  743. * (see above comment about atomicity).
  744. */
  745. if((u32int)d.mode != (u32int)~0 && chmod(fid->path, unixmode(&d)) < 0){
  746. if(chatty9p)
  747. fprint(2, "chmod(%s, 0%luo) failed\n", fid->path, unixmode(&d));
  748. seterror(tx, strerror(errno));
  749. return;
  750. }
  751. if((u32int)d.mtime != (u32int)~0){
  752. struct utimbuf t;
  753. t.actime = 0;
  754. t.modtime = d.mtime;
  755. if(utime(fid->path, &t) < 0){
  756. if(chatty9p)
  757. fprint(2, "utime(%s) failed\n", fid->path);
  758. seterror(tx, strerror(errno));
  759. return;
  760. }
  761. }
  762. if(gid != (gid_t)-1 && gid != fid->st.st_gid){
  763. if(chown(fid->path, (uid_t)-1, gid) < 0){
  764. if(chatty9p)
  765. fprint(2, "chgrp(%s, %d) failed\n", fid->path, gid);
  766. seterror(tx, strerror(errno));
  767. return;
  768. }
  769. }
  770. if(d.name[0]){
  771. old = fid->path;
  772. dir = estrdup(fid->path);
  773. if((p = strrchr(dir, '/')) > dir)
  774. *p = '\0';
  775. else{
  776. seterror(tx, "whoops: can't happen in u9fs");
  777. return;
  778. }
  779. new = estrpath(dir, d.name);
  780. if(strcmp(old, new) != 0 && rename(old, new) < 0){
  781. if(chatty9p)
  782. fprint(2, "rename(%s, %s) failed\n", old, new);
  783. seterror(tx, strerror(errno));
  784. free(new);
  785. free(dir);
  786. return;
  787. }
  788. fid->path = new;
  789. free(old);
  790. free(dir);
  791. }
  792. if((u64int)d.length != (u64int)~0 && truncate(fid->path, d.length) < 0){
  793. fprint(2, "truncate(%s, %lld) failed\n", fid->path, d.length);
  794. seterror(tx, strerror(errno));
  795. return;
  796. }
  797. }
  798. /*
  799. * we keep a table by numeric id. by name lookups happen infrequently
  800. * while by-number lookups happen once for every directory entry read
  801. * and every stat request.
  802. */
  803. User *utab[64];
  804. User *gtab[64];
  805. User*
  806. adduser(struct passwd *p)
  807. {
  808. User *u;
  809. u = emalloc(sizeof(*u));
  810. u->id = p->pw_uid;
  811. u->name = estrdup(p->pw_name);
  812. u->next = utab[p->pw_uid%nelem(utab)];
  813. u->defaultgid = p->pw_gid;
  814. utab[p->pw_uid%nelem(utab)] = u;
  815. return u;
  816. }
  817. int
  818. useringroup(User *u, User *g)
  819. {
  820. int i;
  821. for(i=0; i<g->nmem; i++)
  822. if(strcmp(g->mem[i], u->name) == 0)
  823. return 1;
  824. /*
  825. * Hack around common Unix problem that everyone has
  826. * default group "user" but /etc/group lists no members.
  827. */
  828. if(u->defaultgid == g->id)
  829. return 1;
  830. return 0;
  831. }
  832. User*
  833. addgroup(struct group *g)
  834. {
  835. User *u;
  836. char **p;
  837. int n;
  838. u = emalloc(sizeof(*u));
  839. n = 0;
  840. for(p=g->gr_mem; *p; p++)
  841. n++;
  842. u->mem = emalloc(sizeof(u->mem[0])*n);
  843. n = 0;
  844. for(p=g->gr_mem; *p; p++)
  845. u->mem[n++] = estrdup(*p);
  846. u->nmem = n;
  847. u->id = g->gr_gid;
  848. u->name = estrdup(g->gr_name);
  849. u->next = gtab[g->gr_gid%nelem(gtab)];
  850. gtab[g->gr_gid%nelem(gtab)] = u;
  851. return u;
  852. }
  853. User*
  854. uname2user(char *name)
  855. {
  856. int i;
  857. User *u;
  858. struct passwd *p;
  859. for(i=0; i<nelem(utab); i++)
  860. for(u=utab[i]; u; u=u->next)
  861. if(strcmp(u->name, name) == 0)
  862. return u;
  863. if((p = getpwnam(name)) == nil)
  864. return nil;
  865. return adduser(p);
  866. }
  867. User*
  868. uid2user(int id)
  869. {
  870. User *u;
  871. struct passwd *p;
  872. for(u=utab[id%nelem(utab)]; u; u=u->next)
  873. if(u->id == id)
  874. return u;
  875. if((p = getpwuid(id)) == nil)
  876. return nil;
  877. return adduser(p);
  878. }
  879. User*
  880. gname2user(char *name)
  881. {
  882. int i;
  883. User *u;
  884. struct group *g;
  885. for(i=0; i<nelem(gtab); i++)
  886. for(u=gtab[i]; u; u=u->next)
  887. if(strcmp(u->name, name) == 0)
  888. return u;
  889. if((g = getgrnam(name)) == nil)
  890. return nil;
  891. return addgroup(g);
  892. }
  893. User*
  894. gid2user(int id)
  895. {
  896. User *u;
  897. struct group *g;
  898. for(u=gtab[id%nelem(gtab)]; u; u=u->next)
  899. if(u->id == id)
  900. return u;
  901. if((g = getgrgid(id)) == nil)
  902. return nil;
  903. return addgroup(g);
  904. }
  905. void
  906. sysfatal(char *fmt, ...)
  907. {
  908. char buf[1024];
  909. va_list va;
  910. va_start(va, fmt);
  911. doprint(buf, buf+sizeof buf, fmt, va);
  912. va_end(va);
  913. fprint(2, "u9fs: %s\n", buf);
  914. fprint(2, "last unix error: %s\n", strerror(errno));
  915. exit(1);
  916. }
  917. void*
  918. emalloc(size_t n)
  919. {
  920. void *p;
  921. if(n == 0)
  922. n = 1;
  923. p = malloc(n);
  924. if(p == 0)
  925. sysfatal("malloc(%ld) fails", (long)n);
  926. memset(p, 0, n);
  927. return p;
  928. }
  929. void*
  930. erealloc(void *p, size_t n)
  931. {
  932. if(p == 0)
  933. p = malloc(n);
  934. else
  935. p = realloc(p, n);
  936. if(p == 0)
  937. sysfatal("realloc(..., %ld) fails", (long)n);
  938. return p;
  939. }
  940. char*
  941. estrdup(char *p)
  942. {
  943. p = strdup(p);
  944. if(p == 0)
  945. sysfatal("strdup(%.20s) fails", p);
  946. return p;
  947. }
  948. char*
  949. estrpath(char *p, char *q)
  950. {
  951. char *r, *s;
  952. if(strcmp(q, "..") == 0){
  953. r = estrdup(p);
  954. if((s = strrchr(r, '/')) && s > r)
  955. *s = '\0';
  956. else if(s == r)
  957. s[1] = '\0';
  958. return r;
  959. }
  960. r = emalloc(strlen(p)+1+strlen(q)+1);
  961. strcpy(r, p);
  962. if(r[0]=='\0' || r[strlen(r)-1] != '/')
  963. strcat(r, "/");
  964. strcat(r, q);
  965. return r;
  966. }
  967. Fid *newfid(int, char**);
  968. Fid *oldfid(int, char**);
  969. int fidstat(Fid*, char**);
  970. void freefid(Fid*);
  971. Fid *fidtab[1];
  972. Fid*
  973. lookupfid(int fid)
  974. {
  975. Fid *f;
  976. for(f=fidtab[fid%nelem(fidtab)]; f; f=f->next)
  977. if(f->fid == fid)
  978. return f;
  979. return nil;
  980. }
  981. Fid*
  982. newfid(int fid, char **ep)
  983. {
  984. Fid *f;
  985. if(lookupfid(fid) != nil){
  986. *ep = Efidactive;
  987. return nil;
  988. }
  989. f = emalloc(sizeof(*f));
  990. f->next = fidtab[fid%nelem(fidtab)];
  991. if(f->next)
  992. f->next->prev = f;
  993. fidtab[fid%nelem(fidtab)] = f;
  994. f->fid = fid;
  995. f->fd = -1;
  996. f->omode = -1;
  997. return f;
  998. }
  999. Fid*
  1000. oldfid(int fid, char **ep)
  1001. {
  1002. Fid *f;
  1003. if((f = lookupfid(fid)) == nil){
  1004. *ep = Ebadfid;
  1005. return nil;
  1006. }
  1007. if(userchange(f->u, ep) < 0)
  1008. return nil;
  1009. return f;
  1010. }
  1011. void
  1012. freefid(Fid *f)
  1013. {
  1014. if(f->prev)
  1015. f->prev->next = f->next;
  1016. else
  1017. fidtab[f->fid%nelem(fidtab)] = f->next;
  1018. if(f->next)
  1019. f->next->prev = f->prev;
  1020. if(f->dir)
  1021. closedir(f->dir);
  1022. if(f->fd)
  1023. close(f->fd);
  1024. free(f->path);
  1025. free(f);
  1026. }
  1027. int
  1028. fidstat(Fid *fid, char **ep)
  1029. {
  1030. if(stat(fid->path, &fid->st) < 0){
  1031. fprint(2, "fidstat(%s) failed\n", fid->path);
  1032. if(ep)
  1033. *ep = strerror(errno);
  1034. return -1;
  1035. }
  1036. if(S_ISDIR(fid->st.st_mode))
  1037. fid->st.st_size = 0;
  1038. return 0;
  1039. }
  1040. int
  1041. userchange(User *u, char **ep)
  1042. {
  1043. if(defaultuser)
  1044. return 0;
  1045. if(setreuid(0, 0) < 0){
  1046. fprint(2, "setreuid(0, 0) failed\n");
  1047. *ep = "cannot setuid back to root";
  1048. return -1;
  1049. }
  1050. /*
  1051. * Initgroups does not appear to be SUSV standard.
  1052. * But it exists on SGI and on Linux, which makes me
  1053. * think it's standard enough. We have to do something
  1054. * like this, and the closest other function I can find is
  1055. * setgroups (which initgroups eventually calls).
  1056. * Setgroups is the same as far as standardization though,
  1057. * so we're stuck using a non-SUSV call. Sigh.
  1058. */
  1059. if(initgroups(u->name, u->defaultgid) < 0)
  1060. fprint(2, "initgroups(%s) failed: %s\n", u->name, strerror(errno));
  1061. if(setreuid(-1, u->id) < 0){
  1062. fprint(2, "setreuid(-1, %s) failed\n", u->name);
  1063. *ep = strerror(errno);
  1064. return -1;
  1065. }
  1066. return 0;
  1067. }
  1068. /*
  1069. * We do our own checking here, then switch to root temporarily
  1070. * to set our gid. In a perfect world, you'd be allowed to set your
  1071. * egid to any of the supplemental groups of your euid, but this
  1072. * is not the case on Linux 2.2.14 (and perhaps others).
  1073. *
  1074. * This is a race, of course, but it's a race against processes
  1075. * that can edit the group lists. If you can do that, you can
  1076. * change your own group without our help.
  1077. */
  1078. int
  1079. groupchange(User *u, User *g, char **ep)
  1080. {
  1081. if(!useringroup(u, g)){
  1082. if(chatty9p)
  1083. fprint(2, "%s not in group %s\n", u->name, g->name);
  1084. *ep = Enotingroup;
  1085. return -1;
  1086. }
  1087. setreuid(0,0);
  1088. if(setregid(-1, g->id) < 0){
  1089. fprint(2, "setegid(%s/%d) failed in groupchange\n", g->name, g->id);
  1090. *ep = strerror(errno);
  1091. return -1;
  1092. }
  1093. if(userchange(u, ep) < 0)
  1094. return -1;
  1095. return 0;
  1096. }
  1097. /*
  1098. * An attempt to enforce permissions by looking at the
  1099. * file system. Separation of checking permission and
  1100. * actually performing the action is a terrible idea, of
  1101. * course, so we use setreuid for most of the permission
  1102. * enforcement. This is here only so we can give errors
  1103. * on open(ORCLOSE) in some cases.
  1104. */
  1105. int
  1106. userperm(User *u, char *path, int type, int need)
  1107. {
  1108. char *p, *q;
  1109. int i, have;
  1110. struct stat st;
  1111. User *g;
  1112. switch(type){
  1113. default:
  1114. fprint(2, "bad type %d in userperm\n", type);
  1115. return -1;
  1116. case Tdot:
  1117. if(stat(path, &st) < 0){
  1118. fprint(2, "userperm: stat(%s) failed\n", path);
  1119. return -1;
  1120. }
  1121. break;
  1122. case Tdotdot:
  1123. p = estrdup(path);
  1124. if((q = strrchr(p, '/'))==nil){
  1125. fprint(2, "userperm(%s, ..): bad path\n", p);
  1126. free(p);
  1127. return -1;
  1128. }
  1129. if(q > p)
  1130. *q = '\0';
  1131. else
  1132. *(q+1) = '\0';
  1133. if(stat(p, &st) < 0){
  1134. fprint(2, "userperm: stat(%s) (dotdot of %s) failed\n",
  1135. p, path);
  1136. free(p);
  1137. return -1;
  1138. }
  1139. free(p);
  1140. break;
  1141. }
  1142. if(u == none){
  1143. fprint(2, "userperm: none wants %d in 0%luo\n", need, st.st_mode);
  1144. have = st.st_mode&7;
  1145. if((have&need)==need)
  1146. return 0;
  1147. return -1;
  1148. }
  1149. have = st.st_mode&7;
  1150. if((uid_t)u->id == st.st_uid)
  1151. have |= (st.st_mode>>6)&7;
  1152. if((have&need)==need)
  1153. return 0;
  1154. if(((have|((st.st_mode>>3)&7))&need) != need) /* group won't help */
  1155. return -1;
  1156. g = gid2user(st.st_gid);
  1157. for(i=0; i<g->nmem; i++){
  1158. if(strcmp(g->mem[i], u->name) == 0){
  1159. have |= (st.st_mode>>3)&7;
  1160. break;
  1161. }
  1162. }
  1163. if((have&need)==need)
  1164. return 0;
  1165. return -1;
  1166. }
  1167. int
  1168. userwalk(User *u, char **path, char *elem, Qid *qid, char **ep)
  1169. {
  1170. char *npath;
  1171. struct stat st;
  1172. npath = estrpath(*path, elem);
  1173. if(stat(npath, &st) < 0){
  1174. free(npath);
  1175. *ep = strerror(errno);
  1176. return -1;
  1177. }
  1178. *qid = stat2qid(&st);
  1179. free(*path);
  1180. *path = npath;
  1181. return 0;
  1182. }
  1183. int
  1184. useropen(Fid *fid, int omode, char **ep)
  1185. {
  1186. int a, o;
  1187. /*
  1188. * Check this anyway, to try to head off problems later.
  1189. */
  1190. if((omode&ORCLOSE) && userperm(fid->u, fid->path, Tdotdot, W_OK) < 0){
  1191. *ep = Eperm;
  1192. return -1;
  1193. }
  1194. switch(omode&3){
  1195. default:
  1196. *ep = "programmer error";
  1197. return -1;
  1198. case OREAD:
  1199. a = R_OK;
  1200. o = O_RDONLY;
  1201. break;
  1202. case ORDWR:
  1203. a = R_OK|W_OK;
  1204. o = O_RDWR;
  1205. break;
  1206. case OWRITE:
  1207. a = R_OK;
  1208. o = O_WRONLY;
  1209. break;
  1210. case OEXEC:
  1211. a = X_OK;
  1212. o = O_RDONLY;
  1213. break;
  1214. }
  1215. if(omode & OTRUNC){
  1216. a |= W_OK;
  1217. o |= O_TRUNC;
  1218. }
  1219. if(S_ISDIR(fid->st.st_mode)){
  1220. if(a != R_OK){
  1221. fprint(2, "attempt by %s to open dir %d\n", fid->u->name, omode);
  1222. *ep = Eperm;
  1223. return -1;
  1224. }
  1225. if((fid->dir = opendir(fid->path)) == nil){
  1226. *ep = strerror(errno);
  1227. return -1;
  1228. }
  1229. }else{
  1230. /*
  1231. * This is wrong because access used the real uid
  1232. * and not the effective uid. Let the open sort it out.
  1233. *
  1234. if(access(fid->path, a) < 0){
  1235. *ep = strerror(errno);
  1236. return -1;
  1237. }
  1238. *
  1239. */
  1240. if((fid->fd = open(fid->path, o)) < 0){
  1241. *ep = strerror(errno);
  1242. return -1;
  1243. }
  1244. }
  1245. fid->omode = omode;
  1246. return 0;
  1247. }
  1248. int
  1249. usercreate(Fid *fid, char *elem, int omode, long perm, char **ep)
  1250. {
  1251. int o, m;
  1252. char *opath, *npath;
  1253. struct stat st, parent;
  1254. if(stat(fid->path, &parent) < 0){
  1255. *ep = strerror(errno);
  1256. return -1;
  1257. }
  1258. /*
  1259. * Change group so that created file has expected group
  1260. * by Plan 9 semantics. If that fails, might as well go
  1261. * with the user's default group.
  1262. */
  1263. if(groupchange(fid->u, gid2user(parent.st_gid), ep) < 0
  1264. && groupchange(fid->u, gid2user(fid->u->defaultgid), ep) < 0)
  1265. return -1;
  1266. m = (perm & DMDIR) ? 0777 : 0666;
  1267. perm = perm & (~m | (fid->st.st_mode & m));
  1268. npath = estrpath(fid->path, elem);
  1269. if(perm & DMDIR){
  1270. if((omode&~ORCLOSE) != OREAD){
  1271. *ep = Eperm;
  1272. free(npath);
  1273. return -1;
  1274. }
  1275. if(stat(npath, &st) >= 0 || errno != ENOENT){
  1276. *ep = Eexist;
  1277. free(npath);
  1278. return -1;
  1279. }
  1280. /* race */
  1281. if(mkdir(npath, perm&0777) < 0){
  1282. *ep = strerror(errno);
  1283. free(npath);
  1284. return -1;
  1285. }
  1286. if((fid->dir = opendir(npath)) == nil){
  1287. *ep = strerror(errno);
  1288. remove(npath); /* race */
  1289. free(npath);
  1290. return -1;
  1291. }
  1292. }else{
  1293. o = O_CREAT|O_EXCL;
  1294. switch(omode&3){
  1295. default:
  1296. *ep = "programmer error";
  1297. return -1;
  1298. case OREAD:
  1299. case OEXEC:
  1300. o |= O_RDONLY;
  1301. break;
  1302. case ORDWR:
  1303. o |= O_RDWR;
  1304. break;
  1305. case OWRITE:
  1306. o |= O_WRONLY;
  1307. break;
  1308. }
  1309. if(omode & OTRUNC)
  1310. o |= O_TRUNC;
  1311. if((fid->fd = open(npath, o, perm&0777)) < 0){
  1312. if(chatty9p)
  1313. fprint(2, "create(%s, 0x%x, 0%o) failed\n", npath, o, perm&0777);
  1314. *ep = strerror(errno);
  1315. free(npath);
  1316. return -1;
  1317. }
  1318. }
  1319. opath = fid->path;
  1320. fid->path = npath;
  1321. if(fidstat(fid, ep) < 0){
  1322. fprint(2, "stat after create on %s failed\n", npath);
  1323. remove(npath); /* race */
  1324. free(npath);
  1325. fid->path = opath;
  1326. if(fid->fd >= 0){
  1327. close(fid->fd);
  1328. fid->fd = -1;
  1329. }else{
  1330. closedir(fid->dir);
  1331. fid->dir = nil;
  1332. }
  1333. return -1;
  1334. }
  1335. fid->omode = omode;
  1336. free(opath);
  1337. return 0;
  1338. }
  1339. int
  1340. userremove(Fid *fid, char **ep)
  1341. {
  1342. if(remove(fid->path) < 0){
  1343. *ep = strerror(errno);
  1344. return -1;
  1345. }
  1346. return 0;
  1347. }
  1348. void
  1349. usage(void)
  1350. {
  1351. fprint(2, "usage: u9fs [-Dnz] [-a authmethod] [-m msize] [-u user] [root]\n");
  1352. exit(1);
  1353. }
  1354. int
  1355. main(int argc, char **argv)
  1356. {
  1357. char *authtype;
  1358. int i;
  1359. int fd;
  1360. int logflag;
  1361. auth = authmethods[0];
  1362. logflag = O_WRONLY|O_APPEND|O_CREAT;
  1363. ARGBEGIN{
  1364. case 'D':
  1365. chatty9p = 1;
  1366. break;
  1367. case 'a':
  1368. authtype = EARGF(usage());
  1369. auth = nil;
  1370. for(i=0; i<nelem(authmethods); i++)
  1371. if(strcmp(authmethods[i]->name, authtype)==0)
  1372. auth = authmethods[i];
  1373. if(auth == nil)
  1374. sysfatal("unknown auth type '%s'", authtype);
  1375. break;
  1376. case 'A':
  1377. autharg = EARGF(usage());
  1378. break;
  1379. case 'l':
  1380. logfile = EARGF(usage());
  1381. break;
  1382. case 'm':
  1383. msize = strtol(EARGF(usage()), 0, 0);
  1384. break;
  1385. case 'n':
  1386. network = 0;
  1387. break;
  1388. case 'u':
  1389. defaultuser = EARGF(usage());
  1390. break;
  1391. case 'z':
  1392. logflag |= O_TRUNC;
  1393. }ARGEND
  1394. if(argc > 1)
  1395. usage();
  1396. fd = open(logfile, logflag, 0666);
  1397. if(fd < 0)
  1398. sysfatal("cannot open log '%s'", logfile);
  1399. if(dup2(fd, 2) < 0)
  1400. sysfatal("cannot dup fd onto stderr");
  1401. fprint(2, "u9fs\nkill %d\n", (int)getpid());
  1402. fmtinstall('F', fcallconv);
  1403. fmtinstall('D', dirconv);
  1404. fmtinstall('M', dirmodeconv);
  1405. rxbuf = emalloc(msize);
  1406. txbuf = emalloc(msize);
  1407. databuf = emalloc(msize);
  1408. if(auth->init)
  1409. auth->init();
  1410. if(network)
  1411. getremotehostname(remotehostname, sizeof remotehostname);
  1412. if(gethostname(hostname, sizeof hostname) < 0)
  1413. strcpy(hostname, "gnot");
  1414. umask(0);
  1415. if(argc == 1)
  1416. if(chroot(argv[0]) < 0)
  1417. sysfatal("chroot '%s' failed", argv[0]);
  1418. none = uname2user("none");
  1419. serve(0, 1);
  1420. return 0;
  1421. }