u9fs.c 30 KB

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