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. */
22 static struct passwd *pw;
23 static struct vt_mode ovtm;
24 static struct termios oterm;
26 static unsigned long o_lock_all;
28 static void release_vt(int signo)
31 ioctl(vfd, VT_RELDISP, 1);
33 ioctl(vfd, VT_RELDISP, 0);
36 static void acquire_vt(int signo)
38 ioctl(vfd, VT_RELDISP, VT_ACKACQ);
41 static void restore_terminal(void)
43 ioctl(vfd, VT_SETMODE, &ovtm);
44 tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
47 int vlock_main(int argc, char **argv)
58 o_lock_all = getopt32(argc, argv, "a");
60 if((pw = getpwuid(getuid())) == NULL) {
61 bb_error_msg_and_die("unknown uid %d", getuid());
64 vfd = xopen(CURRENT_TTY, O_RDWR);
66 if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
67 bb_perror_msg_and_die("VT_GETMODE");
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);
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);
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);
97 vtm.mode = VT_PROCESS;
100 ioctl(vfd, VT_SETMODE, &vtm);
102 tcgetattr(STDIN_FILENO, &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);
111 printf("Virtual Console%s locked by %s.\n", (o_lock_all) ? "s" : "", pw->pw_name);
112 if (correct_password(pw)) {
115 bb_do_delay(FAIL_DELAY);
116 puts("Password incorrect");