Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 2 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 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 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 9 | * 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] | 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 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 19 | #include "busybox.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 20 | #include <sys/vt.h> |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 21 | |
| 22 | static struct passwd *pw; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 23 | static struct vt_mode ovtm; |
| 24 | static struct termios oterm; |
| 25 | static int vfd; |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 26 | static unsigned long o_lock_all; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 27 | |
| 28 | static void release_vt(int signo) |
| 29 | { |
Bernhard Reutner-Fischer | 8a0a83d | 2006-11-27 13:58:18 +0000 | [diff] [blame] | 30 | ioctl(vfd, VT_RELDISP, !o_lock_all); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void acquire_vt(int signo) |
| 34 | { |
| 35 | ioctl(vfd, VT_RELDISP, VT_ACKACQ); |
| 36 | } |
| 37 | |
| 38 | static void restore_terminal(void) |
| 39 | { |
| 40 | ioctl(vfd, VT_SETMODE, &ovtm); |
| 41 | tcsetattr(STDIN_FILENO, TCSANOW, &oterm); |
| 42 | } |
| 43 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 44 | int vlock_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 45 | int vlock_main(int argc, char **argv) |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 46 | { |
| 47 | sigset_t sig; |
| 48 | struct sigaction sa; |
| 49 | struct vt_mode vtm; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 50 | struct termios term; |
Bernhard Reutner-Fischer | 8a0a83d | 2006-11-27 13:58:18 +0000 | [diff] [blame] | 51 | uid_t uid = getuid(); |
| 52 | |
| 53 | pw = getpwuid(uid); |
| 54 | if (pw == NULL) |
| 55 | bb_error_msg_and_die("unknown uid %d", uid); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 56 | |
| 57 | if (argc > 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | bb_show_usage(); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 61 | o_lock_all = getopt32(argc, argv, "a"); |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 62 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 63 | vfd = xopen(CURRENT_TTY, O_RDWR); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 64 | |
| 65 | if (ioctl(vfd, VT_GETMODE, &vtm) < 0) { |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 66 | bb_perror_msg_and_die("VT_GETMODE"); |
| 67 | } |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 68 | |
| 69 | /* mask a bunch of signals */ |
| 70 | sigprocmask(SIG_SETMASK, NULL, &sig); |
| 71 | sigdelset(&sig, SIGUSR1); |
| 72 | sigdelset(&sig, SIGUSR2); |
| 73 | sigaddset(&sig, SIGTSTP); |
| 74 | sigaddset(&sig, SIGTTIN); |
| 75 | sigaddset(&sig, SIGTTOU); |
| 76 | sigaddset(&sig, SIGHUP); |
| 77 | sigaddset(&sig, SIGCHLD); |
| 78 | sigaddset(&sig, SIGQUIT); |
| 79 | sigaddset(&sig, SIGINT); |
| 80 | |
| 81 | sigemptyset(&(sa.sa_mask)); |
| 82 | sa.sa_flags = SA_RESTART; |
| 83 | sa.sa_handler = release_vt; |
| 84 | sigaction(SIGUSR1, &sa, NULL); |
| 85 | sa.sa_handler = acquire_vt; |
| 86 | sigaction(SIGUSR2, &sa, NULL); |
| 87 | |
| 88 | /* need to handle some signals so that we don't get killed by them */ |
| 89 | sa.sa_handler = SIG_IGN; |
| 90 | sigaction(SIGHUP, &sa, NULL); |
| 91 | sigaction(SIGQUIT, &sa, NULL); |
| 92 | sigaction(SIGINT, &sa, NULL); |
| 93 | sigaction(SIGTSTP, &sa, NULL); |
| 94 | |
| 95 | ovtm = vtm; |
| 96 | vtm.mode = VT_PROCESS; |
| 97 | vtm.relsig = SIGUSR1; |
| 98 | vtm.acqsig = SIGUSR2; |
| 99 | ioctl(vfd, VT_SETMODE, &vtm); |
| 100 | |
| 101 | tcgetattr(STDIN_FILENO, &oterm); |
| 102 | term = oterm; |
| 103 | term.c_iflag &= ~BRKINT; |
| 104 | term.c_iflag |= IGNBRK; |
| 105 | term.c_lflag &= ~ISIG; |
| 106 | term.c_lflag &= ~(ECHO | ECHOCTL); |
| 107 | tcsetattr(STDIN_FILENO, TCSANOW, &term); |
| 108 | |
| 109 | do { |
Denis Vlasenko | d986732 | 2006-09-24 02:10:03 +0000 | [diff] [blame] | 110 | printf("Virtual Console%s locked by %s.\n", (o_lock_all) ? "s" : "", pw->pw_name); |
Denis Vlasenko | a36a676 | 2006-09-23 13:11:49 +0000 | [diff] [blame] | 111 | if (correct_password(pw)) { |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 112 | break; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 113 | } |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 114 | bb_do_delay(FAIL_DELAY); |
Denis Vlasenko | a36a676 | 2006-09-23 13:11:49 +0000 | [diff] [blame] | 115 | puts("Password incorrect"); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 116 | } while (1); |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 117 | restore_terminal(); |
Bernhard Reutner-Fischer | b8bb27c | 2006-11-30 14:53:51 +0000 | [diff] [blame] | 118 | fflush_stdout_and_exit(0); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 119 | } |