sshpty.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* $OpenBSD: sshpty.c,v 1.34 2019/07/04 16:20:10 deraadt Exp $ */
  2. /*
  3. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  4. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  5. * All rights reserved
  6. * Allocating a pseudo-terminal, and making it the controlling tty.
  7. *
  8. * As far as I am concerned, the code I have written for this software
  9. * can be used freely for any purpose. Any derived versions of this
  10. * software must be clearly marked as such, and if the derived work is
  11. * incompatible with the protocol description in the RFC file, it must be
  12. * called by a name other than "ssh" or "Secure Shell".
  13. */
  14. #include "includes.h"
  15. #include <sys/types.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/stat.h>
  18. #include <signal.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <grp.h>
  22. #ifdef HAVE_PATHS_H
  23. # include <paths.h>
  24. #endif
  25. #include <pwd.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <termios.h>
  30. #ifdef HAVE_UTIL_H
  31. # include <util.h>
  32. #endif
  33. #include <unistd.h>
  34. #include "sshpty.h"
  35. #include "log.h"
  36. #include "misc.h"
  37. #ifdef HAVE_PTY_H
  38. # include <pty.h>
  39. #endif
  40. #ifndef O_NOCTTY
  41. #define O_NOCTTY 0
  42. #endif
  43. #ifdef __APPLE__
  44. # include <AvailabilityMacros.h>
  45. # if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
  46. # define __APPLE_PRIVPTY__
  47. # endif
  48. #endif
  49. /*
  50. * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
  51. * nonzero if a pty was successfully allocated. On success, open file
  52. * descriptors for the pty and tty sides and the name of the tty side are
  53. * returned (the buffer must be able to hold at least 64 characters).
  54. */
  55. int
  56. pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
  57. {
  58. /* openpty(3) exists in OSF/1 and some other os'es */
  59. char *name;
  60. int i;
  61. i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
  62. if (i == -1) {
  63. error("openpty: %.100s", strerror(errno));
  64. return 0;
  65. }
  66. name = ttyname(*ttyfd);
  67. if (!name)
  68. fatal("openpty returns device for which ttyname fails.");
  69. strlcpy(namebuf, name, namebuflen); /* possible truncation */
  70. return 1;
  71. }
  72. /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
  73. void
  74. pty_release(const char *tty)
  75. {
  76. #if !defined(__APPLE_PRIVPTY__) && !defined(HAVE_OPENPTY)
  77. if (chown(tty, (uid_t) 0, (gid_t) 0) == -1)
  78. error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
  79. if (chmod(tty, (mode_t) 0666) == -1)
  80. error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
  81. #endif /* !__APPLE_PRIVPTY__ && !HAVE_OPENPTY */
  82. }
  83. /* Makes the tty the process's controlling tty and sets it to sane modes. */
  84. void
  85. pty_make_controlling_tty(int *ttyfd, const char *tty)
  86. {
  87. int fd;
  88. /* First disconnect from the old controlling tty. */
  89. #ifdef TIOCNOTTY
  90. fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
  91. if (fd >= 0) {
  92. (void) ioctl(fd, TIOCNOTTY, NULL);
  93. close(fd);
  94. }
  95. #endif /* TIOCNOTTY */
  96. if (setsid() == -1)
  97. error("setsid: %.100s", strerror(errno));
  98. /*
  99. * Verify that we are successfully disconnected from the controlling
  100. * tty.
  101. */
  102. fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
  103. if (fd >= 0) {
  104. error("Failed to disconnect from controlling tty.");
  105. close(fd);
  106. }
  107. /* Make it our controlling tty. */
  108. #ifdef TIOCSCTTY
  109. debug("Setting controlling tty using TIOCSCTTY.");
  110. if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
  111. error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
  112. #endif /* TIOCSCTTY */
  113. #ifdef NEED_SETPGRP
  114. if (setpgrp(0,0) < 0)
  115. error("SETPGRP %s",strerror(errno));
  116. #endif /* NEED_SETPGRP */
  117. fd = open(tty, O_RDWR);
  118. if (fd == -1)
  119. error("%.100s: %.100s", tty, strerror(errno));
  120. else
  121. close(fd);
  122. /* Verify that we now have a controlling tty. */
  123. fd = open(_PATH_TTY, O_WRONLY);
  124. if (fd == -1)
  125. error("open /dev/tty failed - could not set controlling tty: %.100s",
  126. strerror(errno));
  127. else
  128. close(fd);
  129. }
  130. /* Changes the window size associated with the pty. */
  131. void
  132. pty_change_window_size(int ptyfd, u_int row, u_int col,
  133. u_int xpixel, u_int ypixel)
  134. {
  135. struct winsize w;
  136. /* may truncate u_int -> u_short */
  137. w.ws_row = row;
  138. w.ws_col = col;
  139. w.ws_xpixel = xpixel;
  140. w.ws_ypixel = ypixel;
  141. (void) ioctl(ptyfd, TIOCSWINSZ, &w);
  142. }
  143. void
  144. pty_setowner(struct passwd *pw, const char *tty)
  145. {
  146. struct group *grp;
  147. gid_t gid;
  148. mode_t mode;
  149. struct stat st;
  150. /* Determine the group to make the owner of the tty. */
  151. grp = getgrnam("tty");
  152. if (grp == NULL)
  153. debug("%s: no tty group", __func__);
  154. gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
  155. mode = (grp != NULL) ? 0620 : 0600;
  156. /*
  157. * Change owner and mode of the tty as required.
  158. * Warn but continue if filesystem is read-only and the uids match/
  159. * tty is owned by root.
  160. */
  161. if (stat(tty, &st) == -1)
  162. fatal("stat(%.100s) failed: %.100s", tty,
  163. strerror(errno));
  164. #ifdef WITH_SELINUX
  165. ssh_selinux_setup_pty(pw->pw_name, tty);
  166. #endif
  167. if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
  168. if (chown(tty, pw->pw_uid, gid) == -1) {
  169. if (errno == EROFS &&
  170. (st.st_uid == pw->pw_uid || st.st_uid == 0))
  171. debug("chown(%.100s, %u, %u) failed: %.100s",
  172. tty, (u_int)pw->pw_uid, (u_int)gid,
  173. strerror(errno));
  174. else
  175. fatal("chown(%.100s, %u, %u) failed: %.100s",
  176. tty, (u_int)pw->pw_uid, (u_int)gid,
  177. strerror(errno));
  178. }
  179. }
  180. if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
  181. if (chmod(tty, mode) == -1) {
  182. if (errno == EROFS &&
  183. (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
  184. debug("chmod(%.100s, 0%o) failed: %.100s",
  185. tty, (u_int)mode, strerror(errno));
  186. else
  187. fatal("chmod(%.100s, 0%o) failed: %.100s",
  188. tty, (u_int)mode, strerror(errno));
  189. }
  190. }
  191. }
  192. /* Disconnect from the controlling tty. */
  193. void
  194. disconnect_controlling_tty(void)
  195. {
  196. #ifdef TIOCNOTTY
  197. int fd;
  198. if ((fd = open(_PATH_TTY, O_RDWR | O_NOCTTY)) >= 0) {
  199. (void) ioctl(fd, TIOCNOTTY, NULL);
  200. close(fd);
  201. }
  202. #endif /* TIOCNOTTY */
  203. }