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