/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-07-16 00:29:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110716002919-r77yikuiulj42o40
* initramfs-tools-script: Abort if plugin-runner is missing.  Removed
                          workaround for Debian bug #633582; the
                          workaround required getopt, which can not be
                          guaranteed.
* plugin-runner.c (main): Work around Debian bug #633582.
* plugins.d/mandos-client.c (main): - '' -

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() */
67
67
int signal_received;
68
68
bool debug = false;
69
69
const char *argp_program_version = "password-prompt " VERSION;
70
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
71
 
72
72
/* Needed for conflict resolution */
73
73
const char plymouth_name[] = "plymouthd";
74
74
 
75
75
/* Function to use when printing errors */
76
 
__attribute__((format (gnu_printf, 3, 4)))
77
76
void error_plus(int status, int errnum, const char *formatstring,
78
77
                ...){
79
78
  va_list ap;
82
81
  
83
82
  va_start(ap, formatstring);
84
83
  ret = vasprintf(&text, formatstring, ap);
85
 
  if(ret == -1){
 
84
  if (ret == -1){
86
85
    fprintf(stderr, "Mandos plugin %s: ",
87
86
            program_invocation_short_name);
88
87
    vfprintf(stderr, formatstring, ap);
89
 
    fprintf(stderr, ": %s\n", strerror(errnum));
 
88
    fprintf(stderr, ": ");
 
89
    fprintf(stderr, "%s\n", strerror(errnum));
90
90
    error(status, errno, "vasprintf while printing error");
91
91
    return;
92
92
  }
109
109
     from the terminal.  Password-prompt will exit if it detects
110
110
     plymouth since plymouth performs the same functionality.
111
111
   */
112
 
  __attribute__((nonnull))
113
112
  int is_plymouth(const struct dirent *proc_entry){
114
113
    int ret;
115
114
    int cl_fd;
116
115
    {
117
 
      uintmax_t proc_id;
 
116
      uintmax_t maxvalue;
118
117
      char *tmp;
119
118
      errno = 0;
120
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
119
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
121
120
      
122
121
      if(errno != 0 or *tmp != '\0'
123
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
122
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
124
123
        return 0;
125
124
      }
126
125
    }
129
128
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
130
129
                   proc_entry->d_name);
131
130
    if(ret == -1){
132
 
      error_plus(0, errno, "asprintf");
 
131
      error(0, errno, "asprintf");
133
132
      return 0;
134
133
    }
135
134
    
138
137
    free(cmdline_filename);
139
138
    if(cl_fd == -1){
140
139
      if(errno != ENOENT){
141
 
        error_plus(0, errno, "open");
 
140
        error(0, errno, "open");
142
141
      }
143
142
      return 0;
144
143
    }
155
154
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
156
155
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
157
156
          if(tmp == NULL){
158
 
            error_plus(0, errno, "realloc");
 
157
            error(0, errno, "realloc");
159
158
            free(cmdline);
160
159
            close(cl_fd);
161
160
            return 0;
168
167
        sret = read(cl_fd, cmdline + cmdline_len,
169
168
                    cmdline_allocated - cmdline_len);
170
169
        if(sret == -1){
171
 
          error_plus(0, errno, "read");
 
170
          error(0, errno, "read");
172
171
          free(cmdline);
173
172
          close(cl_fd);
174
173
          return 0;
177
176
      } while(sret != 0);
178
177
      ret = close(cl_fd);
179
178
      if(ret == -1){
180
 
        error_plus(0, errno, "close");
 
179
        error(0, errno, "close");
181
180
        free(cmdline);
182
181
        return 0;
183
182
      }
209
208
    return 1;
210
209
  }
211
210
  
212
 
  struct dirent **direntries = NULL;
 
211
  struct dirent **direntries;
213
212
  int ret;
214
213
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
215
 
  if(ret == -1){
216
 
    error_plus(1, errno, "scandir");
 
214
  if (ret == -1){
 
215
    error(1, errno, "scandir");
217
216
  }
218
 
  free(direntries);
219
217
  return ret > 0;
220
218
}
221
219
 
250
248
      { .name = NULL }
251
249
    };
252
250
    
253
 
    __attribute__((nonnull(3)))
254
251
    error_t parse_opt (int key, char *arg, struct argp_state *state){
255
252
      errno = 0;
256
253
      switch (key){
292
289
    case ENOMEM:
293
290
    default:
294
291
      errno = ret;
295
 
      error_plus(0, errno, "argp_parse");
 
292
      error(0, errno, "argp_parse");
296
293
      return EX_OSERR;
297
294
    case EINVAL:
298
295
      return EX_USAGE;
303
300
    fprintf(stderr, "Starting %s\n", argv[0]);
304
301
  }
305
302
 
306
 
  if(conflict_detection()){
 
303
  if (conflict_detection()){
307
304
    if(debug){
308
305
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
309
306
    }
316
313
  
317
314
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
318
315
    int e = errno;
319
 
    error_plus(0, errno, "tcgetattr");
 
316
    error(0, errno, "tcgetattr");
320
317
    switch(e){
321
318
    case EBADF:
322
319
    case ENOTTY:
329
326
  sigemptyset(&new_action.sa_mask);
330
327
  ret = sigaddset(&new_action.sa_mask, SIGINT);
331
328
  if(ret == -1){
332
 
    error_plus(0, errno, "sigaddset");
 
329
    error(0, errno, "sigaddset");
333
330
    return EX_OSERR;
334
331
  }
335
332
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
336
333
  if(ret == -1){
337
 
    error_plus(0, errno, "sigaddset");
 
334
    error(0, errno, "sigaddset");
338
335
    return EX_OSERR;
339
336
  }
340
337
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
341
338
  if(ret == -1){
342
 
    error_plus(0, errno, "sigaddset");
 
339
    error(0, errno, "sigaddset");
343
340
    return EX_OSERR;
344
341
  }
345
342
  /* Need to check if the handler is SIG_IGN before handling:
348
345
  */
349
346
  ret = sigaction(SIGINT, NULL, &old_action);
350
347
  if(ret == -1){
351
 
    error_plus(0, errno, "sigaction");
 
348
    error(0, errno, "sigaction");
352
349
    return EX_OSERR;
353
350
  }
354
351
  if(old_action.sa_handler != SIG_IGN){
355
352
    ret = sigaction(SIGINT, &new_action, NULL);
356
353
    if(ret == -1){
357
 
      error_plus(0, errno, "sigaction");
 
354
      error(0, errno, "sigaction");
358
355
      return EX_OSERR;
359
356
    }
360
357
  }
361
358
  ret = sigaction(SIGHUP, NULL, &old_action);
362
359
  if(ret == -1){
363
 
    error_plus(0, errno, "sigaction");
 
360
    error(0, errno, "sigaction");
364
361
    return EX_OSERR;
365
362
  }
366
363
  if(old_action.sa_handler != SIG_IGN){
367
364
    ret = sigaction(SIGHUP, &new_action, NULL);
368
365
    if(ret == -1){
369
 
      error_plus(0, errno, "sigaction");
 
366
      error(0, errno, "sigaction");
370
367
      return EX_OSERR;
371
368
    }
372
369
  }
373
370
  ret = sigaction(SIGTERM, NULL, &old_action);
374
371
  if(ret == -1){
375
 
    error_plus(0, errno, "sigaction");
 
372
    error(0, errno, "sigaction");
376
373
    return EX_OSERR;
377
374
  }
378
375
  if(old_action.sa_handler != SIG_IGN){
379
376
    ret = sigaction(SIGTERM, &new_action, NULL);
380
377
    if(ret == -1){
381
 
      error_plus(0, errno, "sigaction");
 
378
      error(0, errno, "sigaction");
382
379
      return EX_OSERR;
383
380
    }
384
381
  }
392
389
  t_new.c_lflag &= ~(tcflag_t)ECHO;
393
390
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
394
391
    int e = errno;
395
 
    error_plus(0, errno, "tcsetattr-echo");
 
392
    error(0, errno, "tcsetattr-echo");
396
393
    switch(e){
397
394
    case EBADF:
398
395
    case ENOTTY:
462
459
        sret = write(STDOUT_FILENO, buffer + written, n - written);
463
460
        if(sret < 0){
464
461
          int e = errno;
465
 
          error_plus(0, errno, "write");
 
462
          error(0, errno, "write");
466
463
          switch(e){
467
464
          case EBADF:
468
465
          case EFAULT:
484
481
      sret = close(STDOUT_FILENO);
485
482
      if(sret == -1){
486
483
        int e = errno;
487
 
        error_plus(0, errno, "close");
 
484
        error(0, errno, "close");
488
485
        switch(e){
489
486
        case EBADF:
490
487
          status = EX_OSFILE;
500
497
    if(sret < 0){
501
498
      int e = errno;
502
499
      if(errno != EINTR and not feof(stdin)){
503
 
        error_plus(0, errno, "getline");
 
500
        error(0, errno, "getline");
504
501
        switch(e){
505
502
        case EBADF:
506
503
          status = EX_UNAVAILABLE;
507
 
          break;
508
504
        case EIO:
509
505
        case EINVAL:
510
506
        default:
530
526
    fprintf(stderr, "Restoring terminal attributes\n");
531
527
  }
532
528
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
533
 
    error_plus(0, errno, "tcsetattr+echo");
 
529
    error(0, errno, "tcsetattr+echo");
534
530
  }
535
531
  
536
532
  if(quit_now){
538
534
    old_action.sa_handler = SIG_DFL;
539
535
    ret = sigaction(signal_received, &old_action, NULL);
540
536
    if(ret == -1){
541
 
      error_plus(0, errno, "sigaction");
 
537
      error(0, errno, "sigaction");
542
538
    }
543
539
    raise(signal_received);
544
540
  }