More website cleanup.
[oweals/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 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 <stdio.h>
19 #include <stdlib.h>
20 #include <sys/vt.h>
21 #include <signal.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <sys/ioctl.h>
27 #include <termios.h>
28
29 #include "busybox.h"
30
31 static struct passwd *pw;
32 static struct vt_mode ovtm;
33 static struct termios oterm;
34 static int vfd;
35 static unsigned long o_lock_all;
36
37 static void release_vt(int signo)
38 {
39         if (!o_lock_all)
40                 ioctl(vfd, VT_RELDISP, 1);
41         else
42                 ioctl(vfd, VT_RELDISP, 0);
43 }
44
45 static void acquire_vt(int signo)
46 {
47         ioctl(vfd, VT_RELDISP, VT_ACKACQ);
48 }
49
50 static void restore_terminal(void)
51 {
52         ioctl(vfd, VT_SETMODE, &ovtm);
53         tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
54 }
55
56 int vlock_main(int argc, char **argv)
57 {
58         sigset_t sig;
59         struct sigaction sa;
60         struct vt_mode vtm;
61         struct termios term;
62
63         if (argc > 2) {
64                 bb_show_usage();
65         }
66
67         o_lock_all = bb_getopt_ulflags (argc, argv, "a");
68
69         if((pw = getpwuid(getuid())) == NULL) {
70                 bb_error_msg_and_die("Unknown uid %d", getuid());
71         }
72
73         vfd = bb_xopen(CURRENT_TTY, O_RDWR);
74
75         if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
76                 bb_perror_msg_and_die("VT_GETMODE");
77         }
78
79         /* mask a bunch of signals */
80         sigprocmask(SIG_SETMASK, NULL, &sig);
81         sigdelset(&sig, SIGUSR1);
82         sigdelset(&sig, SIGUSR2);
83         sigaddset(&sig, SIGTSTP);
84         sigaddset(&sig, SIGTTIN);
85         sigaddset(&sig, SIGTTOU);
86         sigaddset(&sig, SIGHUP);
87         sigaddset(&sig, SIGCHLD);
88         sigaddset(&sig, SIGQUIT);
89         sigaddset(&sig, SIGINT);
90
91         sigemptyset(&(sa.sa_mask));
92         sa.sa_flags = SA_RESTART;
93         sa.sa_handler = release_vt;
94         sigaction(SIGUSR1, &sa, NULL);
95         sa.sa_handler = acquire_vt;
96         sigaction(SIGUSR2, &sa, NULL);
97
98         /* need to handle some signals so that we don't get killed by them */
99         sa.sa_handler = SIG_IGN;
100         sigaction(SIGHUP, &sa, NULL);
101         sigaction(SIGQUIT, &sa, NULL);
102         sigaction(SIGINT, &sa, NULL);
103         sigaction(SIGTSTP, &sa, NULL);
104
105         ovtm = vtm;
106         vtm.mode = VT_PROCESS;
107         vtm.relsig = SIGUSR1;
108         vtm.acqsig = SIGUSR2;
109         ioctl(vfd, VT_SETMODE, &vtm);
110
111         tcgetattr(STDIN_FILENO, &oterm);
112         term = oterm;
113         term.c_iflag &= ~BRKINT;
114         term.c_iflag |= IGNBRK;
115         term.c_lflag &= ~ISIG;
116         term.c_lflag &= ~(ECHO | ECHOCTL);
117         tcsetattr(STDIN_FILENO, TCSANOW, &term);
118
119         do {
120                 printf("Virtual Console%s locked.\n%s's ", (o_lock_all) ? "s" : "", pw->pw_name);
121                 fflush(stdout);
122                 if (correct_password (pw)) {
123                         break;
124                 }
125                 bb_do_delay(FAIL_DELAY);
126                 puts("Password incorrect.");
127         } while (1);
128         restore_terminal();
129         return 0;
130 }