BSD 4_3_Tahoe release
[unix-history] / usr / src / ucb / lock.c
index ca64fc1..059759f 100644 (file)
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1980, 1987 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)lock.c     5.1 (Berkeley) 5/31/85";
-#endif not lint
+static char sccsid[] = "@(#)lock.c     5.6 (Berkeley) 6/29/88";
+#endif /* not lint */
 
 /*
 
 /*
- * Lock a terminal up until the given key is entered,
- * or until the root password is entered,
- * or the given interval times out.
+ * Lock a terminal up until the given key is entered, until the root
+ * password is entered, or the given interval times out.
  *
  * Timeout interval is by default TIMEOUT, it can be changed with
  * an argument of the form -time where time is in minutes
  */
 
  *
  * Timeout interval is by default TIMEOUT, it can be changed with
  * an argument of the form -time where time is in minutes
  */
 
-#include <pwd.h>
-#include <stdio.h>
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <sys/time.h>
-#include <signal.h>
+#include <sys/signal.h>
+#include <pwd.h>
 #include <sgtty.h>
 #include <sgtty.h>
+#include <stdio.h>
+#include <ctype.h>
 
 
-#define TIMEOUT 15
-
-struct passwd *pwd;
-char   *crypt();
-char   *getpass();
-char   *index();
-char   *ttyname();
-char   *timezone();
-char   *asctime();
-struct tm *localtime();
+#define        TIMEOUT 15
+#define        YES     1
+#define        NO      0
 
 
-int    quit();
-int    bye();
-int    hi();
+int    quit(), bye(), hi();
 
 
-struct timeval timeout = {0, 0};
-struct timeval zerotime = {0, 0};
+struct timeval timeout;
+struct timeval zerotime;
 struct sgttyb  tty, ntty;
 struct sgttyb  tty, ntty;
-long   nexttime;               /* keep the timeout time */
+long   nexttime;                       /* keep the timeout time */
 
 
+/*ARGSUSED*/
 main(argc, argv)
 main(argc, argv)
-       int argc;
-       char **argv;
+       int     argc;
+       char    **argv;
 {
 {
-       register int t;
-       char    *ttynam;
-       char    *ap;
-       int     sectimeout = TIMEOUT;
-       char    s[BUFSIZ], s1[BUFSIZ];
-       char    hostname[32];
-       char    *tzn;
+       struct passwd   *root_pwd, *my_pwd;
        struct timeval  timval;
        struct itimerval        ntimer, otimer;
        struct timeval  timval;
        struct itimerval        ntimer, otimer;
-       struct timezone timzone;
        struct tm       *timp;
        struct tm       *timp;
-       struct stat     statb;
-
-       /* process arguments */
-
-       if (argc > 1){
-               if (argv[1][0] != '-')
-                       goto usage;
-               if (sscanf(&(argv[1][1]), "%d", &sectimeout) != 1)
-                       goto usage;
+       int     sectimeout = TIMEOUT,
+               use_mine;
+       char    *ttynam, *ap, *tzn,
+               hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ],
+               *crypt(), *index(), *ttyname();
+
+       use_mine = NO;
+       for (++argv; *argv; ++argv) {
+               if (argv[0][0] != '-')
+                       usage();
+               if (argv[0][1] == 'p')
+                       use_mine = YES;
+               else if (!isdigit(argv[0][1])) {
+                       fprintf(stderr, "lock: illegal option -- %c\n", argv[0][1]);
+                       usage();
+               }
+               else if ((sectimeout = atoi(*argv + 1)) <= 0)
+                       usage();
        }
        timeout.tv_sec = sectimeout * 60;
 
        /* get information for header */
        }
        timeout.tv_sec = sectimeout * 60;
 
        /* get information for header */
-
        if (ioctl(0, TIOCGETP, &tty))
                exit(1);
        if (ioctl(0, TIOCGETP, &tty))
                exit(1);
-       pwd = getpwuid(0);
        gethostname(hostname, sizeof(hostname));
        gethostname(hostname, sizeof(hostname));
-       if (!(ttynam = ttyname(0))){
-               printf("lock: not a terminal?");
-               exit (1);
+       if (!(ttynam = ttyname(0))) {
+               puts("lock: not a terminal?");
+               exit(1);
+       }
+       if (gettimeofday(&timval, (struct timezone *)NULL)) {
+               perror("gettimeofday");
+               exit(1);
        }
        }
-       gettimeofday(&timval, &timzone);
        nexttime = timval.tv_sec + (sectimeout * 60);
        timp = localtime(&timval.tv_sec);
        ap = asctime(timp);
        nexttime = timval.tv_sec + (sectimeout * 60);
        timp = localtime(&timval.tv_sec);
        ap = asctime(timp);
-       tzn = timezone(timzone.tz_minuteswest, timp->tm_isdst);
-
-       /* get key and check again */
+       tzn = timp->tm_zone;
 
 
-       signal(SIGINT, quit);
-       signal(SIGQUIT, quit);
+       (void)signal(SIGINT, quit);
+       (void)signal(SIGQUIT, quit);
        ntty = tty; ntty.sg_flags &= ~ECHO;
        ntty = tty; ntty.sg_flags &= ~ECHO;
-       ioctl(0, TIOCSETN, &ntty);
-       printf("Key: ");
-       if (fgets(s, sizeof s, stdin) == NULL) {
+       (void)ioctl(0, TIOCSETP, &ntty);
+
+       if (!use_mine) {
+               /* get key and check again */
+               fputs("Key: ", stdout);
+               if (!gets(s, sizeof(s)))
+                       quit();
+               fputs("\nAgain: ", stdout);
+               /*
+                * Don't need EOF test here, if we get EOF, then s1 != s
+                * and the right things will happen.
+                */
+               (void)gets(s1, sizeof(s1));
                putchar('\n');
                putchar('\n');
-               quit();
-       }
-       printf("\nAgain: ");
-       /*
-        * Don't need EOF test here, if we get EOF, then s1 != s
-        * and the right things will happen.
-        */
-       (void) fgets(s1, sizeof s1, stdin);
-       putchar('\n');
-       if (strcmp(s1, s)) {
-               putchar(07);
-               stty(0, &tty);
-               exit(1);
+               if (strcmp(s1, s)) {
+                       puts("\07lock: passwords didn't match.");
+                       ioctl(0, TIOCSETP, &tty);
+                       exit(1);
+               }
+               s[0] = NULL;
        }
        }
-       s[0] = 0;
 
 
-       /* Set signal handlers */
+       /* set signal handlers */
+       (void)signal(SIGINT, hi);
+       (void)signal(SIGQUIT, hi);
+       (void)signal(SIGTSTP, hi);
+       (void)signal(SIGALRM, bye);
 
 
-       signal(SIGINT, hi);
-       signal(SIGQUIT, hi);
-       signal(SIGTSTP, hi);
-       signal(SIGALRM, bye);
        ntimer.it_interval = zerotime;
        ntimer.it_value = timeout;
        setitimer(ITIMER_REAL, &ntimer, &otimer);
 
        ntimer.it_interval = zerotime;
        ntimer.it_value = timeout;
        setitimer(ITIMER_REAL, &ntimer, &otimer);
 
-       /* Header info */
-
-       printf ("lock: %s on %s. timeout in %d minutes\n",
-               ttynam, hostname, sectimeout);
-       printf("time now is %.20s", ap);
-       if (tzn)
-               printf("%s", tzn);
-       printf("%s", ap+19);
+       /* header info */
+       printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
+               ttynam, hostname, sectimeout, ap, tzn, ap + 19);
 
        /* wait */
 
        /* wait */
-
+       root_pwd = getpwuid(0);
+       if (use_mine)
+               my_pwd = getpwuid(getuid());
        for (;;) {
        for (;;) {
-               printf("Key: ");
-               if (fgets(s, sizeof s, stdin) == NULL) {
+               fputs("Key: ", stdout);
+               if (!gets(s, sizeof(s))) {
                        clearerr(stdin);
                        hi();
                        continue;
                }
                        clearerr(stdin);
                        hi();
                        continue;
                }
-               if (strcmp(s1, s) == 0)
-                       break;
-               if (pwd == (struct passwd *) 0 || pwd->pw_passwd[0] == '\0')
+               if (use_mine) {
+                       if (!my_pwd || !*my_pwd->pw_passwd || !strcmp(my_pwd->pw_passwd, crypt(s, my_pwd->pw_passwd)))
+                               break;
+               }
+               else if (!strcmp(s1, s))
                        break;
                        break;
-               ap = index(s, '\n');
-               if (ap != NULL)
-                       *ap = '\0';
-               if (strcmp(pwd->pw_passwd, crypt(s, pwd->pw_passwd)) == 0)
+               if (!root_pwd || !*root_pwd->pw_passwd || !strcmp(root_pwd->pw_passwd, crypt(s, root_pwd->pw_passwd)))
                        break;
                        break;
-               printf("\07\n");
+               puts("\07");
                if (ioctl(0, TIOCGETP, &ntty))
                        exit(1);
        }
                if (ioctl(0, TIOCGETP, &ntty))
                        exit(1);
        }
-       ioctl(0, TIOCSETN, &tty);
        putchar('\n');
        putchar('\n');
-       exit (0);
-usage:
-       printf("Usage: lock [-timeout]\n");
-       exit (1);
+       quit();
 }
 
 }
 
-/*
- *     get out of here
- */
+static
+hi()
+{
+       struct timeval  timval;
+
+       if (!gettimeofday(&timval, (struct timezone *)NULL))
+           printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
+           (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
+}
 
 
+static
 quit()
 {
 quit()
 {
-       ioctl(0, TIOCSETN, &tty);
-       exit (0);
+       (void)ioctl(0, TIOCSETP, &tty);
+       exit(0);
 }
 
 }
 
+static
 bye()
 {
 bye()
 {
-       ioctl(0, TIOCSETN, &tty);
-       printf("lock: timeout\n");
-       exit (1);
+       (void)ioctl(0, TIOCSETP, &tty);
+       puts("lock: timeout");
+       exit(1);
 }
 
 }
 
-/*
- *     tell the user we are waiting
- */
-
-hi()
+static
+usage()
 {
 {
-       long    curtime;
-       struct timeval  timval;
-       struct timezone timzone;
-
-       gettimeofday(&timval, &timzone);
-       curtime = timval.tv_sec;
-       printf("lock: type in the unlock key. timeout in %d minutes\n",
-               (nexttime-curtime)/60);
+       fputs("usage: lock [-p] [-timeout]\n", stderr);
+       exit(1);
 }
 }