Ticket #1634 (BS, Del incorrect works in CP866 locale)
[midnight-commander.git] / src / consaver / cons.saver.c
blob7601b1dd19021ea5dd0b6d3ca4d50a2e90f17144
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 /*** global variables ****************************************************************************/
68 /*** file scope macro definitions ****************************************************************/
70 /*** file scope type declarations ****************************************************************/
72 /*** file scope variables ************************************************************************/
74 /*** file scope functions ************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
77 static void
78 send_contents (char *buffer, unsigned int columns, unsigned int rows)
80 unsigned char begin_line = 0, end_line = 0;
81 unsigned int lastline, lc_index, x;
82 unsigned char message, outbuf[1024], *p;
83 unsigned short bytes;
85 lc_index = 2 * rows * columns;
86 for (lastline = rows; lastline > 0; lastline--)
87 for (x = 0; x < columns; x++)
89 lc_index -= 2;
90 if (buffer[lc_index] != ' ')
91 goto out;
93 out:
95 message = CONSOLE_CONTENTS;
96 if (write (1, &message, 1) != 1)
97 return;
98 if (read (0, &begin_line, 1) != 1)
99 return;
100 if (read (0, &end_line, 1) != 1)
101 return;
102 if (begin_line > lastline)
103 begin_line = lastline;
104 if (end_line > lastline)
105 end_line = lastline;
107 lc_index = (end_line - begin_line) * columns;
108 bytes = lc_index;
110 if (write (1, &bytes, 2) != 2)
111 return;
112 if (bytes == 0)
113 return;
115 p = outbuf;
116 for (lc_index = 2 * begin_line * columns; lc_index < 2 * end_line * columns; lc_index += 2)
118 *p++ = buffer[lc_index];
119 if (p == outbuf + sizeof (outbuf))
121 if (write (1, outbuf, sizeof (outbuf)) != sizeof (outbuf))
122 return;
123 p = outbuf;
127 if (p != outbuf)
128 if (write (1, outbuf, p - outbuf) < (p - outbuf))
129 return;
132 /* --------------------------------------------------------------------------------------------- */
134 static void __attribute__ ((noreturn)) die (void)
136 unsigned char zero = 0;
137 ssize_t ret;
138 ret = write (1, &zero, 1);
139 exit (3);
142 /* --------------------------------------------------------------------------------------------- */
143 /*** public functions ****************************************************************************/
144 /* --------------------------------------------------------------------------------------------- */
147 main (int argc, char **argv)
149 unsigned char action = 0, console_flag = 3;
150 int console_fd, vcsa_fd, console_minor, buffer_size;
151 struct stat st;
152 uid_t uid, euid;
153 char *buffer, *tty_name, console_name[16], vcsa_name[16];
154 const char *p, *q;
155 struct winsize winsz;
157 close (2);
159 if (argc != 2)
160 die ();
162 tty_name = argv[1];
163 if (strnlen (tty_name, 15) == 15 || strncmp (tty_name, "/dev/", 5))
164 die ();
166 setsid ();
167 uid = getuid ();
168 euid = geteuid ();
170 if (seteuid (uid) < 0)
171 die ();
172 console_fd = open (tty_name, O_RDONLY);
173 if (console_fd < 0)
174 die ();
175 if (fstat (console_fd, &st) < 0 || !S_ISCHR (st.st_mode))
176 die ();
177 if ((st.st_rdev & 0xff00) != 0x0400)
178 die ();
179 console_minor = (int) (st.st_rdev & 0x00ff);
180 if (console_minor < 1 || console_minor > 63)
181 die ();
182 if (st.st_uid != uid)
183 die ();
185 switch (tty_name[5])
187 /* devfs */
188 case 'v':
189 p = "/dev/vc/%d";
190 q = "/dev/vcc/a%d";
191 break;
192 /* /dev/ttyN */
193 case 't':
194 p = "/dev/tty%d";
195 q = "/dev/vcsa%d";
196 break;
197 default:
198 die ();
199 break;
202 snprintf (console_name, sizeof (console_name), p, console_minor);
203 if (strncmp (console_name, tty_name, sizeof (console_name)) != 0)
204 die ();
206 if (seteuid (euid) < 0)
207 die ();
209 snprintf (vcsa_name, sizeof (vcsa_name), q, console_minor);
210 vcsa_fd = open (vcsa_name, O_RDWR);
211 if (vcsa_fd < 0)
212 die ();
213 if (fstat (vcsa_fd, &st) < 0 || !S_ISCHR (st.st_mode))
214 die ();
216 if (seteuid (uid) < 0)
217 die ();
219 winsz.ws_col = winsz.ws_row = 0;
220 if (ioctl (console_fd, TIOCGWINSZ, &winsz) < 0
221 || winsz.ws_col <= 0 || winsz.ws_row <= 0 || winsz.ws_col >= 256 || winsz.ws_row >= 256)
222 die ();
224 buffer_size = 4 + 2 * winsz.ws_col * winsz.ws_row;
225 buffer = calloc (buffer_size, 1);
226 if (buffer == NULL)
227 die ();
229 if (write (1, &console_flag, 1) != 1)
230 die ();
232 while (console_flag && read (0, &action, 1) == 1)
234 switch (action)
236 case CONSOLE_DONE:
237 console_flag = 0;
238 continue;
239 case CONSOLE_SAVE:
240 if (seteuid (euid) < 0
241 || lseek (vcsa_fd, 0, 0) != 0
242 || fstat (console_fd, &st) < 0 || st.st_uid != uid
243 || read (vcsa_fd, buffer, buffer_size) != buffer_size
244 || fstat (console_fd, &st) < 0 || st.st_uid != uid)
245 memset (buffer, 0, buffer_size);
246 if (seteuid (uid) < 0)
247 die ();
248 break;
249 case CONSOLE_RESTORE:
250 if (seteuid (euid) >= 0
251 && lseek (vcsa_fd, 0, 0) == 0 && fstat (console_fd, &st) >= 0 && st.st_uid == uid)
252 if (write (vcsa_fd, buffer, buffer_size) != buffer_size)
253 die ();
254 if (seteuid (uid) < 0)
255 die ();
256 break;
257 case CONSOLE_CONTENTS:
258 send_contents (buffer + 4, winsz.ws_col, winsz.ws_row);
259 break;
262 if (write (1, &console_flag, 1) != 1)
263 die ();
266 exit (0);
269 /* --------------------------------------------------------------------------------------------- */