sshpty.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copied from OpenSSH-3.5p1 source, modified by Matt Johnston 2003
  5. *
  6. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  7. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  8. * All rights reserved
  9. * Allocating a pseudo-terminal, and making it the controlling tty.
  10. *
  11. * As far as I am concerned, the code I have written for this software
  12. * can be used freely for any purpose. Any derived versions of this
  13. * software must be clearly marked as such, and if the derived work is
  14. * incompatible with the protocol description in the RFC file, it must be
  15. * called by a name other than "ssh" or "Secure Shell".
  16. */
  17. /*RCSID("OpenBSD: sshpty.c,v 1.7 2002/06/24 17:57:20 deraadt Exp ");*/
  18. #include "includes.h"
  19. #include "dbutil.h"
  20. #include "errno.h"
  21. #include "sshpty.h"
  22. /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
  23. #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
  24. #undef HAVE_DEV_PTMX
  25. #endif
  26. #ifdef HAVE_PTY_H
  27. # include <pty.h>
  28. #endif
  29. #if defined(USE_DEV_PTMX) && defined(HAVE_STROPTS_H)
  30. # include <stropts.h>
  31. #endif
  32. #ifndef O_NOCTTY
  33. #define O_NOCTTY 0
  34. #endif
  35. /*
  36. * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
  37. * nonzero if a pty was successfully allocated. On success, open file
  38. * descriptors for the pty and tty sides and the name of the tty side are
  39. * returned (the buffer must be able to hold at least 64 characters).
  40. */
  41. int
  42. pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
  43. {
  44. #if defined(HAVE_OPENPTY)
  45. /* exists in recent (4.4) BSDs and OSF/1 */
  46. char *name;
  47. int i;
  48. i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
  49. if (i < 0) {
  50. dropbear_log(LOG_WARNING,
  51. "pty_allocate: openpty: %.100s", strerror(errno));
  52. return 0;
  53. }
  54. name = ttyname(*ttyfd);
  55. if (!name) {
  56. dropbear_exit("ttyname fails for openpty device");
  57. }
  58. strlcpy(namebuf, name, namebuflen); /* possible truncation */
  59. return 1;
  60. #else /* HAVE_OPENPTY */
  61. #ifdef HAVE__GETPTY
  62. /*
  63. * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
  64. * pty's automagically when needed
  65. */
  66. char *slave;
  67. slave = _getpty(ptyfd, O_RDWR, 0622, 0);
  68. if (slave == NULL) {
  69. dropbear_log(LOG_WARNING,
  70. "pty_allocate: _getpty: %.100s", strerror(errno));
  71. return 0;
  72. }
  73. strlcpy(namebuf, slave, namebuflen);
  74. /* Open the slave side. */
  75. *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
  76. if (*ttyfd < 0) {
  77. dropbear_log(LOG_WARNING,
  78. "pty_allocate error: ttyftd open error");
  79. close(*ptyfd);
  80. return 0;
  81. }
  82. return 1;
  83. #else /* HAVE__GETPTY */
  84. #if defined(USE_DEV_PTMX)
  85. /*
  86. * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
  87. * also has bsd-style ptys, but they simply do not work.)
  88. *
  89. * Linux systems may have the /dev/ptmx device, but this code won't work.
  90. */
  91. int ptm;
  92. char *pts;
  93. ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  94. if (ptm < 0) {
  95. dropbear_log(LOG_WARNING,
  96. "pty_allocate: /dev/ptmx: %.100s", strerror(errno));
  97. return 0;
  98. }
  99. if (grantpt(ptm) < 0) {
  100. dropbear_log(LOG_WARNING,
  101. "grantpt: %.100s", strerror(errno));
  102. return 0;
  103. }
  104. if (unlockpt(ptm) < 0) {
  105. dropbear_log(LOG_WARNING,
  106. "unlockpt: %.100s", strerror(errno));
  107. return 0;
  108. }
  109. pts = ptsname(ptm);
  110. if (pts == NULL) {
  111. dropbear_log(LOG_WARNING,
  112. "Slave pty side name could not be obtained.");
  113. }
  114. strlcpy(namebuf, pts, namebuflen);
  115. *ptyfd = ptm;
  116. /* Open the slave side. */
  117. *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
  118. if (*ttyfd < 0) {
  119. dropbear_log(LOG_ERR,
  120. "error opening pts %.100s: %.100s", namebuf, strerror(errno));
  121. close(*ptyfd);
  122. return 0;
  123. }
  124. #if !defined(HAVE_CYGWIN) && defined(I_PUSH)
  125. /*
  126. * Push the appropriate streams modules, as described in Solaris pts(7).
  127. * HP-UX pts(7) doesn't have ttcompat module.
  128. */
  129. if (ioctl(*ttyfd, I_PUSH, "ptem") < 0) {
  130. dropbear_log(LOG_WARNING,
  131. "ioctl I_PUSH ptem: %.100s", strerror(errno));
  132. }
  133. if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0) {
  134. dropbear_log(LOG_WARNING,
  135. "ioctl I_PUSH ldterm: %.100s", strerror(errno));
  136. }
  137. #ifndef __hpux
  138. if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) {
  139. dropbear_log(LOG_WARNING,
  140. "ioctl I_PUSH ttcompat: %.100s", strerror(errno));
  141. }
  142. #endif
  143. #endif
  144. return 1;
  145. #else /* USE_DEV_PTMX */
  146. #ifdef HAVE_DEV_PTS_AND_PTC
  147. /* AIX-style pty code. */
  148. const char *name;
  149. *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
  150. if (*ptyfd < 0) {
  151. dropbear_log(LOG_ERR,
  152. "Could not open /dev/ptc: %.100s", strerror(errno));
  153. return 0;
  154. }
  155. name = ttyname(*ptyfd);
  156. if (!name) {
  157. dropbear_exit("ttyname fails for /dev/ptc device");
  158. }
  159. strlcpy(namebuf, name, namebuflen);
  160. *ttyfd = open(name, O_RDWR | O_NOCTTY);
  161. if (*ttyfd < 0) {
  162. dropbear_log(LOG_ERR,
  163. "Could not open pty slave side %.100s: %.100s",
  164. name, strerror(errno));
  165. close(*ptyfd);
  166. return 0;
  167. }
  168. return 1;
  169. #else /* HAVE_DEV_PTS_AND_PTC */
  170. /* BSD-style pty code. */
  171. char buf[64];
  172. int i;
  173. const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
  174. const char *ptyminors = "0123456789abcdef";
  175. int num_minors = strlen(ptyminors);
  176. int num_ptys = strlen(ptymajors) * num_minors;
  177. struct termios tio;
  178. for (i = 0; i < num_ptys; i++) {
  179. snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
  180. ptyminors[i % num_minors]);
  181. snprintf(namebuf, namebuflen, "/dev/tty%c%c",
  182. ptymajors[i / num_minors], ptyminors[i % num_minors]);
  183. *ptyfd = open(buf, O_RDWR | O_NOCTTY);
  184. if (*ptyfd < 0) {
  185. /* Try SCO style naming */
  186. snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
  187. snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
  188. *ptyfd = open(buf, O_RDWR | O_NOCTTY);
  189. if (*ptyfd < 0) {
  190. continue;
  191. }
  192. }
  193. /* Open the slave side. */
  194. *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
  195. if (*ttyfd < 0) {
  196. dropbear_log(LOG_ERR,
  197. "pty_allocate: %.100s: %.100s", namebuf, strerror(errno));
  198. close(*ptyfd);
  199. return 0;
  200. }
  201. /* set tty modes to a sane state for broken clients */
  202. if (tcgetattr(*ptyfd, &tio) < 0) {
  203. dropbear_log(LOG_WARNING,
  204. "ptyallocate: tty modes failed: %.100s", strerror(errno));
  205. } else {
  206. tio.c_lflag |= (ECHO | ISIG | ICANON);
  207. tio.c_oflag |= (OPOST | ONLCR);
  208. tio.c_iflag |= ICRNL;
  209. /* Set the new modes for the terminal. */
  210. if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0) {
  211. dropbear_log(LOG_WARNING,
  212. "Setting tty modes for pty failed: %.100s",
  213. strerror(errno));
  214. }
  215. }
  216. return 1;
  217. }
  218. dropbear_log(LOG_WARNING, "Failed to open any /dev/pty?? devices");
  219. return 0;
  220. #endif /* HAVE_DEV_PTS_AND_PTC */
  221. #endif /* USE_DEV_PTMX */
  222. #endif /* HAVE__GETPTY */
  223. #endif /* HAVE_OPENPTY */
  224. }
  225. /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
  226. void
  227. pty_release(const char *tty_name)
  228. {
  229. if (chown(tty_name, (uid_t) 0, (gid_t) 0) < 0
  230. && (errno != ENOENT)) {
  231. dropbear_log(LOG_ERR,
  232. "chown %.100s 0 0 failed: %.100s", tty_name, strerror(errno));
  233. }
  234. if (chmod(tty_name, (mode_t) 0666) < 0
  235. && (errno != ENOENT)) {
  236. dropbear_log(LOG_ERR,
  237. "chmod %.100s 0666 failed: %.100s", tty_name, strerror(errno));
  238. }
  239. }
  240. /* Makes the tty the processes controlling tty and sets it to sane modes. */
  241. void
  242. pty_make_controlling_tty(int *ttyfd, const char *tty_name)
  243. {
  244. int fd;
  245. #ifdef USE_VHANGUP
  246. void *old;
  247. #endif /* USE_VHANGUP */
  248. /* Solaris has a problem with TIOCNOTTY for a bg process, so
  249. * we disable the signal which would STOP the process - matt */
  250. signal(SIGTTOU, SIG_IGN);
  251. /* First disconnect from the old controlling tty. */
  252. #ifdef TIOCNOTTY
  253. fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
  254. if (fd >= 0) {
  255. (void) ioctl(fd, TIOCNOTTY, NULL);
  256. close(fd);
  257. }
  258. #endif /* TIOCNOTTY */
  259. if (setsid() < 0) {
  260. dropbear_log(LOG_ERR,
  261. "setsid: %.100s", strerror(errno));
  262. }
  263. /*
  264. * Verify that we are successfully disconnected from the controlling
  265. * tty.
  266. */
  267. fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
  268. if (fd >= 0) {
  269. dropbear_log(LOG_ERR,
  270. "Failed to disconnect from controlling tty.\n");
  271. close(fd);
  272. }
  273. /* Make it our controlling tty. */
  274. #ifdef TIOCSCTTY
  275. if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) {
  276. dropbear_log(LOG_ERR,
  277. "ioctl(TIOCSCTTY): %.100s", strerror(errno));
  278. }
  279. #endif /* TIOCSCTTY */
  280. #ifdef HAVE_NEWS4
  281. if (setpgrp(0,0) < 0) {
  282. dropbear_log(LOG_ERR,
  283. error("SETPGRP %s",strerror(errno)));
  284. }
  285. #endif /* HAVE_NEWS4 */
  286. #ifdef USE_VHANGUP
  287. old = mysignal(SIGHUP, SIG_IGN);
  288. vhangup();
  289. mysignal(SIGHUP, old);
  290. #endif /* USE_VHANGUP */
  291. fd = open(tty_name, O_RDWR);
  292. if (fd < 0) {
  293. dropbear_log(LOG_ERR,
  294. "%.100s: %.100s", tty_name, strerror(errno));
  295. } else {
  296. #ifdef USE_VHANGUP
  297. close(*ttyfd);
  298. *ttyfd = fd;
  299. #else /* USE_VHANGUP */
  300. close(fd);
  301. #endif /* USE_VHANGUP */
  302. }
  303. /* Verify that we now have a controlling tty. */
  304. fd = open(_PATH_TTY, O_WRONLY);
  305. if (fd < 0) {
  306. dropbear_log(LOG_ERR,
  307. "open /dev/tty failed - could not set controlling tty: %.100s",
  308. strerror(errno));
  309. } else {
  310. close(fd);
  311. }
  312. }
  313. /* Changes the window size associated with the pty. */
  314. void
  315. pty_change_window_size(int ptyfd, int row, int col,
  316. int xpixel, int ypixel)
  317. {
  318. struct winsize w;
  319. w.ws_row = row;
  320. w.ws_col = col;
  321. w.ws_xpixel = xpixel;
  322. w.ws_ypixel = ypixel;
  323. (void) ioctl(ptyfd, TIOCSWINSZ, &w);
  324. }
  325. void
  326. pty_setowner(struct passwd *pw, const char *tty_name)
  327. {
  328. struct group *grp;
  329. gid_t gid;
  330. mode_t mode;
  331. struct stat st;
  332. /* Determine the group to make the owner of the tty. */
  333. grp = getgrnam("tty");
  334. if (grp) {
  335. gid = grp->gr_gid;
  336. mode = S_IRUSR | S_IWUSR | S_IWGRP;
  337. } else {
  338. gid = pw->pw_gid;
  339. mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
  340. }
  341. /*
  342. * Change owner and mode of the tty as required.
  343. * Warn but continue if filesystem is read-only and the uids match/
  344. * tty is owned by root.
  345. */
  346. if (stat(tty_name, &st)) {
  347. dropbear_exit("pty_setowner: stat(%.101s) failed: %.100s",
  348. tty_name, strerror(errno));
  349. }
  350. if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
  351. if (chown(tty_name, pw->pw_uid, gid) < 0) {
  352. if (errno == EROFS &&
  353. (st.st_uid == pw->pw_uid || st.st_uid == 0)) {
  354. dropbear_log(LOG_ERR,
  355. "chown(%.100s, %u, %u) failed: %.100s",
  356. tty_name, (unsigned int)pw->pw_uid, (unsigned int)gid,
  357. strerror(errno));
  358. } else {
  359. dropbear_exit("chown(%.100s, %u, %u) failed: %.100s",
  360. tty_name, (unsigned int)pw->pw_uid, (unsigned int)gid,
  361. strerror(errno));
  362. }
  363. }
  364. }
  365. if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
  366. if (chmod(tty_name, mode) < 0) {
  367. if (errno == EROFS &&
  368. (st.st_mode & (S_IRGRP | S_IROTH)) == 0) {
  369. dropbear_log(LOG_ERR,
  370. "chmod(%.100s, 0%o) failed: %.100s",
  371. tty_name, mode, strerror(errno));
  372. } else {
  373. dropbear_exit("chmod(%.100s, 0%o) failed: %.100s",
  374. tty_name, mode, strerror(errno));
  375. }
  376. }
  377. }
  378. }