ftpd.c 33 KB

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