/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: 2010-11-16 18:10:22 UTC
  • mfrom: (237.5.1 mandos-release)
  • Revision ID: teddy@fukt.bsnet.se-20101116181022-oz0dahg0q4jnyb0y
Merge from Björn

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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
22
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
 
#define _GNU_SOURCE             /* getline() */
 
25
#define _GNU_SOURCE             /* getline(), asprintf() */
26
26
 
27
27
#include <termios.h>            /* struct termios, tcsetattr(),
28
28
                                   TCSAFLUSH, tcgetattr(), ECHO */
29
29
#include <unistd.h>             /* struct termios, tcsetattr(),
30
30
                                   STDIN_FILENO, TCSAFLUSH,
31
 
                                   tcgetattr(), ECHO */
 
31
                                   tcgetattr(), ECHO, readlink() */
32
32
#include <signal.h>             /* sig_atomic_t, raise(), struct
33
33
                                   sigaction, sigemptyset(),
34
34
                                   sigaction(), sigaddset(), SIGINT,
35
35
                                   SIGQUIT, SIGHUP, SIGTERM,
36
36
                                   raise() */
37
37
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
 
#include <sys/types.h>          /* ssize_t */
 
38
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t, ssize_t */
39
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
40
 
                                   getopt_long, getenv() */
 
40
                                   getenv(), free() */
 
41
#include <dirent.h>             /* scandir(), alphasort() */
41
42
#include <stdio.h>              /* fprintf(), stderr, getline(),
42
 
                                   stdin, feof(), perror(), fputc(),
43
 
                                   getopt_long */
 
43
                                   stdin, feof(), fputc()
 
44
                                */
44
45
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
45
46
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
46
47
                                */
 
48
#include <error.h>              /* error() */
47
49
#include <iso646.h>             /* or, not */
48
50
#include <stdbool.h>            /* bool, false, true */
49
 
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
 
51
#include <inttypes.h>           /* strtoumax() */
 
52
#include <sys/stat.h>           /* struct stat, lstat() */
 
53
#include <string.h>             /* strlen, rindex, memcmp */
50
54
#include <argp.h>               /* struct argp_option, struct
51
55
                                   argp_state, struct argp,
52
56
                                   argp_parse(), error_t,
61
65
const char *argp_program_version = "password-prompt " VERSION;
62
66
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
63
67
 
 
68
/* Needed for conflic resolution */
 
69
const char plymouthd_path[] = "/sbin/plymouth";
 
70
 
 
71
 
64
72
static void termination_handler(int signum){
65
73
  if(quit_now){
66
74
    return;
69
77
  signal_received = signum;
70
78
}
71
79
 
 
80
bool conflict_detection(void){
 
81
 
 
82
  /* plymouth conflicts with password-promt since both want to control the
 
83
     associated terminal. Password-prompt exit since plymouth perferms the same
 
84
     functionallity.
 
85
   */
 
86
  int is_plymouth(const struct dirent *proc_entry){
 
87
    int ret;
 
88
    {
 
89
      uintmax_t maxvalue;
 
90
      char *tmp;
 
91
      errno = 0;
 
92
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
93
      
 
94
      if(errno != 0 or *tmp != '\0'
 
95
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
96
        return 0;
 
97
      }
 
98
    }
 
99
    char exe_target[sizeof(plymouthd_path)];
 
100
    char *exe_link;
 
101
    ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
 
102
    if(ret == -1){
 
103
      error(0, errno, "asprintf");
 
104
      return 0;
 
105
    }
 
106
  
 
107
    struct stat exe_stat;
 
108
    ret = lstat(exe_link, &exe_stat);
 
109
    if(ret == -1){
 
110
      free(exe_link);
 
111
      if(errno != ENOENT){
 
112
        error(0, errno, "lstat");
 
113
      }
 
114
      return 0;
 
115
    }
 
116
  
 
117
    if(not S_ISLNK(exe_stat.st_mode)
 
118
       or exe_stat.st_uid != 0
 
119
       or exe_stat.st_gid != 0){
 
120
      free(exe_link);
 
121
      return 0;
 
122
    }
 
123
  
 
124
    ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
 
125
    free(exe_link);
 
126
    if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
 
127
       (memcmp(plymouthd_path, exe_target,
 
128
               sizeof(plymouthd_path)-1) != 0)){
 
129
      return 0;
 
130
    }
 
131
    return 1;
 
132
  }
 
133
 
 
134
  struct dirent **direntries;
 
135
  int ret;
 
136
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
 
137
  if (ret == -1){
 
138
    error(1, errno, "scandir");
 
139
  }
 
140
  if (ret < 0){
 
141
    return 1;
 
142
  } else {
 
143
    return 0;
 
144
  }
 
145
}
 
146
 
 
147
 
72
148
int main(int argc, char **argv){
73
 
  ssize_t ret;
 
149
  ssize_t sret;
 
150
  int ret;
74
151
  size_t n;
75
152
  struct termios t_new, t_old;
76
153
  char *buffer = NULL;
86
163
        .doc = "Prefix shown before the prompt", .group = 2 },
87
164
      { .name = "debug", .key = 128,
88
165
        .doc = "Debug mode", .group = 3 },
 
166
      /*
 
167
       * These reproduce what we would get without ARGP_NO_HELP
 
168
       */
 
169
      { .name = "help", .key = '?',
 
170
        .doc = "Give this help list", .group = -1 },
 
171
      { .name = "usage", .key = -3,
 
172
        .doc = "Give a short usage message", .group = -1 },
 
173
      { .name = "version", .key = 'V',
 
174
        .doc = "Print program version", .group = -1 },
89
175
      { .name = NULL }
90
176
    };
91
177
    
92
178
    error_t parse_opt (int key, char *arg, struct argp_state *state){
 
179
      errno = 0;
93
180
      switch (key){
94
181
      case 'p':
95
182
        prefix = arg;
97
184
      case 128:
98
185
        debug = true;
99
186
        break;
100
 
      case ARGP_KEY_ARG:
101
 
        argp_usage(state);
102
 
        break;
103
 
      case ARGP_KEY_END:
 
187
        /*
 
188
         * These reproduce what we would get without ARGP_NO_HELP
 
189
         */
 
190
      case '?':                 /* --help */
 
191
        argp_state_help(state, state->out_stream,
 
192
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
193
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
194
      case -3:                  /* --usage */
 
195
        argp_state_help(state, state->out_stream,
 
196
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
197
      case 'V':                 /* --version */
 
198
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
199
        exit(argp_err_exit_status);
104
200
        break;
105
201
      default:
106
202
        return ARGP_ERR_UNKNOWN;
107
203
      }
108
 
      return 0;
 
204
      return errno;
109
205
    }
110
206
    
111
207
    struct argp argp = { .options = options, .parser = parse_opt,
112
208
                         .args_doc = "",
113
209
                         .doc = "Mandos password-prompt -- Read and"
114
210
                         " output a password" };
115
 
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
116
 
    if(ret == ARGP_ERR_UNKNOWN){
117
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
118
 
      return EX_SOFTWARE;
 
211
    ret = argp_parse(&argp, argc, argv,
 
212
                     ARGP_IN_ORDER | ARGP_NO_HELP, NULL, NULL);
 
213
    switch(ret){
 
214
    case 0:
 
215
      break;
 
216
    case ENOMEM:
 
217
    default:
 
218
      errno = ret;
 
219
      error(0, errno, "argp_parse");
 
220
      return EX_OSERR;
 
221
    case EINVAL:
 
222
      return EX_USAGE;
119
223
    }
120
224
  }
121
225
  
122
226
  if(debug){
123
227
    fprintf(stderr, "Starting %s\n", argv[0]);
124
228
  }
 
229
 
 
230
  if (conflict_detection()){
 
231
    if(debug){
 
232
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
 
233
    }
 
234
    return EXIT_FAILURE;
 
235
  }
 
236
  
125
237
  if(debug){
126
238
    fprintf(stderr, "Storing current terminal attributes\n");
127
239
  }
128
240
  
129
241
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
130
242
    int e = errno;
131
 
    perror("tcgetattr");
 
243
    error(0, errno, "tcgetattr");
132
244
    switch(e){
133
245
    case EBADF:
134
246
    case ENOTTY:
141
253
  sigemptyset(&new_action.sa_mask);
142
254
  ret = sigaddset(&new_action.sa_mask, SIGINT);
143
255
  if(ret == -1){
144
 
    perror("sigaddset");
 
256
    error(0, errno, "sigaddset");
145
257
    return EX_OSERR;
146
258
  }
147
259
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
148
260
  if(ret == -1){
149
 
    perror("sigaddset");
 
261
    error(0, errno, "sigaddset");
150
262
    return EX_OSERR;
151
263
  }
152
264
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
153
265
  if(ret == -1){
154
 
    perror("sigaddset");
 
266
    error(0, errno, "sigaddset");
155
267
    return EX_OSERR;
156
268
  }
157
269
  /* Need to check if the handler is SIG_IGN before handling:
160
272
  */
161
273
  ret = sigaction(SIGINT, NULL, &old_action);
162
274
  if(ret == -1){
163
 
    perror("sigaction");
 
275
    error(0, errno, "sigaction");
164
276
    return EX_OSERR;
165
277
  }
166
278
  if(old_action.sa_handler != SIG_IGN){
167
279
    ret = sigaction(SIGINT, &new_action, NULL);
168
280
    if(ret == -1){
169
 
      perror("sigaction");
 
281
      error(0, errno, "sigaction");
170
282
      return EX_OSERR;
171
283
    }
172
284
  }
173
285
  ret = sigaction(SIGHUP, NULL, &old_action);
174
286
  if(ret == -1){
175
 
    perror("sigaction");
 
287
    error(0, errno, "sigaction");
176
288
    return EX_OSERR;
177
289
  }
178
290
  if(old_action.sa_handler != SIG_IGN){
179
291
    ret = sigaction(SIGHUP, &new_action, NULL);
180
292
    if(ret == -1){
181
 
      perror("sigaction");
 
293
      error(0, errno, "sigaction");
182
294
      return EX_OSERR;
183
295
    }
184
296
  }
185
297
  ret = sigaction(SIGTERM, NULL, &old_action);
186
298
  if(ret == -1){
187
 
    perror("sigaction");
 
299
    error(0, errno, "sigaction");
188
300
    return EX_OSERR;
189
301
  }
190
302
  if(old_action.sa_handler != SIG_IGN){
191
303
    ret = sigaction(SIGTERM, &new_action, NULL);
192
304
    if(ret == -1){
193
 
      perror("sigaction");
 
305
      error(0, errno, "sigaction");
194
306
      return EX_OSERR;
195
307
    }
196
308
  }
204
316
  t_new.c_lflag &= ~(tcflag_t)ECHO;
205
317
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
206
318
    int e = errno;
207
 
    perror("tcsetattr-echo");
 
319
    error(0, errno, "tcsetattr-echo");
208
320
    switch(e){
209
321
    case EBADF:
210
322
    case ENOTTY:
231
343
      fprintf(stderr, "%s ", prefix);
232
344
    }
233
345
    {
234
 
      const char *cryptsource = getenv("cryptsource");
235
 
      const char *crypttarget = getenv("crypttarget");
236
 
      const char *const prompt
237
 
        = "Enter passphrase to unlock the disk";
 
346
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
 
347
      const char *crypttarget = getenv("CRYPTTAB_NAME");
 
348
      /* Before cryptsetup 1.1.0~rc2 */
 
349
      if(cryptsource == NULL){
 
350
        cryptsource = getenv("cryptsource");
 
351
      }
 
352
      if(crypttarget == NULL){
 
353
        crypttarget = getenv("crypttarget");
 
354
      }
 
355
      const char *const prompt1 = "Unlocking the disk";
 
356
      const char *const prompt2 = "Enter passphrase";
238
357
      if(cryptsource == NULL){
239
358
        if(crypttarget == NULL){
240
 
          fprintf(stderr, "%s: ", prompt);
 
359
          fprintf(stderr, "%s to unlock the disk: ", prompt2);
241
360
        } else {
242
 
          fprintf(stderr, "%s (%s): ", prompt, crypttarget);
 
361
          fprintf(stderr, "%s (%s)\n%s: ", prompt1, crypttarget,
 
362
                  prompt2);
243
363
        }
244
364
      } else {
245
365
        if(crypttarget == NULL){
246
 
          fprintf(stderr, "%s %s: ", prompt, cryptsource);
 
366
          fprintf(stderr, "%s %s\n%s: ", prompt1, cryptsource,
 
367
                  prompt2);
247
368
        } else {
248
 
          fprintf(stderr, "%s %s (%s): ", prompt, cryptsource,
249
 
                  crypttarget);
 
369
          fprintf(stderr, "%s %s (%s)\n%s: ", prompt1, cryptsource,
 
370
                  crypttarget, prompt2);
250
371
        }
251
372
      }
252
373
    }
253
 
    ret = getline(&buffer, &n, stdin);
254
 
    if(ret > 0){
 
374
    sret = getline(&buffer, &n, stdin);
 
375
    if(sret > 0){
255
376
      status = EXIT_SUCCESS;
256
377
      /* Make n = data size instead of allocated buffer size */
257
 
      n = (size_t)ret;
 
378
      n = (size_t)sret;
258
379
      /* Strip final newline */
259
380
      if(n > 0 and buffer[n-1] == '\n'){
260
381
        buffer[n-1] = '\0';     /* not strictly necessary */
262
383
      }
263
384
      size_t written = 0;
264
385
      while(written < n){
265
 
        ret = write(STDOUT_FILENO, buffer + written, n - written);
266
 
        if(ret < 0){
 
386
        sret = write(STDOUT_FILENO, buffer + written, n - written);
 
387
        if(sret < 0){
267
388
          int e = errno;
268
 
          perror("write");
 
389
          error(0, errno, "write");
269
390
          switch(e){
270
391
          case EBADF:
271
392
          case EFAULT:
282
403
          }
283
404
          break;
284
405
        }
285
 
        written += (size_t)ret;
 
406
        written += (size_t)sret;
286
407
      }
287
 
      ret = close(STDOUT_FILENO);
288
 
      if(ret == -1){
 
408
      sret = close(STDOUT_FILENO);
 
409
      if(sret == -1){
289
410
        int e = errno;
290
 
        perror("close");
 
411
        error(0, errno, "close");
291
412
        switch(e){
292
413
        case EBADF:
293
414
          status = EX_OSFILE;
300
421
      }
301
422
      break;
302
423
    }
303
 
    if(ret < 0){
 
424
    if(sret < 0){
304
425
      int e = errno;
305
426
      if(errno != EINTR and not feof(stdin)){
306
 
        perror("getline");
 
427
        error(0, errno, "getline");
307
428
        switch(e){
308
429
        case EBADF:
309
430
          status = EX_UNAVAILABLE;
316
437
        break;
317
438
      }
318
439
    }
319
 
    /* if(ret == 0), then the only sensible thing to do is to retry to
 
440
    /* if(sret == 0), then the only sensible thing to do is to retry to
320
441
       read from stdin */
321
442
    fputc('\n', stderr);
322
443
    if(debug and not quit_now){
332
453
    fprintf(stderr, "Restoring terminal attributes\n");
333
454
  }
334
455
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
335
 
    perror("tcsetattr+echo");
 
456
    error(0, errno, "tcsetattr+echo");
336
457
  }
337
458
  
338
459
  if(quit_now){
340
461
    old_action.sa_handler = SIG_DFL;
341
462
    ret = sigaction(signal_received, &old_action, NULL);
342
463
    if(ret == -1){
343
 
      perror("sigaction");
 
464
      error(0, errno, "sigaction");
344
465
    }
345
466
    raise(signal_received);
346
467
  }