39
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
 
41
41
#include <stdio.h>              /* fprintf(), stderr, getline(),
 
42
 
                                   stdin, feof(), fputc()
 
 
42
                                   stdin, feof(), perror(), fputc()
 
44
44
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
 
45
45
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
 
47
 
#include <error.h>              /* error() */
 
48
47
#include <iso646.h>             /* or, not */
 
49
48
#include <stdbool.h>            /* bool, false, true */
 
50
 
#include <string.h>             /* strlen, rindex */
 
 
49
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
 
51
50
#include <argp.h>               /* struct argp_option, struct
 
52
51
                                   argp_state, struct argp,
 
53
52
                                   argp_parse(), error_t,
 
 
170
168
  sigemptyset(&new_action.sa_mask);
 
171
169
  ret = sigaddset(&new_action.sa_mask, SIGINT);
 
173
 
    error(0, errno, "sigaddset");
 
176
174
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
178
 
    error(0, errno, "sigaddset");
 
181
179
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
183
 
    error(0, errno, "sigaddset");
 
186
184
  /* Need to check if the handler is SIG_IGN before handling:
 
 
190
188
  ret = sigaction(SIGINT, NULL, &old_action);
 
192
 
    error(0, errno, "sigaction");
 
195
193
  if(old_action.sa_handler != SIG_IGN){
 
196
194
    ret = sigaction(SIGINT, &new_action, NULL);
 
198
 
      error(0, errno, "sigaction");
 
202
200
  ret = sigaction(SIGHUP, NULL, &old_action);
 
204
 
    error(0, errno, "sigaction");
 
207
205
  if(old_action.sa_handler != SIG_IGN){
 
208
206
    ret = sigaction(SIGHUP, &new_action, NULL);
 
210
 
      error(0, errno, "sigaction");
 
214
212
  ret = sigaction(SIGTERM, NULL, &old_action);
 
216
 
    error(0, errno, "sigaction");
 
219
217
  if(old_action.sa_handler != SIG_IGN){
 
220
218
    ret = sigaction(SIGTERM, &new_action, NULL);
 
222
 
      error(0, errno, "sigaction");
 
 
233
231
  t_new.c_lflag &= ~(tcflag_t)ECHO;
 
234
232
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
236
 
    error(0, errno, "tcsetattr-echo");
 
 
234
    perror("tcsetattr-echo");
 
 
260
258
      fprintf(stderr, "%s ", prefix);
 
263
 
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
 
264
 
      const char *crypttarget = getenv("CRYPTTAB_NAME");
 
265
 
      /* Before cryptsetup 1.1.0~rc2 */
 
266
 
      if(cryptsource == NULL){
 
267
 
        cryptsource = getenv("cryptsource");
 
269
 
      if(crypttarget == NULL){
 
270
 
        crypttarget = getenv("crypttarget");
 
272
 
      const char *const prompt1 = "Unlocking the disk";
 
273
 
      const char *const prompt2 = "Enter passphrase";
 
 
261
      const char *cryptsource = getenv("cryptsource");
 
 
262
      const char *crypttarget = getenv("crypttarget");
 
 
263
      const char *const prompt
 
 
264
        = "Enter passphrase to unlock the disk";
 
274
265
      if(cryptsource == NULL){
 
275
266
        if(crypttarget == NULL){
 
276
 
          fprintf(stderr, "%s to unlock the disk: ", prompt2);
 
 
267
          fprintf(stderr, "%s: ", prompt);
 
278
 
          fprintf(stderr, "%s (%s)\n%s: ", prompt1, crypttarget,
 
 
269
          fprintf(stderr, "%s (%s): ", prompt, crypttarget);
 
282
272
        if(crypttarget == NULL){
 
283
 
          fprintf(stderr, "%s %s\n%s: ", prompt1, cryptsource,
 
 
273
          fprintf(stderr, "%s %s: ", prompt, cryptsource);
 
286
 
          fprintf(stderr, "%s %s (%s)\n%s: ", prompt1, cryptsource,
 
287
 
                  crypttarget, prompt2);
 
 
275
          fprintf(stderr, "%s %s (%s): ", prompt, cryptsource,
 
291
 
    sret = getline(&buffer, &n, stdin);
 
 
280
    ret = getline(&buffer, &n, stdin);
 
293
282
      status = EXIT_SUCCESS;
 
294
283
      /* Make n = data size instead of allocated buffer size */
 
296
285
      /* Strip final newline */
 
297
286
      if(n > 0 and buffer[n-1] == '\n'){
 
298
287
        buffer[n-1] = '\0';     /* not strictly necessary */
 
 
301
290
      size_t written = 0;
 
302
291
      while(written < n){
 
303
 
        sret = write(STDOUT_FILENO, buffer + written, n - written);
 
 
292
        ret = write(STDOUT_FILENO, buffer + written, n - written);
 
306
 
          error(0, errno, "write");
 
 
370
359
    fprintf(stderr, "Restoring terminal attributes\n");
 
372
361
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
373
 
    error(0, errno, "tcsetattr+echo");
 
 
362
    perror("tcsetattr+echo");
 
 
378
367
    old_action.sa_handler = SIG_DFL;
 
379
368
    ret = sigaction(signal_received, &old_action, NULL);
 
381
 
      error(0, errno, "sigaction");
 
383
372
    raise(signal_received);