u9fs.c 30 KB

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