/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: 2021-02-01 19:30:45 UTC
  • Revision ID: teddy@recompile.se-20210201193045-lpg6aprpc4srem6k
Fix issue with french translation

Initial white space was missing in both msgid and msgstr of the french
translation, leading to checking tools reporing an incomplete
translation.  The string is a raw command line command, and therefore
did not need translation, so this was never a user-visible issue.

* debian/po/fr.po: Add missing whitespace to the id and translation
  for msgid " mandos-keygen -F/dev/null|grep ^key_id".

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
7
 
 * 
8
 
 * This program is free software: you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation, either version 3 of the
11
 
 * License, or (at your option) any later version.
12
 
 * 
13
 
 * This program is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008-2020 Teddy Hogeborn
 
6
 * Copyright © 2008-2020 Björn Påhlsson
 
7
 * 
 
8
 * This file is part of Mandos.
 
9
 * 
 
10
 * Mandos is free software: you can redistribute it and/or modify it
 
11
 * under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 * 
 
15
 * Mandos is distributed in the hope that it will be useful, but
14
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
18
 * General Public License for more details.
17
19
 * 
18
20
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see
20
 
 * <http://www.gnu.org/licenses/>.
 
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
21
22
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
23
 * Contact the authors at <mandos@recompile.se>.
23
24
 */
24
25
 
25
26
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
27
                                   O_CLOEXEC, pipe2() */
27
28
#include <stddef.h>             /* size_t, NULL */
28
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
 
                                   realloc() */
 
29
#include <stdlib.h>             /* malloc(), reallocarray(), realloc(),
 
30
                                   EXIT_SUCCESS, exit() */
30
31
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
 
                                   stat, waitpid(), WIFEXITED(),
35
 
                                   WEXITSTATUS(), wait(), pid_t,
36
 
                                   uid_t, gid_t, getuid(), getgid(),
37
 
                                   dirfd() */
 
32
#include <stdio.h>              /* fileno(), fprintf(),
 
33
                                   stderr, STDOUT_FILENO, fclose() */
 
34
#include <sys/types.h>          /* fstat(), struct stat, waitpid(),
 
35
                                   WIFEXITED(), WEXITSTATUS(), wait(),
 
36
                                   pid_t, uid_t, gid_t, getuid(),
 
37
                                   getgid() */
38
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG(),
42
 
                                   WCOREDUMP() */
43
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
41
                                   WEXITSTATUS(), WTERMSIG() */
 
42
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
44
43
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
 
                                   readdir(), closedir(), dirfd() */
47
 
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
 
                                   fcntl(), setuid(), setgid(),
49
 
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
50
 
                                   access(), pipe(), fork(), close()
51
 
                                   dup2(), STDOUT_FILENO, _exit(),
52
 
                                   execv(), write(), read(),
53
 
                                   close() */
 
44
#include <dirent.h>             /* struct dirent, scandirat() */
 
45
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
 
46
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
 
47
                                   struct stat, fstat(), close(),
 
48
                                   setgid(), setuid(), S_ISREG(),
 
49
                                   faccessat() pipe2(), fork(),
 
50
                                   _exit(), dup2(), fexecve(), read(),
 
51
                                   lstat(), symlink() */
54
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
 
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
53
                                   FD_CLOEXEC, openat(), scandirat(),
 
54
                                   pipe2() */
 
55
#include <string.h>             /* strsep, strlen(), strsignal(),
 
56
                                   strcmp(), strncmp() */
58
57
#include <errno.h>              /* errno */
59
58
#include <argp.h>               /* struct argp_option, struct
60
59
                                   argp_state, struct argp,
70
69
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
70
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
72
71
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
 
72
#include <errno.h>              /* errno */
 
73
#include <error.h>              /* error() */
 
74
#include <fnmatch.h>            /* fnmatch() */
73
75
 
74
76
#define BUFFER_SIZE 256
75
77
 
76
78
#define PDIR "/lib/mandos/plugins.d"
 
79
#define PHDIR "/lib/mandos/plugin-helpers"
77
80
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
78
81
 
79
82
const char *argp_program_version = "plugin-runner " VERSION;
80
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
83
const char *argp_program_bug_address = "<mandos@recompile.se>";
81
84
 
82
85
typedef struct plugin{
83
86
  char *name;                   /* can be NULL or any plugin name */
103
106
 
104
107
/* Gets an existing plugin based on name,
105
108
   or if none is found, creates a new one */
 
109
__attribute__((warn_unused_result))
106
110
static plugin *getplugin(char *name){
107
111
  /* Check for existing plugin with that name */
108
112
  for(plugin *p = plugin_list; p != NULL; p = p->next){
169
173
}
170
174
 
171
175
/* Helper function for add_argument and add_environment */
 
176
__attribute__((nonnull, warn_unused_result))
172
177
static bool add_to_char_array(const char *new, char ***array,
173
178
                              int *len){
174
179
  /* Resize the pointed-to array to hold one more pointer */
 
180
  char **new_array = NULL;
175
181
  do {
176
 
    *array = realloc(*array, sizeof(char *)
177
 
                     * (size_t) ((*len) + 2));
178
 
  } while(*array == NULL and errno == EINTR);
 
182
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
 
183
    new_array = reallocarray(*array, (size_t)((*len) + 2),
 
184
                             sizeof(char *));
 
185
#else
 
186
    if(((size_t)((*len) + 2)) > (SIZE_MAX / sizeof(char *))){
 
187
      /* overflow */
 
188
      new_array = NULL;
 
189
      errno = ENOMEM;
 
190
    } else {
 
191
      new_array = realloc(*array, (size_t)((*len) + 2)
 
192
                          * sizeof(char *));
 
193
    }
 
194
#endif
 
195
  } while(new_array == NULL and errno == EINTR);
179
196
  /* Malloc check */
180
 
  if(*array == NULL){
 
197
  if(new_array == NULL){
181
198
    return false;
182
199
  }
 
200
  *array = new_array;
183
201
  /* Make a copy of the new string */
184
202
  char *copy;
185
203
  do {
197
215
}
198
216
 
199
217
/* Add to a plugin's argument vector */
 
218
__attribute__((nonnull(2), warn_unused_result))
200
219
static bool add_argument(plugin *p, const char *arg){
201
220
  if(p == NULL){
202
221
    return false;
205
224
}
206
225
 
207
226
/* Add to a plugin's environment */
 
227
__attribute__((nonnull(2), warn_unused_result))
208
228
static bool add_environment(plugin *p, const char *def, bool replace){
209
229
  if(p == NULL){
210
230
    return false;
212
232
  /* namelen = length of name of environment variable */
213
233
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
214
234
  /* Search for this environment variable */
215
 
  for(char **e = p->environ; *e != NULL; e++){
216
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
235
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
236
    if(strncmp(*envdef, def, namelen + 1) == 0){
217
237
      /* It already exists */
218
238
      if(replace){
219
 
        char *new;
 
239
        char *new_envdef;
220
240
        do {
221
 
          new = realloc(*e, strlen(def) + 1);
222
 
        } while(new == NULL and errno == EINTR);
223
 
        if(new == NULL){
 
241
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
242
        } while(new_envdef == NULL and errno == EINTR);
 
243
        if(new_envdef == NULL){
224
244
          return false;
225
245
        }
226
 
        *e = new;
227
 
        strcpy(*e, def);
 
246
        *envdef = new_envdef;
 
247
        strcpy(*envdef, def);
228
248
      }
229
249
      return true;
230
250
    }
232
252
  return add_to_char_array(def, &(p->environ), &(p->envc));
233
253
}
234
254
 
 
255
#ifndef O_CLOEXEC
235
256
/*
236
257
 * Based on the example in the GNU LibC manual chapter 13.13 "File
237
258
 * Descriptor Flags".
238
259
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
239
260
 */
 
261
__attribute__((warn_unused_result))
240
262
static int set_cloexec_flag(int fd){
241
263
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
242
264
  /* If reading the flags failed, return error indication now. */
247
269
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
248
270
                                       ret | FD_CLOEXEC));
249
271
}
 
272
#endif  /* not O_CLOEXEC */
250
273
 
251
274
 
252
275
/* Mark processes as completed when they exit, and save their exit
266
289
        /* No child processes */
267
290
        break;
268
291
      }
269
 
      perror("waitpid");
 
292
      error(0, errno, "waitpid");
270
293
    }
271
294
    
272
295
    /* A child exited, find it in process_list */
284
307
}
285
308
 
286
309
/* Prints out a password to stdout */
 
310
__attribute__((nonnull, warn_unused_result))
287
311
static bool print_out_password(const char *buffer, size_t length){
288
312
  ssize_t ret;
289
313
  for(size_t written = 0; written < length; written += (size_t)ret){
297
321
}
298
322
 
299
323
/* Removes and free a plugin from the plugin list */
 
324
__attribute__((nonnull))
300
325
static void free_plugin(plugin *plugin_node){
301
326
  
302
 
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
 
327
  for(char **arg = (plugin_node->argv)+1; *arg != NULL; arg++){
303
328
    free(*arg);
304
329
  }
 
330
  free(plugin_node->name);
305
331
  free(plugin_node->argv);
306
332
  for(char **env = plugin_node->environ; *env != NULL; env++){
307
333
    free(*env);
334
360
 
335
361
int main(int argc, char *argv[]){
336
362
  char *plugindir = NULL;
 
363
  char *pluginhelperdir = NULL;
337
364
  char *argfile = NULL;
338
365
  FILE *conffp;
339
 
  size_t d_name_len;
340
 
  DIR *dir = NULL;
341
 
  struct dirent *dirst;
 
366
  struct dirent **direntries = NULL;
342
367
  struct stat st;
343
368
  fd_set rfds_all;
344
369
  int ret, maxfd = 0;
352
377
                                      .sa_flags = SA_NOCLDSTOP };
353
378
  char **custom_argv = NULL;
354
379
  int custom_argc = 0;
 
380
  int dir_fd = -1;
355
381
  
356
382
  /* Establish a signal handler */
357
383
  sigemptyset(&sigchld_action.sa_mask);
358
384
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
359
385
  if(ret == -1){
360
 
    perror("sigaddset");
 
386
    error(0, errno, "sigaddset");
361
387
    exitstatus = EX_OSERR;
362
388
    goto fallback;
363
389
  }
364
390
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
365
391
  if(ret == -1){
366
 
    perror("sigaction");
 
392
    error(0, errno, "sigaction");
367
393
    exitstatus = EX_OSERR;
368
394
    goto fallback;
369
395
  }
402
428
      .doc = "Group ID the plugins will run as", .group = 3 },
403
429
    { .name = "debug", .key = 132,
404
430
      .doc = "Debug mode", .group = 4 },
 
431
    { .name = "plugin-helper-dir", .key = 133,
 
432
      .arg = "DIRECTORY",
 
433
      .doc = "Specify a different plugin helper directory",
 
434
      .group = 2 },
405
435
    /*
406
436
     * These reproduce what we would get without ARGP_NO_HELP
407
437
     */
414
444
    { .name = NULL }
415
445
  };
416
446
  
 
447
  __attribute__((nonnull(3)))
417
448
  error_t parse_opt(int key, char *arg, struct argp_state *state){
418
449
    errno = 0;
419
450
    switch(key){
420
451
      char *tmp;
421
 
      intmax_t tmpmax;
 
452
      intmax_t tmp_id;
422
453
    case 'g':                   /* --global-options */
423
454
      {
424
455
        char *plugin_option;
427
458
            break;
428
459
          }
429
460
        }
 
461
        errno = 0;
430
462
      }
431
463
      break;
432
464
    case 'G':                   /* --global-env */
433
 
      add_environment(getplugin(NULL), arg, true);
 
465
      if(add_environment(getplugin(NULL), arg, true)){
 
466
        errno = 0;
 
467
      }
434
468
      break;
435
469
    case 'o':                   /* --options-for */
436
470
      {
453
487
            break;
454
488
          }
455
489
        }
 
490
        errno = 0;
456
491
      }
457
492
      break;
458
493
    case 'E':                   /* --env-for */
470
505
          errno = EINVAL;
471
506
          break;
472
507
        }
473
 
        add_environment(getplugin(arg), envdef, true);
 
508
        if(add_environment(getplugin(arg), envdef, true)){
 
509
          errno = 0;
 
510
        }
474
511
      }
475
512
      break;
476
513
    case 'd':                   /* --disable */
478
515
        plugin *p = getplugin(arg);
479
516
        if(p != NULL){
480
517
          p->disabled = true;
 
518
          errno = 0;
481
519
        }
482
520
      }
483
521
      break;
486
524
        plugin *p = getplugin(arg);
487
525
        if(p != NULL){
488
526
          p->disabled = false;
 
527
          errno = 0;
489
528
        }
490
529
      }
491
530
      break;
492
531
    case 128:                   /* --plugin-dir */
493
532
      free(plugindir);
494
533
      plugindir = strdup(arg);
 
534
      if(plugindir != NULL){
 
535
        errno = 0;
 
536
      }
495
537
      break;
496
538
    case 129:                   /* --config-file */
497
539
      /* This is already done by parse_opt_config_file() */
498
540
      break;
499
541
    case 130:                   /* --userid */
500
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
542
      tmp_id = strtoimax(arg, &tmp, 10);
501
543
      if(errno != 0 or tmp == arg or *tmp != '\0'
502
 
         or tmpmax != (uid_t)tmpmax){
 
544
         or tmp_id != (uid_t)tmp_id){
503
545
        argp_error(state, "Bad user ID number: \"%s\", using %"
504
546
                   PRIdMAX, arg, (intmax_t)uid);
505
547
        break;
506
548
      }
507
 
      uid = (uid_t)tmpmax;
 
549
      uid = (uid_t)tmp_id;
 
550
      errno = 0;
508
551
      break;
509
552
    case 131:                   /* --groupid */
510
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
553
      tmp_id = strtoimax(arg, &tmp, 10);
511
554
      if(errno != 0 or tmp == arg or *tmp != '\0'
512
 
         or tmpmax != (gid_t)tmpmax){
 
555
         or tmp_id != (gid_t)tmp_id){
513
556
        argp_error(state, "Bad group ID number: \"%s\", using %"
514
557
                   PRIdMAX, arg, (intmax_t)gid);
515
558
        break;
516
559
      }
517
 
      gid = (gid_t)tmpmax;
 
560
      gid = (gid_t)tmp_id;
 
561
      errno = 0;
518
562
      break;
519
563
    case 132:                   /* --debug */
520
564
      debug = true;
521
565
      break;
 
566
    case 133:                   /* --plugin-helper-dir */
 
567
      free(pluginhelperdir);
 
568
      pluginhelperdir = strdup(arg);
 
569
      if(pluginhelperdir != NULL){
 
570
        errno = 0;
 
571
      }
 
572
      break;
522
573
      /*
523
574
       * These reproduce what we would get without ARGP_NO_HELP
524
575
       */
525
576
    case '?':                   /* --help */
526
577
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
527
578
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
579
      __builtin_unreachable();
528
580
    case -3:                    /* --usage */
529
581
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
530
582
      argp_state_help(state, state->out_stream,
531
583
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
584
      __builtin_unreachable();
532
585
    case 'V':                   /* --version */
533
586
      fprintf(state->out_stream, "%s\n", argp_program_version);
534
587
      exit(EXIT_SUCCESS);
544
597
      if(arg[0] == '\0'){
545
598
        break;
546
599
      }
 
600
#if __GNUC__ >= 7
 
601
      __attribute__((fallthrough));
 
602
#else
 
603
          /* FALLTHROUGH */
 
604
#endif
547
605
    default:
548
606
      return ARGP_ERR_UNKNOWN;
549
607
    }
568
626
    case 129:                   /* --config-file */
569
627
      free(argfile);
570
628
      argfile = strdup(arg);
 
629
      if(argfile != NULL){
 
630
        errno = 0;
 
631
      }
571
632
      break;
572
633
    case 130:                   /* --userid */
573
634
    case 131:                   /* --groupid */
574
635
    case 132:                   /* --debug */
 
636
    case 133:                   /* --plugin-helper-dir */
575
637
    case '?':                   /* --help */
576
638
    case -3:                    /* --usage */
577
639
    case 'V':                   /* --version */
599
661
  case ENOMEM:
600
662
  default:
601
663
    errno = ret;
602
 
    perror("argp_parse");
 
664
    error(0, errno, "argp_parse");
603
665
    exitstatus = EX_OSERR;
604
666
    goto fallback;
605
667
  case EINVAL:
626
688
    custom_argc = 1;
627
689
    custom_argv = malloc(sizeof(char*) * 2);
628
690
    if(custom_argv == NULL){
629
 
      perror("malloc");
 
691
      error(0, errno, "malloc");
630
692
      exitstatus = EX_OSERR;
631
693
      goto fallback;
632
694
    }
649
711
        }
650
712
        new_arg = strdup(p);
651
713
        if(new_arg == NULL){
652
 
          perror("strdup");
 
714
          error(0, errno, "strdup");
653
715
          exitstatus = EX_OSERR;
654
716
          free(org_line);
655
717
          goto fallback;
656
718
        }
657
719
        
658
720
        custom_argc += 1;
659
 
        custom_argv = realloc(custom_argv, sizeof(char *)
660
 
                              * ((unsigned int) custom_argc + 1));
661
 
        if(custom_argv == NULL){
662
 
          perror("realloc");
663
 
          exitstatus = EX_OSERR;
664
 
          free(org_line);
665
 
          goto fallback;
 
721
        {
 
722
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
 
723
          char **new_argv = reallocarray(custom_argv, (size_t)custom_argc + 1,
 
724
                                         sizeof(char *));
 
725
#else
 
726
          char **new_argv = NULL;
 
727
          if(((size_t)custom_argc + 1) > (SIZE_MAX / sizeof(char *))){
 
728
            /* overflow */
 
729
            errno = ENOMEM;
 
730
          } else {
 
731
            new_argv = realloc(custom_argv, ((size_t)custom_argc + 1)
 
732
                               * sizeof(char *));
 
733
          }
 
734
#endif
 
735
          if(new_argv == NULL){
 
736
            error(0, errno, "reallocarray");
 
737
            exitstatus = EX_OSERR;
 
738
            free(new_arg);
 
739
            free(org_line);
 
740
            goto fallback;
 
741
          } else {
 
742
            custom_argv = new_argv;
 
743
          }
666
744
        }
667
745
        custom_argv[custom_argc-1] = new_arg;
668
746
        custom_argv[custom_argc] = NULL;
672
750
      ret = fclose(conffp);
673
751
    } while(ret == EOF and errno == EINTR);
674
752
    if(ret == EOF){
675
 
      perror("fclose");
 
753
      error(0, errno, "fclose");
676
754
      exitstatus = EX_IOERR;
677
755
      goto fallback;
678
756
    }
681
759
    /* Check for harmful errors and go to fallback. Other errors might
682
760
       not affect opening plugins */
683
761
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
684
 
      perror("fopen");
 
762
      error(0, errno, "fopen");
685
763
      exitstatus = EX_OSERR;
686
764
      goto fallback;
687
765
    }
698
776
    case ENOMEM:
699
777
    default:
700
778
      errno = ret;
701
 
      perror("argp_parse");
 
779
      error(0, errno, "argp_parse");
702
780
      exitstatus = EX_OSERR;
703
781
      goto fallback;
704
782
    case EINVAL:
718
796
  case ENOMEM:
719
797
  default:
720
798
    errno = ret;
721
 
    perror("argp_parse");
 
799
    error(0, errno, "argp_parse");
722
800
    exitstatus = EX_OSERR;
723
801
    goto fallback;
724
802
  case EINVAL:
726
804
    goto fallback;
727
805
  }
728
806
  
 
807
  {
 
808
    char *pluginhelperenv;
 
809
    bool bret = true;
 
810
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
 
811
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
812
    if(ret != -1){
 
813
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
 
814
    }
 
815
    if(ret == -1 or not bret){
 
816
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
 
817
            " environment variable to \"%s\" for all plugins\n",
 
818
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
819
    }
 
820
    if(ret != -1){
 
821
      free(pluginhelperenv);
 
822
    }
 
823
  }
 
824
  
729
825
  if(debug){
730
 
    for(plugin *p = plugin_list; p != NULL; p=p->next){
 
826
    for(plugin *p = plugin_list; p != NULL; p = p->next){
731
827
      fprintf(stderr, "Plugin: %s has %d arguments\n",
732
828
              p->name ? p->name : "Global", p->argc - 1);
733
829
      for(char **a = p->argv; *a != NULL; a++){
740
836
    }
741
837
  }
742
838
  
743
 
  /* Strip permissions down to nobody */
744
 
  setgid(gid);
 
839
  if(getuid() == 0){
 
840
    /* Work around Debian bug #633582:
 
841
       <https://bugs.debian.org/633582> */
 
842
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
843
    if(plugindir_fd == -1){
 
844
      if(errno != ENOENT){
 
845
        error(0, errno, "open(\"" PDIR "\")");
 
846
      }
 
847
    } else {
 
848
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
849
      if(ret == -1){
 
850
        error(0, errno, "fstat");
 
851
      } else {
 
852
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
853
          ret = fchown(plugindir_fd, uid, gid);
 
854
          if(ret == -1){
 
855
            error(0, errno, "fchown");
 
856
          }
 
857
        }
 
858
      }
 
859
      close(plugindir_fd);
 
860
    }
 
861
 
 
862
    /* Work around Debian bug #981302
 
863
       <https://bugs.debian.org/981302> */
 
864
    if(lstat("/dev/fd", &st) != 0 and errno == ENOENT){
 
865
      ret = symlink("/proc/self/fd", "/dev/fd");
 
866
      if(ret == -1){
 
867
        error(0, errno, "Failed to create /dev/fd symlink");
 
868
      }
 
869
    }
 
870
  }
 
871
  
 
872
  /* Lower permissions */
 
873
  ret = setgid(gid);
745
874
  if(ret == -1){
746
 
    perror("setgid");
 
875
    error(0, errno, "setgid");
747
876
  }
748
877
  ret = setuid(uid);
749
878
  if(ret == -1){
750
 
    perror("setuid");
 
879
    error(0, errno, "setuid");
751
880
  }
752
881
  
753
882
  /* Open plugin directory with close_on_exec flag */
754
883
  {
755
 
    int dir_fd = -1;
756
 
    if(plugindir == NULL){
757
 
      dir_fd = open(PDIR, O_RDONLY |
758
 
#ifdef O_CLOEXEC
759
 
                    O_CLOEXEC
760
 
#else  /* not O_CLOEXEC */
761
 
                    0
762
 
#endif  /* not O_CLOEXEC */
763
 
                    );
764
 
    } else {
765
 
      dir_fd = open(plugindir, O_RDONLY |
766
 
#ifdef O_CLOEXEC
767
 
                    O_CLOEXEC
768
 
#else  /* not O_CLOEXEC */
769
 
                    0
770
 
#endif  /* not O_CLOEXEC */
771
 
                    );
772
 
    }
 
884
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
 
885
#ifdef O_CLOEXEC
 
886
                  O_CLOEXEC
 
887
#else  /* not O_CLOEXEC */
 
888
                  0
 
889
#endif  /* not O_CLOEXEC */
 
890
                  );
773
891
    if(dir_fd == -1){
774
 
      perror("Could not open plugin dir");
 
892
      error(0, errno, "Could not open plugin dir");
775
893
      exitstatus = EX_UNAVAILABLE;
776
894
      goto fallback;
777
895
    }
780
898
  /* Set the FD_CLOEXEC flag on the directory */
781
899
    ret = set_cloexec_flag(dir_fd);
782
900
    if(ret < 0){
783
 
      perror("set_cloexec_flag");
784
 
      TEMP_FAILURE_RETRY(close(dir_fd));
 
901
      error(0, errno, "set_cloexec_flag");
785
902
      exitstatus = EX_OSERR;
786
903
      goto fallback;
787
904
    }
788
905
#endif  /* O_CLOEXEC */
789
 
    
790
 
    dir = fdopendir(dir_fd);
791
 
    if(dir == NULL){
792
 
      perror("Could not open plugin dir");
793
 
      TEMP_FAILURE_RETRY(close(dir_fd));
794
 
      exitstatus = EX_OSERR;
795
 
      goto fallback;
 
906
  }
 
907
  
 
908
  int good_name(const struct dirent * const dirent){
 
909
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
 
910
                                      "*.dpkg-old", "*.dpkg-bak",
 
911
                                      "*.dpkg-divert", NULL };
 
912
#ifdef __GNUC__
 
913
#pragma GCC diagnostic push
 
914
#pragma GCC diagnostic ignored "-Wcast-qual"
 
915
#endif
 
916
    for(const char **pat = (const char **)patterns;
 
917
        *pat != NULL; pat++){
 
918
#ifdef __GNUC__
 
919
#pragma GCC diagnostic pop
 
920
#endif
 
921
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
 
922
         != FNM_NOMATCH){
 
923
        if(debug){
 
924
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
925
                    " matching pattern %s\n", dirent->d_name, *pat);
 
926
        }
 
927
        return 0;
 
928
      }
796
929
    }
 
930
    return 1;
 
931
  }
 
932
  
 
933
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
 
934
                             alphasort);
 
935
  if(numplugins == -1){
 
936
    error(0, errno, "Could not scan plugin dir");
 
937
    direntries = NULL;
 
938
    exitstatus = EX_OSERR;
 
939
    goto fallback;
797
940
  }
798
941
  
799
942
  FD_ZERO(&rfds_all);
800
943
  
801
944
  /* Read and execute any executable in the plugin directory*/
802
 
  while(true){
803
 
    do {
804
 
      dirst = readdir(dir);
805
 
    } while(dirst == NULL and errno == EINTR);
806
 
    
807
 
    /* All directory entries have been processed */
808
 
    if(dirst == NULL){
809
 
      if(errno == EBADF){
810
 
        perror("readdir");
811
 
        exitstatus = EX_IOERR;
812
 
        goto fallback;
813
 
      }
814
 
      break;
815
 
    }
816
 
    
817
 
    d_name_len = strlen(dirst->d_name);
818
 
    
819
 
    /* Ignore dotfiles, backup files and other junk */
820
 
    {
821
 
      bool bad_name = false;
822
 
      
823
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
824
 
      
825
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
826
 
                                           ".dpkg-old",
827
 
                                           ".dpkg-bak",
828
 
                                           ".dpkg-divert", NULL };
829
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
830
 
        size_t pre_len = strlen(*pre);
831
 
        if((d_name_len >= pre_len)
832
 
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
833
 
          if(debug){
834
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
835
 
                    " with bad prefix %s\n", dirst->d_name, *pre);
836
 
          }
837
 
          bad_name = true;
838
 
          break;
839
 
        }
840
 
      }
841
 
      if(bad_name){
842
 
        continue;
843
 
      }
844
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
845
 
        size_t suf_len = strlen(*suf);
846
 
        if((d_name_len >= suf_len)
847
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
848
 
                == 0)){
849
 
          if(debug){
850
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
851
 
                    " with bad suffix %s\n", dirst->d_name, *suf);
852
 
          }
853
 
          bad_name = true;
854
 
          break;
855
 
        }
856
 
      }
857
 
      
858
 
      if(bad_name){
859
 
        continue;
860
 
      }
861
 
    }
862
 
    
863
 
    char *filename;
864
 
    if(plugindir == NULL){
865
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
866
 
                                             dirst->d_name));
867
 
    } else {
868
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
869
 
                                             plugindir,
870
 
                                             dirst->d_name));
871
 
    }
872
 
    if(ret < 0){
873
 
      perror("asprintf");
 
945
  for(int i = 0; i < numplugins; i++){
 
946
    
 
947
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
 
948
    if(plugin_fd == -1){
 
949
      error(0, errno, "Could not open plugin");
 
950
      free(direntries[i]);
874
951
      continue;
875
952
    }
876
 
    
877
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
953
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
878
954
    if(ret == -1){
879
 
      perror("stat");
880
 
      free(filename);
 
955
      error(0, errno, "stat");
 
956
      close(plugin_fd);
 
957
      free(direntries[i]);
881
958
      continue;
882
959
    }
883
960
    
884
961
    /* Ignore non-executable files */
885
962
    if(not S_ISREG(st.st_mode)
886
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
963
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
 
964
                                        X_OK, 0)) != 0)){
887
965
      if(debug){
888
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
889
 
                " with bad type or mode\n", filename);
 
966
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
 
967
                " with bad type or mode\n",
 
968
                plugindir != NULL ? plugindir : PDIR,
 
969
                direntries[i]->d_name);
890
970
      }
891
 
      free(filename);
 
971
      close(plugin_fd);
 
972
      free(direntries[i]);
892
973
      continue;
893
974
    }
894
975
    
895
 
    plugin *p = getplugin(dirst->d_name);
 
976
    plugin *p = getplugin(direntries[i]->d_name);
896
977
    if(p == NULL){
897
 
      perror("getplugin");
898
 
      free(filename);
 
978
      error(0, errno, "getplugin");
 
979
      close(plugin_fd);
 
980
      free(direntries[i]);
899
981
      continue;
900
982
    }
901
983
    if(p->disabled){
902
984
      if(debug){
903
985
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
904
 
                dirst->d_name);
 
986
                direntries[i]->d_name);
905
987
      }
906
 
      free(filename);
 
988
      close(plugin_fd);
 
989
      free(direntries[i]);
907
990
      continue;
908
991
    }
909
992
    {
912
995
      if(g != NULL){
913
996
        for(char **a = g->argv + 1; *a != NULL; a++){
914
997
          if(not add_argument(p, *a)){
915
 
            perror("add_argument");
 
998
            error(0, errno, "add_argument");
916
999
          }
917
1000
        }
918
1001
        /* Add global environment variables */
919
1002
        for(char **e = g->environ; *e != NULL; e++){
920
1003
          if(not add_environment(p, *e, false)){
921
 
            perror("add_environment");
 
1004
            error(0, errno, "add_environment");
922
1005
          }
923
1006
        }
924
1007
      }
925
1008
    }
926
 
    /* If this plugin has any environment variables, we will call
927
 
       using execve and need to duplicate the environment from this
928
 
       process, too. */
 
1009
    /* If this plugin has any environment variables, we need to
 
1010
       duplicate the environment from this process, too. */
929
1011
    if(p->environ[0] != NULL){
930
1012
      for(char **e = environ; *e != NULL; e++){
931
1013
        if(not add_environment(p, *e, false)){
932
 
          perror("add_environment");
 
1014
          error(0, errno, "add_environment");
933
1015
        }
934
1016
      }
935
1017
    }
936
1018
    
937
1019
    int pipefd[2];
 
1020
#ifndef O_CLOEXEC
938
1021
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
1022
#else  /* O_CLOEXEC */
 
1023
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
 
1024
#endif  /* O_CLOEXEC */
939
1025
    if(ret == -1){
940
 
      perror("pipe");
941
 
      exitstatus = EX_OSERR;
942
 
      goto fallback;
943
 
    }
 
1026
      error(0, errno, "pipe");
 
1027
      exitstatus = EX_OSERR;
 
1028
      free(direntries[i]);
 
1029
      goto fallback;
 
1030
    }
 
1031
    if(pipefd[0] >= FD_SETSIZE){
 
1032
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
 
1033
              FD_SETSIZE);
 
1034
      close(pipefd[0]);
 
1035
      close(pipefd[1]);
 
1036
      exitstatus = EX_OSERR;
 
1037
      free(direntries[i]);
 
1038
      goto fallback;
 
1039
    }
 
1040
#ifndef O_CLOEXEC
944
1041
    /* Ask OS to automatic close the pipe on exec */
945
1042
    ret = set_cloexec_flag(pipefd[0]);
946
1043
    if(ret < 0){
947
 
      perror("set_cloexec_flag");
 
1044
      error(0, errno, "set_cloexec_flag");
 
1045
      close(pipefd[0]);
 
1046
      close(pipefd[1]);
948
1047
      exitstatus = EX_OSERR;
 
1048
      free(direntries[i]);
949
1049
      goto fallback;
950
1050
    }
951
1051
    ret = set_cloexec_flag(pipefd[1]);
952
1052
    if(ret < 0){
953
 
      perror("set_cloexec_flag");
 
1053
      error(0, errno, "set_cloexec_flag");
 
1054
      close(pipefd[0]);
 
1055
      close(pipefd[1]);
954
1056
      exitstatus = EX_OSERR;
 
1057
      free(direntries[i]);
955
1058
      goto fallback;
956
1059
    }
 
1060
#endif  /* not O_CLOEXEC */
957
1061
    /* Block SIGCHLD until process is safely in process list */
958
1062
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
959
1063
                                              &sigchld_action.sa_mask,
960
1064
                                              NULL));
961
1065
    if(ret < 0){
962
 
      perror("sigprocmask");
 
1066
      error(0, errno, "sigprocmask");
963
1067
      exitstatus = EX_OSERR;
 
1068
      free(direntries[i]);
964
1069
      goto fallback;
965
1070
    }
966
1071
    /* Starting a new process to be watched */
969
1074
      pid = fork();
970
1075
    } while(pid == -1 and errno == EINTR);
971
1076
    if(pid == -1){
972
 
      perror("fork");
 
1077
      error(0, errno, "fork");
 
1078
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1079
                                     &sigchld_action.sa_mask, NULL));
 
1080
      close(pipefd[0]);
 
1081
      close(pipefd[1]);
973
1082
      exitstatus = EX_OSERR;
 
1083
      free(direntries[i]);
974
1084
      goto fallback;
975
1085
    }
976
1086
    if(pid == 0){
977
1087
      /* this is the child process */
978
1088
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
979
1089
      if(ret < 0){
980
 
        perror("sigaction");
 
1090
        error(0, errno, "sigaction");
981
1091
        _exit(EX_OSERR);
982
1092
      }
983
1093
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
984
1094
      if(ret < 0){
985
 
        perror("sigprocmask");
 
1095
        error(0, errno, "sigprocmask");
986
1096
        _exit(EX_OSERR);
987
1097
      }
988
1098
      
989
1099
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
990
1100
      if(ret == -1){
991
 
        perror("dup2");
 
1101
        error(0, errno, "dup2");
992
1102
        _exit(EX_OSERR);
993
1103
      }
994
1104
      
995
 
      if(dirfd(dir) < 0){
996
 
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
997
 
           above and must now close it manually here. */
998
 
        closedir(dir);
999
 
      }
1000
 
      if(p->environ[0] == NULL){
1001
 
        if(execv(filename, p->argv) < 0){
1002
 
          perror("execv");
1003
 
          _exit(EX_OSERR);
1004
 
        }
1005
 
      } else {
1006
 
        if(execve(filename, p->argv, p->environ) < 0){
1007
 
          perror("execve");
1008
 
          _exit(EX_OSERR);
1009
 
        }
 
1105
      if(fexecve(plugin_fd, p->argv,
 
1106
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
 
1107
        error(0, errno, "fexecve for %s/%s",
 
1108
              plugindir != NULL ? plugindir : PDIR,
 
1109
              direntries[i]->d_name);
 
1110
        _exit(EX_OSERR);
1010
1111
      }
1011
1112
      /* no return */
1012
1113
    }
1013
1114
    /* Parent process */
1014
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1015
 
                                             pipe */
1016
 
    free(filename);
1017
 
    plugin *new_plugin = getplugin(dirst->d_name);
 
1115
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1116
    close(plugin_fd);
 
1117
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1018
1118
    if(new_plugin == NULL){
1019
 
      perror("getplugin");
 
1119
      error(0, errno, "getplugin");
1020
1120
      ret = (int)(TEMP_FAILURE_RETRY
1021
1121
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1022
1122
                               NULL)));
1023
1123
      if(ret < 0){
1024
 
        perror("sigprocmask");
 
1124
        error(0, errno, "sigprocmask");
1025
1125
      }
1026
1126
      exitstatus = EX_OSERR;
 
1127
      free(direntries[i]);
1027
1128
      goto fallback;
1028
1129
    }
 
1130
    free(direntries[i]);
1029
1131
    
1030
1132
    new_plugin->pid = pid;
1031
1133
    new_plugin->fd = pipefd[0];
1032
 
    
 
1134
 
 
1135
    if(debug){
 
1136
      fprintf(stderr, "Plugin %s started (PID %" PRIdMAX ")\n",
 
1137
              new_plugin->name, (intmax_t) (new_plugin->pid));
 
1138
    }
 
1139
 
1033
1140
    /* Unblock SIGCHLD so signal handler can be run if this process
1034
1141
       has already completed */
1035
1142
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1036
1143
                                              &sigchld_action.sa_mask,
1037
1144
                                              NULL));
1038
1145
    if(ret < 0){
1039
 
      perror("sigprocmask");
 
1146
      error(0, errno, "sigprocmask");
1040
1147
      exitstatus = EX_OSERR;
1041
1148
      goto fallback;
1042
1149
    }
1043
1150
    
1044
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1045
 
                                          -Wconversion */
 
1151
    FD_SET(new_plugin->fd, &rfds_all);
1046
1152
    
1047
1153
    if(maxfd < new_plugin->fd){
1048
1154
      maxfd = new_plugin->fd;
1049
1155
    }
1050
1156
  }
1051
1157
  
1052
 
  TEMP_FAILURE_RETRY(closedir(dir));
1053
 
  dir = NULL;
 
1158
  free(direntries);
 
1159
  direntries = NULL;
 
1160
  close(dir_fd);
 
1161
  dir_fd = -1;
1054
1162
  free_plugin(getplugin(NULL));
1055
1163
  
1056
1164
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1069
1177
    fd_set rfds = rfds_all;
1070
1178
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1071
1179
    if(select_ret == -1 and errno != EINTR){
1072
 
      perror("select");
 
1180
      error(0, errno, "select");
1073
1181
      exitstatus = EX_OSERR;
1074
1182
      goto fallback;
1075
1183
    }
1095
1203
                      (intmax_t) (proc->pid),
1096
1204
                      WTERMSIG(proc->status),
1097
1205
                      strsignal(WTERMSIG(proc->status)));
1098
 
            } else if(WCOREDUMP(proc->status)){
1099
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1100
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
1101
1206
            }
1102
1207
          }
1103
1208
          
1104
1209
          /* Remove the plugin */
1105
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1106
 
                                          -Wconversion */
 
1210
          FD_CLR(proc->fd, &rfds_all);
1107
1211
          
1108
1212
          /* Block signal while modifying process_list */
1109
1213
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1111
1215
                                         &sigchld_action.sa_mask,
1112
1216
                                         NULL));
1113
1217
          if(ret < 0){
1114
 
            perror("sigprocmask");
 
1218
            error(0, errno, "sigprocmask");
1115
1219
            exitstatus = EX_OSERR;
1116
1220
            goto fallback;
1117
1221
          }
1125
1229
                      (sigprocmask(SIG_UNBLOCK,
1126
1230
                                   &sigchld_action.sa_mask, NULL)));
1127
1231
          if(ret < 0){
1128
 
            perror("sigprocmask");
 
1232
            error(0, errno, "sigprocmask");
1129
1233
            exitstatus = EX_OSERR;
1130
1234
            goto fallback;
1131
1235
          }
1142
1246
        bool bret = print_out_password(proc->buffer,
1143
1247
                                       proc->buffer_length);
1144
1248
        if(not bret){
1145
 
          perror("print_out_password");
 
1249
          error(0, errno, "print_out_password");
1146
1250
          exitstatus = EX_IOERR;
1147
1251
        }
1148
1252
        goto fallback;
1149
1253
      }
1150
1254
      
1151
1255
      /* This process has not completed.  Does it have any output? */
1152
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1153
 
                                                         warning from
1154
 
                                                         -Wconversion */
 
1256
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1155
1257
        /* This process had nothing to say at this time */
1156
1258
        proc = proc->next;
1157
1259
        continue;
1158
1260
      }
1159
1261
      /* Before reading, make the process' data buffer large enough */
1160
1262
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1161
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1162
 
                               + (size_t) BUFFER_SIZE);
1163
 
        if(proc->buffer == NULL){
1164
 
          perror("malloc");
 
1263
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1264
                                   + (size_t) BUFFER_SIZE);
 
1265
        if(new_buffer == NULL){
 
1266
          error(0, errno, "malloc");
1165
1267
          exitstatus = EX_OSERR;
1166
1268
          goto fallback;
1167
1269
        }
 
1270
        proc->buffer = new_buffer;
1168
1271
        proc->buffer_size += BUFFER_SIZE;
1169
1272
      }
1170
1273
      /* Read from the process */
1204
1307
    }
1205
1308
    bret = print_out_password(passwordbuffer, len);
1206
1309
    if(not bret){
1207
 
      perror("print_out_password");
 
1310
      error(0, errno, "print_out_password");
1208
1311
      exitstatus = EX_IOERR;
1209
1312
    }
1210
1313
  }
1212
1315
  /* Restore old signal handler */
1213
1316
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1214
1317
  if(ret == -1){
1215
 
    perror("sigaction");
 
1318
    error(0, errno, "sigaction");
1216
1319
    exitstatus = EX_OSERR;
1217
1320
  }
1218
1321
  
1223
1326
    free(custom_argv);
1224
1327
  }
1225
1328
  
1226
 
  if(dir != NULL){
1227
 
    closedir(dir);
 
1329
  free(direntries);
 
1330
  
 
1331
  if(dir_fd != -1){
 
1332
    close(dir_fd);
1228
1333
  }
1229
1334
  
1230
1335
  /* Kill the processes */
1234
1339
      ret = kill(p->pid, SIGTERM);
1235
1340
      if(ret == -1 and errno != ESRCH){
1236
1341
        /* Set-uid proccesses might not get closed */
1237
 
        perror("kill");
 
1342
        error(0, errno, "kill");
1238
1343
      }
1239
1344
    }
1240
1345
  }
1244
1349
    ret = wait(NULL);
1245
1350
  } while(ret >= 0);
1246
1351
  if(errno != ECHILD){
1247
 
    perror("wait");
 
1352
    error(0, errno, "wait");
1248
1353
  }
1249
1354
  
1250
1355
  free_plugin_list();
1251
1356
  
1252
1357
  free(plugindir);
 
1358
  free(pluginhelperdir);
1253
1359
  free(argfile);
1254
1360
  
1255
1361
  return exitstatus;