root/shared/term.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. echo
  2. dgetpass
  3. fcprompt
  4. fatal

   1 /*
   2 ** Copyright (c) 2024 Amelia Campbell
   3 **
   4 ** Redistribution and use in source and binary forms, with or without
   5 ** modification, are permitted provided that the following conditions are
   6 ** met: 
   7 **
   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
  13 **     distribution.
  14 **  3. Neither the name of the copyright holder nor the names of its
  15 **     contributors may be used to endorse or promote products derived
  16 **     from this software without specific prior written permission.
  17 **
  18 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19 ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  21 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29 */
  30 
  31 #include <stdio.h>
  32 #include <stdlib.h>
  33 #include <stdarg.h>
  34 #include <string.h>
  35 #include <termios.h>
  36 
  37 #include "term.h"
  38 #include "macro.h"
  39 
  40 char password[MAXPASS] = {0};
  41 
  42 
  43 /* enable/disable terminal echo for password prompts */
  44 void echo(FILE *fp, int echo)
  45     {
  46     struct termios t;
  47 
  48     tcgetattr(fileno(fp), &t);
  49     if (!echo)
  50         t.c_lflag &= ~(ECHO);
  51     else
  52         t.c_lflag |= ECHO;
  53     tcsetattr(fileno(fp), TCSANOW, &t);
  54     }
  55 
  56 
  57 /* get a password from /dev/tty directly, ignoring all redirections */
  58 void dgetpass(int verify)
  59     {
  60     char vp[MAXPASS];
  61     FILE *tty;
  62 
  63     memset(password, 0, MAXPASS);
  64 
  65     GET_TTY;
  66     fprintf(tty, "enter password: ");
  67     echo(tty, 0);
  68     fgets(password, MAXPASS, tty);
  69     echo(tty, 1);
  70     fprintf(tty, "\n");
  71     if (verify)
  72         {
  73         fprintf(tty, "confirm password: ");
  74         echo(tty, 0);
  75         fgets(vp, MAXPASS, tty);
  76         echo(tty, 1);
  77         fprintf(tty, "\n");
  78         if (strncmp(password, vp, MAXPASS) != 0)
  79             fatal("the passwords do not match, aborting\n");
  80         }
  81     fclose(tty);
  82     }
  83 
  84 
  85 int fcprompt(const char *warn)
  86     {
  87     FILE *tty;
  88     char recv[16];
  89 
  90     GET_TTY;
  91     fprintf(tty, "%s: are you sure? (y/n): ", warn);
  92     fgets(recv, 16, tty);
  93     fclose(tty);
  94     if (recv[0] != 'y' && recv[0] != 'Y')
  95         return 1;
  96     return 0;
  97     }
  98 
  99 
 100 void fatal(const char *fmt, ...)
 101     {
 102     va_list ap;
 103 
 104     va_start(ap, fmt);
 105     vfprintf(stderr, fmt, ap);
 106     va_end(ap);
 107     exit(1);
 108     }

/* [previous][next][first][last][top][bottom][index][help] */