u9fs.c 31 KB

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