pacemaker  2.1.5-b7adf64e51
Scalable High-Availability cluster resource manager
digest.c
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2022 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <md5.h>
17 
18 #include <crm/crm.h>
19 #include <crm/msg_xml.h>
20 #include <crm/common/xml.h>
21 #include "crmcommon_private.h"
22 
23 #define BEST_EFFORT_STATUS 0
24 
33 static GString *
34 dump_xml_for_digest(xmlNodePtr xml)
35 {
36  GString *buffer = g_string_sized_new(1024);
37 
38  /* for compatibility with the old result which is used for v1 digests */
39  g_string_append_c(buffer, ' ');
40  pcmk__xml2text(xml, 0, buffer, 0);
41  g_string_append_c(buffer, '\n');
42 
43  return buffer;
44 }
45 
56 static char *
57 calculate_xml_digest_v1(xmlNode *input, gboolean sort, gboolean ignored)
58 {
59  char *digest = NULL;
60  GString *buffer = NULL;
61  xmlNode *copy = NULL;
62 
63  if (sort) {
64  crm_trace("Sorting xml...");
65  copy = sorted_xml(input, NULL, TRUE);
66  crm_trace("Done");
67  input = copy;
68  }
69 
70  buffer = dump_xml_for_digest(input);
71  CRM_CHECK(buffer->len > 0, free_xml(copy);
72  g_string_free(buffer, TRUE);
73  return NULL);
74 
75  digest = crm_md5sum((const char *) buffer->str);
76  crm_log_xml_trace(input, "digest:source");
77 
78  g_string_free(buffer, TRUE);
79  free_xml(copy);
80  return digest;
81 }
82 
91 static char *
92 calculate_xml_digest_v2(xmlNode *source, gboolean do_filter)
93 {
94  char *digest = NULL;
95  GString *buffer = g_string_sized_new(1024);
96 
97  static struct qb_log_callsite *digest_cs = NULL;
98 
99  crm_trace("Begin digest %s", do_filter?"filtered":"");
100  pcmk__xml2text(source, (do_filter? xml_log_option_filtered : 0), buffer, 0);
101 
102  CRM_ASSERT(buffer != NULL);
103  digest = crm_md5sum((const char *) buffer->str);
104 
105  if (digest_cs == NULL) {
106  digest_cs = qb_log_callsite_get(__func__, __FILE__, "cib-digest", LOG_TRACE, __LINE__,
108  }
109  if (digest_cs && digest_cs->targets) {
110  char *trace_file = crm_strdup_printf("%s/digest-%s",
111  pcmk__get_tmpdir(), digest);
112 
113  crm_trace("Saving %s.%s.%s to %s",
116  crm_element_value(source, XML_ATTR_NUMUPDATES), trace_file);
117  save_xml_to_file(source, "digest input", trace_file);
118  free(trace_file);
119  }
120 
121  g_string_free(buffer, TRUE);
122  crm_trace("End digest");
123  return digest;
124 }
125 
133 char *
135 {
136  /* Always use the v1 format for on-disk digests
137  * a) it's a compatibility nightmare
138  * b) we only use this once at startup, all other
139  * invocations are in a separate child process
140  */
141  return calculate_xml_digest_v1(input, FALSE, FALSE);
142 }
143 
152 char *
154 {
155  /* We still need the sorting for operation digests */
156  return calculate_xml_digest_v1(input, TRUE, FALSE);
157 }
158 
169 char *
170 calculate_xml_versioned_digest(xmlNode *input, gboolean sort,
171  gboolean do_filter, const char *version)
172 {
173  /*
174  * @COMPAT digests (on-disk or in diffs/patchsets) created <1.1.4;
175  * removing this affects even full-restart upgrades from old versions
176  *
177  * The sorting associated with v1 digest creation accounted for 23% of
178  * the CIB manager's CPU usage on the server. v2 drops this.
179  *
180  * The filtering accounts for an additional 2.5% and we may want to
181  * remove it in future.
182  *
183  * v2 also uses the xmlBuffer contents directly to avoid additional copying
184  */
185  if (version == NULL || compare_version("3.0.5", version) > 0) {
186  crm_trace("Using v1 digest algorithm for %s",
187  pcmk__s(version, "unknown feature set"));
188  return calculate_xml_digest_v1(input, sort, do_filter);
189  }
190  crm_trace("Using v2 digest algorithm for %s",
191  pcmk__s(version, "unknown feature set"));
192  return calculate_xml_digest_v2(input, do_filter);
193 }
194 
204 bool
205 pcmk__verify_digest(xmlNode *input, const char *expected)
206 {
207  char *calculated = NULL;
208  bool passed;
209 
210  if (input != NULL) {
211  calculated = calculate_on_disk_digest(input);
212  if (calculated == NULL) {
213  crm_perror(LOG_ERR, "Could not calculate digest for comparison");
214  return false;
215  }
216  }
217  passed = pcmk__str_eq(expected, calculated, pcmk__str_casei);
218  if (passed) {
219  crm_trace("Digest comparison passed: %s", calculated);
220  } else {
221  crm_err("Digest comparison failed: expected %s, calculated %s",
222  expected, calculated);
223  }
224  free(calculated);
225  return passed;
226 }
227 
236 bool
238 {
239  static const char *filter[] = {
245  };
246 
247  for (int i = 0; i < PCMK__NELEM(filter); i++) {
248  if (strcmp(name, filter[i]) == 0) {
249  return true;
250  }
251  }
252  return false;
253 }
254 
255 char *
256 crm_md5sum(const char *buffer)
257 {
258  int lpc = 0, len = 0;
259  char *digest = NULL;
260  unsigned char raw_digest[MD5_DIGEST_SIZE];
261 
262  if (buffer == NULL) {
263  buffer = "";
264  }
265  len = strlen(buffer);
266 
267  crm_trace("Beginning digest of %d bytes", len);
268  digest = malloc(2 * MD5_DIGEST_SIZE + 1);
269  if (digest) {
270  md5_buffer(buffer, len, raw_digest);
271  for (lpc = 0; lpc < MD5_DIGEST_SIZE; lpc++) {
272  sprintf(digest + (2 * lpc), "%02x", raw_digest[lpc]);
273  }
274  digest[(2 * MD5_DIGEST_SIZE)] = 0;
275  crm_trace("Digest %s.", digest);
276 
277  } else {
278  crm_err("Could not create digest");
279  }
280  return digest;
281 }
#define LOG_TRACE
Definition: logging.h:37
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:227
#define XML_ATTR_UPDATE_ORIG
Definition: msg_xml.h:142
A dumping ground.
#define XML_ATTR_UPDATE_CLIENT
Definition: msg_xml.h:143
#define XML_ATTR_NUMUPDATES
Definition: msg_xml.h:127
const char * name
Definition: cib.c:24
char * calculate_on_disk_digest(xmlNode *input)
Calculate and return digest of XML tree, suitable for storing on disk.
Definition: digest.c:134
#define XML_ATTR_UPDATE_USER
Definition: msg_xml.h:144
unsigned int crm_trace_nonlog
Definition: logging.c:46
#define XML_ATTR_GENERATION
Definition: msg_xml.h:125
#define XML_ATTR_ORIGIN
Definition: msg_xml.h:129
G_GNUC_INTERNAL void pcmk__xml2text(xmlNodePtr data, int options, GString *buffer, int depth)
Definition: xml.c:1982
void * md5_buffer(const char *buffer, size_t len, void *resblock)
Definition: md5.c:227
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:517
#define crm_trace(fmt, args...)
Definition: logging.h:365
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
Wrappers for and extensions to libxml2.
#define PCMK__NELEM(a)
Definition: internal.h:41
bool pcmk__xa_filterable(const char *name)
Definition: digest.c:237
void free_xml(xmlNode *child)
Definition: xml.c:885
char * calculate_xml_versioned_digest(xmlNode *input, gboolean sort, gboolean do_filter, const char *version)
Calculate and return digest of XML tree.
Definition: digest.c:170
xmlNode * sorted_xml(xmlNode *input, xmlNode *parent, gboolean recursive)
Definition: xml.c:2898
const char * pcmk__get_tmpdir(void)
Definition: io.c:541
bool pcmk__verify_digest(xmlNode *input, const char *expected)
Definition: digest.c:205
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
Definition: logging.h:310
#define crm_err(fmt, args...)
Definition: logging.h:359
#define CRM_ASSERT(expr)
Definition: results.h:42
#define XML_CIB_ATTR_WRITTEN
Definition: msg_xml.h:131
xmlNode * input
int compare_version(const char *version1, const char *version2)
Definition: utils.c:189
#define XML_ATTR_GENERATION_ADMIN
Definition: msg_xml.h:126
char * calculate_operation_digest(xmlNode *input, const char *version)
Calculate and return digest of XML operation.
Definition: digest.c:153
void save_xml_to_file(xmlNode *xml, const char *desc, const char *filename)
Definition: xml.c:2163
#define MD5_DIGEST_SIZE
Definition: md5.h:30
#define crm_log_xml_trace(xml, text)
Definition: logging.h:373
char * crm_md5sum(const char *buffer)
Definition: digest.c:256
uint32_t version
Definition: remote.c:213