Merge branch '2199_button_bar_mouse'
[midnight-commander.git] / src / consaver / cons.saver.c
blobb05b1ef9ae27c73649fd4dfddc63e0386a087ca0
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>. */
5 /* General purpose Linux console screen save/restore server
6 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
7 2006, 2007 Free Software Foundation, Inc.
8 Original idea from Unix Interactive Tools version 3.2b (tty.c)
9 This code requires root privileges.
10 You may want to make the cons.saver setuid root.
11 The code should be safe even if it is setuid but who knows?
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program 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.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
27 /* This code does _not_ need to be setuid root. However, it needs
28 read/write access to /dev/vcsa* (which is priviledged
29 operation). You should create user vcsa, make cons.saver setuid
30 user vcsa, and make all vcsa's owned by user vcsa.
32 Seeing other peoples consoles is bad thing, but believe me, full
33 root is even worse. */
35 /** \file cons.saver.c
36 * \brief Source: general purpose Linux console screen save/restore server
38 * This code does _not_ need to be setuid root. However, it needs
39 * read/write access to /dev/vcsa* (which is priviledged
40 * operation). You should create user vcsa, make cons.saver setuid
41 * user vcsa, and make all vcsa's owned by user vcsa.
42 * Seeing other peoples consoles is bad thing, but believe me, full
43 * root is even worse.
46 #include <config.h>
48 #ifndef _GNU_SOURCE
49 #define _GNU_SOURCE
50 #endif
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <string.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/ioctl.h>
59 #include <fcntl.h>
60 #include <termios.h>
61 #include <unistd.h>
63 #define LINUX_CONS_SAVER_C
64 #include "cons.saver.h"
66 static void
67 send_contents (char *buffer, unsigned int columns, unsigned int rows)
69 unsigned char begin_line = 0, end_line = 0;
70 unsigned int lastline, lc_index, x;
71 unsigned char message, outbuf[1024], *p;
72 unsigned short bytes;
74 lc_index = 2 * rows * columns;
75 for (lastline = rows; lastline > 0; lastline--)
76 for (x = 0; x < columns; x++)
78 lc_index -= 2;
79 if (buffer[lc_index] != ' ')
80 goto out;
82 out:
84 message = CONSOLE_CONTENTS;
85 if (write (1, &message, 1) != 1)
86 return;
87 if (read (0, &begin_line, 1) != 1)
88 return;
89 if (read (0, &end_line, 1) != 1)
90 return;
91 if (begin_line > lastline)
92 begin_line = lastline;
93 if (end_line > lastline)
94 end_line = lastline;
96 lc_index = (end_line - begin_line) * columns;
98 if (write (1, &bytes, 2) != 2)
99 return;
100 if (!bytes)
101 return;
103 p = outbuf;
104 for (lc_index = 2 * begin_line * columns; lc_index < 2 * end_line * columns; lc_index += 2)
106 *p++ = buffer[lc_index];
107 if (p == outbuf + sizeof (outbuf))
109 if (write (1, outbuf, sizeof (outbuf)) != sizeof (outbuf))
110 return;
111 p = outbuf;
115 if (p != outbuf)
116 if (write (1, outbuf, p - outbuf) < (p - outbuf))
117 return;
120 static void __attribute__ ((noreturn)) die (void)
122 unsigned char zero = 0;
123 ssize_t ret;
124 ret = write (1, &zero, 1);
125 exit (3);
129 main (int argc, char **argv)
131 unsigned char action = 0, console_flag = 3;
132 int console_fd, vcsa_fd, console_minor, buffer_size;
133 struct stat st;
134 uid_t uid, euid;
135 char *buffer, *tty_name, console_name[16], vcsa_name[16];
136 const char *p, *q;
137 struct winsize winsz;
139 close (2);
141 if (argc != 2)
142 die ();
144 tty_name = argv[1];
145 if (strnlen (tty_name, 15) == 15 || strncmp (tty_name, "/dev/", 5))
146 die ();
148 setsid ();
149 uid = getuid ();
150 euid = geteuid ();
152 if (seteuid (uid) < 0)
153 die ();
154 console_fd = open (tty_name, O_RDONLY);
155 if (console_fd < 0)
156 die ();
157 if (fstat (console_fd, &st) < 0 || !S_ISCHR (st.st_mode))
158 die ();
159 if ((st.st_rdev & 0xff00) != 0x0400)
160 die ();
161 console_minor = (int) (st.st_rdev & 0x00ff);
162 if (console_minor < 1 || console_minor > 63)
163 die ();
164 if (st.st_uid != uid)
165 die ();
167 switch (tty_name[5])
169 /* devfs */
170 case 'v':
171 p = "/dev/vc/%d";
172 q = "/dev/vcc/a%d";
173 break;
174 /* /dev/ttyN */
175 case 't':
176 p = "/dev/tty%d";
177 q = "/dev/vcsa%d";
178 break;
179 default:
180 die ();
181 break;
184 snprintf (console_name, sizeof (console_name), p, console_minor);
185 if (strncmp (console_name, tty_name, sizeof (console_name)) != 0)
186 die ();
188 if (seteuid (euid) < 0)
189 die ();
191 snprintf (vcsa_name, sizeof (vcsa_name), q, console_minor);
192 vcsa_fd = open (vcsa_name, O_RDWR);
193 if (vcsa_fd < 0)
194 die ();
195 if (fstat (vcsa_fd, &st) < 0 || !S_ISCHR (st.st_mode))
196 die ();
198 if (seteuid (uid) < 0)
199 die ();
201 winsz.ws_col = winsz.ws_row = 0;
202 if (ioctl (console_fd, TIOCGWINSZ, &winsz) < 0
203 || winsz.ws_col <= 0 || winsz.ws_row <= 0 || winsz.ws_col >= 256 || winsz.ws_row >= 256)
204 die ();
206 buffer_size = 4 + 2 * winsz.ws_col * winsz.ws_row;
207 buffer = calloc (buffer_size, 1);
208 if (buffer == NULL)
209 die ();
211 if (write (1, &console_flag, 1) != 1)
212 die ();
214 while (console_flag && read (0, &action, 1) == 1)
216 switch (action)
218 case CONSOLE_DONE:
219 console_flag = 0;
220 continue;
221 case CONSOLE_SAVE:
222 if (seteuid (euid) < 0
223 || lseek (vcsa_fd, 0, 0) != 0
224 || fstat (console_fd, &st) < 0 || st.st_uid != uid
225 || read (vcsa_fd, buffer, buffer_size) != buffer_size
226 || fstat (console_fd, &st) < 0 || st.st_uid != uid)
227 memset (buffer, 0, buffer_size);
228 if (seteuid (uid) < 0)
229 die ();
230 break;
231 case CONSOLE_RESTORE:
232 if (seteuid (euid) >= 0
233 && lseek (vcsa_fd, 0, 0) == 0 && fstat (console_fd, &st) >= 0 && st.st_uid == uid)
234 if (write (vcsa_fd, buffer, buffer_size) != buffer_size)
235 die ();
236 if (seteuid (uid) < 0)
237 die ();
238 break;
239 case CONSOLE_CONTENTS:
240 send_contents (buffer + 4, winsz.ws_col, winsz.ws_row);
241 break;
244 if (write (1, &console_flag, 1) != 1)
245 die ();
248 exit (0);