telnet.c 8.4 KB

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