/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 plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2014-03-10 07:04:09 UTC
  • Revision ID: teddy@recompile.se-20140310070409-ho6p1wrnpopi2ny1
White space fix: change "if (" to "if(" in C code.

* plugins.d/askpass-fifo.c (error_plus): White space fix.
* plugins.d/password-prompt.c (error_plus, conflict_detection,
                               main): - '' -
* plugins.d/plymouth.c (error_plus, exec_and_wait, get_pid): - '' -
* plugins.d/splashy.c (error_plus): - '' -
* plugins.d/usplash.c (error_plus): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2013 Teddy Hogeborn
 
6
 * Copyright © 2008-2013 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             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf() */
 
26
                                   asprintf(), O_CLOEXEC */
27
27
#include <stddef.h>             /* size_t, NULL */
28
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
 
                                   EXIT_SUCCESS, realloc() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
 
29
                                   realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
31
#include <stdio.h>              /* fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
42
42
                                   WCOREDUMP() */
43
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
44
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, opendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
46
                                   readdir(), closedir(), dirfd() */
47
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
48
                                   fcntl(), setuid(), setgid(),
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
56
56
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
57
                                   strsignal(), strcmp(), strncmp() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
68
68
                                */
69
69
#include <errno.h>              /* errno, EBADF */
70
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
 
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
 
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
 
73
#include <errno.h>              /* errno */
 
74
#include <error.h>              /* error() */
71
75
 
72
76
#define BUFFER_SIZE 256
73
77
 
75
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
76
80
 
77
81
const char *argp_program_version = "plugin-runner " VERSION;
78
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
82
const char *argp_program_bug_address = "<mandos@recompile.se>";
79
83
 
80
84
typedef struct plugin{
81
85
  char *name;                   /* can be NULL or any plugin name */
102
106
/* Gets an existing plugin based on name,
103
107
   or if none is found, creates a new one */
104
108
static plugin *getplugin(char *name){
105
 
  /* Check for exiting plugin with that name */
 
109
  /* Check for existing plugin with that name */
106
110
  for(plugin *p = plugin_list; p != NULL; p = p->next){
107
111
    if((p->name == name)
108
112
       or (p->name and name and (strcmp(p->name, name) == 0))){
123
127
      copy_name = strdup(name);
124
128
    } while(copy_name == NULL and errno == EINTR);
125
129
    if(copy_name == NULL){
 
130
      int e = errno;
126
131
      free(new_plugin);
 
132
      errno = e;
127
133
      return NULL;
128
134
    }
129
135
  }
137
143
    new_plugin->argv = malloc(sizeof(char *) * 2);
138
144
  } while(new_plugin->argv == NULL and errno == EINTR);
139
145
  if(new_plugin->argv == NULL){
 
146
    int e = errno;
140
147
    free(copy_name);
141
148
    free(new_plugin);
 
149
    errno = e;
142
150
    return NULL;
143
151
  }
144
152
  new_plugin->argv[0] = copy_name;
148
156
    new_plugin->environ = malloc(sizeof(char *));
149
157
  } while(new_plugin->environ == NULL and errno == EINTR);
150
158
  if(new_plugin->environ == NULL){
 
159
    int e = errno;
151
160
    free(copy_name);
152
161
    free(new_plugin->argv);
153
162
    free(new_plugin);
 
163
    errno = e;
154
164
    return NULL;
155
165
  }
156
166
  new_plugin->environ[0] = NULL;
161
171
}
162
172
 
163
173
/* Helper function for add_argument and add_environment */
 
174
__attribute__((nonnull))
164
175
static bool add_to_char_array(const char *new, char ***array,
165
176
                              int *len){
166
177
  /* Resize the pointed-to array to hold one more pointer */
 
178
  char **new_array = NULL;
167
179
  do {
168
 
    *array = realloc(*array, sizeof(char *)
169
 
                     * (size_t) ((*len) + 2));
170
 
  } while(*array == NULL and errno == EINTR);
 
180
    new_array = realloc(*array, sizeof(char *)
 
181
                        * (size_t) ((*len) + 2));
 
182
  } while(new_array == NULL and errno == EINTR);
171
183
  /* Malloc check */
172
 
  if(*array == NULL){
 
184
  if(new_array == NULL){
173
185
    return false;
174
186
  }
 
187
  *array = new_array;
175
188
  /* Make a copy of the new string */
176
189
  char *copy;
177
190
  do {
189
202
}
190
203
 
191
204
/* Add to a plugin's argument vector */
 
205
__attribute__((nonnull(2)))
192
206
static bool add_argument(plugin *p, const char *arg){
193
207
  if(p == NULL){
194
208
    return false;
197
211
}
198
212
 
199
213
/* Add to a plugin's environment */
 
214
__attribute__((nonnull(2)))
200
215
static bool add_environment(plugin *p, const char *def, bool replace){
201
216
  if(p == NULL){
202
217
    return false;
204
219
  /* namelen = length of name of environment variable */
205
220
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
206
221
  /* Search for this environment variable */
207
 
  for(char **e = p->environ; *e != NULL; e++){
208
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
222
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
223
    if(strncmp(*envdef, def, namelen + 1) == 0){
209
224
      /* It already exists */
210
225
      if(replace){
211
 
        char *new;
 
226
        char *new_envdef;
212
227
        do {
213
 
          new = realloc(*e, strlen(def) + 1);
214
 
        } while(new == NULL and errno == EINTR);
215
 
        if(new == NULL){
 
228
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
229
        } while(new_envdef == NULL and errno == EINTR);
 
230
        if(new_envdef == NULL){
216
231
          return false;
217
232
        }
218
 
        *e = new;
219
 
        strcpy(*e, def);
 
233
        *envdef = new_envdef;
 
234
        strcpy(*envdef, def);
220
235
      }
221
236
      return true;
222
237
    }
258
273
        /* No child processes */
259
274
        break;
260
275
      }
261
 
      perror("waitpid");
 
276
      error(0, errno, "waitpid");
262
277
    }
263
278
    
264
279
    /* A child exited, find it in process_list */
276
291
}
277
292
 
278
293
/* Prints out a password to stdout */
 
294
__attribute__((nonnull))
279
295
static bool print_out_password(const char *buffer, size_t length){
280
296
  ssize_t ret;
281
297
  for(size_t written = 0; written < length; written += (size_t)ret){
289
305
}
290
306
 
291
307
/* Removes and free a plugin from the plugin list */
 
308
__attribute__((nonnull))
292
309
static void free_plugin(plugin *plugin_node){
293
310
  
294
311
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
349
366
  sigemptyset(&sigchld_action.sa_mask);
350
367
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
351
368
  if(ret == -1){
352
 
    perror("sigaddset");
353
 
    exitstatus = EXIT_FAILURE;
 
369
    error(0, errno, "sigaddset");
 
370
    exitstatus = EX_OSERR;
354
371
    goto fallback;
355
372
  }
356
373
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
357
374
  if(ret == -1){
358
 
    perror("sigaction");
359
 
    exitstatus = EXIT_FAILURE;
 
375
    error(0, errno, "sigaction");
 
376
    exitstatus = EX_OSERR;
360
377
    goto fallback;
361
378
  }
362
379
  
394
411
      .doc = "Group ID the plugins will run as", .group = 3 },
395
412
    { .name = "debug", .key = 132,
396
413
      .doc = "Debug mode", .group = 4 },
 
414
    /*
 
415
     * These reproduce what we would get without ARGP_NO_HELP
 
416
     */
 
417
    { .name = "help", .key = '?',
 
418
      .doc = "Give this help list", .group = -1 },
 
419
    { .name = "usage", .key = -3,
 
420
      .doc = "Give a short usage message", .group = -1 },
 
421
    { .name = "version", .key = 'V',
 
422
      .doc = "Print program version", .group = -1 },
397
423
    { .name = NULL }
398
424
  };
399
425
  
400
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
401
 
                    struct argp_state *state){
 
426
  __attribute__((nonnull(3)))
 
427
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
428
    errno = 0;
402
429
    switch(key){
403
430
      char *tmp;
404
 
      intmax_t tmpmax;
 
431
      intmax_t tmp_id;
405
432
    case 'g':                   /* --global-options */
406
 
      if(arg != NULL){
 
433
      {
407
434
        char *plugin_option;
408
435
        while((plugin_option = strsep(&arg, ",")) != NULL){
409
 
          if(plugin_option[0] == '\0'){
410
 
            continue;
411
 
          }
412
436
          if(not add_argument(getplugin(NULL), plugin_option)){
413
 
            perror("add_argument");
414
 
            return ARGP_ERR_UNKNOWN;
 
437
            break;
415
438
          }
416
439
        }
417
440
      }
418
441
      break;
419
442
    case 'G':                   /* --global-env */
420
 
      if(arg == NULL){
421
 
        break;
422
 
      }
423
 
      if(not add_environment(getplugin(NULL), arg, true)){
424
 
        perror("add_environment");
425
 
      }
 
443
      add_environment(getplugin(NULL), arg, true);
426
444
      break;
427
445
    case 'o':                   /* --options-for */
428
 
      if(arg != NULL){
429
 
        char *plugin_name = strsep(&arg, ":");
430
 
        if(plugin_name[0] == '\0'){
431
 
          break;
432
 
        }
433
 
        char *plugin_option;
434
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
435
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
436
 
            perror("add_argument");
437
 
            return ARGP_ERR_UNKNOWN;
 
446
      {
 
447
        char *option_list = strchr(arg, ':');
 
448
        if(option_list == NULL){
 
449
          argp_error(state, "No colon in \"%s\"", arg);
 
450
          errno = EINVAL;
 
451
          break;
 
452
        }
 
453
        *option_list = '\0';
 
454
        option_list++;
 
455
        if(arg[0] == '\0'){
 
456
          argp_error(state, "Empty plugin name");
 
457
          errno = EINVAL;
 
458
          break;
 
459
        }
 
460
        char *option;
 
461
        while((option = strsep(&option_list, ",")) != NULL){
 
462
          if(not add_argument(getplugin(arg), option)){
 
463
            break;
438
464
          }
439
465
        }
440
466
      }
441
467
      break;
442
468
    case 'E':                   /* --env-for */
443
 
      if(arg == NULL){
444
 
        break;
445
 
      }
446
469
      {
447
470
        char *envdef = strchr(arg, ':');
448
471
        if(envdef == NULL){
 
472
          argp_error(state, "No colon in \"%s\"", arg);
 
473
          errno = EINVAL;
449
474
          break;
450
475
        }
451
476
        *envdef = '\0';
452
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
453
 
          perror("add_environment");
 
477
        envdef++;
 
478
        if(arg[0] == '\0'){
 
479
          argp_error(state, "Empty plugin name");
 
480
          errno = EINVAL;
 
481
          break;
454
482
        }
 
483
        add_environment(getplugin(arg), envdef, true);
455
484
      }
456
485
      break;
457
486
    case 'd':                   /* --disable */
458
 
      if(arg != NULL){
 
487
      {
459
488
        plugin *p = getplugin(arg);
460
 
        if(p == NULL){
461
 
          return ARGP_ERR_UNKNOWN;
 
489
        if(p != NULL){
 
490
          p->disabled = true;
462
491
        }
463
 
        p->disabled = true;
464
492
      }
465
493
      break;
466
494
    case 'e':                   /* --enable */
467
 
      if(arg != NULL){
 
495
      {
468
496
        plugin *p = getplugin(arg);
469
 
        if(p == NULL){
470
 
          return ARGP_ERR_UNKNOWN;
 
497
        if(p != NULL){
 
498
          p->disabled = false;
471
499
        }
472
 
        p->disabled = false;
473
500
      }
474
501
      break;
475
502
    case 128:                   /* --plugin-dir */
476
503
      free(plugindir);
477
504
      plugindir = strdup(arg);
478
 
      if(plugindir == NULL){
479
 
        perror("strdup");
480
 
      }
481
505
      break;
482
506
    case 129:                   /* --config-file */
483
507
      /* This is already done by parse_opt_config_file() */
484
508
      break;
485
509
    case 130:                   /* --userid */
486
 
      errno = 0;
487
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
510
      tmp_id = strtoimax(arg, &tmp, 10);
488
511
      if(errno != 0 or tmp == arg or *tmp != '\0'
489
 
         or tmpmax != (uid_t)tmpmax){
490
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
491
 
                PRIdMAX "\n", arg, (intmax_t)uid);
492
 
      } else {
493
 
        uid = (uid_t)tmpmax;
 
512
         or tmp_id != (uid_t)tmp_id){
 
513
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
514
                   PRIdMAX, arg, (intmax_t)uid);
 
515
        break;
494
516
      }
 
517
      uid = (uid_t)tmp_id;
495
518
      break;
496
519
    case 131:                   /* --groupid */
497
 
      errno = 0;
498
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
520
      tmp_id = strtoimax(arg, &tmp, 10);
499
521
      if(errno != 0 or tmp == arg or *tmp != '\0'
500
 
         or tmpmax != (gid_t)tmpmax){
501
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
502
 
                PRIdMAX "\n", arg, (intmax_t)gid);
503
 
      } else {
504
 
        gid = (gid_t)tmpmax;
 
522
         or tmp_id != (gid_t)tmp_id){
 
523
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
524
                   PRIdMAX, arg, (intmax_t)gid);
 
525
        break;
505
526
      }
 
527
      gid = (gid_t)tmp_id;
506
528
      break;
507
529
    case 132:                   /* --debug */
508
530
      debug = true;
509
531
      break;
 
532
      /*
 
533
       * These reproduce what we would get without ARGP_NO_HELP
 
534
       */
 
535
    case '?':                   /* --help */
 
536
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
537
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
538
    case -3:                    /* --usage */
 
539
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
540
      argp_state_help(state, state->out_stream,
 
541
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
542
    case 'V':                   /* --version */
 
543
      fprintf(state->out_stream, "%s\n", argp_program_version);
 
544
      exit(EXIT_SUCCESS);
 
545
      break;
510
546
/*
511
547
 * When adding more options before this line, remember to also add a
512
548
 * "case" to the "parse_opt_config_file" function below.
515
551
      /* Cryptsetup always passes an argument, which is an empty
516
552
         string if "none" was specified in /etc/crypttab.  So if
517
553
         argument was empty, we ignore it silently. */
518
 
      if(arg[0] != '\0'){
519
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
554
      if(arg[0] == '\0'){
 
555
        break;
520
556
      }
521
 
      break;
522
 
    case ARGP_KEY_END:
523
 
      break;
524
557
    default:
525
558
      return ARGP_ERR_UNKNOWN;
526
559
    }
527
 
    return 0;
 
560
    return errno;               /* Set to 0 at start */
528
561
  }
529
562
  
530
563
  /* This option parser is the same as parse_opt() above, except it
532
565
  error_t parse_opt_config_file(int key, char *arg,
533
566
                                __attribute__((unused))
534
567
                                struct argp_state *state){
 
568
    errno = 0;
535
569
    switch(key){
536
570
    case 'g':                   /* --global-options */
537
571
    case 'G':                   /* --global-env */
544
578
    case 129:                   /* --config-file */
545
579
      free(argfile);
546
580
      argfile = strdup(arg);
547
 
      if(argfile == NULL){
548
 
        perror("strdup");
549
 
      }
550
581
      break;
551
582
    case 130:                   /* --userid */
552
583
    case 131:                   /* --groupid */
553
584
    case 132:                   /* --debug */
 
585
    case '?':                   /* --help */
 
586
    case -3:                    /* --usage */
 
587
    case 'V':                   /* --version */
554
588
    case ARGP_KEY_ARG:
555
 
    case ARGP_KEY_END:
556
589
      break;
557
590
    default:
558
591
      return ARGP_ERR_UNKNOWN;
559
592
    }
560
 
    return 0;
 
593
    return errno;
561
594
  }
562
595
  
563
596
  struct argp argp = { .options = options,
567
600
  
568
601
  /* Parse using parse_opt_config_file() in order to get the custom
569
602
     config file location, if any. */
570
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
571
 
  if(ret == ARGP_ERR_UNKNOWN){
572
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
573
 
    exitstatus = EXIT_FAILURE;
 
603
  ret = argp_parse(&argp, argc, argv,
 
604
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
605
                   NULL, NULL);
 
606
  switch(ret){
 
607
  case 0:
 
608
    break;
 
609
  case ENOMEM:
 
610
  default:
 
611
    errno = ret;
 
612
    error(0, errno, "argp_parse");
 
613
    exitstatus = EX_OSERR;
 
614
    goto fallback;
 
615
  case EINVAL:
 
616
    exitstatus = EX_USAGE;
574
617
    goto fallback;
575
618
  }
576
619
  
593
636
    custom_argc = 1;
594
637
    custom_argv = malloc(sizeof(char*) * 2);
595
638
    if(custom_argv == NULL){
596
 
      perror("malloc");
597
 
      exitstatus = EXIT_FAILURE;
 
639
      error(0, errno, "malloc");
 
640
      exitstatus = EX_OSERR;
598
641
      goto fallback;
599
642
    }
600
643
    custom_argv[0] = argv[0];
616
659
        }
617
660
        new_arg = strdup(p);
618
661
        if(new_arg == NULL){
619
 
          perror("strdup");
620
 
          exitstatus = EXIT_FAILURE;
 
662
          error(0, errno, "strdup");
 
663
          exitstatus = EX_OSERR;
621
664
          free(org_line);
622
665
          goto fallback;
623
666
        }
624
667
        
625
668
        custom_argc += 1;
626
 
        custom_argv = realloc(custom_argv, sizeof(char *)
627
 
                              * ((unsigned int) custom_argc + 1));
628
 
        if(custom_argv == NULL){
629
 
          perror("realloc");
630
 
          exitstatus = EXIT_FAILURE;
631
 
          free(org_line);
632
 
          goto fallback;
 
669
        {
 
670
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
671
                                    * ((unsigned int)
 
672
                                       custom_argc + 1));
 
673
          if(new_argv == NULL){
 
674
            error(0, errno, "realloc");
 
675
            exitstatus = EX_OSERR;
 
676
            free(new_arg);
 
677
            free(org_line);
 
678
            goto fallback;
 
679
          } else {
 
680
            custom_argv = new_argv;
 
681
          }
633
682
        }
634
683
        custom_argv[custom_argc-1] = new_arg;
635
684
        custom_argv[custom_argc] = NULL;
639
688
      ret = fclose(conffp);
640
689
    } while(ret == EOF and errno == EINTR);
641
690
    if(ret == EOF){
642
 
      perror("fclose");
643
 
      exitstatus = EXIT_FAILURE;
 
691
      error(0, errno, "fclose");
 
692
      exitstatus = EX_IOERR;
644
693
      goto fallback;
645
694
    }
646
695
    free(org_line);
648
697
    /* Check for harmful errors and go to fallback. Other errors might
649
698
       not affect opening plugins */
650
699
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
651
 
      perror("fopen");
652
 
      exitstatus = EXIT_FAILURE;
 
700
      error(0, errno, "fopen");
 
701
      exitstatus = EX_OSERR;
653
702
      goto fallback;
654
703
    }
655
704
  }
656
 
  /* If there was any arguments from configuration file,
657
 
     pass them to parser as command arguments */
 
705
  /* If there were any arguments from the configuration file, pass
 
706
     them to parser as command line arguments */
658
707
  if(custom_argv != NULL){
659
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
660
 
                     0, NULL);
661
 
    if(ret == ARGP_ERR_UNKNOWN){
662
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
663
 
      exitstatus = EXIT_FAILURE;
 
708
    ret = argp_parse(&argp, custom_argc, custom_argv,
 
709
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
710
                     NULL, NULL);
 
711
    switch(ret){
 
712
    case 0:
 
713
      break;
 
714
    case ENOMEM:
 
715
    default:
 
716
      errno = ret;
 
717
      error(0, errno, "argp_parse");
 
718
      exitstatus = EX_OSERR;
 
719
      goto fallback;
 
720
    case EINVAL:
 
721
      exitstatus = EX_CONFIG;
664
722
      goto fallback;
665
723
    }
666
724
  }
667
725
  
668
726
  /* Parse actual command line arguments, to let them override the
669
727
     config file */
670
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
671
 
  if(ret == ARGP_ERR_UNKNOWN){
672
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
673
 
    exitstatus = EXIT_FAILURE;
 
728
  ret = argp_parse(&argp, argc, argv,
 
729
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
730
                   NULL, NULL);
 
731
  switch(ret){
 
732
  case 0:
 
733
    break;
 
734
  case ENOMEM:
 
735
  default:
 
736
    errno = ret;
 
737
    error(0, errno, "argp_parse");
 
738
    exitstatus = EX_OSERR;
 
739
    goto fallback;
 
740
  case EINVAL:
 
741
    exitstatus = EX_USAGE;
674
742
    goto fallback;
675
743
  }
676
744
  
688
756
    }
689
757
  }
690
758
  
691
 
  /* Strip permissions down to nobody */
692
 
  setgid(gid);
 
759
  if(getuid() == 0){
 
760
    /* Work around Debian bug #633582:
 
761
       <http://bugs.debian.org/633582> */
 
762
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
763
    if(plugindir_fd == -1){
 
764
      error(0, errno, "open");
 
765
    } else {
 
766
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
767
      if(ret == -1){
 
768
        error(0, errno, "fstat");
 
769
      } else {
 
770
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
771
          ret = fchown(plugindir_fd, uid, gid);
 
772
          if(ret == -1){
 
773
            error(0, errno, "fchown");
 
774
          }
 
775
        }
 
776
      }
 
777
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
778
    }
 
779
  }
 
780
  
 
781
  /* Lower permissions */
 
782
  ret = setgid(gid);
693
783
  if(ret == -1){
694
 
    perror("setgid");
 
784
    error(0, errno, "setgid");
695
785
  }
696
786
  ret = setuid(uid);
697
787
  if(ret == -1){
698
 
    perror("setuid");
699
 
  }
700
 
  
701
 
  if(plugindir == NULL){
702
 
    dir = opendir(PDIR);
703
 
  } else {
704
 
    dir = opendir(plugindir);
705
 
  }
706
 
  
707
 
  if(dir == NULL){
708
 
    perror("Could not open plugin dir");
709
 
    exitstatus = EXIT_FAILURE;
710
 
    goto fallback;
711
 
  }
712
 
  
713
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
788
    error(0, errno, "setuid");
 
789
  }
 
790
  
 
791
  /* Open plugin directory with close_on_exec flag */
714
792
  {
715
 
    int dir_fd = dirfd(dir);
716
 
    if(dir_fd >= 0){
717
 
      ret = set_cloexec_flag(dir_fd);
718
 
      if(ret < 0){
719
 
        perror("set_cloexec_flag");
720
 
        exitstatus = EXIT_FAILURE;
721
 
        goto fallback;
722
 
      }
 
793
    int dir_fd = -1;
 
794
    if(plugindir == NULL){
 
795
      dir_fd = open(PDIR, O_RDONLY |
 
796
#ifdef O_CLOEXEC
 
797
                    O_CLOEXEC
 
798
#else  /* not O_CLOEXEC */
 
799
                    0
 
800
#endif  /* not O_CLOEXEC */
 
801
                    );
 
802
    } else {
 
803
      dir_fd = open(plugindir, O_RDONLY |
 
804
#ifdef O_CLOEXEC
 
805
                    O_CLOEXEC
 
806
#else  /* not O_CLOEXEC */
 
807
                    0
 
808
#endif  /* not O_CLOEXEC */
 
809
                    );
 
810
    }
 
811
    if(dir_fd == -1){
 
812
      error(0, errno, "Could not open plugin dir");
 
813
      exitstatus = EX_UNAVAILABLE;
 
814
      goto fallback;
 
815
    }
 
816
    
 
817
#ifndef O_CLOEXEC
 
818
  /* Set the FD_CLOEXEC flag on the directory */
 
819
    ret = set_cloexec_flag(dir_fd);
 
820
    if(ret < 0){
 
821
      error(0, errno, "set_cloexec_flag");
 
822
      TEMP_FAILURE_RETRY(close(dir_fd));
 
823
      exitstatus = EX_OSERR;
 
824
      goto fallback;
 
825
    }
 
826
#endif  /* O_CLOEXEC */
 
827
    
 
828
    dir = fdopendir(dir_fd);
 
829
    if(dir == NULL){
 
830
      error(0, errno, "Could not open plugin dir");
 
831
      TEMP_FAILURE_RETRY(close(dir_fd));
 
832
      exitstatus = EX_OSERR;
 
833
      goto fallback;
723
834
    }
724
835
  }
725
836
  
734
845
    /* All directory entries have been processed */
735
846
    if(dirst == NULL){
736
847
      if(errno == EBADF){
737
 
        perror("readdir");
738
 
        exitstatus = EXIT_FAILURE;
 
848
        error(0, errno, "readdir");
 
849
        exitstatus = EX_IOERR;
739
850
        goto fallback;
740
851
      }
741
852
      break;
747
858
    {
748
859
      bool bad_name = false;
749
860
      
750
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
861
      const char * const bad_prefixes[] = { ".", "#", NULL };
751
862
      
752
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
863
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
753
864
                                           ".dpkg-old",
754
865
                                           ".dpkg-bak",
755
866
                                           ".dpkg-divert", NULL };
756
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
867
#ifdef __GNUC__
 
868
#pragma GCC diagnostic push
 
869
#pragma GCC diagnostic ignored "-Wcast-qual"
 
870
#endif
 
871
      for(const char **pre = (const char **)bad_prefixes;
 
872
          *pre != NULL; pre++){
 
873
#ifdef __GNUC__
 
874
#pragma GCC diagnostic pop
 
875
#endif
757
876
        size_t pre_len = strlen(*pre);
758
877
        if((d_name_len >= pre_len)
759
878
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
768
887
      if(bad_name){
769
888
        continue;
770
889
      }
771
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
890
#ifdef __GNUC__
 
891
#pragma GCC diagnostic push
 
892
#pragma GCC diagnostic ignored "-Wcast-qual"
 
893
#endif
 
894
      for(const char **suf = (const char **)bad_suffixes;
 
895
          *suf != NULL; suf++){
 
896
#ifdef __GNUC__
 
897
#pragma GCC diagnostic pop
 
898
#endif
772
899
        size_t suf_len = strlen(*suf);
773
900
        if((d_name_len >= suf_len)
774
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
901
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
775
902
                == 0)){
776
903
          if(debug){
777
904
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
797
924
                                             dirst->d_name));
798
925
    }
799
926
    if(ret < 0){
800
 
      perror("asprintf");
 
927
      error(0, errno, "asprintf");
801
928
      continue;
802
929
    }
803
930
    
804
931
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
805
932
    if(ret == -1){
806
 
      perror("stat");
 
933
      error(0, errno, "stat");
807
934
      free(filename);
808
935
      continue;
809
936
    }
821
948
    
822
949
    plugin *p = getplugin(dirst->d_name);
823
950
    if(p == NULL){
824
 
      perror("getplugin");
 
951
      error(0, errno, "getplugin");
825
952
      free(filename);
826
953
      continue;
827
954
    }
839
966
      if(g != NULL){
840
967
        for(char **a = g->argv + 1; *a != NULL; a++){
841
968
          if(not add_argument(p, *a)){
842
 
            perror("add_argument");
 
969
            error(0, errno, "add_argument");
843
970
          }
844
971
        }
845
972
        /* Add global environment variables */
846
973
        for(char **e = g->environ; *e != NULL; e++){
847
974
          if(not add_environment(p, *e, false)){
848
 
            perror("add_environment");
 
975
            error(0, errno, "add_environment");
849
976
          }
850
977
        }
851
978
      }
856
983
    if(p->environ[0] != NULL){
857
984
      for(char **e = environ; *e != NULL; e++){
858
985
        if(not add_environment(p, *e, false)){
859
 
          perror("add_environment");
 
986
          error(0, errno, "add_environment");
860
987
        }
861
988
      }
862
989
    }
864
991
    int pipefd[2];
865
992
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
866
993
    if(ret == -1){
867
 
      perror("pipe");
868
 
      exitstatus = EXIT_FAILURE;
 
994
      error(0, errno, "pipe");
 
995
      exitstatus = EX_OSERR;
869
996
      goto fallback;
870
997
    }
871
998
    /* Ask OS to automatic close the pipe on exec */
872
999
    ret = set_cloexec_flag(pipefd[0]);
873
1000
    if(ret < 0){
874
 
      perror("set_cloexec_flag");
875
 
      exitstatus = EXIT_FAILURE;
 
1001
      error(0, errno, "set_cloexec_flag");
 
1002
      exitstatus = EX_OSERR;
876
1003
      goto fallback;
877
1004
    }
878
1005
    ret = set_cloexec_flag(pipefd[1]);
879
1006
    if(ret < 0){
880
 
      perror("set_cloexec_flag");
881
 
      exitstatus = EXIT_FAILURE;
 
1007
      error(0, errno, "set_cloexec_flag");
 
1008
      exitstatus = EX_OSERR;
882
1009
      goto fallback;
883
1010
    }
884
1011
    /* Block SIGCHLD until process is safely in process list */
886
1013
                                              &sigchld_action.sa_mask,
887
1014
                                              NULL));
888
1015
    if(ret < 0){
889
 
      perror("sigprocmask");
890
 
      exitstatus = EXIT_FAILURE;
 
1016
      error(0, errno, "sigprocmask");
 
1017
      exitstatus = EX_OSERR;
891
1018
      goto fallback;
892
1019
    }
893
1020
    /* Starting a new process to be watched */
896
1023
      pid = fork();
897
1024
    } while(pid == -1 and errno == EINTR);
898
1025
    if(pid == -1){
899
 
      perror("fork");
900
 
      exitstatus = EXIT_FAILURE;
 
1026
      error(0, errno, "fork");
 
1027
      exitstatus = EX_OSERR;
901
1028
      goto fallback;
902
1029
    }
903
1030
    if(pid == 0){
904
1031
      /* this is the child process */
905
1032
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
906
1033
      if(ret < 0){
907
 
        perror("sigaction");
908
 
        _exit(EXIT_FAILURE);
 
1034
        error(0, errno, "sigaction");
 
1035
        _exit(EX_OSERR);
909
1036
      }
910
1037
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
911
1038
      if(ret < 0){
912
 
        perror("sigprocmask");
913
 
        _exit(EXIT_FAILURE);
 
1039
        error(0, errno, "sigprocmask");
 
1040
        _exit(EX_OSERR);
914
1041
      }
915
1042
      
916
1043
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
917
1044
      if(ret == -1){
918
 
        perror("dup2");
919
 
        _exit(EXIT_FAILURE);
 
1045
        error(0, errno, "dup2");
 
1046
        _exit(EX_OSERR);
920
1047
      }
921
1048
      
922
1049
      if(dirfd(dir) < 0){
926
1053
      }
927
1054
      if(p->environ[0] == NULL){
928
1055
        if(execv(filename, p->argv) < 0){
929
 
          perror("execv");
930
 
          _exit(EXIT_FAILURE);
 
1056
          error(0, errno, "execv for %s", filename);
 
1057
          _exit(EX_OSERR);
931
1058
        }
932
1059
      } else {
933
1060
        if(execve(filename, p->argv, p->environ) < 0){
934
 
          perror("execve");
935
 
          _exit(EXIT_FAILURE);
 
1061
          error(0, errno, "execve for %s", filename);
 
1062
          _exit(EX_OSERR);
936
1063
        }
937
1064
      }
938
1065
      /* no return */
943
1070
    free(filename);
944
1071
    plugin *new_plugin = getplugin(dirst->d_name);
945
1072
    if(new_plugin == NULL){
946
 
      perror("getplugin");
 
1073
      error(0, errno, "getplugin");
947
1074
      ret = (int)(TEMP_FAILURE_RETRY
948
1075
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
949
1076
                               NULL)));
950
1077
      if(ret < 0){
951
 
        perror("sigprocmask");
 
1078
        error(0, errno, "sigprocmask");
952
1079
      }
953
 
      exitstatus = EXIT_FAILURE;
 
1080
      exitstatus = EX_OSERR;
954
1081
      goto fallback;
955
1082
    }
956
1083
    
963
1090
                                              &sigchld_action.sa_mask,
964
1091
                                              NULL));
965
1092
    if(ret < 0){
966
 
      perror("sigprocmask");
967
 
      exitstatus = EXIT_FAILURE;
 
1093
      error(0, errno, "sigprocmask");
 
1094
      exitstatus = EX_OSERR;
968
1095
      goto fallback;
969
1096
    }
970
1097
    
971
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1098
#if defined (__GNUC__) and defined (__GLIBC__)
 
1099
#if not __GLIBC_PREREQ(2, 16)
 
1100
#pragma GCC diagnostic push
 
1101
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1102
#endif
 
1103
#endif
 
1104
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1105
                                          -Wconversion in GNU libc
 
1106
                                          before 2.16 */
 
1107
#if defined (__GNUC__) and defined (__GLIBC__)
 
1108
#if not __GLIBC_PREREQ(2, 16)
 
1109
#pragma GCC diagnostic pop
 
1110
#endif
 
1111
#endif
972
1112
    
973
1113
    if(maxfd < new_plugin->fd){
974
1114
      maxfd = new_plugin->fd;
995
1135
    fd_set rfds = rfds_all;
996
1136
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
997
1137
    if(select_ret == -1 and errno != EINTR){
998
 
      perror("select");
999
 
      exitstatus = EXIT_FAILURE;
 
1138
      error(0, errno, "select");
 
1139
      exitstatus = EX_OSERR;
1000
1140
      goto fallback;
1001
1141
    }
1002
1142
    /* OK, now either a process completed, or something can be read
1028
1168
          }
1029
1169
          
1030
1170
          /* Remove the plugin */
1031
 
          FD_CLR(proc->fd, &rfds_all);
 
1171
#if defined (__GNUC__) and defined (__GLIBC__)
 
1172
#if not __GLIBC_PREREQ(2, 16)
 
1173
#pragma GCC diagnostic push
 
1174
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1175
#endif
 
1176
#endif
 
1177
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1178
                                          -Wconversion in GNU libc
 
1179
                                          before 2.16 */
 
1180
#if defined (__GNUC__) and defined (__GLIBC__)
 
1181
#if not __GLIBC_PREREQ(2, 16)
 
1182
#pragma GCC diagnostic pop
 
1183
#endif
 
1184
#endif
1032
1185
          
1033
1186
          /* Block signal while modifying process_list */
1034
1187
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1036
1189
                                         &sigchld_action.sa_mask,
1037
1190
                                         NULL));
1038
1191
          if(ret < 0){
1039
 
            perror("sigprocmask");
1040
 
            exitstatus = EXIT_FAILURE;
 
1192
            error(0, errno, "sigprocmask");
 
1193
            exitstatus = EX_OSERR;
1041
1194
            goto fallback;
1042
1195
          }
1043
1196
          
1050
1203
                      (sigprocmask(SIG_UNBLOCK,
1051
1204
                                   &sigchld_action.sa_mask, NULL)));
1052
1205
          if(ret < 0){
1053
 
            perror("sigprocmask");
1054
 
            exitstatus = EXIT_FAILURE;
 
1206
            error(0, errno, "sigprocmask");
 
1207
            exitstatus = EX_OSERR;
1055
1208
            goto fallback;
1056
1209
          }
1057
1210
          
1067
1220
        bool bret = print_out_password(proc->buffer,
1068
1221
                                       proc->buffer_length);
1069
1222
        if(not bret){
1070
 
          perror("print_out_password");
1071
 
          exitstatus = EXIT_FAILURE;
 
1223
          error(0, errno, "print_out_password");
 
1224
          exitstatus = EX_IOERR;
1072
1225
        }
1073
1226
        goto fallback;
1074
1227
      }
1075
1228
      
1076
1229
      /* This process has not completed.  Does it have any output? */
1077
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1230
#if defined (__GNUC__) and defined (__GLIBC__)
 
1231
#if not __GLIBC_PREREQ(2, 16)
 
1232
#pragma GCC diagnostic push
 
1233
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1234
#endif
 
1235
#endif
 
1236
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1237
                                                         warning from
 
1238
                                                         -Wconversion
 
1239
                                                         in GNU libc
 
1240
                                                         before
 
1241
                                                         2.16 */
 
1242
#if defined (__GNUC__) and defined (__GLIBC__)
 
1243
#if not __GLIBC_PREREQ(2, 16)
 
1244
#pragma GCC diagnostic pop
 
1245
#endif
 
1246
#endif
1078
1247
        /* This process had nothing to say at this time */
1079
1248
        proc = proc->next;
1080
1249
        continue;
1081
1250
      }
1082
1251
      /* Before reading, make the process' data buffer large enough */
1083
1252
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1084
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1085
 
                               + (size_t) BUFFER_SIZE);
1086
 
        if(proc->buffer == NULL){
1087
 
          perror("malloc");
1088
 
          exitstatus = EXIT_FAILURE;
 
1253
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1254
                                   + (size_t) BUFFER_SIZE);
 
1255
        if(new_buffer == NULL){
 
1256
          error(0, errno, "malloc");
 
1257
          exitstatus = EX_OSERR;
1089
1258
          goto fallback;
1090
1259
        }
 
1260
        proc->buffer = new_buffer;
1091
1261
        proc->buffer_size += BUFFER_SIZE;
1092
1262
      }
1093
1263
      /* Read from the process */
1112
1282
  
1113
1283
 fallback:
1114
1284
  
1115
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1285
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1286
                             and exitstatus != EX_OK)){
1116
1287
    /* Fallback if all plugins failed, none are found or an error
1117
1288
       occured */
1118
1289
    bool bret;
1126
1297
    }
1127
1298
    bret = print_out_password(passwordbuffer, len);
1128
1299
    if(not bret){
1129
 
      perror("print_out_password");
1130
 
      exitstatus = EXIT_FAILURE;
 
1300
      error(0, errno, "print_out_password");
 
1301
      exitstatus = EX_IOERR;
1131
1302
    }
1132
1303
  }
1133
1304
  
1134
1305
  /* Restore old signal handler */
1135
1306
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1136
1307
  if(ret == -1){
1137
 
    perror("sigaction");
1138
 
    exitstatus = EXIT_FAILURE;
 
1308
    error(0, errno, "sigaction");
 
1309
    exitstatus = EX_OSERR;
1139
1310
  }
1140
1311
  
1141
1312
  if(custom_argv != NULL){
1156
1327
      ret = kill(p->pid, SIGTERM);
1157
1328
      if(ret == -1 and errno != ESRCH){
1158
1329
        /* Set-uid proccesses might not get closed */
1159
 
        perror("kill");
 
1330
        error(0, errno, "kill");
1160
1331
      }
1161
1332
    }
1162
1333
  }
1166
1337
    ret = wait(NULL);
1167
1338
  } while(ret >= 0);
1168
1339
  if(errno != ECHILD){
1169
 
    perror("wait");
 
1340
    error(0, errno, "wait");
1170
1341
  }
1171
1342
  
1172
1343
  free_plugin_list();