Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/sbin/lock/lock.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. usage
  3. setpass

   1 /*
   2  * Copyright (c) 2009, Kohsuke Ohtani
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the author nor the names of any co-contibutors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * lock.c - terminal lock utility.
  32  *
  33  * Required capabilities:
  34  *      CAP_USRFILES
  35  */
  36 
  37 #include <sys/stat.h>
  38 #include <sys/fcntl.h>
  39 
  40 #include <unistd.h>
  41 #include <ctype.h>
  42 #include <err.h>
  43 #include <string.h>
  44 #include <stdlib.h>
  45 #include <errno.h>
  46 #include <stdio.h>
  47 #include <paths.h>
  48 
  49 static void usage(void);
  50 static void setpass(void);
  51 
  52 int
  53 main(int argc, char *argv[])
  54 {
  55         char passcode[PASSWORD_LEN + 1];
  56         FILE *fp;
  57         int update = 0;
  58         char *p;
  59 
  60         if (argc == 2) {
  61                 if (!strcmp(argv[1], "-u"))
  62                         update = 1;
  63                 else
  64                         usage();
  65         }
  66 
  67         /*
  68          * Load current passcode.
  69          */
  70         if ((fp = fopen(_PATH_PASSWD, "r")) == NULL) {
  71                 printf("Passcode is not set.\n");
  72                 setpass();
  73         }
  74         if (((p = fgets(passcode, sizeof(passcode), fp)) == NULL) ||
  75             (strlen(passcode) != 4)) {
  76                 printf("Invalid passcode is set.\n");
  77                 fclose(fp);
  78                 setpass();
  79         }
  80         fclose(fp);
  81 
  82         if (update) {
  83                 /*
  84                  * Update current passcode.
  85                  */
  86                 if (strcmp(getpass("Old passcode:"), passcode)) {
  87                         printf("Mismatch.\n");
  88                         exit(0);
  89                 }
  90                 setpass();
  91         }
  92 
  93         /*
  94          * Lock keyboard until correct passcode input.
  95          */
  96         signal(SIGQUIT, SIG_IGN);
  97         signal(SIGINT, SIG_IGN);
  98         signal(SIGTSTP, SIG_IGN);
  99 
 100         printf("\33[2J");       /* clear screen */
 101         printf("Device is locked.\n");
 102         for (;;) {
 103                 if (!strcmp(getpass("Enter passcode:"), passcode))
 104                         break;
 105         }
 106         exit(0);
 107 }
 108 
 109 static void
 110 usage(void)
 111 {
 112         fputs("usage: lock [-u]\n", stderr);
 113         exit(1);
 114         /* NOTREACHED */
 115 }
 116 
 117 /*
 118  * We don't need to encrypt passcode file because it is stored
 119  * secure area in the file system.
 120  */
 121 static void
 122 setpass(void)
 123 {
 124         FILE *fp;
 125         char *p, *t;
 126         char buf[PASSWORD_LEN + 1];
 127 
 128         for (buf[0] = '\0';;) {
 129                 p = getpass("New passcode:");
 130                 if (!*p) {
 131                         printf("Passcode unchanged.\n");
 132                         exit(0);
 133                 }
 134                 for (t = p; *t && isdigit(*t); ++t);
 135                 if (strlen(p) != 4 || *t) {
 136                         printf("Please enter 4 digit number for passcode.\n");
 137                         continue;
 138                 }
 139                 strlcpy(buf, p, sizeof(buf));
 140                 if (!strcmp(buf, getpass("Retype new passcode:")))
 141                         break;
 142                 printf("Mismatch; try again, EOF to quit.\n");
 143         }
 144         if ((fp = fopen(_PATH_PASSWD, "w+")) == NULL) {
 145                 err(1, NULL);
 146                 exit(0);
 147         }
 148         fputs(buf, fp);
 149         fclose(fp);
 150         exit(0);
 151 }

/* [<][>][^][v][top][bottom][index][help] */