/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: 2011-02-27 18:57:58 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110227185758-vie4xhfaevhpqtr6
* plugins.d/password-prompt.c (conflict_detection): Check for both
                                                    "plymouth" and
                                                    "plymouthd".

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-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* getline(), asprintf() */
26
26
 
27
 
#include <termios.h>            /* struct termios, tcsetattr(),
 
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,
41
41
                                   getenv(), free() */
42
42
#include <dirent.h>             /* scandir(), alphasort() */
43
43
#include <stdio.h>              /* fprintf(), stderr, getline(),
44
 
                                   stdin, feof(), fputc(), vfprintf(),
45
 
                                   vasprintf() */
 
44
                                   stdin, feof(), fputc()
 
45
                                */
46
46
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
47
47
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
48
48
                                */
50
50
#include <iso646.h>             /* or, not */
51
51
#include <stdbool.h>            /* bool, false, true */
52
52
#include <inttypes.h>           /* strtoumax() */
53
 
#include <sys/stat.h>           /* struct stat, lstat(), open() */
54
 
#include <string.h>             /* strlen, rindex, memcmp, strerror()
55
 
                                 */
 
53
#include <sys/stat.h>           /* struct stat, lstat(), open() */
 
54
#include <string.h>             /* strlen, rindex, memcmp */
56
55
#include <argp.h>               /* struct argp_option, struct
57
56
                                   argp_state, struct argp,
58
57
                                   argp_parse(), error_t,
61
60
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
62
61
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
63
62
#include <fcntl.h>              /* open() */
64
 
#include <stdarg.h>             /* va_list, va_start(), ... */
65
63
 
66
64
volatile sig_atomic_t quit_now = 0;
67
65
int signal_received;
68
66
bool debug = false;
69
67
const char *argp_program_version = "password-prompt " VERSION;
70
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
68
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
69
 
72
70
/* Needed for conflict resolution */
73
71
const char plymouth_name[] = "plymouthd";
 
72
const char plymouth_alt_name[] = "plymouthd";
74
73
 
75
 
/* Function to use when printing errors */
76
 
__attribute__((format (gnu_printf, 3, 4)))
77
 
void error_plus(int status, int errnum, const char *formatstring,
78
 
                ...){
79
 
  va_list ap;
80
 
  char *text;
81
 
  int ret;
82
 
  
83
 
  va_start(ap, formatstring);
84
 
  ret = vasprintf(&text, formatstring, ap);
85
 
  if(ret == -1){
86
 
    fprintf(stderr, "Mandos plugin %s: ",
87
 
            program_invocation_short_name);
88
 
    vfprintf(stderr, formatstring, ap);
89
 
    fprintf(stderr, ": %s\n", strerror(errnum));
90
 
    error(status, errno, "vasprintf while printing error");
91
 
    return;
92
 
  }
93
 
  fprintf(stderr, "Mandos plugin ");
94
 
  error(status, errnum, "%s", text);
95
 
  free(text);
96
 
}
97
74
 
98
75
static void termination_handler(int signum){
99
76
  if(quit_now){
109
86
     from the terminal.  Password-prompt will exit if it detects
110
87
     plymouth since plymouth performs the same functionality.
111
88
   */
112
 
  __attribute__((nonnull))
113
89
  int is_plymouth(const struct dirent *proc_entry){
114
90
    int ret;
115
91
    int cl_fd;
116
92
    {
117
 
      uintmax_t proc_id;
 
93
      uintmax_t maxvalue;
118
94
      char *tmp;
119
95
      errno = 0;
120
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
96
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
121
97
      
122
98
      if(errno != 0 or *tmp != '\0'
123
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
99
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
124
100
        return 0;
125
101
      }
126
102
    }
127
103
    
128
104
    char *cmdline_filename;
129
 
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
130
 
                   proc_entry->d_name);
 
105
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline", proc_entry->d_name);
131
106
    if(ret == -1){
132
 
      error_plus(0, errno, "asprintf");
 
107
      error(0, errno, "asprintf");
133
108
      return 0;
134
109
    }
135
110
    
136
 
    /* Open /proc/<pid>/cmdline */
 
111
    /* Open /proc/<pid>/cmdline  */
137
112
    cl_fd = open(cmdline_filename, O_RDONLY);
138
113
    free(cmdline_filename);
139
114
    if(cl_fd == -1){
140
 
      if(errno != ENOENT){
141
 
        error_plus(0, errno, "open");
142
 
      }
 
115
      error(0, errno, "open");
143
116
      return 0;
144
117
    }
145
118
    
155
128
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
156
129
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
157
130
          if(tmp == NULL){
158
 
            error_plus(0, errno, "realloc");
 
131
            error(0, errno, "realloc");
159
132
            free(cmdline);
160
133
            close(cl_fd);
161
134
            return 0;
168
141
        sret = read(cl_fd, cmdline + cmdline_len,
169
142
                    cmdline_allocated - cmdline_len);
170
143
        if(sret == -1){
171
 
          error_plus(0, errno, "read");
 
144
          error(0, errno, "read");
172
145
          free(cmdline);
173
146
          close(cl_fd);
174
147
          return 0;
177
150
      } while(sret != 0);
178
151
      ret = close(cl_fd);
179
152
      if(ret == -1){
180
 
        error_plus(0, errno, "close");
 
153
        error(0, errno, "close");
181
154
        free(cmdline);
182
155
        return 0;
183
156
      }
193
166
      cmdline_base = cmdline;
194
167
    }
195
168
    
196
 
    if(strcmp(cmdline_base, plymouth_name) != 0){
197
 
      if(debug){
198
 
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
199
 
                plymouth_name);
200
 
      }
 
169
    if((strcmp(cmdline_base, plymouth_name) != 0)
 
170
       and (strcmp(cmdline_base, plymouth_alt_name) != 0)){
201
171
      free(cmdline);
202
172
      return 0;
203
173
    }
204
 
    if(debug){
205
 
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
206
 
              plymouth_name);
207
 
    }
208
174
    free(cmdline);
209
175
    return 1;
210
176
  }
211
 
  
212
 
  struct dirent **direntries = NULL;
 
177
 
 
178
  struct dirent **direntries;
213
179
  int ret;
214
180
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
215
 
  if(ret == -1){
216
 
    error_plus(1, errno, "scandir");
 
181
  if (ret == -1){
 
182
    error(1, errno, "scandir");
217
183
  }
218
 
  free(direntries);
219
184
  return ret > 0;
220
185
}
221
186
 
250
215
      { .name = NULL }
251
216
    };
252
217
    
253
 
    __attribute__((nonnull(3)))
254
218
    error_t parse_opt (int key, char *arg, struct argp_state *state){
255
219
      errno = 0;
256
220
      switch (key){
292
256
    case ENOMEM:
293
257
    default:
294
258
      errno = ret;
295
 
      error_plus(0, errno, "argp_parse");
 
259
      error(0, errno, "argp_parse");
296
260
      return EX_OSERR;
297
261
    case EINVAL:
298
262
      return EX_USAGE;
303
267
    fprintf(stderr, "Starting %s\n", argv[0]);
304
268
  }
305
269
 
306
 
  if(conflict_detection()){
 
270
  if (conflict_detection()){
307
271
    if(debug){
308
 
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
 
272
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
309
273
    }
310
274
    return EXIT_FAILURE;
311
275
  }
316
280
  
317
281
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
318
282
    int e = errno;
319
 
    error_plus(0, errno, "tcgetattr");
 
283
    error(0, errno, "tcgetattr");
320
284
    switch(e){
321
285
    case EBADF:
322
286
    case ENOTTY:
329
293
  sigemptyset(&new_action.sa_mask);
330
294
  ret = sigaddset(&new_action.sa_mask, SIGINT);
331
295
  if(ret == -1){
332
 
    error_plus(0, errno, "sigaddset");
 
296
    error(0, errno, "sigaddset");
333
297
    return EX_OSERR;
334
298
  }
335
299
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
336
300
  if(ret == -1){
337
 
    error_plus(0, errno, "sigaddset");
 
301
    error(0, errno, "sigaddset");
338
302
    return EX_OSERR;
339
303
  }
340
304
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
341
305
  if(ret == -1){
342
 
    error_plus(0, errno, "sigaddset");
 
306
    error(0, errno, "sigaddset");
343
307
    return EX_OSERR;
344
308
  }
345
309
  /* Need to check if the handler is SIG_IGN before handling:
348
312
  */
349
313
  ret = sigaction(SIGINT, NULL, &old_action);
350
314
  if(ret == -1){
351
 
    error_plus(0, errno, "sigaction");
 
315
    error(0, errno, "sigaction");
352
316
    return EX_OSERR;
353
317
  }
354
318
  if(old_action.sa_handler != SIG_IGN){
355
319
    ret = sigaction(SIGINT, &new_action, NULL);
356
320
    if(ret == -1){
357
 
      error_plus(0, errno, "sigaction");
 
321
      error(0, errno, "sigaction");
358
322
      return EX_OSERR;
359
323
    }
360
324
  }
361
325
  ret = sigaction(SIGHUP, NULL, &old_action);
362
326
  if(ret == -1){
363
 
    error_plus(0, errno, "sigaction");
 
327
    error(0, errno, "sigaction");
364
328
    return EX_OSERR;
365
329
  }
366
330
  if(old_action.sa_handler != SIG_IGN){
367
331
    ret = sigaction(SIGHUP, &new_action, NULL);
368
332
    if(ret == -1){
369
 
      error_plus(0, errno, "sigaction");
 
333
      error(0, errno, "sigaction");
370
334
      return EX_OSERR;
371
335
    }
372
336
  }
373
337
  ret = sigaction(SIGTERM, NULL, &old_action);
374
338
  if(ret == -1){
375
 
    error_plus(0, errno, "sigaction");
 
339
    error(0, errno, "sigaction");
376
340
    return EX_OSERR;
377
341
  }
378
342
  if(old_action.sa_handler != SIG_IGN){
379
343
    ret = sigaction(SIGTERM, &new_action, NULL);
380
344
    if(ret == -1){
381
 
      error_plus(0, errno, "sigaction");
 
345
      error(0, errno, "sigaction");
382
346
      return EX_OSERR;
383
347
    }
384
348
  }
392
356
  t_new.c_lflag &= ~(tcflag_t)ECHO;
393
357
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
394
358
    int e = errno;
395
 
    error_plus(0, errno, "tcsetattr-echo");
 
359
    error(0, errno, "tcsetattr-echo");
396
360
    switch(e){
397
361
    case EBADF:
398
362
    case ENOTTY:
462
426
        sret = write(STDOUT_FILENO, buffer + written, n - written);
463
427
        if(sret < 0){
464
428
          int e = errno;
465
 
          error_plus(0, errno, "write");
 
429
          error(0, errno, "write");
466
430
          switch(e){
467
431
          case EBADF:
468
432
          case EFAULT:
484
448
      sret = close(STDOUT_FILENO);
485
449
      if(sret == -1){
486
450
        int e = errno;
487
 
        error_plus(0, errno, "close");
 
451
        error(0, errno, "close");
488
452
        switch(e){
489
453
        case EBADF:
490
454
          status = EX_OSFILE;
500
464
    if(sret < 0){
501
465
      int e = errno;
502
466
      if(errno != EINTR and not feof(stdin)){
503
 
        error_plus(0, errno, "getline");
 
467
        error(0, errno, "getline");
504
468
        switch(e){
505
469
        case EBADF:
506
470
          status = EX_UNAVAILABLE;
507
 
          break;
508
471
        case EIO:
509
472
        case EINVAL:
510
473
        default:
514
477
        break;
515
478
      }
516
479
    }
517
 
    /* if(sret == 0), then the only sensible thing to do is to retry
518
 
       to read from stdin */
 
480
    /* if(sret == 0), then the only sensible thing to do is to retry to
 
481
       read from stdin */
519
482
    fputc('\n', stderr);
520
483
    if(debug and not quit_now){
521
484
      /* If quit_now is nonzero, we were interrupted by a signal, and
530
493
    fprintf(stderr, "Restoring terminal attributes\n");
531
494
  }
532
495
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
533
 
    error_plus(0, errno, "tcsetattr+echo");
 
496
    error(0, errno, "tcsetattr+echo");
534
497
  }
535
498
  
536
499
  if(quit_now){
538
501
    old_action.sa_handler = SIG_DFL;
539
502
    ret = sigaction(signal_received, &old_action, NULL);
540
503
    if(ret == -1){
541
 
      error_plus(0, errno, "sigaction");
 
504
      error(0, errno, "sigaction");
542
505
    }
543
506
    raise(signal_received);
544
507
  }