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