blob: cec1d0ab0a494ab8b0c8448c80fddc14037955ca [file] [log] [blame]
Eric Andersen27f64e12002-06-23 04:24:25 +00001/* 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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen27f64e12002-06-23 04:24:25 +00009 */
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>
Manuel Novoa III cad53642003-03-19 09:13:01 +000019#include <stdlib.h>
Eric Andersen27f64e12002-06-23 04:24:25 +000020#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
31static struct passwd *pw;
Eric Andersen27f64e12002-06-23 04:24:25 +000032static struct vt_mode ovtm;
33static struct termios oterm;
34static int vfd;
Rob Landley68eb9dd2005-12-19 02:50:10 +000035static unsigned long o_lock_all;
Eric Andersen27f64e12002-06-23 04:24:25 +000036
37static 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
45static void acquire_vt(int signo)
46{
47 ioctl(vfd, VT_RELDISP, VT_ACKACQ);
48}
49
50static void restore_terminal(void)
51{
52 ioctl(vfd, VT_SETMODE, &ovtm);
53 tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
54}
55
Rob Landleydfba7412006-03-06 20:47:33 +000056int vlock_main(int argc, char **argv)
Eric Andersen27f64e12002-06-23 04:24:25 +000057{
58 sigset_t sig;
59 struct sigaction sa;
60 struct vt_mode vtm;
Eric Andersen27f64e12002-06-23 04:24:25 +000061 struct termios term;
62
63 if (argc > 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 bb_show_usage();
Eric Andersen27f64e12002-06-23 04:24:25 +000065 }
66
Rob Landley68eb9dd2005-12-19 02:50:10 +000067 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());
Eric Andersen27f64e12002-06-23 04:24:25 +000071 }
72
Rob Landley68eb9dd2005-12-19 02:50:10 +000073 vfd = bb_xopen(CURRENT_TTY, O_RDWR);
Eric Andersen27f64e12002-06-23 04:24:25 +000074
75 if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
Rob Landley68eb9dd2005-12-19 02:50:10 +000076 bb_perror_msg_and_die("VT_GETMODE");
77 }
Eric Andersen27f64e12002-06-23 04:24:25 +000078
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 {
Rob Landley68eb9dd2005-12-19 02:50:10 +0000120 printf("Virtual Console%s locked.\n%s's ", (o_lock_all) ? "s" : "", pw->pw_name);
Eric Andersen27f64e12002-06-23 04:24:25 +0000121 fflush(stdout);
Rob Landley68eb9dd2005-12-19 02:50:10 +0000122 if (correct_password (pw)) {
123 break;
Eric Andersen27f64e12002-06-23 04:24:25 +0000124 }
Rob Landley84cb7672006-01-06 20:59:09 +0000125 bb_do_delay(FAIL_DELAY);
Rob Landley68eb9dd2005-12-19 02:50:10 +0000126 puts("Password incorrect.");
Eric Andersen27f64e12002-06-23 04:24:25 +0000127 } while (1);
Rob Landley68eb9dd2005-12-19 02:50:10 +0000128 restore_terminal();
129 return 0;
Eric Andersen27f64e12002-06-23 04:24:25 +0000130}