/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 at bsnet
  • Date: 2010-09-02 18:36:36 UTC
  • mto: (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: teddy@fukt.bsnet.se-20100902183636-h0vb4iw3ut2g3mta
* mandos (ClientDBus.approvals_pending): Changed to be a property
                                         which emits D-Bus signals.
  (ClientDBus.approved_pending): Removed - all callers changed.
  (ClientDBus.GotSecret, ClientDBus.Rejected,
  ClientDBus.NeedApproval): Do not emit D-Bus signal.

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