blob: 02d1ea772139f1847a19171a0ba366aceec4c6df [file] [log] [blame]
Eric Andersen27f64e12002-06-23 04:24:25 +00001/* vi: set sw=4 ts=4: */
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00002
Eric Andersen27f64e12002-06-23 04:24:25 +00003/*
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-Fischerb1629b12006-05-19 19:29:19 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen27f64e12002-06-23 04:24:25 +000010 */
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 Andersen27f64e12002-06-23 04:24:25 +000019#include "busybox.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000020#include <sys/vt.h>
Eric Andersen27f64e12002-06-23 04:24:25 +000021
22static struct passwd *pw;
Eric Andersen27f64e12002-06-23 04:24:25 +000023static struct vt_mode ovtm;
24static struct termios oterm;
25static int vfd;
Rob Landley68eb9dd2005-12-19 02:50:10 +000026static unsigned long o_lock_all;
Eric Andersen27f64e12002-06-23 04:24:25 +000027
28static void release_vt(int signo)
29{
30 if (!o_lock_all)
31 ioctl(vfd, VT_RELDISP, 1);
32 else
33 ioctl(vfd, VT_RELDISP, 0);
34}
35
36static void acquire_vt(int signo)
37{
38 ioctl(vfd, VT_RELDISP, VT_ACKACQ);
39}
40
41static void restore_terminal(void)
42{
43 ioctl(vfd, VT_SETMODE, &ovtm);
44 tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
45}
46
Rob Landleydfba7412006-03-06 20:47:33 +000047int vlock_main(int argc, char **argv)
Eric Andersen27f64e12002-06-23 04:24:25 +000048{
49 sigset_t sig;
50 struct sigaction sa;
51 struct vt_mode vtm;
Eric Andersen27f64e12002-06-23 04:24:25 +000052 struct termios term;
53
54 if (argc > 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 bb_show_usage();
Eric Andersen27f64e12002-06-23 04:24:25 +000056 }
57
Denis Vlasenko67b23e62006-10-03 21:00:06 +000058 o_lock_all = getopt32 (argc, argv, "a");
Rob Landley68eb9dd2005-12-19 02:50:10 +000059
60 if((pw = getpwuid(getuid())) == NULL) {
61 bb_error_msg_and_die("Unknown uid %d", getuid());
Eric Andersen27f64e12002-06-23 04:24:25 +000062 }
63
Rob Landleyd921b2e2006-08-03 15:41:12 +000064 vfd = xopen(CURRENT_TTY, O_RDWR);
Eric Andersen27f64e12002-06-23 04:24:25 +000065
66 if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
Rob Landley68eb9dd2005-12-19 02:50:10 +000067 bb_perror_msg_and_die("VT_GETMODE");
68 }
Eric Andersen27f64e12002-06-23 04:24:25 +000069
70 /* mask a bunch of signals */
71 sigprocmask(SIG_SETMASK, NULL, &sig);
72 sigdelset(&sig, SIGUSR1);
73 sigdelset(&sig, SIGUSR2);
74 sigaddset(&sig, SIGTSTP);
75 sigaddset(&sig, SIGTTIN);
76 sigaddset(&sig, SIGTTOU);
77 sigaddset(&sig, SIGHUP);
78 sigaddset(&sig, SIGCHLD);
79 sigaddset(&sig, SIGQUIT);
80 sigaddset(&sig, SIGINT);
81
82 sigemptyset(&(sa.sa_mask));
83 sa.sa_flags = SA_RESTART;
84 sa.sa_handler = release_vt;
85 sigaction(SIGUSR1, &sa, NULL);
86 sa.sa_handler = acquire_vt;
87 sigaction(SIGUSR2, &sa, NULL);
88
89 /* need to handle some signals so that we don't get killed by them */
90 sa.sa_handler = SIG_IGN;
91 sigaction(SIGHUP, &sa, NULL);
92 sigaction(SIGQUIT, &sa, NULL);
93 sigaction(SIGINT, &sa, NULL);
94 sigaction(SIGTSTP, &sa, NULL);
95
96 ovtm = vtm;
97 vtm.mode = VT_PROCESS;
98 vtm.relsig = SIGUSR1;
99 vtm.acqsig = SIGUSR2;
100 ioctl(vfd, VT_SETMODE, &vtm);
101
102 tcgetattr(STDIN_FILENO, &oterm);
103 term = oterm;
104 term.c_iflag &= ~BRKINT;
105 term.c_iflag |= IGNBRK;
106 term.c_lflag &= ~ISIG;
107 term.c_lflag &= ~(ECHO | ECHOCTL);
108 tcsetattr(STDIN_FILENO, TCSANOW, &term);
109
110 do {
Denis Vlasenkod9867322006-09-24 02:10:03 +0000111 printf("Virtual Console%s locked by %s.\n", (o_lock_all) ? "s" : "", pw->pw_name);
Denis Vlasenkoa36a6762006-09-23 13:11:49 +0000112 if (correct_password(pw)) {
Rob Landley68eb9dd2005-12-19 02:50:10 +0000113 break;
Eric Andersen27f64e12002-06-23 04:24:25 +0000114 }
Rob Landley84cb7672006-01-06 20:59:09 +0000115 bb_do_delay(FAIL_DELAY);
Denis Vlasenkoa36a6762006-09-23 13:11:49 +0000116 puts("Password incorrect");
Eric Andersen27f64e12002-06-23 04:24:25 +0000117 } while (1);
Rob Landley68eb9dd2005-12-19 02:50:10 +0000118 restore_terminal();
119 return 0;
Eric Andersen27f64e12002-06-23 04:24:25 +0000120}