src/auth-shadow.c: remove comment regarding check of sp_expire etc.
[vlock.git] / src / vlock-current.c
blob7edd35ca1d08e51eb8f4f022016fb7efcfda4b18
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>
25 #include <time.h>
27 #include "vlock.h"
29 /* Lock the current terminal until proper authentication is received. */
30 int main(void) {
31 char user[40];
32 char *vlock_message;
33 char *vlock_prompt_timeout;
34 struct timespec timeout;
35 struct timespec *timeout_p = NULL;
36 struct termios term, term_bak;
37 struct sigaction sa;
38 /* get the user id */
39 uid_t uid = getuid();
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) {
54 errno = 0;
56 /* get the password entry */
57 struct passwd *pw = getpwuid(uid);
59 if (pw == NULL) {
60 if (errno != 0)
61 perror("vlock-current: getpwuid() failed");
62 else
63 fprintf(stderr, "vlock-current: getpwuid() failed\n");
65 exit (111);
68 /* copy the username */
69 strncpy(user, pw->pw_name, sizeof user - 1);
70 user[sizeof user - 1] = '\0';
71 } else {
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) {
84 char *n;
85 timeout.tv_sec = strtol(vlock_prompt_timeout, &n, 10);
86 timeout.tv_nsec = 0;
89 if (*n == '\0' && timeout.tv_sec > 0)
90 timeout_p = &timeout;
93 /* disable terminal echoing and signals */
94 if (tcgetattr(STDIN_FILENO, &term) == 0) {
95 term_bak = term;
96 term.c_lflag &= ~(ECHO|ISIG);
97 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
100 for (;;) {
101 char c = 0;
103 /* clear the screen */
104 fprintf(stderr, CLEAR_SCREEN);
106 if (vlock_message)
107 /* print vlock message */
108 fprintf(stderr, "%s\n", vlock_message);
110 /* wait for enter to be pressed */
111 fprintf(stderr, "Please press [ENTER] to unlock.\n");
112 while (read(STDIN_FILENO, &c, 1) >= 0)
113 if (c == '\n')
114 break;
116 if (auth(user, timeout_p))
117 break;
118 else
119 sleep(1);
121 #ifndef NO_ROOT_PASS
122 if (auth("root", timeout_p))
123 break;
124 else
125 sleep(1);
126 #endif
129 /* restore the terminal */
130 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term_bak);
132 exit (0);