aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: a1fd0903120c02a374d57972d7750dd876d07fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "ui_ani.h"
#include "ui_input.h"
#include "ui_statusbar.h"
#include "ui.h"

#define AUTHOR "Toni Uhlig"
#define AUTHOR_EMAIL "matzeton@googlemail.com"
#define PKGNAME "naskpass"
#define PKGDESC "A NCurses replacement for cryptsetup's askpass."
#ifdef _VERSION
#define VERSION _VERSION
#else
#define VERSION "unknown"
#endif

#define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger"



static void
usage(char *arg0)
{
  fprintf(stderr, "%s (%s)\n  %s\n", PKGNAME, VERSION, PKGDESC);
  fprintf(stderr, "  Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL);
  fprintf(stderr, "  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\n");
  fprintf(stderr, "  Command: %s: [passfifo]\n", arg0);
}

static bool
check_fifo(char *fifo_path)
{
  struct stat st;

  if (mkfifo(fifo_path, S_IRUSR | S_IWUSR) == 0) {
    return (true);
  } else {
    if (errno == EEXIST) {
      if (stat(fifo_path, &st) == 0) {
        if (S_ISFIFO(st.st_mode) == 1) {
          return (true);
        } else {
          fprintf(stderr, "stat: %s is not a FIFO\n", fifo_path);
          return (false);
        }
      }
    }
  }
  perror("check_fifo");
  return (false);
}

int main(int argc, char **argv)
{
  int ffd;
  size_t plen;

  if (argc != 2) {
    usage(argv[0]);
    exit(EXIT_FAILURE);
  }

  if (check_fifo(argv[1]) == false) {
    exit(EXIT_FAILURE);
  }
  if ((ffd = open(argv[1], O_NONBLOCK | O_RDWR)) < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }

  do_ui();
  if (passwd != NULL) {
    plen = strlen(passwd);
    printf("Sending your password to the FIFO ..\n");
    if (write(ffd, (const void *)passwd, plen) != plen) {
     perror("write");
    } else {
      printf("Ok.\n");
    }
    memset(passwd, '\0', plen);
    plen = 0;
    free(passwd);
  }

  close(ffd);
  return (EXIT_SUCCESS);
}