/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-09-17 01:21:27 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090917012127-4kwzquwuvudwucuv
* debian/control (Standards-Version): Updated to "2.8.3".

* mandos-client.conf.xml (OPTIONS/timeout): Add that a successful
                                            client request will also
                                            reset the timeout.
* mandos.xml (CHECKING): - '' -

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
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   asprintf() */
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
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, opendir(), 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, fdopendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, opendir(),
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(), 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");
789
 
  }
790
 
  
791
 
  /* Open plugin directory with close_on_exec flag */
 
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 */
792
714
  {
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;
 
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
      }
834
723
    }
835
724
  }
836
725
  
845
734
    /* All directory entries have been processed */
846
735
    if(dirst == NULL){
847
736
      if(errno == EBADF){
848
 
        error(0, errno, "readdir");
849
 
        exitstatus = EX_IOERR;
 
737
        perror("readdir");
 
738
        exitstatus = EXIT_FAILURE;
850
739
        goto fallback;
851
740
      }
852
741
      break;
858
747
    {
859
748
      bool bad_name = false;
860
749
      
861
 
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
750
      const char const *bad_prefixes[] = { ".", "#", NULL };
862
751
      
863
 
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
752
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
864
753
                                           ".dpkg-old",
865
754
                                           ".dpkg-bak",
866
755
                                           ".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
 
756
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
876
757
        size_t pre_len = strlen(*pre);
877
758
        if((d_name_len >= pre_len)
878
759
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
887
768
      if(bad_name){
888
769
        continue;
889
770
      }
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
 
771
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
899
772
        size_t suf_len = strlen(*suf);
900
773
        if((d_name_len >= suf_len)
901
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
774
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
902
775
                == 0)){
903
776
          if(debug){
904
777
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
924
797
                                             dirst->d_name));
925
798
    }
926
799
    if(ret < 0){
927
 
      error(0, errno, "asprintf");
 
800
      perror("asprintf");
928
801
      continue;
929
802
    }
930
803
    
931
804
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
932
805
    if(ret == -1){
933
 
      error(0, errno, "stat");
 
806
      perror("stat");
934
807
      free(filename);
935
808
      continue;
936
809
    }
948
821
    
949
822
    plugin *p = getplugin(dirst->d_name);
950
823
    if(p == NULL){
951
 
      error(0, errno, "getplugin");
 
824
      perror("getplugin");
952
825
      free(filename);
953
826
      continue;
954
827
    }
966
839
      if(g != NULL){
967
840
        for(char **a = g->argv + 1; *a != NULL; a++){
968
841
          if(not add_argument(p, *a)){
969
 
            error(0, errno, "add_argument");
 
842
            perror("add_argument");
970
843
          }
971
844
        }
972
845
        /* Add global environment variables */
973
846
        for(char **e = g->environ; *e != NULL; e++){
974
847
          if(not add_environment(p, *e, false)){
975
 
            error(0, errno, "add_environment");
 
848
            perror("add_environment");
976
849
          }
977
850
        }
978
851
      }
983
856
    if(p->environ[0] != NULL){
984
857
      for(char **e = environ; *e != NULL; e++){
985
858
        if(not add_environment(p, *e, false)){
986
 
          error(0, errno, "add_environment");
 
859
          perror("add_environment");
987
860
        }
988
861
      }
989
862
    }
991
864
    int pipefd[2];
992
865
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
993
866
    if(ret == -1){
994
 
      error(0, errno, "pipe");
995
 
      exitstatus = EX_OSERR;
 
867
      perror("pipe");
 
868
      exitstatus = EXIT_FAILURE;
996
869
      goto fallback;
997
870
    }
998
871
    /* Ask OS to automatic close the pipe on exec */
999
872
    ret = set_cloexec_flag(pipefd[0]);
1000
873
    if(ret < 0){
1001
 
      error(0, errno, "set_cloexec_flag");
1002
 
      exitstatus = EX_OSERR;
 
874
      perror("set_cloexec_flag");
 
875
      exitstatus = EXIT_FAILURE;
1003
876
      goto fallback;
1004
877
    }
1005
878
    ret = set_cloexec_flag(pipefd[1]);
1006
879
    if(ret < 0){
1007
 
      error(0, errno, "set_cloexec_flag");
1008
 
      exitstatus = EX_OSERR;
 
880
      perror("set_cloexec_flag");
 
881
      exitstatus = EXIT_FAILURE;
1009
882
      goto fallback;
1010
883
    }
1011
884
    /* Block SIGCHLD until process is safely in process list */
1013
886
                                              &sigchld_action.sa_mask,
1014
887
                                              NULL));
1015
888
    if(ret < 0){
1016
 
      error(0, errno, "sigprocmask");
1017
 
      exitstatus = EX_OSERR;
 
889
      perror("sigprocmask");
 
890
      exitstatus = EXIT_FAILURE;
1018
891
      goto fallback;
1019
892
    }
1020
893
    /* Starting a new process to be watched */
1023
896
      pid = fork();
1024
897
    } while(pid == -1 and errno == EINTR);
1025
898
    if(pid == -1){
1026
 
      error(0, errno, "fork");
1027
 
      exitstatus = EX_OSERR;
 
899
      perror("fork");
 
900
      exitstatus = EXIT_FAILURE;
1028
901
      goto fallback;
1029
902
    }
1030
903
    if(pid == 0){
1031
904
      /* this is the child process */
1032
905
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1033
906
      if(ret < 0){
1034
 
        error(0, errno, "sigaction");
1035
 
        _exit(EX_OSERR);
 
907
        perror("sigaction");
 
908
        _exit(EXIT_FAILURE);
1036
909
      }
1037
910
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1038
911
      if(ret < 0){
1039
 
        error(0, errno, "sigprocmask");
1040
 
        _exit(EX_OSERR);
 
912
        perror("sigprocmask");
 
913
        _exit(EXIT_FAILURE);
1041
914
      }
1042
915
      
1043
916
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
1044
917
      if(ret == -1){
1045
 
        error(0, errno, "dup2");
1046
 
        _exit(EX_OSERR);
 
918
        perror("dup2");
 
919
        _exit(EXIT_FAILURE);
1047
920
      }
1048
921
      
1049
922
      if(dirfd(dir) < 0){
1053
926
      }
1054
927
      if(p->environ[0] == NULL){
1055
928
        if(execv(filename, p->argv) < 0){
1056
 
          error(0, errno, "execv for %s", filename);
1057
 
          _exit(EX_OSERR);
 
929
          perror("execv");
 
930
          _exit(EXIT_FAILURE);
1058
931
        }
1059
932
      } else {
1060
933
        if(execve(filename, p->argv, p->environ) < 0){
1061
 
          error(0, errno, "execve for %s", filename);
1062
 
          _exit(EX_OSERR);
 
934
          perror("execve");
 
935
          _exit(EXIT_FAILURE);
1063
936
        }
1064
937
      }
1065
938
      /* no return */
1070
943
    free(filename);
1071
944
    plugin *new_plugin = getplugin(dirst->d_name);
1072
945
    if(new_plugin == NULL){
1073
 
      error(0, errno, "getplugin");
 
946
      perror("getplugin");
1074
947
      ret = (int)(TEMP_FAILURE_RETRY
1075
948
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1076
949
                               NULL)));
1077
950
      if(ret < 0){
1078
 
        error(0, errno, "sigprocmask");
 
951
        perror("sigprocmask");
1079
952
      }
1080
 
      exitstatus = EX_OSERR;
 
953
      exitstatus = EXIT_FAILURE;
1081
954
      goto fallback;
1082
955
    }
1083
956
    
1090
963
                                              &sigchld_action.sa_mask,
1091
964
                                              NULL));
1092
965
    if(ret < 0){
1093
 
      error(0, errno, "sigprocmask");
1094
 
      exitstatus = EX_OSERR;
 
966
      perror("sigprocmask");
 
967
      exitstatus = EXIT_FAILURE;
1095
968
      goto fallback;
1096
969
    }
1097
970
    
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
 
971
    FD_SET(new_plugin->fd, &rfds_all);
1112
972
    
1113
973
    if(maxfd < new_plugin->fd){
1114
974
      maxfd = new_plugin->fd;
1135
995
    fd_set rfds = rfds_all;
1136
996
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1137
997
    if(select_ret == -1 and errno != EINTR){
1138
 
      error(0, errno, "select");
1139
 
      exitstatus = EX_OSERR;
 
998
      perror("select");
 
999
      exitstatus = EXIT_FAILURE;
1140
1000
      goto fallback;
1141
1001
    }
1142
1002
    /* OK, now either a process completed, or something can be read
1168
1028
          }
1169
1029
          
1170
1030
          /* 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
 
          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
 
1031
          FD_CLR(proc->fd, &rfds_all);
1185
1032
          
1186
1033
          /* Block signal while modifying process_list */
1187
1034
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1189
1036
                                         &sigchld_action.sa_mask,
1190
1037
                                         NULL));
1191
1038
          if(ret < 0){
1192
 
            error(0, errno, "sigprocmask");
1193
 
            exitstatus = EX_OSERR;
 
1039
            perror("sigprocmask");
 
1040
            exitstatus = EXIT_FAILURE;
1194
1041
            goto fallback;
1195
1042
          }
1196
1043
          
1203
1050
                      (sigprocmask(SIG_UNBLOCK,
1204
1051
                                   &sigchld_action.sa_mask, NULL)));
1205
1052
          if(ret < 0){
1206
 
            error(0, errno, "sigprocmask");
1207
 
            exitstatus = EX_OSERR;
 
1053
            perror("sigprocmask");
 
1054
            exitstatus = EXIT_FAILURE;
1208
1055
            goto fallback;
1209
1056
          }
1210
1057
          
1220
1067
        bool bret = print_out_password(proc->buffer,
1221
1068
                                       proc->buffer_length);
1222
1069
        if(not bret){
1223
 
          error(0, errno, "print_out_password");
1224
 
          exitstatus = EX_IOERR;
 
1070
          perror("print_out_password");
 
1071
          exitstatus = EXIT_FAILURE;
1225
1072
        }
1226
1073
        goto fallback;
1227
1074
      }
1228
1075
      
1229
1076
      /* 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
 
      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
 
1077
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1247
1078
        /* This process had nothing to say at this time */
1248
1079
        proc = proc->next;
1249
1080
        continue;
1250
1081
      }
1251
1082
      /* Before reading, make the process' data buffer large enough */
1252
1083
      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;
 
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;
1258
1089
          goto fallback;
1259
1090
        }
1260
 
        proc->buffer = new_buffer;
1261
1091
        proc->buffer_size += BUFFER_SIZE;
1262
1092
      }
1263
1093
      /* Read from the process */
1282
1112
  
1283
1113
 fallback:
1284
1114
  
1285
 
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
1286
 
                             and exitstatus != EX_OK)){
 
1115
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1287
1116
    /* Fallback if all plugins failed, none are found or an error
1288
1117
       occured */
1289
1118
    bool bret;
1297
1126
    }
1298
1127
    bret = print_out_password(passwordbuffer, len);
1299
1128
    if(not bret){
1300
 
      error(0, errno, "print_out_password");
1301
 
      exitstatus = EX_IOERR;
 
1129
      perror("print_out_password");
 
1130
      exitstatus = EXIT_FAILURE;
1302
1131
    }
1303
1132
  }
1304
1133
  
1305
1134
  /* Restore old signal handler */
1306
1135
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1307
1136
  if(ret == -1){
1308
 
    error(0, errno, "sigaction");
1309
 
    exitstatus = EX_OSERR;
 
1137
    perror("sigaction");
 
1138
    exitstatus = EXIT_FAILURE;
1310
1139
  }
1311
1140
  
1312
1141
  if(custom_argv != NULL){
1327
1156
      ret = kill(p->pid, SIGTERM);
1328
1157
      if(ret == -1 and errno != ESRCH){
1329
1158
        /* Set-uid proccesses might not get closed */
1330
 
        error(0, errno, "kill");
 
1159
        perror("kill");
1331
1160
      }
1332
1161
    }
1333
1162
  }
1337
1166
    ret = wait(NULL);
1338
1167
  } while(ret >= 0);
1339
1168
  if(errno != ECHILD){
1340
 
    error(0, errno, "wait");
 
1169
    perror("wait");
1341
1170
  }
1342
1171
  
1343
1172
  free_plugin_list();