cons.saver.c 6.3 KB

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