85f489c2286d1b5574e5a339139be1941c7fb391
[external/busybox.git] / util-linux / loginutils / vlock.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * vlock implementation for busybox
4  *
5  * Copyright (C) 2000 by spoon <spoon@ix.netcom.com>
6  * Written by spoon <spon@ix.netcom.com>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 /* Shoutz to Michael K. Johnson <johnsonm@redhat.com>, author of the
12  * original vlock.  I snagged a bunch of his code to write this
13  * minimalistic vlock.
14  */
15 /* Fixed by Erik Andersen to do passwords the tinylogin way...
16  * It now works with md5, sha1, etc passwords. */
17
18 #include <sys/vt.h>
19 #include "libbb.h"
20
21 static void release_vt(int signo UNUSED_PARAM)
22 {
23         /* If -a, param is 0, which means:
24          * "no, kernel, we don't allow console switch away from us!" */
25         ioctl(STDIN_FILENO, VT_RELDISP, (unsigned long) !option_mask32);
26 }
27
28 static void acquire_vt(int signo UNUSED_PARAM)
29 {
30         /* ACK to kernel that switch to console is successful */
31         ioctl(STDIN_FILENO, VT_RELDISP, VT_ACKACQ);
32 }
33
34 int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
35 int vlock_main(int argc UNUSED_PARAM, char **argv)
36 {
37         struct vt_mode vtm;
38         struct termios term;
39         struct termios oterm;
40         struct vt_mode ovtm;
41         struct passwd *pw;
42
43         pw = xgetpwuid(getuid());
44         opt_complementary = "=0"; /* no params! */
45         getopt32(argv, "a");
46
47         /* Ignore some signals so that we don't get killed by them */
48         bb_signals(0
49                 + (1 << SIGTSTP)
50                 + (1 << SIGTTIN)
51                 + (1 << SIGTTOU)
52                 + (1 << SIGHUP )
53                 + (1 << SIGCHLD) /* paranoia :) */
54                 + (1 << SIGQUIT)
55                 + (1 << SIGINT )
56                 , SIG_IGN);
57
58         /* We will use SIGUSRx for console switch control: */
59         /* 1: set handlers */
60         signal_SA_RESTART_empty_mask(SIGUSR1, release_vt);
61         signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt);
62         /* 2: unmask them */
63         sig_unblock(SIGUSR1);
64         sig_unblock(SIGUSR2);
65
66         /* Revert stdin/out to our controlling tty
67          * (or die if we have none) */
68         xmove_fd(xopen(CURRENT_TTY, O_RDWR), STDIN_FILENO);
69         xdup2(STDIN_FILENO, STDOUT_FILENO);
70
71         xioctl(STDIN_FILENO, VT_GETMODE, &vtm);
72         ovtm = vtm;
73         /* "console switches are controlled by us, not kernel!" */
74         vtm.mode = VT_PROCESS;
75         vtm.relsig = SIGUSR1;
76         vtm.acqsig = SIGUSR2;
77         ioctl(STDIN_FILENO, VT_SETMODE, &vtm);
78
79         tcgetattr(STDIN_FILENO, &oterm);
80         term = oterm;
81         term.c_iflag &= ~BRKINT;
82         term.c_iflag |= IGNBRK;
83         term.c_lflag &= ~ISIG;
84         term.c_lflag &= ~(ECHO | ECHOCTL);
85         tcsetattr_stdin_TCSANOW(&term);
86
87         do {
88                 printf("Virtual console%s locked by %s.\n",
89                                 option_mask32 /*o_lock_all*/ ? "s" : "",
90                                 pw->pw_name);
91                 if (correct_password(pw)) {
92                         break;
93                 }
94                 bb_do_delay(FAIL_DELAY);
95                 puts("Password incorrect");
96         } while (1);
97
98         ioctl(STDIN_FILENO, VT_SETMODE, &ovtm);
99         tcsetattr_stdin_TCSANOW(&oterm);
100         fflush_stdout_and_exit(EXIT_SUCCESS);
101 }