/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

* mandos-clients.conf.xml (EXPANSION/RUNTIME EXPANSION): List possible
                                                         keys.

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