* Copyright (c) 1980, 1987 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of California at Berkeley. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
static char sccsid
[] = "@(#)lock.c 5.5 (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
long nexttime
; /* keep the timeout time */
struct passwd
*root_pwd
, *my_pwd
;
struct itimerval ntimer
, otimer
;
int sectimeout
= TIMEOUT
,
hostname
[MAXHOSTNAMELEN
], s
[BUFSIZ
], s1
[BUFSIZ
],
*crypt(), *index(), *ttyname();
for (++argv
; *argv
; ++argv
) {
else if (!isdigit(argv
[0][1])) {
fprintf(stderr
, "lock: illegal option -- %c\n", argv
[0][1]);
else if ((sectimeout
= atoi(*argv
+ 1)) <= 0)
timeout
.tv_sec
= sectimeout
* 60;
/* get information for header */
if (ioctl(0, TIOCGETP
, &tty
))
gethostname(hostname
, sizeof(hostname
));
if (!(ttynam
= ttyname(0))) {
puts("lock: not a terminal?");
if (gettimeofday(&timval
, (struct timezone
*)NULL
)) {
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 */
fputs("\nAgain: ", stdout
);
* Don't need EOF test here, if we get EOF, then s1 != s
* and the right things will happen.
(void)gets(s1
, sizeof(s1
));
puts("\07lock: passwords didn't match.");
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
);
printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
ttynam
, hostname
, sectimeout
, ap
, tzn
, ap
+ 19);
my_pwd
= getpwuid(getuid());
if (!gets(s
, sizeof(s
))) {
if (!my_pwd
|| !*my_pwd
->pw_passwd
|| !strcmp(my_pwd
->pw_passwd
, crypt(s
, my_pwd
->pw_passwd
)))
if (!root_pwd
|| !*root_pwd
->pw_passwd
|| !strcmp(root_pwd
->pw_passwd
, crypt(s
, root_pwd
->pw_passwd
)))
if (ioctl(0, TIOCGETP
, &ntty
))
if (!gettimeofday(&timval
, (struct timezone
*)NULL
))
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
);
fputs("usage: lock [-p] [-timeout]\n", stderr
);