1 /* vi: set sw=4 ts=4: */
3 * vlock implementation for busybox
5 * Copyright (C) 2000 by spoon <spoon@ix.netcom.com>
6 * Written by spoon <spon@ix.netcom.com>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
15 /* Fixed by Erik Andersen to do passwords the tinylogin way...
16 * It now works with md5, sha1, etc passwords. */
26 #include <sys/ioctl.h>
31 static struct passwd *pw;
32 static struct vt_mode ovtm;
33 static struct termios oterm;
35 static unsigned long o_lock_all;
37 static void release_vt(int signo)
40 ioctl(vfd, VT_RELDISP, 1);
42 ioctl(vfd, VT_RELDISP, 0);
45 static void acquire_vt(int signo)
47 ioctl(vfd, VT_RELDISP, VT_ACKACQ);
50 static void restore_terminal(void)
52 ioctl(vfd, VT_SETMODE, &ovtm);
53 tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
56 int vlock_main(int argc, char **argv)
67 o_lock_all = bb_getopt_ulflags (argc, argv, "a");
69 if((pw = getpwuid(getuid())) == NULL) {
70 bb_error_msg_and_die("Unknown uid %d", getuid());
73 vfd = bb_xopen(CURRENT_TTY, O_RDWR);
75 if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
76 bb_perror_msg_and_die("VT_GETMODE");
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);
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);
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);
106 vtm.mode = VT_PROCESS;
107 vtm.relsig = SIGUSR1;
108 vtm.acqsig = SIGUSR2;
109 ioctl(vfd, VT_SETMODE, &vtm);
111 tcgetattr(STDIN_FILENO, &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);
120 printf("Virtual Console%s locked.\n%s's ", (o_lock_all) ? "s" : "", pw->pw_name);
122 if (correct_password (pw)) {
125 bb_do_delay(FAIL_DELAY);
126 puts("Password incorrect.");