proto.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ip.h>
  5. #include <auth.h>
  6. #include <fcall.h>
  7. #include <ctype.h>
  8. #include <String.h>
  9. #include "ftpfs.h"
  10. enum
  11. {
  12. /* return codes */
  13. Extra= 1,
  14. Success= 2,
  15. Incomplete= 3,
  16. TempFail= 4,
  17. PermFail= 5,
  18. Impossible= 6,
  19. };
  20. Node *remdir; /* current directory on remote machine */
  21. Node *remroot; /* root directory on remote machine */
  22. int ctlfd; /* fd for control connection */
  23. Biobuf ctlin; /* input buffer for control connection */
  24. Biobuf stdin; /* input buffer for standard input */
  25. Biobuf dbuf; /* buffer for data connection */
  26. char msg[512]; /* buffer for replies */
  27. char net[Maxpath]; /* network for connections */
  28. int listenfd; /* fd to listen on for connections */
  29. char netdir[Maxpath];
  30. int os, defos;
  31. char topsdir[64]; /* name of listed directory for TOPS */
  32. String *remrootpath; /* path on remote side to remote root */
  33. char *user;
  34. int nopassive;
  35. long lastsend;
  36. static void sendrequest(char*, char*);
  37. static int getreply(Biobuf*, char*, int, int);
  38. static int active(int, Biobuf**, char*, char*);
  39. static int passive(int, Biobuf**, char*, char*);
  40. static int data(int, Biobuf**, char*, char*);
  41. static int port(void);
  42. static void ascii(void);
  43. static void image(void);
  44. static void unixpath(Node*, String*);
  45. static void vmspath(Node*, String*);
  46. static void mvspath(Node*, String*);
  47. static Node* vmsdir(char*);
  48. static int getpassword(char*, char*);
  49. static int nw_mode(char dirlet, char *s);
  50. /*
  51. * connect to remote server, default network is "tcp/ip"
  52. */
  53. void
  54. hello(char *dest)
  55. {
  56. char *p;
  57. char dir[Maxpath];
  58. Binit(&stdin, 0, OREAD); /* init for later use */
  59. ctlfd = dial(netmkaddr(dest, "tcp", "ftp"), 0, dir, 0);
  60. if(ctlfd < 0){
  61. fprint(2, "can't dial %s: %r\n", dest);
  62. exits("dialing");
  63. }
  64. Binit(&ctlin, ctlfd, OREAD);
  65. /* remember network for the data connections */
  66. p = strrchr(dir+1, '/');
  67. if(p == 0)
  68. fatal("wrong dial(2) linked with ftp");
  69. *p = 0;
  70. safecpy(net, dir, sizeof(net));
  71. /* wait for hello from other side */
  72. if(getreply(&ctlin, msg, sizeof(msg), 1) != Success)
  73. fatal("bad hello");
  74. if(strstr(msg, "Plan 9"))
  75. os = Plan9;
  76. }
  77. /*
  78. * login to remote system
  79. */
  80. void
  81. rlogin(char *rsys)
  82. {
  83. char *line;
  84. char pass[128];
  85. UserPasswd *up;
  86. up = nil;
  87. for(;;){
  88. if(up == nil && os != Plan9)
  89. up = auth_getuserpasswd(auth_getkey, "proto=pass server=%s", rsys);
  90. if(up != nil){
  91. sendrequest("USER", up->user);
  92. } else {
  93. print("User[default = %s]: ", user);
  94. line = Brdline(&stdin, '\n');
  95. if(line == 0)
  96. exits(0);
  97. line[Blinelen(&stdin)-1] = 0;
  98. if(*line){
  99. free(user);
  100. user = strdup(line);
  101. }
  102. sendrequest("USER", user);
  103. }
  104. switch(getreply(&ctlin, msg, sizeof(msg), 1)){
  105. case Success:
  106. goto out;
  107. case Incomplete:
  108. break;
  109. case TempFail:
  110. case PermFail:
  111. continue;
  112. }
  113. if(up != nil){
  114. sendrequest("PASS", up->passwd);
  115. } else {
  116. if(getpassword(pass, pass+sizeof(pass)) < 0)
  117. exits(0);
  118. sendrequest("PASS", pass);
  119. }
  120. if(getreply(&ctlin, msg, sizeof(msg), 1) == Success){
  121. if(strstr(msg, "Sess#"))
  122. defos = MVS;
  123. break;
  124. }
  125. }
  126. out:
  127. if(up != nil){
  128. memset(up, 0, sizeof(*up));
  129. free(up);
  130. }
  131. }
  132. /*
  133. * login to remote system with given user name and password.
  134. */
  135. void
  136. clogin(char *cuser, char *cpassword)
  137. {
  138. free(user);
  139. user = strdup(cuser);
  140. if (strcmp(user, "anonymous") != 0 &&
  141. strcmp(user, "ftp") != 0)
  142. fatal("User must be 'anonymous' or 'ftp'");
  143. sendrequest("USER", user);
  144. switch(getreply(&ctlin, msg, sizeof(msg), 1)){
  145. case Success:
  146. return;
  147. case Incomplete:
  148. break;
  149. case TempFail:
  150. case PermFail:
  151. fatal("login failed");
  152. }
  153. if (cpassword == 0)
  154. fatal("password needed");
  155. sendrequest("PASS", cpassword);
  156. if(getreply(&ctlin, msg, sizeof(msg), 1) != Success)
  157. fatal("password failed");
  158. if(strstr(msg, "Sess#"))
  159. defos = MVS;
  160. return;
  161. }
  162. /*
  163. * find out about the other side. go to it's root if requested. set
  164. * image mode if a Plan9 system.
  165. */
  166. void
  167. preamble(char *mountroot)
  168. {
  169. char *p, *ep;
  170. int rv;
  171. OS *o;
  172. /*
  173. * create a root directory mirror
  174. */
  175. remroot = newnode(0, s_copy("/"));
  176. remroot->d->qid.type = QTDIR;
  177. remroot->d->mode = DMDIR|0777;
  178. remdir = remroot;
  179. /*
  180. * get system type
  181. */
  182. sendrequest("SYST", nil);
  183. switch(getreply(&ctlin, msg, sizeof(msg), 1)){
  184. case Success:
  185. for(o = oslist; o->os != Unknown; o++)
  186. if(strncmp(msg+4, o->name, strlen(o->name)) == 0)
  187. break;
  188. os = o->os;
  189. if(os == NT)
  190. os = Unix;
  191. break;
  192. default:
  193. os = defos;
  194. break;
  195. }
  196. if(os == Unknown)
  197. os = defos;
  198. remrootpath = s_reset(remrootpath);
  199. switch(os){
  200. case Unix:
  201. case Plan9:
  202. case NetWare:
  203. /*
  204. * go to the remote root, if asked
  205. */
  206. if(mountroot){
  207. sendrequest("CWD", mountroot);
  208. getreply(&ctlin, msg, sizeof(msg), 0);
  209. } else {
  210. s_append(remrootpath, "/usr/");
  211. s_append(remrootpath, user);
  212. }
  213. /*
  214. * get the root directory
  215. */
  216. sendrequest("PWD", nil);
  217. rv = getreply(&ctlin, msg, sizeof(msg), 1);
  218. if(rv == PermFail){
  219. sendrequest("XPWD", nil);
  220. rv = getreply(&ctlin, msg, sizeof(msg), 1);
  221. }
  222. if(rv == Success){
  223. p = strchr(msg, '"');
  224. if(p){
  225. p++;
  226. ep = strchr(p, '"');
  227. if(ep){
  228. *ep = 0;
  229. s_append(s_reset(remrootpath), p);
  230. }
  231. }
  232. }
  233. break;
  234. case Tops:
  235. case VM:
  236. /*
  237. * top directory is a figment of our imagination.
  238. * make it permanently cached & valid.
  239. */
  240. CACHED(remroot);
  241. VALID(remroot);
  242. remroot->d->atime = time(0) + 100000;
  243. /*
  244. * no initial directory. We are in the
  245. * imaginary root.
  246. */
  247. remdir = newtopsdir("???");
  248. topsdir[0] = 0;
  249. if(os == Tops && readdir(remdir) >= 0){
  250. CACHED(remdir);
  251. if(*topsdir)
  252. remdir->remname = s_copy(topsdir);
  253. VALID(remdir);
  254. }
  255. break;
  256. case VMS:
  257. /*
  258. * top directory is a figment of our imagination.
  259. * make it permanently cached & valid.
  260. */
  261. CACHED(remroot);
  262. VALID(remroot);
  263. remroot->d->atime = time(0) + 100000;
  264. /*
  265. * get current directory
  266. */
  267. sendrequest("PWD", nil);
  268. rv = getreply(&ctlin, msg, sizeof(msg), 1);
  269. if(rv == PermFail){
  270. sendrequest("XPWD", nil);
  271. rv = getreply(&ctlin, msg, sizeof(msg), 1);
  272. }
  273. if(rv == Success){
  274. p = strchr(msg, '"');
  275. if(p){
  276. p++;
  277. ep = strchr(p, '"');
  278. if(ep){
  279. *ep = 0;
  280. remroot = remdir = vmsdir(p);
  281. }
  282. }
  283. }
  284. break;
  285. case MVS:
  286. usenlst = 1;
  287. break;
  288. }
  289. if(os == Plan9)
  290. image();
  291. }
  292. static void
  293. ascii(void)
  294. {
  295. sendrequest("TYPE A", nil);
  296. switch(getreply(&ctlin, msg, sizeof(msg), 0)){
  297. case Success:
  298. break;
  299. default:
  300. fatal("can't set type to ascii");
  301. }
  302. }
  303. static void
  304. image(void)
  305. {
  306. sendrequest("TYPE I", nil);
  307. switch(getreply(&ctlin, msg, sizeof(msg), 0)){
  308. case Success:
  309. break;
  310. default:
  311. fatal("can't set type to image/binary");
  312. }
  313. }
  314. /*
  315. * decode the time fields, return seconds since epoch began
  316. */
  317. char *monthchars = "janfebmaraprmayjunjulaugsepoctnovdec";
  318. static Tm now;
  319. static ulong
  320. cracktime(char *month, char *day, char *yr, char *hms)
  321. {
  322. Tm tm;
  323. int i;
  324. char *p;
  325. /* default time */
  326. if(now.year == 0)
  327. now = *localtime(time(0));
  328. tm = now;
  329. /* convert ascii month to a number twixt 1 and 12 */
  330. if(*month >= '0' && *month <= '9'){
  331. tm.mon = atoi(month) - 1;
  332. if(tm.mon < 0 || tm.mon > 11)
  333. tm.mon = 5;
  334. } else {
  335. for(p = month; *p; p++)
  336. *p = tolower(*p);
  337. for(i = 0; i < 12; i++)
  338. if(strncmp(&monthchars[i*3], month, 3) == 0){
  339. tm.mon = i;
  340. break;
  341. }
  342. }
  343. tm.mday = atoi(day);
  344. if(hms){
  345. tm.hour = strtol(hms, &p, 0);
  346. if(*p == ':'){
  347. tm.min = strtol(p+1, &p, 0);
  348. if(*p == ':')
  349. tm.sec = strtol(p+1, &p, 0);
  350. }
  351. if(tolower(*p) == 'p')
  352. tm.hour += 12;
  353. }
  354. if(yr){
  355. tm.year = atoi(yr);
  356. if(tm.year >= 1900)
  357. tm.year -= 1900;
  358. } else {
  359. if(tm.mon > now.mon || (tm.mon == now.mon && tm.mday > now.mday+1))
  360. tm.year--;
  361. }
  362. /* convert to epoch seconds */
  363. return tm2sec(&tm);
  364. }
  365. /*
  366. * decode a Unix or Plan 9 file mode
  367. */
  368. static ulong
  369. crackmode(char *p)
  370. {
  371. ulong flags;
  372. ulong mode;
  373. int i;
  374. flags = 0;
  375. switch(strlen(p)){
  376. case 10: /* unix and new style plan 9 */
  377. switch(*p){
  378. case 'l':
  379. return DMSYML|0777;
  380. case 'd':
  381. flags |= DMDIR;
  382. case 'a':
  383. flags |= DMAPPEND;
  384. }
  385. p++;
  386. if(p[2] == 'l')
  387. flags |= DMEXCL;
  388. break;
  389. case 11: /* old style plan 9 */
  390. switch(*p++){
  391. case 'd':
  392. flags |= DMDIR;
  393. break;
  394. case 'a':
  395. flags |= DMAPPEND;
  396. break;
  397. }
  398. if(*p++ == 'l')
  399. flags |= DMEXCL;
  400. break;
  401. default:
  402. return DMDIR|0777;
  403. }
  404. mode = 0;
  405. for(i = 0; i < 3; i++){
  406. mode <<= 3;
  407. if(*p++ == 'r')
  408. mode |= DMREAD;
  409. if(*p++ == 'w')
  410. mode |= DMWRITE;
  411. if(*p == 'x' || *p == 's' || *p == 'S')
  412. mode |= DMEXEC;
  413. p++;
  414. }
  415. return mode | flags;
  416. }
  417. /*
  418. * find first punctuation
  419. */
  420. char*
  421. strpunct(char *p)
  422. {
  423. int c;
  424. for(;c = *p; p++){
  425. if(ispunct(c))
  426. return p;
  427. }
  428. return 0;
  429. }
  430. /*
  431. * decode a Unix or Plan 9 directory listing
  432. */
  433. static Dir*
  434. crackdir(char *p, String **remname)
  435. {
  436. char *field[15];
  437. char *dfield[4];
  438. char *cp;
  439. String *s;
  440. int dn, n;
  441. Dir d, *dp;
  442. memset(&d, 0, sizeof(d));
  443. n = getfields(p, field, 15, 1, " \t");
  444. if(n > 2 && strcmp(field[n-2], "->") == 0)
  445. n -= 2;
  446. switch(os){
  447. case TSO:
  448. cp = strchr(field[0], '.');
  449. if(cp){
  450. *cp++ = 0;
  451. s = s_copy(cp);
  452. d.uid = field[0];
  453. } else {
  454. s = s_copy(field[0]);
  455. d.uid = "TSO";
  456. }
  457. d.gid = "TSO";
  458. d.mode = 0666;
  459. d.length = 0;
  460. d.atime = 0;
  461. break;
  462. case OS½:
  463. s = s_copy(field[n-1]);
  464. d.uid = "OS½";
  465. d.gid = d.uid;
  466. d.mode = 0666;
  467. switch(n){
  468. case 5:
  469. if(strcmp(field[1], "DIR") == 0)
  470. d.mode |= DMDIR;
  471. d.length = atoi(field[0]);
  472. dn = getfields(field[2], dfield, 4, 1, "-");
  473. if(dn == 3)
  474. d.atime = cracktime(dfield[0], dfield[1], dfield[2], field[3]);
  475. break;
  476. }
  477. break;
  478. case Tops:
  479. if(n != 4){ /* tops directory name */
  480. safecpy(topsdir, field[0], sizeof(topsdir));
  481. return 0;
  482. }
  483. s = s_copy(field[3]);
  484. d.length = atoi(field[0]);
  485. d.mode = 0666;
  486. d.uid = "Tops";
  487. d.gid = d.uid;
  488. dn = getfields(field[1], dfield, 4, 1, "-");
  489. if(dn == 3)
  490. d.atime = cracktime(dfield[1], dfield[0], dfield[2], field[2]);
  491. else
  492. d.atime = time(0);
  493. break;
  494. case VM:
  495. switch(n){
  496. case 9:
  497. s = s_copy(field[0]);
  498. s_append(s, ".");
  499. s_append(s, field[1]);
  500. d.length = atoi(field[3])*atoi(field[4]);
  501. if(*field[2] == 'F')
  502. d.mode = 0666;
  503. else
  504. d.mode = 0777;
  505. d.uid = "VM";
  506. d.gid = d.uid;
  507. dn = getfields(field[6], dfield, 4, 1, "/-");
  508. if(dn == 3)
  509. d.atime = cracktime(dfield[0], dfield[1], dfield[2], field[7]);
  510. else
  511. d.atime = time(0);
  512. break;
  513. case 1:
  514. s = s_copy(field[0]);
  515. d.uid = "VM";
  516. d.gid = d.uid;
  517. d.mode = 0777;
  518. d.atime = 0;
  519. break;
  520. default:
  521. return nil;
  522. }
  523. break;
  524. case VMS:
  525. switch(n){
  526. case 6:
  527. for(cp = field[0]; *cp; cp++)
  528. *cp = tolower(*cp);
  529. cp = strchr(field[0], ';');
  530. if(cp)
  531. *cp = 0;
  532. d.mode = 0666;
  533. cp = field[0] + strlen(field[0]) - 4;
  534. if(strcmp(cp, ".dir") == 0){
  535. d.mode |= DMDIR;
  536. *cp = 0;
  537. }
  538. s = s_copy(field[0]);
  539. d.length = atoi(field[1])*512;
  540. field[4][strlen(field[4])-1] = 0;
  541. d.uid = field[4]+1;
  542. d.gid = d.uid;
  543. dn = getfields(field[2], dfield, 4, 1, "/-");
  544. if(dn == 3)
  545. d.atime = cracktime(dfield[1], dfield[0], dfield[2], field[3]);
  546. else
  547. d.atime = time(0);
  548. break;
  549. default:
  550. return nil;
  551. }
  552. break;
  553. case NetWare:
  554. switch(n){
  555. case 8: /* New style */
  556. s = s_copy(field[7]);
  557. d.uid = field[2];
  558. d.gid = d.uid;
  559. d.mode = nw_mode(field[0][0], field[1]);
  560. d.length = atoi(field[3]);
  561. if(strchr(field[6], ':'))
  562. d.atime = cracktime(field[4], field[5], nil, field[6]);
  563. else
  564. d.atime = cracktime(field[4], field[5], field[6], nil);
  565. break;
  566. case 9:
  567. s = s_copy(field[8]);
  568. d.uid = field[2];
  569. d.gid = d.uid;
  570. d.mode = 0666;
  571. if(*field[0] == 'd')
  572. d.mode |= DMDIR;
  573. d.length = atoi(field[3]);
  574. d.atime = cracktime(field[4], field[5], field[6], field[7]);
  575. break;
  576. case 1:
  577. s = s_copy(field[0]);
  578. d.uid = "none";
  579. d.gid = d.uid;
  580. d.mode = 0777;
  581. d.atime = 0;
  582. break;
  583. default:
  584. return nil;
  585. }
  586. break;
  587. case Unix:
  588. case Plan9:
  589. default:
  590. switch(n){
  591. case 8: /* ls -l */
  592. s = s_copy(field[7]);
  593. d.uid = field[2];
  594. d.gid = d.uid;
  595. d.mode = crackmode(field[0]);
  596. d.length = atoi(field[3]);
  597. if(strchr(field[6], ':'))
  598. d.atime = cracktime(field[4], field[5], 0, field[6]);
  599. else
  600. d.atime = cracktime(field[4], field[5], field[6], 0);
  601. break;
  602. case 9: /* ls -lg */
  603. s = s_copy(field[8]);
  604. d.uid = field[2];
  605. d.gid = field[3];
  606. d.mode = crackmode(field[0]);
  607. d.length = atoi(field[4]);
  608. if(strchr(field[7], ':'))
  609. d.atime = cracktime(field[5], field[6], 0, field[7]);
  610. else
  611. d.atime = cracktime(field[5], field[6], field[7], 0);
  612. break;
  613. case 10: /* plan 9 */
  614. s = s_copy(field[9]);
  615. d.uid = field[3];
  616. d.gid = field[4];
  617. d.mode = crackmode(field[0]);
  618. d.length = atoi(field[5]);
  619. if(strchr(field[8], ':'))
  620. d.atime = cracktime(field[6], field[7], 0, field[8]);
  621. else
  622. d.atime = cracktime(field[6], field[7], field[8], 0);
  623. break;
  624. case 4: /* a Windows_NT version */
  625. s = s_copy(field[3]);
  626. d.uid = "NT";
  627. d.gid = d.uid;
  628. if(strcmp("<DIR>", field[2]) == 0){
  629. d.length = 0;
  630. d.mode = DMDIR|0777;
  631. } else {
  632. d.mode = 0666;
  633. d.length = atoi(field[2]);
  634. }
  635. dn = getfields(field[0], dfield, 4, 1, "/-");
  636. if(dn == 3)
  637. d.atime = cracktime(dfield[0], dfield[1], dfield[2], field[1]);
  638. break;
  639. case 1:
  640. s = s_copy(field[0]);
  641. d.uid = "none";
  642. d.gid = d.uid;
  643. d.mode = 0777;
  644. d.atime = 0;
  645. break;
  646. default:
  647. return nil;
  648. }
  649. }
  650. d.muid = d.uid;
  651. d.qid.type = (d.mode & DMDIR) ? QTDIR : QTFILE;
  652. d.mtime = d.atime;
  653. if(ext && (d.qid.type & QTDIR) == 0)
  654. s_append(s, ext);
  655. d.name = s_to_c(s);
  656. /* allocate a freeable dir structure */
  657. dp = reallocdir(&d, 0);
  658. *remname = s;
  659. return dp;
  660. }
  661. /*
  662. * probe files in a directory to see if they are directories
  663. */
  664. /*
  665. * read a remote directory
  666. */
  667. int
  668. readdir(Node *node)
  669. {
  670. Biobuf *bp;
  671. char *line;
  672. Node *np;
  673. Dir *d;
  674. long n;
  675. int tries, x, files;
  676. static int uselist;
  677. int usenlist;
  678. String *remname;
  679. if(changedir(node) < 0)
  680. return -1;
  681. usenlist = 0;
  682. for(tries = 0; tries < 3; tries++){
  683. if(usenlist || usenlst)
  684. x = data(OREAD, &bp, "NLST", nil);
  685. else if(os == Unix && !uselist)
  686. x = data(OREAD, &bp, "LIST -l", nil);
  687. else
  688. x = data(OREAD, &bp, "LIST", nil);
  689. switch(x){
  690. case Extra:
  691. break;
  692. /* case TempFail:
  693. continue;
  694. */
  695. default:
  696. if(os == Unix && uselist == 0){
  697. uselist = 1;
  698. continue;
  699. }
  700. return seterr(nosuchfile);
  701. }
  702. files = 0;
  703. while(line = Brdline(bp, '\n')){
  704. n = Blinelen(bp);
  705. if(debug)
  706. write(2, line, n);
  707. if(n > 1 && line[n-2] == '\r')
  708. n--;
  709. line[n - 1] = 0;
  710. d = crackdir(line, &remname);
  711. if(d == nil)
  712. continue;
  713. files++;
  714. np = extendpath(node, remname);
  715. d->qid.path = np->d->qid.path;
  716. d->qid.vers = np->d->qid.vers;
  717. d->type = np->d->type;
  718. d->dev = 1; /* mark node as valid */
  719. if(os == MVS && node == remroot){
  720. d->qid.type = QTDIR;
  721. d->mode |= DMDIR;
  722. }
  723. free(np->d);
  724. np->d = d;
  725. }
  726. close(Bfildes(bp));
  727. switch(getreply(&ctlin, msg, sizeof(msg), 0)){
  728. case Success:
  729. if(files == 0 && !usenlst && !usenlist){
  730. usenlist = 1;
  731. continue;
  732. }
  733. if(files && usenlist)
  734. usenlst = 1;
  735. if(usenlst)
  736. node->chdirunknown = 1;
  737. return 0;
  738. case TempFail:
  739. break;
  740. default:
  741. return seterr(nosuchfile);
  742. }
  743. }
  744. return seterr(nosuchfile);
  745. }
  746. /*
  747. * create a remote directory
  748. */
  749. int
  750. createdir(Node *node)
  751. {
  752. if(changedir(node->parent) < 0)
  753. return -1;
  754. sendrequest("MKD", node->d->name);
  755. if(getreply(&ctlin, msg, sizeof(msg), 0) != Success)
  756. return -1;
  757. return 0;
  758. }
  759. /*
  760. * change to a remote directory.
  761. */
  762. int
  763. changedir(Node *node)
  764. {
  765. Node *to;
  766. String *cdpath;
  767. to = node;
  768. if(to == remdir)
  769. return 0;
  770. /* build an absolute path */
  771. switch(os){
  772. case Tops:
  773. case VM:
  774. switch(node->depth){
  775. case 0:
  776. remdir = node;
  777. return 0;
  778. case 1:
  779. cdpath = s_clone(node->remname);
  780. break;
  781. default:
  782. return seterr(nosuchfile);
  783. }
  784. break;
  785. case VMS:
  786. switch(node->depth){
  787. case 0:
  788. remdir = node;
  789. return 0;
  790. default:
  791. cdpath = s_new();
  792. vmspath(node, cdpath);
  793. }
  794. break;
  795. case MVS:
  796. if(node->depth == 0)
  797. cdpath = s_clone(remrootpath);
  798. else{
  799. cdpath = s_new();
  800. mvspath(node, cdpath);
  801. }
  802. break;
  803. default:
  804. if(node->depth == 0)
  805. cdpath = s_clone(remrootpath);
  806. else{
  807. cdpath = s_new();
  808. unixpath(node, cdpath);
  809. }
  810. break;
  811. }
  812. uncachedir(remdir, 0);
  813. /*
  814. * connect, if we need a password (Incomplete)
  815. * act like it worked (best we can do).
  816. */
  817. sendrequest("CWD", s_to_c(cdpath));
  818. s_free(cdpath);
  819. switch(getreply(&ctlin, msg, sizeof(msg), 0)){
  820. case Success:
  821. case Incomplete:
  822. remdir = node;
  823. return 0;
  824. default:
  825. return seterr(nosuchfile);
  826. }
  827. }
  828. /*
  829. * read a remote file
  830. */
  831. int
  832. readfile1(Node *node)
  833. {
  834. Biobuf *bp;
  835. char buf[4*1024];
  836. long off;
  837. int n;
  838. int tries;
  839. if(changedir(node->parent) < 0)
  840. return -1;
  841. for(tries = 0; tries < 4; tries++){
  842. switch(data(OREAD, &bp, "RETR", s_to_c(node->remname))){
  843. case Extra:
  844. break;
  845. case TempFail:
  846. continue;
  847. default:
  848. return seterr(nosuchfile);
  849. }
  850. off = 0;
  851. while((n = read(Bfildes(bp), buf, sizeof buf)) > 0){
  852. if(filewrite(node, buf, off, n) != n){
  853. off = -1;
  854. break;
  855. }
  856. off += n;
  857. }
  858. if(off < 0)
  859. return -1;
  860. /* make sure a file gets created even for a zero length file */
  861. if(off == 0)
  862. filewrite(node, buf, 0, 0);
  863. close(Bfildes(bp));
  864. switch(getreply(&ctlin, msg, sizeof(msg), 0)){
  865. case Success:
  866. return off;
  867. case TempFail:
  868. continue;
  869. default:
  870. return seterr(nosuchfile);
  871. }
  872. }
  873. return seterr(nosuchfile);
  874. }
  875. int
  876. readfile(Node *node)
  877. {
  878. int rv, inimage;
  879. switch(os){
  880. case MVS:
  881. case Plan9:
  882. case Tops:
  883. case TSO:
  884. inimage = 0;
  885. break;
  886. default:
  887. inimage = 1;
  888. image();
  889. break;
  890. }
  891. rv = readfile1(node);
  892. if(inimage)
  893. ascii();
  894. return rv;
  895. }
  896. /*
  897. * write back a file
  898. */
  899. int
  900. createfile1(Node *node)
  901. {
  902. Biobuf *bp;
  903. char buf[4*1024];
  904. long off;
  905. int n;
  906. if(changedir(node->parent) < 0)
  907. return -1;
  908. if(data(OWRITE, &bp, "STOR", s_to_c(node->remname)) != Extra)
  909. return -1;
  910. for(off = 0; ; off += n){
  911. n = fileread(node, buf, off, sizeof(buf));
  912. if(n <= 0)
  913. break;
  914. write(Bfildes(bp), buf, n);
  915. }
  916. close(Bfildes(bp));
  917. getreply(&ctlin, msg, sizeof(msg), 0);
  918. return off;
  919. }
  920. int
  921. createfile(Node *node)
  922. {
  923. int rv;
  924. switch(os){
  925. case Plan9:
  926. case Tops:
  927. break;
  928. default:
  929. image();
  930. break;
  931. }
  932. rv = createfile1(node);
  933. switch(os){
  934. case Plan9:
  935. case Tops:
  936. break;
  937. default:
  938. ascii();
  939. break;
  940. }
  941. return rv;
  942. }
  943. /*
  944. * remove a remote file
  945. */
  946. int
  947. removefile(Node *node)
  948. {
  949. if(changedir(node->parent) < 0)
  950. return -1;
  951. sendrequest("DELE", s_to_c(node->remname));
  952. if(getreply(&ctlin, msg, sizeof(msg), 0) != Success)
  953. return -1;
  954. return 0;
  955. }
  956. /*
  957. * remove a remote directory
  958. */
  959. int
  960. removedir(Node *node)
  961. {
  962. if(changedir(node->parent) < 0)
  963. return -1;
  964. sendrequest("RMD", s_to_c(node->remname));
  965. if(getreply(&ctlin, msg, sizeof(msg), 0) != Success)
  966. return -1;
  967. return 0;
  968. }
  969. /*
  970. * tell remote that we're exiting and then do it
  971. */
  972. void
  973. quit(void)
  974. {
  975. sendrequest("QUIT", nil);
  976. getreply(&ctlin, msg, sizeof(msg), 0);
  977. exits(0);
  978. }
  979. /*
  980. * send a request
  981. */
  982. static void
  983. sendrequest(char *a, char *b)
  984. {
  985. char buf[2*1024];
  986. int n;
  987. n = strlen(a)+2+1;
  988. if(b != nil)
  989. n += strlen(b)+1;
  990. if(n >= sizeof(buf))
  991. fatal("proto request too long");
  992. strcpy(buf, a);
  993. if(b != nil){
  994. strcat(buf, " ");
  995. strcat(buf, b);
  996. }
  997. strcat(buf, "\r\n");
  998. n = strlen(buf);
  999. if(write(ctlfd, buf, n) != n)
  1000. fatal("remote side hung up");
  1001. if(debug)
  1002. write(2, buf, n);
  1003. lastsend = time(0);
  1004. }
  1005. /*
  1006. * replies codes are in the range [100, 999] and may contain multiple lines of
  1007. * continuation.
  1008. */
  1009. static int
  1010. getreply(Biobuf *bp, char *msg, int len, int printreply)
  1011. {
  1012. char *line;
  1013. char *p;
  1014. int rv;
  1015. int i, n;
  1016. while(line = Brdline(bp, '\n')){
  1017. /* add line to message buffer, strip off \r */
  1018. n = Blinelen(bp);
  1019. if(n > 1 && line[n-2] == '\r'){
  1020. n--;
  1021. line[n-1] = '\n';
  1022. }
  1023. if(printreply && !quiet)
  1024. write(1, line, n);
  1025. else if(debug)
  1026. write(2, line, n);
  1027. if(n > len - 1)
  1028. i = len - 1;
  1029. else
  1030. i = n;
  1031. if(i > 0){
  1032. memmove(msg, line, i);
  1033. msg += i;
  1034. len -= i;
  1035. *msg = 0;
  1036. }
  1037. /* stop if not a continuation */
  1038. rv = strtol(line, &p, 10);
  1039. if(rv >= 100 && rv < 600 && p==line+3 && *p != '-')
  1040. return rv/100;
  1041. /* tell user about continuations */
  1042. if(!debug && !quiet && !printreply)
  1043. write(2, line, n);
  1044. }
  1045. fatal("remote side closed connection");
  1046. return 0;
  1047. }
  1048. /*
  1049. * Announce on a local port and tell its address to the the remote side
  1050. */
  1051. static int
  1052. port(void)
  1053. {
  1054. char buf[256];
  1055. int n, fd;
  1056. char *ptr;
  1057. uchar ipaddr[IPaddrlen];
  1058. int port;
  1059. /* get a channel to listen on, let kernel pick the port number */
  1060. sprint(buf, "%s!*!0", net);
  1061. listenfd = announce(buf, netdir);
  1062. if(listenfd < 0)
  1063. return seterr("can't announce");
  1064. /* get the local address and port number */
  1065. sprint(buf, "%s/local", netdir);
  1066. fd = open(buf, OREAD);
  1067. if(fd < 0)
  1068. return seterr("opening %s: %r", buf);
  1069. n = read(fd, buf, sizeof(buf)-1);
  1070. close(fd);
  1071. if(n <= 0)
  1072. return seterr("opening %s/local: %r", netdir);
  1073. buf[n] = 0;
  1074. ptr = strchr(buf, ' ');
  1075. if(ptr)
  1076. *ptr = 0;
  1077. ptr = strchr(buf, '!')+1;
  1078. parseip(ipaddr, buf);
  1079. port = atoi(ptr);
  1080. /* tell remote side */
  1081. sprint(buf, "PORT %d,%d,%d,%d,%d,%d", ipaddr[IPv4off+0], ipaddr[IPv4off+1],
  1082. ipaddr[IPv4off+2], ipaddr[IPv4off+3], port>>8, port&0xff);
  1083. sendrequest(buf, nil);
  1084. if(getreply(&ctlin, msg, sizeof(msg), 0) != Success)
  1085. return seterr(msg);
  1086. return 0;
  1087. }
  1088. /*
  1089. * have server call back for a data connection
  1090. */
  1091. static int
  1092. active(int mode, Biobuf **bpp, char *cmda, char *cmdb)
  1093. {
  1094. int cfd, dfd, rv;
  1095. char newdir[Maxpath];
  1096. char datafile[Maxpath + 6];
  1097. if(port() < 0)
  1098. return TempFail;
  1099. sendrequest(cmda, cmdb);
  1100. rv = getreply(&ctlin, msg, sizeof(msg), 0);
  1101. if(rv != Extra){
  1102. close(listenfd);
  1103. return rv;
  1104. }
  1105. /* wait for a new call */
  1106. cfd = listen(netdir, newdir);
  1107. if(cfd < 0)
  1108. fatal("waiting for data connection");
  1109. close(listenfd);
  1110. /* open it's data connection and close the control connection */
  1111. sprint(datafile, "%s/data", newdir);
  1112. dfd = open(datafile, ORDWR);
  1113. close(cfd);
  1114. if(dfd < 0)
  1115. fatal("opening data connection");
  1116. Binit(&dbuf, dfd, mode);
  1117. *bpp = &dbuf;
  1118. return Extra;
  1119. }
  1120. /*
  1121. * call out for a data connection
  1122. */
  1123. static int
  1124. passive(int mode, Biobuf **bpp, char *cmda, char *cmdb)
  1125. {
  1126. char msg[1024];
  1127. char ds[1024];
  1128. char *f[6];
  1129. char *p;
  1130. int x, fd;
  1131. if(nopassive)
  1132. return Impossible;
  1133. sendrequest("PASV", nil);
  1134. if(getreply(&ctlin, msg, sizeof(msg), 0) != Success){
  1135. nopassive = 1;
  1136. return Impossible;
  1137. }
  1138. /* get address and port number from reply, this is AI */
  1139. p = strchr(msg, '(');
  1140. if(p == 0){
  1141. for(p = msg+3; *p; p++)
  1142. if(isdigit(*p))
  1143. break;
  1144. } else
  1145. p++;
  1146. if(getfields(p, f, 6, 0, ",") < 6){
  1147. if(debug)
  1148. fprint(2, "passive mode protocol botch: %s\n", msg);
  1149. werrstr("ftp protocol botch");
  1150. nopassive = 1;
  1151. return Impossible;
  1152. }
  1153. snprint(ds, sizeof(ds), "%s!%s.%s.%s.%s!%d", net,
  1154. f[0], f[1], f[2], f[3],
  1155. ((atoi(f[4])&0xff)<<8) + (atoi(f[5])&0xff));
  1156. /* open data connection */
  1157. fd = dial(ds, 0, 0, 0);
  1158. if(fd < 0){
  1159. if(debug)
  1160. fprint(2, "passive mode connect to %s failed: %r\n", ds);
  1161. nopassive = 1;
  1162. return TempFail;
  1163. }
  1164. /* tell remote to send a file */
  1165. sendrequest(cmda, cmdb);
  1166. x = getreply(&ctlin, msg, sizeof(msg), 0);
  1167. if(x != Extra){
  1168. close(fd);
  1169. if(debug)
  1170. fprint(2, "passive mode retrieve failed: %s\n", msg);
  1171. werrstr(msg);
  1172. return x;
  1173. }
  1174. Binit(&dbuf, fd, mode);
  1175. *bpp = &dbuf;
  1176. return Extra;
  1177. }
  1178. static int
  1179. data(int mode, Biobuf **bpp, char* cmda, char *cmdb)
  1180. {
  1181. int x;
  1182. x = passive(mode, bpp, cmda, cmdb);
  1183. if(x != Impossible)
  1184. return x;
  1185. return active(mode, bpp, cmda, cmdb);
  1186. }
  1187. /*
  1188. * used for keep alives
  1189. */
  1190. void
  1191. nop(void)
  1192. {
  1193. if(lastsend - time(0) < 15)
  1194. return;
  1195. sendrequest("PWD", nil);
  1196. getreply(&ctlin, msg, sizeof(msg), 0);
  1197. }
  1198. /*
  1199. * turn a vms spec into a path
  1200. */
  1201. static Node*
  1202. vmsextendpath(Node *np, char *name)
  1203. {
  1204. np = extendpath(np, s_copy(name));
  1205. if(!ISVALID(np)){
  1206. np->d->qid.type = QTDIR;
  1207. np->d->atime = time(0);
  1208. np->d->mtime = np->d->atime;
  1209. strcpy(np->d->uid, "who");
  1210. strcpy(np->d->gid, "cares");
  1211. np->d->mode = DMDIR|0777;
  1212. np->d->length = 0;
  1213. if(changedir(np) >= 0)
  1214. VALID(np);
  1215. }
  1216. return np;
  1217. }
  1218. static Node*
  1219. vmsdir(char *name)
  1220. {
  1221. char *cp;
  1222. Node *np;
  1223. char *oname;
  1224. np = remroot;
  1225. cp = strchr(name, '[');
  1226. if(cp)
  1227. strcpy(cp, cp+1);
  1228. cp = strchr(name, ']');
  1229. if(cp)
  1230. *cp = 0;
  1231. oname = name = strdup(name);
  1232. if(name == 0)
  1233. return 0;
  1234. while(cp = strchr(name, '.')){
  1235. *cp = 0;
  1236. np = vmsextendpath(np, name);
  1237. name = cp+1;
  1238. }
  1239. np = vmsextendpath(np, name);
  1240. /*
  1241. * walk back to first accessible directory
  1242. */
  1243. for(; np->parent != np; np = np->parent)
  1244. if(ISVALID(np)){
  1245. CACHED(np->parent);
  1246. break;
  1247. }
  1248. free(oname);
  1249. return np;
  1250. }
  1251. /*
  1252. * walk up the tree building a VMS style path
  1253. */
  1254. static void
  1255. vmspath(Node *node, String *path)
  1256. {
  1257. char *p;
  1258. int n;
  1259. if(node->depth == 1){
  1260. p = strchr(s_to_c(node->remname), ':');
  1261. if(p){
  1262. n = p - s_to_c(node->remname) + 1;
  1263. s_nappend(path, s_to_c(node->remname), n);
  1264. s_append(path, "[");
  1265. s_append(path, p+1);
  1266. } else {
  1267. s_append(path, "[");
  1268. s_append(path, s_to_c(node->remname));
  1269. }
  1270. s_append(path, "]");
  1271. return;
  1272. }
  1273. vmspath(node->parent, path);
  1274. s_append(path, ".");
  1275. s_append(path, s_to_c(node->remname));
  1276. }
  1277. /*
  1278. * walk up the tree building a Unix style path
  1279. */
  1280. static void
  1281. unixpath(Node *node, String *path)
  1282. {
  1283. if(node == node->parent){
  1284. s_append(path, s_to_c(remrootpath));
  1285. return;
  1286. }
  1287. unixpath(node->parent, path);
  1288. if(s_len(path) > 0 && strcmp(s_to_c(path), "/") != 0)
  1289. s_append(path, "/");
  1290. s_append(path, s_to_c(node->remname));
  1291. }
  1292. /*
  1293. * walk up the tree building a MVS style path
  1294. */
  1295. static void
  1296. mvspath(Node *node, String *path)
  1297. {
  1298. if(node == node->parent){
  1299. s_append(path, s_to_c(remrootpath));
  1300. return;
  1301. }
  1302. mvspath(node->parent, path);
  1303. if(s_len(path) > 0)
  1304. s_append(path, ".");
  1305. s_append(path, s_to_c(node->remname));
  1306. }
  1307. static int
  1308. getpassword(char *buf, char *e)
  1309. {
  1310. char *p;
  1311. int c;
  1312. int consctl, rv = 0;
  1313. consctl = open("/dev/consctl", OWRITE);
  1314. if(consctl >= 0)
  1315. write(consctl, "rawon", 5);
  1316. print("Password: ");
  1317. e--;
  1318. for(p = buf; p <= e; p++){
  1319. c = Bgetc(&stdin);
  1320. if(c < 0){
  1321. rv = -1;
  1322. goto out;
  1323. }
  1324. if(c == '\n' || c == '\r')
  1325. break;
  1326. *p = c;
  1327. }
  1328. *p = 0;
  1329. print("\n");
  1330. out:
  1331. if(consctl >= 0)
  1332. close(consctl);
  1333. return rv;
  1334. }
  1335. /*
  1336. * convert from latin1 to utf
  1337. */
  1338. static char*
  1339. fromlatin1(char *from)
  1340. {
  1341. char *p, *to;
  1342. Rune r;
  1343. if(os == Plan9)
  1344. return nil;
  1345. /* don't convert if we don't have to */
  1346. for(p = from; *p; p++)
  1347. if(*p & 0x80)
  1348. break;
  1349. if(*p == 0)
  1350. return nil;
  1351. to = malloc(3*strlen(from)+2);
  1352. if(to == nil)
  1353. return nil;
  1354. for(p = to; *from; from++){
  1355. r = (*from) & 0xff;
  1356. p += runetochar(p, &r);
  1357. }
  1358. *p = 0;
  1359. return to;
  1360. }
  1361. Dir*
  1362. reallocdir(Dir *d, int dofree)
  1363. {
  1364. Dir *dp;
  1365. char *p;
  1366. int nn, ng, nu, nm;
  1367. char *utf;
  1368. if(d->name == nil)
  1369. d->name = "?name?";
  1370. if(d->uid == nil)
  1371. d->uid = "?uid?";
  1372. if(d->gid == nil)
  1373. d->gid = d->uid;
  1374. if(d->muid == nil)
  1375. d->muid = d->uid;
  1376. utf = fromlatin1(d->name);
  1377. if(utf != nil)
  1378. d->name = utf;
  1379. nn = strlen(d->name)+1;
  1380. nu = strlen(d->uid)+1;
  1381. ng = strlen(d->gid)+1;
  1382. nm = strlen(d->muid)+1;
  1383. dp = malloc(sizeof(Dir)+nn+nu+ng+nm);
  1384. if(dp == nil){
  1385. if(dofree)
  1386. free(d);
  1387. if(utf != nil)
  1388. free(utf);
  1389. return nil;
  1390. }
  1391. *dp = *d;
  1392. p = (char*)&dp[1];
  1393. strcpy(p, d->name);
  1394. dp->name = p;
  1395. p += nn;
  1396. strcpy(p, d->uid);
  1397. dp->uid = p;
  1398. p += nu;
  1399. strcpy(p, d->gid);
  1400. dp->gid = p;
  1401. p += ng;
  1402. strcpy(p, d->muid);
  1403. dp->muid = p;
  1404. if(dofree)
  1405. free(d);
  1406. if(utf != nil)
  1407. free(utf);
  1408. return dp;
  1409. }
  1410. Dir*
  1411. dir_change_name(Dir *d, char *name)
  1412. {
  1413. if(d->name && strlen(d->name) >= strlen(name)){
  1414. strcpy(d->name, name);
  1415. return d;
  1416. }
  1417. d->name = name;
  1418. return reallocdir(d, 1);
  1419. }
  1420. Dir*
  1421. dir_change_uid(Dir *d, char *name)
  1422. {
  1423. if(d->uid && strlen(d->uid) >= strlen(name)){
  1424. strcpy(d->name, name);
  1425. return d;
  1426. }
  1427. d->uid = name;
  1428. return reallocdir(d, 1);
  1429. }
  1430. Dir*
  1431. dir_change_gid(Dir *d, char *name)
  1432. {
  1433. if(d->gid && strlen(d->gid) >= strlen(name)){
  1434. strcpy(d->name, name);
  1435. return d;
  1436. }
  1437. d->gid = name;
  1438. return reallocdir(d, 1);
  1439. }
  1440. Dir*
  1441. dir_change_muid(Dir *d, char *name)
  1442. {
  1443. if(d->muid && strlen(d->muid) >= strlen(name)){
  1444. strcpy(d->name, name);
  1445. return d;
  1446. }
  1447. d->muid = name;
  1448. return reallocdir(d, 1);
  1449. }
  1450. static int
  1451. nw_mode(char dirlet, char *s) /* NetWare file mode mapping */
  1452. {
  1453. int mode = 0777;
  1454. if(dirlet == 'd')
  1455. mode |= DMDIR;
  1456. if (strlen(s) >= 10 && s[0] != '[' || s[9] != ']')
  1457. return(mode);
  1458. if (s[1] == '-') /* can't read file */
  1459. mode &= ~0444;
  1460. if (dirlet == 'd' && s[6] == '-') /* cannot scan dir */
  1461. mode &= ~0444;
  1462. if (s[2] == '-') /* can't write file */
  1463. mode &= ~0222;
  1464. if (dirlet == 'd' && s[7] == '-' && s[3] == '-') /* cannot create in, or modify dir */
  1465. mode &= ~0222;
  1466. return(mode);
  1467. }