ftpd.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <auth.h>
  5. #include <ip.h>
  6. #include <libsec.h>
  7. #include <String.h>
  8. #include "glob.h"
  9. enum
  10. {
  11. /* telnet control character */
  12. Iac= 255,
  13. /* representation types */
  14. Tascii= 0,
  15. Timage= 1,
  16. /* transmission modes */
  17. Mstream= 0,
  18. Mblock= 1,
  19. Mpage= 2,
  20. /* file structure */
  21. Sfile= 0,
  22. Sblock= 1,
  23. Scompressed= 2,
  24. /* read/write buffer size */
  25. Nbuf= 4096,
  26. /* maximum ms we'll wait for a command */
  27. Maxwait= 1000*60*30, /* inactive for 30 minutes, we hang up */
  28. Maxerr= 128,
  29. Maxpath= 512,
  30. };
  31. int abortcmd(char*);
  32. int appendcmd(char*);
  33. int cdupcmd(char*);
  34. int cwdcmd(char*);
  35. int delcmd(char*);
  36. int helpcmd(char*);
  37. int listcmd(char*);
  38. int mdtmcmd(char*);
  39. int mkdircmd(char*);
  40. int modecmd(char*);
  41. int namelistcmd(char*);
  42. int nopcmd(char*);
  43. int passcmd(char*);
  44. int pasvcmd(char*);
  45. int portcmd(char*);
  46. int pwdcmd(char*);
  47. int quitcmd(char*);
  48. int rnfrcmd(char*);
  49. int rntocmd(char*);
  50. int reply(char*, ...);
  51. int restartcmd(char*);
  52. int retrievecmd(char*);
  53. int sitecmd(char*);
  54. int sizecmd(char*);
  55. int storecmd(char*);
  56. int storeucmd(char*);
  57. int structcmd(char*);
  58. int systemcmd(char*);
  59. int typecmd(char*);
  60. int usercmd(char*);
  61. int dialdata(void);
  62. char* abspath(char*);
  63. int crlfwrite(int, char*, int);
  64. int sodoff(void);
  65. int accessok(char*);
  66. typedef struct Cmd Cmd;
  67. struct Cmd
  68. {
  69. char *name;
  70. int (*f)(char*);
  71. int needlogin;
  72. };
  73. Cmd cmdtab[] =
  74. {
  75. { "abor", abortcmd, 0, },
  76. { "appe", appendcmd, 1, },
  77. { "cdup", cdupcmd, 1, },
  78. { "cwd", cwdcmd, 1, },
  79. { "dele", delcmd, 1, },
  80. { "help", helpcmd, 0, },
  81. { "list", listcmd, 1, },
  82. { "mdtm", mdtmcmd, 1, },
  83. { "mkd", mkdircmd, 1, },
  84. { "mode", modecmd, 0, },
  85. { "nlst", namelistcmd, 1, },
  86. { "noop", nopcmd, 0, },
  87. { "pass", passcmd, 0, },
  88. { "pasv", pasvcmd, 1, },
  89. { "pwd", pwdcmd, 0, },
  90. { "port", portcmd, 1, },
  91. { "quit", quitcmd, 0, },
  92. { "rest", restartcmd, 1, },
  93. { "retr", retrievecmd, 1, },
  94. { "rmd", delcmd, 1, },
  95. { "rnfr", rnfrcmd, 1, },
  96. { "rnto", rntocmd, 1, },
  97. { "site", sitecmd, 1, },
  98. { "size", sizecmd, 1, },
  99. { "stor", storecmd, 1, },
  100. { "stou", storeucmd, 1, },
  101. { "stru", structcmd, 1, },
  102. { "syst", systemcmd, 0, },
  103. { "type", typecmd, 0, },
  104. { "user", usercmd, 0, },
  105. { 0, 0, 0 },
  106. };
  107. #define NONENS "/lib/namespace.ftp" /* default ns for none */
  108. char user[Maxpath]; /* logged in user */
  109. char curdir[Maxpath]; /* current directory path */
  110. Chalstate *ch;
  111. int loggedin;
  112. int type; /* transmission type */
  113. int mode; /* transmission mode */
  114. int structure; /* file structure */
  115. char data[64]; /* data address */
  116. int pid; /* transfer process */
  117. int encryption; /* encryption state */
  118. int isnone, anon_ok, anon_only, anon_everybody;
  119. char cputype[Maxpath]; /* the environment variable of the same name */
  120. char bindir[Maxpath]; /* bin directory for this architecture */
  121. char mailaddr[Maxpath];
  122. char *namespace = NONENS;
  123. int debug;
  124. NetConnInfo *nci;
  125. int createperm = 0660;
  126. int isnoworld;
  127. vlong offset; /* from restart command */
  128. ulong id;
  129. typedef struct Passive Passive;
  130. struct Passive
  131. {
  132. int inuse;
  133. char adir[40];
  134. int afd;
  135. int port;
  136. uchar ipaddr[IPaddrlen];
  137. } passive;
  138. #define FTPLOG "ftp"
  139. void
  140. logit(char *fmt, ...)
  141. {
  142. char buf[8192];
  143. va_list arg;
  144. char errstr[128];
  145. rerrstr(errstr, sizeof errstr);
  146. va_start(arg, fmt);
  147. vseprint(buf, buf+sizeof(buf), fmt, arg);
  148. va_end(arg);
  149. syslog(0, FTPLOG, "%s.%s %s", nci->rsys, nci->rserv, buf);
  150. werrstr(errstr, sizeof errstr);
  151. }
  152. static void
  153. usage(void)
  154. {
  155. syslog(0, "ftp", "usage: %s [-aAde] [-n nsfile]", argv0);
  156. fprint(2, "usage: %s [-aAde] [-n nsfile]\n", argv0);
  157. exits("usage");
  158. }
  159. /*
  160. * read commands from the control stream and dispatch
  161. */
  162. void
  163. main(int argc, char **argv)
  164. {
  165. char *cmd;
  166. char *arg;
  167. char *p;
  168. Cmd *t;
  169. Biobuf in;
  170. int i;
  171. ARGBEGIN{
  172. case 'a': /* anonymous OK */
  173. anon_ok = 1;
  174. break;
  175. case 'A':
  176. anon_ok = 1;
  177. anon_only = 1;
  178. break;
  179. case 'd':
  180. debug++;
  181. break;
  182. case 'e':
  183. anon_ok = 1;
  184. anon_everybody = 1;
  185. break;
  186. case 'n':
  187. namespace = EARGF(usage());
  188. break;
  189. default:
  190. usage();
  191. }ARGEND
  192. /* open log file before doing a newns */
  193. syslog(0, FTPLOG, nil);
  194. /* find out who is calling */
  195. if(argc < 1)
  196. nci = getnetconninfo(nil, 0);
  197. else
  198. nci = getnetconninfo(argv[argc-1], 0);
  199. if(nci == nil)
  200. sysfatal("ftpd needs a network address");
  201. strcpy(mailaddr, "?");
  202. id = getpid();
  203. /* figure out which binaries to bind in later (only for none) */
  204. arg = getenv("cputype");
  205. if(arg)
  206. strecpy(cputype, cputype+sizeof cputype, arg);
  207. else
  208. strcpy(cputype, "mips");
  209. /* shurely /%s/bin */
  210. snprint(bindir, sizeof(bindir), "/bin/%s/bin", cputype);
  211. Binit(&in, 0, OREAD);
  212. reply("220 Plan 9 FTP server ready");
  213. alarm(Maxwait);
  214. while(cmd = Brdline(&in, '\n')){
  215. alarm(0);
  216. /*
  217. * strip out trailing cr's & lf and delimit with null
  218. */
  219. i = Blinelen(&in)-1;
  220. cmd[i] = 0;
  221. if(debug)
  222. logit("%s", cmd);
  223. while(i > 0 && cmd[i-1] == '\r')
  224. cmd[--i] = 0;
  225. /*
  226. * hack for GatorFTP+, look for a 0x10 used as a delimiter
  227. */
  228. p = strchr(cmd, 0x10);
  229. if(p)
  230. *p = 0;
  231. /*
  232. * get rid of telnet control sequences (we don't need them)
  233. */
  234. while(*cmd && (uchar)*cmd == Iac){
  235. cmd++;
  236. if(*cmd)
  237. cmd++;
  238. }
  239. /*
  240. * parse the message (command arg)
  241. */
  242. arg = strchr(cmd, ' ');
  243. if(arg){
  244. *arg++ = 0;
  245. while(*arg == ' ')
  246. arg++;
  247. }
  248. /*
  249. * ignore blank commands
  250. */
  251. if(*cmd == 0)
  252. continue;
  253. /*
  254. * lookup the command and do it
  255. */
  256. for(p = cmd; *p; p++)
  257. *p = tolower(*p);
  258. for(t = cmdtab; t->name; t++)
  259. if(strcmp(cmd, t->name) == 0){
  260. if(t->needlogin && !loggedin)
  261. sodoff();
  262. else if((*t->f)(arg) < 0)
  263. exits(0);
  264. break;
  265. }
  266. if(t->f != restartcmd){
  267. /*
  268. * the file offset is set to zero following
  269. * all commands except the restart command
  270. */
  271. offset = 0;
  272. }
  273. if(t->name == 0){
  274. /*
  275. * the OOB bytes preceding an abort from UCB machines
  276. * comes out as something unrecognizable instead of
  277. * IAC's. Certainly a Plan 9 bug but I can't find it.
  278. * This is a major hack to avoid the problem. -- presotto
  279. */
  280. i = strlen(cmd);
  281. if(i > 4 && strcmp(cmd+i-4, "abor") == 0){
  282. abortcmd(0);
  283. } else{
  284. logit("%s (%s) command not implemented", cmd, arg?arg:"");
  285. reply("502 %s command not implemented", cmd);
  286. }
  287. }
  288. alarm(Maxwait);
  289. }
  290. if(pid)
  291. postnote(PNPROC, pid, "kill");
  292. }
  293. /*
  294. * reply to a command
  295. */
  296. int
  297. reply(char *fmt, ...)
  298. {
  299. va_list arg;
  300. char buf[8192], *s;
  301. va_start(arg, fmt);
  302. s = vseprint(buf, buf+sizeof(buf)-3, fmt, arg);
  303. va_end(arg);
  304. if(debug){
  305. *s = 0;
  306. logit("%s", buf);
  307. }
  308. *s++ = '\r';
  309. *s++ = '\n';
  310. write(1, buf, s - buf);
  311. return 0;
  312. }
  313. int
  314. sodoff(void)
  315. {
  316. return reply("530 Sod off, service requires login");
  317. }
  318. /*
  319. * run a command in a separate process
  320. */
  321. int
  322. asproc(void (*f)(char*, int), char *arg, int arg2)
  323. {
  324. int i;
  325. if(pid){
  326. /* wait for previous command to finish */
  327. for(;;){
  328. i = waitpid();
  329. if(i == pid || i < 0)
  330. break;
  331. }
  332. }
  333. switch(pid = rfork(RFFDG|RFPROC|RFNOTEG)){
  334. case -1:
  335. return reply("450 Out of processes: %r");
  336. case 0:
  337. (*f)(arg, arg2);
  338. exits(0);
  339. default:
  340. break;
  341. }
  342. return 0;
  343. }
  344. /*
  345. * run a command to filter a tail
  346. */
  347. int
  348. transfer(char *cmd, char *a1, char *a2, char *a3, int image)
  349. {
  350. int n, dfd, fd, bytes, eofs, pid;
  351. int pfd[2];
  352. char buf[Nbuf], *p;
  353. Waitmsg *w;
  354. reply("150 Opening data connection for %s (%s)", cmd, data);
  355. dfd = dialdata();
  356. if(dfd < 0)
  357. return reply("425 Error opening data connection: %r");
  358. if(pipe(pfd) < 0)
  359. return reply("520 Internal Error: %r");
  360. bytes = 0;
  361. switch(pid = rfork(RFFDG|RFPROC|RFNAMEG)){
  362. case -1:
  363. return reply("450 Out of processes: %r");
  364. case 0:
  365. logit("running %s %s %s %s pid %d",
  366. cmd, a1?a1:"", a2?a2:"" , a3?a3:"",getpid());
  367. close(pfd[1]);
  368. close(dfd);
  369. dup(pfd[0], 1);
  370. dup(pfd[0], 2);
  371. if(isnone){
  372. fd = open("#s/boot", ORDWR);
  373. if(fd < 0
  374. || bind("#/", "/", MAFTER) < 0
  375. || amount(fd, "/bin", MREPL, "") < 0
  376. || bind("#c", "/dev", MAFTER) < 0
  377. || bind(bindir, "/bin", MREPL) < 0)
  378. exits("building name space");
  379. close(fd);
  380. }
  381. execl(cmd, cmd, a1, a2, a3, nil);
  382. exits(cmd);
  383. default:
  384. close(pfd[0]);
  385. eofs = 0;
  386. while((n = read(pfd[1], buf, sizeof buf)) >= 0){
  387. if(n == 0){
  388. if(eofs++ > 5)
  389. break;
  390. else
  391. continue;
  392. }
  393. eofs = 0;
  394. p = buf;
  395. if(offset > 0){
  396. if(n > offset){
  397. p = buf+offset;
  398. n -= offset;
  399. offset = 0;
  400. } else {
  401. offset -= n;
  402. continue;
  403. }
  404. }
  405. if(!image)
  406. n = crlfwrite(dfd, p, n);
  407. else
  408. n = write(dfd, p, n);
  409. if(n < 0){
  410. postnote(PNPROC, pid, "kill");
  411. bytes = -1;
  412. break;
  413. }
  414. bytes += n;
  415. }
  416. close(pfd[1]);
  417. close(dfd);
  418. break;
  419. }
  420. /* wait for this command to finish */
  421. for(;;){
  422. w = wait();
  423. if(w == nil || w->pid == pid)
  424. break;
  425. free(w);
  426. }
  427. if(w != nil && w->msg != nil && w->msg[0] != 0){
  428. bytes = -1;
  429. logit("%s", w->msg);
  430. logit("%s %s %s %s failed %s", cmd, a1?a1:"", a2?a2:"" , a3?a3:"", w->msg);
  431. }
  432. free(w);
  433. reply("226 Transfer complete");
  434. return bytes;
  435. }
  436. /*
  437. * just reply OK
  438. */
  439. int
  440. nopcmd(char *arg)
  441. {
  442. USED(arg);
  443. reply("510 Plan 9 FTP daemon still alive");
  444. return 0;
  445. }
  446. /*
  447. * login as user
  448. */
  449. int
  450. loginuser(char *user, char *nsfile, int gotoslash)
  451. {
  452. logit("login %s %s %s %s", user, mailaddr, nci->rsys, nsfile);
  453. if(nsfile != nil && newns(user, nsfile) < 0){
  454. logit("namespace file %s does not exist", nsfile);
  455. return reply("530 Not logged in: login out of service");
  456. }
  457. getwd(curdir, sizeof(curdir));
  458. if(gotoslash){
  459. chdir("/");
  460. strcpy(curdir, "/");
  461. }
  462. putenv("service", "ftp");
  463. loggedin = 1;
  464. if(debug == 0)
  465. reply("230- If you have problems, send mail to 'postmaster'.");
  466. return reply("230 Logged in");
  467. }
  468. static void
  469. slowdown(void)
  470. {
  471. static ulong pause;
  472. if (pause) {
  473. sleep(pause); /* deter guessers */
  474. if (pause < (1UL << 20))
  475. pause *= 2;
  476. } else
  477. pause = 1000;
  478. }
  479. /*
  480. * get a user id, reply with a challenge. The users 'anonymous'
  481. * and 'ftp' are equivalent to 'none'. The user 'none' requires
  482. * no challenge.
  483. */
  484. int
  485. usercmd(char *name)
  486. {
  487. slowdown();
  488. logit("user %s %s", name, nci->rsys);
  489. if(loggedin)
  490. return reply("530 Already logged in as %s", user);
  491. if(name == 0 || *name == 0)
  492. return reply("530 user command needs user name");
  493. isnoworld = 0;
  494. if(*name == ':'){
  495. debug = 1;
  496. name++;
  497. }
  498. strncpy(user, name, sizeof(user));
  499. if(debug)
  500. logit("debugging");
  501. user[sizeof(user)-1] = 0;
  502. if(strcmp(user, "anonymous") == 0 || strcmp(user, "ftp") == 0)
  503. strcpy(user, "none");
  504. else if(anon_everybody)
  505. strcpy(user,"none");
  506. if(strcmp(user, "Administrator") == 0 || strcmp(user, "admin") == 0)
  507. return reply("530 go away, script kiddie");
  508. else if(strcmp(user, "*none") == 0){
  509. if(!anon_ok)
  510. return reply("530 Not logged in: anonymous disallowed");
  511. return loginuser("none", namespace, 1);
  512. }
  513. else if(strcmp(user, "none") == 0){
  514. if(!anon_ok)
  515. return reply("530 Not logged in: anonymous disallowed");
  516. return reply("331 Send email address as password");
  517. }
  518. else if(anon_only)
  519. return reply("530 Not logged in: anonymous access only");
  520. isnoworld = noworld(name);
  521. if(isnoworld)
  522. return reply("331 OK");
  523. /* consult the auth server */
  524. if(ch)
  525. auth_freechal(ch);
  526. if((ch = auth_challenge("proto=p9cr role=server user=%q", user)) == nil)
  527. return reply("421 %r");
  528. return reply("331 encrypt challenge, %s, as a password", ch->chal);
  529. }
  530. /*
  531. * get a password, set up user if it works.
  532. */
  533. int
  534. passcmd(char *response)
  535. {
  536. char namefile[128];
  537. AuthInfo *ai;
  538. if(response == nil)
  539. response = "";
  540. if(strcmp(user, "none") == 0 || strcmp(user, "*none") == 0){
  541. /* for none, accept anything as a password */
  542. isnone = 1;
  543. strncpy(mailaddr, response, sizeof(mailaddr)-1);
  544. return loginuser("none", namespace, 1);
  545. }
  546. if(isnoworld){
  547. /* noworld gets a password in the clear */
  548. if(login(user, response, "/lib/namespace.noworld") < 0)
  549. return reply("530 Not logged in");
  550. createperm = 0664;
  551. /* login has already setup the namespace */
  552. return loginuser(user, nil, 0);
  553. } else {
  554. /* for everyone else, do challenge response */
  555. if(ch == nil)
  556. return reply("531 Send user id before encrypted challenge");
  557. ch->resp = response;
  558. ch->nresp = strlen(response);
  559. ai = auth_response(ch);
  560. if(ai == nil || auth_chuid(ai, nil) < 0) {
  561. slowdown();
  562. return reply("530 Not logged in: %r");
  563. }
  564. auth_freechal(ch);
  565. ch = nil;
  566. /* if the user has specified a namespace for ftp, use it */
  567. snprint(namefile, sizeof(namefile), "/usr/%s/lib/namespace.ftp", user);
  568. strcpy(mailaddr, user);
  569. createperm = 0660;
  570. if(access(namefile, 0) == 0)
  571. return loginuser(user, namefile, 0);
  572. else
  573. return loginuser(user, "/lib/namespace", 0);
  574. }
  575. }
  576. /*
  577. * print working directory
  578. */
  579. int
  580. pwdcmd(char *arg)
  581. {
  582. if(arg)
  583. return reply("550 Pwd takes no argument");
  584. return reply("257 \"%s\" is the current directory", curdir);
  585. }
  586. /*
  587. * chdir
  588. */
  589. int
  590. cwdcmd(char *dir)
  591. {
  592. char *rp;
  593. char buf[Maxpath];
  594. /* shell cd semantics */
  595. if(dir == 0 || *dir == 0){
  596. if(isnone)
  597. rp = "/";
  598. else {
  599. snprint(buf, sizeof buf, "/usr/%s", user);
  600. rp = buf;
  601. }
  602. if(accessok(rp) == 0)
  603. rp = nil;
  604. } else
  605. rp = abspath(dir);
  606. if(rp == nil)
  607. return reply("550 Permission denied");
  608. if(chdir(rp) < 0)
  609. return reply("550 Cwd failed: %r");
  610. strcpy(curdir, rp);
  611. return reply("250 directory changed to %s", curdir);
  612. }
  613. /*
  614. * chdir ..
  615. */
  616. int
  617. cdupcmd(char *dp)
  618. {
  619. USED(dp);
  620. return cwdcmd("..");
  621. }
  622. int
  623. quitcmd(char *arg)
  624. {
  625. USED(arg);
  626. reply("200 Bye");
  627. if(pid)
  628. postnote(PNPROC, pid, "kill");
  629. return -1;
  630. }
  631. int
  632. typecmd(char *arg)
  633. {
  634. int c;
  635. char *x;
  636. x = arg;
  637. if(arg == 0)
  638. return reply("501 Type command needs arguments");
  639. while(c = *arg++){
  640. switch(tolower(c)){
  641. case 'a':
  642. type = Tascii;
  643. break;
  644. case 'i':
  645. case 'l':
  646. type = Timage;
  647. break;
  648. case '8':
  649. case ' ':
  650. case 'n':
  651. case 't':
  652. case 'c':
  653. break;
  654. default:
  655. return reply("501 Unimplemented type %s", x);
  656. }
  657. }
  658. return reply("200 Type %s", type==Tascii ? "Ascii" : "Image");
  659. }
  660. int
  661. modecmd(char *arg)
  662. {
  663. if(arg == 0)
  664. return reply("501 Mode command needs arguments");
  665. while(*arg){
  666. switch(tolower(*arg)){
  667. case 's':
  668. mode = Mstream;
  669. break;
  670. default:
  671. return reply("501 Unimplemented mode %c", *arg);
  672. }
  673. arg++;
  674. }
  675. return reply("200 Stream mode");
  676. }
  677. int
  678. structcmd(char *arg)
  679. {
  680. if(arg == 0)
  681. return reply("501 Struct command needs arguments");
  682. for(; *arg; arg++){
  683. switch(tolower(*arg)){
  684. case 'f':
  685. structure = Sfile;
  686. break;
  687. default:
  688. return reply("501 Unimplemented structure %c", *arg);
  689. }
  690. }
  691. return reply("200 File structure");
  692. }
  693. int
  694. portcmd(char *arg)
  695. {
  696. char *field[7];
  697. int n;
  698. if(arg == 0)
  699. return reply("501 Port command needs arguments");
  700. n = getfields(arg, field, 7, 0, ", ");
  701. if(n != 6)
  702. return reply("501 Incorrect port specification");
  703. snprint(data, sizeof data, "tcp!%.3s.%.3s.%.3s.%.3s!%d", field[0], field[1], field[2],
  704. field[3], atoi(field[4])*256 + atoi(field[5]));
  705. return reply("200 Data port is %s", data);
  706. }
  707. int
  708. mountnet(void)
  709. {
  710. int rv;
  711. rv = 0;
  712. if(bind("#/", "/", MAFTER) < 0){
  713. logit("can't bind #/ to /: %r");
  714. return reply("500 can't bind #/ to /: %r");
  715. }
  716. if(bind(nci->spec, "/net", MBEFORE) < 0){
  717. logit("can't bind %s to /net: %r", nci->spec);
  718. rv = reply("500 can't bind %s to /net: %r", nci->spec);
  719. unmount("#/", "/");
  720. }
  721. return rv;
  722. }
  723. void
  724. unmountnet(void)
  725. {
  726. unmount(0, "/net");
  727. unmount("#/", "/");
  728. }
  729. int
  730. pasvcmd(char *arg)
  731. {
  732. NetConnInfo *nnci;
  733. Passive *p;
  734. USED(arg);
  735. p = &passive;
  736. if(p->inuse){
  737. close(p->afd);
  738. p->inuse = 0;
  739. }
  740. if(mountnet() < 0)
  741. return 0;
  742. p->afd = announce("tcp!*!0", passive.adir);
  743. if(p->afd < 0){
  744. unmountnet();
  745. return reply("500 No free ports");
  746. }
  747. nnci = getnetconninfo(p->adir, -1);
  748. unmountnet();
  749. /* parse the local address */
  750. if(debug)
  751. logit("local sys is %s", nci->lsys);
  752. parseip(p->ipaddr, nci->lsys);
  753. if(ipcmp(p->ipaddr, v4prefix) == 0 || ipcmp(p->ipaddr, IPnoaddr) == 0)
  754. parseip(p->ipaddr, nci->lsys);
  755. p->port = atoi(nnci->lserv);
  756. freenetconninfo(nnci);
  757. p->inuse = 1;
  758. return reply("227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)",
  759. p->ipaddr[IPv4off+0], p->ipaddr[IPv4off+1], p->ipaddr[IPv4off+2], p->ipaddr[IPv4off+3],
  760. p->port>>8, p->port&0xff);
  761. }
  762. enum
  763. {
  764. Narg=32,
  765. };
  766. int Cflag, rflag, tflag, Rflag;
  767. int maxnamelen;
  768. int col;
  769. char*
  770. mode2asc(int m)
  771. {
  772. static char asc[12];
  773. char *p;
  774. strcpy(asc, "----------");
  775. if(DMDIR & m)
  776. asc[0] = 'd';
  777. if(DMAPPEND & m)
  778. asc[0] = 'a';
  779. else if(DMEXCL & m)
  780. asc[3] = 'l';
  781. for(p = asc+1; p < asc + 10; p += 3, m<<=3){
  782. if(m & 0400)
  783. p[0] = 'r';
  784. if(m & 0200)
  785. p[1] = 'w';
  786. if(m & 0100)
  787. p[2] = 'x';
  788. }
  789. return asc;
  790. }
  791. void
  792. listfile(Biobufhdr *b, char *name, int lflag, char *dname)
  793. {
  794. char ts[32];
  795. int n, links, pad;
  796. long now;
  797. char *x;
  798. Dir *d;
  799. x = abspath(name);
  800. if(x == nil)
  801. return;
  802. d = dirstat(x);
  803. if(d == nil)
  804. return;
  805. if(isnone){
  806. if(strncmp(x, "/incoming/", sizeof("/incoming/")-1) != 0)
  807. d->mode &= ~0222;
  808. d->uid = "none";
  809. d->gid = "none";
  810. }
  811. strcpy(ts, ctime(d->mtime));
  812. ts[16] = 0;
  813. now = time(0);
  814. if(now - d->mtime > 6*30*24*60*60)
  815. memmove(ts+11, ts+23, 5);
  816. if(lflag){
  817. /* Unix style long listing */
  818. if(DMDIR&d->mode){
  819. links = 2;
  820. d->length = 512;
  821. } else
  822. links = 1;
  823. Bprint(b, "%s %3d %-8s %-8s %7lld %s ",
  824. mode2asc(d->mode), links,
  825. d->uid, d->gid, d->length, ts+4);
  826. }
  827. if(Cflag && maxnamelen < 40){
  828. n = strlen(name);
  829. pad = ((col+maxnamelen)/(maxnamelen+1))*(maxnamelen+1);
  830. if(pad+maxnamelen+1 < 60){
  831. Bprint(b, "%*s", pad-col+n, name);
  832. col = pad+n;
  833. }
  834. else{
  835. Bprint(b, "\r\n%s", name);
  836. col = n;
  837. }
  838. }
  839. else{
  840. if(dname)
  841. Bprint(b, "%s/", dname);
  842. Bprint(b, "%s\r\n", name);
  843. }
  844. free(d);
  845. }
  846. int
  847. dircomp(void *va, void *vb)
  848. {
  849. int rv;
  850. Dir *a, *b;
  851. a = va;
  852. b = vb;
  853. if(tflag)
  854. rv = b->mtime - a->mtime;
  855. else
  856. rv = strcmp(a->name, b->name);
  857. return (rflag?-1:1)*rv;
  858. }
  859. void
  860. listdir(char *name, Biobufhdr *b, int lflag, int *printname, Globlist *gl)
  861. {
  862. Dir *p;
  863. int fd, n, i, l;
  864. char *dname;
  865. uvlong total;
  866. col = 0;
  867. fd = open(name, OREAD);
  868. if(fd < 0){
  869. Bprint(b, "can't read %s: %r\r\n", name);
  870. return;
  871. }
  872. dname = 0;
  873. if(*printname){
  874. if(Rflag || lflag)
  875. Bprint(b, "\r\n%s:\r\n", name);
  876. else
  877. dname = name;
  878. }
  879. n = dirreadall(fd, &p);
  880. close(fd);
  881. if(Cflag){
  882. for(i = 0; i < n; i++){
  883. l = strlen(p[i].name);
  884. if(l > maxnamelen)
  885. maxnamelen = l;
  886. }
  887. }
  888. /* Unix style total line */
  889. if(lflag){
  890. total = 0;
  891. for(i = 0; i < n; i++){
  892. if(p[i].qid.type & QTDIR)
  893. total += 512;
  894. else
  895. total += p[i].length;
  896. }
  897. Bprint(b, "total %ulld\r\n", total/512);
  898. }
  899. qsort(p, n, sizeof(Dir), dircomp);
  900. for(i = 0; i < n; i++){
  901. if(Rflag && (p[i].qid.type & QTDIR)){
  902. *printname = 1;
  903. globadd(gl, name, p[i].name);
  904. }
  905. listfile(b, p[i].name, lflag, dname);
  906. }
  907. free(p);
  908. }
  909. void
  910. list(char *arg, int lflag)
  911. {
  912. Dir *d;
  913. Globlist *gl;
  914. Glob *g;
  915. int dfd, printname;
  916. int i, n, argc;
  917. char *alist[Narg];
  918. char **argv;
  919. Biobufhdr bh;
  920. uchar buf[512];
  921. char *p, *s;
  922. if(arg == 0)
  923. arg = "";
  924. if(debug)
  925. logit("ls %s (. = %s)", arg, curdir);
  926. /* process arguments, understand /bin/ls -l option */
  927. argv = alist;
  928. argv[0] = "/bin/ls";
  929. argc = getfields(arg, argv+1, Narg-2, 1, " \t") + 1;
  930. argv[argc] = 0;
  931. rflag = 0;
  932. tflag = 0;
  933. Rflag = 0;
  934. Cflag = 0;
  935. col = 0;
  936. ARGBEGIN{
  937. case 'l':
  938. lflag++;
  939. break;
  940. case 'R':
  941. Rflag++;
  942. break;
  943. case 'C':
  944. Cflag++;
  945. break;
  946. case 'r':
  947. rflag++;
  948. break;
  949. case 't':
  950. tflag++;
  951. break;
  952. }ARGEND;
  953. if(Cflag)
  954. lflag = 0;
  955. dfd = dialdata();
  956. if(dfd < 0){
  957. reply("425 Error opening data connection:%r");
  958. return;
  959. }
  960. reply("150 Opened data connection (%s)", data);
  961. Binits(&bh, dfd, OWRITE, buf, sizeof(buf));
  962. if(argc == 0){
  963. argc = 1;
  964. argv = alist;
  965. argv[0] = ".";
  966. }
  967. for(i = 0; i < argc; i++){
  968. chdir(curdir);
  969. gl = glob(argv[i]);
  970. if(gl == nil)
  971. continue;
  972. printname = gl->first != nil && gl->first->next != nil;
  973. maxnamelen = 8;
  974. if(Cflag)
  975. for(g = gl->first; g; g = g->next)
  976. if(g->glob && (n = strlen(s_to_c(g->glob))) > maxnamelen)
  977. maxnamelen = n;
  978. while(s = globiter(gl)){
  979. if(debug)
  980. logit("glob %s", s);
  981. p = abspath(s);
  982. if(p == nil){
  983. free(s);
  984. continue;
  985. }
  986. d = dirstat(p);
  987. if(d == nil){
  988. free(s);
  989. continue;
  990. }
  991. if(d->qid.type & QTDIR)
  992. listdir(s, &bh, lflag, &printname, gl);
  993. else
  994. listfile(&bh, s, lflag, 0);
  995. free(s);
  996. free(d);
  997. }
  998. globlistfree(gl);
  999. }
  1000. if(Cflag)
  1001. Bprint(&bh, "\r\n");
  1002. Bflush(&bh);
  1003. close(dfd);
  1004. reply("226 Transfer complete (list %s)", arg);
  1005. }
  1006. int
  1007. namelistcmd(char *arg)
  1008. {
  1009. return asproc(list, arg, 0);
  1010. }
  1011. int
  1012. listcmd(char *arg)
  1013. {
  1014. return asproc(list, arg, 1);
  1015. }
  1016. /*
  1017. * fuse compatability
  1018. */
  1019. int
  1020. oksiteuser(void)
  1021. {
  1022. char buf[64];
  1023. int fd, n;
  1024. fd = open("#c/user", OREAD);
  1025. if(fd < 0)
  1026. return 1;
  1027. n = read(fd, buf, sizeof buf - 1);
  1028. if(n > 0){
  1029. buf[n] = 0;
  1030. if(strcmp(buf, "none") == 0)
  1031. n = -1;
  1032. }
  1033. close(fd);
  1034. return n > 0;
  1035. }
  1036. int
  1037. sitecmd(char *arg)
  1038. {
  1039. char *f[4];
  1040. int nf, r;
  1041. Dir *d;
  1042. if(arg == 0)
  1043. return reply("501 bad site command");
  1044. nf = tokenize(arg, f, nelem(f));
  1045. if(nf != 3 || cistrcmp(f[0], "chmod") != 0)
  1046. return reply("501 bad site command");
  1047. if(!oksiteuser())
  1048. return reply("550 Permission denied");
  1049. d = dirstat(f[2]);
  1050. if(d == nil)
  1051. return reply("501 site chmod: file does not exist");
  1052. d->mode &= ~0777;
  1053. d->mode |= strtoul(f[1], 0, 8) & 0777;
  1054. r = dirwstat(f[2], d);
  1055. free(d);
  1056. if(r < 0)
  1057. return reply("550 Permission denied %r");
  1058. return reply("200 very well, then");
  1059. }
  1060. /*
  1061. * return the size of the file
  1062. */
  1063. int
  1064. sizecmd(char *arg)
  1065. {
  1066. Dir *d;
  1067. int rv;
  1068. if(arg == 0)
  1069. return reply("501 Size command requires pathname");
  1070. arg = abspath(arg);
  1071. d = dirstat(arg);
  1072. if(d == nil)
  1073. return reply("501 %r accessing %s", arg);
  1074. rv = reply("213 %lld", d->length);
  1075. free(d);
  1076. return rv;
  1077. }
  1078. /*
  1079. * return the modify time of the file
  1080. */
  1081. int
  1082. mdtmcmd(char *arg)
  1083. {
  1084. Dir *d;
  1085. Tm *t;
  1086. int rv;
  1087. if(arg == 0)
  1088. return reply("501 Mdtm command requires pathname");
  1089. if(arg == 0)
  1090. return reply("550 Permission denied");
  1091. d = dirstat(arg);
  1092. if(d == nil)
  1093. return reply("501 %r accessing %s", arg);
  1094. t = gmtime(d->mtime);
  1095. rv = reply("213 %4.4d%2.2d%2.2d%2.2d%2.2d%2.2d",
  1096. t->year+1900, t->mon+1, t->mday,
  1097. t->hour, t->min, t->sec);
  1098. free(d);
  1099. return rv;
  1100. }
  1101. /*
  1102. * set an offset to start reading a file from
  1103. * only lasts for one command
  1104. */
  1105. int
  1106. restartcmd(char *arg)
  1107. {
  1108. if(arg == 0)
  1109. return reply("501 Restart command requires offset");
  1110. offset = atoll(arg);
  1111. if(offset < 0){
  1112. offset = 0;
  1113. return reply("501 Bad offset");
  1114. }
  1115. return reply("350 Restarting at %lld. Send STORE or RETRIEVE", offset);
  1116. }
  1117. /*
  1118. * send a file to the user
  1119. */
  1120. int
  1121. crlfwrite(int fd, char *p, int n)
  1122. {
  1123. char *ep, *np;
  1124. char buf[2*Nbuf];
  1125. for(np = buf, ep = p + n; p < ep; p++){
  1126. if(*p == '\n')
  1127. *np++ = '\r';
  1128. *np++ = *p;
  1129. }
  1130. if(write(fd, buf, np - buf) == np - buf)
  1131. return n;
  1132. else
  1133. return -1;
  1134. }
  1135. void
  1136. retrievedir(char *arg)
  1137. {
  1138. int n;
  1139. char *p;
  1140. String *file;
  1141. if(type != Timage){
  1142. reply("550 This file requires type binary/image");
  1143. return;
  1144. }
  1145. file = s_copy(arg);
  1146. p = strrchr(s_to_c(file), '/');
  1147. if(p != s_to_c(file)){
  1148. *p++ = 0;
  1149. chdir(s_to_c(file));
  1150. } else {
  1151. chdir("/");
  1152. p = s_to_c(file)+1;
  1153. }
  1154. n = transfer("/bin/tar", "c", p, 0, 1);
  1155. if(n < 0)
  1156. logit("get %s failed", arg);
  1157. else
  1158. logit("get %s OK %d", arg, n);
  1159. s_free(file);
  1160. }
  1161. void
  1162. retrieve(char *arg, int arg2)
  1163. {
  1164. int dfd, fd, n, i, bytes;
  1165. Dir *d;
  1166. char buf[Nbuf];
  1167. char *p, *ep;
  1168. USED(arg2);
  1169. p = strchr(arg, '\r');
  1170. if(p){
  1171. logit("cr in file name", arg);
  1172. *p = 0;
  1173. }
  1174. fd = open(arg, OREAD);
  1175. if(fd == -1){
  1176. n = strlen(arg);
  1177. if(n > 4 && strcmp(arg+n-4, ".tar") == 0){
  1178. *(arg+n-4) = 0;
  1179. d = dirstat(arg);
  1180. if(d != nil){
  1181. if(d->qid.type & QTDIR){
  1182. retrievedir(arg);
  1183. free(d);
  1184. return;
  1185. }
  1186. free(d);
  1187. }
  1188. }
  1189. logit("get %s failed", arg);
  1190. reply("550 Error opening %s: %r", arg);
  1191. return;
  1192. }
  1193. if(offset != 0)
  1194. if(seek(fd, offset, 0) < 0){
  1195. reply("550 %s: seek to %lld failed", arg, offset);
  1196. close(fd);
  1197. return;
  1198. }
  1199. d = dirfstat(fd);
  1200. if(d != nil){
  1201. if(d->qid.type & QTDIR){
  1202. reply("550 %s: not a plain file.", arg);
  1203. close(fd);
  1204. free(d);
  1205. return;
  1206. }
  1207. free(d);
  1208. }
  1209. n = read(fd, buf, sizeof(buf));
  1210. if(n < 0){
  1211. logit("get %s failed", arg, mailaddr, nci->rsys);
  1212. reply("550 Error reading %s: %r", arg);
  1213. close(fd);
  1214. return;
  1215. }
  1216. if(type != Timage)
  1217. for(p = buf, ep = &buf[n]; p < ep; p++)
  1218. if(*p & 0x80){
  1219. close(fd);
  1220. reply("550 This file requires type binary/image");
  1221. return;
  1222. }
  1223. reply("150 Opening data connection for %s (%s)", arg, data);
  1224. dfd = dialdata();
  1225. if(dfd < 0){
  1226. reply("425 Error opening data connection:%r");
  1227. close(fd);
  1228. return;
  1229. }
  1230. bytes = 0;
  1231. do {
  1232. switch(type){
  1233. case Timage:
  1234. i = write(dfd, buf, n);
  1235. break;
  1236. default:
  1237. i = crlfwrite(dfd, buf, n);
  1238. break;
  1239. }
  1240. if(i != n){
  1241. close(fd);
  1242. close(dfd);
  1243. logit("get %s %r to data connection after %d", arg, bytes);
  1244. reply("550 Error writing to data connection: %r");
  1245. return;
  1246. }
  1247. bytes += n;
  1248. } while((n = read(fd, buf, sizeof(buf))) > 0);
  1249. if(n < 0)
  1250. logit("get %s %r after %d", arg, bytes);
  1251. close(fd);
  1252. close(dfd);
  1253. reply("226 Transfer complete");
  1254. logit("get %s OK %d", arg, bytes);
  1255. }
  1256. int
  1257. retrievecmd(char *arg)
  1258. {
  1259. if(arg == 0)
  1260. return reply("501 Retrieve command requires an argument");
  1261. arg = abspath(arg);
  1262. if(arg == 0)
  1263. return reply("550 Permission denied");
  1264. return asproc(retrieve, arg, 0);
  1265. }
  1266. /*
  1267. * get a file from the user
  1268. */
  1269. int
  1270. lfwrite(int fd, char *p, int n)
  1271. {
  1272. char *ep, *np;
  1273. char buf[Nbuf];
  1274. for(np = buf, ep = p + n; p < ep; p++){
  1275. if(*p != '\r')
  1276. *np++ = *p;
  1277. }
  1278. if(write(fd, buf, np - buf) == np - buf)
  1279. return n;
  1280. else
  1281. return -1;
  1282. }
  1283. void
  1284. store(char *arg, int fd)
  1285. {
  1286. int dfd, n, i;
  1287. char buf[Nbuf];
  1288. reply("150 Opening data connection for %s (%s)", arg, data);
  1289. dfd = dialdata();
  1290. if(dfd < 0){
  1291. reply("425 Error opening data connection:%r");
  1292. close(fd);
  1293. return;
  1294. }
  1295. while((n = read(dfd, buf, sizeof(buf))) > 0){
  1296. switch(type){
  1297. case Timage:
  1298. i = write(fd, buf, n);
  1299. break;
  1300. default:
  1301. i = lfwrite(fd, buf, n);
  1302. break;
  1303. }
  1304. if(i != n){
  1305. close(fd);
  1306. close(dfd);
  1307. reply("550 Error writing file");
  1308. return;
  1309. }
  1310. }
  1311. close(fd);
  1312. close(dfd);
  1313. logit("put %s OK", arg);
  1314. reply("226 Transfer complete");
  1315. }
  1316. int
  1317. storecmd(char *arg)
  1318. {
  1319. int fd, rv;
  1320. if(arg == 0)
  1321. return reply("501 Store command requires an argument");
  1322. arg = abspath(arg);
  1323. if(arg == 0)
  1324. return reply("550 Permission denied");
  1325. if(isnone && strncmp(arg, "/incoming/", sizeof("/incoming/")-1))
  1326. return reply("550 Permission denied");
  1327. if(offset){
  1328. fd = open(arg, OWRITE);
  1329. if(fd == -1)
  1330. return reply("550 Error opening %s: %r", arg);
  1331. if(seek(fd, offset, 0) == -1)
  1332. return reply("550 Error seeking %s to %d: %r",
  1333. arg, offset);
  1334. } else {
  1335. fd = create(arg, OWRITE, createperm);
  1336. if(fd == -1)
  1337. return reply("550 Error creating %s: %r", arg);
  1338. }
  1339. rv = asproc(store, arg, fd);
  1340. close(fd);
  1341. return rv;
  1342. }
  1343. int
  1344. appendcmd(char *arg)
  1345. {
  1346. int fd, rv;
  1347. if(arg == 0)
  1348. return reply("501 Append command requires an argument");
  1349. if(isnone)
  1350. return reply("550 Permission denied");
  1351. arg = abspath(arg);
  1352. if(arg == 0)
  1353. return reply("550 Error creating %s: Permission denied", arg);
  1354. fd = open(arg, OWRITE);
  1355. if(fd == -1){
  1356. fd = create(arg, OWRITE, createperm);
  1357. if(fd == -1)
  1358. return reply("550 Error creating %s: %r", arg);
  1359. }
  1360. seek(fd, 0, 2);
  1361. rv = asproc(store, arg, fd);
  1362. close(fd);
  1363. return rv;
  1364. }
  1365. int
  1366. storeucmd(char *arg)
  1367. {
  1368. int fd, rv;
  1369. char name[Maxpath];
  1370. USED(arg);
  1371. if(isnone)
  1372. return reply("550 Permission denied");
  1373. strncpy(name, "ftpXXXXXXXXXXX", sizeof name);
  1374. mktemp(name);
  1375. fd = create(name, OWRITE, createperm);
  1376. if(fd == -1)
  1377. return reply("550 Error creating %s: %r", name);
  1378. rv = asproc(store, name, fd);
  1379. close(fd);
  1380. return rv;
  1381. }
  1382. int
  1383. mkdircmd(char *name)
  1384. {
  1385. int fd;
  1386. if(name == 0)
  1387. return reply("501 Mkdir command requires an argument");
  1388. if(isnone)
  1389. return reply("550 Permission denied");
  1390. name = abspath(name);
  1391. if(name == 0)
  1392. return reply("550 Permission denied");
  1393. fd = create(name, OREAD, DMDIR|0775);
  1394. if(fd < 0)
  1395. return reply("550 Can't create %s: %r", name);
  1396. close(fd);
  1397. return reply("226 %s created", name);
  1398. }
  1399. int
  1400. delcmd(char *name)
  1401. {
  1402. if(name == 0)
  1403. return reply("501 Rmdir/delete command requires an argument");
  1404. if(isnone)
  1405. return reply("550 Permission denied");
  1406. name = abspath(name);
  1407. if(name == 0)
  1408. return reply("550 Permission denied");
  1409. if(remove(name) < 0)
  1410. return reply("550 Can't remove %s: %r", name);
  1411. else
  1412. return reply("226 %s removed", name);
  1413. }
  1414. /*
  1415. * kill off the last transfer (if the process still exists)
  1416. */
  1417. int
  1418. abortcmd(char *arg)
  1419. {
  1420. USED(arg);
  1421. logit("abort pid %d", pid);
  1422. if(pid){
  1423. if(postnote(PNPROC, pid, "kill") == 0)
  1424. reply("426 Command aborted");
  1425. else
  1426. logit("postnote pid %d %r", pid);
  1427. }
  1428. return reply("226 Abort processed");
  1429. }
  1430. int
  1431. systemcmd(char *arg)
  1432. {
  1433. USED(arg);
  1434. return reply("215 UNIX Type: L8 Version: Plan 9");
  1435. }
  1436. int
  1437. helpcmd(char *arg)
  1438. {
  1439. int i;
  1440. char buf[80];
  1441. char *p, *e;
  1442. USED(arg);
  1443. reply("214- the following commands are implemented:");
  1444. p = buf;
  1445. e = buf+sizeof buf;
  1446. for(i = 0; cmdtab[i].name; i++){
  1447. if((i%8) == 0){
  1448. reply("214-%s", buf);
  1449. p = buf;
  1450. }
  1451. p = seprint(p, e, " %-5.5s", cmdtab[i].name);
  1452. }
  1453. if(p != buf)
  1454. reply("214-%s", buf);
  1455. reply("214 ");
  1456. return 0;
  1457. }
  1458. /*
  1459. * renaming a file takes two commands
  1460. */
  1461. static String *filepath;
  1462. int
  1463. rnfrcmd(char *from)
  1464. {
  1465. if(isnone)
  1466. return reply("550 Permission denied");
  1467. if(from == 0)
  1468. return reply("501 Rename command requires an argument");
  1469. from = abspath(from);
  1470. if(from == 0)
  1471. return reply("550 Permission denied");
  1472. if(filepath == nil)
  1473. filepath = s_copy(from);
  1474. else{
  1475. s_reset(filepath);
  1476. s_append(filepath, from);
  1477. }
  1478. return reply("350 Rename %s to ...", s_to_c(filepath));
  1479. }
  1480. int
  1481. rntocmd(char *to)
  1482. {
  1483. int r;
  1484. Dir nd;
  1485. char *fp, *tp;
  1486. if(isnone)
  1487. return reply("550 Permission denied");
  1488. if(to == 0)
  1489. return reply("501 Rename command requires an argument");
  1490. to = abspath(to);
  1491. if(to == 0)
  1492. return reply("550 Permission denied");
  1493. if(filepath == nil || *(s_to_c(filepath)) == 0)
  1494. return reply("503 Rnto must be preceeded by an rnfr");
  1495. tp = strrchr(to, '/');
  1496. fp = strrchr(s_to_c(filepath), '/');
  1497. if((tp && fp == 0) || (fp && tp == 0)
  1498. || (fp && tp && (fp-s_to_c(filepath) != tp-to || memcmp(s_to_c(filepath), to, tp-to))))
  1499. return reply("550 Rename can't change directory");
  1500. if(tp)
  1501. to = tp+1;
  1502. nulldir(&nd);
  1503. nd.name = to;
  1504. if(dirwstat(s_to_c(filepath), &nd) < 0)
  1505. r = reply("550 Can't rename %s to %s: %r\n", s_to_c(filepath), to);
  1506. else
  1507. r = reply("250 %s now %s", s_to_c(filepath), to);
  1508. s_reset(filepath);
  1509. return r;
  1510. }
  1511. /*
  1512. * to dial out we need the network file system in our
  1513. * name space.
  1514. */
  1515. int
  1516. dialdata(void)
  1517. {
  1518. int fd, cfd;
  1519. char ldir[40];
  1520. char err[Maxerr];
  1521. if(mountnet() < 0)
  1522. return -1;
  1523. if(!passive.inuse){
  1524. fd = dial(data, "20", 0, 0);
  1525. errstr(err, sizeof err);
  1526. } else {
  1527. alarm(5*60*1000);
  1528. cfd = listen(passive.adir, ldir);
  1529. alarm(0);
  1530. errstr(err, sizeof err);
  1531. if(cfd < 0)
  1532. return -1;
  1533. fd = accept(cfd, ldir);
  1534. errstr(err, sizeof err);
  1535. close(cfd);
  1536. }
  1537. if(fd < 0)
  1538. logit("can't dial %s: %s", data, err);
  1539. unmountnet();
  1540. werrstr(err, sizeof err);
  1541. return fd;
  1542. }
  1543. int
  1544. postnote(int group, int pid, char *note)
  1545. {
  1546. char file[128];
  1547. int f, r;
  1548. /*
  1549. * Use #p because /proc may not be in the namespace.
  1550. */
  1551. switch(group) {
  1552. case PNPROC:
  1553. sprint(file, "#p/%d/note", pid);
  1554. break;
  1555. case PNGROUP:
  1556. sprint(file, "#p/%d/notepg", pid);
  1557. break;
  1558. default:
  1559. return -1;
  1560. }
  1561. f = open(file, OWRITE);
  1562. if(f < 0)
  1563. return -1;
  1564. r = strlen(note);
  1565. if(write(f, note, r) != r) {
  1566. close(f);
  1567. return -1;
  1568. }
  1569. close(f);
  1570. return 0;
  1571. }
  1572. /*
  1573. * to circumscribe the accessible files we have to eliminate ..'s
  1574. * and resolve all names from the root. We also remove any /bin/rc
  1575. * special characters to avoid later problems with executed commands.
  1576. */
  1577. char *special = "`;| ";
  1578. char*
  1579. abspath(char *origpath)
  1580. {
  1581. char *p, *sp, *path;
  1582. static String *rpath;
  1583. if(rpath == nil)
  1584. rpath = s_new();
  1585. else
  1586. s_reset(rpath);
  1587. if(origpath == nil)
  1588. s_append(rpath, curdir);
  1589. else{
  1590. if(*origpath != '/'){
  1591. s_append(rpath, curdir);
  1592. s_append(rpath, "/");
  1593. }
  1594. s_append(rpath, origpath);
  1595. }
  1596. path = s_to_c(rpath);
  1597. for(sp = special; *sp; sp++){
  1598. p = strchr(path, *sp);
  1599. if(p)
  1600. *p = 0;
  1601. }
  1602. cleanname(s_to_c(rpath));
  1603. rpath->ptr = rpath->base+strlen(rpath->base);
  1604. if(!accessok(s_to_c(rpath)))
  1605. return nil;
  1606. return s_to_c(rpath);
  1607. }
  1608. typedef struct Path Path;
  1609. struct Path {
  1610. Path *next;
  1611. String *path;
  1612. int inuse;
  1613. int ok;
  1614. };
  1615. enum
  1616. {
  1617. Maxlevel = 16,
  1618. Maxperlevel= 8,
  1619. };
  1620. Path *pathlevel[Maxlevel];
  1621. Path*
  1622. unlinkpath(char *path, int level)
  1623. {
  1624. String *s;
  1625. Path **l, *p;
  1626. int n;
  1627. n = 0;
  1628. for(l = &pathlevel[level]; *l; l = &(*l)->next){
  1629. p = *l;
  1630. /* hit */
  1631. if(strcmp(s_to_c(p->path), path) == 0){
  1632. *l = p->next;
  1633. p->next = nil;
  1634. return p;
  1635. }
  1636. /* reuse */
  1637. if(++n >= Maxperlevel){
  1638. *l = p->next;
  1639. s = p->path;
  1640. s_reset(p->path);
  1641. memset(p, 0, sizeof *p);
  1642. p->path = s_append(s, path);
  1643. return p;
  1644. }
  1645. }
  1646. /* allocate */
  1647. p = mallocz(sizeof *p, 1);
  1648. p->path = s_copy(path);
  1649. return p;
  1650. }
  1651. void
  1652. linkpath(Path *p, int level)
  1653. {
  1654. p->next = pathlevel[level];
  1655. pathlevel[level] = p;
  1656. p->inuse = 1;
  1657. }
  1658. void
  1659. addpath(Path *p, int level, int ok)
  1660. {
  1661. p->ok = ok;
  1662. p->next = pathlevel[level];
  1663. pathlevel[level] = p;
  1664. }
  1665. int
  1666. _accessok(String *s, int level)
  1667. {
  1668. Path *p;
  1669. char *cp;
  1670. int lvl, offset;
  1671. static char httplogin[] = "/.httplogin";
  1672. if(level < 0)
  1673. return 1;
  1674. lvl = level;
  1675. if(lvl >= Maxlevel)
  1676. lvl = Maxlevel - 1;
  1677. p = unlinkpath(s_to_c(s), lvl);
  1678. if(p->inuse){
  1679. /* move to front */
  1680. linkpath(p, lvl);
  1681. return p->ok;
  1682. }
  1683. cp = strrchr(s_to_c(s), '/');
  1684. if(cp == nil)
  1685. offset = 0;
  1686. else
  1687. offset = cp - s_to_c(s);
  1688. s_append(s, httplogin);
  1689. if(access(s_to_c(s), AEXIST) == 0){
  1690. addpath(p, lvl, 0);
  1691. return 0;
  1692. }
  1693. /*
  1694. * There's no way to shorten a String without
  1695. * knowing the implementation.
  1696. */
  1697. s->ptr = s->base+offset;
  1698. s_terminate(s);
  1699. addpath(p, lvl, _accessok(s, level-1));
  1700. return p->ok;
  1701. }
  1702. /*
  1703. * check for a subdirectory containing .httplogin
  1704. * at each level of the path.
  1705. */
  1706. int
  1707. accessok(char *path)
  1708. {
  1709. int level, r;
  1710. char *p;
  1711. String *npath;
  1712. npath = s_copy(path);
  1713. p = s_to_c(npath)+1;
  1714. for(level = 1; level < Maxlevel; level++){
  1715. p = strchr(p, '/');
  1716. if(p == nil)
  1717. break;
  1718. p++;
  1719. }
  1720. r = _accessok(npath, level-1);
  1721. s_free(npath);
  1722. return r;
  1723. }