/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: 2011-10-03 16:17:11 UTC
  • mfrom: (505.1.1 teddy)
  • Revision ID: teddy@recompile.se-20111003161711-q9bjy1hlov9xanxh
Some cleanup

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-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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
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
 
265
268
        /* No child processes */
266
269
        break;
267
270
      }
268
 
      perror("waitpid");
 
271
      error(0, errno, "waitpid");
269
272
    }
270
273
    
271
274
    /* A child exited, find it in process_list */
356
359
  sigemptyset(&sigchld_action.sa_mask);
357
360
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
358
361
  if(ret == -1){
359
 
    perror("sigaddset");
360
 
    exitstatus = EXIT_FAILURE;
 
362
    error(0, errno, "sigaddset");
 
363
    exitstatus = EX_OSERR;
361
364
    goto fallback;
362
365
  }
363
366
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
364
367
  if(ret == -1){
365
 
    perror("sigaction");
366
 
    exitstatus = EXIT_FAILURE;
 
368
    error(0, errno, "sigaction");
 
369
    exitstatus = EX_OSERR;
367
370
    goto fallback;
368
371
  }
369
372
  
598
601
  case ENOMEM:
599
602
  default:
600
603
    errno = ret;
601
 
    perror("argp_parse");
 
604
    error(0, errno, "argp_parse");
602
605
    exitstatus = EX_OSERR;
603
606
    goto fallback;
604
607
  case EINVAL:
625
628
    custom_argc = 1;
626
629
    custom_argv = malloc(sizeof(char*) * 2);
627
630
    if(custom_argv == NULL){
628
 
      perror("malloc");
629
 
      exitstatus = EXIT_FAILURE;
 
631
      error(0, errno, "malloc");
 
632
      exitstatus = EX_OSERR;
630
633
      goto fallback;
631
634
    }
632
635
    custom_argv[0] = argv[0];
648
651
        }
649
652
        new_arg = strdup(p);
650
653
        if(new_arg == NULL){
651
 
          perror("strdup");
652
 
          exitstatus = EXIT_FAILURE;
 
654
          error(0, errno, "strdup");
 
655
          exitstatus = EX_OSERR;
653
656
          free(org_line);
654
657
          goto fallback;
655
658
        }
658
661
        custom_argv = realloc(custom_argv, sizeof(char *)
659
662
                              * ((unsigned int) custom_argc + 1));
660
663
        if(custom_argv == NULL){
661
 
          perror("realloc");
662
 
          exitstatus = EXIT_FAILURE;
 
664
          error(0, errno, "realloc");
 
665
          exitstatus = EX_OSERR;
663
666
          free(org_line);
664
667
          goto fallback;
665
668
        }
671
674
      ret = fclose(conffp);
672
675
    } while(ret == EOF and errno == EINTR);
673
676
    if(ret == EOF){
674
 
      perror("fclose");
675
 
      exitstatus = EXIT_FAILURE;
 
677
      error(0, errno, "fclose");
 
678
      exitstatus = EX_IOERR;
676
679
      goto fallback;
677
680
    }
678
681
    free(org_line);
680
683
    /* Check for harmful errors and go to fallback. Other errors might
681
684
       not affect opening plugins */
682
685
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
683
 
      perror("fopen");
684
 
      exitstatus = EXIT_FAILURE;
 
686
      error(0, errno, "fopen");
 
687
      exitstatus = EX_OSERR;
685
688
      goto fallback;
686
689
    }
687
690
  }
697
700
    case ENOMEM:
698
701
    default:
699
702
      errno = ret;
700
 
      perror("argp_parse");
 
703
      error(0, errno, "argp_parse");
701
704
      exitstatus = EX_OSERR;
702
705
      goto fallback;
703
706
    case EINVAL:
717
720
  case ENOMEM:
718
721
  default:
719
722
    errno = ret;
720
 
    perror("argp_parse");
 
723
    error(0, errno, "argp_parse");
721
724
    exitstatus = EX_OSERR;
722
725
    goto fallback;
723
726
  case EINVAL:
739
742
    }
740
743
  }
741
744
  
742
 
  /* Strip permissions down to nobody */
 
745
  {
 
746
    /* Work around Debian bug #633582:
 
747
       <http://bugs.debian.org/633582> */
 
748
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
749
    if(plugindir_fd == -1){
 
750
      error(0, errno, "open");
 
751
    } else {
 
752
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
753
      if(ret == -1){
 
754
        error(0, errno, "fstat");
 
755
      } else {
 
756
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
757
          ret = fchown(plugindir_fd, uid, gid);
 
758
          if(ret == -1){
 
759
            error(0, errno, "fchown");
 
760
          }
 
761
        }
 
762
      }
 
763
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
764
    }
 
765
  }
 
766
  
 
767
  /* Lower permissions */
743
768
  setgid(gid);
744
769
  if(ret == -1){
745
 
    perror("setgid");
 
770
    error(0, errno, "setgid");
746
771
  }
747
772
  ret = setuid(uid);
748
773
  if(ret == -1){
749
 
    perror("setuid");
 
774
    error(0, errno, "setuid");
750
775
  }
751
776
  
752
777
  /* Open plugin directory with close_on_exec flag */
770
795
                    );
771
796
    }
772
797
    if(dir_fd == -1){
773
 
      perror("Could not open plugin dir");
774
 
      exitstatus = EXIT_FAILURE;
 
798
      error(0, errno, "Could not open plugin dir");
 
799
      exitstatus = EX_UNAVAILABLE;
775
800
      goto fallback;
776
801
    }
777
802
    
779
804
  /* Set the FD_CLOEXEC flag on the directory */
780
805
    ret = set_cloexec_flag(dir_fd);
781
806
    if(ret < 0){
782
 
      perror("set_cloexec_flag");
 
807
      error(0, errno, "set_cloexec_flag");
783
808
      TEMP_FAILURE_RETRY(close(dir_fd));
784
 
      exitstatus = EXIT_FAILURE;
 
809
      exitstatus = EX_OSERR;
785
810
      goto fallback;
786
811
    }
787
812
#endif  /* O_CLOEXEC */
788
813
    
789
814
    dir = fdopendir(dir_fd);
790
815
    if(dir == NULL){
791
 
      perror("Could not open plugin dir");
 
816
      error(0, errno, "Could not open plugin dir");
792
817
      TEMP_FAILURE_RETRY(close(dir_fd));
793
 
      exitstatus = EXIT_FAILURE;
 
818
      exitstatus = EX_OSERR;
794
819
      goto fallback;
795
820
    }
796
821
  }
806
831
    /* All directory entries have been processed */
807
832
    if(dirst == NULL){
808
833
      if(errno == EBADF){
809
 
        perror("readdir");
810
 
        exitstatus = EXIT_FAILURE;
 
834
        error(0, errno, "readdir");
 
835
        exitstatus = EX_IOERR;
811
836
        goto fallback;
812
837
      }
813
838
      break;
843
868
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
844
869
        size_t suf_len = strlen(*suf);
845
870
        if((d_name_len >= suf_len)
846
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
871
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
847
872
                == 0)){
848
873
          if(debug){
849
874
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
869
894
                                             dirst->d_name));
870
895
    }
871
896
    if(ret < 0){
872
 
      perror("asprintf");
 
897
      error(0, errno, "asprintf");
873
898
      continue;
874
899
    }
875
900
    
876
901
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
877
902
    if(ret == -1){
878
 
      perror("stat");
 
903
      error(0, errno, "stat");
879
904
      free(filename);
880
905
      continue;
881
906
    }
893
918
    
894
919
    plugin *p = getplugin(dirst->d_name);
895
920
    if(p == NULL){
896
 
      perror("getplugin");
 
921
      error(0, errno, "getplugin");
897
922
      free(filename);
898
923
      continue;
899
924
    }
911
936
      if(g != NULL){
912
937
        for(char **a = g->argv + 1; *a != NULL; a++){
913
938
          if(not add_argument(p, *a)){
914
 
            perror("add_argument");
 
939
            error(0, errno, "add_argument");
915
940
          }
916
941
        }
917
942
        /* Add global environment variables */
918
943
        for(char **e = g->environ; *e != NULL; e++){
919
944
          if(not add_environment(p, *e, false)){
920
 
            perror("add_environment");
 
945
            error(0, errno, "add_environment");
921
946
          }
922
947
        }
923
948
      }
928
953
    if(p->environ[0] != NULL){
929
954
      for(char **e = environ; *e != NULL; e++){
930
955
        if(not add_environment(p, *e, false)){
931
 
          perror("add_environment");
 
956
          error(0, errno, "add_environment");
932
957
        }
933
958
      }
934
959
    }
936
961
    int pipefd[2];
937
962
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
938
963
    if(ret == -1){
939
 
      perror("pipe");
940
 
      exitstatus = EXIT_FAILURE;
 
964
      error(0, errno, "pipe");
 
965
      exitstatus = EX_OSERR;
941
966
      goto fallback;
942
967
    }
943
968
    /* Ask OS to automatic close the pipe on exec */
944
969
    ret = set_cloexec_flag(pipefd[0]);
945
970
    if(ret < 0){
946
 
      perror("set_cloexec_flag");
947
 
      exitstatus = EXIT_FAILURE;
 
971
      error(0, errno, "set_cloexec_flag");
 
972
      exitstatus = EX_OSERR;
948
973
      goto fallback;
949
974
    }
950
975
    ret = set_cloexec_flag(pipefd[1]);
951
976
    if(ret < 0){
952
 
      perror("set_cloexec_flag");
953
 
      exitstatus = EXIT_FAILURE;
 
977
      error(0, errno, "set_cloexec_flag");
 
978
      exitstatus = EX_OSERR;
954
979
      goto fallback;
955
980
    }
956
981
    /* Block SIGCHLD until process is safely in process list */
958
983
                                              &sigchld_action.sa_mask,
959
984
                                              NULL));
960
985
    if(ret < 0){
961
 
      perror("sigprocmask");
962
 
      exitstatus = EXIT_FAILURE;
 
986
      error(0, errno, "sigprocmask");
 
987
      exitstatus = EX_OSERR;
963
988
      goto fallback;
964
989
    }
965
990
    /* Starting a new process to be watched */
968
993
      pid = fork();
969
994
    } while(pid == -1 and errno == EINTR);
970
995
    if(pid == -1){
971
 
      perror("fork");
972
 
      exitstatus = EXIT_FAILURE;
 
996
      error(0, errno, "fork");
 
997
      exitstatus = EX_OSERR;
973
998
      goto fallback;
974
999
    }
975
1000
    if(pid == 0){
976
1001
      /* this is the child process */
977
1002
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
978
1003
      if(ret < 0){
979
 
        perror("sigaction");
980
 
        _exit(EXIT_FAILURE);
 
1004
        error(0, errno, "sigaction");
 
1005
        _exit(EX_OSERR);
981
1006
      }
982
1007
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
983
1008
      if(ret < 0){
984
 
        perror("sigprocmask");
985
 
        _exit(EXIT_FAILURE);
 
1009
        error(0, errno, "sigprocmask");
 
1010
        _exit(EX_OSERR);
986
1011
      }
987
1012
      
988
1013
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
989
1014
      if(ret == -1){
990
 
        perror("dup2");
991
 
        _exit(EXIT_FAILURE);
 
1015
        error(0, errno, "dup2");
 
1016
        _exit(EX_OSERR);
992
1017
      }
993
1018
      
994
1019
      if(dirfd(dir) < 0){
998
1023
      }
999
1024
      if(p->environ[0] == NULL){
1000
1025
        if(execv(filename, p->argv) < 0){
1001
 
          perror("execv");
1002
 
          _exit(EXIT_FAILURE);
 
1026
          error(0, errno, "execv for %s", filename);
 
1027
          _exit(EX_OSERR);
1003
1028
        }
1004
1029
      } else {
1005
1030
        if(execve(filename, p->argv, p->environ) < 0){
1006
 
          perror("execve");
1007
 
          _exit(EXIT_FAILURE);
 
1031
          error(0, errno, "execve for %s", filename);
 
1032
          _exit(EX_OSERR);
1008
1033
        }
1009
1034
      }
1010
1035
      /* no return */
1015
1040
    free(filename);
1016
1041
    plugin *new_plugin = getplugin(dirst->d_name);
1017
1042
    if(new_plugin == NULL){
1018
 
      perror("getplugin");
 
1043
      error(0, errno, "getplugin");
1019
1044
      ret = (int)(TEMP_FAILURE_RETRY
1020
1045
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1021
1046
                               NULL)));
1022
1047
      if(ret < 0){
1023
 
        perror("sigprocmask");
 
1048
        error(0, errno, "sigprocmask");
1024
1049
      }
1025
 
      exitstatus = EXIT_FAILURE;
 
1050
      exitstatus = EX_OSERR;
1026
1051
      goto fallback;
1027
1052
    }
1028
1053
    
1035
1060
                                              &sigchld_action.sa_mask,
1036
1061
                                              NULL));
1037
1062
    if(ret < 0){
1038
 
      perror("sigprocmask");
1039
 
      exitstatus = EXIT_FAILURE;
 
1063
      error(0, errno, "sigprocmask");
 
1064
      exitstatus = EX_OSERR;
1040
1065
      goto fallback;
1041
1066
    }
1042
1067
    
1068
1093
    fd_set rfds = rfds_all;
1069
1094
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1070
1095
    if(select_ret == -1 and errno != EINTR){
1071
 
      perror("select");
1072
 
      exitstatus = EXIT_FAILURE;
 
1096
      error(0, errno, "select");
 
1097
      exitstatus = EX_OSERR;
1073
1098
      goto fallback;
1074
1099
    }
1075
1100
    /* OK, now either a process completed, or something can be read
1110
1135
                                         &sigchld_action.sa_mask,
1111
1136
                                         NULL));
1112
1137
          if(ret < 0){
1113
 
            perror("sigprocmask");
1114
 
            exitstatus = EXIT_FAILURE;
 
1138
            error(0, errno, "sigprocmask");
 
1139
            exitstatus = EX_OSERR;
1115
1140
            goto fallback;
1116
1141
          }
1117
1142
          
1124
1149
                      (sigprocmask(SIG_UNBLOCK,
1125
1150
                                   &sigchld_action.sa_mask, NULL)));
1126
1151
          if(ret < 0){
1127
 
            perror("sigprocmask");
1128
 
            exitstatus = EXIT_FAILURE;
 
1152
            error(0, errno, "sigprocmask");
 
1153
            exitstatus = EX_OSERR;
1129
1154
            goto fallback;
1130
1155
          }
1131
1156
          
1141
1166
        bool bret = print_out_password(proc->buffer,
1142
1167
                                       proc->buffer_length);
1143
1168
        if(not bret){
1144
 
          perror("print_out_password");
1145
 
          exitstatus = EXIT_FAILURE;
 
1169
          error(0, errno, "print_out_password");
 
1170
          exitstatus = EX_IOERR;
1146
1171
        }
1147
1172
        goto fallback;
1148
1173
      }
1160
1185
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1161
1186
                               + (size_t) BUFFER_SIZE);
1162
1187
        if(proc->buffer == NULL){
1163
 
          perror("malloc");
1164
 
          exitstatus = EXIT_FAILURE;
 
1188
          error(0, errno, "malloc");
 
1189
          exitstatus = EX_OSERR;
1165
1190
          goto fallback;
1166
1191
        }
1167
1192
        proc->buffer_size += BUFFER_SIZE;
1188
1213
  
1189
1214
 fallback:
1190
1215
  
1191
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1216
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1217
                             and exitstatus != EX_OK)){
1192
1218
    /* Fallback if all plugins failed, none are found or an error
1193
1219
       occured */
1194
1220
    bool bret;
1202
1228
    }
1203
1229
    bret = print_out_password(passwordbuffer, len);
1204
1230
    if(not bret){
1205
 
      perror("print_out_password");
1206
 
      exitstatus = EXIT_FAILURE;
 
1231
      error(0, errno, "print_out_password");
 
1232
      exitstatus = EX_IOERR;
1207
1233
    }
1208
1234
  }
1209
1235
  
1210
1236
  /* Restore old signal handler */
1211
1237
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1212
1238
  if(ret == -1){
1213
 
    perror("sigaction");
1214
 
    exitstatus = EXIT_FAILURE;
 
1239
    error(0, errno, "sigaction");
 
1240
    exitstatus = EX_OSERR;
1215
1241
  }
1216
1242
  
1217
1243
  if(custom_argv != NULL){
1232
1258
      ret = kill(p->pid, SIGTERM);
1233
1259
      if(ret == -1 and errno != ESRCH){
1234
1260
        /* Set-uid proccesses might not get closed */
1235
 
        perror("kill");
 
1261
        error(0, errno, "kill");
1236
1262
      }
1237
1263
    }
1238
1264
  }
1242
1268
    ret = wait(NULL);
1243
1269
  } while(ret >= 0);
1244
1270
  if(errno != ECHILD){
1245
 
    perror("wait");
 
1271
    error(0, errno, "wait");
1246
1272
  }
1247
1273
  
1248
1274
  free_plugin_list();