/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-06-19 20:25:38 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110619202538-0js072v8fso12u07
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.

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