1 /* vi: set sw=4 ts=4: */
4 * vlock implementation for busybox
6 * Copyright (C) 2000 by spoon <spoon@ix.netcom.com>
7 * Written by spoon <spon@ix.netcom.com>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
16 /* Fixed by Erik Andersen to do passwords the tinylogin way...
17 * It now works with md5, sha1, etc passwords. */
27 #include <sys/ioctl.h>
32 static struct passwd *pw;
33 static struct vt_mode ovtm;
34 static struct termios oterm;
36 static unsigned long o_lock_all;
38 static void release_vt(int signo)
41 ioctl(vfd, VT_RELDISP, 1);
43 ioctl(vfd, VT_RELDISP, 0);
46 static void acquire_vt(int signo)
48 ioctl(vfd, VT_RELDISP, VT_ACKACQ);
51 static void restore_terminal(void)
53 ioctl(vfd, VT_SETMODE, &ovtm);
54 tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
57 int vlock_main(int argc, char **argv)
68 o_lock_all = bb_getopt_ulflags (argc, argv, "a");
70 if((pw = getpwuid(getuid())) == NULL) {
71 bb_error_msg_and_die("Unknown uid %d", getuid());
74 vfd = bb_xopen(CURRENT_TTY, O_RDWR);
76 if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
77 bb_perror_msg_and_die("VT_GETMODE");
80 /* mask a bunch of signals */
81 sigprocmask(SIG_SETMASK, NULL, &sig);
82 sigdelset(&sig, SIGUSR1);
83 sigdelset(&sig, SIGUSR2);
84 sigaddset(&sig, SIGTSTP);
85 sigaddset(&sig, SIGTTIN);
86 sigaddset(&sig, SIGTTOU);
87 sigaddset(&sig, SIGHUP);
88 sigaddset(&sig, SIGCHLD);
89 sigaddset(&sig, SIGQUIT);
90 sigaddset(&sig, SIGINT);
92 sigemptyset(&(sa.sa_mask));
93 sa.sa_flags = SA_RESTART;
94 sa.sa_handler = release_vt;
95 sigaction(SIGUSR1, &sa, NULL);
96 sa.sa_handler = acquire_vt;
97 sigaction(SIGUSR2, &sa, NULL);
99 /* need to handle some signals so that we don't get killed by them */
100 sa.sa_handler = SIG_IGN;
101 sigaction(SIGHUP, &sa, NULL);
102 sigaction(SIGQUIT, &sa, NULL);
103 sigaction(SIGINT, &sa, NULL);
104 sigaction(SIGTSTP, &sa, NULL);
107 vtm.mode = VT_PROCESS;
108 vtm.relsig = SIGUSR1;
109 vtm.acqsig = SIGUSR2;
110 ioctl(vfd, VT_SETMODE, &vtm);
112 tcgetattr(STDIN_FILENO, &oterm);
114 term.c_iflag &= ~BRKINT;
115 term.c_iflag |= IGNBRK;
116 term.c_lflag &= ~ISIG;
117 term.c_lflag &= ~(ECHO | ECHOCTL);
118 tcsetattr(STDIN_FILENO, TCSANOW, &term);
121 printf("Virtual Console%s locked.\n%s's ", (o_lock_all) ? "s" : "", pw->pw_name);
123 if (correct_password (pw)) {
126 bb_do_delay(FAIL_DELAY);
127 puts("Password incorrect.");