fixed order of tgoto args, zapped reliance on former isprint bug
[unix-history] / usr / src / usr.bin / lock / lock.c
CommitLineData
22e155fc 1/*
750362f3
KB
2 * Copyright (c) 1980, 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22e155fc
DF
16 */
17
18#ifndef lint
19char copyright[] =
750362f3 20"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
22e155fc 21 All rights reserved.\n";
750362f3 22#endif /* not lint */
22e155fc 23
4ca10280 24#ifndef lint
d91fe873 25static char sccsid[] = "@(#)lock.c 5.9 (Berkeley) %G%";
750362f3 26#endif /* not lint */
4ca10280
SL
27
28/*
f84e1e5b
KB
29 * Lock a terminal up until the given key is entered, until the root
30 * password is entered, or the given interval times out.
d523b17b
RC
31 *
32 * Timeout interval is by default TIMEOUT, it can be changed with
33 * an argument of the form -time where time is in minutes
4ca10280 34 */
d523b17b 35
654949dc 36#include <sys/param.h>
36e4dcd1 37#include <sys/stat.h>
d523b17b 38#include <sys/time.h>
654949dc 39#include <sys/signal.h>
d97d0c53 40#include <sgtty.h>
92d691ba 41#include <pwd.h>
654949dc 42#include <stdio.h>
2cbfd2f5 43#include <ctype.h>
92d691ba 44#include <strings.h>
d97d0c53 45
f84e1e5b 46#define TIMEOUT 15
d523b17b 47
654949dc 48int quit(), bye(), hi();
d523b17b 49
654949dc
KB
50struct timeval timeout;
51struct timeval zerotime;
d523b17b 52struct sgttyb tty, ntty;
f84e1e5b 53long nexttime; /* keep the timeout time */
d97d0c53 54
f84e1e5b
KB
55/*ARGSUSED*/
56main(argc, argv)
fcc2be53
KB
57 int argc;
58 char **argv;
d97d0c53 59{
fcc2be53 60 extern char *optarg;
92d691ba
KB
61 extern int errno, optind;
62 struct passwd *pw;
fcc2be53
KB
63 struct timeval timval;
64 struct itimerval ntimer, otimer;
65 struct tm *timp;
92d691ba 66 int ch, sectimeout, usemine;
d91fe873 67 char *ap, *mypw, *ttynam, *tzn;
fcc2be53 68 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
92d691ba 69 char *crypt(), *ttyname();
fcc2be53 70
fcc2be53 71 sectimeout = TIMEOUT;
92d691ba
KB
72 mypw = NULL;
73 usemine = 0;
fcc2be53
KB
74 while ((ch = getopt(argc, argv, "pt:")) != EOF)
75 switch((char)ch) {
76 case 't':
77 if ((sectimeout = atoi(optarg)) <= 0)
92d691ba 78 exit(1);
fcc2be53
KB
79 break;
80 case 'p':
92d691ba
KB
81 usemine = 1;
82 if (!(pw = getpwuid(getuid()))) {
83 fprintf(stderr, "lock: unknown uid %d.\n",
84 getuid());
85 exit(1);
86 }
87 mypw = strdup(pw->pw_passwd);
fcc2be53
KB
88 break;
89 case '?':
90 default:
92d691ba 91 fprintf(stderr, "usage: lock [-p] [-t timeout]\n");
fcc2be53 92 exit(1);
d523b17b
RC
93 }
94 timeout.tv_sec = sectimeout * 60;
95
92d691ba
KB
96 setuid(getuid()); /* discard privs */
97
98 if (ioctl(0, TIOCGETP, &tty)) /* get information for header */
d97d0c53 99 exit(1);
d523b17b 100 gethostname(hostname, sizeof(hostname));
654949dc 101 if (!(ttynam = ttyname(0))) {
92d691ba 102 printf("lock: not a terminal?\n");
f84e1e5b 103 exit(1);
654949dc
KB
104 }
105 if (gettimeofday(&timval, (struct timezone *)NULL)) {
92d691ba 106 fprintf(stderr, "lock: gettimeofday: %s\n", strerror(errno));
f84e1e5b 107 exit(1);
d523b17b 108 }
d523b17b
RC
109 nexttime = timval.tv_sec + (sectimeout * 60);
110 timp = localtime(&timval.tv_sec);
111 ap = asctime(timp);
654949dc 112 tzn = timp->tm_zone;
d523b17b 113
654949dc
KB
114 (void)signal(SIGINT, quit);
115 (void)signal(SIGQUIT, quit);
d97d0c53 116 ntty = tty; ntty.sg_flags &= ~ECHO;
654949dc
KB
117 (void)ioctl(0, TIOCSETP, &ntty);
118
92d691ba 119 if (!mypw) {
f84e1e5b 120 /* get key and check again */
92d691ba
KB
121 printf("Key: ");
122 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
f84e1e5b 123 quit();
92d691ba 124 printf("\nAgain: ");
f84e1e5b
KB
125 /*
126 * Don't need EOF test here, if we get EOF, then s1 != s
127 * and the right things will happen.
128 */
92d691ba 129 (void)fgets(s1, sizeof(s1), stdin);
f84e1e5b
KB
130 putchar('\n');
131 if (strcmp(s1, s)) {
92d691ba 132 printf("\07lock: passwords didn't match.\n");
f84e1e5b
KB
133 ioctl(0, TIOCSETP, &tty);
134 exit(1);
135 }
136 s[0] = NULL;
92d691ba 137 mypw = s1;
d97d0c53 138 }
d523b17b 139
f84e1e5b 140 /* set signal handlers */
654949dc
KB
141 (void)signal(SIGINT, hi);
142 (void)signal(SIGQUIT, hi);
143 (void)signal(SIGTSTP, hi);
144 (void)signal(SIGALRM, bye);
145
d523b17b
RC
146 ntimer.it_interval = zerotime;
147 ntimer.it_value = timeout;
148 setitimer(ITIMER_REAL, &ntimer, &otimer);
149
f84e1e5b 150 /* header info */
654949dc 151 printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
f84e1e5b 152 ttynam, hostname, sectimeout, ap, tzn, ap + 19);
d523b17b 153
f84e1e5b 154 for (;;) {
92d691ba
KB
155 printf("Key: ");
156 if (!fgets(s, sizeof(s), stdin)) {
3e58f554
RE
157 clearerr(stdin);
158 hi();
159 continue;
160 }
92d691ba
KB
161 if (usemine) {
162 if (!strcmp(mypw, crypt(s, mypw)))
f84e1e5b
KB
163 break;
164 }
92d691ba 165 else if (!strcmp(s, s1))
f84e1e5b 166 break;
92d691ba 167 printf("\07\n");
4ca10280 168 if (ioctl(0, TIOCGETP, &ntty))
d97d0c53
BJ
169 exit(1);
170 }
654949dc 171 quit();
d523b17b
RC
172}
173
654949dc
KB
174static
175hi()
d523b17b 176{
fcc2be53 177 struct timeval timval;
d523b17b 178
654949dc 179 if (!gettimeofday(&timval, (struct timezone *)NULL))
f84e1e5b
KB
180 printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
181 (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
d523b17b
RC
182}
183
654949dc
KB
184static
185quit()
d523b17b 186{
92d691ba 187 putchar('\n');
654949dc 188 (void)ioctl(0, TIOCSETP, &tty);
f84e1e5b 189 exit(0);
654949dc 190}
d523b17b 191
654949dc
KB
192static
193bye()
194{
195 (void)ioctl(0, TIOCSETP, &tty);
92d691ba 196 printf("lock: timeout\n");
f84e1e5b
KB
197 exit(1);
198}