cons.saver.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. General purpose Linux console screen save/restore server
  3. This program should be setuid vcsa and /dev/vcsa* should be
  4. owned by the same user too.
  5. Original idea from Unix Interactive Tools version 3.2b (tty.c)
  6. This code requires root privileges.
  7. You may want to make the cons.saver setuid root.
  8. The code should be safe even if it is setuid but who knows?
  9. Partly rewritten by Jakub Jelinek <jakub@redhat.com>.
  10. Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  11. 2006, 2007, 2011
  12. The Free Software Foundation, Inc.
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /* This code does _not_ need to be setuid root. However, it needs
  26. read/write access to /dev/vcsa* (which is priviledged
  27. operation). You should create user vcsa, make cons.saver setuid
  28. user vcsa, and make all vcsa's owned by user vcsa.
  29. Seeing other peoples consoles is bad thing, but believe me, full
  30. root is even worse. */
  31. /** \file cons.saver.c
  32. * \brief Source: general purpose Linux console screen save/restore server
  33. *
  34. * This code does _not_ need to be setuid root. However, it needs
  35. * read/write access to /dev/vcsa* (which is priviledged
  36. * operation). You should create user vcsa, make cons.saver setuid
  37. * user vcsa, and make all vcsa's owned by user vcsa.
  38. * Seeing other peoples consoles is bad thing, but believe me, full
  39. * root is even worse.
  40. */
  41. #include <config.h>
  42. #ifndef _GNU_SOURCE
  43. #define _GNU_SOURCE
  44. #endif
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <sys/types.h>
  49. #include <sys/stat.h>
  50. #ifdef HAVE_SYS_IOCTL_H
  51. #include <sys/ioctl.h>
  52. #endif
  53. #include <fcntl.h>
  54. #include <termios.h>
  55. #include <unistd.h>
  56. #define LINUX_CONS_SAVER_C
  57. #include "cons.saver.h"
  58. /*** global variables ****************************************************************************/
  59. /*** file scope macro definitions ****************************************************************/
  60. /*** file scope type declarations ****************************************************************/
  61. /*** file scope variables ************************************************************************/
  62. /*** file scope functions ************************************************************************/
  63. /* --------------------------------------------------------------------------------------------- */
  64. static void
  65. send_contents (char *buffer, unsigned int columns, unsigned int rows)
  66. {
  67. unsigned char begin_line = 0, end_line = 0;
  68. unsigned int lastline, lc_index, x;
  69. unsigned char message, outbuf[1024], *p;
  70. unsigned short bytes;
  71. lc_index = 2 * rows * columns;
  72. for (lastline = rows; lastline > 0; lastline--)
  73. for (x = 0; x < columns; x++)
  74. {
  75. lc_index -= 2;
  76. if (buffer[lc_index] != ' ')
  77. goto out;
  78. }
  79. out:
  80. message = CONSOLE_CONTENTS;
  81. if (write (1, &message, 1) != 1)
  82. return;
  83. if (read (0, &begin_line, 1) != 1)
  84. return;
  85. if (read (0, &end_line, 1) != 1)
  86. return;
  87. if (begin_line > lastline)
  88. begin_line = lastline;
  89. if (end_line > lastline)
  90. end_line = lastline;
  91. lc_index = (end_line - begin_line) * columns;
  92. bytes = lc_index;
  93. if (write (1, &bytes, 2) != 2)
  94. return;
  95. if (bytes == 0)
  96. return;
  97. p = outbuf;
  98. for (lc_index = 2 * begin_line * columns; lc_index < 2 * end_line * columns; lc_index += 2)
  99. {
  100. *p++ = buffer[lc_index];
  101. if (p == outbuf + sizeof (outbuf))
  102. {
  103. if (write (1, outbuf, sizeof (outbuf)) != sizeof (outbuf))
  104. return;
  105. p = outbuf;
  106. }
  107. }
  108. if (p != outbuf)
  109. if (write (1, outbuf, p - outbuf) < (p - outbuf))
  110. return;
  111. }
  112. /* --------------------------------------------------------------------------------------------- */
  113. static void __attribute__ ((noreturn)) die (void)
  114. {
  115. unsigned char zero = 0;
  116. ssize_t ret;
  117. ret = write (1, &zero, 1);
  118. exit (3);
  119. }
  120. /* --------------------------------------------------------------------------------------------- */
  121. /*** public functions ****************************************************************************/
  122. /* --------------------------------------------------------------------------------------------- */
  123. int
  124. main (int argc, char **argv)
  125. {
  126. unsigned char action = 0, console_flag = 3;
  127. int console_fd, vcsa_fd, console_minor, buffer_size;
  128. struct stat st;
  129. uid_t uid, euid;
  130. char *buffer, *tty_name, console_name[16], vcsa_name[16];
  131. const char *p, *q;
  132. struct winsize winsz;
  133. close (2);
  134. if (argc != 2)
  135. die ();
  136. tty_name = argv[1];
  137. if (strnlen (tty_name, 15) == 15 || strncmp (tty_name, "/dev/", 5))
  138. die ();
  139. setsid ();
  140. uid = getuid ();
  141. euid = geteuid ();
  142. if (seteuid (uid) < 0)
  143. die ();
  144. console_fd = open (tty_name, O_RDONLY);
  145. if (console_fd < 0)
  146. die ();
  147. if (fstat (console_fd, &st) < 0 || !S_ISCHR (st.st_mode))
  148. die ();
  149. if ((st.st_rdev & 0xff00) != 0x0400)
  150. die ();
  151. console_minor = (int) (st.st_rdev & 0x00ff);
  152. if (console_minor < 1 || console_minor > 63)
  153. die ();
  154. if (st.st_uid != uid)
  155. die ();
  156. switch (tty_name[5])
  157. {
  158. /* devfs */
  159. case 'v':
  160. p = "/dev/vc/%d";
  161. q = "/dev/vcc/a%d";
  162. break;
  163. /* /dev/ttyN */
  164. case 't':
  165. p = "/dev/tty%d";
  166. q = "/dev/vcsa%d";
  167. break;
  168. default:
  169. die ();
  170. break;
  171. }
  172. snprintf (console_name, sizeof (console_name), p, console_minor);
  173. if (strncmp (console_name, tty_name, sizeof (console_name)) != 0)
  174. die ();
  175. if (seteuid (euid) < 0)
  176. die ();
  177. snprintf (vcsa_name, sizeof (vcsa_name), q, console_minor);
  178. vcsa_fd = open (vcsa_name, O_RDWR);
  179. if (vcsa_fd < 0)
  180. die ();
  181. if (fstat (vcsa_fd, &st) < 0 || !S_ISCHR (st.st_mode))
  182. die ();
  183. if (seteuid (uid) < 0)
  184. die ();
  185. winsz.ws_col = winsz.ws_row = 0;
  186. if (ioctl (console_fd, TIOCGWINSZ, &winsz) < 0
  187. || winsz.ws_col <= 0 || winsz.ws_row <= 0 || winsz.ws_col >= 256 || winsz.ws_row >= 256)
  188. die ();
  189. buffer_size = 4 + 2 * winsz.ws_col * winsz.ws_row;
  190. buffer = calloc (buffer_size, 1);
  191. if (buffer == NULL)
  192. die ();
  193. if (write (1, &console_flag, 1) != 1)
  194. die ();
  195. while (console_flag && read (0, &action, 1) == 1)
  196. {
  197. switch (action)
  198. {
  199. case CONSOLE_DONE:
  200. console_flag = 0;
  201. continue;
  202. case CONSOLE_SAVE:
  203. if (seteuid (euid) < 0
  204. || lseek (vcsa_fd, 0, 0) != 0
  205. || fstat (console_fd, &st) < 0 || st.st_uid != uid
  206. || read (vcsa_fd, buffer, buffer_size) != buffer_size
  207. || fstat (console_fd, &st) < 0 || st.st_uid != uid)
  208. memset (buffer, 0, buffer_size);
  209. if (seteuid (uid) < 0)
  210. die ();
  211. break;
  212. case CONSOLE_RESTORE:
  213. if (seteuid (euid) >= 0
  214. && lseek (vcsa_fd, 0, 0) == 0 && fstat (console_fd, &st) >= 0 && st.st_uid == uid)
  215. if (write (vcsa_fd, buffer, buffer_size) != buffer_size)
  216. die ();
  217. if (seteuid (uid) < 0)
  218. die ();
  219. break;
  220. case CONSOLE_CONTENTS:
  221. send_contents (buffer + 4, winsz.ws_col, winsz.ws_row);
  222. break;
  223. }
  224. if (write (1, &console_flag, 1) != 1)
  225. die ();
  226. }
  227. exit (0);
  228. }
  229. /* --------------------------------------------------------------------------------------------- */