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