blob: 96c1f679a2137d5520e2cd53b2cd7d1eadd19532 [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
Rob Landleyd921b2e2006-08-03 15:41:12 +000018#include <sys/vt.h>
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000019#include "libbb.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000020
Denis Vlasenko68404f12008-03-17 09:00:54 +000021static void release_vt(int signo ATTRIBUTE_UNUSED)
Eric Andersen27f64e12002-06-23 04:24:25 +000022{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000023 /* If -a, param is 0, which means:
24 * "no, kernel, we don't allow console switch away from us!" */
25 ioctl(STDIN_FILENO, VT_RELDISP, (unsigned long) !option_mask32);
Eric Andersen27f64e12002-06-23 04:24:25 +000026}
27
Denis Vlasenko68404f12008-03-17 09:00:54 +000028static void acquire_vt(int signo ATTRIBUTE_UNUSED)
Eric Andersen27f64e12002-06-23 04:24:25 +000029{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000030 /* ACK to kernel that switch to console is successful */
31 ioctl(STDIN_FILENO, VT_RELDISP, VT_ACKACQ);
Eric Andersen27f64e12002-06-23 04:24:25 +000032}
33
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000034int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000035int vlock_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersen27f64e12002-06-23 04:24:25 +000036{
Eric Andersen27f64e12002-06-23 04:24:25 +000037 struct vt_mode vtm;
Eric Andersen27f64e12002-06-23 04:24:25 +000038 struct termios term;
Denis Vlasenko33b900f2007-09-27 10:12:02 +000039 struct termios oterm;
40 struct vt_mode ovtm;
41 uid_t uid;
42 struct passwd *pw;
Bernhard Reutner-Fischer8a0a83d2006-11-27 13:58:18 +000043
Denis Vlasenko33b900f2007-09-27 10:12:02 +000044 uid = getuid();
Bernhard Reutner-Fischer8a0a83d2006-11-27 13:58:18 +000045 pw = getpwuid(uid);
46 if (pw == NULL)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000047 bb_error_msg_and_die("unknown uid %d", (int)uid);
Eric Andersen27f64e12002-06-23 04:24:25 +000048
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000049 opt_complementary = "=0"; /* no params! */
50 getopt32(argv, "a");
Eric Andersen27f64e12002-06-23 04:24:25 +000051
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000052 /* Ignore some signals so that we don't get killed by them */
53 bb_signals(0
54 + (1 << SIGTSTP)
55 + (1 << SIGTTIN)
56 + (1 << SIGTTOU)
57 + (1 << SIGHUP )
58 + (1 << SIGCHLD) /* paranoia :) */
59 + (1 << SIGQUIT)
60 + (1 << SIGINT )
61 , SIG_IGN);
Rob Landley68eb9dd2005-12-19 02:50:10 +000062
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000063 /* We will use SIGUSRx for console switch control: */
64 /* 1: set handlers */
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000065 signal_SA_RESTART_empty_mask(SIGUSR1, release_vt);
66 signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000067 /* 2: unmask them */
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000068 sig_unblock(SIGUSR1);
69 sig_unblock(SIGUSR2);
Eric Andersen27f64e12002-06-23 04:24:25 +000070
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000071 /* Revert stdin/out to our controlling tty
72 * (or die if we have none) */
73 xmove_fd(xopen(CURRENT_TTY, O_RDWR), STDIN_FILENO);
74 xdup2(STDIN_FILENO, STDOUT_FILENO);
Eric Andersen27f64e12002-06-23 04:24:25 +000075
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000076 xioctl(STDIN_FILENO, VT_GETMODE, &vtm);
Eric Andersen27f64e12002-06-23 04:24:25 +000077 ovtm = vtm;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000078 /* "console switches are controlled by us, not kernel!" */
Eric Andersen27f64e12002-06-23 04:24:25 +000079 vtm.mode = VT_PROCESS;
80 vtm.relsig = SIGUSR1;
81 vtm.acqsig = SIGUSR2;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000082 ioctl(STDIN_FILENO, VT_SETMODE, &vtm);
Eric Andersen27f64e12002-06-23 04:24:25 +000083
84 tcgetattr(STDIN_FILENO, &oterm);
85 term = oterm;
86 term.c_iflag &= ~BRKINT;
87 term.c_iflag |= IGNBRK;
88 term.c_lflag &= ~ISIG;
89 term.c_lflag &= ~(ECHO | ECHOCTL);
90 tcsetattr(STDIN_FILENO, TCSANOW, &term);
91
92 do {
Denis Vlasenko33b900f2007-09-27 10:12:02 +000093 printf("Virtual console%s locked by %s.\n",
94 option_mask32 /*o_lock_all*/ ? "s" : "",
95 pw->pw_name);
Denis Vlasenkoa36a6762006-09-23 13:11:49 +000096 if (correct_password(pw)) {
Rob Landley68eb9dd2005-12-19 02:50:10 +000097 break;
Eric Andersen27f64e12002-06-23 04:24:25 +000098 }
Rob Landley84cb7672006-01-06 20:59:09 +000099 bb_do_delay(FAIL_DELAY);
Denis Vlasenkoa36a6762006-09-23 13:11:49 +0000100 puts("Password incorrect");
Eric Andersen27f64e12002-06-23 04:24:25 +0000101 } while (1);
Denis Vlasenko33b900f2007-09-27 10:12:02 +0000102
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000103 ioctl(STDIN_FILENO, VT_SETMODE, &ovtm);
Denis Vlasenko33b900f2007-09-27 10:12:02 +0000104 tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
Bernhard Reutner-Fischerb8bb27c2006-11-30 14:53:51 +0000105 fflush_stdout_and_exit(0);
Eric Andersen27f64e12002-06-23 04:24:25 +0000106}