/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2009-10-24 17:56:24 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091024175624-86qzvjlfp3qd8y8n
* mandos-keygen (keygen): Warn about long key generation time.

Show diffs side-by-side

added added

removed removed

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