30 #include <sys/types.h> 39 static int do_replace_secret_params(
char *rsc_id, GHashTable *params, gboolean from_legacy_dir);
40 static int is_magic_value(
char *p);
41 static int check_md5_hash(
char *hash,
char *value);
42 static void add_secret_params(gpointer key, gpointer value, gpointer user_data);
43 static char *read_local_file(
char *local_file);
45 #define MAX_VALUE_LEN 255 46 #define MAGIC "lrm://" 49 is_magic_value(
char *p)
51 return !strcmp(p,
MAGIC);
55 check_md5_hash(
char *hash,
char *value)
61 crm_debug(
"hash: %s, calculated hash: %s", hash, hash2);
71 read_local_file(
char *local_file)
73 FILE *fp = fopen(local_file,
"r");
78 if (errno != ENOENT) {
79 crm_perror(LOG_ERR,
"cannot open %s" , local_file);
85 crm_perror(LOG_ERR,
"cannot read %s", local_file);
90 for (p = buf+strlen(buf)-1; p >= buf && isspace(*p); p--)
104 if (do_replace_secret_params(rsc_id, params, FALSE) < 0
105 && do_replace_secret_params(rsc_id, params, TRUE) < 0) {
113 do_replace_secret_params(
char *rsc_id, GHashTable *params, gboolean from_legacy_dir)
115 char local_file[FILENAME_MAX+1], *start_pname;
116 char hash_file[FILENAME_MAX+1], *hash;
117 GList *secret_params = NULL, *l;
118 char *key, *pvalue, *secret_value;
120 const char *dir_prefix = NULL;
122 if (params == NULL) {
126 if (from_legacy_dir) {
127 dir_prefix = LRM_LEGACY_CIBSECRETS_DIR;
130 dir_prefix = LRM_CIBSECRETS_DIR;
137 g_hash_table_foreach(params, add_secret_params, &secret_params);
138 if (!secret_params) {
142 crm_debug(
"replace secret parameters for resource %s", rsc_id);
144 if (snprintf(local_file, FILENAME_MAX,
145 "%s/%s/", dir_prefix, rsc_id) > FILENAME_MAX) {
146 crm_err(
"filename size exceeded for resource %s", rsc_id);
149 start_pname = local_file + strlen(local_file);
151 for (l = g_list_first(secret_params); l; l = g_list_next(l)) {
152 key = (
char *)(l->data);
153 pvalue = g_hash_table_lookup(params, key);
155 crm_err(
"odd, no parameter %s for rsc %s found now", key, rsc_id);
159 if ((strlen(key) + strlen(local_file)) >= FILENAME_MAX-2) {
160 crm_err(
"%d: parameter name %s too big", key);
165 strcpy(start_pname, key);
166 secret_value = read_local_file(local_file);
168 if (from_legacy_dir == FALSE) {
169 crm_debug(
"secret for rsc %s parameter %s not found in %s. " 170 "will try "LRM_LEGACY_CIBSECRETS_DIR, rsc_id, key, dir_prefix);
173 crm_err(
"secret for rsc %s parameter %s not found in %s",
174 rsc_id, key, dir_prefix);
180 strcpy(hash_file, local_file);
181 if (strlen(hash_file) + 5 > FILENAME_MAX) {
182 crm_err(
"cannot build such a long name " 183 "for the sign file: %s.sign", hash_file);
189 strncat(hash_file,
".sign", 5);
190 hash = read_local_file(hash_file);
192 crm_err(
"md5 sum for rsc %s parameter %s " 193 "cannot be read from %s", rsc_id, key, hash_file);
198 }
else if (!check_md5_hash(hash, secret_value)) {
199 crm_err(
"md5 sum for rsc %s parameter %s " 200 "does not match", rsc_id, key);
208 g_hash_table_replace(params, strdup(key), secret_value);
210 g_list_free(secret_params);
215 add_secret_params(gpointer key, gpointer value, gpointer user_data)
217 GList **lp = (GList **)user_data;
219 if (is_magic_value((
char *)value)) {
220 *lp = g_list_append(*lp, (
char *)key);
#define crm_debug(fmt, args...)
int replace_secret_params(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)