BSD 4_4 release
[unix-history] / usr / src / usr.bin / lock / lock.c
CommitLineData
22e155fc 1/*
ad787160
C
2 * Copyright (c) 1980, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
750362f3 4 *
050a4e8e
KB
5 * This code is derived from software contributed to Berkeley by
6 * Bob Toxen.
7 *
ad787160
C
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
22e155fc
DF
35 */
36
37#ifndef lint
ad787160
C
38static char copyright[] =
39"@(#) Copyright (c) 1980, 1987, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
750362f3 41#endif /* not lint */
22e155fc 42
4ca10280 43#ifndef lint
ad787160 44static char sccsid[] = "@(#)lock.c 8.1 (Berkeley) 6/6/93";
750362f3 45#endif /* not lint */
4ca10280
SL
46
47/*
f84e1e5b
KB
48 * Lock a terminal up until the given key is entered, until the root
49 * password is entered, or the given interval times out.
d523b17b
RC
50 *
51 * Timeout interval is by default TIMEOUT, it can be changed with
52 * an argument of the form -time where time is in minutes
4ca10280 53 */
d523b17b 54
654949dc 55#include <sys/param.h>
36e4dcd1 56#include <sys/stat.h>
d523b17b 57#include <sys/time.h>
654949dc 58#include <sys/signal.h>
d97d0c53 59#include <sgtty.h>
92d691ba 60#include <pwd.h>
654949dc 61#include <stdio.h>
2cbfd2f5 62#include <ctype.h>
38dde0cd 63#include <string.h>
d97d0c53 64
f84e1e5b 65#define TIMEOUT 15
d523b17b 66
eb8ba43e 67void quit(), bye(), hi();
d523b17b 68
654949dc
KB
69struct timeval timeout;
70struct timeval zerotime;
d523b17b 71struct sgttyb tty, ntty;
f84e1e5b 72long nexttime; /* keep the timeout time */
d97d0c53 73
f84e1e5b
KB
74/*ARGSUSED*/
75main(argc, argv)
fcc2be53
KB
76 int argc;
77 char **argv;
d97d0c53 78{
fcc2be53 79 extern char *optarg;
92d691ba
KB
80 extern int errno, optind;
81 struct passwd *pw;
fcc2be53
KB
82 struct timeval timval;
83 struct itimerval ntimer, otimer;
84 struct tm *timp;
92d691ba 85 int ch, sectimeout, usemine;
d91fe873 86 char *ap, *mypw, *ttynam, *tzn;
fcc2be53 87 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
92d691ba 88 char *crypt(), *ttyname();
fcc2be53 89
fcc2be53 90 sectimeout = TIMEOUT;
92d691ba
KB
91 mypw = NULL;
92 usemine = 0;
fcc2be53
KB
93 while ((ch = getopt(argc, argv, "pt:")) != EOF)
94 switch((char)ch) {
95 case 't':
eb8ba43e
KB
96 if ((sectimeout = atoi(optarg)) <= 0) {
97 (void)fprintf(stderr,
98 "lock: illegal timeout value.\n");
92d691ba 99 exit(1);
eb8ba43e 100 }
fcc2be53
KB
101 break;
102 case 'p':
92d691ba
KB
103 usemine = 1;
104 if (!(pw = getpwuid(getuid()))) {
eb8ba43e
KB
105 (void)fprintf(stderr,
106 "lock: unknown uid %d.\n", getuid());
92d691ba
KB
107 exit(1);
108 }
109 mypw = strdup(pw->pw_passwd);
fcc2be53
KB
110 break;
111 case '?':
112 default:
eb8ba43e
KB
113 (void)fprintf(stderr,
114 "usage: lock [-p] [-t timeout]\n");
fcc2be53 115 exit(1);
d523b17b
RC
116 }
117 timeout.tv_sec = sectimeout * 60;
118
92d691ba
KB
119 setuid(getuid()); /* discard privs */
120
121 if (ioctl(0, TIOCGETP, &tty)) /* get information for header */
d97d0c53 122 exit(1);
d523b17b 123 gethostname(hostname, sizeof(hostname));
654949dc 124 if (!(ttynam = ttyname(0))) {
eb8ba43e 125 (void)printf("lock: not a terminal?\n");
f84e1e5b 126 exit(1);
654949dc
KB
127 }
128 if (gettimeofday(&timval, (struct timezone *)NULL)) {
eb8ba43e
KB
129 (void)fprintf(stderr,
130 "lock: gettimeofday: %s\n", strerror(errno));
f84e1e5b 131 exit(1);
d523b17b 132 }
d523b17b
RC
133 nexttime = timval.tv_sec + (sectimeout * 60);
134 timp = localtime(&timval.tv_sec);
135 ap = asctime(timp);
654949dc 136 tzn = timp->tm_zone;
d523b17b 137
654949dc
KB
138 (void)signal(SIGINT, quit);
139 (void)signal(SIGQUIT, quit);
d97d0c53 140 ntty = tty; ntty.sg_flags &= ~ECHO;
654949dc
KB
141 (void)ioctl(0, TIOCSETP, &ntty);
142
92d691ba 143 if (!mypw) {
f84e1e5b 144 /* get key and check again */
eb8ba43e 145 (void)printf("Key: ");
92d691ba 146 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
f84e1e5b 147 quit();
eb8ba43e 148 (void)printf("\nAgain: ");
f84e1e5b
KB
149 /*
150 * Don't need EOF test here, if we get EOF, then s1 != s
151 * and the right things will happen.
152 */
92d691ba 153 (void)fgets(s1, sizeof(s1), stdin);
eb8ba43e 154 (void)putchar('\n');
f84e1e5b 155 if (strcmp(s1, s)) {
eb8ba43e 156 (void)printf("\07lock: passwords didn't match.\n");
f84e1e5b
KB
157 ioctl(0, TIOCSETP, &tty);
158 exit(1);
159 }
160 s[0] = NULL;
92d691ba 161 mypw = s1;
d97d0c53 162 }
d523b17b 163
f84e1e5b 164 /* set signal handlers */
654949dc
KB
165 (void)signal(SIGINT, hi);
166 (void)signal(SIGQUIT, hi);
167 (void)signal(SIGTSTP, hi);
168 (void)signal(SIGALRM, bye);
169
d523b17b
RC
170 ntimer.it_interval = zerotime;
171 ntimer.it_value = timeout;
172 setitimer(ITIMER_REAL, &ntimer, &otimer);
173
f84e1e5b 174 /* header info */
eb8ba43e
KB
175(void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
176 ttynam, hostname, sectimeout, ap, tzn, ap + 19);
d523b17b 177
f84e1e5b 178 for (;;) {
eb8ba43e 179 (void)printf("Key: ");
92d691ba 180 if (!fgets(s, sizeof(s), stdin)) {
3e58f554
RE
181 clearerr(stdin);
182 hi();
183 continue;
184 }
92d691ba 185 if (usemine) {
2a36a68c 186 s[strlen(s) - 1] = '\0';
92d691ba 187 if (!strcmp(mypw, crypt(s, mypw)))
f84e1e5b
KB
188 break;
189 }
92d691ba 190 else if (!strcmp(s, s1))
f84e1e5b 191 break;
eb8ba43e 192 (void)printf("\07\n");
4ca10280 193 if (ioctl(0, TIOCGETP, &ntty))
d97d0c53
BJ
194 exit(1);
195 }
654949dc 196 quit();
d523b17b
RC
197}
198
eb8ba43e 199void
654949dc 200hi()
d523b17b 201{
fcc2be53 202 struct timeval timval;
d523b17b 203
654949dc 204 if (!gettimeofday(&timval, (struct timezone *)NULL))
eb8ba43e 205(void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
f84e1e5b 206 (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
d523b17b
RC
207}
208
eb8ba43e 209void
654949dc 210quit()
d523b17b 211{
eb8ba43e 212 (void)putchar('\n');
654949dc 213 (void)ioctl(0, TIOCSETP, &tty);
f84e1e5b 214 exit(0);
654949dc 215}
d523b17b 216
eb8ba43e 217void
654949dc
KB
218bye()
219{
220 (void)ioctl(0, TIOCSETP, &tty);
eb8ba43e 221 (void)printf("lock: timeout\n");
f84e1e5b
KB
222 exit(1);
223}