/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: 2014-03-23 19:55:18 UTC
  • Revision ID: teddy@recompile.se-20140323195518-amvoytwi12iheypo
Make mandos-client prefer /run/tmp over /tmp.

* plugins.d/mandos-client.c (init_gpgme): Add more const.
  (main): Make "tempdir" a pointer to the temporary directory, if one
          has been created, or NULL.  This also eliminates the need
          for the "tempdir_created" variable.

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
                                   asprintf(), 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
 
                                   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() */
 
32
                                   stderr, STDOUT_FILENO */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), 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(),
40
41
                                   WEXITSTATUS(), WTERMSIG(),
41
42
                                   WCOREDUMP() */
42
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
44
 
#include <dirent.h>             /* struct dirent, scandirat() */
45
 
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
46
 
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
47
 
                                   struct stat, fstat(), close(),
48
 
                                   setgid(), setuid(), S_ISREG(),
49
 
                                   faccessat() pipe2(), fork(),
50
 
                                   _exit(), dup2(), fexecve(), read()
51
 
                                */
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
46
                                   readdir(), closedir(), dirfd() */
 
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
 
48
                                   fcntl(), setuid(), setgid(),
 
49
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
 
50
                                   access(), pipe(), fork(), close()
 
51
                                   dup2(), STDOUT_FILENO, _exit(),
 
52
                                   execv(), write(), read(),
 
53
                                   close() */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC, openat(), scandirat(),
54
 
                                   pipe2() */
55
 
#include <string.h>             /* strsep, strlen(), strsignal(),
56
 
                                   strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal(), strcmp(), strncmp() */
57
58
#include <errno.h>              /* errno */
58
59
#include <argp.h>               /* struct argp_option, struct
59
60
                                   argp_state, struct argp,
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
 
240
240
  return add_to_char_array(def, &(p->environ), &(p->envc));
241
241
}
242
242
 
243
 
#ifndef O_CLOEXEC
244
243
/*
245
244
 * Based on the example in the GNU LibC manual chapter 13.13 "File
246
245
 * Descriptor Flags".
257
256
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
258
257
                                       ret | FD_CLOEXEC));
259
258
}
260
 
#endif  /* not O_CLOEXEC */
261
259
 
262
260
 
263
261
/* Mark processes as completed when they exit, and save their exit
349
347
  char *plugindir = NULL;
350
348
  char *argfile = NULL;
351
349
  FILE *conffp;
352
 
  struct dirent **direntries;
 
350
  size_t d_name_len;
 
351
  DIR *dir = NULL;
 
352
  struct dirent *dirst;
353
353
  struct stat st;
354
354
  fd_set rfds_all;
355
355
  int ret, maxfd = 0;
363
363
                                      .sa_flags = SA_NOCLDSTOP };
364
364
  char **custom_argv = NULL;
365
365
  int custom_argc = 0;
366
 
  int dir_fd = -1;
367
366
  
368
367
  /* Establish a signal handler */
369
368
  sigemptyset(&sigchld_action.sa_mask);
780
779
       <http://bugs.debian.org/633582> */
781
780
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
782
781
    if(plugindir_fd == -1){
783
 
      if(errno != ENOENT){
784
 
        error(0, errno, "open(\"" PDIR "\")");
785
 
      }
 
782
      error(0, errno, "open");
786
783
    } else {
787
784
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
788
785
      if(ret == -1){
811
808
  
812
809
  /* Open plugin directory with close_on_exec flag */
813
810
  {
814
 
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
815
 
#ifdef O_CLOEXEC
816
 
                  O_CLOEXEC
817
 
#else  /* not O_CLOEXEC */
818
 
                  0
819
 
#endif  /* not O_CLOEXEC */
820
 
                  );
 
811
    int dir_fd = -1;
 
812
    if(plugindir == NULL){
 
813
      dir_fd = open(PDIR, O_RDONLY |
 
814
#ifdef O_CLOEXEC
 
815
                    O_CLOEXEC
 
816
#else  /* not O_CLOEXEC */
 
817
                    0
 
818
#endif  /* not O_CLOEXEC */
 
819
                    );
 
820
    } else {
 
821
      dir_fd = open(plugindir, O_RDONLY |
 
822
#ifdef O_CLOEXEC
 
823
                    O_CLOEXEC
 
824
#else  /* not O_CLOEXEC */
 
825
                    0
 
826
#endif  /* not O_CLOEXEC */
 
827
                    );
 
828
    }
821
829
    if(dir_fd == -1){
822
830
      error(0, errno, "Could not open plugin dir");
823
831
      exitstatus = EX_UNAVAILABLE;
834
842
      goto fallback;
835
843
    }
836
844
#endif  /* O_CLOEXEC */
837
 
  }
838
 
  
839
 
  int good_name(const struct dirent * const dirent){
840
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
841
 
                                      "*.dpkg-old", "*.dpkg-bak",
842
 
                                      "*.dpkg-divert", NULL };
843
 
#ifdef __GNUC__
844
 
#pragma GCC diagnostic push
845
 
#pragma GCC diagnostic ignored "-Wcast-qual"
846
 
#endif
847
 
    for(const char **pat = (const char **)patterns;
848
 
        *pat != NULL; pat++){
849
 
#ifdef __GNUC__
850
 
#pragma GCC diagnostic pop
851
 
#endif
852
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
853
 
         != FNM_NOMATCH){
854
 
        if(debug){
855
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
856
 
                    " matching pattern %s\n", dirent->d_name, *pat);
857
 
        }
858
 
        return 0;
859
 
      }
 
845
    
 
846
    dir = fdopendir(dir_fd);
 
847
    if(dir == NULL){
 
848
      error(0, errno, "Could not open plugin dir");
 
849
      TEMP_FAILURE_RETRY(close(dir_fd));
 
850
      exitstatus = EX_OSERR;
 
851
      goto fallback;
860
852
    }
861
 
    return 1;
862
 
  }
863
 
  
864
 
#ifdef __GLIBC__
865
 
#if __GLIBC_PREREQ(2, 15)
866
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
867
 
                             alphasort);
868
 
#else  /* not __GLIBC_PREREQ(2, 15) */
869
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
870
 
                           &direntries, good_name, alphasort);
871
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
872
 
#else   /* not __GLIBC__ */
873
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
874
 
                           &direntries, good_name, alphasort);
875
 
#endif  /* not __GLIBC__ */
876
 
  if(numplugins == -1){
877
 
    error(0, errno, "Could not scan plugin dir");
878
 
    TEMP_FAILURE_RETRY(close(dir_fd));
879
 
    exitstatus = EX_OSERR;
880
 
    goto fallback;
881
853
  }
882
854
  
883
855
  FD_ZERO(&rfds_all);
884
856
  
885
857
  /* Read and execute any executable in the plugin directory*/
886
 
  for(int i = 0; i < numplugins; i++){
887
 
    
888
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
889
 
    if(plugin_fd == -1){
890
 
      error(0, errno, "Could not open plugin");
 
858
  while(true){
 
859
    do {
 
860
      dirst = readdir(dir);
 
861
    } while(dirst == NULL and errno == EINTR);
 
862
    
 
863
    /* All directory entries have been processed */
 
864
    if(dirst == NULL){
 
865
      if(errno == EBADF){
 
866
        error(0, errno, "readdir");
 
867
        exitstatus = EX_IOERR;
 
868
        goto fallback;
 
869
      }
 
870
      break;
 
871
    }
 
872
    
 
873
    d_name_len = strlen(dirst->d_name);
 
874
    
 
875
    /* Ignore dotfiles, backup files and other junk */
 
876
    {
 
877
      bool bad_name = false;
 
878
      
 
879
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
880
      
 
881
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
882
                                           ".dpkg-old",
 
883
                                           ".dpkg-bak",
 
884
                                           ".dpkg-divert", NULL };
 
885
#ifdef __GNUC__
 
886
#pragma GCC diagnostic push
 
887
#pragma GCC diagnostic ignored "-Wcast-qual"
 
888
#endif
 
889
      for(const char **pre = (const char **)bad_prefixes;
 
890
          *pre != NULL; pre++){
 
891
#ifdef __GNUC__
 
892
#pragma GCC diagnostic pop
 
893
#endif
 
894
        size_t pre_len = strlen(*pre);
 
895
        if((d_name_len >= pre_len)
 
896
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
 
897
          if(debug){
 
898
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
899
                    " with bad prefix %s\n", dirst->d_name, *pre);
 
900
          }
 
901
          bad_name = true;
 
902
          break;
 
903
        }
 
904
      }
 
905
      if(bad_name){
 
906
        continue;
 
907
      }
 
908
#ifdef __GNUC__
 
909
#pragma GCC diagnostic push
 
910
#pragma GCC diagnostic ignored "-Wcast-qual"
 
911
#endif
 
912
      for(const char **suf = (const char **)bad_suffixes;
 
913
          *suf != NULL; suf++){
 
914
#ifdef __GNUC__
 
915
#pragma GCC diagnostic pop
 
916
#endif
 
917
        size_t suf_len = strlen(*suf);
 
918
        if((d_name_len >= suf_len)
 
919
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
920
                == 0)){
 
921
          if(debug){
 
922
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
923
                    " with bad suffix %s\n", dirst->d_name, *suf);
 
924
          }
 
925
          bad_name = true;
 
926
          break;
 
927
        }
 
928
      }
 
929
      
 
930
      if(bad_name){
 
931
        continue;
 
932
      }
 
933
    }
 
934
    
 
935
    char *filename;
 
936
    if(plugindir == NULL){
 
937
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
938
                                             dirst->d_name));
 
939
    } else {
 
940
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
941
                                             plugindir,
 
942
                                             dirst->d_name));
 
943
    }
 
944
    if(ret < 0){
 
945
      error(0, errno, "asprintf");
891
946
      continue;
892
947
    }
893
 
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
 
948
    
 
949
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
894
950
    if(ret == -1){
895
951
      error(0, errno, "stat");
896
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
952
      free(filename);
897
953
      continue;
898
954
    }
899
955
    
900
956
    /* Ignore non-executable files */
901
957
    if(not S_ISREG(st.st_mode)
902
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
903
 
                                        X_OK, 0)) != 0)){
 
958
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
904
959
      if(debug){
905
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
906
 
                " with bad type or mode\n",
907
 
                plugindir != NULL ? plugindir : PDIR,
908
 
                direntries[i]->d_name);
 
960
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
961
                " with bad type or mode\n", filename);
909
962
      }
910
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
963
      free(filename);
911
964
      continue;
912
965
    }
913
966
    
914
 
    plugin *p = getplugin(direntries[i]->d_name);
 
967
    plugin *p = getplugin(dirst->d_name);
915
968
    if(p == NULL){
916
969
      error(0, errno, "getplugin");
917
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
970
      free(filename);
918
971
      continue;
919
972
    }
920
973
    if(p->disabled){
921
974
      if(debug){
922
975
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
923
 
                direntries[i]->d_name);
 
976
                dirst->d_name);
924
977
      }
925
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
978
      free(filename);
926
979
      continue;
927
980
    }
928
981
    {
942
995
        }
943
996
      }
944
997
    }
945
 
    /* If this plugin has any environment variables, we need to
946
 
       duplicate the environment from this process, too. */
 
998
    /* If this plugin has any environment variables, we will call
 
999
       using execve and need to duplicate the environment from this
 
1000
       process, too. */
947
1001
    if(p->environ[0] != NULL){
948
1002
      for(char **e = environ; *e != NULL; e++){
949
1003
        if(not add_environment(p, *e, false)){
953
1007
    }
954
1008
    
955
1009
    int pipefd[2];
956
 
#ifndef O_CLOEXEC
957
1010
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
958
 
#else  /* O_CLOEXEC */
959
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
960
 
#endif  /* O_CLOEXEC */
961
1011
    if(ret == -1){
962
1012
      error(0, errno, "pipe");
963
1013
      exitstatus = EX_OSERR;
964
1014
      goto fallback;
965
1015
    }
966
 
    if(pipefd[0] >= FD_SETSIZE){
967
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
968
 
              FD_SETSIZE);
969
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
970
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
971
 
      exitstatus = EX_OSERR;
972
 
      goto fallback;
973
 
    }
974
 
#ifndef O_CLOEXEC
975
1016
    /* Ask OS to automatic close the pipe on exec */
976
1017
    ret = set_cloexec_flag(pipefd[0]);
977
1018
    if(ret < 0){
978
1019
      error(0, errno, "set_cloexec_flag");
979
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
980
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
981
1020
      exitstatus = EX_OSERR;
982
1021
      goto fallback;
983
1022
    }
984
1023
    ret = set_cloexec_flag(pipefd[1]);
985
1024
    if(ret < 0){
986
1025
      error(0, errno, "set_cloexec_flag");
987
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
988
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
989
1026
      exitstatus = EX_OSERR;
990
1027
      goto fallback;
991
1028
    }
992
 
#endif  /* not O_CLOEXEC */
993
1029
    /* Block SIGCHLD until process is safely in process list */
994
1030
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
995
1031
                                              &sigchld_action.sa_mask,
1006
1042
    } while(pid == -1 and errno == EINTR);
1007
1043
    if(pid == -1){
1008
1044
      error(0, errno, "fork");
1009
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1010
 
                                     &sigchld_action.sa_mask, NULL));
1011
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1012
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1013
1045
      exitstatus = EX_OSERR;
1014
1046
      goto fallback;
1015
1047
    }
1032
1064
        _exit(EX_OSERR);
1033
1065
      }
1034
1066
      
1035
 
      if(fexecve(plugin_fd, p->argv,
1036
 
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1037
 
        error(0, errno, "fexecve for %s/%s",
1038
 
              plugindir != NULL ? plugindir : PDIR,
1039
 
              direntries[i]->d_name);
1040
 
        _exit(EX_OSERR);
 
1067
      if(dirfd(dir) < 0){
 
1068
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
1069
           above and must now close it manually here. */
 
1070
        closedir(dir);
 
1071
      }
 
1072
      if(p->environ[0] == NULL){
 
1073
        if(execv(filename, p->argv) < 0){
 
1074
          error(0, errno, "execv for %s", filename);
 
1075
          _exit(EX_OSERR);
 
1076
        }
 
1077
      } else {
 
1078
        if(execve(filename, p->argv, p->environ) < 0){
 
1079
          error(0, errno, "execve for %s", filename);
 
1080
          _exit(EX_OSERR);
 
1081
        }
1041
1082
      }
1042
1083
      /* no return */
1043
1084
    }
1044
1085
    /* Parent process */
1045
1086
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1046
1087
                                             pipe */
1047
 
    TEMP_FAILURE_RETRY(close(plugin_fd));
1048
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1088
    free(filename);
 
1089
    plugin *new_plugin = getplugin(dirst->d_name);
1049
1090
    if(new_plugin == NULL){
1050
1091
      error(0, errno, "getplugin");
1051
1092
      ret = (int)(TEMP_FAILURE_RETRY
1092
1133
    }
1093
1134
  }
1094
1135
  
1095
 
  TEMP_FAILURE_RETRY(close(dir_fd));
 
1136
  TEMP_FAILURE_RETRY(closedir(dir));
 
1137
  dir = NULL;
1096
1138
  free_plugin(getplugin(NULL));
1097
1139
  
1098
1140
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1292
1334
    free(custom_argv);
1293
1335
  }
1294
1336
  
1295
 
  if(dir_fd != -1){
1296
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1337
  if(dir != NULL){
 
1338
    closedir(dir);
1297
1339
  }
1298
1340
  
1299
1341
  /* Kill the processes */