/mandos/trunk

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2014-06-07 20:29:36 UTC
  • Revision ID: teddy@recompile.se-20140607202936-crh5fxrdd804oora
Fix typo in code comment.

* debian/mandos.postinst: Fix typo in comment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2014 Teddy Hogeborn
 
6
 * Copyright © 2008-2014 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   O_CLOEXEC */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
29
                                   realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
31
#include <stdio.h>              /* fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO, fclose() */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), fstat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
41
                                   WEXITSTATUS(), WTERMSIG(),
42
42
                                   WCOREDUMP() */
43
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
43
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
44
44
#include <iso646.h>             /* and, or, not */
45
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
46
                                   readdir(), closedir(), dirfd() */
47
 
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
 
                                   fcntl(), setuid(), setgid(),
49
 
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
50
 
                                   access(), pipe(), fork(), close()
51
 
                                   dup2(), STDOUT_FILENO, _exit(),
52
 
                                   execv(), write(), read(),
53
 
                                   close() */
 
47
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
 
48
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
 
49
                                   struct stat, fstat(), close(),
 
50
                                   setgid(), setuid(), S_ISREG(),
 
51
                                   faccessat() pipe(), fork(),
 
52
                                   _exit(), dup2(), fexecve(), read()
 
53
                                */
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
 
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
55
                                   FD_CLOEXEC, openat() */
 
56
#include <string.h>             /* strsep, strlen(), strsignal(),
 
57
                                   strcmp(), strncmp() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
70
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
72
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
 
73
#include <errno.h>              /* errno */
 
74
#include <error.h>              /* error() */
 
75
#include <fnmatch.h>            /* fnmatch() */
73
76
 
74
77
#define BUFFER_SIZE 256
75
78
 
77
80
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
78
81
 
79
82
const char *argp_program_version = "plugin-runner " VERSION;
80
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
83
const char *argp_program_bug_address = "<mandos@recompile.se>";
81
84
 
82
85
typedef struct plugin{
83
86
  char *name;                   /* can be NULL or any plugin name */
103
106
 
104
107
/* Gets an existing plugin based on name,
105
108
   or if none is found, creates a new one */
 
109
__attribute__((warn_unused_result))
106
110
static plugin *getplugin(char *name){
107
111
  /* Check for existing plugin with that name */
108
112
  for(plugin *p = plugin_list; p != NULL; p = p->next){
169
173
}
170
174
 
171
175
/* Helper function for add_argument and add_environment */
 
176
__attribute__((nonnull, warn_unused_result))
172
177
static bool add_to_char_array(const char *new, char ***array,
173
178
                              int *len){
174
179
  /* Resize the pointed-to array to hold one more pointer */
 
180
  char **new_array = NULL;
175
181
  do {
176
 
    *array = realloc(*array, sizeof(char *)
177
 
                     * (size_t) ((*len) + 2));
178
 
  } while(*array == NULL and errno == EINTR);
 
182
    new_array = realloc(*array, sizeof(char *)
 
183
                        * (size_t) ((*len) + 2));
 
184
  } while(new_array == NULL and errno == EINTR);
179
185
  /* Malloc check */
180
 
  if(*array == NULL){
 
186
  if(new_array == NULL){
181
187
    return false;
182
188
  }
 
189
  *array = new_array;
183
190
  /* Make a copy of the new string */
184
191
  char *copy;
185
192
  do {
197
204
}
198
205
 
199
206
/* Add to a plugin's argument vector */
 
207
__attribute__((nonnull(2), warn_unused_result))
200
208
static bool add_argument(plugin *p, const char *arg){
201
209
  if(p == NULL){
202
210
    return false;
205
213
}
206
214
 
207
215
/* Add to a plugin's environment */
 
216
__attribute__((nonnull(2), warn_unused_result))
208
217
static bool add_environment(plugin *p, const char *def, bool replace){
209
218
  if(p == NULL){
210
219
    return false;
212
221
  /* namelen = length of name of environment variable */
213
222
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
214
223
  /* Search for this environment variable */
215
 
  for(char **e = p->environ; *e != NULL; e++){
216
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
224
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
225
    if(strncmp(*envdef, def, namelen + 1) == 0){
217
226
      /* It already exists */
218
227
      if(replace){
219
 
        char *new;
 
228
        char *new_envdef;
220
229
        do {
221
 
          new = realloc(*e, strlen(def) + 1);
222
 
        } while(new == NULL and errno == EINTR);
223
 
        if(new == NULL){
 
230
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
231
        } while(new_envdef == NULL and errno == EINTR);
 
232
        if(new_envdef == NULL){
224
233
          return false;
225
234
        }
226
 
        *e = new;
227
 
        strcpy(*e, def);
 
235
        *envdef = new_envdef;
 
236
        strcpy(*envdef, def);
228
237
      }
229
238
      return true;
230
239
    }
237
246
 * Descriptor Flags".
238
247
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
239
248
 */
 
249
__attribute__((warn_unused_result))
240
250
static int set_cloexec_flag(int fd){
241
251
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
242
252
  /* If reading the flags failed, return error indication now. */
266
276
        /* No child processes */
267
277
        break;
268
278
      }
269
 
      perror("waitpid");
 
279
      error(0, errno, "waitpid");
270
280
    }
271
281
    
272
282
    /* A child exited, find it in process_list */
284
294
}
285
295
 
286
296
/* Prints out a password to stdout */
 
297
__attribute__((nonnull, warn_unused_result))
287
298
static bool print_out_password(const char *buffer, size_t length){
288
299
  ssize_t ret;
289
300
  for(size_t written = 0; written < length; written += (size_t)ret){
297
308
}
298
309
 
299
310
/* Removes and free a plugin from the plugin list */
 
311
__attribute__((nonnull))
300
312
static void free_plugin(plugin *plugin_node){
301
313
  
302
314
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
336
348
  char *plugindir = NULL;
337
349
  char *argfile = NULL;
338
350
  FILE *conffp;
339
 
  size_t d_name_len;
340
351
  DIR *dir = NULL;
341
352
  struct dirent *dirst;
342
353
  struct stat st;
352
363
                                      .sa_flags = SA_NOCLDSTOP };
353
364
  char **custom_argv = NULL;
354
365
  int custom_argc = 0;
 
366
  int dir_fd;
355
367
  
356
368
  /* Establish a signal handler */
357
369
  sigemptyset(&sigchld_action.sa_mask);
358
370
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
359
371
  if(ret == -1){
360
 
    perror("sigaddset");
 
372
    error(0, errno, "sigaddset");
361
373
    exitstatus = EX_OSERR;
362
374
    goto fallback;
363
375
  }
364
376
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
365
377
  if(ret == -1){
366
 
    perror("sigaction");
 
378
    error(0, errno, "sigaction");
367
379
    exitstatus = EX_OSERR;
368
380
    goto fallback;
369
381
  }
414
426
    { .name = NULL }
415
427
  };
416
428
  
 
429
  __attribute__((nonnull(3)))
417
430
  error_t parse_opt(int key, char *arg, struct argp_state *state){
418
431
    errno = 0;
419
432
    switch(key){
420
433
      char *tmp;
421
 
      intmax_t tmpmax;
 
434
      intmax_t tmp_id;
422
435
    case 'g':                   /* --global-options */
423
436
      {
424
437
        char *plugin_option;
427
440
            break;
428
441
          }
429
442
        }
 
443
        errno = 0;
430
444
      }
431
445
      break;
432
446
    case 'G':                   /* --global-env */
433
 
      add_environment(getplugin(NULL), arg, true);
 
447
      if(add_environment(getplugin(NULL), arg, true)){
 
448
        errno = 0;
 
449
      }
434
450
      break;
435
451
    case 'o':                   /* --options-for */
436
452
      {
453
469
            break;
454
470
          }
455
471
        }
 
472
        errno = 0;
456
473
      }
457
474
      break;
458
475
    case 'E':                   /* --env-for */
470
487
          errno = EINVAL;
471
488
          break;
472
489
        }
473
 
        add_environment(getplugin(arg), envdef, true);
 
490
        if(add_environment(getplugin(arg), envdef, true)){
 
491
          errno = 0;
 
492
        }
474
493
      }
475
494
      break;
476
495
    case 'd':                   /* --disable */
478
497
        plugin *p = getplugin(arg);
479
498
        if(p != NULL){
480
499
          p->disabled = true;
 
500
          errno = 0;
481
501
        }
482
502
      }
483
503
      break;
486
506
        plugin *p = getplugin(arg);
487
507
        if(p != NULL){
488
508
          p->disabled = false;
 
509
          errno = 0;
489
510
        }
490
511
      }
491
512
      break;
492
513
    case 128:                   /* --plugin-dir */
493
514
      free(plugindir);
494
515
      plugindir = strdup(arg);
 
516
      if(plugindir != NULL){
 
517
        errno = 0;
 
518
      }
495
519
      break;
496
520
    case 129:                   /* --config-file */
497
521
      /* This is already done by parse_opt_config_file() */
498
522
      break;
499
523
    case 130:                   /* --userid */
500
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
524
      tmp_id = strtoimax(arg, &tmp, 10);
501
525
      if(errno != 0 or tmp == arg or *tmp != '\0'
502
 
         or tmpmax != (uid_t)tmpmax){
 
526
         or tmp_id != (uid_t)tmp_id){
503
527
        argp_error(state, "Bad user ID number: \"%s\", using %"
504
528
                   PRIdMAX, arg, (intmax_t)uid);
505
529
        break;
506
530
      }
507
 
      uid = (uid_t)tmpmax;
 
531
      uid = (uid_t)tmp_id;
 
532
      errno = 0;
508
533
      break;
509
534
    case 131:                   /* --groupid */
510
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
535
      tmp_id = strtoimax(arg, &tmp, 10);
511
536
      if(errno != 0 or tmp == arg or *tmp != '\0'
512
 
         or tmpmax != (gid_t)tmpmax){
 
537
         or tmp_id != (gid_t)tmp_id){
513
538
        argp_error(state, "Bad group ID number: \"%s\", using %"
514
539
                   PRIdMAX, arg, (intmax_t)gid);
515
540
        break;
516
541
      }
517
 
      gid = (gid_t)tmpmax;
 
542
      gid = (gid_t)tmp_id;
 
543
      errno = 0;
518
544
      break;
519
545
    case 132:                   /* --debug */
520
546
      debug = true;
568
594
    case 129:                   /* --config-file */
569
595
      free(argfile);
570
596
      argfile = strdup(arg);
 
597
      if(argfile != NULL){
 
598
        errno = 0;
 
599
      }
571
600
      break;
572
601
    case 130:                   /* --userid */
573
602
    case 131:                   /* --groupid */
599
628
  case ENOMEM:
600
629
  default:
601
630
    errno = ret;
602
 
    perror("argp_parse");
 
631
    error(0, errno, "argp_parse");
603
632
    exitstatus = EX_OSERR;
604
633
    goto fallback;
605
634
  case EINVAL:
626
655
    custom_argc = 1;
627
656
    custom_argv = malloc(sizeof(char*) * 2);
628
657
    if(custom_argv == NULL){
629
 
      perror("malloc");
 
658
      error(0, errno, "malloc");
630
659
      exitstatus = EX_OSERR;
631
660
      goto fallback;
632
661
    }
649
678
        }
650
679
        new_arg = strdup(p);
651
680
        if(new_arg == NULL){
652
 
          perror("strdup");
 
681
          error(0, errno, "strdup");
653
682
          exitstatus = EX_OSERR;
654
683
          free(org_line);
655
684
          goto fallback;
656
685
        }
657
686
        
658
687
        custom_argc += 1;
659
 
        custom_argv = realloc(custom_argv, sizeof(char *)
660
 
                              * ((unsigned int) custom_argc + 1));
661
 
        if(custom_argv == NULL){
662
 
          perror("realloc");
663
 
          exitstatus = EX_OSERR;
664
 
          free(org_line);
665
 
          goto fallback;
 
688
        {
 
689
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
690
                                    * ((unsigned int)
 
691
                                       custom_argc + 1));
 
692
          if(new_argv == NULL){
 
693
            error(0, errno, "realloc");
 
694
            exitstatus = EX_OSERR;
 
695
            free(new_arg);
 
696
            free(org_line);
 
697
            goto fallback;
 
698
          } else {
 
699
            custom_argv = new_argv;
 
700
          }
666
701
        }
667
702
        custom_argv[custom_argc-1] = new_arg;
668
703
        custom_argv[custom_argc] = NULL;
672
707
      ret = fclose(conffp);
673
708
    } while(ret == EOF and errno == EINTR);
674
709
    if(ret == EOF){
675
 
      perror("fclose");
 
710
      error(0, errno, "fclose");
676
711
      exitstatus = EX_IOERR;
677
712
      goto fallback;
678
713
    }
681
716
    /* Check for harmful errors and go to fallback. Other errors might
682
717
       not affect opening plugins */
683
718
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
684
 
      perror("fopen");
 
719
      error(0, errno, "fopen");
685
720
      exitstatus = EX_OSERR;
686
721
      goto fallback;
687
722
    }
698
733
    case ENOMEM:
699
734
    default:
700
735
      errno = ret;
701
 
      perror("argp_parse");
 
736
      error(0, errno, "argp_parse");
702
737
      exitstatus = EX_OSERR;
703
738
      goto fallback;
704
739
    case EINVAL:
718
753
  case ENOMEM:
719
754
  default:
720
755
    errno = ret;
721
 
    perror("argp_parse");
 
756
    error(0, errno, "argp_parse");
722
757
    exitstatus = EX_OSERR;
723
758
    goto fallback;
724
759
  case EINVAL:
740
775
    }
741
776
  }
742
777
  
743
 
  /* Strip permissions down to nobody */
744
 
  setgid(gid);
 
778
  if(getuid() == 0){
 
779
    /* Work around Debian bug #633582:
 
780
       <http://bugs.debian.org/633582> */
 
781
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
782
    if(plugindir_fd == -1){
 
783
      if(errno != ENOENT){
 
784
        error(0, errno, "open(\"" PDIR "\")");
 
785
      }
 
786
    } else {
 
787
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
788
      if(ret == -1){
 
789
        error(0, errno, "fstat");
 
790
      } else {
 
791
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
792
          ret = fchown(plugindir_fd, uid, gid);
 
793
          if(ret == -1){
 
794
            error(0, errno, "fchown");
 
795
          }
 
796
        }
 
797
      }
 
798
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
799
    }
 
800
  }
 
801
  
 
802
  /* Lower permissions */
 
803
  ret = setgid(gid);
745
804
  if(ret == -1){
746
 
    perror("setgid");
 
805
    error(0, errno, "setgid");
747
806
  }
748
807
  ret = setuid(uid);
749
808
  if(ret == -1){
750
 
    perror("setuid");
 
809
    error(0, errno, "setuid");
751
810
  }
752
811
  
753
812
  /* Open plugin directory with close_on_exec flag */
754
813
  {
755
 
    int dir_fd = -1;
756
 
    if(plugindir == NULL){
757
 
      dir_fd = open(PDIR, O_RDONLY |
758
 
#ifdef O_CLOEXEC
759
 
                    O_CLOEXEC
760
 
#else  /* not O_CLOEXEC */
761
 
                    0
762
 
#endif  /* not O_CLOEXEC */
763
 
                    );
764
 
    } else {
765
 
      dir_fd = open(plugindir, O_RDONLY |
766
 
#ifdef O_CLOEXEC
767
 
                    O_CLOEXEC
768
 
#else  /* not O_CLOEXEC */
769
 
                    0
770
 
#endif  /* not O_CLOEXEC */
771
 
                    );
772
 
    }
 
814
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
 
815
#ifdef O_CLOEXEC
 
816
                  O_CLOEXEC
 
817
#else  /* not O_CLOEXEC */
 
818
                  0
 
819
#endif  /* not O_CLOEXEC */
 
820
                  );
773
821
    if(dir_fd == -1){
774
 
      perror("Could not open plugin dir");
 
822
      error(0, errno, "Could not open plugin dir");
775
823
      exitstatus = EX_UNAVAILABLE;
776
824
      goto fallback;
777
825
    }
780
828
  /* Set the FD_CLOEXEC flag on the directory */
781
829
    ret = set_cloexec_flag(dir_fd);
782
830
    if(ret < 0){
783
 
      perror("set_cloexec_flag");
 
831
      error(0, errno, "set_cloexec_flag");
784
832
      TEMP_FAILURE_RETRY(close(dir_fd));
785
833
      exitstatus = EX_OSERR;
786
834
      goto fallback;
789
837
    
790
838
    dir = fdopendir(dir_fd);
791
839
    if(dir == NULL){
792
 
      perror("Could not open plugin dir");
 
840
      error(0, errno, "Could not open plugin dir");
793
841
      TEMP_FAILURE_RETRY(close(dir_fd));
794
842
      exitstatus = EX_OSERR;
795
843
      goto fallback;
807
855
    /* All directory entries have been processed */
808
856
    if(dirst == NULL){
809
857
      if(errno == EBADF){
810
 
        perror("readdir");
 
858
        error(0, errno, "readdir");
811
859
        exitstatus = EX_IOERR;
812
860
        goto fallback;
813
861
      }
814
862
      break;
815
863
    }
816
864
    
817
 
    d_name_len = strlen(dirst->d_name);
818
 
    
819
865
    /* Ignore dotfiles, backup files and other junk */
820
866
    {
821
867
      bool bad_name = false;
822
 
      
823
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
824
 
      
825
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
826
 
                                           ".dpkg-old",
827
 
                                           ".dpkg-bak",
828
 
                                           ".dpkg-divert", NULL };
829
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
830
 
        size_t pre_len = strlen(*pre);
831
 
        if((d_name_len >= pre_len)
832
 
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
833
 
          if(debug){
834
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
835
 
                    " with bad prefix %s\n", dirst->d_name, *pre);
836
 
          }
837
 
          bad_name = true;
838
 
          break;
839
 
        }
840
 
      }
841
 
      if(bad_name){
842
 
        continue;
843
 
      }
844
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
845
 
        size_t suf_len = strlen(*suf);
846
 
        if((d_name_len >= suf_len)
847
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
848
 
                == 0)){
849
 
          if(debug){
850
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
851
 
                    " with bad suffix %s\n", dirst->d_name, *suf);
852
 
          }
853
 
          bad_name = true;
854
 
          break;
855
 
        }
856
 
      }
857
 
      
 
868
      const char * const patterns[] = { ".*", "#*#", "*~",
 
869
                                        "*.dpkg-new", "*.dpkg-old",
 
870
                                        "*.dpkg-bak", "*.dpkg-divert",
 
871
                                        NULL };
 
872
#ifdef __GNUC__
 
873
#pragma GCC diagnostic push
 
874
#pragma GCC diagnostic ignored "-Wcast-qual"
 
875
#endif
 
876
      for(const char **pat = (const char **)patterns;
 
877
          *pat != NULL; pat++){
 
878
#ifdef __GNUC__
 
879
#pragma GCC diagnostic pop
 
880
#endif
 
881
        if(fnmatch(*pat, dirst->d_name,
 
882
                   FNM_FILE_NAME | FNM_PERIOD) != FNM_NOMATCH){
 
883
          if(debug){
 
884
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
885
                    " matching pattern %s\n", dirst->d_name, *pat);
 
886
          }
 
887
          bad_name = true;
 
888
          break;
 
889
        }
 
890
      }
858
891
      if(bad_name){
859
892
        continue;
860
893
      }
861
894
    }
862
895
    
863
 
    char *filename;
864
 
    if(plugindir == NULL){
865
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
866
 
                                             dirst->d_name));
867
 
    } else {
868
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
869
 
                                             plugindir,
870
 
                                             dirst->d_name));
 
896
    int plugin_fd = openat(dir_fd, dirst->d_name, O_RDONLY |
 
897
#ifdef O_CLOEXEC
 
898
                            O_CLOEXEC
 
899
#else  /* not O_CLOEXEC */
 
900
                            0
 
901
#endif  /* not O_CLOEXEC */
 
902
                            );
 
903
    if(plugin_fd == -1){
 
904
      error(0, errno, "Could not open plugin");
 
905
      continue;
871
906
    }
 
907
#ifndef O_CLOEXEC
 
908
  /* Set the FD_CLOEXEC flag on the plugin FD */
 
909
    ret = set_cloexec_flag(plugin_fd);
872
910
    if(ret < 0){
873
 
      perror("asprintf");
 
911
      error(0, errno, "set_cloexec_flag");
 
912
      TEMP_FAILURE_RETRY(close(plugin_fd));
874
913
      continue;
875
914
    }
876
 
    
877
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
915
#endif  /* O_CLOEXEC */
 
916
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
878
917
    if(ret == -1){
879
 
      perror("stat");
880
 
      free(filename);
 
918
      error(0, errno, "stat");
 
919
      TEMP_FAILURE_RETRY(close(plugin_fd));
881
920
      continue;
882
921
    }
883
922
    
884
923
    /* Ignore non-executable files */
885
924
    if(not S_ISREG(st.st_mode)
886
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
925
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, dirst->d_name, X_OK,
 
926
                                        0)) != 0)){
887
927
      if(debug){
888
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
889
 
                " with bad type or mode\n", filename);
 
928
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
 
929
                " with bad type or mode\n",
 
930
                plugindir != NULL ? plugindir : PDIR, dirst->d_name);
890
931
      }
891
 
      free(filename);
 
932
      TEMP_FAILURE_RETRY(close(plugin_fd));
892
933
      continue;
893
934
    }
894
935
    
895
936
    plugin *p = getplugin(dirst->d_name);
896
937
    if(p == NULL){
897
 
      perror("getplugin");
898
 
      free(filename);
 
938
      error(0, errno, "getplugin");
 
939
      TEMP_FAILURE_RETRY(close(plugin_fd));
899
940
      continue;
900
941
    }
901
942
    if(p->disabled){
903
944
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
904
945
                dirst->d_name);
905
946
      }
906
 
      free(filename);
 
947
      TEMP_FAILURE_RETRY(close(plugin_fd));
907
948
      continue;
908
949
    }
909
950
    {
912
953
      if(g != NULL){
913
954
        for(char **a = g->argv + 1; *a != NULL; a++){
914
955
          if(not add_argument(p, *a)){
915
 
            perror("add_argument");
 
956
            error(0, errno, "add_argument");
916
957
          }
917
958
        }
918
959
        /* Add global environment variables */
919
960
        for(char **e = g->environ; *e != NULL; e++){
920
961
          if(not add_environment(p, *e, false)){
921
 
            perror("add_environment");
 
962
            error(0, errno, "add_environment");
922
963
          }
923
964
        }
924
965
      }
925
966
    }
926
 
    /* If this plugin has any environment variables, we will call
927
 
       using execve and need to duplicate the environment from this
928
 
       process, too. */
 
967
    /* If this plugin has any environment variables, we need to
 
968
       duplicate the environment from this process, too. */
929
969
    if(p->environ[0] != NULL){
930
970
      for(char **e = environ; *e != NULL; e++){
931
971
        if(not add_environment(p, *e, false)){
932
 
          perror("add_environment");
 
972
          error(0, errno, "add_environment");
933
973
        }
934
974
      }
935
975
    }
937
977
    int pipefd[2];
938
978
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
939
979
    if(ret == -1){
940
 
      perror("pipe");
 
980
      error(0, errno, "pipe");
941
981
      exitstatus = EX_OSERR;
942
982
      goto fallback;
943
983
    }
944
984
    /* Ask OS to automatic close the pipe on exec */
945
985
    ret = set_cloexec_flag(pipefd[0]);
946
986
    if(ret < 0){
947
 
      perror("set_cloexec_flag");
 
987
      error(0, errno, "set_cloexec_flag");
948
988
      exitstatus = EX_OSERR;
949
989
      goto fallback;
950
990
    }
951
991
    ret = set_cloexec_flag(pipefd[1]);
952
992
    if(ret < 0){
953
 
      perror("set_cloexec_flag");
 
993
      error(0, errno, "set_cloexec_flag");
954
994
      exitstatus = EX_OSERR;
955
995
      goto fallback;
956
996
    }
959
999
                                              &sigchld_action.sa_mask,
960
1000
                                              NULL));
961
1001
    if(ret < 0){
962
 
      perror("sigprocmask");
 
1002
      error(0, errno, "sigprocmask");
963
1003
      exitstatus = EX_OSERR;
964
1004
      goto fallback;
965
1005
    }
969
1009
      pid = fork();
970
1010
    } while(pid == -1 and errno == EINTR);
971
1011
    if(pid == -1){
972
 
      perror("fork");
 
1012
      error(0, errno, "fork");
973
1013
      exitstatus = EX_OSERR;
974
1014
      goto fallback;
975
1015
    }
977
1017
      /* this is the child process */
978
1018
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
979
1019
      if(ret < 0){
980
 
        perror("sigaction");
 
1020
        error(0, errno, "sigaction");
981
1021
        _exit(EX_OSERR);
982
1022
      }
983
1023
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
984
1024
      if(ret < 0){
985
 
        perror("sigprocmask");
 
1025
        error(0, errno, "sigprocmask");
986
1026
        _exit(EX_OSERR);
987
1027
      }
988
1028
      
989
1029
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
990
1030
      if(ret == -1){
991
 
        perror("dup2");
 
1031
        error(0, errno, "dup2");
992
1032
        _exit(EX_OSERR);
993
1033
      }
994
1034
      
997
1037
           above and must now close it manually here. */
998
1038
        closedir(dir);
999
1039
      }
1000
 
      if(p->environ[0] == NULL){
1001
 
        if(execv(filename, p->argv) < 0){
1002
 
          perror("execv");
1003
 
          _exit(EX_OSERR);
1004
 
        }
1005
 
      } else {
1006
 
        if(execve(filename, p->argv, p->environ) < 0){
1007
 
          perror("execve");
1008
 
          _exit(EX_OSERR);
1009
 
        }
 
1040
      if(fexecve(plugin_fd, p->argv,
 
1041
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
 
1042
        error(0, errno, "fexecve for %s/%s",
 
1043
              plugindir != NULL ? plugindir : PDIR, dirst->d_name);
 
1044
        _exit(EX_OSERR);
1010
1045
      }
1011
1046
      /* no return */
1012
1047
    }
1013
1048
    /* Parent process */
1014
1049
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1015
1050
                                             pipe */
1016
 
    free(filename);
 
1051
    TEMP_FAILURE_RETRY(close(plugin_fd));
1017
1052
    plugin *new_plugin = getplugin(dirst->d_name);
1018
1053
    if(new_plugin == NULL){
1019
 
      perror("getplugin");
 
1054
      error(0, errno, "getplugin");
1020
1055
      ret = (int)(TEMP_FAILURE_RETRY
1021
1056
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1022
1057
                               NULL)));
1023
1058
      if(ret < 0){
1024
 
        perror("sigprocmask");
 
1059
        error(0, errno, "sigprocmask");
1025
1060
      }
1026
1061
      exitstatus = EX_OSERR;
1027
1062
      goto fallback;
1036
1071
                                              &sigchld_action.sa_mask,
1037
1072
                                              NULL));
1038
1073
    if(ret < 0){
1039
 
      perror("sigprocmask");
 
1074
      error(0, errno, "sigprocmask");
1040
1075
      exitstatus = EX_OSERR;
1041
1076
      goto fallback;
1042
1077
    }
1043
1078
    
 
1079
#if defined (__GNUC__) and defined (__GLIBC__)
 
1080
#if not __GLIBC_PREREQ(2, 16)
 
1081
#pragma GCC diagnostic push
 
1082
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1083
#endif
 
1084
#endif
1044
1085
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1045
 
                                          -Wconversion */
 
1086
                                          -Wconversion in GNU libc
 
1087
                                          before 2.16 */
 
1088
#if defined (__GNUC__) and defined (__GLIBC__)
 
1089
#if not __GLIBC_PREREQ(2, 16)
 
1090
#pragma GCC diagnostic pop
 
1091
#endif
 
1092
#endif
1046
1093
    
1047
1094
    if(maxfd < new_plugin->fd){
1048
1095
      maxfd = new_plugin->fd;
1069
1116
    fd_set rfds = rfds_all;
1070
1117
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1071
1118
    if(select_ret == -1 and errno != EINTR){
1072
 
      perror("select");
 
1119
      error(0, errno, "select");
1073
1120
      exitstatus = EX_OSERR;
1074
1121
      goto fallback;
1075
1122
    }
1102
1149
          }
1103
1150
          
1104
1151
          /* Remove the plugin */
 
1152
#if defined (__GNUC__) and defined (__GLIBC__)
 
1153
#if not __GLIBC_PREREQ(2, 16)
 
1154
#pragma GCC diagnostic push
 
1155
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1156
#endif
 
1157
#endif
1105
1158
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1106
 
                                          -Wconversion */
 
1159
                                          -Wconversion in GNU libc
 
1160
                                          before 2.16 */
 
1161
#if defined (__GNUC__) and defined (__GLIBC__)
 
1162
#if not __GLIBC_PREREQ(2, 16)
 
1163
#pragma GCC diagnostic pop
 
1164
#endif
 
1165
#endif
1107
1166
          
1108
1167
          /* Block signal while modifying process_list */
1109
1168
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1111
1170
                                         &sigchld_action.sa_mask,
1112
1171
                                         NULL));
1113
1172
          if(ret < 0){
1114
 
            perror("sigprocmask");
 
1173
            error(0, errno, "sigprocmask");
1115
1174
            exitstatus = EX_OSERR;
1116
1175
            goto fallback;
1117
1176
          }
1125
1184
                      (sigprocmask(SIG_UNBLOCK,
1126
1185
                                   &sigchld_action.sa_mask, NULL)));
1127
1186
          if(ret < 0){
1128
 
            perror("sigprocmask");
 
1187
            error(0, errno, "sigprocmask");
1129
1188
            exitstatus = EX_OSERR;
1130
1189
            goto fallback;
1131
1190
          }
1142
1201
        bool bret = print_out_password(proc->buffer,
1143
1202
                                       proc->buffer_length);
1144
1203
        if(not bret){
1145
 
          perror("print_out_password");
 
1204
          error(0, errno, "print_out_password");
1146
1205
          exitstatus = EX_IOERR;
1147
1206
        }
1148
1207
        goto fallback;
1149
1208
      }
1150
1209
      
1151
1210
      /* This process has not completed.  Does it have any output? */
 
1211
#if defined (__GNUC__) and defined (__GLIBC__)
 
1212
#if not __GLIBC_PREREQ(2, 16)
 
1213
#pragma GCC diagnostic push
 
1214
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1215
#endif
 
1216
#endif
1152
1217
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1153
1218
                                                         warning from
1154
 
                                                         -Wconversion */
 
1219
                                                         -Wconversion
 
1220
                                                         in GNU libc
 
1221
                                                         before
 
1222
                                                         2.16 */
 
1223
#if defined (__GNUC__) and defined (__GLIBC__)
 
1224
#if not __GLIBC_PREREQ(2, 16)
 
1225
#pragma GCC diagnostic pop
 
1226
#endif
 
1227
#endif
1155
1228
        /* This process had nothing to say at this time */
1156
1229
        proc = proc->next;
1157
1230
        continue;
1158
1231
      }
1159
1232
      /* Before reading, make the process' data buffer large enough */
1160
1233
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1161
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1162
 
                               + (size_t) BUFFER_SIZE);
1163
 
        if(proc->buffer == NULL){
1164
 
          perror("malloc");
 
1234
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1235
                                   + (size_t) BUFFER_SIZE);
 
1236
        if(new_buffer == NULL){
 
1237
          error(0, errno, "malloc");
1165
1238
          exitstatus = EX_OSERR;
1166
1239
          goto fallback;
1167
1240
        }
 
1241
        proc->buffer = new_buffer;
1168
1242
        proc->buffer_size += BUFFER_SIZE;
1169
1243
      }
1170
1244
      /* Read from the process */
1204
1278
    }
1205
1279
    bret = print_out_password(passwordbuffer, len);
1206
1280
    if(not bret){
1207
 
      perror("print_out_password");
 
1281
      error(0, errno, "print_out_password");
1208
1282
      exitstatus = EX_IOERR;
1209
1283
    }
1210
1284
  }
1212
1286
  /* Restore old signal handler */
1213
1287
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1214
1288
  if(ret == -1){
1215
 
    perror("sigaction");
 
1289
    error(0, errno, "sigaction");
1216
1290
    exitstatus = EX_OSERR;
1217
1291
  }
1218
1292
  
1234
1308
      ret = kill(p->pid, SIGTERM);
1235
1309
      if(ret == -1 and errno != ESRCH){
1236
1310
        /* Set-uid proccesses might not get closed */
1237
 
        perror("kill");
 
1311
        error(0, errno, "kill");
1238
1312
      }
1239
1313
    }
1240
1314
  }
1244
1318
    ret = wait(NULL);
1245
1319
  } while(ret >= 0);
1246
1320
  if(errno != ECHILD){
1247
 
    perror("wait");
 
1321
    error(0, errno, "wait");
1248
1322
  }
1249
1323
  
1250
1324
  free_plugin_list();