/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-01 09:51:07 UTC
  • Revision ID: teddy@recompile.se-20140301095107-wfcwbmol2a71d3xz
* mandos-keygen (keygen): Add workaround for Debian bug #737128.

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-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
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
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
56
56
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
57
                                   strsignal(), strcmp(), strncmp() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
68
68
                                */
69
69
#include <errno.h>              /* errno, EBADF */
70
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
 
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() */
72
75
 
73
76
#define BUFFER_SIZE 256
74
77
 
76
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
77
80
 
78
81
const char *argp_program_version = "plugin-runner " VERSION;
79
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
82
const char *argp_program_bug_address = "<mandos@recompile.se>";
80
83
 
81
84
typedef struct plugin{
82
85
  char *name;                   /* can be NULL or any plugin name */
168
171
}
169
172
 
170
173
/* Helper function for add_argument and add_environment */
 
174
__attribute__((nonnull))
171
175
static bool add_to_char_array(const char *new, char ***array,
172
176
                              int *len){
173
177
  /* Resize the pointed-to array to hold one more pointer */
196
200
}
197
201
 
198
202
/* Add to a plugin's argument vector */
 
203
__attribute__((nonnull(2)))
199
204
static bool add_argument(plugin *p, const char *arg){
200
205
  if(p == NULL){
201
206
    return false;
204
209
}
205
210
 
206
211
/* Add to a plugin's environment */
 
212
__attribute__((nonnull(2)))
207
213
static bool add_environment(plugin *p, const char *def, bool replace){
208
214
  if(p == NULL){
209
215
    return false;
265
271
        /* No child processes */
266
272
        break;
267
273
      }
268
 
      perror("waitpid");
 
274
      error(0, errno, "waitpid");
269
275
    }
270
276
    
271
277
    /* A child exited, find it in process_list */
283
289
}
284
290
 
285
291
/* Prints out a password to stdout */
 
292
__attribute__((nonnull))
286
293
static bool print_out_password(const char *buffer, size_t length){
287
294
  ssize_t ret;
288
295
  for(size_t written = 0; written < length; written += (size_t)ret){
296
303
}
297
304
 
298
305
/* Removes and free a plugin from the plugin list */
 
306
__attribute__((nonnull))
299
307
static void free_plugin(plugin *plugin_node){
300
308
  
301
309
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
356
364
  sigemptyset(&sigchld_action.sa_mask);
357
365
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
358
366
  if(ret == -1){
359
 
    perror("sigaddset");
360
 
    exitstatus = EXIT_FAILURE;
 
367
    error(0, errno, "sigaddset");
 
368
    exitstatus = EX_OSERR;
361
369
    goto fallback;
362
370
  }
363
371
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
364
372
  if(ret == -1){
365
 
    perror("sigaction");
366
 
    exitstatus = EXIT_FAILURE;
 
373
    error(0, errno, "sigaction");
 
374
    exitstatus = EX_OSERR;
367
375
    goto fallback;
368
376
  }
369
377
  
413
421
    { .name = NULL }
414
422
  };
415
423
  
 
424
  __attribute__((nonnull(3)))
416
425
  error_t parse_opt(int key, char *arg, struct argp_state *state){
417
426
    errno = 0;
418
427
    switch(key){
419
428
      char *tmp;
420
 
      intmax_t tmpmax;
 
429
      intmax_t tmp_id;
421
430
    case 'g':                   /* --global-options */
422
431
      {
423
432
        char *plugin_option;
496
505
      /* This is already done by parse_opt_config_file() */
497
506
      break;
498
507
    case 130:                   /* --userid */
499
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
508
      tmp_id = strtoimax(arg, &tmp, 10);
500
509
      if(errno != 0 or tmp == arg or *tmp != '\0'
501
 
         or tmpmax != (uid_t)tmpmax){
 
510
         or tmp_id != (uid_t)tmp_id){
502
511
        argp_error(state, "Bad user ID number: \"%s\", using %"
503
512
                   PRIdMAX, arg, (intmax_t)uid);
504
513
        break;
505
514
      }
506
 
      uid = (uid_t)tmpmax;
 
515
      uid = (uid_t)tmp_id;
507
516
      break;
508
517
    case 131:                   /* --groupid */
509
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
518
      tmp_id = strtoimax(arg, &tmp, 10);
510
519
      if(errno != 0 or tmp == arg or *tmp != '\0'
511
 
         or tmpmax != (gid_t)tmpmax){
 
520
         or tmp_id != (gid_t)tmp_id){
512
521
        argp_error(state, "Bad group ID number: \"%s\", using %"
513
522
                   PRIdMAX, arg, (intmax_t)gid);
514
523
        break;
515
524
      }
516
 
      gid = (gid_t)tmpmax;
 
525
      gid = (gid_t)tmp_id;
517
526
      break;
518
527
    case 132:                   /* --debug */
519
528
      debug = true;
598
607
  case ENOMEM:
599
608
  default:
600
609
    errno = ret;
601
 
    perror("argp_parse");
 
610
    error(0, errno, "argp_parse");
602
611
    exitstatus = EX_OSERR;
603
612
    goto fallback;
604
613
  case EINVAL:
625
634
    custom_argc = 1;
626
635
    custom_argv = malloc(sizeof(char*) * 2);
627
636
    if(custom_argv == NULL){
628
 
      perror("malloc");
629
 
      exitstatus = EXIT_FAILURE;
 
637
      error(0, errno, "malloc");
 
638
      exitstatus = EX_OSERR;
630
639
      goto fallback;
631
640
    }
632
641
    custom_argv[0] = argv[0];
648
657
        }
649
658
        new_arg = strdup(p);
650
659
        if(new_arg == NULL){
651
 
          perror("strdup");
652
 
          exitstatus = EXIT_FAILURE;
 
660
          error(0, errno, "strdup");
 
661
          exitstatus = EX_OSERR;
653
662
          free(org_line);
654
663
          goto fallback;
655
664
        }
658
667
        custom_argv = realloc(custom_argv, sizeof(char *)
659
668
                              * ((unsigned int) custom_argc + 1));
660
669
        if(custom_argv == NULL){
661
 
          perror("realloc");
662
 
          exitstatus = EXIT_FAILURE;
 
670
          error(0, errno, "realloc");
 
671
          exitstatus = EX_OSERR;
663
672
          free(org_line);
664
673
          goto fallback;
665
674
        }
671
680
      ret = fclose(conffp);
672
681
    } while(ret == EOF and errno == EINTR);
673
682
    if(ret == EOF){
674
 
      perror("fclose");
675
 
      exitstatus = EXIT_FAILURE;
 
683
      error(0, errno, "fclose");
 
684
      exitstatus = EX_IOERR;
676
685
      goto fallback;
677
686
    }
678
687
    free(org_line);
680
689
    /* Check for harmful errors and go to fallback. Other errors might
681
690
       not affect opening plugins */
682
691
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
683
 
      perror("fopen");
684
 
      exitstatus = EXIT_FAILURE;
 
692
      error(0, errno, "fopen");
 
693
      exitstatus = EX_OSERR;
685
694
      goto fallback;
686
695
    }
687
696
  }
697
706
    case ENOMEM:
698
707
    default:
699
708
      errno = ret;
700
 
      perror("argp_parse");
 
709
      error(0, errno, "argp_parse");
701
710
      exitstatus = EX_OSERR;
702
711
      goto fallback;
703
712
    case EINVAL:
717
726
  case ENOMEM:
718
727
  default:
719
728
    errno = ret;
720
 
    perror("argp_parse");
 
729
    error(0, errno, "argp_parse");
721
730
    exitstatus = EX_OSERR;
722
731
    goto fallback;
723
732
  case EINVAL:
739
748
    }
740
749
  }
741
750
  
742
 
  /* Strip permissions down to nobody */
743
 
  setgid(gid);
 
751
  if(getuid() == 0){
 
752
    /* Work around Debian bug #633582:
 
753
       <http://bugs.debian.org/633582> */
 
754
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
755
    if(plugindir_fd == -1){
 
756
      error(0, errno, "open");
 
757
    } else {
 
758
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
759
      if(ret == -1){
 
760
        error(0, errno, "fstat");
 
761
      } else {
 
762
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
763
          ret = fchown(plugindir_fd, uid, gid);
 
764
          if(ret == -1){
 
765
            error(0, errno, "fchown");
 
766
          }
 
767
        }
 
768
      }
 
769
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
770
    }
 
771
  }
 
772
  
 
773
  /* Lower permissions */
 
774
  ret = setgid(gid);
744
775
  if(ret == -1){
745
 
    perror("setgid");
 
776
    error(0, errno, "setgid");
746
777
  }
747
778
  ret = setuid(uid);
748
779
  if(ret == -1){
749
 
    perror("setuid");
 
780
    error(0, errno, "setuid");
750
781
  }
751
782
  
752
783
  /* Open plugin directory with close_on_exec flag */
770
801
                    );
771
802
    }
772
803
    if(dir_fd == -1){
773
 
      perror("Could not open plugin dir");
774
 
      exitstatus = EXIT_FAILURE;
 
804
      error(0, errno, "Could not open plugin dir");
 
805
      exitstatus = EX_UNAVAILABLE;
775
806
      goto fallback;
776
807
    }
777
808
    
779
810
  /* Set the FD_CLOEXEC flag on the directory */
780
811
    ret = set_cloexec_flag(dir_fd);
781
812
    if(ret < 0){
782
 
      perror("set_cloexec_flag");
 
813
      error(0, errno, "set_cloexec_flag");
783
814
      TEMP_FAILURE_RETRY(close(dir_fd));
784
 
      exitstatus = EXIT_FAILURE;
 
815
      exitstatus = EX_OSERR;
785
816
      goto fallback;
786
817
    }
787
818
#endif  /* O_CLOEXEC */
788
819
    
789
820
    dir = fdopendir(dir_fd);
790
821
    if(dir == NULL){
791
 
      perror("Could not open plugin dir");
 
822
      error(0, errno, "Could not open plugin dir");
792
823
      TEMP_FAILURE_RETRY(close(dir_fd));
793
 
      exitstatus = EXIT_FAILURE;
 
824
      exitstatus = EX_OSERR;
794
825
      goto fallback;
795
826
    }
796
827
  }
806
837
    /* All directory entries have been processed */
807
838
    if(dirst == NULL){
808
839
      if(errno == EBADF){
809
 
        perror("readdir");
810
 
        exitstatus = EXIT_FAILURE;
 
840
        error(0, errno, "readdir");
 
841
        exitstatus = EX_IOERR;
811
842
        goto fallback;
812
843
      }
813
844
      break;
819
850
    {
820
851
      bool bad_name = false;
821
852
      
822
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
853
      const char * const bad_prefixes[] = { ".", "#", NULL };
823
854
      
824
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
855
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
825
856
                                           ".dpkg-old",
826
857
                                           ".dpkg-bak",
827
858
                                           ".dpkg-divert", NULL };
828
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
859
#pragma GCC diagnostic push
 
860
#pragma GCC diagnostic ignored "-Wcast-qual"
 
861
      for(const char **pre = (const char **)bad_prefixes;
 
862
          *pre != NULL; pre++){
 
863
#pragma GCC diagnostic pop
829
864
        size_t pre_len = strlen(*pre);
830
865
        if((d_name_len >= pre_len)
831
866
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
840
875
      if(bad_name){
841
876
        continue;
842
877
      }
843
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
878
#pragma GCC diagnostic push
 
879
#pragma GCC diagnostic ignored "-Wcast-qual"
 
880
      for(const char **suf = (const char **)bad_suffixes;
 
881
          *suf != NULL; suf++){
 
882
#pragma GCC diagnostic pop
844
883
        size_t suf_len = strlen(*suf);
845
884
        if((d_name_len >= suf_len)
846
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
885
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
847
886
                == 0)){
848
887
          if(debug){
849
888
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
869
908
                                             dirst->d_name));
870
909
    }
871
910
    if(ret < 0){
872
 
      perror("asprintf");
 
911
      error(0, errno, "asprintf");
873
912
      continue;
874
913
    }
875
914
    
876
915
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
877
916
    if(ret == -1){
878
 
      perror("stat");
 
917
      error(0, errno, "stat");
879
918
      free(filename);
880
919
      continue;
881
920
    }
893
932
    
894
933
    plugin *p = getplugin(dirst->d_name);
895
934
    if(p == NULL){
896
 
      perror("getplugin");
 
935
      error(0, errno, "getplugin");
897
936
      free(filename);
898
937
      continue;
899
938
    }
911
950
      if(g != NULL){
912
951
        for(char **a = g->argv + 1; *a != NULL; a++){
913
952
          if(not add_argument(p, *a)){
914
 
            perror("add_argument");
 
953
            error(0, errno, "add_argument");
915
954
          }
916
955
        }
917
956
        /* Add global environment variables */
918
957
        for(char **e = g->environ; *e != NULL; e++){
919
958
          if(not add_environment(p, *e, false)){
920
 
            perror("add_environment");
 
959
            error(0, errno, "add_environment");
921
960
          }
922
961
        }
923
962
      }
928
967
    if(p->environ[0] != NULL){
929
968
      for(char **e = environ; *e != NULL; e++){
930
969
        if(not add_environment(p, *e, false)){
931
 
          perror("add_environment");
 
970
          error(0, errno, "add_environment");
932
971
        }
933
972
      }
934
973
    }
936
975
    int pipefd[2];
937
976
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
938
977
    if(ret == -1){
939
 
      perror("pipe");
940
 
      exitstatus = EXIT_FAILURE;
 
978
      error(0, errno, "pipe");
 
979
      exitstatus = EX_OSERR;
941
980
      goto fallback;
942
981
    }
943
982
    /* Ask OS to automatic close the pipe on exec */
944
983
    ret = set_cloexec_flag(pipefd[0]);
945
984
    if(ret < 0){
946
 
      perror("set_cloexec_flag");
947
 
      exitstatus = EXIT_FAILURE;
 
985
      error(0, errno, "set_cloexec_flag");
 
986
      exitstatus = EX_OSERR;
948
987
      goto fallback;
949
988
    }
950
989
    ret = set_cloexec_flag(pipefd[1]);
951
990
    if(ret < 0){
952
 
      perror("set_cloexec_flag");
953
 
      exitstatus = EXIT_FAILURE;
 
991
      error(0, errno, "set_cloexec_flag");
 
992
      exitstatus = EX_OSERR;
954
993
      goto fallback;
955
994
    }
956
995
    /* Block SIGCHLD until process is safely in process list */
958
997
                                              &sigchld_action.sa_mask,
959
998
                                              NULL));
960
999
    if(ret < 0){
961
 
      perror("sigprocmask");
962
 
      exitstatus = EXIT_FAILURE;
 
1000
      error(0, errno, "sigprocmask");
 
1001
      exitstatus = EX_OSERR;
963
1002
      goto fallback;
964
1003
    }
965
1004
    /* Starting a new process to be watched */
968
1007
      pid = fork();
969
1008
    } while(pid == -1 and errno == EINTR);
970
1009
    if(pid == -1){
971
 
      perror("fork");
972
 
      exitstatus = EXIT_FAILURE;
 
1010
      error(0, errno, "fork");
 
1011
      exitstatus = EX_OSERR;
973
1012
      goto fallback;
974
1013
    }
975
1014
    if(pid == 0){
976
1015
      /* this is the child process */
977
1016
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
978
1017
      if(ret < 0){
979
 
        perror("sigaction");
980
 
        _exit(EXIT_FAILURE);
 
1018
        error(0, errno, "sigaction");
 
1019
        _exit(EX_OSERR);
981
1020
      }
982
1021
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
983
1022
      if(ret < 0){
984
 
        perror("sigprocmask");
985
 
        _exit(EXIT_FAILURE);
 
1023
        error(0, errno, "sigprocmask");
 
1024
        _exit(EX_OSERR);
986
1025
      }
987
1026
      
988
1027
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
989
1028
      if(ret == -1){
990
 
        perror("dup2");
991
 
        _exit(EXIT_FAILURE);
 
1029
        error(0, errno, "dup2");
 
1030
        _exit(EX_OSERR);
992
1031
      }
993
1032
      
994
1033
      if(dirfd(dir) < 0){
998
1037
      }
999
1038
      if(p->environ[0] == NULL){
1000
1039
        if(execv(filename, p->argv) < 0){
1001
 
          perror("execv");
1002
 
          _exit(EXIT_FAILURE);
 
1040
          error(0, errno, "execv for %s", filename);
 
1041
          _exit(EX_OSERR);
1003
1042
        }
1004
1043
      } else {
1005
1044
        if(execve(filename, p->argv, p->environ) < 0){
1006
 
          perror("execve");
1007
 
          _exit(EXIT_FAILURE);
 
1045
          error(0, errno, "execve for %s", filename);
 
1046
          _exit(EX_OSERR);
1008
1047
        }
1009
1048
      }
1010
1049
      /* no return */
1015
1054
    free(filename);
1016
1055
    plugin *new_plugin = getplugin(dirst->d_name);
1017
1056
    if(new_plugin == NULL){
1018
 
      perror("getplugin");
 
1057
      error(0, errno, "getplugin");
1019
1058
      ret = (int)(TEMP_FAILURE_RETRY
1020
1059
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1021
1060
                               NULL)));
1022
1061
      if(ret < 0){
1023
 
        perror("sigprocmask");
 
1062
        error(0, errno, "sigprocmask");
1024
1063
      }
1025
 
      exitstatus = EXIT_FAILURE;
 
1064
      exitstatus = EX_OSERR;
1026
1065
      goto fallback;
1027
1066
    }
1028
1067
    
1035
1074
                                              &sigchld_action.sa_mask,
1036
1075
                                              NULL));
1037
1076
    if(ret < 0){
1038
 
      perror("sigprocmask");
1039
 
      exitstatus = EXIT_FAILURE;
 
1077
      error(0, errno, "sigprocmask");
 
1078
      exitstatus = EX_OSERR;
1040
1079
      goto fallback;
1041
1080
    }
1042
1081
    
 
1082
#if defined (__GNUC__) and defined (__GLIBC__)
 
1083
#if not __GLIBC_PREREQ(2, 16)
 
1084
#pragma GCC diagnostic push
 
1085
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1086
#endif
 
1087
#endif
1043
1088
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1044
 
                                          -Wconversion */
 
1089
                                          -Wconversion in GNU libc
 
1090
                                          before 2.16 */
 
1091
#if defined (__GNUC__) and defined (__GLIBC__)
 
1092
#if not __GLIBC_PREREQ(2, 16)
 
1093
#pragma GCC diagnostic pop
 
1094
#endif
 
1095
#endif
1045
1096
    
1046
1097
    if(maxfd < new_plugin->fd){
1047
1098
      maxfd = new_plugin->fd;
1068
1119
    fd_set rfds = rfds_all;
1069
1120
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1070
1121
    if(select_ret == -1 and errno != EINTR){
1071
 
      perror("select");
1072
 
      exitstatus = EXIT_FAILURE;
 
1122
      error(0, errno, "select");
 
1123
      exitstatus = EX_OSERR;
1073
1124
      goto fallback;
1074
1125
    }
1075
1126
    /* OK, now either a process completed, or something can be read
1101
1152
          }
1102
1153
          
1103
1154
          /* Remove the plugin */
 
1155
#if defined (__GNUC__) and defined (__GLIBC__)
 
1156
#if not __GLIBC_PREREQ(2, 16)
 
1157
#pragma GCC diagnostic push
 
1158
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1159
#endif
 
1160
#endif
1104
1161
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion */
 
1162
                                          -Wconversion in GNU libc
 
1163
                                          before 2.16 */
 
1164
#if defined (__GNUC__) and defined (__GLIBC__)
 
1165
#if not __GLIBC_PREREQ(2, 16)
 
1166
#pragma GCC diagnostic pop
 
1167
#endif
 
1168
#endif
1106
1169
          
1107
1170
          /* Block signal while modifying process_list */
1108
1171
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1110
1173
                                         &sigchld_action.sa_mask,
1111
1174
                                         NULL));
1112
1175
          if(ret < 0){
1113
 
            perror("sigprocmask");
1114
 
            exitstatus = EXIT_FAILURE;
 
1176
            error(0, errno, "sigprocmask");
 
1177
            exitstatus = EX_OSERR;
1115
1178
            goto fallback;
1116
1179
          }
1117
1180
          
1124
1187
                      (sigprocmask(SIG_UNBLOCK,
1125
1188
                                   &sigchld_action.sa_mask, NULL)));
1126
1189
          if(ret < 0){
1127
 
            perror("sigprocmask");
1128
 
            exitstatus = EXIT_FAILURE;
 
1190
            error(0, errno, "sigprocmask");
 
1191
            exitstatus = EX_OSERR;
1129
1192
            goto fallback;
1130
1193
          }
1131
1194
          
1141
1204
        bool bret = print_out_password(proc->buffer,
1142
1205
                                       proc->buffer_length);
1143
1206
        if(not bret){
1144
 
          perror("print_out_password");
1145
 
          exitstatus = EXIT_FAILURE;
 
1207
          error(0, errno, "print_out_password");
 
1208
          exitstatus = EX_IOERR;
1146
1209
        }
1147
1210
        goto fallback;
1148
1211
      }
1149
1212
      
1150
1213
      /* This process has not completed.  Does it have any output? */
 
1214
#if defined (__GNUC__) and defined (__GLIBC__)
 
1215
#if not __GLIBC_PREREQ(2, 16)
 
1216
#pragma GCC diagnostic push
 
1217
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1218
#endif
 
1219
#endif
1151
1220
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1152
1221
                                                         warning from
1153
 
                                                         -Wconversion */
 
1222
                                                         -Wconversion
 
1223
                                                         in GNU libc
 
1224
                                                         before
 
1225
                                                         2.16 */
 
1226
#if defined (__GNUC__) and defined (__GLIBC__)
 
1227
#if not __GLIBC_PREREQ(2, 16)
 
1228
#pragma GCC diagnostic pop
 
1229
#endif
 
1230
#endif
1154
1231
        /* This process had nothing to say at this time */
1155
1232
        proc = proc->next;
1156
1233
        continue;
1160
1237
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1161
1238
                               + (size_t) BUFFER_SIZE);
1162
1239
        if(proc->buffer == NULL){
1163
 
          perror("malloc");
1164
 
          exitstatus = EXIT_FAILURE;
 
1240
          error(0, errno, "malloc");
 
1241
          exitstatus = EX_OSERR;
1165
1242
          goto fallback;
1166
1243
        }
1167
1244
        proc->buffer_size += BUFFER_SIZE;
1188
1265
  
1189
1266
 fallback:
1190
1267
  
1191
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1268
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1269
                             and exitstatus != EX_OK)){
1192
1270
    /* Fallback if all plugins failed, none are found or an error
1193
1271
       occured */
1194
1272
    bool bret;
1202
1280
    }
1203
1281
    bret = print_out_password(passwordbuffer, len);
1204
1282
    if(not bret){
1205
 
      perror("print_out_password");
1206
 
      exitstatus = EXIT_FAILURE;
 
1283
      error(0, errno, "print_out_password");
 
1284
      exitstatus = EX_IOERR;
1207
1285
    }
1208
1286
  }
1209
1287
  
1210
1288
  /* Restore old signal handler */
1211
1289
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1212
1290
  if(ret == -1){
1213
 
    perror("sigaction");
1214
 
    exitstatus = EXIT_FAILURE;
 
1291
    error(0, errno, "sigaction");
 
1292
    exitstatus = EX_OSERR;
1215
1293
  }
1216
1294
  
1217
1295
  if(custom_argv != NULL){
1232
1310
      ret = kill(p->pid, SIGTERM);
1233
1311
      if(ret == -1 and errno != ESRCH){
1234
1312
        /* Set-uid proccesses might not get closed */
1235
 
        perror("kill");
 
1313
        error(0, errno, "kill");
1236
1314
      }
1237
1315
    }
1238
1316
  }
1242
1320
    ret = wait(NULL);
1243
1321
  } while(ret >= 0);
1244
1322
  if(errno != ECHILD){
1245
 
    perror("wait");
 
1323
    error(0, errno, "wait");
1246
1324
  }
1247
1325
  
1248
1326
  free_plugin_list();