/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-09 18:16:14 UTC
  • mfrom: (237.2.35 mandos-empty-device)
  • Revision ID: teddy@fukt.bsnet.se-20100909181614-oanlmvkzsiodbo3c
Merge in branch to interpret an empty device name to mean
"autodetect".

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