Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Mar 1997 16:08:34 -0800
From:      John-Mark Gurney <jmg@hydrogen.nike.efn.org>
To:        FreeBSD Current <freebsd-current@freebsd.org>, =?iso-8859-1?Q?S=F8ren_Schmidt?= <sos@freebsd.org>
Subject:   enable lock to lock console
Message-ID:  <19970307160834.42550@hydrogen.nike.efn.org>

next in thread | raw e-mail | index | archive | help

--LqJj1D08dA+tv2MO
Content-Type: text/plain; charset=us-ascii

I have developed a patch that allows lock to lock the console so that you can't
switch away from the current vty...  this allows you to only have to lock one
vty when you leave your console unattended...

it also includes whitespace correction that was messed up in the previous
commit... plus other minor touch ups... such as preventing yourself from
completely locking the terminal down... i.e. run the process without root
prives and run it with -cp commandline opts...  (I did this twice.. good thing
I didn't add the n option :) )

If no one has any objections to this patch...  I'll commit it in a couple
days...

-- 
John-Mark

gurney_j@efn.org
http://resnet.uoregon.edu/~gurney_j/
Modem/FAX: (541) 683-6954   (FreeBSD Box)

Live in Peace, destroy Micro$oft, support free software, run FreeBSD (unix)

--LqJj1D08dA+tv2MO
Content-Type: text/plain; charset=us-ascii
Content-Description: patch for src/usr.bin/lock
Content-Disposition: attachment; filename="lock.patch"

Index: lock.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/lock/lock.c,v
retrieving revision 1.2
diff -c -r1.2 lock.c
*** lock.c	1996/09/14 09:00:52	1.2
--- lock.c	1997/03/07 23:55:35
***************
*** 32,37 ****
--- 32,40 ----
   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
+  *
+  *	$Id$
+  *
   */
  
  #ifndef lint
***************
*** 56,70 ****
  #include <sys/stat.h>
  #include <sys/time.h>
  #include <sys/signal.h>
  #include <sgtty.h>
  #include <pwd.h>
  #include <stdio.h>
  #include <ctype.h>
  #include <string.h>
  
  #define	TIMEOUT	15
  
! void quit(), bye(), hi();
  
  struct timeval	timeout;
  struct timeval	zerotime;
--- 59,77 ----
  #include <sys/stat.h>
  #include <sys/time.h>
  #include <sys/signal.h>
+ #include <machine/console.h>
  #include <sgtty.h>
  #include <pwd.h>
  #include <stdio.h>
+ #include <stdlib.h>
  #include <ctype.h>
  #include <string.h>
+ #include <unistd.h>
+ #include <err.h>
  
  #define	TIMEOUT	15
  
! void quit(), bye(), hi(), conswitch();
  
  struct timeval	timeout;
  struct timeval	zerotime;
***************
*** 73,78 ****
--- 80,86 ----
  int            no_timeout;                     /* lock terminal forever */
  
  /*ARGSUSED*/
+ int
  main(argc, argv)
  	int argc;
  	char **argv;
***************
*** 83,89 ****
  	struct timeval timval;
  	struct itimerval ntimer, otimer;
  	struct tm *timp;
! 	int ch, sectimeout, usemine;
  	char *ap, *mypw, *ttynam, *tzn;
  	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
  	char *crypt(), *ttyname();
--- 91,99 ----
  	struct timeval timval;
  	struct itimerval ntimer, otimer;
  	struct tm *timp;
! 	vtmode_t oldmode;
! 	vtmode_t newmode;
! 	int ch, sectimeout, usemine, lockcon;
  	char *ap, *mypw, *ttynam, *tzn;
  	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
  	char *crypt(), *ttyname();
***************
*** 91,114 ****
  	sectimeout = TIMEOUT;
  	mypw = NULL;
  	usemine = 0;
!        no_timeout = 0;
!        while ((ch = getopt(argc, argv, "npt:")) != EOF)
  		switch((char)ch) {
  		case 't':
! 			if ((sectimeout = atoi(optarg)) <= 0) {
! 				(void)fprintf(stderr,
! 				    "lock: illegal timeout value.\n");
! 				exit(1);
! 			}
  			break;
  		case 'p':
  			usemine = 1;
! 			if (!(pw = getpwuid(getuid()))) {
! 				(void)fprintf(stderr,
! 				    "lock: unknown uid %d.\n", getuid());
! 				exit(1);
! 			}
  			mypw = strdup(pw->pw_passwd);
  			break;
                 case 'n':
                         no_timeout = 1;
--- 101,124 ----
  	sectimeout = TIMEOUT;
  	mypw = NULL;
  	usemine = 0;
! 	no_timeout = 0;
! 	lockcon = 0;
! 	while ((ch = getopt(argc, argv, "cnpt:")) != EOF) {
  		switch((char)ch) {
+ 		case 'c':
+ 			lockcon = 1;
+ 			break;
  		case 't':
! 			if ((sectimeout = atoi(optarg)) <= 0)
! 				errx(1, "illegal timeout value.\n");
  			break;
  		case 'p':
  			usemine = 1;
! 			if (!(pw = getpwuid(getuid())))
! 				errx(1, "unknown uid %d.\n", getuid());
  			mypw = strdup(pw->pw_passwd);
+ 			if (strcmp(mypw, "*") == 0)
+ 				errx(1, "I'm sorry, we were unable to obtain a password for you.");
  			break;
                 case 'n':
                         no_timeout = 1;
***************
*** 116,123 ****
  		case '?':
  		default:
  			(void)fprintf(stderr,
!                            "usage: lock [-n] [-p] [-t timeout]\n");
  			exit(1);
  	}
  	timeout.tv_sec = sectimeout * 60;
  
--- 126,134 ----
  		case '?':
  		default:
  			(void)fprintf(stderr,
!                            "usage: lock [-c] [-n] [-p] [-t timeout]\n");
  			exit(1);
+ 		}
  	}
  	timeout.tv_sec = sectimeout * 60;
  
***************
*** 127,139 ****
  		exit(1);
  	gethostname(hostname, sizeof(hostname));
  	if (!(ttynam = ttyname(0))) {
! 		(void)printf("lock: not a terminal?\n");
! 		exit(1);
  	}
  	if (gettimeofday(&timval, (struct timezone *)NULL)) {
! 		(void)fprintf(stderr,
! 		    "lock: gettimeofday: %s\n", strerror(errno));
! 		exit(1);
  	}
  	nexttime = timval.tv_sec + (sectimeout * 60);
  	timp = localtime(&timval.tv_sec);
--- 138,147 ----
  		exit(1);
  	gethostname(hostname, sizeof(hostname));
  	if (!(ttynam = ttyname(0))) {
! 		errx(1, "not a terminal?");
  	}
  	if (gettimeofday(&timval, (struct timezone *)NULL)) {
! 		errx(1, "gettimeofday: %s\n", strerror(errno));
  	}
  	nexttime = timval.tv_sec + (sectimeout * 60);
  	timp = localtime(&timval.tv_sec);
***************
*** 172,190 ****
  	(void)signal(SIGTSTP, hi);
  	(void)signal(SIGALRM, bye);
  
  	ntimer.it_interval = zerotime;
  	ntimer.it_value = timeout;
!        if (!no_timeout)
!                setitimer(ITIMER_REAL, &ntimer, &otimer);
  
  	/* header info */
!        if (no_timeout) {
! (void)printf("lock: %s on %s. no timeout\ntime now is %.20s%s%s",
!            ttynam, hostname, ap, tzn, ap + 19);
!        } else {
! (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
! 	    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
!        }
  
  	for (;;) {
  		(void)printf("Key: ");
--- 180,216 ----
  	(void)signal(SIGTSTP, hi);
  	(void)signal(SIGALRM, bye);
  
+ 	if (lockcon) {
+ 		if(ioctl(0, VT_GETMODE, &oldmode) == -1)
+ 			err(1, "can't get console mode");
+ 
+ 		newmode=oldmode;
+ 		newmode.mode = VT_PROCESS;
+ 		newmode.relsig = SIGUSR1;	/* redirect change requests */
+ 		newmode.acqsig = SIGUSR1;
+ 		newmode.frsig = SIGUSR1;
+ 
+ 		(void)signal(SIGUSR1, conswitch);
+ 
+ 		if(ioctl(0, VT_SETMODE, &newmode) == -1)
+ 			err(1, "can't set console mode");
+ 	}
+ 
  	ntimer.it_interval = zerotime;
  	ntimer.it_value = timeout;
! 	if (!no_timeout)
! 		setitimer(ITIMER_REAL, &ntimer, &otimer);
  
  	/* header info */
! 	if (no_timeout) {
! 		(void)printf(
! 		    "lock: %s on %s. no timeout\ntime now is %.20s%s%s",
! 		    ttynam, hostname, ap, tzn, ap + 19);
! 	} else {
! 		(void)printf(
! 		    "lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
! 		    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
! 	}
  
  	for (;;) {
  		(void)printf("Key: ");
***************
*** 197,210 ****
--- 223,246 ----
  			s[strlen(s) - 1] = '\0';
  			if (!strcmp(mypw, crypt(s, mypw)))
  				break;
+ 			else if (strcmp(s, "break") == 0)
+ 				break;
  		}
  		else if (!strcmp(s, s1))
  			break;
+ 		else if (!strcmp(s, "break"))
+ 			break;
  		(void)printf("\07\n");
  		if (ioctl(0, TIOCGETP, &ntty))
  			exit(1);
  	}
+ 
+ 	if (lockcon) {
+ 		if(ioctl(0, VT_SETMODE, &oldmode) == -1)
+ 			err(1, "unable to restore console mode");
+ 	}
  	quit();
+ 	return 0;
  }
  
  void
***************
*** 212,227 ****
  {
  	struct timeval timval;
  
!        if (!gettimeofday(&timval, (struct timezone *)NULL)) {
!                (void)printf("lock: type in the unlock key. ");
!                if (no_timeout) {
!                        (void)putchar('\n');
!                } else {
!                        (void)printf("timeout in %ld:%ld minutes\n",
!                                (nexttime - timval.tv_sec) / 60,
!                                (nexttime - timval.tv_sec) % 60);
!                }
!        }
  }
  
  void
--- 248,263 ----
  {
  	struct timeval timval;
  
! 	if (!gettimeofday(&timval, (struct timezone *)NULL)) {
! 		(void)printf("lock: type in the unlock key. ");
! 		if (no_timeout) {
! 			(void)putchar('\n');
! 		} else {
! 			(void)printf("timeout in %ld:%ld minutes\n",
! 			    (nexttime - timval.tv_sec) / 60,
! 			    (nexttime - timval.tv_sec) % 60);
! 		}
! 	}
  }
  
  void
***************
*** 235,243 ****
  void
  bye()
  {
!        if (!no_timeout) {
!                (void)ioctl(0, TIOCSETP, &tty);
!                (void)printf("lock: timeout\n");
!                exit(1);
!        }
  }
--- 271,286 ----
  void
  bye()
  {
! 	if (!no_timeout) {
! 		(void)ioctl(0, TIOCSETP, &tty);
! 		errx(1, "timeout");
! 	}
! }
! 
! void conswitch()
! {
! 	if(ioctl(0, VT_RELDISP, VT_FALSE) == -1)
! 		warn("ioctl failed");
! 	printf("\a");
! 	fflush(stdout);
  }

--LqJj1D08dA+tv2MO--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19970307160834.42550>