/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-10 10:11:37 UTC
  • Revision ID: teddy@recompile.se-20140310101137-3khoc3qrntp13l08
White space fix: change "if (" to "if(" in C code.

* plugins.d/mandos-client.c (start_mandos_communication): White space
                                                          fix.
  (avahi_loop_with_timeout, main): - '' -

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(),
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(),
 
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(),
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() */
73
75
 
74
76
#define BUFFER_SIZE 256
75
77
 
77
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
78
80
 
79
81
const char *argp_program_version = "plugin-runner " VERSION;
80
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
82
const char *argp_program_bug_address = "<mandos@recompile.se>";
81
83
 
82
84
typedef struct plugin{
83
85
  char *name;                   /* can be NULL or any plugin name */
169
171
}
170
172
 
171
173
/* Helper function for add_argument and add_environment */
 
174
__attribute__((nonnull))
172
175
static bool add_to_char_array(const char *new, char ***array,
173
176
                              int *len){
174
177
  /* Resize the pointed-to array to hold one more pointer */
 
178
  char **new_array = NULL;
175
179
  do {
176
 
    *array = realloc(*array, sizeof(char *)
177
 
                     * (size_t) ((*len) + 2));
178
 
  } while(*array == NULL and errno == EINTR);
 
180
    new_array = realloc(*array, sizeof(char *)
 
181
                        * (size_t) ((*len) + 2));
 
182
  } while(new_array == NULL and errno == EINTR);
179
183
  /* Malloc check */
180
 
  if(*array == NULL){
 
184
  if(new_array == NULL){
181
185
    return false;
182
186
  }
 
187
  *array = new_array;
183
188
  /* Make a copy of the new string */
184
189
  char *copy;
185
190
  do {
197
202
}
198
203
 
199
204
/* Add to a plugin's argument vector */
 
205
__attribute__((nonnull(2)))
200
206
static bool add_argument(plugin *p, const char *arg){
201
207
  if(p == NULL){
202
208
    return false;
205
211
}
206
212
 
207
213
/* Add to a plugin's environment */
 
214
__attribute__((nonnull(2)))
208
215
static bool add_environment(plugin *p, const char *def, bool replace){
209
216
  if(p == NULL){
210
217
    return false;
212
219
  /* namelen = length of name of environment variable */
213
220
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
214
221
  /* Search for this environment variable */
215
 
  for(char **e = p->environ; *e != NULL; e++){
216
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
222
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
223
    if(strncmp(*envdef, def, namelen + 1) == 0){
217
224
      /* It already exists */
218
225
      if(replace){
219
 
        char *new;
 
226
        char *new_envdef;
220
227
        do {
221
 
          new = realloc(*e, strlen(def) + 1);
222
 
        } while(new == NULL and errno == EINTR);
223
 
        if(new == NULL){
 
228
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
229
        } while(new_envdef == NULL and errno == EINTR);
 
230
        if(new_envdef == NULL){
224
231
          return false;
225
232
        }
226
 
        *e = new;
227
 
        strcpy(*e, def);
 
233
        *envdef = new_envdef;
 
234
        strcpy(*envdef, def);
228
235
      }
229
236
      return true;
230
237
    }
266
273
        /* No child processes */
267
274
        break;
268
275
      }
269
 
      perror("waitpid");
 
276
      error(0, errno, "waitpid");
270
277
    }
271
278
    
272
279
    /* A child exited, find it in process_list */
284
291
}
285
292
 
286
293
/* Prints out a password to stdout */
 
294
__attribute__((nonnull))
287
295
static bool print_out_password(const char *buffer, size_t length){
288
296
  ssize_t ret;
289
297
  for(size_t written = 0; written < length; written += (size_t)ret){
297
305
}
298
306
 
299
307
/* Removes and free a plugin from the plugin list */
 
308
__attribute__((nonnull))
300
309
static void free_plugin(plugin *plugin_node){
301
310
  
302
311
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
357
366
  sigemptyset(&sigchld_action.sa_mask);
358
367
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
359
368
  if(ret == -1){
360
 
    perror("sigaddset");
 
369
    error(0, errno, "sigaddset");
361
370
    exitstatus = EX_OSERR;
362
371
    goto fallback;
363
372
  }
364
373
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
365
374
  if(ret == -1){
366
 
    perror("sigaction");
 
375
    error(0, errno, "sigaction");
367
376
    exitstatus = EX_OSERR;
368
377
    goto fallback;
369
378
  }
414
423
    { .name = NULL }
415
424
  };
416
425
  
 
426
  __attribute__((nonnull(3)))
417
427
  error_t parse_opt(int key, char *arg, struct argp_state *state){
418
428
    errno = 0;
419
429
    switch(key){
420
430
      char *tmp;
421
 
      intmax_t tmpmax;
 
431
      intmax_t tmp_id;
422
432
    case 'g':                   /* --global-options */
423
433
      {
424
434
        char *plugin_option;
497
507
      /* This is already done by parse_opt_config_file() */
498
508
      break;
499
509
    case 130:                   /* --userid */
500
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
510
      tmp_id = strtoimax(arg, &tmp, 10);
501
511
      if(errno != 0 or tmp == arg or *tmp != '\0'
502
 
         or tmpmax != (uid_t)tmpmax){
 
512
         or tmp_id != (uid_t)tmp_id){
503
513
        argp_error(state, "Bad user ID number: \"%s\", using %"
504
514
                   PRIdMAX, arg, (intmax_t)uid);
505
515
        break;
506
516
      }
507
 
      uid = (uid_t)tmpmax;
 
517
      uid = (uid_t)tmp_id;
508
518
      break;
509
519
    case 131:                   /* --groupid */
510
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
520
      tmp_id = strtoimax(arg, &tmp, 10);
511
521
      if(errno != 0 or tmp == arg or *tmp != '\0'
512
 
         or tmpmax != (gid_t)tmpmax){
 
522
         or tmp_id != (gid_t)tmp_id){
513
523
        argp_error(state, "Bad group ID number: \"%s\", using %"
514
524
                   PRIdMAX, arg, (intmax_t)gid);
515
525
        break;
516
526
      }
517
 
      gid = (gid_t)tmpmax;
 
527
      gid = (gid_t)tmp_id;
518
528
      break;
519
529
    case 132:                   /* --debug */
520
530
      debug = true;
599
609
  case ENOMEM:
600
610
  default:
601
611
    errno = ret;
602
 
    perror("argp_parse");
 
612
    error(0, errno, "argp_parse");
603
613
    exitstatus = EX_OSERR;
604
614
    goto fallback;
605
615
  case EINVAL:
626
636
    custom_argc = 1;
627
637
    custom_argv = malloc(sizeof(char*) * 2);
628
638
    if(custom_argv == NULL){
629
 
      perror("malloc");
 
639
      error(0, errno, "malloc");
630
640
      exitstatus = EX_OSERR;
631
641
      goto fallback;
632
642
    }
649
659
        }
650
660
        new_arg = strdup(p);
651
661
        if(new_arg == NULL){
652
 
          perror("strdup");
 
662
          error(0, errno, "strdup");
653
663
          exitstatus = EX_OSERR;
654
664
          free(org_line);
655
665
          goto fallback;
656
666
        }
657
667
        
658
668
        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;
 
669
        {
 
670
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
671
                                    * ((unsigned int)
 
672
                                       custom_argc + 1));
 
673
          if(new_argv == NULL){
 
674
            error(0, errno, "realloc");
 
675
            exitstatus = EX_OSERR;
 
676
            free(new_arg);
 
677
            free(org_line);
 
678
            goto fallback;
 
679
          } else {
 
680
            custom_argv = new_argv;
 
681
          }
666
682
        }
667
683
        custom_argv[custom_argc-1] = new_arg;
668
684
        custom_argv[custom_argc] = NULL;
672
688
      ret = fclose(conffp);
673
689
    } while(ret == EOF and errno == EINTR);
674
690
    if(ret == EOF){
675
 
      perror("fclose");
 
691
      error(0, errno, "fclose");
676
692
      exitstatus = EX_IOERR;
677
693
      goto fallback;
678
694
    }
681
697
    /* Check for harmful errors and go to fallback. Other errors might
682
698
       not affect opening plugins */
683
699
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
684
 
      perror("fopen");
 
700
      error(0, errno, "fopen");
685
701
      exitstatus = EX_OSERR;
686
702
      goto fallback;
687
703
    }
698
714
    case ENOMEM:
699
715
    default:
700
716
      errno = ret;
701
 
      perror("argp_parse");
 
717
      error(0, errno, "argp_parse");
702
718
      exitstatus = EX_OSERR;
703
719
      goto fallback;
704
720
    case EINVAL:
718
734
  case ENOMEM:
719
735
  default:
720
736
    errno = ret;
721
 
    perror("argp_parse");
 
737
    error(0, errno, "argp_parse");
722
738
    exitstatus = EX_OSERR;
723
739
    goto fallback;
724
740
  case EINVAL:
740
756
    }
741
757
  }
742
758
  
743
 
  /* Strip permissions down to nobody */
744
 
  setgid(gid);
 
759
  if(getuid() == 0){
 
760
    /* Work around Debian bug #633582:
 
761
       <http://bugs.debian.org/633582> */
 
762
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
763
    if(plugindir_fd == -1){
 
764
      error(0, errno, "open");
 
765
    } else {
 
766
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
767
      if(ret == -1){
 
768
        error(0, errno, "fstat");
 
769
      } else {
 
770
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
771
          ret = fchown(plugindir_fd, uid, gid);
 
772
          if(ret == -1){
 
773
            error(0, errno, "fchown");
 
774
          }
 
775
        }
 
776
      }
 
777
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
778
    }
 
779
  }
 
780
  
 
781
  /* Lower permissions */
 
782
  ret = setgid(gid);
745
783
  if(ret == -1){
746
 
    perror("setgid");
 
784
    error(0, errno, "setgid");
747
785
  }
748
786
  ret = setuid(uid);
749
787
  if(ret == -1){
750
 
    perror("setuid");
 
788
    error(0, errno, "setuid");
751
789
  }
752
790
  
753
791
  /* Open plugin directory with close_on_exec flag */
771
809
                    );
772
810
    }
773
811
    if(dir_fd == -1){
774
 
      perror("Could not open plugin dir");
 
812
      error(0, errno, "Could not open plugin dir");
775
813
      exitstatus = EX_UNAVAILABLE;
776
814
      goto fallback;
777
815
    }
780
818
  /* Set the FD_CLOEXEC flag on the directory */
781
819
    ret = set_cloexec_flag(dir_fd);
782
820
    if(ret < 0){
783
 
      perror("set_cloexec_flag");
 
821
      error(0, errno, "set_cloexec_flag");
784
822
      TEMP_FAILURE_RETRY(close(dir_fd));
785
823
      exitstatus = EX_OSERR;
786
824
      goto fallback;
789
827
    
790
828
    dir = fdopendir(dir_fd);
791
829
    if(dir == NULL){
792
 
      perror("Could not open plugin dir");
 
830
      error(0, errno, "Could not open plugin dir");
793
831
      TEMP_FAILURE_RETRY(close(dir_fd));
794
832
      exitstatus = EX_OSERR;
795
833
      goto fallback;
807
845
    /* All directory entries have been processed */
808
846
    if(dirst == NULL){
809
847
      if(errno == EBADF){
810
 
        perror("readdir");
 
848
        error(0, errno, "readdir");
811
849
        exitstatus = EX_IOERR;
812
850
        goto fallback;
813
851
      }
820
858
    {
821
859
      bool bad_name = false;
822
860
      
823
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
861
      const char * const bad_prefixes[] = { ".", "#", NULL };
824
862
      
825
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
863
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
826
864
                                           ".dpkg-old",
827
865
                                           ".dpkg-bak",
828
866
                                           ".dpkg-divert", NULL };
829
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
867
#ifdef __GNUC__
 
868
#pragma GCC diagnostic push
 
869
#pragma GCC diagnostic ignored "-Wcast-qual"
 
870
#endif
 
871
      for(const char **pre = (const char **)bad_prefixes;
 
872
          *pre != NULL; pre++){
 
873
#ifdef __GNUC__
 
874
#pragma GCC diagnostic pop
 
875
#endif
830
876
        size_t pre_len = strlen(*pre);
831
877
        if((d_name_len >= pre_len)
832
878
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
841
887
      if(bad_name){
842
888
        continue;
843
889
      }
844
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
890
#ifdef __GNUC__
 
891
#pragma GCC diagnostic push
 
892
#pragma GCC diagnostic ignored "-Wcast-qual"
 
893
#endif
 
894
      for(const char **suf = (const char **)bad_suffixes;
 
895
          *suf != NULL; suf++){
 
896
#ifdef __GNUC__
 
897
#pragma GCC diagnostic pop
 
898
#endif
845
899
        size_t suf_len = strlen(*suf);
846
900
        if((d_name_len >= suf_len)
847
901
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
870
924
                                             dirst->d_name));
871
925
    }
872
926
    if(ret < 0){
873
 
      perror("asprintf");
 
927
      error(0, errno, "asprintf");
874
928
      continue;
875
929
    }
876
930
    
877
931
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
878
932
    if(ret == -1){
879
 
      perror("stat");
 
933
      error(0, errno, "stat");
880
934
      free(filename);
881
935
      continue;
882
936
    }
894
948
    
895
949
    plugin *p = getplugin(dirst->d_name);
896
950
    if(p == NULL){
897
 
      perror("getplugin");
 
951
      error(0, errno, "getplugin");
898
952
      free(filename);
899
953
      continue;
900
954
    }
912
966
      if(g != NULL){
913
967
        for(char **a = g->argv + 1; *a != NULL; a++){
914
968
          if(not add_argument(p, *a)){
915
 
            perror("add_argument");
 
969
            error(0, errno, "add_argument");
916
970
          }
917
971
        }
918
972
        /* Add global environment variables */
919
973
        for(char **e = g->environ; *e != NULL; e++){
920
974
          if(not add_environment(p, *e, false)){
921
 
            perror("add_environment");
 
975
            error(0, errno, "add_environment");
922
976
          }
923
977
        }
924
978
      }
929
983
    if(p->environ[0] != NULL){
930
984
      for(char **e = environ; *e != NULL; e++){
931
985
        if(not add_environment(p, *e, false)){
932
 
          perror("add_environment");
 
986
          error(0, errno, "add_environment");
933
987
        }
934
988
      }
935
989
    }
937
991
    int pipefd[2];
938
992
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
939
993
    if(ret == -1){
940
 
      perror("pipe");
 
994
      error(0, errno, "pipe");
941
995
      exitstatus = EX_OSERR;
942
996
      goto fallback;
943
997
    }
944
998
    /* Ask OS to automatic close the pipe on exec */
945
999
    ret = set_cloexec_flag(pipefd[0]);
946
1000
    if(ret < 0){
947
 
      perror("set_cloexec_flag");
 
1001
      error(0, errno, "set_cloexec_flag");
948
1002
      exitstatus = EX_OSERR;
949
1003
      goto fallback;
950
1004
    }
951
1005
    ret = set_cloexec_flag(pipefd[1]);
952
1006
    if(ret < 0){
953
 
      perror("set_cloexec_flag");
 
1007
      error(0, errno, "set_cloexec_flag");
954
1008
      exitstatus = EX_OSERR;
955
1009
      goto fallback;
956
1010
    }
959
1013
                                              &sigchld_action.sa_mask,
960
1014
                                              NULL));
961
1015
    if(ret < 0){
962
 
      perror("sigprocmask");
 
1016
      error(0, errno, "sigprocmask");
963
1017
      exitstatus = EX_OSERR;
964
1018
      goto fallback;
965
1019
    }
969
1023
      pid = fork();
970
1024
    } while(pid == -1 and errno == EINTR);
971
1025
    if(pid == -1){
972
 
      perror("fork");
 
1026
      error(0, errno, "fork");
973
1027
      exitstatus = EX_OSERR;
974
1028
      goto fallback;
975
1029
    }
977
1031
      /* this is the child process */
978
1032
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
979
1033
      if(ret < 0){
980
 
        perror("sigaction");
 
1034
        error(0, errno, "sigaction");
981
1035
        _exit(EX_OSERR);
982
1036
      }
983
1037
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
984
1038
      if(ret < 0){
985
 
        perror("sigprocmask");
 
1039
        error(0, errno, "sigprocmask");
986
1040
        _exit(EX_OSERR);
987
1041
      }
988
1042
      
989
1043
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
990
1044
      if(ret == -1){
991
 
        perror("dup2");
 
1045
        error(0, errno, "dup2");
992
1046
        _exit(EX_OSERR);
993
1047
      }
994
1048
      
999
1053
      }
1000
1054
      if(p->environ[0] == NULL){
1001
1055
        if(execv(filename, p->argv) < 0){
1002
 
          perror("execv");
 
1056
          error(0, errno, "execv for %s", filename);
1003
1057
          _exit(EX_OSERR);
1004
1058
        }
1005
1059
      } else {
1006
1060
        if(execve(filename, p->argv, p->environ) < 0){
1007
 
          perror("execve");
 
1061
          error(0, errno, "execve for %s", filename);
1008
1062
          _exit(EX_OSERR);
1009
1063
        }
1010
1064
      }
1016
1070
    free(filename);
1017
1071
    plugin *new_plugin = getplugin(dirst->d_name);
1018
1072
    if(new_plugin == NULL){
1019
 
      perror("getplugin");
 
1073
      error(0, errno, "getplugin");
1020
1074
      ret = (int)(TEMP_FAILURE_RETRY
1021
1075
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1022
1076
                               NULL)));
1023
1077
      if(ret < 0){
1024
 
        perror("sigprocmask");
 
1078
        error(0, errno, "sigprocmask");
1025
1079
      }
1026
1080
      exitstatus = EX_OSERR;
1027
1081
      goto fallback;
1036
1090
                                              &sigchld_action.sa_mask,
1037
1091
                                              NULL));
1038
1092
    if(ret < 0){
1039
 
      perror("sigprocmask");
 
1093
      error(0, errno, "sigprocmask");
1040
1094
      exitstatus = EX_OSERR;
1041
1095
      goto fallback;
1042
1096
    }
1043
1097
    
 
1098
#if defined (__GNUC__) and defined (__GLIBC__)
 
1099
#if not __GLIBC_PREREQ(2, 16)
 
1100
#pragma GCC diagnostic push
 
1101
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1102
#endif
 
1103
#endif
1044
1104
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1045
 
                                          -Wconversion */
 
1105
                                          -Wconversion in GNU libc
 
1106
                                          before 2.16 */
 
1107
#if defined (__GNUC__) and defined (__GLIBC__)
 
1108
#if not __GLIBC_PREREQ(2, 16)
 
1109
#pragma GCC diagnostic pop
 
1110
#endif
 
1111
#endif
1046
1112
    
1047
1113
    if(maxfd < new_plugin->fd){
1048
1114
      maxfd = new_plugin->fd;
1069
1135
    fd_set rfds = rfds_all;
1070
1136
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1071
1137
    if(select_ret == -1 and errno != EINTR){
1072
 
      perror("select");
 
1138
      error(0, errno, "select");
1073
1139
      exitstatus = EX_OSERR;
1074
1140
      goto fallback;
1075
1141
    }
1102
1168
          }
1103
1169
          
1104
1170
          /* Remove the plugin */
 
1171
#if defined (__GNUC__) and defined (__GLIBC__)
 
1172
#if not __GLIBC_PREREQ(2, 16)
 
1173
#pragma GCC diagnostic push
 
1174
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1175
#endif
 
1176
#endif
1105
1177
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1106
 
                                          -Wconversion */
 
1178
                                          -Wconversion in GNU libc
 
1179
                                          before 2.16 */
 
1180
#if defined (__GNUC__) and defined (__GLIBC__)
 
1181
#if not __GLIBC_PREREQ(2, 16)
 
1182
#pragma GCC diagnostic pop
 
1183
#endif
 
1184
#endif
1107
1185
          
1108
1186
          /* Block signal while modifying process_list */
1109
1187
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1111
1189
                                         &sigchld_action.sa_mask,
1112
1190
                                         NULL));
1113
1191
          if(ret < 0){
1114
 
            perror("sigprocmask");
 
1192
            error(0, errno, "sigprocmask");
1115
1193
            exitstatus = EX_OSERR;
1116
1194
            goto fallback;
1117
1195
          }
1125
1203
                      (sigprocmask(SIG_UNBLOCK,
1126
1204
                                   &sigchld_action.sa_mask, NULL)));
1127
1205
          if(ret < 0){
1128
 
            perror("sigprocmask");
 
1206
            error(0, errno, "sigprocmask");
1129
1207
            exitstatus = EX_OSERR;
1130
1208
            goto fallback;
1131
1209
          }
1142
1220
        bool bret = print_out_password(proc->buffer,
1143
1221
                                       proc->buffer_length);
1144
1222
        if(not bret){
1145
 
          perror("print_out_password");
 
1223
          error(0, errno, "print_out_password");
1146
1224
          exitstatus = EX_IOERR;
1147
1225
        }
1148
1226
        goto fallback;
1149
1227
      }
1150
1228
      
1151
1229
      /* This process has not completed.  Does it have any output? */
 
1230
#if defined (__GNUC__) and defined (__GLIBC__)
 
1231
#if not __GLIBC_PREREQ(2, 16)
 
1232
#pragma GCC diagnostic push
 
1233
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1234
#endif
 
1235
#endif
1152
1236
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1153
1237
                                                         warning from
1154
 
                                                         -Wconversion */
 
1238
                                                         -Wconversion
 
1239
                                                         in GNU libc
 
1240
                                                         before
 
1241
                                                         2.16 */
 
1242
#if defined (__GNUC__) and defined (__GLIBC__)
 
1243
#if not __GLIBC_PREREQ(2, 16)
 
1244
#pragma GCC diagnostic pop
 
1245
#endif
 
1246
#endif
1155
1247
        /* This process had nothing to say at this time */
1156
1248
        proc = proc->next;
1157
1249
        continue;
1158
1250
      }
1159
1251
      /* Before reading, make the process' data buffer large enough */
1160
1252
      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");
 
1253
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1254
                                   + (size_t) BUFFER_SIZE);
 
1255
        if(new_buffer == NULL){
 
1256
          error(0, errno, "malloc");
1165
1257
          exitstatus = EX_OSERR;
1166
1258
          goto fallback;
1167
1259
        }
 
1260
        proc->buffer = new_buffer;
1168
1261
        proc->buffer_size += BUFFER_SIZE;
1169
1262
      }
1170
1263
      /* Read from the process */
1204
1297
    }
1205
1298
    bret = print_out_password(passwordbuffer, len);
1206
1299
    if(not bret){
1207
 
      perror("print_out_password");
 
1300
      error(0, errno, "print_out_password");
1208
1301
      exitstatus = EX_IOERR;
1209
1302
    }
1210
1303
  }
1212
1305
  /* Restore old signal handler */
1213
1306
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1214
1307
  if(ret == -1){
1215
 
    perror("sigaction");
 
1308
    error(0, errno, "sigaction");
1216
1309
    exitstatus = EX_OSERR;
1217
1310
  }
1218
1311
  
1234
1327
      ret = kill(p->pid, SIGTERM);
1235
1328
      if(ret == -1 and errno != ESRCH){
1236
1329
        /* Set-uid proccesses might not get closed */
1237
 
        perror("kill");
 
1330
        error(0, errno, "kill");
1238
1331
      }
1239
1332
    }
1240
1333
  }
1244
1337
    ret = wait(NULL);
1245
1338
  } while(ret >= 0);
1246
1339
  if(errno != ECHILD){
1247
 
    perror("wait");
 
1340
    error(0, errno, "wait");
1248
1341
  }
1249
1342
  
1250
1343
  free_plugin_list();