src/vlock-new.c: slightly reformat get_console_name()
[vlock.git] / src / vlock-current.c
blob0bc547a171f11bae39606cc944b073a4c1c6bc63
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
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>
26 #include "vlock.h"
28 /* Lock the current terminal until proper authentication is received. */
29 int main(void) {
30 char user[40];
31 char *vlock_message;
32 struct termios term, term_bak;
33 /* get the user id */
34 uid_t uid = getuid();
35 /* get the user name from the environment */
36 char *envuser = getenv("USER");
38 /* ignore some signals */
39 signal(SIGINT, SIG_IGN);
40 signal(SIGQUIT, SIG_IGN);
41 signal(SIGTSTP, SIG_IGN);
43 if (uid > 0 || envuser == NULL) {
44 /* get the password entry */
45 struct passwd *pw = getpwuid(uid);
47 if (pw == NULL) {
48 if (errno != 0)
49 perror("vlock-current: getpwuid() failed");
50 else
51 fprintf(stderr, "vlock-current: getpwuid() failed\n");
53 exit (111);
56 /* copy the username */
57 strncpy(user, pw->pw_name, sizeof user - 1);
58 user[sizeof user - 1] = '\0';
59 } else {
60 /* copy the username */
61 strncpy(user, envuser, sizeof user - 1);
62 user[sizeof user - 1] = '\0';
65 /* get the vlock message from the environment */
66 vlock_message = getenv("VLOCK_MESSAGE");
68 /* disable terminal echoing and signals */
69 if (tcgetattr(STDIN_FILENO, &term) == 0) {
70 term_bak = term;
71 term.c_lflag &= ~(ECHO|ISIG);
72 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
75 for (;;) {
76 /* clear the screen */
77 fprintf(stderr, CLEAR_SCREEN);
79 if (vlock_message)
80 /* print vlock message */
81 fprintf(stderr, "%s\n", vlock_message);
83 /* wait for enter to be pressed */
84 fprintf(stderr, "Please press [ENTER] to unlock.\n");
85 while (fgetc(stdin) != '\n');
87 if (auth(user))
88 break;
89 else
90 sleep(1);
92 #ifndef NO_ROOT_PASS
93 if (auth("root"))
94 break;
95 else
96 sleep(1);
97 #endif
100 /* restore the terminal */
101 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term_bak);
103 exit (0);