src/vlock-main.c: check for error in dependency handling
[vlock.git] / src / vlock-main.c
blobdd922d5720e9d61a3f289670d7379414e7e44dc4
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
10 * the author.
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
18 #include <pwd.h>
20 #include <termios.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <errno.h>
25 #include <time.h>
27 #include "prompt.h"
28 #include "auth.h"
29 #include "console_switch.h"
30 #include "util.h"
32 #ifdef USE_PLUGINS
33 #include "plugins.h"
34 #endif
36 int vlock_debug = 0;
38 #define ensure_atexit(func) \
39 do { \
40 if (atexit(func) != 0) \
41 fatal_perror("vlock-main: atexit() failed"); \
42 } while (0)
44 static char *get_username(void)
46 uid_t uid = getuid();
47 char *username = NULL;
49 /* Get the user name from the environment if started as root. */
50 if (uid == 0)
51 username = getenv("USER");
53 if (username == NULL) {
54 struct passwd *pw;
56 /* Get the password entry. */
57 pw = getpwuid(uid);
59 if (pw == NULL)
60 return NULL;
62 username = pw->pw_name;
65 return strdup(username);
68 static void terminate(int signum)
70 if (signum == SIGTERM)
71 fprintf(stderr, "vlock-main: Terminated!\n");
72 else if (signum == SIGABRT)
73 fprintf(stderr, "vlock-main: Aborted!\n");
75 exit(1);
78 static void block_signals(void)
80 struct sigaction sa;
82 /* Ignore some signals. */
83 /* These signals shouldn't be delivered anyway, because terminal signals are
84 * disabled below. */
85 (void) sigemptyset(&(sa.sa_mask));
86 sa.sa_flags = SA_RESTART;
87 sa.sa_handler = SIG_IGN;
88 (void) sigaction(SIGINT, &sa, NULL);
89 (void) sigaction(SIGQUIT, &sa, NULL);
90 (void) sigaction(SIGTSTP, &sa, NULL);
92 /* Install special handler for TERM and ABRT. */
93 sa.sa_flags = SA_RESETHAND;
94 sa.sa_handler = terminate;
95 (void) sigaction(SIGTERM, &sa, NULL);
96 (void) sigaction(SIGABRT, &sa, NULL);
99 static struct termios term;
100 static tcflag_t lflag;
102 static void secure_terminal(void)
104 /* Disable terminal echoing and signals. */
105 (void) tcgetattr(STDIN_FILENO, &term);
106 lflag = term.c_lflag;
107 term.c_lflag &= ~(ECHO | ISIG);
108 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
111 static void restore_terminal(void)
113 /* Restore the terminal. */
114 term.c_lflag = lflag;
115 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
118 static void auth_loop(const char *username)
120 struct timespec *prompt_timeout;
121 struct timespec *wait_timeout;
122 char *vlock_message;
124 /* Get the vlock message from the environment. */
125 vlock_message = getenv("VLOCK_MESSAGE");
127 if (vlock_message == NULL) {
128 if (console_switch_locked)
129 vlock_message = getenv("VLOCK_ALL_MESSAGE");
130 else
131 vlock_message = getenv("VLOCK_CURRENT_MESSAGE");
134 /* Get the timeouts from the environment. */
135 prompt_timeout = parse_seconds(getenv("VLOCK_PROMPT_TIMEOUT"));
136 #ifdef USE_PLUGINS
137 wait_timeout = parse_seconds(getenv("VLOCK_TIMEOUT"));
138 #else
139 wait_timeout = NULL;
140 #endif
142 for (;;) {
143 char c;
145 /* Print vlock message if there is one. */
146 if (vlock_message && *vlock_message) {
147 fputs(vlock_message, stderr);
148 fputc('\n', stderr);
151 /* Wait for enter or escape to be pressed. */
152 c = wait_for_character("\n\033", wait_timeout);
154 /* Escape was pressed or the timeout occurred. */
155 if (c == '\033' || c == 0) {
156 #ifdef USE_PLUGINS
157 plugin_hook("vlock_save");
158 /* Wait for any key to be pressed. */
159 c = wait_for_character(NULL, NULL);
160 plugin_hook("vlock_save_abort");
162 /* Do not require enter to be pressed twice. */
163 if (c != '\n')
164 continue;
165 #else
166 continue;
167 #endif
170 if (auth(username, prompt_timeout))
171 break;
172 else
173 sleep(1);
175 #ifndef NO_ROOT_PASS
176 if (auth("root", prompt_timeout))
177 break;
178 else
179 sleep(1);
180 #endif
183 /* Free timeouts memory. */
184 free(wait_timeout);
185 free(prompt_timeout);
188 #ifdef USE_PLUGINS
189 static void call_end_hook(void)
191 (void) plugin_hook("vlock_end");
193 #endif
195 /* Lock the current terminal until proper authentication is received. */
196 int main(int argc, char *const argv[])
198 char *username;
200 vlock_debug = (getenv("VLOCK_DEBUG") != NULL);
202 block_signals();
204 username = get_username();
206 if (username == NULL)
207 fatal_perror("vlock-main: could not get username");
209 #ifdef USE_PLUGINS
210 for (int i = 1; i < argc; i++)
211 if (!load_plugin(argv[i]))
212 fatal_error("vlock-main: loading plugin '%s' failed: %s", argv[i], STRERROR);
214 ensure_atexit(unload_plugins);
216 if (!resolve_dependencies()) {
217 if (errno == 0)
218 exit(EXIT_FAILURE);
219 else
220 fatal_error("vlock-main: error resolving plugin dependencies: %s", STRERROR);
223 plugin_hook("vlock_start");
224 ensure_atexit(call_end_hook);
225 #else /* !USE_PLUGINS */
226 /* Emulate pseudo plugin "all". */
227 if (argc == 2 && (strcmp(argv[1], "all") == 0)) {
228 if (!lock_console_switch()) {
229 if (errno == ENOTTY || errno == EINVAL)
230 fatal_error("vlock-main: this terminal is not a virtual console");
231 else
232 fatal_perror("vlock-main: could not disable console switching");
235 ensure_atexit((void (*)(void))unlock_console_switch);
236 } else if (argc > 1) {
237 fatal_error("vlock-main: plugin support disabled");
239 #endif
241 secure_terminal();
242 ensure_atexit(restore_terminal);
244 auth_loop(username);
246 free(username);
248 exit(0);