Commit | Line | Data |
---|---|---|
4ca10280 | 1 | #ifndef lint |
d523b17b | 2 | static char *sccsid = "@(#)lock.c 4.4 (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 | ||
24 | struct passwd *pwd; | |
25 | char *crypt(); | |
26 | char *getpass(); | |
27 | char *index(); | |
28 | char *ttyname(); | |
29 | char *timezone(); | |
30 | char *asctime(); | |
31 | struct tm *localtime(); | |
32 | ||
33 | int quit(); | |
34 | int bye(); | |
35 | int hi(); | |
36 | ||
37 | struct timeval timeout = {0, 0}; | |
38 | struct timeval zerotime = {0, 0}; | |
39 | struct sgttyb tty, ntty; | |
40 | long nexttime; /* keep the timeout time */ | |
d97d0c53 BJ |
41 | |
42 | main(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", §imeout) != 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 BJ |
91 | printf("Key: "); |
92 | fgets(s, sizeof s, stdin); | |
93 | printf("\nAgain: "); | |
94 | fgets(s1, sizeof s1, stdin); | |
95 | putchar('\n'); | |
96 | if (strcmp(s1, s)) { | |
97 | putchar(07); | |
98 | stty(0, &tty); | |
99 | exit(1); | |
100 | } | |
101 | s[0] = 0; | |
d523b17b RC |
102 | |
103 | /* Set signal handlers */ | |
104 | ||
105 | signal(SIGINT, hi); | |
106 | signal(SIGQUIT, hi); | |
107 | signal(SIGTSTP, hi); | |
108 | signal(SIGALRM, bye); | |
109 | ntimer.it_interval = zerotime; | |
110 | ntimer.it_value = timeout; | |
111 | setitimer(ITIMER_REAL, &ntimer, &otimer); | |
112 | ||
113 | /* Header info */ | |
114 | ||
115 | printf ("lock: %s on %s. timeout in %d minutes\n", | |
116 | ttynam, hostname, sectimeout); | |
117 | printf("time now is %.20s", ap); | |
118 | if (tzn) | |
119 | printf("%s", tzn); | |
120 | printf("%s", ap+19); | |
121 | ||
122 | /* wait */ | |
123 | ||
d97d0c53 | 124 | for (;;) { |
d523b17b | 125 | printf("Key: "); |
d97d0c53 BJ |
126 | fgets(s, sizeof s, stdin); |
127 | if (strcmp(s1, s) == 0) | |
128 | break; | |
d523b17b | 129 | if (pwd == (struct passwd *) 0 || pwd->pw_passwd[0] == '\0') |
d97d0c53 | 130 | break; |
d523b17b RC |
131 | ap = index(s, '\n'); |
132 | if (ap != NULL) | |
133 | *ap = '\0'; | |
134 | if (strcmp(pwd->pw_passwd, crypt(s, pwd->pw_passwd)) == 0) | |
135 | break; | |
136 | printf("\07\n"); | |
4ca10280 | 137 | if (ioctl(0, TIOCGETP, &ntty)) |
d97d0c53 BJ |
138 | exit(1); |
139 | } | |
4ca10280 | 140 | ioctl(0, TIOCSETN, &tty); |
d523b17b RC |
141 | putchar('\n'); |
142 | exit (0); | |
143 | usage: | |
144 | printf("Usage: lock [-timeout]\n"); | |
145 | exit (1); | |
146 | } | |
147 | ||
148 | /* | |
149 | * get out of here | |
150 | */ | |
151 | ||
152 | quit() | |
153 | { | |
154 | ioctl(0, TIOCSETN, &tty); | |
155 | exit (0); | |
156 | } | |
157 | ||
158 | bye() | |
159 | { | |
160 | ioctl(0, TIOCSETN, &tty); | |
161 | printf("lock: timeout\n"); | |
162 | exit (1); | |
163 | } | |
164 | ||
165 | /* | |
166 | * tell the user we are waiting | |
167 | */ | |
168 | ||
169 | hi() | |
170 | { | |
171 | long curtime; | |
172 | struct timeval timval; | |
173 | struct timezone timzone; | |
174 | ||
175 | gettimeofday(&timval, &timzone); | |
176 | curtime = timval.tv_sec; | |
177 | printf("lock: type in the unlock key. timeout in %d minutes\n", | |
178 | (nexttime-curtime)/60); | |
d97d0c53 | 179 | } |