/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: 2009-10-18 08:47:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091018084740-fa1qgm22lg125r10
* plugins.d/splashy.c: Use exit codes from <sysexits.h>.

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