1
/* vlock-current.c -- locking routine for vlock,
2
* the VT locking program for linux
4
* This program is copyright (C) 2007 Frank Benkstein, and is free
5
* software which is freely distributable under the terms of the
6
* GNU General Public License version 2, included as the file COPYING in this
7
* distribution. It is NOT public domain software, and any
8
* redistribution not permitted by the GNU General Public License is
9
* expressly forbidden without prior written permission from
22
#include <sys/types.h>
29
/* Lock the current terminal until proper authentication is received. */
33
char *vlock_prompt_timeout;
34
struct timespec timeout;
35
struct timespec *timeout_p = NULL;
36
struct termios term, term_bak;
40
/* get the user name from the environment */
41
char *envuser = getenv("USER");
43
/* ignore some signals */
44
/* these signals shouldn't be delivered anyway, because
45
* terminal signals are disabled below */
46
(void) sigemptyset(&(sa.sa_mask));
47
sa.sa_flags = SA_RESTART;
48
sa.sa_handler = SIG_IGN;
49
(void) sigaction(SIGINT, &sa, NULL);
50
(void) sigaction(SIGQUIT, &sa, NULL);
51
(void) sigaction(SIGTSTP, &sa, NULL);
53
if (uid > 0 || envuser == NULL) {
56
/* get the password entry */
57
struct passwd *pw = getpwuid(uid);
61
perror("vlock-current: getpwuid() failed");
63
fprintf(stderr, "vlock-current: getpwuid() failed\n");
68
/* copy the username */
69
strncpy(user, pw->pw_name, sizeof user - 1);
70
user[sizeof user - 1] = '\0';
72
/* copy the username */
73
strncpy(user, envuser, sizeof user - 1);
74
user[sizeof user - 1] = '\0';
77
/* get the vlock message from the environment */
78
vlock_message = getenv("VLOCK_MESSAGE");
80
/* get the timeout from the environment */
81
vlock_prompt_timeout = getenv("VLOCK_PROMPT_TIMEOUT");
83
if (vlock_prompt_timeout != NULL) {
85
timeout.tv_sec = strtol(vlock_prompt_timeout, &n, 10);
89
if (*n == '\0' && timeout.tv_sec > 0)
93
/* disable terminal echoing and signals */
94
if (tcgetattr(STDIN_FILENO, &term) == 0) {
96
term.c_lflag &= ~(ECHO|ISIG);
97
(void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
104
/* print vlock message */
105
fputs(vlock_message, stderr);
109
/* wait for enter to be pressed */
110
while (read(STDIN_FILENO, &c, 1) >= 0)
114
if (auth(user, timeout_p))
120
if (auth("root", timeout_p))
127
/* restore the terminal */
128
(void) tcsetattr(STDIN_FILENO, TCSANOW, &term_bak);