This source file includes following definitions.
- dump_xml_for_digest
- calculate_xml_digest_v1
- calculate_xml_digest_v2
- calculate_on_disk_digest
- calculate_operation_digest
- calculate_xml_versioned_digest
- crm_digest_verify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <crm_internal.h>
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <crm/crm.h>
27 #include <crm/msg_xml.h>
28 #include <crm/common/xml.h>
29
30 #define BEST_EFFORT_STATUS 0
31
32
33
34
35
36
37
38
39 static char *
40 dump_xml_for_digest(xmlNode * an_xml_node)
41 {
42 char *buffer = NULL;
43 int offset = 0, max = 0;
44
45
46 crm_buffer_add_char(&buffer, &offset, &max, ' ');
47 crm_xml_dump(an_xml_node, 0, &buffer, &offset, &max, 0);
48 crm_buffer_add_char(&buffer, &offset, &max, '\n');
49
50 return buffer;
51 }
52
53
54
55
56
57
58
59
60
61
62
63 static char *
64 calculate_xml_digest_v1(xmlNode * input, gboolean sort, gboolean ignored)
65 {
66 char *digest = NULL;
67 char *buffer = NULL;
68 xmlNode *copy = NULL;
69
70 if (sort) {
71 crm_trace("Sorting xml...");
72 copy = sorted_xml(input, NULL, TRUE);
73 crm_trace("Done");
74 input = copy;
75 }
76
77 buffer = dump_xml_for_digest(input);
78 CRM_CHECK(buffer != NULL && strlen(buffer) > 0, free_xml(copy);
79 free(buffer);
80 return NULL);
81
82 digest = crm_md5sum(buffer);
83 crm_log_xml_trace(input, "digest:source");
84
85 free(buffer);
86 free_xml(copy);
87 return digest;
88 }
89
90
91
92
93
94
95
96
97
98 static char *
99 calculate_xml_digest_v2(xmlNode * source, gboolean do_filter)
100 {
101 char *digest = NULL;
102 char *buffer = NULL;
103 int offset, max;
104
105 static struct qb_log_callsite *digest_cs = NULL;
106
107 crm_trace("Begin digest %s", do_filter?"filtered":"");
108 if (do_filter && BEST_EFFORT_STATUS) {
109
110
111
112
113
114
115
116
117
118
119
120
121 } else {
122 crm_xml_dump(source, do_filter ? xml_log_option_filtered : 0, &buffer, &offset, &max, 0);
123 }
124
125 CRM_ASSERT(buffer != NULL);
126 digest = crm_md5sum(buffer);
127
128 if (digest_cs == NULL) {
129 digest_cs = qb_log_callsite_get(__func__, __FILE__, "cib-digest", LOG_TRACE, __LINE__,
130 crm_trace_nonlog);
131 }
132 if (digest_cs && digest_cs->targets) {
133 char *trace_file = crm_concat("/tmp/digest", digest, '-');
134
135 crm_trace("Saving %s.%s.%s to %s",
136 crm_element_value(source, XML_ATTR_GENERATION_ADMIN),
137 crm_element_value(source, XML_ATTR_GENERATION),
138 crm_element_value(source, XML_ATTR_NUMUPDATES), trace_file);
139 save_xml_to_file(source, "digest input", trace_file);
140 free(trace_file);
141 }
142
143 free(buffer);
144 crm_trace("End digest");
145 return digest;
146 }
147
148
149
150
151
152
153
154
155 char *
156 calculate_on_disk_digest(xmlNode * input)
157 {
158
159
160
161
162
163 return calculate_xml_digest_v1(input, FALSE, FALSE);
164 }
165
166
167
168
169
170
171
172
173
174 char *
175 calculate_operation_digest(xmlNode *input, const char *version)
176 {
177
178 return calculate_xml_digest_v1(input, TRUE, FALSE);
179 }
180
181
182
183
184
185
186
187
188
189
190
191 char *
192 calculate_xml_versioned_digest(xmlNode * input, gboolean sort, gboolean do_filter,
193 const char *version)
194 {
195
196
197
198
199
200
201
202
203
204 if (version == NULL || compare_version("3.0.5", version) > 0) {
205 crm_trace("Using v1 digest algorithm for %s", crm_str(version));
206 return calculate_xml_digest_v1(input, sort, do_filter);
207 }
208 crm_trace("Using v2 digest algorithm for %s", crm_str(version));
209 return calculate_xml_digest_v2(input, do_filter);
210 }
211
212
213
214
215
216
217
218
219
220
221 gboolean
222 crm_digest_verify(xmlNode *input, const char *expected)
223 {
224 char *calculated = NULL;
225 gboolean passed;
226
227 if (input != NULL) {
228 calculated = calculate_on_disk_digest(input);
229 if (calculated == NULL) {
230 crm_perror(LOG_ERR, "Could not calculate digest for comparison");
231 return FALSE;
232 }
233 }
234 passed = safe_str_eq(expected, calculated);
235 if (passed) {
236 crm_trace("Digest comparison passed: %s", calculated);
237 } else {
238 crm_err("Digest comparison failed: expected %s, calculated %s",
239 expected, calculated);
240 }
241 free(calculated);
242 return passed;
243 }