/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: Björn Påhlsson
  • Date: 2011-12-31 22:44:06 UTC
  • Revision ID: belorn@recompile.se-20111231224406-b1w5a1p3639aymxe
fixed bug with bad stored config state for expires and last_checked_ok.

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-2011 Teddy Hogeborn
 
6
 * Copyright © 2008-2011 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 <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* getline(), asprintf() */
41
41
                                   getenv(), free() */
42
42
#include <dirent.h>             /* scandir(), alphasort() */
43
43
#include <stdio.h>              /* fprintf(), stderr, getline(),
44
 
                                   stdin, feof(), fputc()
45
 
                                */
 
44
                                   stdin, feof(), fputc(), vfprintf(),
 
45
                                   vasprintf() */
46
46
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
47
47
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
48
48
                                */
51
51
#include <stdbool.h>            /* bool, false, true */
52
52
#include <inttypes.h>           /* strtoumax() */
53
53
#include <sys/stat.h>           /* struct stat, lstat(), open() */
54
 
#include <string.h>             /* strlen, rindex, memcmp */
 
54
#include <string.h>             /* strlen, rindex, memcmp, strerror()
 
55
                                 */
55
56
#include <argp.h>               /* struct argp_option, struct
56
57
                                   argp_state, struct argp,
57
58
                                   argp_parse(), error_t,
60
61
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
61
62
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
62
63
#include <fcntl.h>              /* open() */
 
64
#include <stdarg.h>             /* va_list, va_start(), ... */
63
65
 
64
66
volatile sig_atomic_t quit_now = 0;
65
67
int signal_received;
66
68
bool debug = false;
67
69
const char *argp_program_version = "password-prompt " VERSION;
68
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
70
const char *argp_program_bug_address = "<mandos@recompile.se>";
69
71
 
70
72
/* Needed for conflict resolution */
71
73
const char plymouth_name[] = "plymouthd";
72
 
const char plymouth_alt_name[] = "plymouthd";
73
 
 
 
74
 
 
75
__attribute__((format (gnu_printf, 2, 3), nonnull(1)))
 
76
int fprintf_plus(FILE *stream, const char *format, ...){
 
77
  va_list ap;
 
78
  va_start (ap, format);
 
79
  
 
80
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
 
81
                             program_invocation_short_name));
 
82
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
83
}
 
84
 
 
85
/* Function to use when printing errors */
 
86
__attribute__((format (gnu_printf, 3, 4)))
 
87
void error_plus(int status, int errnum, const char *formatstring,
 
88
                ...){
 
89
  va_list ap;
 
90
  char *text;
 
91
  int ret;
 
92
  
 
93
  va_start(ap, formatstring);
 
94
  ret = vasprintf(&text, formatstring, ap);
 
95
  if (ret == -1){
 
96
    fprintf(stderr, "Mandos plugin %s: ",
 
97
            program_invocation_short_name);
 
98
    vfprintf(stderr, formatstring, ap);
 
99
    fprintf(stderr, ": %s\n", strerror(errnum));
 
100
    error(status, errno, "vasprintf while printing error");
 
101
    return;
 
102
  }
 
103
  fprintf(stderr, "Mandos plugin ");
 
104
  error(status, errnum, "%s", text);
 
105
  free(text);
 
106
}
74
107
 
75
108
static void termination_handler(int signum){
76
109
  if(quit_now){
86
119
     from the terminal.  Password-prompt will exit if it detects
87
120
     plymouth since plymouth performs the same functionality.
88
121
   */
 
122
  __attribute__((nonnull))
89
123
  int is_plymouth(const struct dirent *proc_entry){
90
124
    int ret;
91
125
    int cl_fd;
92
126
    {
93
 
      uintmax_t maxvalue;
 
127
      uintmax_t proc_id;
94
128
      char *tmp;
95
129
      errno = 0;
96
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
130
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
97
131
      
98
132
      if(errno != 0 or *tmp != '\0'
99
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
133
         or proc_id != (uintmax_t)((pid_t)proc_id)){
100
134
        return 0;
101
135
      }
102
136
    }
105
139
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
106
140
                   proc_entry->d_name);
107
141
    if(ret == -1){
108
 
      error(0, errno, "asprintf");
 
142
      error_plus(0, errno, "asprintf");
109
143
      return 0;
110
144
    }
111
145
    
113
147
    cl_fd = open(cmdline_filename, O_RDONLY);
114
148
    free(cmdline_filename);
115
149
    if(cl_fd == -1){
116
 
      error(0, errno, "open");
 
150
      if(errno != ENOENT){
 
151
        error_plus(0, errno, "open");
 
152
      }
117
153
      return 0;
118
154
    }
119
155
    
129
165
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
130
166
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
131
167
          if(tmp == NULL){
132
 
            error(0, errno, "realloc");
 
168
            error_plus(0, errno, "realloc");
133
169
            free(cmdline);
134
170
            close(cl_fd);
135
171
            return 0;
142
178
        sret = read(cl_fd, cmdline + cmdline_len,
143
179
                    cmdline_allocated - cmdline_len);
144
180
        if(sret == -1){
145
 
          error(0, errno, "read");
 
181
          error_plus(0, errno, "read");
146
182
          free(cmdline);
147
183
          close(cl_fd);
148
184
          return 0;
151
187
      } while(sret != 0);
152
188
      ret = close(cl_fd);
153
189
      if(ret == -1){
154
 
        error(0, errno, "close");
 
190
        error_plus(0, errno, "close");
155
191
        free(cmdline);
156
192
        return 0;
157
193
      }
167
203
      cmdline_base = cmdline;
168
204
    }
169
205
    
170
 
    if((strcmp(cmdline_base, plymouth_name) != 0)
171
 
       and (strcmp(cmdline_base, plymouth_alt_name) != 0)){
 
206
    if(strcmp(cmdline_base, plymouth_name) != 0){
172
207
      if(debug){
173
 
        fprintf(stderr, "\"%s\" is not \"%s\" or \"%s\"\n",
174
 
                cmdline_base, plymouth_name, plymouth_alt_name);
 
208
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
 
209
                plymouth_name);
175
210
      }
176
211
      free(cmdline);
177
212
      return 0;
178
213
    }
179
 
    fprintf(stderr, "\"%s\" equals \"%s\" or \"%s\"\n",
180
 
            cmdline_base, plymouth_name, plymouth_alt_name);
 
214
    if(debug){
 
215
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
 
216
              plymouth_name);
 
217
    }
181
218
    free(cmdline);
182
219
    return 1;
183
220
  }
184
221
  
185
 
  struct dirent **direntries;
 
222
  struct dirent **direntries = NULL;
186
223
  int ret;
187
224
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
188
225
  if (ret == -1){
189
 
    error(1, errno, "scandir");
 
226
    error_plus(1, errno, "scandir");
190
227
  }
 
228
  free(direntries);
191
229
  return ret > 0;
192
230
}
193
231
 
222
260
      { .name = NULL }
223
261
    };
224
262
    
 
263
    __attribute__((nonnull(3)))
225
264
    error_t parse_opt (int key, char *arg, struct argp_state *state){
226
265
      errno = 0;
227
266
      switch (key){
263
302
    case ENOMEM:
264
303
    default:
265
304
      errno = ret;
266
 
      error(0, errno, "argp_parse");
 
305
      error_plus(0, errno, "argp_parse");
267
306
      return EX_OSERR;
268
307
    case EINVAL:
269
308
      return EX_USAGE;
287
326
  
288
327
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
289
328
    int e = errno;
290
 
    error(0, errno, "tcgetattr");
 
329
    error_plus(0, errno, "tcgetattr");
291
330
    switch(e){
292
331
    case EBADF:
293
332
    case ENOTTY:
300
339
  sigemptyset(&new_action.sa_mask);
301
340
  ret = sigaddset(&new_action.sa_mask, SIGINT);
302
341
  if(ret == -1){
303
 
    error(0, errno, "sigaddset");
 
342
    error_plus(0, errno, "sigaddset");
304
343
    return EX_OSERR;
305
344
  }
306
345
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
307
346
  if(ret == -1){
308
 
    error(0, errno, "sigaddset");
 
347
    error_plus(0, errno, "sigaddset");
309
348
    return EX_OSERR;
310
349
  }
311
350
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
312
351
  if(ret == -1){
313
 
    error(0, errno, "sigaddset");
 
352
    error_plus(0, errno, "sigaddset");
314
353
    return EX_OSERR;
315
354
  }
316
355
  /* Need to check if the handler is SIG_IGN before handling:
319
358
  */
320
359
  ret = sigaction(SIGINT, NULL, &old_action);
321
360
  if(ret == -1){
322
 
    error(0, errno, "sigaction");
 
361
    error_plus(0, errno, "sigaction");
323
362
    return EX_OSERR;
324
363
  }
325
364
  if(old_action.sa_handler != SIG_IGN){
326
365
    ret = sigaction(SIGINT, &new_action, NULL);
327
366
    if(ret == -1){
328
 
      error(0, errno, "sigaction");
 
367
      error_plus(0, errno, "sigaction");
329
368
      return EX_OSERR;
330
369
    }
331
370
  }
332
371
  ret = sigaction(SIGHUP, NULL, &old_action);
333
372
  if(ret == -1){
334
 
    error(0, errno, "sigaction");
 
373
    error_plus(0, errno, "sigaction");
335
374
    return EX_OSERR;
336
375
  }
337
376
  if(old_action.sa_handler != SIG_IGN){
338
377
    ret = sigaction(SIGHUP, &new_action, NULL);
339
378
    if(ret == -1){
340
 
      error(0, errno, "sigaction");
 
379
      error_plus(0, errno, "sigaction");
341
380
      return EX_OSERR;
342
381
    }
343
382
  }
344
383
  ret = sigaction(SIGTERM, NULL, &old_action);
345
384
  if(ret == -1){
346
 
    error(0, errno, "sigaction");
 
385
    error_plus(0, errno, "sigaction");
347
386
    return EX_OSERR;
348
387
  }
349
388
  if(old_action.sa_handler != SIG_IGN){
350
389
    ret = sigaction(SIGTERM, &new_action, NULL);
351
390
    if(ret == -1){
352
 
      error(0, errno, "sigaction");
 
391
      error_plus(0, errno, "sigaction");
353
392
      return EX_OSERR;
354
393
    }
355
394
  }
363
402
  t_new.c_lflag &= ~(tcflag_t)ECHO;
364
403
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
365
404
    int e = errno;
366
 
    error(0, errno, "tcsetattr-echo");
 
405
    error_plus(0, errno, "tcsetattr-echo");
367
406
    switch(e){
368
407
    case EBADF:
369
408
    case ENOTTY:
433
472
        sret = write(STDOUT_FILENO, buffer + written, n - written);
434
473
        if(sret < 0){
435
474
          int e = errno;
436
 
          error(0, errno, "write");
 
475
          error_plus(0, errno, "write");
437
476
          switch(e){
438
477
          case EBADF:
439
478
          case EFAULT:
455
494
      sret = close(STDOUT_FILENO);
456
495
      if(sret == -1){
457
496
        int e = errno;
458
 
        error(0, errno, "close");
 
497
        error_plus(0, errno, "close");
459
498
        switch(e){
460
499
        case EBADF:
461
500
          status = EX_OSFILE;
471
510
    if(sret < 0){
472
511
      int e = errno;
473
512
      if(errno != EINTR and not feof(stdin)){
474
 
        error(0, errno, "getline");
 
513
        error_plus(0, errno, "getline");
475
514
        switch(e){
476
515
        case EBADF:
477
516
          status = EX_UNAVAILABLE;
 
517
          break;
478
518
        case EIO:
479
519
        case EINVAL:
480
520
        default:
500
540
    fprintf(stderr, "Restoring terminal attributes\n");
501
541
  }
502
542
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
503
 
    error(0, errno, "tcsetattr+echo");
 
543
    error_plus(0, errno, "tcsetattr+echo");
504
544
  }
505
545
  
506
546
  if(quit_now){
508
548
    old_action.sa_handler = SIG_DFL;
509
549
    ret = sigaction(signal_received, &old_action, NULL);
510
550
    if(ret == -1){
511
 
      error(0, errno, "sigaction");
 
551
      error_plus(0, errno, "sigaction");
512
552
    }
513
553
    raise(signal_received);
514
554
  }