/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-08 06:28:20 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090908062820-xgkdkrnvuze9vc72
* debian/mandos-client.README.Debian: Improved wording and formatting.
                                      Updated location of nfsroot.txt.
* debian/mandos.README.Debian: Improved wording and formatting.

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
    }
245
230
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
246
231
 */
247
232
static int set_cloexec_flag(int fd){
248
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
233
  int ret = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
249
234
  /* If reading the flags failed, return error indication now. */
250
235
  if(ret < 0){
251
236
    return ret;
252
237
  }
253
238
  /* Store modified flag word in the descriptor. */
254
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
255
 
                                       ret | FD_CLOEXEC));
 
239
  return TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, ret | FD_CLOEXEC));
256
240
}
257
241
 
258
242
 
273
257
        /* No child processes */
274
258
        break;
275
259
      }
276
 
      error(0, errno, "waitpid");
 
260
      perror("waitpid");
277
261
    }
278
262
    
279
263
    /* A child exited, find it in process_list */
291
275
}
292
276
 
293
277
/* Prints out a password to stdout */
294
 
__attribute__((nonnull))
295
278
static bool print_out_password(const char *buffer, size_t length){
296
279
  ssize_t ret;
297
280
  for(size_t written = 0; written < length; written += (size_t)ret){
305
288
}
306
289
 
307
290
/* Removes and free a plugin from the plugin list */
308
 
__attribute__((nonnull))
309
291
static void free_plugin(plugin *plugin_node){
310
292
  
311
293
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
366
348
  sigemptyset(&sigchld_action.sa_mask);
367
349
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
368
350
  if(ret == -1){
369
 
    error(0, errno, "sigaddset");
370
 
    exitstatus = EX_OSERR;
 
351
    perror("sigaddset");
 
352
    exitstatus = EXIT_FAILURE;
371
353
    goto fallback;
372
354
  }
373
355
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
374
356
  if(ret == -1){
375
 
    error(0, errno, "sigaction");
376
 
    exitstatus = EX_OSERR;
 
357
    perror("sigaction");
 
358
    exitstatus = EXIT_FAILURE;
377
359
    goto fallback;
378
360
  }
379
361
  
411
393
      .doc = "Group ID the plugins will run as", .group = 3 },
412
394
    { .name = "debug", .key = 132,
413
395
      .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
396
    { .name = NULL }
424
397
  };
425
398
  
426
 
  __attribute__((nonnull(3)))
427
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
428
 
    errno = 0;
 
399
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
400
                    struct argp_state *state){
429
401
    switch(key){
430
402
      char *tmp;
431
 
      intmax_t tmp_id;
 
403
      intmax_t tmpmax;
432
404
    case 'g':                   /* --global-options */
433
 
      {
 
405
      if(arg != NULL){
434
406
        char *plugin_option;
435
407
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
408
          if(plugin_option[0] == '\0'){
 
409
            continue;
 
410
          }
436
411
          if(not add_argument(getplugin(NULL), plugin_option)){
437
 
            break;
 
412
            perror("add_argument");
 
413
            return ARGP_ERR_UNKNOWN;
438
414
          }
439
415
        }
440
416
      }
441
417
      break;
442
418
    case 'G':                   /* --global-env */
443
 
      add_environment(getplugin(NULL), arg, true);
 
419
      if(arg == NULL){
 
420
        break;
 
421
      }
 
422
      if(not add_environment(getplugin(NULL), arg, true)){
 
423
        perror("add_environment");
 
424
      }
444
425
      break;
445
426
    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;
 
427
      if(arg != NULL){
 
428
        char *plugin_name = strsep(&arg, ":");
 
429
        if(plugin_name[0] == '\0'){
 
430
          break;
 
431
        }
 
432
        char *plugin_option;
 
433
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
434
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
435
            perror("add_argument");
 
436
            return ARGP_ERR_UNKNOWN;
464
437
          }
465
438
        }
466
439
      }
467
440
      break;
468
441
    case 'E':                   /* --env-for */
 
442
      if(arg == NULL){
 
443
        break;
 
444
      }
469
445
      {
470
446
        char *envdef = strchr(arg, ':');
471
447
        if(envdef == NULL){
472
 
          argp_error(state, "No colon in \"%s\"", arg);
473
 
          errno = EINVAL;
474
448
          break;
475
449
        }
476
450
        *envdef = '\0';
477
 
        envdef++;
478
 
        if(arg[0] == '\0'){
479
 
          argp_error(state, "Empty plugin name");
480
 
          errno = EINVAL;
481
 
          break;
 
451
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
452
          perror("add_environment");
482
453
        }
483
 
        add_environment(getplugin(arg), envdef, true);
484
454
      }
485
455
      break;
486
456
    case 'd':                   /* --disable */
487
 
      {
 
457
      if(arg != NULL){
488
458
        plugin *p = getplugin(arg);
489
 
        if(p != NULL){
490
 
          p->disabled = true;
 
459
        if(p == NULL){
 
460
          return ARGP_ERR_UNKNOWN;
491
461
        }
 
462
        p->disabled = true;
492
463
      }
493
464
      break;
494
465
    case 'e':                   /* --enable */
495
 
      {
 
466
      if(arg != NULL){
496
467
        plugin *p = getplugin(arg);
497
 
        if(p != NULL){
498
 
          p->disabled = false;
 
468
        if(p == NULL){
 
469
          return ARGP_ERR_UNKNOWN;
499
470
        }
 
471
        p->disabled = false;
500
472
      }
501
473
      break;
502
474
    case 128:                   /* --plugin-dir */
503
475
      free(plugindir);
504
476
      plugindir = strdup(arg);
 
477
      if(plugindir == NULL){
 
478
        perror("strdup");
 
479
      }
505
480
      break;
506
481
    case 129:                   /* --config-file */
507
482
      /* This is already done by parse_opt_config_file() */
508
483
      break;
509
484
    case 130:                   /* --userid */
510
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
485
      errno = 0;
 
486
      tmpmax = strtoimax(arg, &tmp, 10);
511
487
      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;
 
488
         or tmpmax != (uid_t)tmpmax){
 
489
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
490
                PRIdMAX "\n", arg, (intmax_t)uid);
 
491
      } else {
 
492
        uid = (uid_t)tmpmax;
516
493
      }
517
 
      uid = (uid_t)tmp_id;
518
494
      break;
519
495
    case 131:                   /* --groupid */
520
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
496
      errno = 0;
 
497
      tmpmax = strtoimax(arg, &tmp, 10);
521
498
      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;
 
499
         or tmpmax != (gid_t)tmpmax){
 
500
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
501
                PRIdMAX "\n", arg, (intmax_t)gid);
 
502
      } else {
 
503
        gid = (gid_t)tmpmax;
526
504
      }
527
 
      gid = (gid_t)tmp_id;
528
505
      break;
529
506
    case 132:                   /* --debug */
530
507
      debug = true;
531
508
      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
509
/*
547
510
 * When adding more options before this line, remember to also add a
548
511
 * "case" to the "parse_opt_config_file" function below.
551
514
      /* Cryptsetup always passes an argument, which is an empty
552
515
         string if "none" was specified in /etc/crypttab.  So if
553
516
         argument was empty, we ignore it silently. */
554
 
      if(arg[0] == '\0'){
555
 
        break;
 
517
      if(arg[0] != '\0'){
 
518
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
556
519
      }
 
520
      break;
 
521
    case ARGP_KEY_END:
 
522
      break;
557
523
    default:
558
524
      return ARGP_ERR_UNKNOWN;
559
525
    }
560
 
    return errno;               /* Set to 0 at start */
 
526
    return 0;
561
527
  }
562
528
  
563
529
  /* This option parser is the same as parse_opt() above, except it
565
531
  error_t parse_opt_config_file(int key, char *arg,
566
532
                                __attribute__((unused))
567
533
                                struct argp_state *state){
568
 
    errno = 0;
569
534
    switch(key){
570
535
    case 'g':                   /* --global-options */
571
536
    case 'G':                   /* --global-env */
578
543
    case 129:                   /* --config-file */
579
544
      free(argfile);
580
545
      argfile = strdup(arg);
 
546
      if(argfile == NULL){
 
547
        perror("strdup");
 
548
      }
581
549
      break;
582
550
    case 130:                   /* --userid */
583
551
    case 131:                   /* --groupid */
584
552
    case 132:                   /* --debug */
585
 
    case '?':                   /* --help */
586
 
    case -3:                    /* --usage */
587
 
    case 'V':                   /* --version */
588
553
    case ARGP_KEY_ARG:
 
554
    case ARGP_KEY_END:
589
555
      break;
590
556
    default:
591
557
      return ARGP_ERR_UNKNOWN;
592
558
    }
593
 
    return errno;
 
559
    return 0;
594
560
  }
595
561
  
596
562
  struct argp argp = { .options = options,
600
566
  
601
567
  /* Parse using parse_opt_config_file() in order to get the custom
602
568
     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;
 
569
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
570
  if(ret == ARGP_ERR_UNKNOWN){
 
571
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
572
    exitstatus = EXIT_FAILURE;
617
573
    goto fallback;
618
574
  }
619
575
  
636
592
    custom_argc = 1;
637
593
    custom_argv = malloc(sizeof(char*) * 2);
638
594
    if(custom_argv == NULL){
639
 
      error(0, errno, "malloc");
640
 
      exitstatus = EX_OSERR;
 
595
      perror("malloc");
 
596
      exitstatus = EXIT_FAILURE;
641
597
      goto fallback;
642
598
    }
643
599
    custom_argv[0] = argv[0];
659
615
        }
660
616
        new_arg = strdup(p);
661
617
        if(new_arg == NULL){
662
 
          error(0, errno, "strdup");
663
 
          exitstatus = EX_OSERR;
 
618
          perror("strdup");
 
619
          exitstatus = EXIT_FAILURE;
664
620
          free(org_line);
665
621
          goto fallback;
666
622
        }
667
623
        
668
624
        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
 
          }
 
625
        custom_argv = realloc(custom_argv, sizeof(char *)
 
626
                              * ((unsigned int) custom_argc + 1));
 
627
        if(custom_argv == NULL){
 
628
          perror("realloc");
 
629
          exitstatus = EXIT_FAILURE;
 
630
          free(org_line);
 
631
          goto fallback;
682
632
        }
683
633
        custom_argv[custom_argc-1] = new_arg;
684
634
        custom_argv[custom_argc] = NULL;
688
638
      ret = fclose(conffp);
689
639
    } while(ret == EOF and errno == EINTR);
690
640
    if(ret == EOF){
691
 
      error(0, errno, "fclose");
692
 
      exitstatus = EX_IOERR;
 
641
      perror("fclose");
 
642
      exitstatus = EXIT_FAILURE;
693
643
      goto fallback;
694
644
    }
695
645
    free(org_line);
697
647
    /* Check for harmful errors and go to fallback. Other errors might
698
648
       not affect opening plugins */
699
649
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
700
 
      error(0, errno, "fopen");
701
 
      exitstatus = EX_OSERR;
 
650
      perror("fopen");
 
651
      exitstatus = EXIT_FAILURE;
702
652
      goto fallback;
703
653
    }
704
654
  }
705
 
  /* If there were any arguments from the configuration file, pass
706
 
     them to parser as command line arguments */
 
655
  /* If there was any arguments from configuration file,
 
656
     pass them to parser as command arguments */
707
657
  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;
 
658
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
659
                     0, NULL);
 
660
    if(ret == ARGP_ERR_UNKNOWN){
 
661
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
662
      exitstatus = EXIT_FAILURE;
722
663
      goto fallback;
723
664
    }
724
665
  }
725
666
  
726
667
  /* Parse actual command line arguments, to let them override the
727
668
     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;
 
669
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
670
  if(ret == ARGP_ERR_UNKNOWN){
 
671
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
672
    exitstatus = EXIT_FAILURE;
742
673
    goto fallback;
743
674
  }
744
675
  
756
687
    }
757
688
  }
758
689
  
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);
 
690
  /* Strip permissions down to nobody */
 
691
  setgid(gid);
783
692
  if(ret == -1){
784
 
    error(0, errno, "setgid");
 
693
    perror("setgid");
785
694
  }
786
695
  ret = setuid(uid);
787
696
  if(ret == -1){
788
 
    error(0, errno, "setuid");
789
 
  }
790
 
  
791
 
  /* Open plugin directory with close_on_exec flag */
 
697
    perror("setuid");
 
698
  }
 
699
  
 
700
  if(plugindir == NULL){
 
701
    dir = opendir(PDIR);
 
702
  } else {
 
703
    dir = opendir(plugindir);
 
704
  }
 
705
  
 
706
  if(dir == NULL){
 
707
    perror("Could not open plugin dir");
 
708
    exitstatus = EXIT_FAILURE;
 
709
    goto fallback;
 
710
  }
 
711
  
 
712
  /* Set the FD_CLOEXEC flag on the directory, if possible */
792
713
  {
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;
 
714
    int dir_fd = dirfd(dir);
 
715
    if(dir_fd >= 0){
 
716
      ret = set_cloexec_flag(dir_fd);
 
717
      if(ret < 0){
 
718
        perror("set_cloexec_flag");
 
719
        exitstatus = EXIT_FAILURE;
 
720
        goto fallback;
 
721
      }
834
722
    }
835
723
  }
836
724
  
845
733
    /* All directory entries have been processed */
846
734
    if(dirst == NULL){
847
735
      if(errno == EBADF){
848
 
        error(0, errno, "readdir");
849
 
        exitstatus = EX_IOERR;
 
736
        perror("readdir");
 
737
        exitstatus = EXIT_FAILURE;
850
738
        goto fallback;
851
739
      }
852
740
      break;
858
746
    {
859
747
      bool bad_name = false;
860
748
      
861
 
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
749
      const char const *bad_prefixes[] = { ".", "#", NULL };
862
750
      
863
 
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
751
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
864
752
                                           ".dpkg-old",
865
753
                                           ".dpkg-bak",
866
754
                                           ".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
 
755
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
876
756
        size_t pre_len = strlen(*pre);
877
757
        if((d_name_len >= pre_len)
878
758
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
887
767
      if(bad_name){
888
768
        continue;
889
769
      }
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
 
770
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
899
771
        size_t suf_len = strlen(*suf);
900
772
        if((d_name_len >= suf_len)
901
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
773
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
902
774
                == 0)){
903
775
          if(debug){
904
776
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
916
788
    
917
789
    char *filename;
918
790
    if(plugindir == NULL){
919
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
920
 
                                             dirst->d_name));
 
791
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
792
                                        dirst->d_name));
921
793
    } else {
922
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
923
 
                                             plugindir,
924
 
                                             dirst->d_name));
 
794
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
 
795
                                        dirst->d_name));
925
796
    }
926
797
    if(ret < 0){
927
 
      error(0, errno, "asprintf");
 
798
      perror("asprintf");
928
799
      continue;
929
800
    }
930
801
    
931
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
802
    ret = TEMP_FAILURE_RETRY(stat(filename, &st));
932
803
    if(ret == -1){
933
 
      error(0, errno, "stat");
 
804
      perror("stat");
934
805
      free(filename);
935
806
      continue;
936
807
    }
948
819
    
949
820
    plugin *p = getplugin(dirst->d_name);
950
821
    if(p == NULL){
951
 
      error(0, errno, "getplugin");
 
822
      perror("getplugin");
952
823
      free(filename);
953
824
      continue;
954
825
    }
966
837
      if(g != NULL){
967
838
        for(char **a = g->argv + 1; *a != NULL; a++){
968
839
          if(not add_argument(p, *a)){
969
 
            error(0, errno, "add_argument");
 
840
            perror("add_argument");
970
841
          }
971
842
        }
972
843
        /* Add global environment variables */
973
844
        for(char **e = g->environ; *e != NULL; e++){
974
845
          if(not add_environment(p, *e, false)){
975
 
            error(0, errno, "add_environment");
 
846
            perror("add_environment");
976
847
          }
977
848
        }
978
849
      }
983
854
    if(p->environ[0] != NULL){
984
855
      for(char **e = environ; *e != NULL; e++){
985
856
        if(not add_environment(p, *e, false)){
986
 
          error(0, errno, "add_environment");
 
857
          perror("add_environment");
987
858
        }
988
859
      }
989
860
    }
990
861
    
991
862
    int pipefd[2];
992
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
863
    ret = TEMP_FAILURE_RETRY(pipe(pipefd));
993
864
    if(ret == -1){
994
 
      error(0, errno, "pipe");
995
 
      exitstatus = EX_OSERR;
 
865
      perror("pipe");
 
866
      exitstatus = EXIT_FAILURE;
996
867
      goto fallback;
997
868
    }
998
869
    /* Ask OS to automatic close the pipe on exec */
999
870
    ret = set_cloexec_flag(pipefd[0]);
1000
871
    if(ret < 0){
1001
 
      error(0, errno, "set_cloexec_flag");
1002
 
      exitstatus = EX_OSERR;
 
872
      perror("set_cloexec_flag");
 
873
      exitstatus = EXIT_FAILURE;
1003
874
      goto fallback;
1004
875
    }
1005
876
    ret = set_cloexec_flag(pipefd[1]);
1006
877
    if(ret < 0){
1007
 
      error(0, errno, "set_cloexec_flag");
1008
 
      exitstatus = EX_OSERR;
 
878
      perror("set_cloexec_flag");
 
879
      exitstatus = EXIT_FAILURE;
1009
880
      goto fallback;
1010
881
    }
1011
882
    /* Block SIGCHLD until process is safely in process list */
1012
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1013
 
                                              &sigchld_action.sa_mask,
1014
 
                                              NULL));
 
883
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
884
                                         &sigchld_action.sa_mask,
 
885
                                         NULL));
1015
886
    if(ret < 0){
1016
 
      error(0, errno, "sigprocmask");
1017
 
      exitstatus = EX_OSERR;
 
887
      perror("sigprocmask");
 
888
      exitstatus = EXIT_FAILURE;
1018
889
      goto fallback;
1019
890
    }
1020
891
    /* Starting a new process to be watched */
1023
894
      pid = fork();
1024
895
    } while(pid == -1 and errno == EINTR);
1025
896
    if(pid == -1){
1026
 
      error(0, errno, "fork");
1027
 
      exitstatus = EX_OSERR;
 
897
      perror("fork");
 
898
      exitstatus = EXIT_FAILURE;
1028
899
      goto fallback;
1029
900
    }
1030
901
    if(pid == 0){
1031
902
      /* this is the child process */
1032
903
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1033
904
      if(ret < 0){
1034
 
        error(0, errno, "sigaction");
1035
 
        _exit(EX_OSERR);
 
905
        perror("sigaction");
 
906
        _exit(EXIT_FAILURE);
1036
907
      }
1037
908
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1038
909
      if(ret < 0){
1039
 
        error(0, errno, "sigprocmask");
1040
 
        _exit(EX_OSERR);
 
910
        perror("sigprocmask");
 
911
        _exit(EXIT_FAILURE);
1041
912
      }
1042
913
      
1043
914
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
1044
915
      if(ret == -1){
1045
 
        error(0, errno, "dup2");
1046
 
        _exit(EX_OSERR);
 
916
        perror("dup2");
 
917
        _exit(EXIT_FAILURE);
1047
918
      }
1048
919
      
1049
920
      if(dirfd(dir) < 0){
1053
924
      }
1054
925
      if(p->environ[0] == NULL){
1055
926
        if(execv(filename, p->argv) < 0){
1056
 
          error(0, errno, "execv for %s", filename);
1057
 
          _exit(EX_OSERR);
 
927
          perror("execv");
 
928
          _exit(EXIT_FAILURE);
1058
929
        }
1059
930
      } else {
1060
931
        if(execve(filename, p->argv, p->environ) < 0){
1061
 
          error(0, errno, "execve for %s", filename);
1062
 
          _exit(EX_OSERR);
 
932
          perror("execve");
 
933
          _exit(EXIT_FAILURE);
1063
934
        }
1064
935
      }
1065
936
      /* no return */
1070
941
    free(filename);
1071
942
    plugin *new_plugin = getplugin(dirst->d_name);
1072
943
    if(new_plugin == NULL){
1073
 
      error(0, errno, "getplugin");
1074
 
      ret = (int)(TEMP_FAILURE_RETRY
1075
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1076
 
                               NULL)));
 
944
      perror("getplugin");
 
945
      ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
946
                                           &sigchld_action.sa_mask,
 
947
                                           NULL));
1077
948
      if(ret < 0){
1078
 
        error(0, errno, "sigprocmask");
 
949
        perror("sigprocmask");
1079
950
      }
1080
 
      exitstatus = EX_OSERR;
 
951
      exitstatus = EXIT_FAILURE;
1081
952
      goto fallback;
1082
953
    }
1083
954
    
1086
957
    
1087
958
    /* Unblock SIGCHLD so signal handler can be run if this process
1088
959
       has already completed */
1089
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1090
 
                                              &sigchld_action.sa_mask,
1091
 
                                              NULL));
 
960
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
961
                                         &sigchld_action.sa_mask,
 
962
                                         NULL));
1092
963
    if(ret < 0){
1093
 
      error(0, errno, "sigprocmask");
1094
 
      exitstatus = EX_OSERR;
 
964
      perror("sigprocmask");
 
965
      exitstatus = EXIT_FAILURE;
1095
966
      goto fallback;
1096
967
    }
1097
968
    
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
 
969
    FD_SET(new_plugin->fd, &rfds_all);
1112
970
    
1113
971
    if(maxfd < new_plugin->fd){
1114
972
      maxfd = new_plugin->fd;
1135
993
    fd_set rfds = rfds_all;
1136
994
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1137
995
    if(select_ret == -1 and errno != EINTR){
1138
 
      error(0, errno, "select");
1139
 
      exitstatus = EX_OSERR;
 
996
      perror("select");
 
997
      exitstatus = EXIT_FAILURE;
1140
998
      goto fallback;
1141
999
    }
1142
1000
    /* OK, now either a process completed, or something can be read
1168
1026
          }
1169
1027
          
1170
1028
          /* 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
 
1029
          FD_CLR(proc->fd, &rfds_all);
1185
1030
          
1186
1031
          /* Block signal while modifying process_list */
1187
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1188
 
                                        (SIG_BLOCK,
1189
 
                                         &sigchld_action.sa_mask,
1190
 
                                         NULL));
 
1032
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
1033
                                               &sigchld_action.sa_mask,
 
1034
                                               NULL));
1191
1035
          if(ret < 0){
1192
 
            error(0, errno, "sigprocmask");
1193
 
            exitstatus = EX_OSERR;
 
1036
            perror("sigprocmask");
 
1037
            exitstatus = EXIT_FAILURE;
1194
1038
            goto fallback;
1195
1039
          }
1196
1040
          
1199
1043
          proc = next_plugin;
1200
1044
          
1201
1045
          /* We are done modifying process list, so unblock signal */
1202
 
          ret = (int)(TEMP_FAILURE_RETRY
1203
 
                      (sigprocmask(SIG_UNBLOCK,
1204
 
                                   &sigchld_action.sa_mask, NULL)));
 
1046
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1047
                                               &sigchld_action.sa_mask,
 
1048
                                               NULL));
1205
1049
          if(ret < 0){
1206
 
            error(0, errno, "sigprocmask");
1207
 
            exitstatus = EX_OSERR;
 
1050
            perror("sigprocmask");
 
1051
            exitstatus = EXIT_FAILURE;
1208
1052
            goto fallback;
1209
1053
          }
1210
1054
          
1220
1064
        bool bret = print_out_password(proc->buffer,
1221
1065
                                       proc->buffer_length);
1222
1066
        if(not bret){
1223
 
          error(0, errno, "print_out_password");
1224
 
          exitstatus = EX_IOERR;
 
1067
          perror("print_out_password");
 
1068
          exitstatus = EXIT_FAILURE;
1225
1069
        }
1226
1070
        goto fallback;
1227
1071
      }
1228
1072
      
1229
1073
      /* 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
 
1074
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1247
1075
        /* This process had nothing to say at this time */
1248
1076
        proc = proc->next;
1249
1077
        continue;
1250
1078
      }
1251
1079
      /* Before reading, make the process' data buffer large enough */
1252
1080
      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;
 
1081
        proc->buffer = realloc(proc->buffer, proc->buffer_size
 
1082
                               + (size_t) BUFFER_SIZE);
 
1083
        if(proc->buffer == NULL){
 
1084
          perror("malloc");
 
1085
          exitstatus = EXIT_FAILURE;
1258
1086
          goto fallback;
1259
1087
        }
1260
 
        proc->buffer = new_buffer;
1261
1088
        proc->buffer_size += BUFFER_SIZE;
1262
1089
      }
1263
1090
      /* Read from the process */
1282
1109
  
1283
1110
 fallback:
1284
1111
  
1285
 
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
1286
 
                             and exitstatus != EX_OK)){
 
1112
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1287
1113
    /* Fallback if all plugins failed, none are found or an error
1288
1114
       occured */
1289
1115
    bool bret;
1297
1123
    }
1298
1124
    bret = print_out_password(passwordbuffer, len);
1299
1125
    if(not bret){
1300
 
      error(0, errno, "print_out_password");
1301
 
      exitstatus = EX_IOERR;
 
1126
      perror("print_out_password");
 
1127
      exitstatus = EXIT_FAILURE;
1302
1128
    }
1303
1129
  }
1304
1130
  
1305
1131
  /* Restore old signal handler */
1306
1132
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1307
1133
  if(ret == -1){
1308
 
    error(0, errno, "sigaction");
1309
 
    exitstatus = EX_OSERR;
 
1134
    perror("sigaction");
 
1135
    exitstatus = EXIT_FAILURE;
1310
1136
  }
1311
1137
  
1312
1138
  if(custom_argv != NULL){
1327
1153
      ret = kill(p->pid, SIGTERM);
1328
1154
      if(ret == -1 and errno != ESRCH){
1329
1155
        /* Set-uid proccesses might not get closed */
1330
 
        error(0, errno, "kill");
 
1156
        perror("kill");
1331
1157
      }
1332
1158
    }
1333
1159
  }
1337
1163
    ret = wait(NULL);
1338
1164
  } while(ret >= 0);
1339
1165
  if(errno != ECHILD){
1340
 
    error(0, errno, "wait");
 
1166
    perror("wait");
1341
1167
  }
1342
1168
  
1343
1169
  free_plugin_list();