aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 670503ebf0ddb56229670ac78438403e219fdaff (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

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


static bool ui_active = true;

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:\n\t%s [args]\n", arg0);
  fprintf(stderr, "  Arguments:\n\t-h this\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", DEFAULT_FIFO);
}

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);
}

/* stolen from http://www.gnu.org/software/libc/manual/html_node/Waiting-for-I_002fO.html */
static int
input_timeout(int filedes, unsigned int seconds)
{
  fd_set set;
  struct timeval timeout;

  /* Initialize the file descriptor set. */
  FD_ZERO (&set);
  FD_SET (filedes, &set);
  /* Initialize the timeout data structure. */
  timeout.tv_sec = seconds;
  timeout.tv_usec = 0;
  /* select returns 0 if timeout, 1 if input available, -1 if error. */
  return TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &timeout));
}

int
run_cryptcreate(char *pass, char *crypt_cmd)
{
  int retval;
  char *cmd;

  if (crypt_cmd == NULL || pass == NULL) return (-1);
  asprintf(&cmd, "echo '%s' | %s", pass, crypt_cmd);
  retval = system(cmd);
  return (retval);
}

int
main(int argc, char **argv)
{
  int ffd, c_status, opt;
  pid_t child;
  char pbuf[MAX_PASSWD_LEN+1];
  char *fifo_path = NULL;
  char *crypt_cmd = NULL;

  memset(pbuf, '\0', MAX_PASSWD_LEN+1);

  while ((opt = getopt(argc, argv, "hf:c:")) != -1) {
    switch (opt) {
      case 'h':
        usage(argv[0]);
        exit(EXIT_SUCCESS);
      case 'f':
        fifo_path = strdup(optarg);
        break;
      case 'c':
        crypt_cmd = strdup(optarg);
        break;
      default:
        usage(argv[0]);
        exit(EXIT_FAILURE);
    }
  }
  if (optind < argc) {
    fprintf(stderr, "%s: I dont understand you.\n\n", argv[0]);
    usage(argv[0]);
    exit(EXIT_FAILURE);
  }
  if (fifo_path == NULL) fifo_path = strdup(DEFAULT_FIFO);

  if (check_fifo(fifo_path) == false) {
    usage(argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((ffd = open(fifo_path, O_NONBLOCK | O_RDWR)) < 0) {
    fprintf(stderr, "fifo: %s\n", fifo_path);
    perror("open");
    exit(EXIT_FAILURE);
  }

  if ((child = fork()) == 0) {
    /* child */
    ui_active = true;
    do_ui(ffd);
    ui_active = false;
  } else if (child > 0) {
    /* parent */
    fclose(stdin);
    while (input_timeout(ffd, 1) == 0) {
      usleep(100000);
      if (ui_active == true) {
        // TODO: smthng
      }
    }
    stop_ui();
    wait(&c_status);
    if (read(ffd, pbuf, MAX_PASSWD_LEN) > 0) {
      if (run_cryptcreate(pbuf, crypt_cmd) != 0) {
        fprintf(stderr, "cryptcreate error\n");
      }
    }
    memset(pbuf, '\0', MAX_PASSWD_LEN+1);
  } else {
    /* fork error */
    perror("fork");
    exit(EXIT_FAILURE);
  }

  close(ffd);
  if (crypt_cmd != NULL) free(crypt_cmd);
  free(fifo_path);
  return (EXIT_SUCCESS);
}