/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-23 19:24:40 UTC
  • Revision ID: teddy@recompile.se-20140323192440-d71iiqxebsxf9u2v
Update GCC warning flags and function attributes to GCC 4.7.

* Makefile (WARN): Update to include almost all warning flags.
* plugin-runner.c (getplugin, add_to_char_array, add_argument,
                   add_environment, set_cloexec_flag,
                   print_out_password): Add attribute
                                        "warn_unused_result".
  (main/parse_opt): Bug fix: Add error checking to --global-env,
                    --env-for, --plugin-dir, and --config-file, and
                    make sure errno does not "leak" from unrelated
                    functions.
* plugins.d/mandos-client.c
  (fprintf_plus, debuggnutls, resolve_callback): Add "nonnull"
                                                 attribute.
  (incbuffer, add_server, init_gpgme): Add "nonnull" and
                                       "warn_unused_result"
                                       attributes.
  (pgp_packet_decrypt, init_gnutls_global): - '' -
  (init_gnutls_session start_mandos_communication, get_flags): - '' -
  (good_flags, good_interface, interface_is_up): - '' -
  (interface_is_running, runnable_hook): - '' -
  (avahi_loop_with_timeout, bring_up_interface): : - '' -
  (safer_gnutls_strerror): Add "warn_unused_result" attribute.
  (notdotentries): Set "nonnull", "pure", and "warn_unused_result"
                   attributes.
  (raise_privileges, raise_privileges_permanently, lower_privileges,
  lower_privileges_permanently): Set "warn_unused_result" attribute.
  (run_network_hooks): Exit child process if it fails to do anything
                       it needs to do.  Make explicit cast to double
                       when passing float value to asprintf().  Change
                       return type to void - all callers changed.
  (bring_up_interface): Move variables "sd", "ret_errno", and
                        "ret_setflags" to innermost scope.  Bug fix:
                        Fail if could not get interface flags also in
                        non-debug mode, and restore old errno
                        correctly.  Print message if could not raise
                        (or later lower) privileges.
  (take_down_interface): Bug fix: When failing because it could not
                         get interface flags, restore old errno
                         correctly.  Print message if it could not
                         raise (or later lower) privileges.
  (main): Complain if failed to raise or lower privileges.  Only run
          network hooks or lower privileges if raising privileges was
          successful.

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 */
103
105
 
104
106
/* Gets an existing plugin based on name,
105
107
   or if none is found, creates a new one */
 
108
__attribute__((warn_unused_result))
106
109
static plugin *getplugin(char *name){
107
110
  /* Check for existing plugin with that name */
108
111
  for(plugin *p = plugin_list; p != NULL; p = p->next){
169
172
}
170
173
 
171
174
/* Helper function for add_argument and add_environment */
 
175
__attribute__((nonnull, warn_unused_result))
172
176
static bool add_to_char_array(const char *new, char ***array,
173
177
                              int *len){
174
178
  /* Resize the pointed-to array to hold one more pointer */
 
179
  char **new_array = NULL;
175
180
  do {
176
 
    *array = realloc(*array, sizeof(char *)
177
 
                     * (size_t) ((*len) + 2));
178
 
  } while(*array == NULL and errno == EINTR);
 
181
    new_array = realloc(*array, sizeof(char *)
 
182
                        * (size_t) ((*len) + 2));
 
183
  } while(new_array == NULL and errno == EINTR);
179
184
  /* Malloc check */
180
 
  if(*array == NULL){
 
185
  if(new_array == NULL){
181
186
    return false;
182
187
  }
 
188
  *array = new_array;
183
189
  /* Make a copy of the new string */
184
190
  char *copy;
185
191
  do {
197
203
}
198
204
 
199
205
/* Add to a plugin's argument vector */
 
206
__attribute__((nonnull(2), warn_unused_result))
200
207
static bool add_argument(plugin *p, const char *arg){
201
208
  if(p == NULL){
202
209
    return false;
205
212
}
206
213
 
207
214
/* Add to a plugin's environment */
 
215
__attribute__((nonnull(2), warn_unused_result))
208
216
static bool add_environment(plugin *p, const char *def, bool replace){
209
217
  if(p == NULL){
210
218
    return false;
212
220
  /* namelen = length of name of environment variable */
213
221
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
214
222
  /* Search for this environment variable */
215
 
  for(char **e = p->environ; *e != NULL; e++){
216
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
223
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
224
    if(strncmp(*envdef, def, namelen + 1) == 0){
217
225
      /* It already exists */
218
226
      if(replace){
219
 
        char *new;
 
227
        char *new_envdef;
220
228
        do {
221
 
          new = realloc(*e, strlen(def) + 1);
222
 
        } while(new == NULL and errno == EINTR);
223
 
        if(new == NULL){
 
229
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
230
        } while(new_envdef == NULL and errno == EINTR);
 
231
        if(new_envdef == NULL){
224
232
          return false;
225
233
        }
226
 
        *e = new;
227
 
        strcpy(*e, def);
 
234
        *envdef = new_envdef;
 
235
        strcpy(*envdef, def);
228
236
      }
229
237
      return true;
230
238
    }
237
245
 * Descriptor Flags".
238
246
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
239
247
 */
 
248
__attribute__((warn_unused_result))
240
249
static int set_cloexec_flag(int fd){
241
250
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
242
251
  /* If reading the flags failed, return error indication now. */
266
275
        /* No child processes */
267
276
        break;
268
277
      }
269
 
      perror("waitpid");
 
278
      error(0, errno, "waitpid");
270
279
    }
271
280
    
272
281
    /* A child exited, find it in process_list */
284
293
}
285
294
 
286
295
/* Prints out a password to stdout */
 
296
__attribute__((nonnull, warn_unused_result))
287
297
static bool print_out_password(const char *buffer, size_t length){
288
298
  ssize_t ret;
289
299
  for(size_t written = 0; written < length; written += (size_t)ret){
297
307
}
298
308
 
299
309
/* Removes and free a plugin from the plugin list */
 
310
__attribute__((nonnull))
300
311
static void free_plugin(plugin *plugin_node){
301
312
  
302
313
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
357
368
  sigemptyset(&sigchld_action.sa_mask);
358
369
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
359
370
  if(ret == -1){
360
 
    perror("sigaddset");
 
371
    error(0, errno, "sigaddset");
361
372
    exitstatus = EX_OSERR;
362
373
    goto fallback;
363
374
  }
364
375
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
365
376
  if(ret == -1){
366
 
    perror("sigaction");
 
377
    error(0, errno, "sigaction");
367
378
    exitstatus = EX_OSERR;
368
379
    goto fallback;
369
380
  }
414
425
    { .name = NULL }
415
426
  };
416
427
  
 
428
  __attribute__((nonnull(3)))
417
429
  error_t parse_opt(int key, char *arg, struct argp_state *state){
418
430
    errno = 0;
419
431
    switch(key){
420
432
      char *tmp;
421
 
      intmax_t tmpmax;
 
433
      intmax_t tmp_id;
422
434
    case 'g':                   /* --global-options */
423
435
      {
424
436
        char *plugin_option;
427
439
            break;
428
440
          }
429
441
        }
 
442
        errno = 0;
430
443
      }
431
444
      break;
432
445
    case 'G':                   /* --global-env */
433
 
      add_environment(getplugin(NULL), arg, true);
 
446
      if(add_environment(getplugin(NULL), arg, true)){
 
447
        errno = 0;
 
448
      }
434
449
      break;
435
450
    case 'o':                   /* --options-for */
436
451
      {
453
468
            break;
454
469
          }
455
470
        }
 
471
        errno = 0;
456
472
      }
457
473
      break;
458
474
    case 'E':                   /* --env-for */
470
486
          errno = EINVAL;
471
487
          break;
472
488
        }
473
 
        add_environment(getplugin(arg), envdef, true);
 
489
        if(add_environment(getplugin(arg), envdef, true)){
 
490
          errno = 0;
 
491
        }
474
492
      }
475
493
      break;
476
494
    case 'd':                   /* --disable */
478
496
        plugin *p = getplugin(arg);
479
497
        if(p != NULL){
480
498
          p->disabled = true;
 
499
          errno = 0;
481
500
        }
482
501
      }
483
502
      break;
486
505
        plugin *p = getplugin(arg);
487
506
        if(p != NULL){
488
507
          p->disabled = false;
 
508
          errno = 0;
489
509
        }
490
510
      }
491
511
      break;
492
512
    case 128:                   /* --plugin-dir */
493
513
      free(plugindir);
494
514
      plugindir = strdup(arg);
 
515
      if(plugindir != NULL){
 
516
        errno = 0;
 
517
      }
495
518
      break;
496
519
    case 129:                   /* --config-file */
497
520
      /* This is already done by parse_opt_config_file() */
498
521
      break;
499
522
    case 130:                   /* --userid */
500
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
523
      tmp_id = strtoimax(arg, &tmp, 10);
501
524
      if(errno != 0 or tmp == arg or *tmp != '\0'
502
 
         or tmpmax != (uid_t)tmpmax){
 
525
         or tmp_id != (uid_t)tmp_id){
503
526
        argp_error(state, "Bad user ID number: \"%s\", using %"
504
527
                   PRIdMAX, arg, (intmax_t)uid);
505
528
        break;
506
529
      }
507
 
      uid = (uid_t)tmpmax;
 
530
      uid = (uid_t)tmp_id;
 
531
      errno = 0;
508
532
      break;
509
533
    case 131:                   /* --groupid */
510
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
534
      tmp_id = strtoimax(arg, &tmp, 10);
511
535
      if(errno != 0 or tmp == arg or *tmp != '\0'
512
 
         or tmpmax != (gid_t)tmpmax){
 
536
         or tmp_id != (gid_t)tmp_id){
513
537
        argp_error(state, "Bad group ID number: \"%s\", using %"
514
538
                   PRIdMAX, arg, (intmax_t)gid);
515
539
        break;
516
540
      }
517
 
      gid = (gid_t)tmpmax;
 
541
      gid = (gid_t)tmp_id;
 
542
      errno = 0;
518
543
      break;
519
544
    case 132:                   /* --debug */
520
545
      debug = true;
568
593
    case 129:                   /* --config-file */
569
594
      free(argfile);
570
595
      argfile = strdup(arg);
 
596
      if(argfile != NULL){
 
597
        errno = 0;
 
598
      }
571
599
      break;
572
600
    case 130:                   /* --userid */
573
601
    case 131:                   /* --groupid */
599
627
  case ENOMEM:
600
628
  default:
601
629
    errno = ret;
602
 
    perror("argp_parse");
 
630
    error(0, errno, "argp_parse");
603
631
    exitstatus = EX_OSERR;
604
632
    goto fallback;
605
633
  case EINVAL:
626
654
    custom_argc = 1;
627
655
    custom_argv = malloc(sizeof(char*) * 2);
628
656
    if(custom_argv == NULL){
629
 
      perror("malloc");
 
657
      error(0, errno, "malloc");
630
658
      exitstatus = EX_OSERR;
631
659
      goto fallback;
632
660
    }
649
677
        }
650
678
        new_arg = strdup(p);
651
679
        if(new_arg == NULL){
652
 
          perror("strdup");
 
680
          error(0, errno, "strdup");
653
681
          exitstatus = EX_OSERR;
654
682
          free(org_line);
655
683
          goto fallback;
656
684
        }
657
685
        
658
686
        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;
 
687
        {
 
688
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
689
                                    * ((unsigned int)
 
690
                                       custom_argc + 1));
 
691
          if(new_argv == NULL){
 
692
            error(0, errno, "realloc");
 
693
            exitstatus = EX_OSERR;
 
694
            free(new_arg);
 
695
            free(org_line);
 
696
            goto fallback;
 
697
          } else {
 
698
            custom_argv = new_argv;
 
699
          }
666
700
        }
667
701
        custom_argv[custom_argc-1] = new_arg;
668
702
        custom_argv[custom_argc] = NULL;
672
706
      ret = fclose(conffp);
673
707
    } while(ret == EOF and errno == EINTR);
674
708
    if(ret == EOF){
675
 
      perror("fclose");
 
709
      error(0, errno, "fclose");
676
710
      exitstatus = EX_IOERR;
677
711
      goto fallback;
678
712
    }
681
715
    /* Check for harmful errors and go to fallback. Other errors might
682
716
       not affect opening plugins */
683
717
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
684
 
      perror("fopen");
 
718
      error(0, errno, "fopen");
685
719
      exitstatus = EX_OSERR;
686
720
      goto fallback;
687
721
    }
698
732
    case ENOMEM:
699
733
    default:
700
734
      errno = ret;
701
 
      perror("argp_parse");
 
735
      error(0, errno, "argp_parse");
702
736
      exitstatus = EX_OSERR;
703
737
      goto fallback;
704
738
    case EINVAL:
718
752
  case ENOMEM:
719
753
  default:
720
754
    errno = ret;
721
 
    perror("argp_parse");
 
755
    error(0, errno, "argp_parse");
722
756
    exitstatus = EX_OSERR;
723
757
    goto fallback;
724
758
  case EINVAL:
740
774
    }
741
775
  }
742
776
  
743
 
  /* Strip permissions down to nobody */
744
 
  setgid(gid);
 
777
  if(getuid() == 0){
 
778
    /* Work around Debian bug #633582:
 
779
       <http://bugs.debian.org/633582> */
 
780
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
781
    if(plugindir_fd == -1){
 
782
      error(0, errno, "open");
 
783
    } else {
 
784
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
785
      if(ret == -1){
 
786
        error(0, errno, "fstat");
 
787
      } else {
 
788
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
789
          ret = fchown(plugindir_fd, uid, gid);
 
790
          if(ret == -1){
 
791
            error(0, errno, "fchown");
 
792
          }
 
793
        }
 
794
      }
 
795
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
796
    }
 
797
  }
 
798
  
 
799
  /* Lower permissions */
 
800
  ret = setgid(gid);
745
801
  if(ret == -1){
746
 
    perror("setgid");
 
802
    error(0, errno, "setgid");
747
803
  }
748
804
  ret = setuid(uid);
749
805
  if(ret == -1){
750
 
    perror("setuid");
 
806
    error(0, errno, "setuid");
751
807
  }
752
808
  
753
809
  /* Open plugin directory with close_on_exec flag */
771
827
                    );
772
828
    }
773
829
    if(dir_fd == -1){
774
 
      perror("Could not open plugin dir");
 
830
      error(0, errno, "Could not open plugin dir");
775
831
      exitstatus = EX_UNAVAILABLE;
776
832
      goto fallback;
777
833
    }
780
836
  /* Set the FD_CLOEXEC flag on the directory */
781
837
    ret = set_cloexec_flag(dir_fd);
782
838
    if(ret < 0){
783
 
      perror("set_cloexec_flag");
 
839
      error(0, errno, "set_cloexec_flag");
784
840
      TEMP_FAILURE_RETRY(close(dir_fd));
785
841
      exitstatus = EX_OSERR;
786
842
      goto fallback;
789
845
    
790
846
    dir = fdopendir(dir_fd);
791
847
    if(dir == NULL){
792
 
      perror("Could not open plugin dir");
 
848
      error(0, errno, "Could not open plugin dir");
793
849
      TEMP_FAILURE_RETRY(close(dir_fd));
794
850
      exitstatus = EX_OSERR;
795
851
      goto fallback;
807
863
    /* All directory entries have been processed */
808
864
    if(dirst == NULL){
809
865
      if(errno == EBADF){
810
 
        perror("readdir");
 
866
        error(0, errno, "readdir");
811
867
        exitstatus = EX_IOERR;
812
868
        goto fallback;
813
869
      }
820
876
    {
821
877
      bool bad_name = false;
822
878
      
823
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
879
      const char * const bad_prefixes[] = { ".", "#", NULL };
824
880
      
825
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
881
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
826
882
                                           ".dpkg-old",
827
883
                                           ".dpkg-bak",
828
884
                                           ".dpkg-divert", NULL };
829
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
885
#ifdef __GNUC__
 
886
#pragma GCC diagnostic push
 
887
#pragma GCC diagnostic ignored "-Wcast-qual"
 
888
#endif
 
889
      for(const char **pre = (const char **)bad_prefixes;
 
890
          *pre != NULL; pre++){
 
891
#ifdef __GNUC__
 
892
#pragma GCC diagnostic pop
 
893
#endif
830
894
        size_t pre_len = strlen(*pre);
831
895
        if((d_name_len >= pre_len)
832
896
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
841
905
      if(bad_name){
842
906
        continue;
843
907
      }
844
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
908
#ifdef __GNUC__
 
909
#pragma GCC diagnostic push
 
910
#pragma GCC diagnostic ignored "-Wcast-qual"
 
911
#endif
 
912
      for(const char **suf = (const char **)bad_suffixes;
 
913
          *suf != NULL; suf++){
 
914
#ifdef __GNUC__
 
915
#pragma GCC diagnostic pop
 
916
#endif
845
917
        size_t suf_len = strlen(*suf);
846
918
        if((d_name_len >= suf_len)
847
919
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
870
942
                                             dirst->d_name));
871
943
    }
872
944
    if(ret < 0){
873
 
      perror("asprintf");
 
945
      error(0, errno, "asprintf");
874
946
      continue;
875
947
    }
876
948
    
877
949
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
878
950
    if(ret == -1){
879
 
      perror("stat");
 
951
      error(0, errno, "stat");
880
952
      free(filename);
881
953
      continue;
882
954
    }
894
966
    
895
967
    plugin *p = getplugin(dirst->d_name);
896
968
    if(p == NULL){
897
 
      perror("getplugin");
 
969
      error(0, errno, "getplugin");
898
970
      free(filename);
899
971
      continue;
900
972
    }
912
984
      if(g != NULL){
913
985
        for(char **a = g->argv + 1; *a != NULL; a++){
914
986
          if(not add_argument(p, *a)){
915
 
            perror("add_argument");
 
987
            error(0, errno, "add_argument");
916
988
          }
917
989
        }
918
990
        /* Add global environment variables */
919
991
        for(char **e = g->environ; *e != NULL; e++){
920
992
          if(not add_environment(p, *e, false)){
921
 
            perror("add_environment");
 
993
            error(0, errno, "add_environment");
922
994
          }
923
995
        }
924
996
      }
929
1001
    if(p->environ[0] != NULL){
930
1002
      for(char **e = environ; *e != NULL; e++){
931
1003
        if(not add_environment(p, *e, false)){
932
 
          perror("add_environment");
 
1004
          error(0, errno, "add_environment");
933
1005
        }
934
1006
      }
935
1007
    }
937
1009
    int pipefd[2];
938
1010
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
939
1011
    if(ret == -1){
940
 
      perror("pipe");
 
1012
      error(0, errno, "pipe");
941
1013
      exitstatus = EX_OSERR;
942
1014
      goto fallback;
943
1015
    }
944
1016
    /* Ask OS to automatic close the pipe on exec */
945
1017
    ret = set_cloexec_flag(pipefd[0]);
946
1018
    if(ret < 0){
947
 
      perror("set_cloexec_flag");
 
1019
      error(0, errno, "set_cloexec_flag");
948
1020
      exitstatus = EX_OSERR;
949
1021
      goto fallback;
950
1022
    }
951
1023
    ret = set_cloexec_flag(pipefd[1]);
952
1024
    if(ret < 0){
953
 
      perror("set_cloexec_flag");
 
1025
      error(0, errno, "set_cloexec_flag");
954
1026
      exitstatus = EX_OSERR;
955
1027
      goto fallback;
956
1028
    }
959
1031
                                              &sigchld_action.sa_mask,
960
1032
                                              NULL));
961
1033
    if(ret < 0){
962
 
      perror("sigprocmask");
 
1034
      error(0, errno, "sigprocmask");
963
1035
      exitstatus = EX_OSERR;
964
1036
      goto fallback;
965
1037
    }
969
1041
      pid = fork();
970
1042
    } while(pid == -1 and errno == EINTR);
971
1043
    if(pid == -1){
972
 
      perror("fork");
 
1044
      error(0, errno, "fork");
973
1045
      exitstatus = EX_OSERR;
974
1046
      goto fallback;
975
1047
    }
977
1049
      /* this is the child process */
978
1050
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
979
1051
      if(ret < 0){
980
 
        perror("sigaction");
 
1052
        error(0, errno, "sigaction");
981
1053
        _exit(EX_OSERR);
982
1054
      }
983
1055
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
984
1056
      if(ret < 0){
985
 
        perror("sigprocmask");
 
1057
        error(0, errno, "sigprocmask");
986
1058
        _exit(EX_OSERR);
987
1059
      }
988
1060
      
989
1061
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
990
1062
      if(ret == -1){
991
 
        perror("dup2");
 
1063
        error(0, errno, "dup2");
992
1064
        _exit(EX_OSERR);
993
1065
      }
994
1066
      
999
1071
      }
1000
1072
      if(p->environ[0] == NULL){
1001
1073
        if(execv(filename, p->argv) < 0){
1002
 
          perror("execv");
 
1074
          error(0, errno, "execv for %s", filename);
1003
1075
          _exit(EX_OSERR);
1004
1076
        }
1005
1077
      } else {
1006
1078
        if(execve(filename, p->argv, p->environ) < 0){
1007
 
          perror("execve");
 
1079
          error(0, errno, "execve for %s", filename);
1008
1080
          _exit(EX_OSERR);
1009
1081
        }
1010
1082
      }
1016
1088
    free(filename);
1017
1089
    plugin *new_plugin = getplugin(dirst->d_name);
1018
1090
    if(new_plugin == NULL){
1019
 
      perror("getplugin");
 
1091
      error(0, errno, "getplugin");
1020
1092
      ret = (int)(TEMP_FAILURE_RETRY
1021
1093
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1022
1094
                               NULL)));
1023
1095
      if(ret < 0){
1024
 
        perror("sigprocmask");
 
1096
        error(0, errno, "sigprocmask");
1025
1097
      }
1026
1098
      exitstatus = EX_OSERR;
1027
1099
      goto fallback;
1036
1108
                                              &sigchld_action.sa_mask,
1037
1109
                                              NULL));
1038
1110
    if(ret < 0){
1039
 
      perror("sigprocmask");
 
1111
      error(0, errno, "sigprocmask");
1040
1112
      exitstatus = EX_OSERR;
1041
1113
      goto fallback;
1042
1114
    }
1043
1115
    
 
1116
#if defined (__GNUC__) and defined (__GLIBC__)
 
1117
#if not __GLIBC_PREREQ(2, 16)
 
1118
#pragma GCC diagnostic push
 
1119
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1120
#endif
 
1121
#endif
1044
1122
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1045
 
                                          -Wconversion */
 
1123
                                          -Wconversion in GNU libc
 
1124
                                          before 2.16 */
 
1125
#if defined (__GNUC__) and defined (__GLIBC__)
 
1126
#if not __GLIBC_PREREQ(2, 16)
 
1127
#pragma GCC diagnostic pop
 
1128
#endif
 
1129
#endif
1046
1130
    
1047
1131
    if(maxfd < new_plugin->fd){
1048
1132
      maxfd = new_plugin->fd;
1069
1153
    fd_set rfds = rfds_all;
1070
1154
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1071
1155
    if(select_ret == -1 and errno != EINTR){
1072
 
      perror("select");
 
1156
      error(0, errno, "select");
1073
1157
      exitstatus = EX_OSERR;
1074
1158
      goto fallback;
1075
1159
    }
1102
1186
          }
1103
1187
          
1104
1188
          /* Remove the plugin */
 
1189
#if defined (__GNUC__) and defined (__GLIBC__)
 
1190
#if not __GLIBC_PREREQ(2, 16)
 
1191
#pragma GCC diagnostic push
 
1192
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1193
#endif
 
1194
#endif
1105
1195
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1106
 
                                          -Wconversion */
 
1196
                                          -Wconversion in GNU libc
 
1197
                                          before 2.16 */
 
1198
#if defined (__GNUC__) and defined (__GLIBC__)
 
1199
#if not __GLIBC_PREREQ(2, 16)
 
1200
#pragma GCC diagnostic pop
 
1201
#endif
 
1202
#endif
1107
1203
          
1108
1204
          /* Block signal while modifying process_list */
1109
1205
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1111
1207
                                         &sigchld_action.sa_mask,
1112
1208
                                         NULL));
1113
1209
          if(ret < 0){
1114
 
            perror("sigprocmask");
 
1210
            error(0, errno, "sigprocmask");
1115
1211
            exitstatus = EX_OSERR;
1116
1212
            goto fallback;
1117
1213
          }
1125
1221
                      (sigprocmask(SIG_UNBLOCK,
1126
1222
                                   &sigchld_action.sa_mask, NULL)));
1127
1223
          if(ret < 0){
1128
 
            perror("sigprocmask");
 
1224
            error(0, errno, "sigprocmask");
1129
1225
            exitstatus = EX_OSERR;
1130
1226
            goto fallback;
1131
1227
          }
1142
1238
        bool bret = print_out_password(proc->buffer,
1143
1239
                                       proc->buffer_length);
1144
1240
        if(not bret){
1145
 
          perror("print_out_password");
 
1241
          error(0, errno, "print_out_password");
1146
1242
          exitstatus = EX_IOERR;
1147
1243
        }
1148
1244
        goto fallback;
1149
1245
      }
1150
1246
      
1151
1247
      /* This process has not completed.  Does it have any output? */
 
1248
#if defined (__GNUC__) and defined (__GLIBC__)
 
1249
#if not __GLIBC_PREREQ(2, 16)
 
1250
#pragma GCC diagnostic push
 
1251
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1252
#endif
 
1253
#endif
1152
1254
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1153
1255
                                                         warning from
1154
 
                                                         -Wconversion */
 
1256
                                                         -Wconversion
 
1257
                                                         in GNU libc
 
1258
                                                         before
 
1259
                                                         2.16 */
 
1260
#if defined (__GNUC__) and defined (__GLIBC__)
 
1261
#if not __GLIBC_PREREQ(2, 16)
 
1262
#pragma GCC diagnostic pop
 
1263
#endif
 
1264
#endif
1155
1265
        /* This process had nothing to say at this time */
1156
1266
        proc = proc->next;
1157
1267
        continue;
1158
1268
      }
1159
1269
      /* Before reading, make the process' data buffer large enough */
1160
1270
      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");
 
1271
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1272
                                   + (size_t) BUFFER_SIZE);
 
1273
        if(new_buffer == NULL){
 
1274
          error(0, errno, "malloc");
1165
1275
          exitstatus = EX_OSERR;
1166
1276
          goto fallback;
1167
1277
        }
 
1278
        proc->buffer = new_buffer;
1168
1279
        proc->buffer_size += BUFFER_SIZE;
1169
1280
      }
1170
1281
      /* Read from the process */
1204
1315
    }
1205
1316
    bret = print_out_password(passwordbuffer, len);
1206
1317
    if(not bret){
1207
 
      perror("print_out_password");
 
1318
      error(0, errno, "print_out_password");
1208
1319
      exitstatus = EX_IOERR;
1209
1320
    }
1210
1321
  }
1212
1323
  /* Restore old signal handler */
1213
1324
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1214
1325
  if(ret == -1){
1215
 
    perror("sigaction");
 
1326
    error(0, errno, "sigaction");
1216
1327
    exitstatus = EX_OSERR;
1217
1328
  }
1218
1329
  
1234
1345
      ret = kill(p->pid, SIGTERM);
1235
1346
      if(ret == -1 and errno != ESRCH){
1236
1347
        /* Set-uid proccesses might not get closed */
1237
 
        perror("kill");
 
1348
        error(0, errno, "kill");
1238
1349
      }
1239
1350
    }
1240
1351
  }
1244
1355
    ret = wait(NULL);
1245
1356
  } while(ret >= 0);
1246
1357
  if(errno != ECHILD){
1247
 
    perror("wait");
 
1358
    error(0, errno, "wait");
1248
1359
  }
1249
1360
  
1250
1361
  free_plugin_list();