move help text from include/usage.src.h to debianutils/*.c e2fsprogs/*.c editors...
[platform/upstream/busybox.git] / 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 source tree.
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 //usage:#define vlock_trivial_usage
19 //usage:       "[-a]"
20 //usage:#define vlock_full_usage "\n\n"
21 //usage:       "Lock a virtual terminal. A password is required to unlock.\n"
22 //usage:     "\nOptions:"
23 //usage:     "\n        -a      Lock all VTs"
24
25 #include "libbb.h"
26
27 #ifdef __linux__
28 #include <sys/vt.h>
29
30 static void release_vt(int signo UNUSED_PARAM)
31 {
32         /* If -a, param is 0, which means:
33          * "no, kernel, we don't allow console switch away from us!" */
34         ioctl(STDIN_FILENO, VT_RELDISP, (unsigned long) !option_mask32);
35 }
36
37 static void acquire_vt(int signo UNUSED_PARAM)
38 {
39         /* ACK to kernel that switch to console is successful */
40         ioctl(STDIN_FILENO, VT_RELDISP, VT_ACKACQ);
41 }
42 #endif
43
44 int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45 int vlock_main(int argc UNUSED_PARAM, char **argv)
46 {
47 #ifdef __linux__
48         struct vt_mode vtm;
49         struct vt_mode ovtm;
50 #endif
51         struct termios term;
52         struct termios oterm;
53         struct passwd *pw;
54
55         pw = xgetpwuid(getuid());
56         opt_complementary = "=0"; /* no params! */
57         getopt32(argv, "a");
58
59         /* Ignore some signals so that we don't get killed by them */
60         bb_signals(0
61                 + (1 << SIGTSTP)
62                 + (1 << SIGTTIN)
63                 + (1 << SIGTTOU)
64                 + (1 << SIGHUP )
65                 + (1 << SIGCHLD) /* paranoia :) */
66                 + (1 << SIGQUIT)
67                 + (1 << SIGINT )
68                 , SIG_IGN);
69
70 #ifdef __linux__
71         /* We will use SIGUSRx for console switch control: */
72         /* 1: set handlers */
73         signal_SA_RESTART_empty_mask(SIGUSR1, release_vt);
74         signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt);
75         /* 2: unmask them */
76         sig_unblock(SIGUSR1);
77         sig_unblock(SIGUSR2);
78 #endif
79
80         /* Revert stdin/out to our controlling tty
81          * (or die if we have none) */
82         xmove_fd(xopen(CURRENT_TTY, O_RDWR), STDIN_FILENO);
83         xdup2(STDIN_FILENO, STDOUT_FILENO);
84
85 #ifdef __linux__
86         xioctl(STDIN_FILENO, VT_GETMODE, &vtm);
87         ovtm = vtm;
88         /* "console switches are controlled by us, not kernel!" */
89         vtm.mode = VT_PROCESS;
90         vtm.relsig = SIGUSR1;
91         vtm.acqsig = SIGUSR2;
92         ioctl(STDIN_FILENO, VT_SETMODE, &vtm);
93 #endif
94
95         tcgetattr(STDIN_FILENO, &oterm);
96         term = oterm;
97         term.c_iflag &= ~BRKINT;
98         term.c_iflag |= IGNBRK;
99         term.c_lflag &= ~ISIG;
100         term.c_lflag &= ~(ECHO | ECHOCTL);
101         tcsetattr_stdin_TCSANOW(&term);
102
103         while (1) {
104                 printf("Virtual console%s locked by %s.\n",
105                                 /* "s" if -a, else "": */ "s" + !option_mask32,
106                                 pw->pw_name
107                 );
108                 if (correct_password(pw)) {
109                         break;
110                 }
111                 bb_do_delay(LOGIN_FAIL_DELAY);
112                 puts("Incorrect password");
113         }
114
115 #ifdef __linux__
116         ioctl(STDIN_FILENO, VT_SETMODE, &ovtm);
117 #endif
118         tcsetattr_stdin_TCSANOW(&oterm);
119         fflush_stdout_and_exit(EXIT_SUCCESS);
120 }