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 Janne Kukonlehto <jtklehto@stekt.oulu.fi>
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?
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 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, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 /* This code does _not_ need to be setuid root. However, it needs
27 read/write access to /dev/vcsa* (which is priviledged
28 operation). You should create user vcsa, make cons.saver setuid
29 user vcsa, and make all vcsa's owned by user vcsa.
31 Seeing other peoples consoles is bad thing, but believe me, full
32 root is even worse. */
42 #include <sys/types.h>
44 #include <sys/ioctl.h>
51 #define LINUX_CONS_SAVER_C
52 #include "cons.saver.h"
55 send_contents (char *buffer
, unsigned int columns
, unsigned int rows
)
57 unsigned char begin_line
= 0, end_line
= 0;
58 unsigned int lastline
, index
, x
;
59 unsigned char message
, outbuf
[1024], *p
;
62 index
= 2 * rows
* columns
;
63 for (lastline
= rows
; lastline
> 0; lastline
--)
64 for (x
= 0; x
< columns
; x
++)
67 if (buffer
[index
] != ' ')
72 message
= CONSOLE_CONTENTS
;
73 write (1, &message
, 1);
75 read (0, &begin_line
, 1);
76 read (0, &end_line
, 1);
77 if (begin_line
> lastline
)
78 begin_line
= lastline
;
79 if (end_line
> lastline
)
82 index
= (end_line
- begin_line
) * columns
;
91 for (index
= 2 * begin_line
* columns
;
92 index
< 2 * end_line
* columns
;
95 *p
++ = buffer
[index
];
96 if (p
== outbuf
+ sizeof (outbuf
))
98 write (1, outbuf
, sizeof (outbuf
));
104 write (1, outbuf
, p
- outbuf
);
107 static void __attribute__ ((noreturn
))
110 unsigned char zero
= 0;
116 main (int argc
, char **argv
)
118 unsigned char action
= 0, console_flag
= 3;
119 int console_fd
, vcsa_fd
, console_minor
, buffer_size
;
122 char *buffer
, *tty_name
, console_name
[16], vcsa_name
[16], *p
, *q
;
123 struct winsize winsz
;
131 if (strnlen (tty_name
, 15) == 15
132 || strncmp (tty_name
, "/dev/", 5))
139 if (seteuid (uid
) < 0)
141 console_fd
= open (tty_name
, O_RDONLY
);
144 if (fstat (console_fd
, &st
) < 0 || ! S_ISCHR (st
.st_mode
))
146 if ((st
.st_rdev
& 0xff00) != 0x0400)
148 console_minor
= (int) (st
.st_rdev
& 0x00ff);
149 if (console_minor
< 1 || console_minor
> 63)
151 if (st
.st_uid
!= uid
)
154 switch (tty_name
[5])
157 case 'v': p
= "/dev/vc/%d"; q
= "/dev/vcc/a%d"; break;
159 case 't': p
= "/dev/tty%d"; q
= "/dev/vcsa%d"; break;
160 default: die (); break;
163 snprintf (console_name
, sizeof (console_name
), p
, console_minor
);
164 if (strncmp (console_name
, tty_name
, sizeof (console_name
)) != 0)
167 if (seteuid (euid
) < 0)
170 snprintf (vcsa_name
, sizeof (vcsa_name
), q
, console_minor
);
171 vcsa_fd
= open (vcsa_name
, O_RDWR
);
174 if (fstat (vcsa_fd
, &st
) < 0 || ! S_ISCHR (st
.st_mode
))
177 if (seteuid (uid
) < 0)
180 winsz
.ws_col
= winsz
.ws_row
= 0;
181 if (ioctl (console_fd
, TIOCGWINSZ
, &winsz
) < 0
182 || winsz
.ws_col
<= 0 || winsz
.ws_row
<= 0
183 || winsz
.ws_col
>= 256 || winsz
.ws_row
>= 256)
186 buffer_size
= 4 + 2 * winsz
.ws_col
* winsz
.ws_row
;
187 buffer
= calloc (buffer_size
, 1);
191 write (1, &console_flag
, 1);
193 while (console_flag
&& read (0, &action
, 1) == 1)
201 if (seteuid (euid
) < 0
202 || lseek (vcsa_fd
, 0, 0) != 0
203 || fstat (console_fd
, &st
) < 0 || st
.st_uid
!= uid
204 || read (vcsa_fd
, buffer
, buffer_size
) != buffer_size
205 || fstat (console_fd
, &st
) < 0 || st
.st_uid
!= uid
)
206 memset (buffer
, 0, buffer_size
);
207 if (seteuid (uid
) < 0)
210 case CONSOLE_RESTORE
:
211 if (seteuid (euid
) >= 0
212 && lseek (vcsa_fd
, 0, 0) == 0
213 && fstat (console_fd
, &st
) >= 0 && st
.st_uid
== uid
)
214 write (vcsa_fd
, buffer
, buffer_size
);
215 if (seteuid (uid
) < 0)
218 case CONSOLE_CONTENTS
:
219 send_contents (buffer
+ 4, winsz
.ws_col
, winsz
.ws_row
);
223 write (1, &console_flag
, 1);