[821] | 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 | *
|
---|
[2725] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 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 |
|
---|
[3232] | 18 | //usage:#define vlock_trivial_usage
|
---|
| 19 | //usage: "[-a]"
|
---|
| 20 | //usage:#define vlock_full_usage "\n\n"
|
---|
| 21 | //usage: "Lock a virtual terminal. A password is required to unlock.\n"
|
---|
| 22 | //usage: "\n -a Lock all VTs"
|
---|
| 23 |
|
---|
[1765] | 24 | #include "libbb.h"
|
---|
[2725] | 25 |
|
---|
| 26 | #ifdef __linux__
|
---|
[821] | 27 | #include <sys/vt.h>
|
---|
| 28 |
|
---|
[2725] | 29 | static void release_vt(int signo UNUSED_PARAM)
|
---|
[821] | 30 | {
|
---|
[2725] | 31 | /* If -a, param is 0, which means:
|
---|
| 32 | * "no, kernel, we don't allow console switch away from us!" */
|
---|
| 33 | ioctl(STDIN_FILENO, VT_RELDISP, (unsigned long) !option_mask32);
|
---|
[821] | 34 | }
|
---|
| 35 |
|
---|
[2725] | 36 | static void acquire_vt(int signo UNUSED_PARAM)
|
---|
[821] | 37 | {
|
---|
[2725] | 38 | /* ACK to kernel that switch to console is successful */
|
---|
| 39 | ioctl(STDIN_FILENO, VT_RELDISP, VT_ACKACQ);
|
---|
[821] | 40 | }
|
---|
[2725] | 41 | #endif
|
---|
[821] | 42 |
|
---|
[2725] | 43 | int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 44 | int vlock_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 45 | {
|
---|
[2725] | 46 | #ifdef __linux__
|
---|
[821] | 47 | struct vt_mode vtm;
|
---|
[2725] | 48 | struct vt_mode ovtm;
|
---|
| 49 | #endif
|
---|
[821] | 50 | struct termios term;
|
---|
[2725] | 51 | struct termios oterm;
|
---|
| 52 | struct passwd *pw;
|
---|
[821] | 53 |
|
---|
[2725] | 54 | pw = xgetpwuid(getuid());
|
---|
| 55 | opt_complementary = "=0"; /* no params! */
|
---|
| 56 | getopt32(argv, "a");
|
---|
[1765] | 57 |
|
---|
[2725] | 58 | /* Ignore some signals so that we don't get killed by them */
|
---|
| 59 | bb_signals(0
|
---|
| 60 | + (1 << SIGTSTP)
|
---|
| 61 | + (1 << SIGTTIN)
|
---|
| 62 | + (1 << SIGTTOU)
|
---|
| 63 | + (1 << SIGHUP )
|
---|
| 64 | + (1 << SIGCHLD) /* paranoia :) */
|
---|
| 65 | + (1 << SIGQUIT)
|
---|
| 66 | + (1 << SIGINT )
|
---|
| 67 | , SIG_IGN);
|
---|
[821] | 68 |
|
---|
[2725] | 69 | #ifdef __linux__
|
---|
| 70 | /* We will use SIGUSRx for console switch control: */
|
---|
| 71 | /* 1: set handlers */
|
---|
| 72 | signal_SA_RESTART_empty_mask(SIGUSR1, release_vt);
|
---|
| 73 | signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt);
|
---|
| 74 | /* 2: unmask them */
|
---|
| 75 | sig_unblock(SIGUSR1);
|
---|
| 76 | sig_unblock(SIGUSR2);
|
---|
| 77 | #endif
|
---|
[821] | 78 |
|
---|
[2725] | 79 | /* Revert stdin/out to our controlling tty
|
---|
| 80 | * (or die if we have none) */
|
---|
| 81 | xmove_fd(xopen(CURRENT_TTY, O_RDWR), STDIN_FILENO);
|
---|
| 82 | xdup2(STDIN_FILENO, STDOUT_FILENO);
|
---|
[821] | 83 |
|
---|
[2725] | 84 | #ifdef __linux__
|
---|
| 85 | xioctl(STDIN_FILENO, VT_GETMODE, &vtm);
|
---|
[821] | 86 | ovtm = vtm;
|
---|
[2725] | 87 | /* "console switches are controlled by us, not kernel!" */
|
---|
[821] | 88 | vtm.mode = VT_PROCESS;
|
---|
| 89 | vtm.relsig = SIGUSR1;
|
---|
| 90 | vtm.acqsig = SIGUSR2;
|
---|
[2725] | 91 | ioctl(STDIN_FILENO, VT_SETMODE, &vtm);
|
---|
| 92 | #endif
|
---|
[821] | 93 |
|
---|
| 94 | tcgetattr(STDIN_FILENO, &oterm);
|
---|
| 95 | term = oterm;
|
---|
| 96 | term.c_iflag &= ~BRKINT;
|
---|
| 97 | term.c_iflag |= IGNBRK;
|
---|
| 98 | term.c_lflag &= ~ISIG;
|
---|
| 99 | term.c_lflag &= ~(ECHO | ECHOCTL);
|
---|
[2725] | 100 | tcsetattr_stdin_TCSANOW(&term);
|
---|
[821] | 101 |
|
---|
[3232] | 102 | while (1) {
|
---|
[2725] | 103 | printf("Virtual console%s locked by %s.\n",
|
---|
[3232] | 104 | /* "s" if -a, else "": */ "s" + !option_mask32,
|
---|
| 105 | pw->pw_name
|
---|
| 106 | );
|
---|
[1765] | 107 | if (correct_password(pw)) {
|
---|
[821] | 108 | break;
|
---|
| 109 | }
|
---|
[3232] | 110 | bb_do_delay(LOGIN_FAIL_DELAY);
|
---|
| 111 | puts("Incorrect password");
|
---|
| 112 | }
|
---|
[2725] | 113 |
|
---|
| 114 | #ifdef __linux__
|
---|
| 115 | ioctl(STDIN_FILENO, VT_SETMODE, &ovtm);
|
---|
| 116 | #endif
|
---|
| 117 | tcsetattr_stdin_TCSANOW(&oterm);
|
---|
| 118 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
[821] | 119 | }
|
---|