/mandos/release

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2012-06-22 23:33:56 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 303.
  • Revision ID: teddy@recompile.se-20120622233356-odoqqt2ki2gssn37
* Makefile (check): Also check mandos-ctl.
* mandos-ctl: All options taking a time interval argument can now take
              an RFC 3339 duration.
  (rfc3339_duration_to_delta): New function.
  (string_to_delta): Try rfc3339_duration_to_delta first.
  (main): New "--check" option.
* mandos-ctl.xml (SYNOPSIS, OPTIONS): Document new "--check" option.

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-2012 Teddy Hogeborn
 
6
 * Copyright © 2008-2012 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(), vfprintf(), vasprintf()
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, strerror() */
 
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,
66
67
int signal_received;
67
68
bool debug = false;
68
69
const char *argp_program_version = "password-prompt " VERSION;
69
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
70
const char *argp_program_bug_address = "<mandos@recompile.se>";
70
71
 
71
72
/* Needed for conflict resolution */
72
73
const char plymouth_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
 
74
85
/* Function to use when printing errors */
75
 
void error_plus(int status, int errnum, const char *formatstring, ...){
 
86
__attribute__((format (gnu_printf, 3, 4)))
 
87
void error_plus(int status, int errnum, const char *formatstring,
 
88
                ...){
76
89
  va_list ap;
77
90
  char *text;
78
91
  int ret;
80
93
  va_start(ap, formatstring);
81
94
  ret = vasprintf(&text, formatstring, ap);
82
95
  if (ret == -1){
83
 
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
 
96
    fprintf(stderr, "Mandos plugin %s: ",
 
97
            program_invocation_short_name);
84
98
    vfprintf(stderr, formatstring, ap);
85
 
    fprintf(stderr, ": ");
86
 
    fprintf(stderr, "%s\n", strerror(errnum));
 
99
    fprintf(stderr, ": %s\n", strerror(errnum));
87
100
    error(status, errno, "vasprintf while printing error");
88
101
    return;
89
102
  }
106
119
     from the terminal.  Password-prompt will exit if it detects
107
120
     plymouth since plymouth performs the same functionality.
108
121
   */
 
122
  __attribute__((nonnull))
109
123
  int is_plymouth(const struct dirent *proc_entry){
110
124
    int ret;
111
125
    int cl_fd;
112
126
    {
113
 
      uintmax_t maxvalue;
 
127
      uintmax_t proc_id;
114
128
      char *tmp;
115
129
      errno = 0;
116
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
130
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
117
131
      
118
132
      if(errno != 0 or *tmp != '\0'
119
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
133
         or proc_id != (uintmax_t)((pid_t)proc_id)){
120
134
        return 0;
121
135
      }
122
136
    }
125
139
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
126
140
                   proc_entry->d_name);
127
141
    if(ret == -1){
128
 
      error(0, errno, "asprintf");
 
142
      error_plus(0, errno, "asprintf");
129
143
      return 0;
130
144
    }
131
145
    
134
148
    free(cmdline_filename);
135
149
    if(cl_fd == -1){
136
150
      if(errno != ENOENT){
137
 
        error(0, errno, "open");
 
151
        error_plus(0, errno, "open");
138
152
      }
139
153
      return 0;
140
154
    }
151
165
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
152
166
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
153
167
          if(tmp == NULL){
154
 
            error(0, errno, "realloc");
 
168
            error_plus(0, errno, "realloc");
155
169
            free(cmdline);
156
170
            close(cl_fd);
157
171
            return 0;
164
178
        sret = read(cl_fd, cmdline + cmdline_len,
165
179
                    cmdline_allocated - cmdline_len);
166
180
        if(sret == -1){
167
 
          error(0, errno, "read");
 
181
          error_plus(0, errno, "read");
168
182
          free(cmdline);
169
183
          close(cl_fd);
170
184
          return 0;
173
187
      } while(sret != 0);
174
188
      ret = close(cl_fd);
175
189
      if(ret == -1){
176
 
        error(0, errno, "close");
 
190
        error_plus(0, errno, "close");
177
191
        free(cmdline);
178
192
        return 0;
179
193
      }
205
219
    return 1;
206
220
  }
207
221
  
208
 
  struct dirent **direntries;
 
222
  struct dirent **direntries = NULL;
209
223
  int ret;
210
224
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
211
225
  if (ret == -1){
212
 
    error(1, errno, "scandir");
 
226
    error_plus(1, errno, "scandir");
213
227
  }
 
228
  free(direntries);
214
229
  return ret > 0;
215
230
}
216
231
 
245
260
      { .name = NULL }
246
261
    };
247
262
    
 
263
    __attribute__((nonnull(3)))
248
264
    error_t parse_opt (int key, char *arg, struct argp_state *state){
249
265
      errno = 0;
250
266
      switch (key){
286
302
    case ENOMEM:
287
303
    default:
288
304
      errno = ret;
289
 
      error(0, errno, "argp_parse");
 
305
      error_plus(0, errno, "argp_parse");
290
306
      return EX_OSERR;
291
307
    case EINVAL:
292
308
      return EX_USAGE;
310
326
  
311
327
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
312
328
    int e = errno;
313
 
    error(0, errno, "tcgetattr");
 
329
    error_plus(0, errno, "tcgetattr");
314
330
    switch(e){
315
331
    case EBADF:
316
332
    case ENOTTY:
323
339
  sigemptyset(&new_action.sa_mask);
324
340
  ret = sigaddset(&new_action.sa_mask, SIGINT);
325
341
  if(ret == -1){
326
 
    error(0, errno, "sigaddset");
 
342
    error_plus(0, errno, "sigaddset");
327
343
    return EX_OSERR;
328
344
  }
329
345
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
330
346
  if(ret == -1){
331
 
    error(0, errno, "sigaddset");
 
347
    error_plus(0, errno, "sigaddset");
332
348
    return EX_OSERR;
333
349
  }
334
350
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
335
351
  if(ret == -1){
336
 
    error(0, errno, "sigaddset");
 
352
    error_plus(0, errno, "sigaddset");
337
353
    return EX_OSERR;
338
354
  }
339
355
  /* Need to check if the handler is SIG_IGN before handling:
342
358
  */
343
359
  ret = sigaction(SIGINT, NULL, &old_action);
344
360
  if(ret == -1){
345
 
    error(0, errno, "sigaction");
 
361
    error_plus(0, errno, "sigaction");
346
362
    return EX_OSERR;
347
363
  }
348
364
  if(old_action.sa_handler != SIG_IGN){
349
365
    ret = sigaction(SIGINT, &new_action, NULL);
350
366
    if(ret == -1){
351
 
      error(0, errno, "sigaction");
 
367
      error_plus(0, errno, "sigaction");
352
368
      return EX_OSERR;
353
369
    }
354
370
  }
355
371
  ret = sigaction(SIGHUP, NULL, &old_action);
356
372
  if(ret == -1){
357
 
    error(0, errno, "sigaction");
 
373
    error_plus(0, errno, "sigaction");
358
374
    return EX_OSERR;
359
375
  }
360
376
  if(old_action.sa_handler != SIG_IGN){
361
377
    ret = sigaction(SIGHUP, &new_action, NULL);
362
378
    if(ret == -1){
363
 
      error(0, errno, "sigaction");
 
379
      error_plus(0, errno, "sigaction");
364
380
      return EX_OSERR;
365
381
    }
366
382
  }
367
383
  ret = sigaction(SIGTERM, NULL, &old_action);
368
384
  if(ret == -1){
369
 
    error(0, errno, "sigaction");
 
385
    error_plus(0, errno, "sigaction");
370
386
    return EX_OSERR;
371
387
  }
372
388
  if(old_action.sa_handler != SIG_IGN){
373
389
    ret = sigaction(SIGTERM, &new_action, NULL);
374
390
    if(ret == -1){
375
 
      error(0, errno, "sigaction");
 
391
      error_plus(0, errno, "sigaction");
376
392
      return EX_OSERR;
377
393
    }
378
394
  }
386
402
  t_new.c_lflag &= ~(tcflag_t)ECHO;
387
403
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
388
404
    int e = errno;
389
 
    error(0, errno, "tcsetattr-echo");
 
405
    error_plus(0, errno, "tcsetattr-echo");
390
406
    switch(e){
391
407
    case EBADF:
392
408
    case ENOTTY:
456
472
        sret = write(STDOUT_FILENO, buffer + written, n - written);
457
473
        if(sret < 0){
458
474
          int e = errno;
459
 
          error(0, errno, "write");
 
475
          error_plus(0, errno, "write");
460
476
          switch(e){
461
477
          case EBADF:
462
478
          case EFAULT:
478
494
      sret = close(STDOUT_FILENO);
479
495
      if(sret == -1){
480
496
        int e = errno;
481
 
        error(0, errno, "close");
 
497
        error_plus(0, errno, "close");
482
498
        switch(e){
483
499
        case EBADF:
484
500
          status = EX_OSFILE;
494
510
    if(sret < 0){
495
511
      int e = errno;
496
512
      if(errno != EINTR and not feof(stdin)){
497
 
        error(0, errno, "getline");
 
513
        error_plus(0, errno, "getline");
498
514
        switch(e){
499
515
        case EBADF:
500
516
          status = EX_UNAVAILABLE;
 
517
          break;
501
518
        case EIO:
502
519
        case EINVAL:
503
520
        default:
523
540
    fprintf(stderr, "Restoring terminal attributes\n");
524
541
  }
525
542
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
526
 
    error(0, errno, "tcsetattr+echo");
 
543
    error_plus(0, errno, "tcsetattr+echo");
527
544
  }
528
545
  
529
546
  if(quit_now){
531
548
    old_action.sa_handler = SIG_DFL;
532
549
    ret = sigaction(signal_received, &old_action, NULL);
533
550
    if(ret == -1){
534
 
      error(0, errno, "sigaction");
 
551
      error_plus(0, errno, "sigaction");
535
552
    }
536
553
    raise(signal_received);
537
554
  }