/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/password-prompt.c

  • Committer: Teddy Hogeborn
  • Date: 2009-09-05 17:20:46 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905172046-s92efz3m1j4c1sti
* plugins-runner.c (main): Bug fix: Close config file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
5
 
 * Copyright © 2008-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
 
23
 * <https://www.fukt.bsnet.se/~teddy/>.
23
24
 */
24
25
 
25
 
#define _GNU_SOURCE             /* getline(), asprintf() */
 
26
#define _GNU_SOURCE             /* getline() */
26
27
 
27
28
#include <termios.h>            /* struct termios, tcsetattr(),
28
29
                                   TCSAFLUSH, tcgetattr(), ECHO */
29
30
#include <unistd.h>             /* struct termios, tcsetattr(),
30
31
                                   STDIN_FILENO, TCSAFLUSH,
31
 
                                   tcgetattr(), ECHO, readlink() */
 
32
                                   tcgetattr(), ECHO */
32
33
#include <signal.h>             /* sig_atomic_t, raise(), struct
33
34
                                   sigaction, sigemptyset(),
34
35
                                   sigaction(), sigaddset(), SIGINT,
35
36
                                   SIGQUIT, SIGHUP, SIGTERM,
36
37
                                   raise() */
37
38
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
 
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t,
39
 
                                   ssize_t, open() */
 
39
#include <sys/types.h>          /* ssize_t */
40
40
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
41
 
                                   getenv(), free() */
42
 
#include <dirent.h>             /* scandir(), alphasort() */
 
41
                                   getopt_long, getenv() */
43
42
#include <stdio.h>              /* fprintf(), stderr, getline(),
44
 
                                   stdin, feof(), fputc()
45
 
                                */
46
 
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
47
 
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
48
 
                                */
49
 
#include <error.h>              /* error() */
 
43
                                   stdin, feof(), perror(), fputc(),
 
44
                                   stdout, getopt_long */
 
45
#include <errno.h>              /* errno, EINVAL */
50
46
#include <iso646.h>             /* or, not */
51
47
#include <stdbool.h>            /* bool, false, true */
52
 
#include <inttypes.h>           /* strtoumax() */
53
 
#include <sys/stat.h>           /* struct stat, lstat(), open() */
54
 
#include <string.h>             /* strlen, rindex, memcmp */
 
48
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
55
49
#include <argp.h>               /* struct argp_option, struct
56
50
                                   argp_state, struct argp,
57
51
                                   argp_parse(), error_t,
58
52
                                   ARGP_KEY_ARG, ARGP_KEY_END,
59
53
                                   ARGP_ERR_UNKNOWN */
60
 
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
61
 
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
62
 
#include <fcntl.h>              /* open() */
63
54
 
64
55
volatile sig_atomic_t quit_now = 0;
65
56
int signal_received;
67
58
const char *argp_program_version = "password-prompt " VERSION;
68
59
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
69
60
 
70
 
/* Needed for conflict resolution */
71
 
const char plymouth_name[] = "plymouthd";
72
 
const char plymouth_alt_name[] = "plymouthd";
73
 
 
74
 
 
75
61
static void termination_handler(int signum){
76
62
  if(quit_now){
77
63
    return;
80
66
  signal_received = signum;
81
67
}
82
68
 
83
 
bool conflict_detection(void){
84
 
 
85
 
  /* plymouth conflicts with password-prompt since both want to read
86
 
     from the terminal.  Password-prompt will exit if it detects
87
 
     plymouth since plymouth performs the same functionality.
88
 
   */
89
 
  int is_plymouth(const struct dirent *proc_entry){
90
 
    int ret;
91
 
    int cl_fd;
92
 
    {
93
 
      uintmax_t maxvalue;
94
 
      char *tmp;
95
 
      errno = 0;
96
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
97
 
      
98
 
      if(errno != 0 or *tmp != '\0'
99
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
100
 
        return 0;
101
 
      }
102
 
    }
103
 
    
104
 
    char *cmdline_filename;
105
 
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline", proc_entry->d_name);
106
 
    if(ret == -1){
107
 
      error(0, errno, "asprintf");
108
 
      return 0;
109
 
    }
110
 
    
111
 
    /* Open /proc/<pid>/cmdline  */
112
 
    cl_fd = open(cmdline_filename, O_RDONLY);
113
 
    free(cmdline_filename);
114
 
    if(cl_fd == -1){
115
 
      error(0, errno, "open");
116
 
      return 0;
117
 
    }
118
 
    
119
 
    char *cmdline = NULL;
120
 
    {
121
 
      size_t cmdline_len = 0;
122
 
      size_t cmdline_allocated = 0;
123
 
      char *tmp;
124
 
      const size_t blocksize = 1024;
125
 
      ssize_t sret;
126
 
      do {
127
 
        /* Allocate more space? */
128
 
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
129
 
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
130
 
          if(tmp == NULL){
131
 
            error(0, errno, "realloc");
132
 
            free(cmdline);
133
 
            close(cl_fd);
134
 
            return 0;
135
 
          }
136
 
          cmdline = tmp;
137
 
          cmdline_allocated += blocksize;
138
 
        }
139
 
        
140
 
        /* Read data */
141
 
        sret = read(cl_fd, cmdline + cmdline_len,
142
 
                    cmdline_allocated - cmdline_len);
143
 
        if(sret == -1){
144
 
          error(0, errno, "read");
145
 
          free(cmdline);
146
 
          close(cl_fd);
147
 
          return 0;
148
 
        }
149
 
        cmdline_len += (size_t)sret;
150
 
      } while(sret != 0);
151
 
      ret = close(cl_fd);
152
 
      if(ret == -1){
153
 
        error(0, errno, "close");
154
 
        free(cmdline);
155
 
        return 0;
156
 
      }
157
 
      cmdline[cmdline_len] = '\0'; /* Make sure it is terminated */
158
 
    }
159
 
    /* we now have cmdline */
160
 
    
161
 
    /* get basename */
162
 
    char *cmdline_base = strrchr(cmdline, '/');
163
 
    if(cmdline_base != NULL){
164
 
      cmdline_base += 1;                /* skip the slash */
165
 
    } else {
166
 
      cmdline_base = cmdline;
167
 
    }
168
 
    
169
 
    if((strcmp(cmdline_base, plymouth_name) != 0)
170
 
       and (strcmp(cmdline_base, plymouth_alt_name) != 0)){
171
 
      free(cmdline);
172
 
      return 0;
173
 
    }
174
 
    free(cmdline);
175
 
    return 1;
176
 
  }
177
 
 
178
 
  struct dirent **direntries;
179
 
  int ret;
180
 
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
181
 
  if (ret == -1){
182
 
    error(1, errno, "scandir");
183
 
  }
184
 
  return ret > 0;
185
 
}
186
 
 
187
 
 
188
69
int main(int argc, char **argv){
189
 
  ssize_t sret;
190
 
  int ret;
 
70
  ssize_t ret;
191
71
  size_t n;
192
72
  struct termios t_new, t_old;
193
73
  char *buffer = NULL;
203
83
        .doc = "Prefix shown before the prompt", .group = 2 },
204
84
      { .name = "debug", .key = 128,
205
85
        .doc = "Debug mode", .group = 3 },
206
 
      /*
207
 
       * These reproduce what we would get without ARGP_NO_HELP
208
 
       */
209
 
      { .name = "help", .key = '?',
210
 
        .doc = "Give this help list", .group = -1 },
211
 
      { .name = "usage", .key = -3,
212
 
        .doc = "Give a short usage message", .group = -1 },
213
 
      { .name = "version", .key = 'V',
214
 
        .doc = "Print program version", .group = -1 },
215
86
      { .name = NULL }
216
87
    };
217
88
    
218
89
    error_t parse_opt (int key, char *arg, struct argp_state *state){
219
 
      errno = 0;
220
90
      switch (key){
221
91
      case 'p':
222
92
        prefix = arg;
224
94
      case 128:
225
95
        debug = true;
226
96
        break;
227
 
        /*
228
 
         * These reproduce what we would get without ARGP_NO_HELP
229
 
         */
230
 
      case '?':                 /* --help */
231
 
        argp_state_help(state, state->out_stream,
232
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
233
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
234
 
      case -3:                  /* --usage */
235
 
        argp_state_help(state, state->out_stream,
236
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
237
 
      case 'V':                 /* --version */
238
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
239
 
        exit(argp_err_exit_status);
 
97
      case ARGP_KEY_ARG:
 
98
        argp_usage(state);
 
99
        break;
 
100
      case ARGP_KEY_END:
240
101
        break;
241
102
      default:
242
103
        return ARGP_ERR_UNKNOWN;
243
104
      }
244
 
      return errno;
 
105
      return 0;
245
106
    }
246
107
    
247
108
    struct argp argp = { .options = options, .parser = parse_opt,
248
109
                         .args_doc = "",
249
110
                         .doc = "Mandos password-prompt -- Read and"
250
111
                         " output a password" };
251
 
    ret = argp_parse(&argp, argc, argv,
252
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, NULL, NULL);
253
 
    switch(ret){
254
 
    case 0:
255
 
      break;
256
 
    case ENOMEM:
257
 
    default:
258
 
      errno = ret;
259
 
      error(0, errno, "argp_parse");
260
 
      return EX_OSERR;
261
 
    case EINVAL:
262
 
      return EX_USAGE;
 
112
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
113
    if(ret == ARGP_ERR_UNKNOWN){
 
114
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
115
      return EXIT_FAILURE;
263
116
    }
264
117
  }
265
118
  
266
119
  if(debug){
267
120
    fprintf(stderr, "Starting %s\n", argv[0]);
268
121
  }
269
 
 
270
 
  if (conflict_detection()){
271
 
    if(debug){
272
 
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
273
 
    }
274
 
    return EXIT_FAILURE;
275
 
  }
276
 
  
277
122
  if(debug){
278
123
    fprintf(stderr, "Storing current terminal attributes\n");
279
124
  }
280
125
  
281
126
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
282
 
    int e = errno;
283
 
    error(0, errno, "tcgetattr");
284
 
    switch(e){
285
 
    case EBADF:
286
 
    case ENOTTY:
287
 
      return EX_UNAVAILABLE;
288
 
    default:
289
 
      return EX_OSERR;
290
 
    }
 
127
    perror("tcgetattr");
 
128
    return EXIT_FAILURE;
291
129
  }
292
130
  
293
131
  sigemptyset(&new_action.sa_mask);
294
132
  ret = sigaddset(&new_action.sa_mask, SIGINT);
295
133
  if(ret == -1){
296
 
    error(0, errno, "sigaddset");
297
 
    return EX_OSERR;
 
134
    perror("sigaddset");
 
135
    return EXIT_FAILURE;
298
136
  }
299
137
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
300
138
  if(ret == -1){
301
 
    error(0, errno, "sigaddset");
302
 
    return EX_OSERR;
 
139
    perror("sigaddset");
 
140
    return EXIT_FAILURE;
303
141
  }
304
142
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
305
143
  if(ret == -1){
306
 
    error(0, errno, "sigaddset");
307
 
    return EX_OSERR;
 
144
    perror("sigaddset");
 
145
    return EXIT_FAILURE;
308
146
  }
309
147
  /* Need to check if the handler is SIG_IGN before handling:
310
148
     | [[info:libc:Initial Signal Actions]] |
312
150
  */
313
151
  ret = sigaction(SIGINT, NULL, &old_action);
314
152
  if(ret == -1){
315
 
    error(0, errno, "sigaction");
316
 
    return EX_OSERR;
 
153
    perror("sigaction");
 
154
    return EXIT_FAILURE;
317
155
  }
318
156
  if(old_action.sa_handler != SIG_IGN){
319
157
    ret = sigaction(SIGINT, &new_action, NULL);
320
158
    if(ret == -1){
321
 
      error(0, errno, "sigaction");
322
 
      return EX_OSERR;
 
159
      perror("sigaction");
 
160
      return EXIT_FAILURE;
323
161
    }
324
162
  }
325
163
  ret = sigaction(SIGHUP, NULL, &old_action);
326
164
  if(ret == -1){
327
 
    error(0, errno, "sigaction");
328
 
    return EX_OSERR;
 
165
    perror("sigaction");
 
166
    return EXIT_FAILURE;
329
167
  }
330
168
  if(old_action.sa_handler != SIG_IGN){
331
169
    ret = sigaction(SIGHUP, &new_action, NULL);
332
170
    if(ret == -1){
333
 
      error(0, errno, "sigaction");
334
 
      return EX_OSERR;
 
171
      perror("sigaction");
 
172
      return EXIT_FAILURE;
335
173
    }
336
174
  }
337
175
  ret = sigaction(SIGTERM, NULL, &old_action);
338
176
  if(ret == -1){
339
 
    error(0, errno, "sigaction");
340
 
    return EX_OSERR;
 
177
    perror("sigaction");
 
178
    return EXIT_FAILURE;
341
179
  }
342
180
  if(old_action.sa_handler != SIG_IGN){
343
181
    ret = sigaction(SIGTERM, &new_action, NULL);
344
182
    if(ret == -1){
345
 
      error(0, errno, "sigaction");
346
 
      return EX_OSERR;
 
183
      perror("sigaction");
 
184
      return EXIT_FAILURE;
347
185
    }
348
186
  }
349
187
  
353
191
  }
354
192
  
355
193
  t_new = t_old;
356
 
  t_new.c_lflag &= ~(tcflag_t)ECHO;
 
194
  t_new.c_lflag &= ~ECHO;
357
195
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
358
 
    int e = errno;
359
 
    error(0, errno, "tcsetattr-echo");
360
 
    switch(e){
361
 
    case EBADF:
362
 
    case ENOTTY:
363
 
      return EX_UNAVAILABLE;
364
 
    case EINVAL:
365
 
    default:
366
 
      return EX_OSERR;
367
 
    }
 
196
    perror("tcsetattr-echo");
 
197
    return EXIT_FAILURE;
368
198
  }
369
 
  
 
199
 
370
200
  if(debug){
371
201
    fprintf(stderr, "Waiting for input from stdin \n");
372
202
  }
383
213
      fprintf(stderr, "%s ", prefix);
384
214
    }
385
215
    {
386
 
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
387
 
      const char *crypttarget = getenv("CRYPTTAB_NAME");
388
 
      /* Before cryptsetup 1.1.0~rc2 */
389
 
      if(cryptsource == NULL){
390
 
        cryptsource = getenv("cryptsource");
391
 
      }
392
 
      if(crypttarget == NULL){
393
 
        crypttarget = getenv("crypttarget");
394
 
      }
395
 
      const char *const prompt1 = "Unlocking the disk";
396
 
      const char *const prompt2 = "Enter passphrase";
 
216
      const char *cryptsource = getenv("cryptsource");
 
217
      const char *crypttarget = getenv("crypttarget");
 
218
      const char *const prompt
 
219
        = "Enter passphrase to unlock the disk";
397
220
      if(cryptsource == NULL){
398
221
        if(crypttarget == NULL){
399
 
          fprintf(stderr, "%s to unlock the disk: ", prompt2);
 
222
          fprintf(stderr, "%s: ", prompt);
400
223
        } else {
401
 
          fprintf(stderr, "%s (%s)\n%s: ", prompt1, crypttarget,
402
 
                  prompt2);
 
224
          fprintf(stderr, "%s (%s): ", prompt, crypttarget);
403
225
        }
404
226
      } else {
405
227
        if(crypttarget == NULL){
406
 
          fprintf(stderr, "%s %s\n%s: ", prompt1, cryptsource,
407
 
                  prompt2);
 
228
          fprintf(stderr, "%s %s: ", prompt, cryptsource);
408
229
        } else {
409
 
          fprintf(stderr, "%s %s (%s)\n%s: ", prompt1, cryptsource,
410
 
                  crypttarget, prompt2);
 
230
          fprintf(stderr, "%s %s (%s): ", prompt, cryptsource,
 
231
                  crypttarget);
411
232
        }
412
233
      }
413
234
    }
414
 
    sret = getline(&buffer, &n, stdin);
415
 
    if(sret > 0){
 
235
    ret = getline(&buffer, &n, stdin);
 
236
    if(ret > 0){
416
237
      status = EXIT_SUCCESS;
417
238
      /* Make n = data size instead of allocated buffer size */
418
 
      n = (size_t)sret;
 
239
      n = (size_t)ret;
419
240
      /* Strip final newline */
420
 
      if(n > 0 and buffer[n-1] == '\n'){
 
241
      if(n>0 and buffer[n-1] == '\n'){
421
242
        buffer[n-1] = '\0';     /* not strictly necessary */
422
243
        n--;
423
244
      }
424
245
      size_t written = 0;
425
246
      while(written < n){
426
 
        sret = write(STDOUT_FILENO, buffer + written, n - written);
427
 
        if(sret < 0){
428
 
          int e = errno;
429
 
          error(0, errno, "write");
430
 
          switch(e){
431
 
          case EBADF:
432
 
          case EFAULT:
433
 
          case EINVAL:
434
 
          case EFBIG:
435
 
          case EIO:
436
 
          case ENOSPC:
437
 
          default:
438
 
            status = EX_IOERR;
439
 
            break;
440
 
          case EINTR:
441
 
            status = EXIT_FAILURE;
442
 
            break;
443
 
          }
444
 
          break;
445
 
        }
446
 
        written += (size_t)sret;
447
 
      }
448
 
      sret = close(STDOUT_FILENO);
449
 
      if(sret == -1){
450
 
        int e = errno;
451
 
        error(0, errno, "close");
452
 
        switch(e){
453
 
        case EBADF:
454
 
          status = EX_OSFILE;
455
 
          break;
456
 
        case EIO:
457
 
        default:
458
 
          status = EX_IOERR;
459
 
          break;
460
 
        }
 
247
        ret = write(STDOUT_FILENO, buffer + written, n - written);
 
248
        if(ret < 0){
 
249
          perror("write");
 
250
          status = EXIT_FAILURE;
 
251
          break;
 
252
        }
 
253
        written += (size_t)ret;
461
254
      }
462
255
      break;
463
256
    }
464
 
    if(sret < 0){
465
 
      int e = errno;
 
257
    if(ret < 0){
466
258
      if(errno != EINTR and not feof(stdin)){
467
 
        error(0, errno, "getline");
468
 
        switch(e){
469
 
        case EBADF:
470
 
          status = EX_UNAVAILABLE;
471
 
        case EIO:
472
 
        case EINVAL:
473
 
        default:
474
 
          status = EX_IOERR;
475
 
          break;
476
 
        }
 
259
        perror("getline");
 
260
        status = EXIT_FAILURE;
477
261
        break;
478
262
      }
479
263
    }
480
 
    /* if(sret == 0), then the only sensible thing to do is to retry to
 
264
    /* if(ret == 0), then the only sensible thing to do is to retry to
481
265
       read from stdin */
482
266
    fputc('\n', stderr);
483
267
    if(debug and not quit_now){
493
277
    fprintf(stderr, "Restoring terminal attributes\n");
494
278
  }
495
279
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
496
 
    error(0, errno, "tcsetattr+echo");
 
280
    perror("tcsetattr+echo");
497
281
  }
498
282
  
499
283
  if(quit_now){
501
285
    old_action.sa_handler = SIG_DFL;
502
286
    ret = sigaction(signal_received, &old_action, NULL);
503
287
    if(ret == -1){
504
 
      error(0, errno, "sigaction");
 
288
      perror("sigaction");
505
289
    }
506
290
    raise(signal_received);
507
291
  }
510
294
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
511
295
            status);
512
296
  }
513
 
  if(status == EXIT_SUCCESS or status == EX_OK){
 
297
  if(status == EXIT_SUCCESS){
514
298
    fputc('\n', stderr);
515
299
  }
516
300