/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-27 21:24:33 UTC
  • Revision ID: teddy@recompile.se-20140327212433-oxdvfg1vsaagc6v0
Use fnmatch()in plugin-runner.

* plugin-runner.c (main): Use fnmatch().

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 */
240
241
  return add_to_char_array(def, &(p->environ), &(p->envc));
241
242
}
242
243
 
243
 
#ifndef O_CLOEXEC
244
244
/*
245
245
 * Based on the example in the GNU LibC manual chapter 13.13 "File
246
246
 * Descriptor Flags".
257
257
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
258
258
                                       ret | FD_CLOEXEC));
259
259
}
260
 
#endif  /* not O_CLOEXEC */
261
260
 
262
261
 
263
262
/* Mark processes as completed when they exit, and save their exit
349
348
  char *plugindir = NULL;
350
349
  char *argfile = NULL;
351
350
  FILE *conffp;
352
 
  struct dirent **direntries;
 
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;
 
366
  int dir_fd;
367
367
  
368
368
  /* Establish a signal handler */
369
369
  sigemptyset(&sigchld_action.sa_mask);
834
834
      goto fallback;
835
835
    }
836
836
#endif  /* O_CLOEXEC */
 
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;
 
844
    }
837
845
  }
838
846
  
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 };
 
847
  FD_ZERO(&rfds_all);
 
848
  
 
849
  /* Read and execute any executable in the plugin directory*/
 
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
    /* Ignore dotfiles, backup files and other junk */
 
866
    {
 
867
      bool bad_name = false;
 
868
      const char * const patterns[] = { ".*", "#*#", "*~",
 
869
                                        "*.dpkg-new", "*.dpkg-old",
 
870
                                        "*.dpkg-bak", "*.dpkg-divert",
 
871
                                        NULL };
843
872
#ifdef __GNUC__
844
873
#pragma GCC diagnostic push
845
874
#pragma GCC diagnostic ignored "-Wcast-qual"
846
875
#endif
847
 
    for(const char **pat = (const char **)patterns;
848
 
        *pat != NULL; pat++){
 
876
      for(const char **pat = (const char **)patterns;
 
877
          *pat != NULL; pat++){
849
878
#ifdef __GNUC__
850
879
#pragma GCC diagnostic pop
851
880
#endif
852
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
853
 
         != FNM_NOMATCH){
854
 
        if(debug){
 
881
        if(fnmatch(*pat, dirst->d_name,
 
882
                   FNM_FILE_NAME | FNM_PERIOD) != FNM_NOMATCH){
 
883
          if(debug){
855
884
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
856
 
                    " matching pattern %s\n", dirent->d_name, *pat);
 
885
                    " matching pattern %s\n", dirst->d_name, *pat);
 
886
          }
 
887
          bad_name = true;
 
888
          break;
857
889
        }
858
 
        return 0;
 
890
      }
 
891
      if(bad_name){
 
892
        continue;
859
893
      }
860
894
    }
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
 
  }
882
 
  
883
 
  FD_ZERO(&rfds_all);
884
 
  
885
 
  /* Read and execute any executable in the plugin directory*/
886
 
  for(int i = 0; i < numplugins; i++){
887
895
    
888
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
 
896
    int plugin_fd = openat(dir_fd, dirst->d_name, O_RDONLY |
 
897
#ifdef O_CLOEXEC
 
898
                            O_CLOEXEC
 
899
#else  /* not O_CLOEXEC */
 
900
                            0
 
901
#endif  /* not O_CLOEXEC */
 
902
                            );
889
903
    if(plugin_fd == -1){
890
904
      error(0, errno, "Could not open plugin");
891
905
      continue;
892
906
    }
 
907
#ifndef O_CLOEXEC
 
908
  /* Set the FD_CLOEXEC flag on the plugin FD */
 
909
    ret = set_cloexec_flag(plugin_fd);
 
910
    if(ret < 0){
 
911
      error(0, errno, "set_cloexec_flag");
 
912
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
913
      continue;
 
914
    }
 
915
#endif  /* O_CLOEXEC */
893
916
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
894
917
    if(ret == -1){
895
918
      error(0, errno, "stat");
899
922
    
900
923
    /* Ignore non-executable files */
901
924
    if(not S_ISREG(st.st_mode)
902
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
903
 
                                        X_OK, 0)) != 0)){
 
925
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, dirst->d_name, X_OK,
 
926
                                        0)) != 0)){
904
927
      if(debug){
905
928
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
906
929
                " with bad type or mode\n",
907
 
                plugindir != NULL ? plugindir : PDIR,
908
 
                direntries[i]->d_name);
 
930
                plugindir != NULL ? plugindir : PDIR, dirst->d_name);
909
931
      }
910
932
      TEMP_FAILURE_RETRY(close(plugin_fd));
911
933
      continue;
912
934
    }
913
935
    
914
 
    plugin *p = getplugin(direntries[i]->d_name);
 
936
    plugin *p = getplugin(dirst->d_name);
915
937
    if(p == NULL){
916
938
      error(0, errno, "getplugin");
917
939
      TEMP_FAILURE_RETRY(close(plugin_fd));
920
942
    if(p->disabled){
921
943
      if(debug){
922
944
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
923
 
                direntries[i]->d_name);
 
945
                dirst->d_name);
924
946
      }
925
947
      TEMP_FAILURE_RETRY(close(plugin_fd));
926
948
      continue;
953
975
    }
954
976
    
955
977
    int pipefd[2];
956
 
#ifndef O_CLOEXEC
957
978
    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
979
    if(ret == -1){
962
980
      error(0, errno, "pipe");
963
981
      exitstatus = EX_OSERR;
964
982
      goto fallback;
965
983
    }
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
984
    /* Ask OS to automatic close the pipe on exec */
976
985
    ret = set_cloexec_flag(pipefd[0]);
977
986
    if(ret < 0){
978
987
      error(0, errno, "set_cloexec_flag");
979
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
980
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
981
988
      exitstatus = EX_OSERR;
982
989
      goto fallback;
983
990
    }
984
991
    ret = set_cloexec_flag(pipefd[1]);
985
992
    if(ret < 0){
986
993
      error(0, errno, "set_cloexec_flag");
987
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
988
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
989
994
      exitstatus = EX_OSERR;
990
995
      goto fallback;
991
996
    }
992
 
#endif  /* not O_CLOEXEC */
993
997
    /* Block SIGCHLD until process is safely in process list */
994
998
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
995
999
                                              &sigchld_action.sa_mask,
1006
1010
    } while(pid == -1 and errno == EINTR);
1007
1011
    if(pid == -1){
1008
1012
      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
1013
      exitstatus = EX_OSERR;
1014
1014
      goto fallback;
1015
1015
    }
1032
1032
        _exit(EX_OSERR);
1033
1033
      }
1034
1034
      
 
1035
      if(dirfd(dir) < 0){
 
1036
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
1037
           above and must now close it manually here. */
 
1038
        closedir(dir);
 
1039
      }
1035
1040
      if(fexecve(plugin_fd, p->argv,
1036
1041
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1037
1042
        error(0, errno, "fexecve for %s/%s",
1038
 
              plugindir != NULL ? plugindir : PDIR,
1039
 
              direntries[i]->d_name);
 
1043
              plugindir != NULL ? plugindir : PDIR, dirst->d_name);
1040
1044
        _exit(EX_OSERR);
1041
1045
      }
1042
1046
      /* no return */
1045
1049
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1046
1050
                                             pipe */
1047
1051
    TEMP_FAILURE_RETRY(close(plugin_fd));
1048
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1052
    plugin *new_plugin = getplugin(dirst->d_name);
1049
1053
    if(new_plugin == NULL){
1050
1054
      error(0, errno, "getplugin");
1051
1055
      ret = (int)(TEMP_FAILURE_RETRY
1092
1096
    }
1093
1097
  }
1094
1098
  
1095
 
  TEMP_FAILURE_RETRY(close(dir_fd));
 
1099
  TEMP_FAILURE_RETRY(closedir(dir));
 
1100
  dir = NULL;
1096
1101
  free_plugin(getplugin(NULL));
1097
1102
  
1098
1103
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1292
1297
    free(custom_argv);
1293
1298
  }
1294
1299
  
1295
 
  if(dir_fd != -1){
1296
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1300
  if(dir != NULL){
 
1301
    closedir(dir);
1297
1302
  }
1298
1303
  
1299
1304
  /* Kill the processes */