/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: 2014-06-13 22:13:56 UTC
  • Revision ID: teddy@recompile.se-20140613221356-b9c36ctxmw4pelja
Fix free() of possibly uninitialized pointer in mandos-client.

* plugins.d/mandos-client (main): Initialize "direntries" to NULL so
                                  it can be free()'d even if scandir()
                                  fails.

Show diffs side-by-side

added added

removed removed

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