1414613de510e9c69095f826e069c79977b2058f
[oweals/busybox.git] / loginutils / vlock.c
1 /* vi: set sw=4 ts=4: */
2
3 /*
4  * vlock implementation for busybox
5  *
6  * Copyright (C) 2000 by spoon <spoon@ix.netcom.com>
7  * Written by spoon <spon@ix.netcom.com>
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 /* Shoutz to Michael K. Johnson <johnsonm@redhat.com>, author of the
13  * original vlock.  I snagged a bunch of his code to write this
14  * minimalistic vlock.
15  */
16 /* Fixed by Erik Andersen to do passwords the tinylogin way...
17  * It now works with md5, sha1, etc passwords. */
18
19 #include "busybox.h"
20 #include <sys/vt.h>
21
22 static struct passwd *pw;
23 static struct vt_mode ovtm;
24 static struct termios oterm;
25 static int vfd;
26 static unsigned long o_lock_all;
27
28 static void release_vt(int signo)
29 {
30         ioctl(vfd, VT_RELDISP, !o_lock_all);
31 }
32
33 static void acquire_vt(int signo)
34 {
35         ioctl(vfd, VT_RELDISP, VT_ACKACQ);
36 }
37
38 static void restore_terminal(void)
39 {
40         ioctl(vfd, VT_SETMODE, &ovtm);
41         tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
42 }
43
44 int vlock_main(int argc, char **argv)
45 {
46         sigset_t sig;
47         struct sigaction sa;
48         struct vt_mode vtm;
49         struct termios term;
50         uid_t uid = getuid();
51
52         pw = getpwuid(uid);
53         if (pw == NULL)
54                 bb_error_msg_and_die("unknown uid %d", uid);
55
56         if (argc > 2) {
57                 bb_show_usage();
58         }
59
60         o_lock_all = getopt32(argc, argv, "a");
61
62         vfd = xopen(CURRENT_TTY, O_RDWR);
63
64         if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
65                 bb_perror_msg_and_die("VT_GETMODE");
66         }
67
68         /* mask a bunch of signals */
69         sigprocmask(SIG_SETMASK, NULL, &sig);
70         sigdelset(&sig, SIGUSR1);
71         sigdelset(&sig, SIGUSR2);
72         sigaddset(&sig, SIGTSTP);
73         sigaddset(&sig, SIGTTIN);
74         sigaddset(&sig, SIGTTOU);
75         sigaddset(&sig, SIGHUP);
76         sigaddset(&sig, SIGCHLD);
77         sigaddset(&sig, SIGQUIT);
78         sigaddset(&sig, SIGINT);
79
80         sigemptyset(&(sa.sa_mask));
81         sa.sa_flags = SA_RESTART;
82         sa.sa_handler = release_vt;
83         sigaction(SIGUSR1, &sa, NULL);
84         sa.sa_handler = acquire_vt;
85         sigaction(SIGUSR2, &sa, NULL);
86
87         /* need to handle some signals so that we don't get killed by them */
88         sa.sa_handler = SIG_IGN;
89         sigaction(SIGHUP, &sa, NULL);
90         sigaction(SIGQUIT, &sa, NULL);
91         sigaction(SIGINT, &sa, NULL);
92         sigaction(SIGTSTP, &sa, NULL);
93
94         ovtm = vtm;
95         vtm.mode = VT_PROCESS;
96         vtm.relsig = SIGUSR1;
97         vtm.acqsig = SIGUSR2;
98         ioctl(vfd, VT_SETMODE, &vtm);
99
100         tcgetattr(STDIN_FILENO, &oterm);
101         term = oterm;
102         term.c_iflag &= ~BRKINT;
103         term.c_iflag |= IGNBRK;
104         term.c_lflag &= ~ISIG;
105         term.c_lflag &= ~(ECHO | ECHOCTL);
106         tcsetattr(STDIN_FILENO, TCSANOW, &term);
107
108         do {
109                 printf("Virtual Console%s locked by %s.\n", (o_lock_all) ? "s" : "", pw->pw_name);
110                 if (correct_password(pw)) {
111                         break;
112                 }
113                 bb_do_delay(FAIL_DELAY);
114                 puts("Password incorrect");
115         } while (1);
116         restore_terminal();
117         return 0;
118 }