telnet.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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(ulong);
  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("%s: %r", name, 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(;;){
  196. if(Bbuffered(&ib) == 0)
  197. Bflush(&ob);
  198. if(interrupted){
  199. interrupted = 0;
  200. send2(net, Iac, Interrupt);
  201. }
  202. c = Bgetc(&ib);
  203. if(c < 0){
  204. if(eofs++ >= 2)
  205. return;
  206. continue;
  207. }
  208. eofs = 0;
  209. switch(c){
  210. case '\n': /* skip nl after string of cr's */
  211. if(!opt[Binary].local && !comm->returns){
  212. ++crnls;
  213. if(freenl == 0)
  214. break;
  215. freenl = 0;
  216. continue;
  217. }
  218. break;
  219. case '\r': /* first cr becomes nl, remainder dropped */
  220. if(!opt[Binary].local && !comm->returns){
  221. if(crnls++ == 0){
  222. freenl = 1;
  223. c = '\n';
  224. break;
  225. }
  226. continue;
  227. }
  228. break;
  229. case 0: /* remove nulls from crnl string */
  230. if(crnls)
  231. continue;
  232. break;
  233. case Iac:
  234. crnls = 0;
  235. freenl = 0;
  236. c = Bgetc(&ib);
  237. if(c == Iac)
  238. break;
  239. if(Bflush(&ob) < 0)
  240. return;
  241. if(control(&ib, c) < 0)
  242. return;
  243. continue;
  244. default:
  245. crnls = 0;
  246. freenl = 0;
  247. break;
  248. }
  249. if(Bputc(&ob, c) < 0)
  250. return;
  251. }
  252. }
  253. /*
  254. * turn keyboard raw mode on
  255. */
  256. void
  257. rawon(void)
  258. {
  259. if(debug)
  260. fprint(2, "rawon\n");
  261. if(consctl < 0)
  262. consctl = open("/dev/consctl", OWRITE);
  263. if(consctl < 0){
  264. fprint(2, "can't open consctl: %r\n");
  265. return;
  266. }
  267. write(consctl, "rawon", 5);
  268. }
  269. /*
  270. * turn keyboard raw mode off
  271. */
  272. void
  273. rawoff(void)
  274. {
  275. if(debug)
  276. fprint(2, "rawoff\n");
  277. if(consctl < 0)
  278. consctl = open("/dev/consctl", OWRITE);
  279. if(consctl < 0){
  280. fprint(2, "can't open consctl: %r\n");
  281. return;
  282. }
  283. write(consctl, "rawoff", 6);
  284. }
  285. /*
  286. * control menu
  287. */
  288. #define STDHELP "\t(b)reak, (i)nterrupt, (q)uit, (r)eturns, (!cmd), (.)continue\n"
  289. int
  290. menu(Biobuf *bp, int net)
  291. {
  292. char *cp;
  293. int done;
  294. comm->stopped = 1;
  295. rawoff();
  296. fprint(2, ">>> ");
  297. for(done = 0; !done; ){
  298. cp = Brdline(bp, '\n');
  299. if(cp == 0){
  300. comm->stopped = 0;
  301. return -1;
  302. }
  303. cp[Blinelen(bp)-1] = 0;
  304. switch(*cp){
  305. case '!':
  306. system(Bfildes(bp), cp+1);
  307. done = 1;
  308. break;
  309. case '.':
  310. done = 1;
  311. break;
  312. case 'q':
  313. comm->stopped = 0;
  314. return -1;
  315. case 'o':
  316. switch(*(cp+1)){
  317. case 'd':
  318. send3(net, Iac, Do, atoi(cp+2));
  319. break;
  320. case 'w':
  321. send3(net, Iac, Will, atoi(cp+2));
  322. break;
  323. }
  324. break;
  325. case 'r':
  326. comm->returns = !comm->returns;
  327. done = 1;
  328. break;
  329. case 'i':
  330. send2(net, Iac, Interrupt);
  331. break;
  332. case 'b':
  333. send2(net, Iac, Break);
  334. break;
  335. default:
  336. fprint(2, STDHELP);
  337. break;
  338. }
  339. if(!done)
  340. fprint(2, ">>> ");
  341. }
  342. rawon();
  343. comm->stopped = 0;
  344. return 0;
  345. }
  346. /*
  347. * ignore interrupts
  348. */
  349. void
  350. notifyf(void *a, char *msg)
  351. {
  352. USED(a);
  353. if(strcmp(msg, "interrupt") == 0){
  354. interrupted = 1;
  355. noted(NCONT);
  356. }
  357. if(strcmp(msg, "hangup") == 0)
  358. noted(NCONT);
  359. noted(NDFLT);
  360. }
  361. /*
  362. * run a command with the network connection as standard IO
  363. */
  364. char *
  365. system(int fd, char *cmd)
  366. {
  367. int pid;
  368. int p;
  369. static Waitmsg msg;
  370. if((pid = fork()) == -1){
  371. perror("con");
  372. return "fork failed";
  373. }
  374. else if(pid == 0){
  375. dup(fd, 0);
  376. close(ctl);
  377. close(fd);
  378. if(*cmd)
  379. execl("/bin/rc", "rc", "-c", cmd, nil);
  380. else
  381. execl("/bin/rc", "rc", nil);
  382. perror("con");
  383. exits("exec");
  384. }
  385. for(p = waitpid(); p >= 0; p = waitpid()){
  386. if(p == pid)
  387. return msg.msg;
  388. }
  389. return "lost child";
  390. }
  391. /*
  392. * suppress local echo if the remote side is doing it
  393. */
  394. int
  395. echochange(Biobuf *bp, int cmd)
  396. {
  397. USED(bp);
  398. switch(cmd){
  399. case Will:
  400. rawon();
  401. break;
  402. case Wont:
  403. rawoff();
  404. break;
  405. }
  406. return 0;
  407. }
  408. /*
  409. * send terminal type to the other side
  410. */
  411. int
  412. termsub(Biobuf *bp, uchar *sub, int n)
  413. {
  414. char buf[64];
  415. char *term;
  416. char *p = buf;
  417. if(n < 1)
  418. return 0;
  419. if(sub[0] == 1){
  420. *p++ = Iac;
  421. *p++ = Sb;
  422. *p++ = opt[Term].code;
  423. *p++ = 0;
  424. term = getenv("TERM");
  425. if(term == 0 || *term == 0)
  426. term = "p9win";
  427. strncpy(p, term, sizeof(buf) - (p - buf) - 2);
  428. buf[sizeof(buf)-2] = 0;
  429. p += strlen(p);
  430. *p++ = Iac;
  431. *p++ = Se;
  432. return iwrite(Bfildes(bp), buf, p-buf);
  433. }
  434. return 0;
  435. }
  436. /*
  437. * send an x display location to the other side
  438. */
  439. int
  440. xlocsub(Biobuf *bp, uchar *sub, int n)
  441. {
  442. char buf[64];
  443. char *term;
  444. char *p = buf;
  445. if(n < 1)
  446. return 0;
  447. if(sub[0] == 1){
  448. *p++ = Iac;
  449. *p++ = Sb;
  450. *p++ = opt[Xloc].code;
  451. *p++ = 0;
  452. term = getenv("XDISP");
  453. if(term == 0 || *term == 0)
  454. term = "unknown";
  455. strncpy(p, term, p - buf - 2);
  456. p += strlen(term);
  457. *p++ = Iac;
  458. *p++ = Se;
  459. return iwrite(Bfildes(bp), buf, p-buf);
  460. }
  461. return 0;
  462. }
  463. static int
  464. islikeatty(int fd)
  465. {
  466. char buf[64];
  467. if(fd2path(fd, buf, sizeof buf) != 0)
  468. return 0;
  469. /* might be /mnt/term/dev/cons */
  470. return strlen(buf) >= 9 && strcmp(buf+strlen(buf)-9, "/dev/cons") == 0;
  471. }
  472. /*
  473. * create a shared segment. Make is start 2 meg higher than the current
  474. * end of process memory.
  475. */
  476. void*
  477. share(ulong len)
  478. {
  479. uchar *vastart;
  480. vastart = sbrk(0);
  481. if(vastart == (void*)-1)
  482. return 0;
  483. vastart += 2*1024*1024;
  484. if(segattach(0, "shared", vastart, len) == (void*)-1)
  485. return 0;
  486. return vastart;
  487. }