Code

844ceea19cade597f04bf1cdd28276c788b2b31b
[pkg-collectd.git] / debian / patches / bts833013-gcry-init.dpatch
1 #! /bin/sh /usr/share/dpatch/dpatch-run
2 ## bts833013-gcry-init.dpatch by Florian Forster <octo@collectd.org>
3 ## Backported to 5.1.0 by Sebastian Harl <tokkee@debian.org>
4 ## Rebased on top of bts832577-gcry-control.dpatch
5 ##
6 ## DP: Make sure gcrypt is initialized before using any of its functions.
7 ## DP:
8 ## DP: @marekbecka found that gcrypt functionality is called during the
9 ## DP: configuration phase, but the library is only initialized later during
10 ## DP: the initialization phase.
11 ## DP:
12 ## DP: Upstream commit:
13 ## DP: https://github.com/collectd/collectd/commit/0ec776a
14 ## DP: Upstream report:
15 ## DP: https://github.com/collectd/collectd/issues/273
17 @DPATCH@
19 diff a/src/network.c b/src/network.c
20 --- a/src/network.c
21 +++ b/src/network.c
22 @@ -476,6 +476,28 @@
23  } /* }}} int network_dispatch_notification */
24  
25  #if HAVE_LIBGCRYPT
26 +static void network_init_gcrypt (void) /* {{{ */
27 +{
28 +       gcry_error_t err;
29 +
30 +       if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
31 +               return;
32 +
33 +       err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
34 +       if (err)
35 +       {
36 +               ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err));
37 +               return (-1);
38 +       }
39 +       err = gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
40 +       if (err)
41 +       {
42 +               ERROR ("network plugin: gcry_control (GCRYCTL_INIT_SECMEM) failed: %s", gcry_strerror (err));
43 +               return (-1);
44 +       }
45 +       gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
46 +} /* }}} void network_init_gcrypt */
47 +
48  static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
49      const void *iv, size_t iv_size, const char *username)
50  {
51 @@ -2011,6 +2033,8 @@
52         {
53                 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
54                 {
55 +                       network_init_gcrypt ();
56 +
57                         if ((se->data.client.username == NULL)
58                                         || (se->data.client.password == NULL))
59                         {
60 @@ -2029,6 +2053,8 @@
61         {
62                 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
63                 {
64 +                       network_init_gcrypt ();
65 +
66                         if (se->data.server.auth_file == NULL)
67                         {
68                                 ERROR ("network plugin: Server socket with "
69 @@ -3345,7 +3371,6 @@
70  static int network_init (void)
71  {
72         static _Bool have_init = 0;
73 -       gcry_error_t err;
74  
75         /* Check if we were already initialized. If so, just return - there's
76          * nothing more to do (for now, that is). */
77 @@ -3354,19 +3379,7 @@
78         have_init = 1;
79  
80  #if HAVE_LIBGCRYPT
81 -       err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
82 -       if (err)
83 -       {
84 -               ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err));
85 -               return (-1);
86 -       }
87 -       err = gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
88 -       if (err)
89 -       {
90 -               ERROR ("network plugin: gcry_control (GCRYCTL_INIT_SECMEM) failed: %s", gcry_strerror (err));
91 -               return (-1);
92 -       }
93 -       gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
94 +       network_init_gcrypt ();
95  #endif
96  
97         if (network_config_stats != 0)