remove USER_KILL compile option, only root can kill vlock now
[vlock.git] / src / vlock-current.c
blob91ff6f99dee190feff66580ba9a883e9f707de77
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(SIGHUP, SIG_IGN);
41 signal(SIGQUIT, SIG_IGN);
42 signal(SIGTSTP, SIG_IGN);
44 if (uid > 0 || envuser == NULL) {
45 /* get the password entry */
46 struct passwd *pw = getpwuid(uid);
48 if (pw == NULL) {
49 if (errno != 0)
50 perror("vlock-current: getpwuid() failed");
51 else
52 fprintf(stderr, "vlock-current: getpwuid() failed\n");
54 exit (111);
57 /* copy the username */
58 strncpy(user, pw->pw_name, sizeof user - 1);
59 user[sizeof user - 1] = '\0';
60 } else {
61 /* copy the username */
62 strncpy(user, envuser, sizeof user - 1);
63 user[sizeof user - 1] = '\0';
66 /* get the vlock message from the environment */
67 vlock_message = getenv("VLOCK_MESSAGE");
69 /* disable terminal echoing and signals */
70 if (tcgetattr(STDIN_FILENO, &term) == 0) {
71 term_bak = term;
72 term.c_lflag &= ~(ECHO|ISIG);
73 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
76 for (;;) {
77 /* clear the screen */
78 fprintf(stderr, CLEAR_SCREEN);
80 if (vlock_message)
81 /* print vlock message */
82 fprintf(stderr, "%s\n", vlock_message);
84 /* wait for enter to be pressed */
85 fprintf(stderr, "Please press [ENTER] to unlock.\n");
86 while (fgetc(stdin) != '\n');
88 if (auth(user))
89 break;
90 else
91 sleep(1);
93 #ifndef NO_ROOT_PASS
94 if (auth("root"))
95 break;
96 else
97 sleep(1);
98 #endif
101 /* restore the terminal */
102 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term_bak);
104 exit (0);