Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 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 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 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 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 18 | #include <sys/vt.h> |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 19 | #include "libbb.h" |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 21 | static void release_vt(int signo ATTRIBUTE_UNUSED) |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 22 | { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 23 | /* 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 Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 28 | static void acquire_vt(int signo ATTRIBUTE_UNUSED) |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 29 | { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 30 | /* ACK to kernel that switch to console is successful */ |
| 31 | ioctl(STDIN_FILENO, VT_RELDISP, VT_ACKACQ); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 34 | int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 35 | int vlock_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 36 | { |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 37 | struct vt_mode vtm; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 38 | struct termios term; |
Denis Vlasenko | 33b900f | 2007-09-27 10:12:02 +0000 | [diff] [blame] | 39 | struct termios oterm; |
| 40 | struct vt_mode ovtm; |
| 41 | uid_t uid; |
| 42 | struct passwd *pw; |
Bernhard Reutner-Fischer | 8a0a83d | 2006-11-27 13:58:18 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 33b900f | 2007-09-27 10:12:02 +0000 | [diff] [blame] | 44 | uid = getuid(); |
Bernhard Reutner-Fischer | 8a0a83d | 2006-11-27 13:58:18 +0000 | [diff] [blame] | 45 | pw = getpwuid(uid); |
| 46 | if (pw == NULL) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 47 | bb_error_msg_and_die("unknown uid %d", (int)uid); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 49 | opt_complementary = "=0"; /* no params! */ |
| 50 | getopt32(argv, "a"); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 52 | /* 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 Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 63 | /* We will use SIGUSRx for console switch control: */ |
| 64 | /* 1: set handlers */ |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 65 | signal_SA_RESTART_empty_mask(SIGUSR1, release_vt); |
| 66 | signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 67 | /* 2: unmask them */ |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 68 | sig_unblock(SIGUSR1); |
| 69 | sig_unblock(SIGUSR2); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 71 | /* 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 Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 76 | xioctl(STDIN_FILENO, VT_GETMODE, &vtm); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 77 | ovtm = vtm; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 78 | /* "console switches are controlled by us, not kernel!" */ |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 79 | vtm.mode = VT_PROCESS; |
| 80 | vtm.relsig = SIGUSR1; |
| 81 | vtm.acqsig = SIGUSR2; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 82 | ioctl(STDIN_FILENO, VT_SETMODE, &vtm); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 83 | |
| 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 Vlasenko | 33b900f | 2007-09-27 10:12:02 +0000 | [diff] [blame] | 93 | printf("Virtual console%s locked by %s.\n", |
| 94 | option_mask32 /*o_lock_all*/ ? "s" : "", |
| 95 | pw->pw_name); |
Denis Vlasenko | a36a676 | 2006-09-23 13:11:49 +0000 | [diff] [blame] | 96 | if (correct_password(pw)) { |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 97 | break; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 98 | } |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 99 | bb_do_delay(FAIL_DELAY); |
Denis Vlasenko | a36a676 | 2006-09-23 13:11:49 +0000 | [diff] [blame] | 100 | puts("Password incorrect"); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 101 | } while (1); |
Denis Vlasenko | 33b900f | 2007-09-27 10:12:02 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 103 | ioctl(STDIN_FILENO, VT_SETMODE, &ovtm); |
Denis Vlasenko | 33b900f | 2007-09-27 10:12:02 +0000 | [diff] [blame] | 104 | tcsetattr(STDIN_FILENO, TCSANOW, &oterm); |
Bernhard Reutner-Fischer | b8bb27c | 2006-11-30 14:53:51 +0000 | [diff] [blame] | 105 | fflush_stdout_and_exit(0); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 106 | } |