cttyhack: add missing ';'
[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 "libbb.h"
20 #include <sys/vt.h>
21
22 enum { vfd = 3 };
23
24 /* static unsigned long o_lock_all; */
25
26 static void release_vt(int signo)
27 {
28         ioctl(vfd, VT_RELDISP, (unsigned long) !option_mask32 /*!o_lock_all*/);
29 }
30
31 static void acquire_vt(int signo)
32 {
33         ioctl(vfd, VT_RELDISP, VT_ACKACQ);
34 }
35
36 int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
37 int vlock_main(int argc, char **argv)
38 {
39         sigset_t sig;
40         struct sigaction sa;
41         struct vt_mode vtm;
42         struct termios term;
43         struct termios oterm;
44         struct vt_mode ovtm;
45         uid_t uid;
46         struct passwd *pw;
47
48         uid = getuid();
49         pw = getpwuid(uid);
50         if (pw == NULL)
51                 bb_error_msg_and_die("unknown uid %d", uid);
52
53         if (argc > 2) {
54                 bb_show_usage();
55         }
56
57         /*o_lock_all = */getopt32(argv, "a");
58
59         /* Avoid using statics - use constant fd */
60         xmove_fd(xopen(CURRENT_TTY, O_RDWR), vfd);
61         xioctl(vfd, VT_GETMODE, &vtm);
62
63         /* mask a bunch of signals */
64         sigprocmask(SIG_SETMASK, NULL, &sig);
65         sigdelset(&sig, SIGUSR1);
66         sigdelset(&sig, SIGUSR2);
67         sigaddset(&sig, SIGTSTP);
68         sigaddset(&sig, SIGTTIN);
69         sigaddset(&sig, SIGTTOU);
70         sigaddset(&sig, SIGHUP);
71         sigaddset(&sig, SIGCHLD);
72         sigaddset(&sig, SIGQUIT);
73         sigaddset(&sig, SIGINT);
74
75         sigemptyset(&(sa.sa_mask));
76         sa.sa_flags = SA_RESTART;
77         sa.sa_handler = release_vt;
78         sigaction(SIGUSR1, &sa, NULL);
79         sa.sa_handler = acquire_vt;
80         sigaction(SIGUSR2, &sa, NULL);
81
82         /* need to handle some signals so that we don't get killed by them */
83         sa.sa_handler = SIG_IGN;
84         sigaction(SIGHUP, &sa, NULL);
85         sigaction(SIGQUIT, &sa, NULL);
86         sigaction(SIGINT, &sa, NULL);
87         sigaction(SIGTSTP, &sa, NULL);
88
89         ovtm = vtm;
90         vtm.mode = VT_PROCESS;
91         vtm.relsig = SIGUSR1;
92         vtm.acqsig = SIGUSR2;
93         ioctl(vfd, VT_SETMODE, &vtm);
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_FILENO, TCSANOW, &term);
102
103         do {
104                 printf("Virtual console%s locked by %s.\n",
105                                 option_mask32 /*o_lock_all*/ ? "s" : "",
106                                 pw->pw_name);
107                 if (correct_password(pw)) {
108                         break;
109                 }
110                 bb_do_delay(FAIL_DELAY);
111                 puts("Password incorrect");
112         } while (1);
113
114         ioctl(vfd, VT_SETMODE, &ovtm);
115         tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
116         fflush_stdout_and_exit(0);
117 }