17 #include <sys/types.h>
26 static int is_magic_value(
char *p);
27 static int check_md5_hash(
char *hash,
char *value);
28 static void add_secret_params(gpointer key, gpointer value, gpointer user_data);
29 static char *read_local_file(
char *local_file);
31 #define MAX_VALUE_LEN 255
32 #define MAGIC "lrm://"
35 is_magic_value(
char *p)
37 return !strcmp(p,
MAGIC);
41 check_md5_hash(
char *hash,
char *value)
47 crm_debug(
"hash: %s, calculated hash: %s", hash, hash2);
57 read_local_file(
char *local_file)
59 FILE *fp = fopen(local_file,
"r");
64 if (errno != ENOENT) {
65 crm_perror(LOG_ERR,
"cannot open %s" , local_file);
71 crm_perror(LOG_ERR,
"cannot read %s", local_file);
78 for (p = buf+strlen(buf)-1; p >= buf && isspace(*p); p--)
92 char local_file[FILENAME_MAX+1], *start_pname;
93 char hash_file[FILENAME_MAX+1], *hash;
94 GList *secret_params = NULL, *l;
95 char *key, *pvalue, *secret_value;
106 g_hash_table_foreach(params, add_secret_params, &secret_params);
107 if (!secret_params) {
111 crm_debug(
"replace secret parameters for resource %s", rsc_id);
113 if (snprintf(local_file, FILENAME_MAX, LRM_CIBSECRETS_DIR
"/%s/", rsc_id)
115 crm_err(
"filename size exceeded for resource %s", rsc_id);
118 start_pname = local_file + strlen(local_file);
120 for (l = g_list_first(secret_params); l; l = g_list_next(l)) {
121 key = (
char *)(l->data);
122 pvalue = g_hash_table_lookup(params, key);
124 crm_err(
"odd, no parameter %s for rsc %s found now", key, rsc_id);
128 if ((strlen(key) + strlen(local_file)) >= FILENAME_MAX-2) {
129 crm_err(
"%d: parameter name %s too big", key);
134 strcpy(start_pname, key);
135 secret_value = read_local_file(local_file);
137 crm_err(
"secret for rsc %s parameter %s not found in %s",
138 rsc_id, key, LRM_CIBSECRETS_DIR);
143 strcpy(hash_file, local_file);
144 if (strlen(hash_file) + 5 > FILENAME_MAX) {
145 crm_err(
"cannot build such a long name "
146 "for the sign file: %s.sign", hash_file);
152 strncat(hash_file,
".sign", 5);
153 hash = read_local_file(hash_file);
155 crm_err(
"md5 sum for rsc %s parameter %s "
156 "cannot be read from %s", rsc_id, key, hash_file);
161 }
else if (!check_md5_hash(hash, secret_value)) {
162 crm_err(
"md5 sum for rsc %s parameter %s "
163 "does not match", rsc_id, key);
171 g_hash_table_replace(params, strdup(key), secret_value);
173 g_list_free(secret_params);
178 add_secret_params(gpointer key, gpointer value, gpointer user_data)
180 GList **lp = (GList **)user_data;
182 if (is_magic_value((
char *)value)) {
183 *lp = g_list_append(*lp, (
char *)key);
#define crm_debug(fmt, args...)
int replace_secret_params(const char *rsc_id, GHashTable *params)
#define crm_perror(level, fmt, args...)
Log a system error message.
#define crm_err(fmt, args...)
char * crm_md5sum(const char *buffer)
#define safe_str_eq(a, b)