/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: 2009-11-05 19:16:46 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091105191646-5l7bkq5h4wkh3huh
* mandos-monitor: New prototype version of interactive server
                  administraton tool using D-Bus.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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(),
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
56
56
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
57
                                   strsignal() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
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:
745
743
  /* Strip permissions down to nobody */
746
744
  setgid(gid);
747
745
  if(ret == -1){
748
 
    error(0, errno, "setgid");
 
746
    perror("setgid");
749
747
  }
750
748
  ret = setuid(uid);
751
749
  if(ret == -1){
752
 
    error(0, errno, "setuid");
 
750
    perror("setuid");
753
751
  }
754
752
  
755
753
  /* Open plugin directory with close_on_exec flag */
773
771
                    );
774
772
    }
775
773
    if(dir_fd == -1){
776
 
      error(0, errno, "Could not open plugin dir");
 
774
      perror("Could not open plugin dir");
777
775
      exitstatus = EX_UNAVAILABLE;
778
776
      goto fallback;
779
777
    }
782
780
  /* Set the FD_CLOEXEC flag on the directory */
783
781
    ret = set_cloexec_flag(dir_fd);
784
782
    if(ret < 0){
785
 
      error(0, errno, "set_cloexec_flag");
 
783
      perror("set_cloexec_flag");
786
784
      TEMP_FAILURE_RETRY(close(dir_fd));
787
785
      exitstatus = EX_OSERR;
788
786
      goto fallback;
791
789
    
792
790
    dir = fdopendir(dir_fd);
793
791
    if(dir == NULL){
794
 
      error(0, errno, "Could not open plugin dir");
 
792
      perror("Could not open plugin dir");
795
793
      TEMP_FAILURE_RETRY(close(dir_fd));
796
794
      exitstatus = EX_OSERR;
797
795
      goto fallback;
809
807
    /* All directory entries have been processed */
810
808
    if(dirst == NULL){
811
809
      if(errno == EBADF){
812
 
        error(0, errno, "readdir");
 
810
        perror("readdir");
813
811
        exitstatus = EX_IOERR;
814
812
        goto fallback;
815
813
      }
846
844
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
847
845
        size_t suf_len = strlen(*suf);
848
846
        if((d_name_len >= suf_len)
849
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
847
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
850
848
                == 0)){
851
849
          if(debug){
852
850
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
872
870
                                             dirst->d_name));
873
871
    }
874
872
    if(ret < 0){
875
 
      error(0, errno, "asprintf");
 
873
      perror("asprintf");
876
874
      continue;
877
875
    }
878
876
    
879
877
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
880
878
    if(ret == -1){
881
 
      error(0, errno, "stat");
 
879
      perror("stat");
882
880
      free(filename);
883
881
      continue;
884
882
    }
896
894
    
897
895
    plugin *p = getplugin(dirst->d_name);
898
896
    if(p == NULL){
899
 
      error(0, errno, "getplugin");
 
897
      perror("getplugin");
900
898
      free(filename);
901
899
      continue;
902
900
    }
914
912
      if(g != NULL){
915
913
        for(char **a = g->argv + 1; *a != NULL; a++){
916
914
          if(not add_argument(p, *a)){
917
 
            error(0, errno, "add_argument");
 
915
            perror("add_argument");
918
916
          }
919
917
        }
920
918
        /* Add global environment variables */
921
919
        for(char **e = g->environ; *e != NULL; e++){
922
920
          if(not add_environment(p, *e, false)){
923
 
            error(0, errno, "add_environment");
 
921
            perror("add_environment");
924
922
          }
925
923
        }
926
924
      }
931
929
    if(p->environ[0] != NULL){
932
930
      for(char **e = environ; *e != NULL; e++){
933
931
        if(not add_environment(p, *e, false)){
934
 
          error(0, errno, "add_environment");
 
932
          perror("add_environment");
935
933
        }
936
934
      }
937
935
    }
939
937
    int pipefd[2];
940
938
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
941
939
    if(ret == -1){
942
 
      error(0, errno, "pipe");
 
940
      perror("pipe");
943
941
      exitstatus = EX_OSERR;
944
942
      goto fallback;
945
943
    }
946
944
    /* Ask OS to automatic close the pipe on exec */
947
945
    ret = set_cloexec_flag(pipefd[0]);
948
946
    if(ret < 0){
949
 
      error(0, errno, "set_cloexec_flag");
 
947
      perror("set_cloexec_flag");
950
948
      exitstatus = EX_OSERR;
951
949
      goto fallback;
952
950
    }
953
951
    ret = set_cloexec_flag(pipefd[1]);
954
952
    if(ret < 0){
955
 
      error(0, errno, "set_cloexec_flag");
 
953
      perror("set_cloexec_flag");
956
954
      exitstatus = EX_OSERR;
957
955
      goto fallback;
958
956
    }
961
959
                                              &sigchld_action.sa_mask,
962
960
                                              NULL));
963
961
    if(ret < 0){
964
 
      error(0, errno, "sigprocmask");
 
962
      perror("sigprocmask");
965
963
      exitstatus = EX_OSERR;
966
964
      goto fallback;
967
965
    }
971
969
      pid = fork();
972
970
    } while(pid == -1 and errno == EINTR);
973
971
    if(pid == -1){
974
 
      error(0, errno, "fork");
 
972
      perror("fork");
975
973
      exitstatus = EX_OSERR;
976
974
      goto fallback;
977
975
    }
979
977
      /* this is the child process */
980
978
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
981
979
      if(ret < 0){
982
 
        error(0, errno, "sigaction");
 
980
        perror("sigaction");
983
981
        _exit(EX_OSERR);
984
982
      }
985
983
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
986
984
      if(ret < 0){
987
 
        error(0, errno, "sigprocmask");
 
985
        perror("sigprocmask");
988
986
        _exit(EX_OSERR);
989
987
      }
990
988
      
991
989
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
992
990
      if(ret == -1){
993
 
        error(0, errno, "dup2");
 
991
        perror("dup2");
994
992
        _exit(EX_OSERR);
995
993
      }
996
994
      
1001
999
      }
1002
1000
      if(p->environ[0] == NULL){
1003
1001
        if(execv(filename, p->argv) < 0){
1004
 
          error(0, errno, "execv for %s", filename);
 
1002
          perror("execv");
1005
1003
          _exit(EX_OSERR);
1006
1004
        }
1007
1005
      } else {
1008
1006
        if(execve(filename, p->argv, p->environ) < 0){
1009
 
          error(0, errno, "execve for %s", filename);
 
1007
          perror("execve");
1010
1008
          _exit(EX_OSERR);
1011
1009
        }
1012
1010
      }
1018
1016
    free(filename);
1019
1017
    plugin *new_plugin = getplugin(dirst->d_name);
1020
1018
    if(new_plugin == NULL){
1021
 
      error(0, errno, "getplugin");
 
1019
      perror("getplugin");
1022
1020
      ret = (int)(TEMP_FAILURE_RETRY
1023
1021
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1024
1022
                               NULL)));
1025
1023
      if(ret < 0){
1026
 
        error(0, errno, "sigprocmask");
 
1024
        perror("sigprocmask");
1027
1025
      }
1028
1026
      exitstatus = EX_OSERR;
1029
1027
      goto fallback;
1038
1036
                                              &sigchld_action.sa_mask,
1039
1037
                                              NULL));
1040
1038
    if(ret < 0){
1041
 
      error(0, errno, "sigprocmask");
 
1039
      perror("sigprocmask");
1042
1040
      exitstatus = EX_OSERR;
1043
1041
      goto fallback;
1044
1042
    }
1071
1069
    fd_set rfds = rfds_all;
1072
1070
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1073
1071
    if(select_ret == -1 and errno != EINTR){
1074
 
      error(0, errno, "select");
 
1072
      perror("select");
1075
1073
      exitstatus = EX_OSERR;
1076
1074
      goto fallback;
1077
1075
    }
1113
1111
                                         &sigchld_action.sa_mask,
1114
1112
                                         NULL));
1115
1113
          if(ret < 0){
1116
 
            error(0, errno, "sigprocmask");
 
1114
            perror("sigprocmask");
1117
1115
            exitstatus = EX_OSERR;
1118
1116
            goto fallback;
1119
1117
          }
1127
1125
                      (sigprocmask(SIG_UNBLOCK,
1128
1126
                                   &sigchld_action.sa_mask, NULL)));
1129
1127
          if(ret < 0){
1130
 
            error(0, errno, "sigprocmask");
 
1128
            perror("sigprocmask");
1131
1129
            exitstatus = EX_OSERR;
1132
1130
            goto fallback;
1133
1131
          }
1144
1142
        bool bret = print_out_password(proc->buffer,
1145
1143
                                       proc->buffer_length);
1146
1144
        if(not bret){
1147
 
          error(0, errno, "print_out_password");
 
1145
          perror("print_out_password");
1148
1146
          exitstatus = EX_IOERR;
1149
1147
        }
1150
1148
        goto fallback;
1163
1161
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1164
1162
                               + (size_t) BUFFER_SIZE);
1165
1163
        if(proc->buffer == NULL){
1166
 
          error(0, errno, "malloc");
 
1164
          perror("malloc");
1167
1165
          exitstatus = EX_OSERR;
1168
1166
          goto fallback;
1169
1167
        }
1206
1204
    }
1207
1205
    bret = print_out_password(passwordbuffer, len);
1208
1206
    if(not bret){
1209
 
      error(0, errno, "print_out_password");
 
1207
      perror("print_out_password");
1210
1208
      exitstatus = EX_IOERR;
1211
1209
    }
1212
1210
  }
1214
1212
  /* Restore old signal handler */
1215
1213
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1216
1214
  if(ret == -1){
1217
 
    error(0, errno, "sigaction");
 
1215
    perror("sigaction");
1218
1216
    exitstatus = EX_OSERR;
1219
1217
  }
1220
1218
  
1236
1234
      ret = kill(p->pid, SIGTERM);
1237
1235
      if(ret == -1 and errno != ESRCH){
1238
1236
        /* Set-uid proccesses might not get closed */
1239
 
        error(0, errno, "kill");
 
1237
        perror("kill");
1240
1238
      }
1241
1239
    }
1242
1240
  }
1246
1244
    ret = wait(NULL);
1247
1245
  } while(ret >= 0);
1248
1246
  if(errno != ECHILD){
1249
 
    error(0, errno, "wait");
 
1247
    perror("wait");
1250
1248
  }
1251
1249
  
1252
1250
  free_plugin_list();