/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: 2008-10-04 01:55:56 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081004015556-hp0374bc62khen0z
* mandos-keygen.xml (FILES): Fixed id to be "files", not "file".
* mandos.xml (FILES): - '' -
  (CLIENTS, SERVER): Changed id attribute to lower case.  All
                     referrers changed.
* plugins.d/mandos-client.xml (FILES): Fixed id to be "files", not
                                       "file".

* plugins.d/password-prompt.xml (DESCRIPTION): Improved wording.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
 
1
/*  -*- coding: utf-8 -*- */
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 Teddy Hogeborn & Björn Påhlsson
7
6
 * 
8
7
 * This program is free software: you can redistribute it and/or
9
8
 * modify it under the terms of the GNU General Public License as
23
22
 */
24
23
 
25
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
25
                                   asprintf() */
27
26
#include <stddef.h>             /* size_t, NULL */
28
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
28
                                   EXIT_SUCCESS, realloc() */
30
29
#include <stdbool.h>            /* bool, true, false */
31
30
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
31
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
33
                                   stat, waitpid(), WIFEXITED(),
35
34
                                   WEXITSTATUS(), wait(), pid_t,
36
35
                                   uid_t, gid_t, getuid(), getgid(),
38
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG(),
42
 
                                   WCOREDUMP() */
 
40
                                   WEXITSTATUS() */
43
41
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
42
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
43
#include <dirent.h>             /* DIR, struct dirent, opendir(),
46
44
                                   readdir(), closedir(), dirfd() */
47
45
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
46
                                   fcntl(), setuid(), setgid(),
53
51
                                   close() */
54
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
53
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
54
#include <string.h>             /* strsep, strlen(), asprintf() */
58
55
#include <errno.h>              /* errno */
59
56
#include <argp.h>               /* struct argp_option, struct
60
57
                                   argp_state, struct argp,
64
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
65
62
                                   sigaddset(), sigaction(),
66
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
67
 
                                   SIG_UNBLOCK, kill(), sig_atomic_t
68
 
                                */
 
64
                                   SIG_UNBLOCK, kill() */
69
65
#include <errno.h>              /* errno, EBADF */
70
 
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
72
66
 
73
67
#define BUFFER_SIZE 256
74
68
 
85
79
  char **environ;
86
80
  int envc;
87
81
  bool disabled;
88
 
  
 
82
 
89
83
  /* Variables used for running processes*/
90
84
  pid_t pid;
91
85
  int fd;
93
87
  size_t buffer_size;
94
88
  size_t buffer_length;
95
89
  bool eof;
96
 
  volatile sig_atomic_t completed;
97
 
  int status;
 
90
  volatile bool completed;
 
91
  volatile int status;
98
92
  struct plugin *next;
99
93
} plugin;
100
94
 
103
97
/* Gets an existing plugin based on name,
104
98
   or if none is found, creates a new one */
105
99
static plugin *getplugin(char *name){
106
 
  /* Check for existing plugin with that name */
107
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
108
 
    if((p->name == name)
109
 
       or (p->name and name and (strcmp(p->name, name) == 0))){
 
100
  /* Check for exiting plugin with that name */
 
101
  for (plugin *p = plugin_list; p != NULL; p = p->next){
 
102
    if ((p->name == name)
 
103
        or (p->name and name and (strcmp(p->name, name) == 0))){
110
104
      return p;
111
105
    }
112
106
  }
113
107
  /* Create a new plugin */
114
 
  plugin *new_plugin = NULL;
115
 
  do {
116
 
    new_plugin = malloc(sizeof(plugin));
117
 
  } while(new_plugin == NULL and errno == EINTR);
118
 
  if(new_plugin == NULL){
 
108
  plugin *new_plugin = malloc(sizeof(plugin));
 
109
  if (new_plugin == NULL){
119
110
    return NULL;
120
111
  }
121
112
  char *copy_name = NULL;
122
113
  if(name != NULL){
123
 
    do {
124
 
      copy_name = strdup(name);
125
 
    } while(copy_name == NULL and errno == EINTR);
 
114
    copy_name = strdup(name);
126
115
    if(copy_name == NULL){
127
 
      int e = errno;
128
 
      free(new_plugin);
129
 
      errno = e;
130
116
      return NULL;
131
117
    }
132
118
  }
133
119
  
134
 
  *new_plugin = (plugin){ .name = copy_name,
135
 
                          .argc = 1,
136
 
                          .disabled = false,
137
 
                          .next = plugin_list };
 
120
  *new_plugin = (plugin) { .name = copy_name,
 
121
                           .argc = 1,
 
122
                           .disabled = false,
 
123
                           .next = plugin_list };
138
124
  
139
 
  do {
140
 
    new_plugin->argv = malloc(sizeof(char *) * 2);
141
 
  } while(new_plugin->argv == NULL and errno == EINTR);
142
 
  if(new_plugin->argv == NULL){
143
 
    int e = errno;
 
125
  new_plugin->argv = malloc(sizeof(char *) * 2);
 
126
  if (new_plugin->argv == NULL){
144
127
    free(copy_name);
145
128
    free(new_plugin);
146
 
    errno = e;
147
129
    return NULL;
148
130
  }
149
131
  new_plugin->argv[0] = copy_name;
150
132
  new_plugin->argv[1] = NULL;
151
133
  
152
 
  do {
153
 
    new_plugin->environ = malloc(sizeof(char *));
154
 
  } while(new_plugin->environ == NULL and errno == EINTR);
 
134
  new_plugin->environ = malloc(sizeof(char *));
155
135
  if(new_plugin->environ == NULL){
156
 
    int e = errno;
157
136
    free(copy_name);
158
137
    free(new_plugin->argv);
159
138
    free(new_plugin);
160
 
    errno = e;
161
139
    return NULL;
162
140
  }
163
141
  new_plugin->environ[0] = NULL;
171
149
static bool add_to_char_array(const char *new, char ***array,
172
150
                              int *len){
173
151
  /* Resize the pointed-to array to hold one more pointer */
174
 
  do {
175
 
    *array = realloc(*array, sizeof(char *)
176
 
                     * (size_t) ((*len) + 2));
177
 
  } while(*array == NULL and errno == EINTR);
 
152
  *array = realloc(*array, sizeof(char *)
 
153
                   * (size_t) ((*len) + 2));
178
154
  /* Malloc check */
179
155
  if(*array == NULL){
180
156
    return false;
181
157
  }
182
158
  /* Make a copy of the new string */
183
 
  char *copy;
184
 
  do {
185
 
    copy = strdup(new);
186
 
  } while(copy == NULL and errno == EINTR);
 
159
  char *copy = strdup(new);
187
160
  if(copy == NULL){
188
161
    return false;
189
162
  }
215
188
    if(strncmp(*e, def, namelen + 1) == 0){
216
189
      /* It already exists */
217
190
      if(replace){
218
 
        char *new;
219
 
        do {
220
 
          new = realloc(*e, strlen(def) + 1);
221
 
        } while(new == NULL and errno == EINTR);
 
191
        char *new = realloc(*e, strlen(def) + 1);
222
192
        if(new == NULL){
223
193
          return false;
224
194
        }
234
204
/*
235
205
 * Based on the example in the GNU LibC manual chapter 13.13 "File
236
206
 * Descriptor Flags".
237
 
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
207
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
238
208
 */
239
209
static int set_cloexec_flag(int fd){
240
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
210
  int ret = fcntl(fd, F_GETFD, 0);
241
211
  /* If reading the flags failed, return error indication now. */
242
212
  if(ret < 0){
243
213
    return ret;
244
214
  }
245
215
  /* Store modified flag word in the descriptor. */
246
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
247
 
                                       ret | FD_CLOEXEC));
 
216
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
248
217
}
249
218
 
250
219
 
251
220
/* Mark processes as completed when they exit, and save their exit
252
221
   status. */
253
222
static void handle_sigchld(__attribute__((unused)) int sig){
254
 
  int old_errno = errno;
255
223
  while(true){
256
224
    plugin *proc = plugin_list;
257
225
    int status;
261
229
      break;
262
230
    }
263
231
    if(pid == -1){
264
 
      if(errno == ECHILD){
265
 
        /* No child processes */
266
 
        break;
 
232
      if (errno != ECHILD){
 
233
        perror("waitpid");
267
234
      }
268
 
      perror("waitpid");
 
235
      /* No child processes */
 
236
      break;
269
237
    }
270
238
    
271
239
    /* A child exited, find it in process_list */
277
245
      continue;
278
246
    }
279
247
    proc->status = status;
280
 
    proc->completed = 1;
 
248
    proc->completed = true;
281
249
  }
282
 
  errno = old_errno;
283
250
}
284
251
 
285
252
/* Prints out a password to stdout */
307
274
  }
308
275
  free(plugin_node->environ);
309
276
  free(plugin_node->buffer);
310
 
  
 
277
 
311
278
  /* Removes the plugin from the singly-linked list */
312
279
  if(plugin_node == plugin_list){
313
280
    /* First one - simple */
341
308
  struct stat st;
342
309
  fd_set rfds_all;
343
310
  int ret, maxfd = 0;
344
 
  ssize_t sret;
345
311
  uid_t uid = 65534;
346
312
  gid_t gid = 65534;
347
313
  bool debug = false;
401
367
      .doc = "Group ID the plugins will run as", .group = 3 },
402
368
    { .name = "debug", .key = 132,
403
369
      .doc = "Debug mode", .group = 4 },
404
 
    /*
405
 
     * These reproduce what we would get without ARGP_NO_HELP
406
 
     */
407
 
    { .name = "help", .key = '?',
408
 
      .doc = "Give this help list", .group = -1 },
409
 
    { .name = "usage", .key = -3,
410
 
      .doc = "Give a short usage message", .group = -1 },
411
 
    { .name = "version", .key = 'V',
412
 
      .doc = "Print program version", .group = -1 },
413
370
    { .name = NULL }
414
371
  };
415
372
  
416
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
417
 
    errno = 0;
418
 
    switch(key){
419
 
      char *tmp;
420
 
      intmax_t tmpmax;
 
373
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
374
                     struct argp_state *state) {
 
375
    switch (key) {
421
376
    case 'g':                   /* --global-options */
422
 
      {
423
 
        char *plugin_option;
424
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
425
 
          if(not add_argument(getplugin(NULL), plugin_option)){
426
 
            break;
 
377
      if (arg != NULL){
 
378
        char *p;
 
379
        while((p = strsep(&arg, ",")) != NULL){
 
380
          if(p[0] == '\0'){
 
381
            continue;
 
382
          }
 
383
          if(not add_argument(getplugin(NULL), p)){
 
384
            perror("add_argument");
 
385
            return ARGP_ERR_UNKNOWN;
427
386
          }
428
387
        }
429
388
      }
430
389
      break;
431
390
    case 'G':                   /* --global-env */
432
 
      add_environment(getplugin(NULL), arg, true);
 
391
      if(arg == NULL){
 
392
        break;
 
393
      }
 
394
      if(not add_environment(getplugin(NULL), arg, true)){
 
395
        perror("add_environment");
 
396
      }
433
397
      break;
434
398
    case 'o':                   /* --options-for */
435
 
      {
436
 
        char *option_list = strchr(arg, ':');
437
 
        if(option_list == NULL){
438
 
          argp_error(state, "No colon in \"%s\"", arg);
439
 
          errno = EINVAL;
440
 
          break;
441
 
        }
442
 
        *option_list = '\0';
443
 
        option_list++;
444
 
        if(arg[0] == '\0'){
445
 
          argp_error(state, "Empty plugin name");
446
 
          errno = EINVAL;
447
 
          break;
448
 
        }
449
 
        char *option;
450
 
        while((option = strsep(&option_list, ",")) != NULL){
451
 
          if(not add_argument(getplugin(arg), option)){
452
 
            break;
 
399
      if (arg != NULL){
 
400
        char *p_name = strsep(&arg, ":");
 
401
        if(p_name[0] == '\0' or arg == NULL){
 
402
          break;
 
403
        }
 
404
        char *opt = strsep(&arg, ":");
 
405
        if(opt[0] == '\0' or opt == NULL){
 
406
          break;
 
407
        }
 
408
        char *p;
 
409
        while((p = strsep(&opt, ",")) != NULL){
 
410
          if(p[0] == '\0'){
 
411
            continue;
 
412
          }
 
413
          if(not add_argument(getplugin(p_name), p)){
 
414
            perror("add_argument");
 
415
            return ARGP_ERR_UNKNOWN;
453
416
          }
454
417
        }
455
418
      }
456
419
      break;
457
420
    case 'E':                   /* --env-for */
 
421
      if(arg == NULL){
 
422
        break;
 
423
      }
458
424
      {
459
425
        char *envdef = strchr(arg, ':');
460
426
        if(envdef == NULL){
461
 
          argp_error(state, "No colon in \"%s\"", arg);
462
 
          errno = EINVAL;
463
427
          break;
464
428
        }
465
429
        *envdef = '\0';
466
 
        envdef++;
467
 
        if(arg[0] == '\0'){
468
 
          argp_error(state, "Empty plugin name");
469
 
          errno = EINVAL;
470
 
          break;
 
430
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
431
          perror("add_environment");
471
432
        }
472
 
        add_environment(getplugin(arg), envdef, true);
473
433
      }
474
434
      break;
475
435
    case 'd':                   /* --disable */
476
 
      {
 
436
      if (arg != NULL){
477
437
        plugin *p = getplugin(arg);
478
 
        if(p != NULL){
479
 
          p->disabled = true;
 
438
        if(p == NULL){
 
439
          return ARGP_ERR_UNKNOWN;
480
440
        }
 
441
        p->disabled = true;
481
442
      }
482
443
      break;
483
444
    case 'e':                   /* --enable */
484
 
      {
 
445
      if (arg != NULL){
485
446
        plugin *p = getplugin(arg);
486
 
        if(p != NULL){
487
 
          p->disabled = false;
 
447
        if(p == NULL){
 
448
          return ARGP_ERR_UNKNOWN;
488
449
        }
 
450
        p->disabled = false;
489
451
      }
490
452
      break;
491
453
    case 128:                   /* --plugin-dir */
492
454
      free(plugindir);
493
455
      plugindir = strdup(arg);
 
456
      if(plugindir == NULL){
 
457
        perror("strdup");
 
458
      }      
494
459
      break;
495
460
    case 129:                   /* --config-file */
496
461
      /* This is already done by parse_opt_config_file() */
497
462
      break;
498
463
    case 130:                   /* --userid */
499
 
      tmpmax = strtoimax(arg, &tmp, 10);
500
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
501
 
         or tmpmax != (uid_t)tmpmax){
502
 
        argp_error(state, "Bad user ID number: \"%s\", using %"
503
 
                   PRIdMAX, arg, (intmax_t)uid);
504
 
        break;
505
 
      }
506
 
      uid = (uid_t)tmpmax;
 
464
      uid = (uid_t)strtol(arg, NULL, 10);
507
465
      break;
508
466
    case 131:                   /* --groupid */
509
 
      tmpmax = strtoimax(arg, &tmp, 10);
510
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
511
 
         or tmpmax != (gid_t)tmpmax){
512
 
        argp_error(state, "Bad group ID number: \"%s\", using %"
513
 
                   PRIdMAX, arg, (intmax_t)gid);
514
 
        break;
515
 
      }
516
 
      gid = (gid_t)tmpmax;
 
467
      gid = (gid_t)strtol(arg, NULL, 10);
517
468
      break;
518
469
    case 132:                   /* --debug */
519
470
      debug = true;
520
471
      break;
521
 
      /*
522
 
       * These reproduce what we would get without ARGP_NO_HELP
523
 
       */
524
 
    case '?':                   /* --help */
525
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
526
 
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
527
 
    case -3:                    /* --usage */
528
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
529
 
      argp_state_help(state, state->out_stream,
530
 
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
531
 
    case 'V':                   /* --version */
532
 
      fprintf(state->out_stream, "%s\n", argp_program_version);
533
 
      exit(EXIT_SUCCESS);
534
 
      break;
535
 
/*
536
 
 * When adding more options before this line, remember to also add a
537
 
 * "case" to the "parse_opt_config_file" function below.
538
 
 */
539
472
    case ARGP_KEY_ARG:
540
473
      /* Cryptsetup always passes an argument, which is an empty
541
474
         string if "none" was specified in /etc/crypttab.  So if
542
475
         argument was empty, we ignore it silently. */
543
 
      if(arg[0] == '\0'){
544
 
        break;
 
476
      if(arg[0] != '\0'){
 
477
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
545
478
      }
 
479
      break;
 
480
    case ARGP_KEY_END:
 
481
      break;
546
482
    default:
547
483
      return ARGP_ERR_UNKNOWN;
548
484
    }
549
 
    return errno;               /* Set to 0 at start */
 
485
    return 0;
550
486
  }
551
487
  
552
488
  /* This option parser is the same as parse_opt() above, except it
553
489
     ignores everything but the --config-file option. */
554
 
  error_t parse_opt_config_file(int key, char *arg,
555
 
                                __attribute__((unused))
556
 
                                struct argp_state *state){
557
 
    errno = 0;
558
 
    switch(key){
 
490
  error_t parse_opt_config_file (int key, char *arg,
 
491
                                 __attribute__((unused))
 
492
                                 struct argp_state *state) {
 
493
    switch (key) {
559
494
    case 'g':                   /* --global-options */
560
495
    case 'G':                   /* --global-env */
561
496
    case 'o':                   /* --options-for */
567
502
    case 129:                   /* --config-file */
568
503
      free(argfile);
569
504
      argfile = strdup(arg);
570
 
      break;
 
505
      if(argfile == NULL){
 
506
        perror("strdup");
 
507
      }
 
508
      break;      
571
509
    case 130:                   /* --userid */
572
510
    case 131:                   /* --groupid */
573
511
    case 132:                   /* --debug */
574
 
    case '?':                   /* --help */
575
 
    case -3:                    /* --usage */
576
 
    case 'V':                   /* --version */
577
512
    case ARGP_KEY_ARG:
 
513
    case ARGP_KEY_END:
578
514
      break;
579
515
    default:
580
516
      return ARGP_ERR_UNKNOWN;
581
517
    }
582
 
    return errno;
 
518
    return 0;
583
519
  }
584
520
  
585
521
  struct argp argp = { .options = options,
587
523
                       .args_doc = "",
588
524
                       .doc = "Mandos plugin runner -- Run plugins" };
589
525
  
590
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
526
  /* Parse using the parse_opt_config_file in order to get the custom
591
527
     config file location, if any. */
592
 
  ret = argp_parse(&argp, argc, argv,
593
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
594
 
                   NULL, NULL);
595
 
  switch(ret){
596
 
  case 0:
597
 
    break;
598
 
  case ENOMEM:
599
 
  default:
600
 
    errno = ret;
601
 
    perror("argp_parse");
602
 
    exitstatus = EX_OSERR;
603
 
    goto fallback;
604
 
  case EINVAL:
605
 
    exitstatus = EX_USAGE;
 
528
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
529
  if (ret == ARGP_ERR_UNKNOWN){
 
530
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
531
    exitstatus = EXIT_FAILURE;
606
532
    goto fallback;
607
533
  }
608
534
  
610
536
  argp.parser = parse_opt;
611
537
  
612
538
  /* Open the configfile if available */
613
 
  if(argfile == NULL){
 
539
  if (argfile == NULL){
614
540
    conffp = fopen(AFILE, "r");
615
541
  } else {
616
542
    conffp = fopen(argfile, "r");
617
 
  }
 
543
  }  
618
544
  if(conffp != NULL){
619
545
    char *org_line = NULL;
620
546
    char *p, *arg, *new_arg, *line;
621
547
    size_t size = 0;
 
548
    ssize_t sret;
622
549
    const char whitespace_delims[] = " \r\t\f\v\n";
623
550
    const char comment_delim[] = "#";
624
 
    
 
551
 
625
552
    custom_argc = 1;
626
553
    custom_argv = malloc(sizeof(char*) * 2);
627
554
    if(custom_argv == NULL){
631
558
    }
632
559
    custom_argv[0] = argv[0];
633
560
    custom_argv[1] = NULL;
634
 
    
 
561
 
635
562
    /* for each line in the config file, strip whitespace and ignore
636
563
       commented text */
637
564
    while(true){
639
566
      if(sret == -1){
640
567
        break;
641
568
      }
642
 
      
 
569
 
643
570
      line = org_line;
644
571
      arg = strsep(&line, comment_delim);
645
572
      while((p = strsep(&arg, whitespace_delims)) != NULL){
664
591
          goto fallback;
665
592
        }
666
593
        custom_argv[custom_argc-1] = new_arg;
667
 
        custom_argv[custom_argc] = NULL;
 
594
        custom_argv[custom_argc] = NULL;        
668
595
      }
669
596
    }
670
 
    do {
671
 
      ret = fclose(conffp);
672
 
    } while(ret == EOF and errno == EINTR);
673
 
    if(ret == EOF){
674
 
      perror("fclose");
675
 
      exitstatus = EXIT_FAILURE;
676
 
      goto fallback;
677
 
    }
678
597
    free(org_line);
679
598
  } else {
680
599
    /* Check for harmful errors and go to fallback. Other errors might
681
600
       not affect opening plugins */
682
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
601
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
683
602
      perror("fopen");
684
603
      exitstatus = EXIT_FAILURE;
685
604
      goto fallback;
686
605
    }
687
606
  }
688
 
  /* If there were any arguments from the configuration file, pass
689
 
     them to parser as command line arguments */
 
607
  /* If there was any arguments from configuration file,
 
608
     pass them to parser as command arguments */
690
609
  if(custom_argv != NULL){
691
 
    ret = argp_parse(&argp, custom_argc, custom_argv,
692
 
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
693
 
                     NULL, NULL);
694
 
    switch(ret){
695
 
    case 0:
696
 
      break;
697
 
    case ENOMEM:
698
 
    default:
699
 
      errno = ret;
700
 
      perror("argp_parse");
701
 
      exitstatus = EX_OSERR;
702
 
      goto fallback;
703
 
    case EINVAL:
704
 
      exitstatus = EX_CONFIG;
 
610
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
611
                      0, NULL);
 
612
    if (ret == ARGP_ERR_UNKNOWN){
 
613
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
614
      exitstatus = EXIT_FAILURE;
705
615
      goto fallback;
706
616
    }
707
617
  }
708
618
  
709
619
  /* Parse actual command line arguments, to let them override the
710
620
     config file */
711
 
  ret = argp_parse(&argp, argc, argv,
712
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
713
 
                   NULL, NULL);
714
 
  switch(ret){
715
 
  case 0:
716
 
    break;
717
 
  case ENOMEM:
718
 
  default:
719
 
    errno = ret;
720
 
    perror("argp_parse");
721
 
    exitstatus = EX_OSERR;
722
 
    goto fallback;
723
 
  case EINVAL:
724
 
    exitstatus = EX_USAGE;
 
621
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
622
  if (ret == ARGP_ERR_UNKNOWN){
 
623
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
624
    exitstatus = EXIT_FAILURE;
725
625
    goto fallback;
726
626
  }
727
627
  
732
632
      for(char **a = p->argv; *a != NULL; a++){
733
633
        fprintf(stderr, "\tArg: %s\n", *a);
734
634
      }
735
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
635
      fprintf(stderr, "...and %u environment variables\n", p->envc);
736
636
      for(char **a = p->environ; *a != NULL; a++){
737
637
        fprintf(stderr, "\t%s\n", *a);
738
638
      }
740
640
  }
741
641
  
742
642
  /* Strip permissions down to nobody */
 
643
  ret = setuid(uid);
 
644
  if (ret == -1){
 
645
    perror("setuid");
 
646
  }  
743
647
  setgid(gid);
744
 
  if(ret == -1){
 
648
  if (ret == -1){
745
649
    perror("setgid");
746
650
  }
747
 
  ret = setuid(uid);
748
 
  if(ret == -1){
749
 
    perror("setuid");
750
 
  }
751
 
  
752
 
  /* Open plugin directory with close_on_exec flag */
 
651
  
 
652
  if (plugindir == NULL){
 
653
    dir = opendir(PDIR);
 
654
  } else {
 
655
    dir = opendir(plugindir);
 
656
  }
 
657
  
 
658
  if(dir == NULL){
 
659
    perror("Could not open plugin dir");
 
660
    exitstatus = EXIT_FAILURE;
 
661
    goto fallback;
 
662
  }
 
663
  
 
664
  /* Set the FD_CLOEXEC flag on the directory, if possible */
753
665
  {
754
 
    int dir_fd = -1;
755
 
    if(plugindir == NULL){
756
 
      dir_fd = open(PDIR, O_RDONLY |
757
 
#ifdef O_CLOEXEC
758
 
                    O_CLOEXEC
759
 
#else  /* not O_CLOEXEC */
760
 
                    0
761
 
#endif  /* not O_CLOEXEC */
762
 
                    );
763
 
    } else {
764
 
      dir_fd = open(plugindir, O_RDONLY |
765
 
#ifdef O_CLOEXEC
766
 
                    O_CLOEXEC
767
 
#else  /* not O_CLOEXEC */
768
 
                    0
769
 
#endif  /* not O_CLOEXEC */
770
 
                    );
771
 
    }
772
 
    if(dir_fd == -1){
773
 
      perror("Could not open plugin dir");
774
 
      exitstatus = EXIT_FAILURE;
775
 
      goto fallback;
776
 
    }
777
 
    
778
 
#ifndef O_CLOEXEC
779
 
  /* Set the FD_CLOEXEC flag on the directory */
780
 
    ret = set_cloexec_flag(dir_fd);
781
 
    if(ret < 0){
782
 
      perror("set_cloexec_flag");
783
 
      TEMP_FAILURE_RETRY(close(dir_fd));
784
 
      exitstatus = EXIT_FAILURE;
785
 
      goto fallback;
786
 
    }
787
 
#endif  /* O_CLOEXEC */
788
 
    
789
 
    dir = fdopendir(dir_fd);
790
 
    if(dir == NULL){
791
 
      perror("Could not open plugin dir");
792
 
      TEMP_FAILURE_RETRY(close(dir_fd));
793
 
      exitstatus = EXIT_FAILURE;
794
 
      goto fallback;
 
666
    int dir_fd = dirfd(dir);
 
667
    if(dir_fd >= 0){
 
668
      ret = set_cloexec_flag(dir_fd);
 
669
      if(ret < 0){
 
670
        perror("set_cloexec_flag");
 
671
        exitstatus = EXIT_FAILURE;
 
672
        goto fallback;
 
673
      }
795
674
    }
796
675
  }
797
676
  
799
678
  
800
679
  /* Read and execute any executable in the plugin directory*/
801
680
  while(true){
802
 
    do {
803
 
      dirst = readdir(dir);
804
 
    } while(dirst == NULL and errno == EINTR);
 
681
    dirst = readdir(dir);
805
682
    
806
683
    /* All directory entries have been processed */
807
684
    if(dirst == NULL){
808
 
      if(errno == EBADF){
 
685
      if (errno == EBADF){
809
686
        perror("readdir");
810
687
        exitstatus = EXIT_FAILURE;
811
688
        goto fallback;
823
700
      
824
701
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
825
702
                                           ".dpkg-old",
826
 
                                           ".dpkg-bak",
827
703
                                           ".dpkg-divert", NULL };
828
704
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
829
705
        size_t pre_len = strlen(*pre);
858
734
        continue;
859
735
      }
860
736
    }
861
 
    
 
737
 
862
738
    char *filename;
863
739
    if(plugindir == NULL){
864
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
865
 
                                             dirst->d_name));
 
740
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
866
741
    } else {
867
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
868
 
                                             plugindir,
869
 
                                             dirst->d_name));
 
742
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
870
743
    }
871
744
    if(ret < 0){
872
745
      perror("asprintf");
873
746
      continue;
874
747
    }
875
748
    
876
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
877
 
    if(ret == -1){
 
749
    ret = stat(filename, &st);
 
750
    if (ret == -1){
878
751
      perror("stat");
879
752
      free(filename);
880
753
      continue;
881
754
    }
882
 
    
 
755
 
883
756
    /* Ignore non-executable files */
884
 
    if(not S_ISREG(st.st_mode)
885
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
757
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
886
758
      if(debug){
887
759
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
888
760
                " with bad type or mode\n", filename);
934
806
    }
935
807
    
936
808
    int pipefd[2];
937
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
938
 
    if(ret == -1){
 
809
    ret = pipe(pipefd);
 
810
    if (ret == -1){
939
811
      perror("pipe");
940
812
      exitstatus = EXIT_FAILURE;
941
813
      goto fallback;
954
826
      goto fallback;
955
827
    }
956
828
    /* Block SIGCHLD until process is safely in process list */
957
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
958
 
                                              &sigchld_action.sa_mask,
959
 
                                              NULL));
 
829
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
960
830
    if(ret < 0){
961
831
      perror("sigprocmask");
962
832
      exitstatus = EXIT_FAILURE;
963
833
      goto fallback;
964
834
    }
965
835
    /* Starting a new process to be watched */
966
 
    pid_t pid;
967
 
    do {
968
 
      pid = fork();
969
 
    } while(pid == -1 and errno == EINTR);
 
836
    pid_t pid = fork();
970
837
    if(pid == -1){
971
838
      perror("fork");
972
839
      exitstatus = EXIT_FAILURE;
1010
877
      /* no return */
1011
878
    }
1012
879
    /* Parent process */
1013
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1014
 
                                             pipe */
 
880
    close(pipefd[1]);           /* Close unused write end of pipe */
1015
881
    free(filename);
1016
882
    plugin *new_plugin = getplugin(dirst->d_name);
1017
 
    if(new_plugin == NULL){
 
883
    if (new_plugin == NULL){
1018
884
      perror("getplugin");
1019
 
      ret = (int)(TEMP_FAILURE_RETRY
1020
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1021
 
                               NULL)));
 
885
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1022
886
      if(ret < 0){
1023
887
        perror("sigprocmask");
1024
888
      }
1031
895
    
1032
896
    /* Unblock SIGCHLD so signal handler can be run if this process
1033
897
       has already completed */
1034
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1035
 
                                              &sigchld_action.sa_mask,
1036
 
                                              NULL));
 
898
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1037
899
    if(ret < 0){
1038
900
      perror("sigprocmask");
1039
901
      exitstatus = EXIT_FAILURE;
1040
902
      goto fallback;
1041
903
    }
1042
904
    
1043
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1044
 
                                          -Wconversion */
 
905
    FD_SET(new_plugin->fd, &rfds_all);
1045
906
    
1046
 
    if(maxfd < new_plugin->fd){
 
907
    if (maxfd < new_plugin->fd){
1047
908
      maxfd = new_plugin->fd;
1048
909
    }
1049
910
  }
1050
911
  
1051
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
912
  closedir(dir);
1052
913
  dir = NULL;
1053
 
  free_plugin(getplugin(NULL));
1054
914
  
1055
915
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1056
916
    if(p->pid != 0){
1067
927
  while(plugin_list){
1068
928
    fd_set rfds = rfds_all;
1069
929
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1070
 
    if(select_ret == -1 and errno != EINTR){
 
930
    if (select_ret == -1){
1071
931
      perror("select");
1072
932
      exitstatus = EXIT_FAILURE;
1073
933
      goto fallback;
1076
936
       from one of them */
1077
937
    for(plugin *proc = plugin_list; proc != NULL;){
1078
938
      /* Is this process completely done? */
1079
 
      if(proc->completed and proc->eof){
 
939
      if(proc->eof and proc->completed){
1080
940
        /* Only accept the plugin output if it exited cleanly */
1081
941
        if(not WIFEXITED(proc->status)
1082
942
           or WEXITSTATUS(proc->status) != 0){
1083
943
          /* Bad exit by plugin */
1084
 
          
 
944
 
1085
945
          if(debug){
1086
946
            if(WIFEXITED(proc->status)){
1087
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
1088
 
                      " status %d\n", proc->name,
1089
 
                      (intmax_t) (proc->pid),
 
947
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
948
                      (unsigned int) (proc->pid),
1090
949
                      WEXITSTATUS(proc->status));
1091
 
            } else if(WIFSIGNALED(proc->status)){
1092
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1093
 
                      " signal %d: %s\n", proc->name,
1094
 
                      (intmax_t) (proc->pid),
1095
 
                      WTERMSIG(proc->status),
1096
 
                      strsignal(WTERMSIG(proc->status)));
 
950
            } else if(WIFSIGNALED(proc->status)) {
 
951
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
952
                      (unsigned int) (proc->pid),
 
953
                      WTERMSIG(proc->status));
1097
954
            } else if(WCOREDUMP(proc->status)){
1098
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1099
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
955
              fprintf(stderr, "Plugin %d dumped core\n",
 
956
                      (unsigned int) (proc->pid));
1100
957
            }
1101
958
          }
1102
959
          
1103
960
          /* Remove the plugin */
1104
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion */
1106
 
          
 
961
          FD_CLR(proc->fd, &rfds_all);
 
962
 
1107
963
          /* Block signal while modifying process_list */
1108
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1109
 
                                        (SIG_BLOCK,
1110
 
                                         &sigchld_action.sa_mask,
1111
 
                                         NULL));
 
964
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1112
965
          if(ret < 0){
1113
966
            perror("sigprocmask");
1114
967
            exitstatus = EXIT_FAILURE;
1120
973
          proc = next_plugin;
1121
974
          
1122
975
          /* We are done modifying process list, so unblock signal */
1123
 
          ret = (int)(TEMP_FAILURE_RETRY
1124
 
                      (sigprocmask(SIG_UNBLOCK,
1125
 
                                   &sigchld_action.sa_mask, NULL)));
 
976
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
977
                             NULL);
1126
978
          if(ret < 0){
1127
979
            perror("sigprocmask");
1128
980
            exitstatus = EXIT_FAILURE;
1148
1000
      }
1149
1001
      
1150
1002
      /* This process has not completed.  Does it have any output? */
1151
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1152
 
                                                         warning from
1153
 
                                                         -Wconversion */
 
1003
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1154
1004
        /* This process had nothing to say at this time */
1155
1005
        proc = proc->next;
1156
1006
        continue;
1159
1009
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1160
1010
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1161
1011
                               + (size_t) BUFFER_SIZE);
1162
 
        if(proc->buffer == NULL){
 
1012
        if (proc->buffer == NULL){
1163
1013
          perror("malloc");
1164
1014
          exitstatus = EXIT_FAILURE;
1165
1015
          goto fallback;
1167
1017
        proc->buffer_size += BUFFER_SIZE;
1168
1018
      }
1169
1019
      /* Read from the process */
1170
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1171
 
                                     proc->buffer
1172
 
                                     + proc->buffer_length,
1173
 
                                     BUFFER_SIZE));
1174
 
      if(sret < 0){
 
1020
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1021
                 BUFFER_SIZE);
 
1022
      if(ret < 0){
1175
1023
        /* Read error from this process; ignore the error */
1176
1024
        proc = proc->next;
1177
1025
        continue;
1178
1026
      }
1179
 
      if(sret == 0){
 
1027
      if(ret == 0){
1180
1028
        /* got EOF */
1181
1029
        proc->eof = true;
1182
1030
      } else {
1183
 
        proc->buffer_length += (size_t) sret;
 
1031
        proc->buffer_length += (size_t) ret;
1184
1032
      }
1185
1033
    }
1186
1034
  }
1187
 
  
1188
 
  
 
1035
 
 
1036
 
1189
1037
 fallback:
1190
1038
  
1191
1039
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1238
1086
  }
1239
1087
  
1240
1088
  /* Wait for any remaining child processes to terminate */
1241
 
  do {
 
1089
  do{
1242
1090
    ret = wait(NULL);
1243
1091
  } while(ret >= 0);
1244
1092
  if(errno != ECHILD){