manual page distributed with 4.1BSD
[unix-history] / usr / src / usr.bin / lock / lock.c
CommitLineData
4ca10280 1#ifndef lint
3e58f554 2static char *sccsid = "@(#)lock.c 4.5 (Berkeley) %G%";
4ca10280
SL
3#endif
4
5/*
d523b17b
RC
6 * Lock a terminal up until the given key is entered,
7 * or until the root password is entered,
8 * or the given interval times out.
9 *
10 * Timeout interval is by default TIMEOUT, it can be changed with
11 * an argument of the form -time where time is in minutes
4ca10280 12 */
d523b17b
RC
13
14#include <pwd.h>
d97d0c53
BJ
15#include <stdio.h>
16#include <sys/types.h>
36e4dcd1 17#include <sys/stat.h>
d523b17b 18#include <sys/time.h>
d97d0c53
BJ
19#include <signal.h>
20#include <sgtty.h>
21
d523b17b
RC
22#define TIMEOUT 15
23
24struct passwd *pwd;
25char *crypt();
26char *getpass();
27char *index();
28char *ttyname();
29char *timezone();
30char *asctime();
31struct tm *localtime();
32
33int quit();
34int bye();
35int hi();
36
37struct timeval timeout = {0, 0};
38struct timeval zerotime = {0, 0};
39struct sgttyb tty, ntty;
40long nexttime; /* keep the timeout time */
d97d0c53
BJ
41
42main(argc, argv)
d523b17b 43 int argc;
d97d0c53
BJ
44 char **argv;
45{
46 register int t;
d523b17b
RC
47 char *ttynam;
48 char *ap;
49 int sectimeout = TIMEOUT;
50 char s[BUFSIZ], s1[BUFSIZ];
51 char hostname[32];
52 char *tzn;
53 struct timeval timval;
54 struct itimerval ntimer, otimer;
55 struct timezone timzone;
56 struct tm *timp;
57 struct stat statb;
58
59 /* process arguments */
60
61 if (argc > 1){
62 if (argv[1][0] != '-')
63 goto usage;
64 if (sscanf(&(argv[1][1]), "%d", &sectimeout) != 1)
65 goto usage;
66 }
67 timeout.tv_sec = sectimeout * 60;
68
69 /* get information for header */
d97d0c53 70
4ca10280 71 if (ioctl(0, TIOCGETP, &tty))
d97d0c53 72 exit(1);
d523b17b
RC
73 pwd = getpwuid(0);
74 gethostname(hostname, sizeof(hostname));
75 if (!(ttynam = ttyname(0))){
76 printf("lock: not a terminal?");
77 exit (1);
78 }
79 gettimeofday(&timval, &timzone);
80 nexttime = timval.tv_sec + (sectimeout * 60);
81 timp = localtime(&timval.tv_sec);
82 ap = asctime(timp);
83 tzn = timezone(timzone.tz_minuteswest, timp->tm_isdst);
84
85 /* get key and check again */
86
87 signal(SIGINT, quit);
88 signal(SIGQUIT, quit);
d97d0c53 89 ntty = tty; ntty.sg_flags &= ~ECHO;
4ca10280 90 ioctl(0, TIOCSETN, &ntty);
d97d0c53 91 printf("Key: ");
3e58f554
RE
92 if (fgets(s, sizeof s, stdin) == NULL) {
93 putchar('\n');
94 quit();
95 }
d97d0c53 96 printf("\nAgain: ");
3e58f554
RE
97 /*
98 * Don't need EOF test here, if we get EOF, then s1 != s
99 * and the right things will happen.
100 */
101 (void) fgets(s1, sizeof s1, stdin);
d97d0c53
BJ
102 putchar('\n');
103 if (strcmp(s1, s)) {
104 putchar(07);
105 stty(0, &tty);
106 exit(1);
107 }
108 s[0] = 0;
d523b17b
RC
109
110 /* Set signal handlers */
111
112 signal(SIGINT, hi);
113 signal(SIGQUIT, hi);
114 signal(SIGTSTP, hi);
115 signal(SIGALRM, bye);
116 ntimer.it_interval = zerotime;
117 ntimer.it_value = timeout;
118 setitimer(ITIMER_REAL, &ntimer, &otimer);
119
120 /* Header info */
121
122 printf ("lock: %s on %s. timeout in %d minutes\n",
123 ttynam, hostname, sectimeout);
124 printf("time now is %.20s", ap);
125 if (tzn)
126 printf("%s", tzn);
127 printf("%s", ap+19);
128
129 /* wait */
130
d97d0c53 131 for (;;) {
d523b17b 132 printf("Key: ");
3e58f554
RE
133 if (fgets(s, sizeof s, stdin) == NULL) {
134 clearerr(stdin);
135 hi();
136 continue;
137 }
d97d0c53
BJ
138 if (strcmp(s1, s) == 0)
139 break;
d523b17b 140 if (pwd == (struct passwd *) 0 || pwd->pw_passwd[0] == '\0')
d97d0c53 141 break;
d523b17b
RC
142 ap = index(s, '\n');
143 if (ap != NULL)
144 *ap = '\0';
145 if (strcmp(pwd->pw_passwd, crypt(s, pwd->pw_passwd)) == 0)
146 break;
147 printf("\07\n");
4ca10280 148 if (ioctl(0, TIOCGETP, &ntty))
d97d0c53
BJ
149 exit(1);
150 }
4ca10280 151 ioctl(0, TIOCSETN, &tty);
d523b17b
RC
152 putchar('\n');
153 exit (0);
154usage:
155 printf("Usage: lock [-timeout]\n");
156 exit (1);
157}
158
159/*
160 * get out of here
161 */
162
163quit()
164{
165 ioctl(0, TIOCSETN, &tty);
166 exit (0);
167}
168
169bye()
170{
171 ioctl(0, TIOCSETN, &tty);
172 printf("lock: timeout\n");
173 exit (1);
174}
175
176/*
177 * tell the user we are waiting
178 */
179
180hi()
181{
182 long curtime;
183 struct timeval timval;
184 struct timezone timzone;
185
186 gettimeofday(&timval, &timzone);
187 curtime = timval.tv_sec;
188 printf("lock: type in the unlock key. timeout in %d minutes\n",
189 (nexttime-curtime)/60);
d97d0c53 190}