/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

  • Committer: Teddy Hogeborn
  • Date: 2014-03-23 22:12:57 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 311.
  • Revision ID: teddy@recompile.se-20140323221257-9lkufevfdn4kwoq6
Use openat() etc. in plugin-runner.  Also one less useless warning.

* plugin-runner.c (main): Only print warning about failure to work
                          around Debian bug #633582 if the plugin
                          directory actually exists.  Use openat(),
                          fstat(), faccessat(), and eliminate
                          asprintf().

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-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 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
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   O_CLOEXEC, pipe2() */
 
26
                                   O_CLOEXEC */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
29
                                   realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#include <stdio.h>              /* fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO, fclose() */
33
 
#include <sys/types.h>          /* fstat(), struct stat, waitpid(),
34
 
                                   WIFEXITED(), WEXITSTATUS(), wait(),
35
 
                                   pid_t, uid_t, gid_t, getuid(),
36
 
                                   getgid() */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), fstat(), struct
 
34
                                   stat, waitpid(), WIFEXITED(),
 
35
                                   WEXITSTATUS(), wait(), pid_t,
 
36
                                   uid_t, gid_t, getuid(), getgid(),
 
37
                                   dirfd() */
37
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
38
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
39
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
42
                                   WCOREDUMP() */
42
43
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
44
 
#include <dirent.h>             /* struct dirent, scandirat() */
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
46
                                   readdir(), closedir(), dirfd() */
45
47
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
46
48
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
47
49
                                   struct stat, fstat(), close(),
48
50
                                   setgid(), setuid(), S_ISREG(),
49
 
                                   faccessat() pipe2(), fork(),
 
51
                                   faccessat() pipe(), fork(),
50
52
                                   _exit(), dup2(), fexecve(), read()
51
53
                                */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC, openat(), scandirat(),
54
 
                                   pipe2() */
 
55
                                   FD_CLOEXEC, openat() */
55
56
#include <string.h>             /* strsep, strlen(), strsignal(),
56
57
                                   strcmp(), strncmp() */
57
58
#include <errno.h>              /* errno */
71
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
72
73
#include <errno.h>              /* errno */
73
74
#include <error.h>              /* error() */
74
 
#include <fnmatch.h>            /* fnmatch() */
75
75
 
76
76
#define BUFFER_SIZE 256
77
77
 
78
78
#define PDIR "/lib/mandos/plugins.d"
79
 
#define PHDIR "/lib/mandos/plugin-helpers"
80
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
81
80
 
82
81
const char *argp_program_version = "plugin-runner " VERSION;
241
240
  return add_to_char_array(def, &(p->environ), &(p->envc));
242
241
}
243
242
 
244
 
#ifndef O_CLOEXEC
245
243
/*
246
244
 * Based on the example in the GNU LibC manual chapter 13.13 "File
247
245
 * Descriptor Flags".
258
256
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
259
257
                                       ret | FD_CLOEXEC));
260
258
}
261
 
#endif  /* not O_CLOEXEC */
262
259
 
263
260
 
264
261
/* Mark processes as completed when they exit, and save their exit
348
345
 
349
346
int main(int argc, char *argv[]){
350
347
  char *plugindir = NULL;
351
 
  char *pluginhelperdir = NULL;
352
348
  char *argfile = NULL;
353
349
  FILE *conffp;
354
 
  struct dirent **direntries = NULL;
 
350
  size_t d_name_len;
 
351
  DIR *dir = NULL;
 
352
  struct dirent *dirst;
355
353
  struct stat st;
356
354
  fd_set rfds_all;
357
355
  int ret, maxfd = 0;
365
363
                                      .sa_flags = SA_NOCLDSTOP };
366
364
  char **custom_argv = NULL;
367
365
  int custom_argc = 0;
368
 
  int dir_fd = -1;
 
366
  int dir_fd;
369
367
  
370
368
  /* Establish a signal handler */
371
369
  sigemptyset(&sigchld_action.sa_mask);
416
414
      .doc = "Group ID the plugins will run as", .group = 3 },
417
415
    { .name = "debug", .key = 132,
418
416
      .doc = "Debug mode", .group = 4 },
419
 
    { .name = "plugin-helper-dir", .key = 133,
420
 
      .arg = "DIRECTORY",
421
 
      .doc = "Specify a different plugin helper directory",
422
 
      .group = 2 },
423
417
    /*
424
418
     * These reproduce what we would get without ARGP_NO_HELP
425
419
     */
551
545
    case 132:                   /* --debug */
552
546
      debug = true;
553
547
      break;
554
 
    case 133:                   /* --plugin-helper-dir */
555
 
      free(pluginhelperdir);
556
 
      pluginhelperdir = strdup(arg);
557
 
      if(pluginhelperdir != NULL){
558
 
        errno = 0;
559
 
      }
560
 
      break;
561
548
      /*
562
549
       * These reproduce what we would get without ARGP_NO_HELP
563
550
       */
614
601
    case 130:                   /* --userid */
615
602
    case 131:                   /* --groupid */
616
603
    case 132:                   /* --debug */
617
 
    case 133:                   /* --plugin-helper-dir */
618
604
    case '?':                   /* --help */
619
605
    case -3:                    /* --usage */
620
606
    case 'V':                   /* --version */
775
761
    goto fallback;
776
762
  }
777
763
  
778
 
  {
779
 
    char *pluginhelperenv;
780
 
    bool bret = true;
781
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
782
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
783
 
    if(ret != -1){
784
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
785
 
    }
786
 
    if(ret == -1 or not bret){
787
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
788
 
            " environment variable to \"%s\" for all plugins\n",
789
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
790
 
    }
791
 
    if(ret != -1){
792
 
      free(pluginhelperenv);
793
 
    }
794
 
  }
795
 
  
796
764
  if(debug){
797
765
    for(plugin *p = plugin_list; p != NULL; p=p->next){
798
766
      fprintf(stderr, "Plugin: %s has %d arguments\n",
861
829
    ret = set_cloexec_flag(dir_fd);
862
830
    if(ret < 0){
863
831
      error(0, errno, "set_cloexec_flag");
 
832
      TEMP_FAILURE_RETRY(close(dir_fd));
864
833
      exitstatus = EX_OSERR;
865
834
      goto fallback;
866
835
    }
867
836
#endif  /* O_CLOEXEC */
868
 
  }
869
 
  
870
 
  int good_name(const struct dirent * const dirent){
871
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
872
 
                                      "*.dpkg-old", "*.dpkg-bak",
873
 
                                      "*.dpkg-divert", NULL };
874
 
#ifdef __GNUC__
875
 
#pragma GCC diagnostic push
876
 
#pragma GCC diagnostic ignored "-Wcast-qual"
877
 
#endif
878
 
    for(const char **pat = (const char **)patterns;
879
 
        *pat != NULL; pat++){
880
 
#ifdef __GNUC__
881
 
#pragma GCC diagnostic pop
882
 
#endif
883
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
884
 
         != FNM_NOMATCH){
885
 
        if(debug){
886
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
887
 
                    " matching pattern %s\n", dirent->d_name, *pat);
888
 
        }
889
 
        return 0;
890
 
      }
 
837
    
 
838
    dir = fdopendir(dir_fd);
 
839
    if(dir == NULL){
 
840
      error(0, errno, "Could not open plugin dir");
 
841
      TEMP_FAILURE_RETRY(close(dir_fd));
 
842
      exitstatus = EX_OSERR;
 
843
      goto fallback;
891
844
    }
892
 
    return 1;
893
 
  }
894
 
  
895
 
#ifdef __GLIBC__
896
 
#if __GLIBC_PREREQ(2, 15)
897
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
898
 
                             alphasort);
899
 
#else  /* not __GLIBC_PREREQ(2, 15) */
900
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
901
 
                           &direntries, good_name, alphasort);
902
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
903
 
#else   /* not __GLIBC__ */
904
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
905
 
                           &direntries, good_name, alphasort);
906
 
#endif  /* not __GLIBC__ */
907
 
  if(numplugins == -1){
908
 
    error(0, errno, "Could not scan plugin dir");
909
 
    direntries = NULL;
910
 
    exitstatus = EX_OSERR;
911
 
    goto fallback;
912
845
  }
913
846
  
914
847
  FD_ZERO(&rfds_all);
915
848
  
916
849
  /* Read and execute any executable in the plugin directory*/
917
 
  for(int i = 0; i < numplugins; i++){
918
 
    
919
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
 
850
  while(true){
 
851
    do {
 
852
      dirst = readdir(dir);
 
853
    } while(dirst == NULL and errno == EINTR);
 
854
    
 
855
    /* All directory entries have been processed */
 
856
    if(dirst == NULL){
 
857
      if(errno == EBADF){
 
858
        error(0, errno, "readdir");
 
859
        exitstatus = EX_IOERR;
 
860
        goto fallback;
 
861
      }
 
862
      break;
 
863
    }
 
864
    
 
865
    d_name_len = strlen(dirst->d_name);
 
866
    
 
867
    /* Ignore dotfiles, backup files and other junk */
 
868
    {
 
869
      bool bad_name = false;
 
870
      
 
871
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
872
      
 
873
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
874
                                           ".dpkg-old",
 
875
                                           ".dpkg-bak",
 
876
                                           ".dpkg-divert", NULL };
 
877
#ifdef __GNUC__
 
878
#pragma GCC diagnostic push
 
879
#pragma GCC diagnostic ignored "-Wcast-qual"
 
880
#endif
 
881
      for(const char **pre = (const char **)bad_prefixes;
 
882
          *pre != NULL; pre++){
 
883
#ifdef __GNUC__
 
884
#pragma GCC diagnostic pop
 
885
#endif
 
886
        size_t pre_len = strlen(*pre);
 
887
        if((d_name_len >= pre_len)
 
888
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
 
889
          if(debug){
 
890
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
891
                    " with bad prefix %s\n", dirst->d_name, *pre);
 
892
          }
 
893
          bad_name = true;
 
894
          break;
 
895
        }
 
896
      }
 
897
      if(bad_name){
 
898
        continue;
 
899
      }
 
900
#ifdef __GNUC__
 
901
#pragma GCC diagnostic push
 
902
#pragma GCC diagnostic ignored "-Wcast-qual"
 
903
#endif
 
904
      for(const char **suf = (const char **)bad_suffixes;
 
905
          *suf != NULL; suf++){
 
906
#ifdef __GNUC__
 
907
#pragma GCC diagnostic pop
 
908
#endif
 
909
        size_t suf_len = strlen(*suf);
 
910
        if((d_name_len >= suf_len)
 
911
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
912
                == 0)){
 
913
          if(debug){
 
914
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
915
                    " with bad suffix %s\n", dirst->d_name, *suf);
 
916
          }
 
917
          bad_name = true;
 
918
          break;
 
919
        }
 
920
      }
 
921
      
 
922
      if(bad_name){
 
923
        continue;
 
924
      }
 
925
    }
 
926
    
 
927
    int plugin_fd = openat(dir_fd, dirst->d_name, O_RDONLY |
 
928
#ifdef O_CLOEXEC
 
929
                            O_CLOEXEC
 
930
#else  /* not O_CLOEXEC */
 
931
                            0
 
932
#endif  /* not O_CLOEXEC */
 
933
                            );
920
934
    if(plugin_fd == -1){
921
935
      error(0, errno, "Could not open plugin");
922
 
      free(direntries[i]);
923
 
      continue;
924
 
    }
 
936
      continue;
 
937
    }
 
938
#ifndef O_CLOEXEC
 
939
  /* Set the FD_CLOEXEC flag on the plugin FD */
 
940
    ret = set_cloexec_flag(plugin_fd);
 
941
    if(ret < 0){
 
942
      error(0, errno, "set_cloexec_flag");
 
943
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
944
      continue;
 
945
    }
 
946
#endif  /* O_CLOEXEC */
925
947
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
926
948
    if(ret == -1){
927
949
      error(0, errno, "stat");
928
950
      TEMP_FAILURE_RETRY(close(plugin_fd));
929
 
      free(direntries[i]);
930
951
      continue;
931
952
    }
932
953
    
933
954
    /* Ignore non-executable files */
934
955
    if(not S_ISREG(st.st_mode)
935
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
936
 
                                        X_OK, 0)) != 0)){
 
956
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, dirst->d_name, X_OK,
 
957
                                        0)) != 0)){
937
958
      if(debug){
938
959
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
939
960
                " with bad type or mode\n",
940
 
                plugindir != NULL ? plugindir : PDIR,
941
 
                direntries[i]->d_name);
 
961
                plugindir != NULL ? plugindir : PDIR, dirst->d_name);
942
962
      }
943
963
      TEMP_FAILURE_RETRY(close(plugin_fd));
944
 
      free(direntries[i]);
945
964
      continue;
946
965
    }
947
966
    
948
 
    plugin *p = getplugin(direntries[i]->d_name);
 
967
    plugin *p = getplugin(dirst->d_name);
949
968
    if(p == NULL){
950
969
      error(0, errno, "getplugin");
951
970
      TEMP_FAILURE_RETRY(close(plugin_fd));
952
 
      free(direntries[i]);
953
971
      continue;
954
972
    }
955
973
    if(p->disabled){
956
974
      if(debug){
957
975
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
958
 
                direntries[i]->d_name);
 
976
                dirst->d_name);
959
977
      }
960
978
      TEMP_FAILURE_RETRY(close(plugin_fd));
961
 
      free(direntries[i]);
962
979
      continue;
963
980
    }
964
981
    {
989
1006
    }
990
1007
    
991
1008
    int pipefd[2];
992
 
#ifndef O_CLOEXEC
993
1009
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
994
 
#else  /* O_CLOEXEC */
995
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
996
 
#endif  /* O_CLOEXEC */
997
1010
    if(ret == -1){
998
1011
      error(0, errno, "pipe");
999
1012
      exitstatus = EX_OSERR;
1000
 
      free(direntries[i]);
1001
 
      goto fallback;
1002
 
    }
1003
 
    if(pipefd[0] >= FD_SETSIZE){
1004
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
1005
 
              FD_SETSIZE);
1006
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1007
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1008
 
      exitstatus = EX_OSERR;
1009
 
      free(direntries[i]);
1010
 
      goto fallback;
1011
 
    }
1012
 
#ifndef O_CLOEXEC
 
1013
      goto fallback;
 
1014
    }
1013
1015
    /* Ask OS to automatic close the pipe on exec */
1014
1016
    ret = set_cloexec_flag(pipefd[0]);
1015
1017
    if(ret < 0){
1016
1018
      error(0, errno, "set_cloexec_flag");
1017
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1018
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1019
1019
      exitstatus = EX_OSERR;
1020
 
      free(direntries[i]);
1021
1020
      goto fallback;
1022
1021
    }
1023
1022
    ret = set_cloexec_flag(pipefd[1]);
1024
1023
    if(ret < 0){
1025
1024
      error(0, errno, "set_cloexec_flag");
1026
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1027
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1028
1025
      exitstatus = EX_OSERR;
1029
 
      free(direntries[i]);
1030
1026
      goto fallback;
1031
1027
    }
1032
 
#endif  /* not O_CLOEXEC */
1033
1028
    /* Block SIGCHLD until process is safely in process list */
1034
1029
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1035
1030
                                              &sigchld_action.sa_mask,
1037
1032
    if(ret < 0){
1038
1033
      error(0, errno, "sigprocmask");
1039
1034
      exitstatus = EX_OSERR;
1040
 
      free(direntries[i]);
1041
1035
      goto fallback;
1042
1036
    }
1043
1037
    /* Starting a new process to be watched */
1047
1041
    } while(pid == -1 and errno == EINTR);
1048
1042
    if(pid == -1){
1049
1043
      error(0, errno, "fork");
1050
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1051
 
                                     &sigchld_action.sa_mask, NULL));
1052
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1053
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1054
1044
      exitstatus = EX_OSERR;
1055
 
      free(direntries[i]);
1056
1045
      goto fallback;
1057
1046
    }
1058
1047
    if(pid == 0){
1074
1063
        _exit(EX_OSERR);
1075
1064
      }
1076
1065
      
 
1066
      if(dirfd(dir) < 0){
 
1067
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
1068
           above and must now close it manually here. */
 
1069
        closedir(dir);
 
1070
      }
1077
1071
      if(fexecve(plugin_fd, p->argv,
1078
1072
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1079
1073
        error(0, errno, "fexecve for %s/%s",
1080
 
              plugindir != NULL ? plugindir : PDIR,
1081
 
              direntries[i]->d_name);
 
1074
              plugindir != NULL ? plugindir : PDIR, dirst->d_name);
1082
1075
        _exit(EX_OSERR);
1083
1076
      }
1084
1077
      /* no return */
1087
1080
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1088
1081
                                             pipe */
1089
1082
    TEMP_FAILURE_RETRY(close(plugin_fd));
1090
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1083
    plugin *new_plugin = getplugin(dirst->d_name);
1091
1084
    if(new_plugin == NULL){
1092
1085
      error(0, errno, "getplugin");
1093
1086
      ret = (int)(TEMP_FAILURE_RETRY
1097
1090
        error(0, errno, "sigprocmask");
1098
1091
      }
1099
1092
      exitstatus = EX_OSERR;
1100
 
      free(direntries[i]);
1101
1093
      goto fallback;
1102
1094
    }
1103
 
    free(direntries[i]);
1104
1095
    
1105
1096
    new_plugin->pid = pid;
1106
1097
    new_plugin->fd = pipefd[0];
1136
1127
    }
1137
1128
  }
1138
1129
  
1139
 
  free(direntries);
1140
 
  direntries = NULL;
1141
 
  TEMP_FAILURE_RETRY(close(dir_fd));
1142
 
  dir_fd = -1;
 
1130
  TEMP_FAILURE_RETRY(closedir(dir));
 
1131
  dir = NULL;
1143
1132
  free_plugin(getplugin(NULL));
1144
1133
  
1145
1134
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1339
1328
    free(custom_argv);
1340
1329
  }
1341
1330
  
1342
 
  free(direntries);
1343
 
  
1344
 
  if(dir_fd != -1){
1345
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1331
  if(dir != NULL){
 
1332
    closedir(dir);
1346
1333
  }
1347
1334
  
1348
1335
  /* Kill the processes */
1368
1355
  free_plugin_list();
1369
1356
  
1370
1357
  free(plugindir);
1371
 
  free(pluginhelperdir);
1372
1358
  free(argfile);
1373
1359
  
1374
1360
  return exitstatus;