Skip to content

Commit 2fd4bc0

Browse files
committed
Refactor: libcrmcommon: Functionize read/validate CIB secret hash
Reduces the scope of the hash variable and the need for a bunch of free statements in "continue" paths. Signed-off-by: Reid Wahl <[email protected]>
1 parent de0f03c commit 2fd4bc0

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

lib/common/cib_secrets.c

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@
2525

2626
#define MAX_VALUE_LEN 255
2727

28-
static bool
29-
check_md5_hash(char *hash, char *value)
30-
{
31-
bool rc = false;
32-
char *hash2 = NULL;
33-
34-
hash2 = crm_md5sum(value);
35-
crm_debug("hash: %s, calculated hash: %s", hash, hash2);
36-
if (pcmk__str_eq(hash, hash2, pcmk__str_casei)) {
37-
rc = true;
38-
}
39-
free(hash2);
40-
return rc;
41-
}
42-
4328
static char *
4429
read_local_file(char *local_file)
4530
{
@@ -64,7 +49,57 @@ read_local_file(char *local_file)
6449
// Strip trailing white space
6550
for (p = buf + strlen(buf) - 1; (p >= buf) && isspace(*p); p--);
6651
*(p+1) = '\0';
67-
return strdup(buf);
52+
return pcmk__str_copy(buf);
53+
}
54+
55+
/*!
56+
* \internal
57+
* \brief Read checksum from a file and compare against calculated checksum
58+
*
59+
* \param[in] filename File containing stored checksum
60+
* \param[in] secret_value String to calculate checksum from
61+
* \param[in] rsc_id Resource ID (for logging only)
62+
* \param[in] param Parameter name (for logging only)
63+
*
64+
* \return Standard Pacemaker return code
65+
*/
66+
static int
67+
validate_hash(const char *filename, const char *secret_value,
68+
const char *rsc_id, const char *param)
69+
{
70+
char *stored = NULL;
71+
char *calculated = NULL;
72+
int rc = pcmk_rc_ok;
73+
74+
stored = read_local_file(filename);
75+
if (stored == NULL) {
76+
crm_err("Could not read md5 sum for resource %s parameter '%s' from "
77+
"file '%s'",
78+
rsc_id, param, filename);
79+
rc = ENOENT;
80+
goto done;
81+
}
82+
83+
calculated = crm_md5sum(secret_value);
84+
if (calculated == NULL) {
85+
// Should be impossible
86+
rc = EINVAL;
87+
goto done;
88+
}
89+
90+
crm_trace("Stored hash: %s, calculated hash: %s", stored, calculated);
91+
92+
if (!pcmk__str_eq(stored, calculated, pcmk__str_casei)) {
93+
crm_err("Calculated md5 sum for resource %s parameter '%s' does not "
94+
"match stored md5 sum",
95+
rsc_id, param);
96+
rc = pcmk_rc_cib_corrupt;
97+
}
98+
99+
done:
100+
free(stored);
101+
free(calculated);
102+
return rc;
68103
}
69104

70105
/*!
@@ -99,7 +134,7 @@ pcmk__substitute_secrets(const char *rsc_id, GHashTable *params)
99134
while (g_hash_table_iter_next(&iter, (gpointer *) &param,
100135
(gpointer *) &value)) {
101136
char *secret_value = NULL;
102-
char *hash = NULL;
137+
int hash_rc = pcmk_rc_ok;
103138

104139
if (!pcmk__str_eq(value, "lrm://", pcmk__str_none)) {
105140
// Not a secret parameter
@@ -133,27 +168,13 @@ pcmk__substitute_secrets(const char *rsc_id, GHashTable *params)
133168

134169
// Path to file containing md5 sum for this parameter
135170
g_string_append(filename, ".sign");
136-
hash = read_local_file(filename->str);
137-
if (hash == NULL) {
138-
crm_err("Could not read md5 sum for resource %s parameter '%s' "
139-
"from file '%s'",
140-
rsc_id, param, filename->str);
141-
free(secret_value);
142-
rc = ENOENT;
143-
continue;
144-
}
145-
146-
if (!check_md5_hash(hash, secret_value)) {
147-
crm_err("Calculated md5 sum for resource %s parameter '%s' does "
148-
"not match stored md5 sum",
149-
rsc_id, param);
171+
hash_rc = validate_hash(filename->str, secret_value, rsc_id, param);
172+
if (hash_rc != pcmk_rc_ok) {
173+
rc = hash_rc;
150174
free(secret_value);
151-
free(hash);
152-
rc = pcmk_rc_cib_corrupt;
153175
continue;
154176
}
155177

156-
free(hash);
157178
g_hash_table_iter_replace(&iter, (gpointer) secret_value);
158179
}
159180

0 commit comments

Comments
 (0)