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 | |
| 18 | #include <stdio.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 20 | #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 | |
| 31 | static struct passwd *pw; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 32 | static struct vt_mode ovtm; |
| 33 | static struct termios oterm; |
| 34 | static int vfd; |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 35 | static unsigned long o_lock_all; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 36 | |
| 37 | static 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 | |
| 45 | static void acquire_vt(int signo) |
| 46 | { |
| 47 | ioctl(vfd, VT_RELDISP, VT_ACKACQ); |
| 48 | } |
| 49 | |
| 50 | static void restore_terminal(void) |
| 51 | { |
| 52 | ioctl(vfd, VT_SETMODE, &ovtm); |
| 53 | tcsetattr(STDIN_FILENO, TCSANOW, &oterm); |
| 54 | } |
| 55 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 56 | int vlock_main(int argc, char **argv) |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 57 | { |
| 58 | sigset_t sig; |
| 59 | struct sigaction sa; |
| 60 | struct vt_mode vtm; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 61 | struct termios term; |
| 62 | |
| 63 | if (argc > 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | bb_show_usage(); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 67 | 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 Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 73 | vfd = bb_xopen(CURRENT_TTY, O_RDWR); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 74 | |
| 75 | if (ioctl(vfd, VT_GETMODE, &vtm) < 0) { |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 76 | bb_perror_msg_and_die("VT_GETMODE"); |
| 77 | } |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 78 | |
| 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 Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 120 | printf("Virtual Console%s locked.\n%s's ", (o_lock_all) ? "s" : "", pw->pw_name); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 121 | fflush(stdout); |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 122 | if (correct_password (pw)) { |
| 123 | break; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 124 | } |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 125 | bb_do_delay(FAIL_DELAY); |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 126 | puts("Password incorrect."); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 127 | } while (1); |
Rob Landley | 68eb9dd | 2005-12-19 02:50:10 +0000 | [diff] [blame] | 128 | restore_terminal(); |
| 129 | return 0; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 130 | } |