* Copyright (c) 1980, 1987, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
static char copyright
[] =
"@(#) Copyright (c) 1980, 1987, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)lock.c 8.1 (Berkeley) %G%";
* Lock a terminal up until the given key is entered, until the root
* password is entered, or the given interval times out.
* Timeout interval is by default TIMEOUT, it can be changed with
* an argument of the form -time where time is in minutes
void quit(), bye(), hi();
long nexttime
; /* keep the timeout time */
extern int errno
, optind
;
struct itimerval ntimer
, otimer
;
int ch
, sectimeout
, usemine
;
char *ap
, *mypw
, *ttynam
, *tzn
;
char hostname
[MAXHOSTNAMELEN
], s
[BUFSIZ
], s1
[BUFSIZ
];
char *crypt(), *ttyname();
while ((ch
= getopt(argc
, argv
, "pt:")) != EOF
)
if ((sectimeout
= atoi(optarg
)) <= 0) {
"lock: illegal timeout value.\n");
if (!(pw
= getpwuid(getuid()))) {
"lock: unknown uid %d.\n", getuid());
mypw
= strdup(pw
->pw_passwd
);
"usage: lock [-p] [-t timeout]\n");
timeout
.tv_sec
= sectimeout
* 60;
setuid(getuid()); /* discard privs */
if (ioctl(0, TIOCGETP
, &tty
)) /* get information for header */
gethostname(hostname
, sizeof(hostname
));
if (!(ttynam
= ttyname(0))) {
(void)printf("lock: not a terminal?\n");
if (gettimeofday(&timval
, (struct timezone
*)NULL
)) {
"lock: gettimeofday: %s\n", strerror(errno
));
nexttime
= timval
.tv_sec
+ (sectimeout
* 60);
timp
= localtime(&timval
.tv_sec
);
(void)signal(SIGINT
, quit
);
(void)signal(SIGQUIT
, quit
);
ntty
= tty
; ntty
.sg_flags
&= ~ECHO
;
(void)ioctl(0, TIOCSETP
, &ntty
);
/* get key and check again */
if (!fgets(s
, sizeof(s
), stdin
) || *s
== '\n')
(void)printf("\nAgain: ");
* Don't need EOF test here, if we get EOF, then s1 != s
* and the right things will happen.
(void)fgets(s1
, sizeof(s1
), stdin
);
(void)printf("\07lock: passwords didn't match.\n");
ioctl(0, TIOCSETP
, &tty
);
/* set signal handlers */
(void)signal(SIGINT
, hi
);
(void)signal(SIGQUIT
, hi
);
(void)signal(SIGTSTP
, hi
);
(void)signal(SIGALRM
, bye
);
ntimer
.it_interval
= zerotime
;
ntimer
.it_value
= timeout
;
setitimer(ITIMER_REAL
, &ntimer
, &otimer
);
(void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
ttynam
, hostname
, sectimeout
, ap
, tzn
, ap
+ 19);
if (!fgets(s
, sizeof(s
), stdin
)) {
if (!strcmp(mypw
, crypt(s
, mypw
)))
if (ioctl(0, TIOCGETP
, &ntty
))
if (!gettimeofday(&timval
, (struct timezone
*)NULL
))
(void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
(nexttime
- timval
.tv_sec
) / 60, (nexttime
- timval
.tv_sec
) % 60);
(void)ioctl(0, TIOCSETP
, &tty
);
(void)ioctl(0, TIOCSETP
, &tty
);
(void)printf("lock: timeout\n");