swanctl: Fail loading a connection if loading a cacert constraint fails

This commit is contained in:
Martin Willi
2014-12-12 10:23:59 +01:00
parent 6855b8b36b
commit 108e388580
+37 -10
View File
@@ -93,11 +93,12 @@ static void add_list_key(vici_req_t *req, char *key, char *value)
/** /**
* Add a vici list of blobs from a comma separated file list * Add a vici list of blobs from a comma separated file list
*/ */
static void add_file_list_key(vici_req_t *req, char *key, char *value) static bool add_file_list_key(vici_req_t *req, char *key, char *value)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
chunk_t *map; chunk_t *map;
char *token, buf[PATH_MAX]; char *token, buf[PATH_MAX];
bool ret = TRUE;
vici_begin_list(req, key); vici_begin_list(req, key);
enumerator = enumerator_create_token(value, ",", " "); enumerator = enumerator_create_token(value, ",", " ");
@@ -127,21 +128,26 @@ static void add_file_list_key(vici_req_t *req, char *key, char *value)
} }
else else
{ {
fprintf(stderr, "loading certificate '%s' failed: %s\n", fprintf(stderr, "loading %s certificate '%s' failed: %s\n",
token, strerror(errno)); key, token, strerror(errno));
ret = FALSE;
break;
} }
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
vici_end_list(req); vici_end_list(req);
return ret;
} }
/** /**
* Translate setting key/values from a section into vici key-values/lists * Translate setting key/values from a section into vici key-values/lists
*/ */
static void add_key_values(vici_req_t *req, settings_t *cfg, char *section) static bool add_key_values(vici_req_t *req, settings_t *cfg, char *section)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
char *key, *value; char *key, *value;
bool ret = TRUE;
enumerator = cfg->create_key_value_enumerator(cfg, section); enumerator = cfg->create_key_value_enumerator(cfg, section);
while (enumerator->enumerate(enumerator, &key, &value)) while (enumerator->enumerate(enumerator, &key, &value))
@@ -152,34 +158,51 @@ static void add_key_values(vici_req_t *req, settings_t *cfg, char *section)
} }
else if (is_file_list_key(key)) else if (is_file_list_key(key))
{ {
add_file_list_key(req, key, value); ret = add_file_list_key(req, key, value);
} }
else else
{ {
vici_add_key_valuef(req, key, "%s", value); vici_add_key_valuef(req, key, "%s", value);
} }
if (!ret)
{
break;
}
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
return ret;
} }
/** /**
* Translate a settings section to a vici section * Translate a settings section to a vici section
*/ */
static void add_sections(vici_req_t *req, settings_t *cfg, char *section) static bool add_sections(vici_req_t *req, settings_t *cfg, char *section)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
char *name, buf[256]; char *name, buf[256];
bool ret = TRUE;
enumerator = cfg->create_section_enumerator(cfg, section); enumerator = cfg->create_section_enumerator(cfg, section);
while (enumerator->enumerate(enumerator, &name)) while (enumerator->enumerate(enumerator, &name))
{ {
vici_begin_section(req, name); vici_begin_section(req, name);
snprintf(buf, sizeof(buf), "%s.%s", section, name); snprintf(buf, sizeof(buf), "%s.%s", section, name);
add_key_values(req, cfg, buf); ret = add_key_values(req, cfg, buf);
add_sections(req, cfg, buf); if (!ret)
{
break;
}
ret = add_sections(req, cfg, buf);
if (!ret)
{
break;
}
vici_end_section(req); vici_end_section(req);
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
return ret;
} }
/** /**
@@ -198,8 +221,12 @@ static bool load_conn(vici_conn_t *conn, settings_t *cfg,
req = vici_begin("load-conn"); req = vici_begin("load-conn");
vici_begin_section(req, section); vici_begin_section(req, section);
add_key_values(req, cfg, buf); if (!add_key_values(req, cfg, buf) ||
add_sections(req, cfg, buf); !add_sections(req, cfg, buf))
{
vici_free_req(req);
return FALSE;
}
vici_end_section(req); vici_end_section(req);
res = vici_submit(req, conn); res = vici_submit(req, conn);