PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/usr.bin/tip/tip/tip.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 627 lines | 435 code | 64 blank | 128 comment | 121 complexity | 74e2788363ac55d32752be2cbaa80429 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* $OpenBSD: tip.c,v 1.30 2006/08/18 03:06:18 jason Exp $ */
  2. /* $NetBSD: tip.c,v 1.13 1997/04/20 00:03:05 mellon Exp $ */
  3. /*
  4. * Copyright (c) 1983, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/cdefs.h>
  32. __FBSDID("$FreeBSD$");
  33. #ifndef lint
  34. static const char copyright[] =
  35. "@(#) Copyright (c) 1983, 1993\n\
  36. The Regents of the University of California. All rights reserved.\n";
  37. #endif /* not lint */
  38. #ifndef lint
  39. #if 0
  40. static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93";
  41. static const char rcsid[] = "$OpenBSD: tip.c,v 1.30 2006/08/18 03:06:18 jason Exp $";
  42. #endif
  43. #endif /* not lint */
  44. /*
  45. * tip - UNIX link to other systems
  46. * tip [-v] [-speed] system-name
  47. * or
  48. * cu phone-number [-s speed] [-l line] [-a acu]
  49. */
  50. #include "tip.h"
  51. #include "pathnames.h"
  52. int disc = TTYDISC; /* tip normally runs this way */
  53. char PNbuf[256]; /* This limits the size of a number */
  54. static void intprompt(int);
  55. static void tipin(void);
  56. static int escape(void);
  57. int
  58. main(int argc, char *argv[])
  59. {
  60. char *sys = NOSTR, sbuf[12], *p;
  61. int i;
  62. /* XXX preserve previous braindamaged behavior */
  63. setboolean(value(DC), TRUE);
  64. gid = getgid();
  65. egid = getegid();
  66. uid = getuid();
  67. euid = geteuid();
  68. if (equal(__progname, "cu")) {
  69. cumode = 1;
  70. cumain(argc, argv);
  71. goto cucommon;
  72. }
  73. if (argc > 4) {
  74. fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
  75. exit(1);
  76. }
  77. if (!isatty(0)) {
  78. fprintf(stderr, "%s: must be interactive\n", __progname);
  79. exit(1);
  80. }
  81. for (; argc > 1; argv++, argc--) {
  82. if (argv[1][0] != '-')
  83. sys = argv[1];
  84. else switch (argv[1][1]) {
  85. case 'v':
  86. vflag++;
  87. break;
  88. case 'n':
  89. noesc++;
  90. break;
  91. case '0': case '1': case '2': case '3': case '4':
  92. case '5': case '6': case '7': case '8': case '9':
  93. BR = atoi(&argv[1][1]);
  94. break;
  95. default:
  96. fprintf(stderr, "%s: %s, unknown option\n", __progname,
  97. argv[1]);
  98. break;
  99. }
  100. }
  101. if (sys == NOSTR)
  102. goto notnumber;
  103. if (isalpha(*sys))
  104. goto notnumber;
  105. /*
  106. * System name is really a phone number...
  107. * Copy the number then stomp on the original (in case the number
  108. * is private, we don't want 'ps' or 'w' to find it).
  109. */
  110. if (strlen(sys) > sizeof PNbuf - 1) {
  111. fprintf(stderr, "%s: phone number too long (max = %d bytes)\n",
  112. __progname, (int)sizeof(PNbuf) - 1);
  113. exit(1);
  114. }
  115. strlcpy(PNbuf, sys, sizeof PNbuf - 1);
  116. for (p = sys; *p; p++)
  117. *p = '\0';
  118. PN = PNbuf;
  119. (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
  120. sys = sbuf;
  121. notnumber:
  122. (void)signal(SIGINT, cleanup);
  123. (void)signal(SIGQUIT, cleanup);
  124. (void)signal(SIGHUP, cleanup);
  125. (void)signal(SIGTERM, cleanup);
  126. (void)signal(SIGCHLD, SIG_DFL);
  127. if ((i = hunt(sys)) == 0) {
  128. printf("all ports busy\n");
  129. exit(3);
  130. }
  131. if (i == -1) {
  132. printf("link down\n");
  133. (void)uu_unlock(uucplock);
  134. exit(3);
  135. }
  136. setbuf(stdout, NULL);
  137. loginit();
  138. /*
  139. * Now that we have the logfile and the ACU open
  140. * return to the real uid and gid. These things will
  141. * be closed on exit. Swap real and effective uid's
  142. * so we can get the original permissions back
  143. * for removing the uucp lock.
  144. */
  145. user_uid();
  146. /*
  147. * Kludge, their's no easy way to get the initialization
  148. * in the right order, so force it here
  149. */
  150. if ((PH = getenv("PHONES")) == NOSTR)
  151. PH = _PATH_PHONES;
  152. vinit(); /* init variables */
  153. setparity("none"); /* set the parity table */
  154. /*
  155. * Hardwired connections require the
  156. * line speed set before they make any transmissions
  157. * (this is particularly true of things like a DF03-AC)
  158. */
  159. if (HW && ttysetup(number(value(BAUDRATE)))) {
  160. fprintf(stderr, "%s: bad baud rate %ld\n", __progname,
  161. number(value(BAUDRATE)));
  162. daemon_uid();
  163. (void)uu_unlock(uucplock);
  164. exit(3);
  165. }
  166. if ((p = con())) {
  167. printf("\07%s\n[EOT]\n", p);
  168. daemon_uid();
  169. (void)uu_unlock(uucplock);
  170. exit(1);
  171. }
  172. if (!HW && ttysetup(number(value(BAUDRATE)))) {
  173. fprintf(stderr, "%s: bad baud rate %ld\n", __progname,
  174. number(value(BAUDRATE)));
  175. daemon_uid();
  176. (void)uu_unlock(uucplock);
  177. exit(3);
  178. }
  179. cucommon:
  180. /*
  181. * From here down the code is shared with
  182. * the "cu" version of tip.
  183. */
  184. i = fcntl(FD, F_GETFL);
  185. if (i == -1) {
  186. perror("fcntl");
  187. cleanup(0);
  188. }
  189. i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK);
  190. if (i == -1) {
  191. perror("fcntl");
  192. cleanup(0);
  193. }
  194. tcgetattr(0, &defterm);
  195. gotdefterm = 1;
  196. term = defterm;
  197. term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
  198. term.c_iflag &= ~(INPCK|ICRNL);
  199. term.c_oflag &= ~OPOST;
  200. term.c_cc[VMIN] = 1;
  201. term.c_cc[VTIME] = 0;
  202. defchars = term;
  203. term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
  204. term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
  205. term.c_cc[VLNEXT] = _POSIX_VDISABLE;
  206. raw();
  207. pipe(fildes); pipe(repdes);
  208. (void)signal(SIGALRM, timeout);
  209. if (value(LINEDISC) != TTYDISC) {
  210. int ld = (int)(intptr_t)value(LINEDISC);
  211. ioctl(FD, TIOCSETD, &ld);
  212. }
  213. /*
  214. * Everything's set up now:
  215. * connection established (hardwired or dialup)
  216. * line conditioned (baud rate, mode, etc.)
  217. * internal data structures (variables)
  218. * so, fork one process for local side and one for remote.
  219. */
  220. printf(cumode ? "Connected\r\n" : "\07connected\r\n");
  221. tipin_pid = getpid();
  222. if ((tipout_pid = fork()))
  223. tipin();
  224. else
  225. tipout();
  226. /*NOTREACHED*/
  227. exit(0);
  228. }
  229. void
  230. cleanup(int signo)
  231. {
  232. daemon_uid();
  233. (void)uu_unlock(uucplock);
  234. if (odisc)
  235. ioctl(0, TIOCSETD, &odisc);
  236. unraw();
  237. if (signo && tipout_pid) {
  238. kill(tipout_pid, signo);
  239. wait(NULL);
  240. }
  241. exit(0);
  242. }
  243. /*
  244. * Muck with user ID's. We are setuid to the owner of the lock
  245. * directory when we start. user_uid() reverses real and effective
  246. * ID's after startup, to run with the user's permissions.
  247. * daemon_uid() switches back to the privileged uid for unlocking.
  248. * Finally, to avoid running a shell with the wrong real uid,
  249. * shell_uid() sets real and effective uid's to the user's real ID.
  250. */
  251. static int uidswapped;
  252. void
  253. user_uid(void)
  254. {
  255. if (uidswapped == 0) {
  256. seteuid(uid);
  257. uidswapped = 1;
  258. }
  259. }
  260. void
  261. daemon_uid(void)
  262. {
  263. if (uidswapped) {
  264. seteuid(euid);
  265. uidswapped = 0;
  266. }
  267. }
  268. void
  269. shell_uid(void)
  270. {
  271. setegid(gid);
  272. seteuid(uid);
  273. }
  274. /*
  275. * put the controlling keyboard into raw mode
  276. */
  277. void
  278. raw(void)
  279. {
  280. tcsetattr(0, TCSADRAIN, &term);
  281. }
  282. /*
  283. * return keyboard to normal mode
  284. */
  285. void
  286. unraw(void)
  287. {
  288. if (gotdefterm)
  289. tcsetattr(0, TCSADRAIN, &defterm);
  290. }
  291. /*
  292. * give up exclusive tty access
  293. */
  294. void
  295. unexcl()
  296. {
  297. ioctl(FD, TIOCNXCL, 0);
  298. }
  299. static jmp_buf promptbuf;
  300. /*
  301. * Print string ``s'', then read a string
  302. * in from the terminal. Handles signals & allows use of
  303. * normal erase and kill characters.
  304. */
  305. int
  306. prompt(char *s, char *p, size_t sz)
  307. {
  308. int c;
  309. char *b = p;
  310. sig_t oint, oquit;
  311. stoprompt = 0;
  312. oint = signal(SIGINT, intprompt);
  313. oquit = signal(SIGQUIT, SIG_IGN);
  314. unraw();
  315. printf("%s", s);
  316. if (setjmp(promptbuf) == 0)
  317. while ((c = getchar()) != EOF && (*p = c) != '\n' && --sz > 0)
  318. p++;
  319. *p = '\0';
  320. raw();
  321. (void)signal(SIGINT, oint);
  322. (void)signal(SIGQUIT, oquit);
  323. return (stoprompt || p == b);
  324. }
  325. /*
  326. * Interrupt service routine during prompting
  327. */
  328. /*ARGSUSED*/
  329. static void
  330. intprompt(int signo)
  331. {
  332. (void)signal(SIGINT, SIG_IGN);
  333. stoprompt = 1;
  334. printf("\r\n");
  335. longjmp(promptbuf, 1);
  336. }
  337. /*
  338. * ****TIPIN TIPIN****
  339. */
  340. static void
  341. tipin(void)
  342. {
  343. int bol = 1;
  344. int gch;
  345. char ch;
  346. /*
  347. * Kinda klugey here...
  348. * check for scripting being turned on from the .tiprc file,
  349. * but be careful about just using setscript(), as we may
  350. * send a SIGEMT before tipout has a chance to set up catching
  351. * it; so wait a second, then setscript()
  352. */
  353. if (boolean(value(SCRIPT))) {
  354. sleep(1);
  355. setscript();
  356. }
  357. while (1) {
  358. gch = getchar()&STRIP_PAR;
  359. /* XXX does not check for EOF */
  360. if ((gch == character(value(ESCAPE))) && bol) {
  361. if (!noesc) {
  362. if (!(gch = escape()))
  363. continue;
  364. }
  365. } else if (!cumode && gch == character(value(RAISECHAR))) {
  366. setboolean(value(RAISE), !boolean(value(RAISE)));
  367. continue;
  368. } else if (gch == '\r') {
  369. bol = 1;
  370. ch = gch;
  371. parwrite(FD, &ch, 1);
  372. if (boolean(value(HALFDUPLEX)))
  373. printf("\r\n");
  374. continue;
  375. } else if (!cumode && gch == character(value(FORCE)))
  376. gch = getchar()&STRIP_PAR;
  377. bol = any(gch, value(EOL));
  378. if (boolean(value(RAISE)) && islower(gch))
  379. gch = toupper(gch);
  380. ch = gch;
  381. parwrite(FD, &ch, 1);
  382. if (boolean(value(HALFDUPLEX)))
  383. printf("%c", ch);
  384. }
  385. }
  386. extern esctable_t etable[];
  387. /*
  388. * Escape handler --
  389. * called on recognition of ``escapec'' at the beginning of a line
  390. */
  391. static int
  392. escape(void)
  393. {
  394. int gch;
  395. esctable_t *p;
  396. char c = character(value(ESCAPE));
  397. gch = (getchar()&STRIP_PAR);
  398. /* XXX does not check for EOF */
  399. for (p = etable; p->e_char; p++)
  400. if (p->e_char == gch) {
  401. if ((p->e_flags&PRIV) && uid)
  402. continue;
  403. printf("%s", ctrl(c));
  404. (*p->e_func)(gch);
  405. return (0);
  406. }
  407. /* ESCAPE ESCAPE forces ESCAPE */
  408. if (c != gch)
  409. parwrite(FD, &c, 1);
  410. return (gch);
  411. }
  412. int
  413. any(int cc, char *p)
  414. {
  415. char c = cc;
  416. while (p && *p)
  417. if (*p++ == c)
  418. return (1);
  419. return (0);
  420. }
  421. size_t
  422. size(char *s)
  423. {
  424. size_t i = 0;
  425. while (s && *s++)
  426. i++;
  427. return (i);
  428. }
  429. char *
  430. interp(char *s)
  431. {
  432. static char buf[256];
  433. char *p = buf, c, *q;
  434. while ((c = *s++)) {
  435. for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
  436. if (*q++ == c) {
  437. *p++ = '\\'; *p++ = *q;
  438. goto next;
  439. }
  440. if (c < 040) {
  441. *p++ = '^'; *p++ = c + 'A'-1;
  442. } else if (c == 0177) {
  443. *p++ = '^'; *p++ = '?';
  444. } else
  445. *p++ = c;
  446. next:
  447. ;
  448. }
  449. *p = '\0';
  450. return (buf);
  451. }
  452. char *
  453. ctrl(char c)
  454. {
  455. static char s[3];
  456. if (c < 040 || c == 0177) {
  457. s[0] = '^';
  458. s[1] = c == 0177 ? '?' : c+'A'-1;
  459. s[2] = '\0';
  460. } else {
  461. s[0] = c;
  462. s[1] = '\0';
  463. }
  464. return (s);
  465. }
  466. /*
  467. * Help command
  468. */
  469. void
  470. help(int c)
  471. {
  472. esctable_t *p;
  473. printf("%c\r\n", c);
  474. for (p = etable; p->e_char; p++) {
  475. if ((p->e_flags&PRIV) && uid)
  476. continue;
  477. printf("%2s", ctrl(character(value(ESCAPE))));
  478. printf("%-2s %c %s\r\n", ctrl(p->e_char),
  479. p->e_flags&EXP ? '*': ' ', p->e_help);
  480. }
  481. }
  482. /*
  483. * Set up the "remote" tty's state
  484. */
  485. int
  486. ttysetup(int speed)
  487. {
  488. struct termios cntrl;
  489. if (tcgetattr(FD, &cntrl))
  490. return (-1);
  491. cfsetspeed(&cntrl, speed);
  492. cntrl.c_cflag &= ~(CSIZE|PARENB);
  493. cntrl.c_cflag |= CS8;
  494. if (boolean(value(DC)))
  495. cntrl.c_cflag |= CLOCAL;
  496. if (boolean(value(HARDWAREFLOW)))
  497. cntrl.c_cflag |= CRTSCTS;
  498. cntrl.c_iflag &= ~(ISTRIP|ICRNL);
  499. cntrl.c_oflag &= ~OPOST;
  500. cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
  501. cntrl.c_cc[VMIN] = 1;
  502. cntrl.c_cc[VTIME] = 0;
  503. if (boolean(value(TAND)))
  504. cntrl.c_iflag |= IXOFF;
  505. return (tcsetattr(FD, TCSAFLUSH, &cntrl));
  506. }
  507. static char partab[0200];
  508. /*
  509. * Do a write to the remote machine with the correct parity.
  510. * We are doing 8 bit wide output, so we just generate a character
  511. * with the right parity and output it.
  512. */
  513. void
  514. parwrite(int fd, char *buf, size_t n)
  515. {
  516. size_t i;
  517. char *bp;
  518. bp = buf;
  519. if (bits8 == 0)
  520. for (i = 0; i < n; i++) {
  521. *bp = partab[(*bp) & 0177];
  522. bp++;
  523. }
  524. if (write(fd, buf, n) < 0) {
  525. if (errno == EIO || errno == ENXIO)
  526. tipabort("Lost carrier.");
  527. /* this is questionable */
  528. perror("write");
  529. }
  530. }
  531. /*
  532. * Build a parity table with appropriate high-order bit.
  533. */
  534. void
  535. setparity(char *defparity)
  536. {
  537. int i, flip, clr, set;
  538. char *parity;
  539. extern const unsigned char evenpartab[];
  540. if (value(PARITY) == NOSTR)
  541. value(PARITY) = defparity;
  542. parity = value(PARITY);
  543. if (equal(parity, "none")) {
  544. bits8 = 1;
  545. return;
  546. }
  547. bits8 = 0;
  548. flip = 0;
  549. clr = 0377;
  550. set = 0;
  551. if (equal(parity, "odd"))
  552. flip = 0200; /* reverse bit 7 */
  553. else if (equal(parity, "zero"))
  554. clr = 0177; /* turn off bit 7 */
  555. else if (equal(parity, "one"))
  556. set = 0200; /* turn on bit 7 */
  557. else if (!equal(parity, "even")) {
  558. (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
  559. (void) fflush(stderr);
  560. }
  561. for (i = 0; i < 0200; i++)
  562. partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
  563. }