/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:24:40 UTC
  • Revision ID: teddy@recompile.se-20140323192440-d71iiqxebsxf9u2v
Update GCC warning flags and function attributes to GCC 4.7.

* Makefile (WARN): Update to include almost all warning flags.
* plugin-runner.c (getplugin, add_to_char_array, add_argument,
                   add_environment, set_cloexec_flag,
                   print_out_password): Add attribute
                                        "warn_unused_result".
  (main/parse_opt): Bug fix: Add error checking to --global-env,
                    --env-for, --plugin-dir, and --config-file, and
                    make sure errno does not "leak" from unrelated
                    functions.
* plugins.d/mandos-client.c
  (fprintf_plus, debuggnutls, resolve_callback): Add "nonnull"
                                                 attribute.
  (incbuffer, add_server, init_gpgme): Add "nonnull" and
                                       "warn_unused_result"
                                       attributes.
  (pgp_packet_decrypt, init_gnutls_global): - '' -
  (init_gnutls_session start_mandos_communication, get_flags): - '' -
  (good_flags, good_interface, interface_is_up): - '' -
  (interface_is_running, runnable_hook): - '' -
  (avahi_loop_with_timeout, bring_up_interface): : - '' -
  (safer_gnutls_strerror): Add "warn_unused_result" attribute.
  (notdotentries): Set "nonnull", "pure", and "warn_unused_result"
                   attributes.
  (raise_privileges, raise_privileges_permanently, lower_privileges,
  lower_privileges_permanently): Set "warn_unused_result" attribute.
  (run_network_hooks): Exit child process if it fails to do anything
                       it needs to do.  Make explicit cast to double
                       when passing float value to asprintf().  Change
                       return type to void - all callers changed.
  (bring_up_interface): Move variables "sd", "ret_errno", and
                        "ret_setflags" to innermost scope.  Bug fix:
                        Fail if could not get interface flags also in
                        non-debug mode, and restore old errno
                        correctly.  Print message if could not raise
                        (or later lower) privileges.
  (take_down_interface): Bug fix: When failing because it could not
                         get interface flags, restore old errno
                         correctly.  Print message if it could not
                         raise (or later lower) privileges.
  (main): Complain if failed to raise or lower privileges.  Only run
          network hooks or lower privileges if raising privileges was
          successful.

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 */
 
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>          /* DIR, fdopendir(), fstat(), struct
 
32
                                   stderr, STDOUT_FILENO */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
41
                                   WEXITSTATUS(), WTERMSIG(),
42
42
                                   WCOREDUMP() */
43
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
44
#include <iso646.h>             /* and, or, not */
45
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
46
                                   readdir(), closedir(), dirfd() */
47
 
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
48
 
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
49
 
                                   struct stat, fstat(), close(),
50
 
                                   setgid(), setuid(), S_ISREG(),
51
 
                                   faccessat() pipe(), fork(),
52
 
                                   _exit(), dup2(), fexecve(), read()
53
 
                                */
 
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() */
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
 
                                   FD_CLOEXEC, openat() */
56
 
#include <string.h>             /* strsep, strlen(), strsignal(),
57
 
                                   strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
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,
72
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
73
73
#include <errno.h>              /* errno */
74
74
#include <error.h>              /* error() */
75
 
#include <fnmatch.h>            /* fnmatch() */
76
75
 
77
76
#define BUFFER_SIZE 256
78
77
 
348
347
  char *plugindir = NULL;
349
348
  char *argfile = NULL;
350
349
  FILE *conffp;
 
350
  size_t d_name_len;
351
351
  DIR *dir = NULL;
352
352
  struct dirent *dirst;
353
353
  struct stat st;
363
363
                                      .sa_flags = SA_NOCLDSTOP };
364
364
  char **custom_argv = NULL;
365
365
  int custom_argc = 0;
366
 
  int dir_fd;
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;
862
870
      break;
863
871
    }
864
872
    
 
873
    d_name_len = strlen(dirst->d_name);
 
874
    
865
875
    /* Ignore dotfiles, backup files and other junk */
866
876
    {
867
877
      bool bad_name = false;
868
 
      const char * const patterns[] = { ".*", "#*#", "*~",
869
 
                                        "*.dpkg-new", "*.dpkg-old",
870
 
                                        "*.dpkg-bak", "*.dpkg-divert",
871
 
                                        NULL };
872
 
#ifdef __GNUC__
873
 
#pragma GCC diagnostic push
874
 
#pragma GCC diagnostic ignored "-Wcast-qual"
875
 
#endif
876
 
      for(const char **pat = (const char **)patterns;
877
 
          *pat != NULL; pat++){
878
 
#ifdef __GNUC__
879
 
#pragma GCC diagnostic pop
880
 
#endif
881
 
        if(fnmatch(*pat, dirst->d_name,
882
 
                   FNM_FILE_NAME | FNM_PERIOD) != FNM_NOMATCH){
883
 
          if(debug){
884
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
885
 
                    " matching pattern %s\n", dirst->d_name, *pat);
886
 
          }
887
 
          bad_name = true;
888
 
          break;
889
 
        }
890
 
      }
 
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
      
891
930
      if(bad_name){
892
931
        continue;
893
932
      }
894
933
    }
895
934
    
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
 
                            );
903
 
    if(plugin_fd == -1){
904
 
      error(0, errno, "Could not open plugin");
905
 
      continue;
 
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));
906
943
    }
907
 
#ifndef O_CLOEXEC
908
 
  /* Set the FD_CLOEXEC flag on the plugin FD */
909
 
    ret = set_cloexec_flag(plugin_fd);
910
944
    if(ret < 0){
911
 
      error(0, errno, "set_cloexec_flag");
912
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
945
      error(0, errno, "asprintf");
913
946
      continue;
914
947
    }
915
 
#endif  /* O_CLOEXEC */
916
 
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
 
948
    
 
949
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
917
950
    if(ret == -1){
918
951
      error(0, errno, "stat");
919
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
952
      free(filename);
920
953
      continue;
921
954
    }
922
955
    
923
956
    /* Ignore non-executable files */
924
957
    if(not S_ISREG(st.st_mode)
925
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, dirst->d_name, X_OK,
926
 
                                        0)) != 0)){
 
958
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
927
959
      if(debug){
928
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
929
 
                " with bad type or mode\n",
930
 
                plugindir != NULL ? plugindir : PDIR, dirst->d_name);
 
960
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
961
                " with bad type or mode\n", filename);
931
962
      }
932
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
963
      free(filename);
933
964
      continue;
934
965
    }
935
966
    
936
967
    plugin *p = getplugin(dirst->d_name);
937
968
    if(p == NULL){
938
969
      error(0, errno, "getplugin");
939
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
970
      free(filename);
940
971
      continue;
941
972
    }
942
973
    if(p->disabled){
944
975
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
945
976
                dirst->d_name);
946
977
      }
947
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
978
      free(filename);
948
979
      continue;
949
980
    }
950
981
    {
964
995
        }
965
996
      }
966
997
    }
967
 
    /* If this plugin has any environment variables, we need to
968
 
       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. */
969
1001
    if(p->environ[0] != NULL){
970
1002
      for(char **e = environ; *e != NULL; e++){
971
1003
        if(not add_environment(p, *e, false)){
1037
1069
           above and must now close it manually here. */
1038
1070
        closedir(dir);
1039
1071
      }
1040
 
      if(fexecve(plugin_fd, p->argv,
1041
 
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1042
 
        error(0, errno, "fexecve for %s/%s",
1043
 
              plugindir != NULL ? plugindir : PDIR, dirst->d_name);
1044
 
        _exit(EX_OSERR);
 
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
        }
1045
1082
      }
1046
1083
      /* no return */
1047
1084
    }
1048
1085
    /* Parent process */
1049
1086
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1050
1087
                                             pipe */
1051
 
    TEMP_FAILURE_RETRY(close(plugin_fd));
 
1088
    free(filename);
1052
1089
    plugin *new_plugin = getplugin(dirst->d_name);
1053
1090
    if(new_plugin == NULL){
1054
1091
      error(0, errno, "getplugin");