/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

* initramfs-tools-hook: Only copy needed file; avoid copying Mandos
                        server config files.

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-2016 Teddy Hogeborn
6
 
 * Copyright © 2008-2016 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
 
                                   O_CLOEXEC, pipe2() */
 
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(),
32
 
                                   stderr, STDOUT_FILENO, fclose() */
33
 
#include <sys/types.h>          /* fstat(), struct stat, waitpid(),
34
 
                                   WIFEXITED(), WEXITSTATUS(), wait(),
35
 
                                   pid_t, uid_t, gid_t, getuid(),
36
 
                                   getgid() */
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO */
 
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
34
                                   stat, waitpid(), WIFEXITED(),
 
35
                                   WEXITSTATUS(), wait(), pid_t,
 
36
                                   uid_t, gid_t, getuid(), getgid(),
 
37
                                   dirfd() */
37
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
38
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
39
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
40
41
                                   WEXITSTATUS(), WTERMSIG(),
41
42
                                   WCOREDUMP() */
42
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
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
 
                                */
 
45
#include <dirent.h>             /* DIR, struct dirent, opendir(),
 
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() */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC, openat(), scandirat(),
54
 
                                   pipe2() */
55
 
#include <string.h>             /* strsep, strlen(), strsignal(),
56
 
                                   strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal() */
57
58
#include <errno.h>              /* errno */
58
59
#include <argp.h>               /* struct argp_option, struct
59
60
                                   argp_state, struct argp,
67
68
                                */
68
69
#include <errno.h>              /* errno, EBADF */
69
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
70
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
71
 
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
72
 
#include <errno.h>              /* errno */
73
 
#include <error.h>              /* error() */
74
 
#include <fnmatch.h>            /* fnmatch() */
75
71
 
76
72
#define BUFFER_SIZE 256
77
73
 
78
74
#define PDIR "/lib/mandos/plugins.d"
79
 
#define PHDIR "/lib/mandos/plugin-helpers"
80
75
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
81
76
 
82
77
const char *argp_program_version = "plugin-runner " VERSION;
83
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
78
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
84
79
 
85
80
typedef struct plugin{
86
81
  char *name;                   /* can be NULL or any plugin name */
106
101
 
107
102
/* Gets an existing plugin based on name,
108
103
   or if none is found, creates a new one */
109
 
__attribute__((warn_unused_result))
110
104
static plugin *getplugin(char *name){
111
 
  /* Check for existing plugin with that name */
 
105
  /* Check for exiting plugin with that name */
112
106
  for(plugin *p = plugin_list; p != NULL; p = p->next){
113
107
    if((p->name == name)
114
108
       or (p->name and name and (strcmp(p->name, name) == 0))){
129
123
      copy_name = strdup(name);
130
124
    } while(copy_name == NULL and errno == EINTR);
131
125
    if(copy_name == NULL){
132
 
      int e = errno;
133
126
      free(new_plugin);
134
 
      errno = e;
135
127
      return NULL;
136
128
    }
137
129
  }
145
137
    new_plugin->argv = malloc(sizeof(char *) * 2);
146
138
  } while(new_plugin->argv == NULL and errno == EINTR);
147
139
  if(new_plugin->argv == NULL){
148
 
    int e = errno;
149
140
    free(copy_name);
150
141
    free(new_plugin);
151
 
    errno = e;
152
142
    return NULL;
153
143
  }
154
144
  new_plugin->argv[0] = copy_name;
158
148
    new_plugin->environ = malloc(sizeof(char *));
159
149
  } while(new_plugin->environ == NULL and errno == EINTR);
160
150
  if(new_plugin->environ == NULL){
161
 
    int e = errno;
162
151
    free(copy_name);
163
152
    free(new_plugin->argv);
164
153
    free(new_plugin);
165
 
    errno = e;
166
154
    return NULL;
167
155
  }
168
156
  new_plugin->environ[0] = NULL;
173
161
}
174
162
 
175
163
/* Helper function for add_argument and add_environment */
176
 
__attribute__((nonnull, warn_unused_result))
177
164
static bool add_to_char_array(const char *new, char ***array,
178
165
                              int *len){
179
166
  /* Resize the pointed-to array to hold one more pointer */
180
 
  char **new_array = NULL;
181
167
  do {
182
 
    new_array = realloc(*array, sizeof(char *)
183
 
                        * (size_t) ((*len) + 2));
184
 
  } 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);
185
171
  /* Malloc check */
186
 
  if(new_array == NULL){
 
172
  if(*array == NULL){
187
173
    return false;
188
174
  }
189
 
  *array = new_array;
190
175
  /* Make a copy of the new string */
191
176
  char *copy;
192
177
  do {
204
189
}
205
190
 
206
191
/* Add to a plugin's argument vector */
207
 
__attribute__((nonnull(2), warn_unused_result))
208
192
static bool add_argument(plugin *p, const char *arg){
209
193
  if(p == NULL){
210
194
    return false;
213
197
}
214
198
 
215
199
/* Add to a plugin's environment */
216
 
__attribute__((nonnull(2), warn_unused_result))
217
200
static bool add_environment(plugin *p, const char *def, bool replace){
218
201
  if(p == NULL){
219
202
    return false;
221
204
  /* namelen = length of name of environment variable */
222
205
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
223
206
  /* Search for this environment variable */
224
 
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
225
 
    if(strncmp(*envdef, def, namelen + 1) == 0){
 
207
  for(char **e = p->environ; *e != NULL; e++){
 
208
    if(strncmp(*e, def, namelen + 1) == 0){
226
209
      /* It already exists */
227
210
      if(replace){
228
 
        char *new_envdef;
 
211
        char *new;
229
212
        do {
230
 
          new_envdef = realloc(*envdef, strlen(def) + 1);
231
 
        } while(new_envdef == NULL and errno == EINTR);
232
 
        if(new_envdef == NULL){
 
213
          new = realloc(*e, strlen(def) + 1);
 
214
        } while(new == NULL and errno == EINTR);
 
215
        if(new == NULL){
233
216
          return false;
234
217
        }
235
 
        *envdef = new_envdef;
236
 
        strcpy(*envdef, def);
 
218
        *e = new;
 
219
        strcpy(*e, def);
237
220
      }
238
221
      return true;
239
222
    }
241
224
  return add_to_char_array(def, &(p->environ), &(p->envc));
242
225
}
243
226
 
244
 
#ifndef O_CLOEXEC
245
227
/*
246
228
 * Based on the example in the GNU LibC manual chapter 13.13 "File
247
229
 * Descriptor Flags".
248
230
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
249
231
 */
250
 
__attribute__((warn_unused_result))
251
232
static int set_cloexec_flag(int fd){
252
233
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
253
234
  /* If reading the flags failed, return error indication now. */
258
239
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
259
240
                                       ret | FD_CLOEXEC));
260
241
}
261
 
#endif  /* not O_CLOEXEC */
262
242
 
263
243
 
264
244
/* Mark processes as completed when they exit, and save their exit
278
258
        /* No child processes */
279
259
        break;
280
260
      }
281
 
      error(0, errno, "waitpid");
 
261
      perror("waitpid");
282
262
    }
283
263
    
284
264
    /* A child exited, find it in process_list */
296
276
}
297
277
 
298
278
/* Prints out a password to stdout */
299
 
__attribute__((nonnull, warn_unused_result))
300
279
static bool print_out_password(const char *buffer, size_t length){
301
280
  ssize_t ret;
302
281
  for(size_t written = 0; written < length; written += (size_t)ret){
310
289
}
311
290
 
312
291
/* Removes and free a plugin from the plugin list */
313
 
__attribute__((nonnull))
314
292
static void free_plugin(plugin *plugin_node){
315
293
  
316
294
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
348
326
 
349
327
int main(int argc, char *argv[]){
350
328
  char *plugindir = NULL;
351
 
  char *pluginhelperdir = NULL;
352
329
  char *argfile = NULL;
353
330
  FILE *conffp;
354
 
  struct dirent **direntries = NULL;
 
331
  size_t d_name_len;
 
332
  DIR *dir = NULL;
 
333
  struct dirent *dirst;
355
334
  struct stat st;
356
335
  fd_set rfds_all;
357
336
  int ret, maxfd = 0;
365
344
                                      .sa_flags = SA_NOCLDSTOP };
366
345
  char **custom_argv = NULL;
367
346
  int custom_argc = 0;
368
 
  int dir_fd = -1;
369
347
  
370
348
  /* Establish a signal handler */
371
349
  sigemptyset(&sigchld_action.sa_mask);
372
350
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
373
351
  if(ret == -1){
374
 
    error(0, errno, "sigaddset");
375
 
    exitstatus = EX_OSERR;
 
352
    perror("sigaddset");
 
353
    exitstatus = EXIT_FAILURE;
376
354
    goto fallback;
377
355
  }
378
356
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
379
357
  if(ret == -1){
380
 
    error(0, errno, "sigaction");
381
 
    exitstatus = EX_OSERR;
 
358
    perror("sigaction");
 
359
    exitstatus = EXIT_FAILURE;
382
360
    goto fallback;
383
361
  }
384
362
  
416
394
      .doc = "Group ID the plugins will run as", .group = 3 },
417
395
    { .name = "debug", .key = 132,
418
396
      .doc = "Debug mode", .group = 4 },
419
 
    { .name = "plugin-helper-dir", .key = 133,
420
 
      .arg = "DIRECTORY",
421
 
      .doc = "Specify a different plugin helper directory",
422
 
      .group = 2 },
423
 
    /*
424
 
     * These reproduce what we would get without ARGP_NO_HELP
425
 
     */
426
 
    { .name = "help", .key = '?',
427
 
      .doc = "Give this help list", .group = -1 },
428
 
    { .name = "usage", .key = -3,
429
 
      .doc = "Give a short usage message", .group = -1 },
430
 
    { .name = "version", .key = 'V',
431
 
      .doc = "Print program version", .group = -1 },
432
397
    { .name = NULL }
433
398
  };
434
399
  
435
 
  __attribute__((nonnull(3)))
436
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
437
 
    errno = 0;
 
400
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
401
                    struct argp_state *state){
438
402
    switch(key){
439
403
      char *tmp;
440
 
      intmax_t tmp_id;
 
404
      intmax_t tmpmax;
441
405
    case 'g':                   /* --global-options */
442
 
      {
 
406
      if(arg != NULL){
443
407
        char *plugin_option;
444
408
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
409
          if(plugin_option[0] == '\0'){
 
410
            continue;
 
411
          }
445
412
          if(not add_argument(getplugin(NULL), plugin_option)){
446
 
            break;
 
413
            perror("add_argument");
 
414
            return ARGP_ERR_UNKNOWN;
447
415
          }
448
416
        }
449
 
        errno = 0;
450
417
      }
451
418
      break;
452
419
    case 'G':                   /* --global-env */
453
 
      if(add_environment(getplugin(NULL), arg, true)){
454
 
        errno = 0;
 
420
      if(arg == NULL){
 
421
        break;
 
422
      }
 
423
      if(not add_environment(getplugin(NULL), arg, true)){
 
424
        perror("add_environment");
455
425
      }
456
426
      break;
457
427
    case 'o':                   /* --options-for */
458
 
      {
459
 
        char *option_list = strchr(arg, ':');
460
 
        if(option_list == NULL){
461
 
          argp_error(state, "No colon in \"%s\"", arg);
462
 
          errno = EINVAL;
463
 
          break;
464
 
        }
465
 
        *option_list = '\0';
466
 
        option_list++;
467
 
        if(arg[0] == '\0'){
468
 
          argp_error(state, "Empty plugin name");
469
 
          errno = EINVAL;
470
 
          break;
471
 
        }
472
 
        char *option;
473
 
        while((option = strsep(&option_list, ",")) != NULL){
474
 
          if(not add_argument(getplugin(arg), option)){
475
 
            break;
 
428
      if(arg != NULL){
 
429
        char *plugin_name = strsep(&arg, ":");
 
430
        if(plugin_name[0] == '\0'){
 
431
          break;
 
432
        }
 
433
        char *plugin_option;
 
434
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
435
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
436
            perror("add_argument");
 
437
            return ARGP_ERR_UNKNOWN;
476
438
          }
477
439
        }
478
 
        errno = 0;
479
440
      }
480
441
      break;
481
442
    case 'E':                   /* --env-for */
 
443
      if(arg == NULL){
 
444
        break;
 
445
      }
482
446
      {
483
447
        char *envdef = strchr(arg, ':');
484
448
        if(envdef == NULL){
485
 
          argp_error(state, "No colon in \"%s\"", arg);
486
 
          errno = EINVAL;
487
449
          break;
488
450
        }
489
451
        *envdef = '\0';
490
 
        envdef++;
491
 
        if(arg[0] == '\0'){
492
 
          argp_error(state, "Empty plugin name");
493
 
          errno = EINVAL;
494
 
          break;
495
 
        }
496
 
        if(add_environment(getplugin(arg), envdef, true)){
497
 
          errno = 0;
 
452
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
453
          perror("add_environment");
498
454
        }
499
455
      }
500
456
      break;
501
457
    case 'd':                   /* --disable */
502
 
      {
 
458
      if(arg != NULL){
503
459
        plugin *p = getplugin(arg);
504
 
        if(p != NULL){
505
 
          p->disabled = true;
506
 
          errno = 0;
 
460
        if(p == NULL){
 
461
          return ARGP_ERR_UNKNOWN;
507
462
        }
 
463
        p->disabled = true;
508
464
      }
509
465
      break;
510
466
    case 'e':                   /* --enable */
511
 
      {
 
467
      if(arg != NULL){
512
468
        plugin *p = getplugin(arg);
513
 
        if(p != NULL){
514
 
          p->disabled = false;
515
 
          errno = 0;
 
469
        if(p == NULL){
 
470
          return ARGP_ERR_UNKNOWN;
516
471
        }
 
472
        p->disabled = false;
517
473
      }
518
474
      break;
519
475
    case 128:                   /* --plugin-dir */
520
476
      free(plugindir);
521
477
      plugindir = strdup(arg);
522
 
      if(plugindir != NULL){
523
 
        errno = 0;
 
478
      if(plugindir == NULL){
 
479
        perror("strdup");
524
480
      }
525
481
      break;
526
482
    case 129:                   /* --config-file */
527
483
      /* This is already done by parse_opt_config_file() */
528
484
      break;
529
485
    case 130:                   /* --userid */
530
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
486
      errno = 0;
 
487
      tmpmax = strtoimax(arg, &tmp, 10);
531
488
      if(errno != 0 or tmp == arg or *tmp != '\0'
532
 
         or tmp_id != (uid_t)tmp_id){
533
 
        argp_error(state, "Bad user ID number: \"%s\", using %"
534
 
                   PRIdMAX, arg, (intmax_t)uid);
535
 
        break;
 
489
         or tmpmax != (uid_t)tmpmax){
 
490
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
491
                PRIdMAX "\n", arg, (intmax_t)uid);
 
492
      } else {
 
493
        uid = (uid_t)tmpmax;
536
494
      }
537
 
      uid = (uid_t)tmp_id;
538
 
      errno = 0;
539
495
      break;
540
496
    case 131:                   /* --groupid */
541
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
497
      errno = 0;
 
498
      tmpmax = strtoimax(arg, &tmp, 10);
542
499
      if(errno != 0 or tmp == arg or *tmp != '\0'
543
 
         or tmp_id != (gid_t)tmp_id){
544
 
        argp_error(state, "Bad group ID number: \"%s\", using %"
545
 
                   PRIdMAX, arg, (intmax_t)gid);
546
 
        break;
 
500
         or tmpmax != (gid_t)tmpmax){
 
501
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
502
                PRIdMAX "\n", arg, (intmax_t)gid);
 
503
      } else {
 
504
        gid = (gid_t)tmpmax;
547
505
      }
548
 
      gid = (gid_t)tmp_id;
549
 
      errno = 0;
550
506
      break;
551
507
    case 132:                   /* --debug */
552
508
      debug = true;
553
509
      break;
554
 
    case 133:                   /* --plugin-helper-dir */
555
 
      free(pluginhelperdir);
556
 
      pluginhelperdir = strdup(arg);
557
 
      if(pluginhelperdir != NULL){
558
 
        errno = 0;
559
 
      }
560
 
      break;
561
 
      /*
562
 
       * These reproduce what we would get without ARGP_NO_HELP
563
 
       */
564
 
    case '?':                   /* --help */
565
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
566
 
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
567
 
    case -3:                    /* --usage */
568
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
569
 
      argp_state_help(state, state->out_stream,
570
 
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
571
 
    case 'V':                   /* --version */
572
 
      fprintf(state->out_stream, "%s\n", argp_program_version);
573
 
      exit(EXIT_SUCCESS);
574
 
      break;
575
510
/*
576
511
 * When adding more options before this line, remember to also add a
577
512
 * "case" to the "parse_opt_config_file" function below.
580
515
      /* Cryptsetup always passes an argument, which is an empty
581
516
         string if "none" was specified in /etc/crypttab.  So if
582
517
         argument was empty, we ignore it silently. */
583
 
      if(arg[0] == '\0'){
584
 
        break;
 
518
      if(arg[0] != '\0'){
 
519
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
585
520
      }
 
521
      break;
 
522
    case ARGP_KEY_END:
 
523
      break;
586
524
    default:
587
525
      return ARGP_ERR_UNKNOWN;
588
526
    }
589
 
    return errno;               /* Set to 0 at start */
 
527
    return 0;
590
528
  }
591
529
  
592
530
  /* This option parser is the same as parse_opt() above, except it
594
532
  error_t parse_opt_config_file(int key, char *arg,
595
533
                                __attribute__((unused))
596
534
                                struct argp_state *state){
597
 
    errno = 0;
598
535
    switch(key){
599
536
    case 'g':                   /* --global-options */
600
537
    case 'G':                   /* --global-env */
607
544
    case 129:                   /* --config-file */
608
545
      free(argfile);
609
546
      argfile = strdup(arg);
610
 
      if(argfile != NULL){
611
 
        errno = 0;
 
547
      if(argfile == NULL){
 
548
        perror("strdup");
612
549
      }
613
550
      break;
614
551
    case 130:                   /* --userid */
615
552
    case 131:                   /* --groupid */
616
553
    case 132:                   /* --debug */
617
 
    case 133:                   /* --plugin-helper-dir */
618
 
    case '?':                   /* --help */
619
 
    case -3:                    /* --usage */
620
 
    case 'V':                   /* --version */
621
554
    case ARGP_KEY_ARG:
 
555
    case ARGP_KEY_END:
622
556
      break;
623
557
    default:
624
558
      return ARGP_ERR_UNKNOWN;
625
559
    }
626
 
    return errno;
 
560
    return 0;
627
561
  }
628
562
  
629
563
  struct argp argp = { .options = options,
633
567
  
634
568
  /* Parse using parse_opt_config_file() in order to get the custom
635
569
     config file location, if any. */
636
 
  ret = argp_parse(&argp, argc, argv,
637
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
638
 
                   NULL, NULL);
639
 
  switch(ret){
640
 
  case 0:
641
 
    break;
642
 
  case ENOMEM:
643
 
  default:
644
 
    errno = ret;
645
 
    error(0, errno, "argp_parse");
646
 
    exitstatus = EX_OSERR;
647
 
    goto fallback;
648
 
  case EINVAL:
649
 
    exitstatus = EX_USAGE;
 
570
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
571
  if(ret == ARGP_ERR_UNKNOWN){
 
572
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
573
    exitstatus = EXIT_FAILURE;
650
574
    goto fallback;
651
575
  }
652
576
  
669
593
    custom_argc = 1;
670
594
    custom_argv = malloc(sizeof(char*) * 2);
671
595
    if(custom_argv == NULL){
672
 
      error(0, errno, "malloc");
673
 
      exitstatus = EX_OSERR;
 
596
      perror("malloc");
 
597
      exitstatus = EXIT_FAILURE;
674
598
      goto fallback;
675
599
    }
676
600
    custom_argv[0] = argv[0];
692
616
        }
693
617
        new_arg = strdup(p);
694
618
        if(new_arg == NULL){
695
 
          error(0, errno, "strdup");
696
 
          exitstatus = EX_OSERR;
 
619
          perror("strdup");
 
620
          exitstatus = EXIT_FAILURE;
697
621
          free(org_line);
698
622
          goto fallback;
699
623
        }
700
624
        
701
625
        custom_argc += 1;
702
 
        {
703
 
          char **new_argv = realloc(custom_argv, sizeof(char *)
704
 
                                    * ((size_t)custom_argc + 1));
705
 
          if(new_argv == NULL){
706
 
            error(0, errno, "realloc");
707
 
            exitstatus = EX_OSERR;
708
 
            free(new_arg);
709
 
            free(org_line);
710
 
            goto fallback;
711
 
          } else {
712
 
            custom_argv = new_argv;
713
 
          }
 
626
        custom_argv = realloc(custom_argv, sizeof(char *)
 
627
                              * ((unsigned int) custom_argc + 1));
 
628
        if(custom_argv == NULL){
 
629
          perror("realloc");
 
630
          exitstatus = EXIT_FAILURE;
 
631
          free(org_line);
 
632
          goto fallback;
714
633
        }
715
634
        custom_argv[custom_argc-1] = new_arg;
716
635
        custom_argv[custom_argc] = NULL;
720
639
      ret = fclose(conffp);
721
640
    } while(ret == EOF and errno == EINTR);
722
641
    if(ret == EOF){
723
 
      error(0, errno, "fclose");
724
 
      exitstatus = EX_IOERR;
 
642
      perror("fclose");
 
643
      exitstatus = EXIT_FAILURE;
725
644
      goto fallback;
726
645
    }
727
646
    free(org_line);
729
648
    /* Check for harmful errors and go to fallback. Other errors might
730
649
       not affect opening plugins */
731
650
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
732
 
      error(0, errno, "fopen");
733
 
      exitstatus = EX_OSERR;
 
651
      perror("fopen");
 
652
      exitstatus = EXIT_FAILURE;
734
653
      goto fallback;
735
654
    }
736
655
  }
737
 
  /* If there were any arguments from the configuration file, pass
738
 
     them to parser as command line arguments */
 
656
  /* If there was any arguments from configuration file,
 
657
     pass them to parser as command arguments */
739
658
  if(custom_argv != NULL){
740
 
    ret = argp_parse(&argp, custom_argc, custom_argv,
741
 
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
742
 
                     NULL, NULL);
743
 
    switch(ret){
744
 
    case 0:
745
 
      break;
746
 
    case ENOMEM:
747
 
    default:
748
 
      errno = ret;
749
 
      error(0, errno, "argp_parse");
750
 
      exitstatus = EX_OSERR;
751
 
      goto fallback;
752
 
    case EINVAL:
753
 
      exitstatus = EX_CONFIG;
 
659
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
660
                     0, NULL);
 
661
    if(ret == ARGP_ERR_UNKNOWN){
 
662
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
663
      exitstatus = EXIT_FAILURE;
754
664
      goto fallback;
755
665
    }
756
666
  }
757
667
  
758
668
  /* Parse actual command line arguments, to let them override the
759
669
     config file */
760
 
  ret = argp_parse(&argp, argc, argv,
761
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
762
 
                   NULL, NULL);
763
 
  switch(ret){
764
 
  case 0:
765
 
    break;
766
 
  case ENOMEM:
767
 
  default:
768
 
    errno = ret;
769
 
    error(0, errno, "argp_parse");
770
 
    exitstatus = EX_OSERR;
771
 
    goto fallback;
772
 
  case EINVAL:
773
 
    exitstatus = EX_USAGE;
774
 
    goto fallback;
775
 
  }
776
 
  
777
 
  {
778
 
    char *pluginhelperenv;
779
 
    bool bret = true;
780
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
781
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
782
 
    if(ret != -1){
783
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
784
 
    }
785
 
    if(ret == -1 or not bret){
786
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
787
 
            " environment variable to \"%s\" for all plugins\n",
788
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
789
 
    }
790
 
    if(ret != -1){
791
 
      free(pluginhelperenv);
792
 
    }
 
670
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
671
  if(ret == ARGP_ERR_UNKNOWN){
 
672
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
673
    exitstatus = EXIT_FAILURE;
 
674
    goto fallback;
793
675
  }
794
676
  
795
677
  if(debug){
806
688
    }
807
689
  }
808
690
  
809
 
  if(getuid() == 0){
810
 
    /* Work around Debian bug #633582:
811
 
       <http://bugs.debian.org/633582> */
812
 
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
813
 
    if(plugindir_fd == -1){
814
 
      if(errno != ENOENT){
815
 
        error(0, errno, "open(\"" PDIR "\")");
816
 
      }
817
 
    } else {
818
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
819
 
      if(ret == -1){
820
 
        error(0, errno, "fstat");
821
 
      } else {
822
 
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
823
 
          ret = fchown(plugindir_fd, uid, gid);
824
 
          if(ret == -1){
825
 
            error(0, errno, "fchown");
826
 
          }
827
 
        }
828
 
      }
829
 
      close(plugindir_fd);
830
 
    }
831
 
  }
832
 
  
833
 
  /* Lower permissions */
834
 
  ret = setgid(gid);
 
691
  /* Strip permissions down to nobody */
 
692
  setgid(gid);
835
693
  if(ret == -1){
836
 
    error(0, errno, "setgid");
 
694
    perror("setgid");
837
695
  }
838
696
  ret = setuid(uid);
839
697
  if(ret == -1){
840
 
    error(0, errno, "setuid");
841
 
  }
842
 
  
843
 
  /* Open plugin directory with close_on_exec flag */
 
698
    perror("setuid");
 
699
  }
 
700
  
 
701
  if(plugindir == NULL){
 
702
    dir = opendir(PDIR);
 
703
  } else {
 
704
    dir = opendir(plugindir);
 
705
  }
 
706
  
 
707
  if(dir == NULL){
 
708
    perror("Could not open plugin dir");
 
709
    exitstatus = EXIT_FAILURE;
 
710
    goto fallback;
 
711
  }
 
712
  
 
713
  /* Set the FD_CLOEXEC flag on the directory, if possible */
844
714
  {
845
 
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
846
 
#ifdef O_CLOEXEC
847
 
                  O_CLOEXEC
848
 
#else  /* not O_CLOEXEC */
849
 
                  0
850
 
#endif  /* not O_CLOEXEC */
851
 
                  );
852
 
    if(dir_fd == -1){
853
 
      error(0, errno, "Could not open plugin dir");
854
 
      exitstatus = EX_UNAVAILABLE;
855
 
      goto fallback;
856
 
    }
857
 
    
858
 
#ifndef O_CLOEXEC
859
 
  /* Set the FD_CLOEXEC flag on the directory */
860
 
    ret = set_cloexec_flag(dir_fd);
861
 
    if(ret < 0){
862
 
      error(0, errno, "set_cloexec_flag");
863
 
      exitstatus = EX_OSERR;
864
 
      goto fallback;
865
 
    }
866
 
#endif  /* O_CLOEXEC */
867
 
  }
868
 
  
869
 
  int good_name(const struct dirent * const dirent){
870
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
871
 
                                      "*.dpkg-old", "*.dpkg-bak",
872
 
                                      "*.dpkg-divert", NULL };
873
 
#ifdef __GNUC__
874
 
#pragma GCC diagnostic push
875
 
#pragma GCC diagnostic ignored "-Wcast-qual"
876
 
#endif
877
 
    for(const char **pat = (const char **)patterns;
878
 
        *pat != NULL; pat++){
879
 
#ifdef __GNUC__
880
 
#pragma GCC diagnostic pop
881
 
#endif
882
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
883
 
         != FNM_NOMATCH){
884
 
        if(debug){
885
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
886
 
                    " matching pattern %s\n", dirent->d_name, *pat);
887
 
        }
888
 
        return 0;
 
715
    int dir_fd = dirfd(dir);
 
716
    if(dir_fd >= 0){
 
717
      ret = set_cloexec_flag(dir_fd);
 
718
      if(ret < 0){
 
719
        perror("set_cloexec_flag");
 
720
        exitstatus = EXIT_FAILURE;
 
721
        goto fallback;
889
722
      }
890
723
    }
891
 
    return 1;
892
 
  }
893
 
  
894
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
895
 
                             alphasort);
896
 
  if(numplugins == -1){
897
 
    error(0, errno, "Could not scan plugin dir");
898
 
    direntries = NULL;
899
 
    exitstatus = EX_OSERR;
900
 
    goto fallback;
901
724
  }
902
725
  
903
726
  FD_ZERO(&rfds_all);
904
727
  
905
728
  /* Read and execute any executable in the plugin directory*/
906
 
  for(int i = 0; i < numplugins; i++){
907
 
    
908
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
909
 
    if(plugin_fd == -1){
910
 
      error(0, errno, "Could not open plugin");
911
 
      free(direntries[i]);
 
729
  while(true){
 
730
    do {
 
731
      dirst = readdir(dir);
 
732
    } while(dirst == NULL and errno == EINTR);
 
733
    
 
734
    /* All directory entries have been processed */
 
735
    if(dirst == NULL){
 
736
      if(errno == EBADF){
 
737
        perror("readdir");
 
738
        exitstatus = EXIT_FAILURE;
 
739
        goto fallback;
 
740
      }
 
741
      break;
 
742
    }
 
743
    
 
744
    d_name_len = strlen(dirst->d_name);
 
745
    
 
746
    /* Ignore dotfiles, backup files and other junk */
 
747
    {
 
748
      bool bad_name = false;
 
749
      
 
750
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
751
      
 
752
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
753
                                           ".dpkg-old",
 
754
                                           ".dpkg-bak",
 
755
                                           ".dpkg-divert", NULL };
 
756
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
757
        size_t pre_len = strlen(*pre);
 
758
        if((d_name_len >= pre_len)
 
759
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
 
760
          if(debug){
 
761
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
762
                    " with bad prefix %s\n", dirst->d_name, *pre);
 
763
          }
 
764
          bad_name = true;
 
765
          break;
 
766
        }
 
767
      }
 
768
      if(bad_name){
 
769
        continue;
 
770
      }
 
771
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
772
        size_t suf_len = strlen(*suf);
 
773
        if((d_name_len >= suf_len)
 
774
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
775
                == 0)){
 
776
          if(debug){
 
777
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
778
                    " with bad suffix %s\n", dirst->d_name, *suf);
 
779
          }
 
780
          bad_name = true;
 
781
          break;
 
782
        }
 
783
      }
 
784
      
 
785
      if(bad_name){
 
786
        continue;
 
787
      }
 
788
    }
 
789
    
 
790
    char *filename;
 
791
    if(plugindir == NULL){
 
792
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
793
                                             dirst->d_name));
 
794
    } else {
 
795
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
796
                                             plugindir,
 
797
                                             dirst->d_name));
 
798
    }
 
799
    if(ret < 0){
 
800
      perror("asprintf");
912
801
      continue;
913
802
    }
914
 
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
 
803
    
 
804
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
915
805
    if(ret == -1){
916
 
      error(0, errno, "stat");
917
 
      close(plugin_fd);
918
 
      free(direntries[i]);
 
806
      perror("stat");
 
807
      free(filename);
919
808
      continue;
920
809
    }
921
810
    
922
811
    /* Ignore non-executable files */
923
812
    if(not S_ISREG(st.st_mode)
924
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
925
 
                                        X_OK, 0)) != 0)){
 
813
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
926
814
      if(debug){
927
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
928
 
                " with bad type or mode\n",
929
 
                plugindir != NULL ? plugindir : PDIR,
930
 
                direntries[i]->d_name);
 
815
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
816
                " with bad type or mode\n", filename);
931
817
      }
932
 
      close(plugin_fd);
933
 
      free(direntries[i]);
 
818
      free(filename);
934
819
      continue;
935
820
    }
936
821
    
937
 
    plugin *p = getplugin(direntries[i]->d_name);
 
822
    plugin *p = getplugin(dirst->d_name);
938
823
    if(p == NULL){
939
 
      error(0, errno, "getplugin");
940
 
      close(plugin_fd);
941
 
      free(direntries[i]);
 
824
      perror("getplugin");
 
825
      free(filename);
942
826
      continue;
943
827
    }
944
828
    if(p->disabled){
945
829
      if(debug){
946
830
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
947
 
                direntries[i]->d_name);
 
831
                dirst->d_name);
948
832
      }
949
 
      close(plugin_fd);
950
 
      free(direntries[i]);
 
833
      free(filename);
951
834
      continue;
952
835
    }
953
836
    {
956
839
      if(g != NULL){
957
840
        for(char **a = g->argv + 1; *a != NULL; a++){
958
841
          if(not add_argument(p, *a)){
959
 
            error(0, errno, "add_argument");
 
842
            perror("add_argument");
960
843
          }
961
844
        }
962
845
        /* Add global environment variables */
963
846
        for(char **e = g->environ; *e != NULL; e++){
964
847
          if(not add_environment(p, *e, false)){
965
 
            error(0, errno, "add_environment");
 
848
            perror("add_environment");
966
849
          }
967
850
        }
968
851
      }
969
852
    }
970
 
    /* If this plugin has any environment variables, we need to
971
 
       duplicate the environment from this process, too. */
 
853
    /* If this plugin has any environment variables, we will call
 
854
       using execve and need to duplicate the environment from this
 
855
       process, too. */
972
856
    if(p->environ[0] != NULL){
973
857
      for(char **e = environ; *e != NULL; e++){
974
858
        if(not add_environment(p, *e, false)){
975
 
          error(0, errno, "add_environment");
 
859
          perror("add_environment");
976
860
        }
977
861
      }
978
862
    }
979
863
    
980
864
    int pipefd[2];
981
 
#ifndef O_CLOEXEC
982
865
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
983
 
#else  /* O_CLOEXEC */
984
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
985
 
#endif  /* O_CLOEXEC */
986
866
    if(ret == -1){
987
 
      error(0, errno, "pipe");
988
 
      exitstatus = EX_OSERR;
989
 
      free(direntries[i]);
990
 
      goto fallback;
991
 
    }
992
 
    if(pipefd[0] >= FD_SETSIZE){
993
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
994
 
              FD_SETSIZE);
995
 
      close(pipefd[0]);
996
 
      close(pipefd[1]);
997
 
      exitstatus = EX_OSERR;
998
 
      free(direntries[i]);
999
 
      goto fallback;
1000
 
    }
1001
 
#ifndef O_CLOEXEC
 
867
      perror("pipe");
 
868
      exitstatus = EXIT_FAILURE;
 
869
      goto fallback;
 
870
    }
1002
871
    /* Ask OS to automatic close the pipe on exec */
1003
872
    ret = set_cloexec_flag(pipefd[0]);
1004
873
    if(ret < 0){
1005
 
      error(0, errno, "set_cloexec_flag");
1006
 
      close(pipefd[0]);
1007
 
      close(pipefd[1]);
1008
 
      exitstatus = EX_OSERR;
1009
 
      free(direntries[i]);
 
874
      perror("set_cloexec_flag");
 
875
      exitstatus = EXIT_FAILURE;
1010
876
      goto fallback;
1011
877
    }
1012
878
    ret = set_cloexec_flag(pipefd[1]);
1013
879
    if(ret < 0){
1014
 
      error(0, errno, "set_cloexec_flag");
1015
 
      close(pipefd[0]);
1016
 
      close(pipefd[1]);
1017
 
      exitstatus = EX_OSERR;
1018
 
      free(direntries[i]);
 
880
      perror("set_cloexec_flag");
 
881
      exitstatus = EXIT_FAILURE;
1019
882
      goto fallback;
1020
883
    }
1021
 
#endif  /* not O_CLOEXEC */
1022
884
    /* Block SIGCHLD until process is safely in process list */
1023
885
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1024
886
                                              &sigchld_action.sa_mask,
1025
887
                                              NULL));
1026
888
    if(ret < 0){
1027
 
      error(0, errno, "sigprocmask");
1028
 
      exitstatus = EX_OSERR;
1029
 
      free(direntries[i]);
 
889
      perror("sigprocmask");
 
890
      exitstatus = EXIT_FAILURE;
1030
891
      goto fallback;
1031
892
    }
1032
893
    /* Starting a new process to be watched */
1035
896
      pid = fork();
1036
897
    } while(pid == -1 and errno == EINTR);
1037
898
    if(pid == -1){
1038
 
      error(0, errno, "fork");
1039
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1040
 
                                     &sigchld_action.sa_mask, NULL));
1041
 
      close(pipefd[0]);
1042
 
      close(pipefd[1]);
1043
 
      exitstatus = EX_OSERR;
1044
 
      free(direntries[i]);
 
899
      perror("fork");
 
900
      exitstatus = EXIT_FAILURE;
1045
901
      goto fallback;
1046
902
    }
1047
903
    if(pid == 0){
1048
904
      /* this is the child process */
1049
905
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1050
906
      if(ret < 0){
1051
 
        error(0, errno, "sigaction");
1052
 
        _exit(EX_OSERR);
 
907
        perror("sigaction");
 
908
        _exit(EXIT_FAILURE);
1053
909
      }
1054
910
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1055
911
      if(ret < 0){
1056
 
        error(0, errno, "sigprocmask");
1057
 
        _exit(EX_OSERR);
 
912
        perror("sigprocmask");
 
913
        _exit(EXIT_FAILURE);
1058
914
      }
1059
915
      
1060
916
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
1061
917
      if(ret == -1){
1062
 
        error(0, errno, "dup2");
1063
 
        _exit(EX_OSERR);
 
918
        perror("dup2");
 
919
        _exit(EXIT_FAILURE);
1064
920
      }
1065
921
      
1066
 
      if(fexecve(plugin_fd, p->argv,
1067
 
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1068
 
        error(0, errno, "fexecve for %s/%s",
1069
 
              plugindir != NULL ? plugindir : PDIR,
1070
 
              direntries[i]->d_name);
1071
 
        _exit(EX_OSERR);
 
922
      if(dirfd(dir) < 0){
 
923
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
924
           above and must now close it manually here. */
 
925
        closedir(dir);
 
926
      }
 
927
      if(p->environ[0] == NULL){
 
928
        if(execv(filename, p->argv) < 0){
 
929
          perror("execv");
 
930
          _exit(EXIT_FAILURE);
 
931
        }
 
932
      } else {
 
933
        if(execve(filename, p->argv, p->environ) < 0){
 
934
          perror("execve");
 
935
          _exit(EXIT_FAILURE);
 
936
        }
1072
937
      }
1073
938
      /* no return */
1074
939
    }
1075
940
    /* Parent process */
1076
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1077
 
    close(plugin_fd);
1078
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
941
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
942
                                             pipe */
 
943
    free(filename);
 
944
    plugin *new_plugin = getplugin(dirst->d_name);
1079
945
    if(new_plugin == NULL){
1080
 
      error(0, errno, "getplugin");
 
946
      perror("getplugin");
1081
947
      ret = (int)(TEMP_FAILURE_RETRY
1082
948
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1083
949
                               NULL)));
1084
950
      if(ret < 0){
1085
 
        error(0, errno, "sigprocmask");
 
951
        perror("sigprocmask");
1086
952
      }
1087
 
      exitstatus = EX_OSERR;
1088
 
      free(direntries[i]);
 
953
      exitstatus = EXIT_FAILURE;
1089
954
      goto fallback;
1090
955
    }
1091
 
    free(direntries[i]);
1092
956
    
1093
957
    new_plugin->pid = pid;
1094
958
    new_plugin->fd = pipefd[0];
1099
963
                                              &sigchld_action.sa_mask,
1100
964
                                              NULL));
1101
965
    if(ret < 0){
1102
 
      error(0, errno, "sigprocmask");
1103
 
      exitstatus = EX_OSERR;
 
966
      perror("sigprocmask");
 
967
      exitstatus = EXIT_FAILURE;
1104
968
      goto fallback;
1105
969
    }
1106
970
    
1111
975
    }
1112
976
  }
1113
977
  
1114
 
  free(direntries);
1115
 
  direntries = NULL;
1116
 
  close(dir_fd);
1117
 
  dir_fd = -1;
 
978
  TEMP_FAILURE_RETRY(closedir(dir));
 
979
  dir = NULL;
1118
980
  free_plugin(getplugin(NULL));
1119
981
  
1120
982
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1133
995
    fd_set rfds = rfds_all;
1134
996
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1135
997
    if(select_ret == -1 and errno != EINTR){
1136
 
      error(0, errno, "select");
1137
 
      exitstatus = EX_OSERR;
 
998
      perror("select");
 
999
      exitstatus = EXIT_FAILURE;
1138
1000
      goto fallback;
1139
1001
    }
1140
1002
    /* OK, now either a process completed, or something can be read
1174
1036
                                         &sigchld_action.sa_mask,
1175
1037
                                         NULL));
1176
1038
          if(ret < 0){
1177
 
            error(0, errno, "sigprocmask");
1178
 
            exitstatus = EX_OSERR;
 
1039
            perror("sigprocmask");
 
1040
            exitstatus = EXIT_FAILURE;
1179
1041
            goto fallback;
1180
1042
          }
1181
1043
          
1188
1050
                      (sigprocmask(SIG_UNBLOCK,
1189
1051
                                   &sigchld_action.sa_mask, NULL)));
1190
1052
          if(ret < 0){
1191
 
            error(0, errno, "sigprocmask");
1192
 
            exitstatus = EX_OSERR;
 
1053
            perror("sigprocmask");
 
1054
            exitstatus = EXIT_FAILURE;
1193
1055
            goto fallback;
1194
1056
          }
1195
1057
          
1205
1067
        bool bret = print_out_password(proc->buffer,
1206
1068
                                       proc->buffer_length);
1207
1069
        if(not bret){
1208
 
          error(0, errno, "print_out_password");
1209
 
          exitstatus = EX_IOERR;
 
1070
          perror("print_out_password");
 
1071
          exitstatus = EXIT_FAILURE;
1210
1072
        }
1211
1073
        goto fallback;
1212
1074
      }
1219
1081
      }
1220
1082
      /* Before reading, make the process' data buffer large enough */
1221
1083
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1222
 
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
1223
 
                                   + (size_t) BUFFER_SIZE);
1224
 
        if(new_buffer == NULL){
1225
 
          error(0, errno, "malloc");
1226
 
          exitstatus = EX_OSERR;
 
1084
        proc->buffer = realloc(proc->buffer, proc->buffer_size
 
1085
                               + (size_t) BUFFER_SIZE);
 
1086
        if(proc->buffer == NULL){
 
1087
          perror("malloc");
 
1088
          exitstatus = EXIT_FAILURE;
1227
1089
          goto fallback;
1228
1090
        }
1229
 
        proc->buffer = new_buffer;
1230
1091
        proc->buffer_size += BUFFER_SIZE;
1231
1092
      }
1232
1093
      /* Read from the process */
1251
1112
  
1252
1113
 fallback:
1253
1114
  
1254
 
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
1255
 
                             and exitstatus != EX_OK)){
 
1115
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1256
1116
    /* Fallback if all plugins failed, none are found or an error
1257
1117
       occured */
1258
1118
    bool bret;
1266
1126
    }
1267
1127
    bret = print_out_password(passwordbuffer, len);
1268
1128
    if(not bret){
1269
 
      error(0, errno, "print_out_password");
1270
 
      exitstatus = EX_IOERR;
 
1129
      perror("print_out_password");
 
1130
      exitstatus = EXIT_FAILURE;
1271
1131
    }
1272
1132
  }
1273
1133
  
1274
1134
  /* Restore old signal handler */
1275
1135
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1276
1136
  if(ret == -1){
1277
 
    error(0, errno, "sigaction");
1278
 
    exitstatus = EX_OSERR;
 
1137
    perror("sigaction");
 
1138
    exitstatus = EXIT_FAILURE;
1279
1139
  }
1280
1140
  
1281
1141
  if(custom_argv != NULL){
1285
1145
    free(custom_argv);
1286
1146
  }
1287
1147
  
1288
 
  free(direntries);
1289
 
  
1290
 
  if(dir_fd != -1){
1291
 
    close(dir_fd);
 
1148
  if(dir != NULL){
 
1149
    closedir(dir);
1292
1150
  }
1293
1151
  
1294
1152
  /* Kill the processes */
1298
1156
      ret = kill(p->pid, SIGTERM);
1299
1157
      if(ret == -1 and errno != ESRCH){
1300
1158
        /* Set-uid proccesses might not get closed */
1301
 
        error(0, errno, "kill");
 
1159
        perror("kill");
1302
1160
      }
1303
1161
    }
1304
1162
  }
1308
1166
    ret = wait(NULL);
1309
1167
  } while(ret >= 0);
1310
1168
  if(errno != ECHILD){
1311
 
    error(0, errno, "wait");
 
1169
    perror("wait");
1312
1170
  }
1313
1171
  
1314
1172
  free_plugin_list();
1315
1173
  
1316
1174
  free(plugindir);
1317
 
  free(pluginhelperdir);
1318
1175
  free(argfile);
1319
1176
  
1320
1177
  return exitstatus;