1 /* vlock-main.c -- main 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 #include "console_switch.h"
38 #define ensure_atexit(func) \
40 if (atexit(func) != 0) \
41 fatal_perror("vlock-main: atexit() failed"); \
44 static char *get_username(void)
47 char *username
= NULL
;
49 /* Get the user name from the environment if started as root. */
51 username
= getenv("USER");
53 if (username
== NULL
) {
56 /* Get the password entry. */
62 username
= pw
->pw_name
;
65 return strdup(username
);
68 static void terminate(int __attribute__((unused
)) signum
)
70 fprintf(stderr
, "vlock-main: Terminated!\n");
74 static void block_signals(void)
78 /* Ignore some signals. */
79 /* These signals shouldn't be delivered anyway, because terminal signals are
81 (void) sigemptyset(&(sa
.sa_mask
));
82 sa
.sa_flags
= SA_RESTART
;
83 sa
.sa_handler
= SIG_IGN
;
84 (void) sigaction(SIGINT
, &sa
, NULL
);
85 (void) sigaction(SIGQUIT
, &sa
, NULL
);
86 (void) sigaction(SIGTSTP
, &sa
, NULL
);
88 /* Install special handler for SIGTERM. */
89 sa
.sa_flags
= SA_RESETHAND
;
90 sa
.sa_handler
= terminate
;
91 (void) sigaction(SIGTERM
, &sa
, NULL
);
94 static struct termios term
;
95 static tcflag_t lflag
;
97 static void secure_terminal(void)
99 /* Disable terminal echoing and signals. */
100 (void) tcgetattr(STDIN_FILENO
, &term
);
101 lflag
= term
.c_lflag
;
102 term
.c_lflag
&= ~(ECHO
| ISIG
);
103 (void) tcsetattr(STDIN_FILENO
, TCSANOW
, &term
);
106 static void restore_terminal(void)
108 /* Restore the terminal. */
109 term
.c_lflag
= lflag
;
110 (void) tcsetattr(STDIN_FILENO
, TCSANOW
, &term
);
113 static void auth_loop(const char *username
)
115 struct timespec
*prompt_timeout
;
116 struct timespec
*wait_timeout
;
119 /* Get the vlock message from the environment. */
120 vlock_message
= getenv("VLOCK_MESSAGE");
122 if (vlock_message
== NULL
) {
123 if (console_switch_locked
)
124 vlock_message
= getenv("VLOCK_ALL_MESSAGE");
126 vlock_message
= getenv("VLOCK_CURRENT_MESSAGE");
129 /* Get the timeouts from the environment. */
130 prompt_timeout
= parse_seconds(getenv("VLOCK_PROMPT_TIMEOUT"));
132 wait_timeout
= parse_seconds(getenv("VLOCK_TIMEOUT"));
140 /* Print vlock message if there is one. */
141 if (vlock_message
&& *vlock_message
) {
142 fputs(vlock_message
, stderr
);
146 /* Wait for enter or escape to be pressed. */
147 c
= wait_for_character("\n\033", wait_timeout
);
149 /* Escape was pressed or the timeout occurred. */
150 if (c
== '\033' || c
== 0) {
152 plugin_hook("vlock_save");
153 /* Wait for any key to be pressed. */
154 c
= wait_for_character(NULL
, NULL
);
155 plugin_hook("vlock_save_abort");
157 /* Do not require enter to be pressed twice. */
165 if (auth(username
, prompt_timeout
))
171 if (auth("root", prompt_timeout
))
178 /* Free timeouts memory. */
180 free(prompt_timeout
);
184 static void call_end_hook(void)
186 (void) plugin_hook("vlock_end");
190 /* Lock the current terminal until proper authentication is received. */
191 int main(int argc
, char *const argv
[])
195 vlock_debug
= (getenv("VLOCK_DEBUG") != NULL
);
199 username
= get_username();
201 if (username
== NULL
)
202 fatal_perror("vlock-main: could not get username");
205 for (int i
= 1; i
< argc
; i
++)
206 if (!load_plugin(argv
[i
]))
207 fatal_error("vlock-main: loading plugin '%s' failed: %s", argv
[i
], STRERROR
);
209 ensure_atexit(unload_plugins
);
211 if (!resolve_dependencies()) {
215 fatal_error("vlock-main: error resolving plugin dependencies: %s", STRERROR
);
218 plugin_hook("vlock_start");
219 ensure_atexit(call_end_hook
);
220 #else /* !USE_PLUGINS */
221 /* Emulate pseudo plugin "all". */
222 if (argc
== 2 && (strcmp(argv
[1], "all") == 0)) {
223 if (!lock_console_switch()) {
224 if (errno
== ENOTTY
|| errno
== EINVAL
)
225 fatal_error("vlock-main: this terminal is not a virtual console");
227 fatal_perror("vlock-main: could not disable console switching");
230 ensure_atexit((void (*)(void))unlock_console_switch
);
231 } else if (argc
> 1) {
232 fatal_error("vlock-main: plugin support disabled");
237 ensure_atexit(restore_terminal
);