/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-03-23 20:48:58 UTC
  • Revision ID: teddy@recompile.se-20140323204858-l1x3aiick21oog4y
Improve error message when working around Debian bug #633582.

* plugins-runner.c (main): Improve error message when failing to work
                           around Debian bug #633582.  Also simplify
                           code to always use execve().

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