telnet.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "telnet.h"
  5. int ctl = -1; /* control fd (for break's) */
  6. int consctl = -1; /* consctl fd */
  7. int ttypid; /* pid's if the 2 processes (used to kill them) */
  8. int netpid;
  9. int interrupted;
  10. int localecho;
  11. int notkbd;
  12. static char *srv;
  13. typedef struct Comm Comm;
  14. struct Comm {
  15. int returns;
  16. int stopped;
  17. };
  18. Comm *comm;
  19. int dodial(char*);
  20. void fromkbd(int);
  21. void fromnet(int);
  22. int menu(Biobuf*, int);
  23. void notifyf(void*, char*);
  24. void rawoff(void);
  25. void rawon(void);
  26. void telnet(int);
  27. char* system(int, char*);
  28. int echochange(Biobuf*, int);
  29. int termsub(Biobuf*, uchar*, int);
  30. int xlocsub(Biobuf*, uchar*, int);
  31. void* share(ulong);
  32. static int islikeatty(int);
  33. void
  34. usage(void)
  35. {
  36. fatal("usage: telnet [-Cdnr] [-s srv] net!host[!service]", 0, 0);
  37. }
  38. void
  39. main(int argc, char *argv[])
  40. {
  41. int returns;
  42. returns = 1;
  43. ARGBEGIN{
  44. case 'C':
  45. opt[Echo].noway = 1;
  46. break;
  47. case 'd':
  48. debug = 1;
  49. break;
  50. case 'n':
  51. notkbd = 1;
  52. break;
  53. case 'r':
  54. returns = 0;
  55. break;
  56. case 's':
  57. srv = EARGF(usage());
  58. break;
  59. default:
  60. usage();
  61. }ARGEND
  62. if(argc != 1)
  63. usage();
  64. /* options we need routines for */
  65. opt[Echo].change = echochange;
  66. opt[Term].sub = termsub;
  67. opt[Xloc].sub = xlocsub;
  68. comm = share(sizeof(comm));
  69. comm->returns = returns;
  70. telnet(dodial(argv[0]));
  71. }
  72. /*
  73. * dial and return a data connection
  74. */
  75. int
  76. dodial(char *dest)
  77. {
  78. char *name;
  79. int data;
  80. char devdir[NETPATHLEN];
  81. name = netmkaddr(dest, "tcp", "telnet");
  82. data = dial(name, 0, devdir, 0);
  83. if(data < 0)
  84. fatal("%s: %r", name, 0);
  85. fprint(2, "connected to %s on %s\n", name, devdir);
  86. return data;
  87. }
  88. void
  89. post(char *srv, int fd)
  90. {
  91. int f;
  92. char buf[32];
  93. f = create(srv, OWRITE, 0666);
  94. if(f < 0)
  95. sysfatal("create %s: %r", srv);
  96. snprint(buf, sizeof buf, "%d", fd);
  97. if(write(f, buf, strlen(buf)) != strlen(buf))
  98. sysfatal("write %s: %r", srv);
  99. close(f);
  100. }
  101. /*
  102. * two processes pass bytes back and forth between the
  103. * terminal and the network.
  104. */
  105. void
  106. telnet(int net)
  107. {
  108. int pid;
  109. int p[2];
  110. char *svc;
  111. rawoff();
  112. svc = nil;
  113. if (srv) {
  114. if(pipe(p) < 0)
  115. sysfatal("pipe: %r");
  116. if (srv[0] != '/')
  117. svc = smprint("/srv/%s", srv);
  118. else
  119. svc = srv;
  120. post(svc, p[0]);
  121. close(p[0]);
  122. dup(p[1], 0);
  123. dup(p[1], 1);
  124. /* pipe is now std in & out */
  125. }
  126. ttypid = getpid();
  127. switch(pid = rfork(RFPROC|RFFDG|RFMEM)){
  128. case -1:
  129. perror("con");
  130. exits("fork");
  131. case 0:
  132. rawoff();
  133. notify(notifyf);
  134. fromnet(net);
  135. if (svc)
  136. remove(svc);
  137. sendnote(ttypid, "die");
  138. exits(0);
  139. default:
  140. netpid = pid;
  141. notify(notifyf);
  142. fromkbd(net);
  143. if(notkbd)
  144. for(;;)
  145. sleep(0);
  146. if (svc)
  147. remove(svc);
  148. sendnote(netpid, "die");
  149. exits(0);
  150. }
  151. }
  152. /*
  153. * Read the keyboard and write it to the network. '^\' gets us into
  154. * the menu.
  155. */
  156. void
  157. fromkbd(int net)
  158. {
  159. Biobuf ib, ob;
  160. int c, likeatty;
  161. int eofs;
  162. Binit(&ib, 0, OREAD);
  163. Binit(&ob, net, OWRITE);
  164. likeatty = islikeatty(0);
  165. eofs = 0;
  166. for(;;){
  167. c = Bgetc(&ib);
  168. /*
  169. * with raw off, all ^D's get turned into Eof's.
  170. * change them back.
  171. * 10 in a row implies that the terminal is really gone so
  172. * just hang up.
  173. */
  174. if(c < 0){
  175. if(notkbd)
  176. return;
  177. if(eofs++ > 10)
  178. return;
  179. c = 004;
  180. } else
  181. eofs = 0;
  182. /*
  183. * if not in binary mode, look for the ^\ escape to menu.
  184. * also turn \n into \r\n
  185. */
  186. if(likeatty || !opt[Binary].local){
  187. if(c == 0034){ /* CTRL \ */
  188. if(Bflush(&ob) < 0)
  189. return;
  190. if(menu(&ib, net) < 0)
  191. return;
  192. continue;
  193. }
  194. }
  195. if(!opt[Binary].local){
  196. if(c == '\n'){
  197. /*
  198. * This is a very strange use of the SGA option.
  199. * I did this because some systems that don't
  200. * announce a willingness to supress-go-ahead
  201. * need the \r\n sequence to recognize input.
  202. * If someone can explain this to me, please
  203. * send me mail. - presotto
  204. */
  205. if(opt[SGA].remote){
  206. c = '\r';
  207. } else {
  208. if(Bputc(&ob, '\r') < 0)
  209. return;
  210. }
  211. }
  212. }
  213. if(Bputc(&ob, c) < 0)
  214. return;
  215. if(Bbuffered(&ib) == 0)
  216. if(Bflush(&ob) < 0)
  217. return;
  218. }
  219. }
  220. /*
  221. * Read from the network and write to the screen. If 'stopped' is set
  222. * spin and don't read. Filter out spurious carriage returns.
  223. */
  224. void
  225. fromnet(int net)
  226. {
  227. int c;
  228. int crnls = 0, freenl = 0, eofs;
  229. Biobuf ib, ob;
  230. Binit(&ib, net, OREAD);
  231. Binit(&ob, 1, OWRITE);
  232. eofs = 0;
  233. for(;;){
  234. if(Bbuffered(&ib) == 0)
  235. Bflush(&ob);
  236. if(interrupted){
  237. interrupted = 0;
  238. send2(net, Iac, Interrupt);
  239. }
  240. c = Bgetc(&ib);
  241. if(c < 0){
  242. if(eofs++ >= 2)
  243. return;
  244. continue;
  245. }
  246. eofs = 0;
  247. switch(c){
  248. case '\n': /* skip nl after string of cr's */
  249. if(!opt[Binary].local && !comm->returns){
  250. ++crnls;
  251. if(freenl == 0)
  252. break;
  253. freenl = 0;
  254. continue;
  255. }
  256. break;
  257. case '\r': /* first cr becomes nl, remainder dropped */
  258. if(!opt[Binary].local && !comm->returns){
  259. if(crnls++ == 0){
  260. freenl = 1;
  261. c = '\n';
  262. break;
  263. }
  264. continue;
  265. }
  266. break;
  267. case 0: /* remove nulls from crnl string */
  268. if(crnls)
  269. continue;
  270. break;
  271. case Iac:
  272. crnls = 0;
  273. freenl = 0;
  274. c = Bgetc(&ib);
  275. if(c == Iac)
  276. break;
  277. if(Bflush(&ob) < 0)
  278. return;
  279. if(control(&ib, c) < 0)
  280. return;
  281. continue;
  282. default:
  283. crnls = 0;
  284. freenl = 0;
  285. break;
  286. }
  287. if(Bputc(&ob, c) < 0)
  288. return;
  289. }
  290. }
  291. /*
  292. * turn keyboard raw mode on
  293. */
  294. void
  295. rawon(void)
  296. {
  297. if(debug)
  298. fprint(2, "rawon\n");
  299. if(consctl < 0)
  300. consctl = open("/dev/consctl", OWRITE);
  301. if(consctl < 0){
  302. fprint(2, "can't open consctl: %r\n");
  303. return;
  304. }
  305. write(consctl, "rawon", 5);
  306. }
  307. /*
  308. * turn keyboard raw mode off
  309. */
  310. void
  311. rawoff(void)
  312. {
  313. if(debug)
  314. fprint(2, "rawoff\n");
  315. if(consctl < 0)
  316. consctl = open("/dev/consctl", OWRITE);
  317. if(consctl < 0){
  318. fprint(2, "can't open consctl: %r\n");
  319. return;
  320. }
  321. write(consctl, "rawoff", 6);
  322. }
  323. /*
  324. * control menu
  325. */
  326. #define STDHELP "\t(b)reak, (i)nterrupt, (q)uit, (r)eturns, (!cmd), (.)continue\n"
  327. int
  328. menu(Biobuf *bp, int net)
  329. {
  330. char *cp;
  331. int done;
  332. comm->stopped = 1;
  333. rawoff();
  334. fprint(2, ">>> ");
  335. for(done = 0; !done; ){
  336. cp = Brdline(bp, '\n');
  337. if(cp == 0){
  338. comm->stopped = 0;
  339. return -1;
  340. }
  341. cp[Blinelen(bp)-1] = 0;
  342. switch(*cp){
  343. case '!':
  344. system(Bfildes(bp), cp+1);
  345. done = 1;
  346. break;
  347. case '.':
  348. done = 1;
  349. break;
  350. case 'q':
  351. comm->stopped = 0;
  352. return -1;
  353. case 'o':
  354. switch(*(cp+1)){
  355. case 'd':
  356. send3(net, Iac, Do, atoi(cp+2));
  357. break;
  358. case 'w':
  359. send3(net, Iac, Will, atoi(cp+2));
  360. break;
  361. }
  362. break;
  363. case 'r':
  364. comm->returns = !comm->returns;
  365. done = 1;
  366. break;
  367. case 'i':
  368. send2(net, Iac, Interrupt);
  369. break;
  370. case 'b':
  371. send2(net, Iac, Break);
  372. break;
  373. default:
  374. fprint(2, STDHELP);
  375. break;
  376. }
  377. if(!done)
  378. fprint(2, ">>> ");
  379. }
  380. rawon();
  381. comm->stopped = 0;
  382. return 0;
  383. }
  384. /*
  385. * ignore interrupts
  386. */
  387. void
  388. notifyf(void *a, char *msg)
  389. {
  390. USED(a);
  391. if(strcmp(msg, "interrupt") == 0){
  392. interrupted = 1;
  393. noted(NCONT);
  394. }
  395. if(strcmp(msg, "hangup") == 0)
  396. noted(NCONT);
  397. noted(NDFLT);
  398. }
  399. /*
  400. * run a command with the network connection as standard IO
  401. */
  402. char *
  403. system(int fd, char *cmd)
  404. {
  405. int pid;
  406. int p;
  407. static Waitmsg msg;
  408. if((pid = fork()) == -1){
  409. perror("con");
  410. return "fork failed";
  411. }
  412. else if(pid == 0){
  413. dup(fd, 0);
  414. close(ctl);
  415. close(fd);
  416. if(*cmd)
  417. execl("/bin/rc", "rc", "-c", cmd, nil);
  418. else
  419. execl("/bin/rc", "rc", nil);
  420. perror("con");
  421. exits("exec");
  422. }
  423. for(p = waitpid(); p >= 0; p = waitpid()){
  424. if(p == pid)
  425. return msg.msg;
  426. }
  427. return "lost child";
  428. }
  429. /*
  430. * suppress local echo if the remote side is doing it
  431. */
  432. int
  433. echochange(Biobuf *bp, int cmd)
  434. {
  435. USED(bp);
  436. switch(cmd){
  437. case Will:
  438. rawon();
  439. break;
  440. case Wont:
  441. rawoff();
  442. break;
  443. }
  444. return 0;
  445. }
  446. /*
  447. * send terminal type to the other side
  448. */
  449. int
  450. termsub(Biobuf *bp, uchar *sub, int n)
  451. {
  452. char buf[64];
  453. char *term;
  454. char *p = buf;
  455. if(n < 1)
  456. return 0;
  457. if(sub[0] == 1){
  458. *p++ = Iac;
  459. *p++ = Sb;
  460. *p++ = opt[Term].code;
  461. *p++ = 0;
  462. term = getenv("TERM");
  463. if(term == 0 || *term == 0)
  464. term = "p9win";
  465. strncpy(p, term, sizeof(buf) - (p - buf) - 2);
  466. buf[sizeof(buf)-2] = 0;
  467. p += strlen(p);
  468. *p++ = Iac;
  469. *p++ = Se;
  470. return iwrite(Bfildes(bp), buf, p-buf);
  471. }
  472. return 0;
  473. }
  474. /*
  475. * send an x display location to the other side
  476. */
  477. int
  478. xlocsub(Biobuf *bp, uchar *sub, int n)
  479. {
  480. char buf[64];
  481. char *term;
  482. char *p = buf;
  483. if(n < 1)
  484. return 0;
  485. if(sub[0] == 1){
  486. *p++ = Iac;
  487. *p++ = Sb;
  488. *p++ = opt[Xloc].code;
  489. *p++ = 0;
  490. term = getenv("XDISP");
  491. if(term == 0 || *term == 0)
  492. term = "unknown";
  493. strncpy(p, term, p - buf - 2);
  494. p += strlen(term);
  495. *p++ = Iac;
  496. *p++ = Se;
  497. return iwrite(Bfildes(bp), buf, p-buf);
  498. }
  499. return 0;
  500. }
  501. static int
  502. islikeatty(int fd)
  503. {
  504. char buf[64];
  505. if(fd2path(fd, buf, sizeof buf) != 0)
  506. return 0;
  507. /* might be /mnt/term/dev/cons */
  508. return strlen(buf) >= 9 && strcmp(buf+strlen(buf)-9, "/dev/cons") == 0;
  509. }
  510. /*
  511. * create a shared segment. Make is start 2 meg higher than the current
  512. * end of process memory.
  513. */
  514. void*
  515. share(ulong len)
  516. {
  517. uchar *vastart;
  518. vastart = sbrk(0);
  519. if(vastart == (void*)-1)
  520. return 0;
  521. vastart += 2*1024*1024;
  522. if(segattach(0, "shared", vastart, len) == (void*)-1)
  523. return 0;
  524. return vastart;
  525. }