summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/irmd/configfile.c109
-rw-r--r--src/tools/irm/irm_name_create.c95
2 files changed, 73 insertions, 131 deletions
diff --git a/src/irmd/configfile.c b/src/irmd/configfile.c
index 229a3fd3..9b0fa2f3 100644
--- a/src/irmd/configfile.c
+++ b/src/irmd/configfile.c
@@ -830,6 +830,37 @@ static int toml_prog_list(toml_array_t * progs,
return ret;
}
+static int cp_chk_path(char * buf,
+ char * path)
+{
+ char * rp;
+
+ assert(path != NULL);
+
+ rp = realpath(path, NULL);
+ if (rp == NULL) {
+ log_err("Failed to check path %s: %s.", path, strerror(errno));
+ goto fail_rp;
+ }
+
+ if (strlen(rp) > NAME_PATH_SIZE) {
+ log_err("File path too long: %s.", rp);
+ goto fail_len;
+ }
+
+ strcpy(buf, rp);
+ free(rp);
+ free(path);
+
+ return 0;
+
+ fail_len:
+ free(rp);
+ fail_rp:
+ free(path);
+ return -1;
+}
+
static int toml_name(toml_table_t * table,
const char * name)
{
@@ -871,82 +902,20 @@ static int toml_name(toml_table_t * table,
}
scrt = toml_string_in(table, "server_crt_file");
- if (scrt.ok) {
- char * scrt_path = realpath(scrt.u.s, NULL);
- if (scrt_path == NULL) {
- log_err("Failed to check path for %s: %s.",
- scrt.u.s, strerror(errno));
- free(scrt.u.s);
- return -1;
- }
- if (strlen(scrt.u.s) > NAME_PATH_SIZE) {
- log_err("Server certificate file path too long: %s",
- scrt_path);
- free(scrt.u.s);
- return -1;
- }
- strcpy(info.s.crt, scrt_path);
- free(scrt_path);
- free(scrt.u.s);
- }
+ if (scrt.ok && cp_chk_path(info.s.crt, scrt.u.s) < 0)
+ return -1;
skey = toml_string_in(table, "server_key_file");
- if (skey.ok) {
- char * skey_path = realpath(skey.u.s, NULL);
- if (skey_path == NULL) {
- log_err("Failed to check path for %s: %s.",
- skey.u.s, strerror(errno));
- free(skey.u.s);
- return -1;
- }
- if (strlen(skey.u.s) > NAME_PATH_SIZE) {
- log_err("Server key file path too long: %s", skey_path);
- free(skey.u.s);
- return -1;
- }
- strcpy(info.s.key, skey_path);
- free(skey_path);
- free(skey.u.s);
- }
+ if (skey.ok && cp_chk_path(info.s.key, skey.u.s) < 0)
+ return -1;
ccrt = toml_string_in(table, "client_crt_file");
- if (ccrt.ok) {
- char * ccrt_path = realpath(ccrt.u.s, NULL);
- if (ccrt_path == NULL) {
- log_err("Failed to check path for %s: %s.",
- ccrt.u.s, strerror(errno));
- free(ccrt.u.s);
- return -1;
- }
- if (strlen(ccrt.u.s) > NAME_PATH_SIZE) {
- log_err("Client certificate file path too long: %s",
- ccrt_path);
- free(ccrt.u.s);
- return -1;
- }
- strcpy(info.c.crt, ccrt_path);
- free(ccrt_path);
- free(ccrt.u.s);
- }
+ if (ccrt.ok && cp_chk_path(info.c.crt, ccrt.u.s) < 0)
+ return -1;
ckey = toml_string_in(table, "client_key_file");
- if (ckey.ok) {
- char * ckey_path = realpath(ckey.u.s, NULL);
- if (ckey_path == NULL) {
- log_err("Failed to check path for %s: %s.",
- ckey.u.s, strerror(errno));
- free(ckey.u.s);
- return -1;
- }
- if (strlen(ckey.u.s) > NAME_PATH_SIZE) {
- log_err("Client key file path too long: %s", ckey_path);
- free(ckey.u.s);
- return -1;
- }
- strcpy(info.c.key, ckey_path);
- free(ckey_path);
- free(ckey.u.s);
- }
+ if (ckey.ok && cp_chk_path(info.c.key, ckey.u.s) < 0)
+ return -1;
if (name_create(&info) < 0) {
log_err("Failed to create name %s.", name);
diff --git a/src/tools/irm/irm_name_create.c b/src/tools/irm/irm_name_create.c
index 04d7f95f..f363ac3e 100644
--- a/src/tools/irm/irm_name_create.c
+++ b/src/tools/irm/irm_name_create.c
@@ -70,6 +70,32 @@ static void usage(void)
NAME_SIZE, RR);
}
+static int cp_chk_path(char * buf,
+ const char * path)
+{
+ char * rp = realpath(path, NULL);
+ if (rp == NULL) {
+ printf("Failed to check path %s: %s\n.",
+ path, strerror(errno));
+ goto fail_rp;
+ }
+
+ if (strlen(rp) > NAME_PATH_SIZE) {
+ printf("File path too long: %s.\n", rp);
+ goto fail_len;
+ }
+
+ strcpy(buf, rp);
+ free(rp);
+
+ return 0;
+
+ fail_len:
+ free(rp);
+ fail_rp:
+ return -1;
+}
+
int do_create_name(int argc,
char ** argv)
{
@@ -115,70 +141,17 @@ int do_create_name(int argc,
strcpy(info.name, name);
- if (scrtpath != NULL) {
- scrtpath = realpath(scrtpath, NULL);
- if (scrtpath == NULL) {
- printf("Failed to resolve server crt path: %s.\n",
- strerror(errno));
- goto fail;
- }
- if (strlen(scrtpath) > NAME_PATH_SIZE) {
- printf("Server crt path > %d chars.", NAME_PATH_SIZE);
- free(scrtpath);
- goto fail;
- }
- strcpy(info.s.crt, scrtpath);
- free(scrtpath);
- }
-
- if (skeypath != NULL) {
- skeypath = realpath(skeypath, NULL);
- if (skeypath == NULL) {
- printf("Failed to resolve server key path: %s.\n",
- strerror(errno));
- goto fail;
- }
- if (strlen(skeypath) > NAME_PATH_SIZE) {
- printf("Server key path > %d chars.", NAME_PATH_SIZE);
- free(skeypath);
- goto fail;
- }
- strcpy(info.s.key, skeypath);
- free(skeypath);
- }
+ if (scrtpath != NULL && cp_chk_path(info.s.crt, scrtpath) < 0)
+ goto fail;
- if (ccrtpath != NULL) {
- ccrtpath = realpath(ccrtpath, NULL);
- if (ccrtpath == NULL) {
- printf("Failed to resolve client crt path: %s.\n",
- strerror(errno));
- goto fail;
- }
- if (strlen(ccrtpath) > NAME_PATH_SIZE) {
- printf("Client crt path > %d chars.", NAME_PATH_SIZE);
- free(ccrtpath);
- goto fail;
- }
- strcpy(info.c.crt, ccrtpath);
- free(ccrtpath);
- }
+ if (skeypath != NULL && cp_chk_path(info.s.key, skeypath) < 0)
+ goto fail;
- if (ckeypath != NULL) {
- ckeypath = realpath(ckeypath, NULL);
- if (ckeypath == NULL) {
- printf("Failed to resolve client key path: %s.\n",
- strerror(errno));
- goto fail;
- }
+ if (ccrtpath != NULL && cp_chk_path(info.c.crt, ccrtpath) < 0)
+ goto fail;
- if (strlen(ckeypath) > NAME_PATH_SIZE) {
- printf("Client key path > %d chars.", NAME_PATH_SIZE);
- free(ckeypath);
- goto fail;
- }
- strcpy(info.c.key, ckeypath);
- free(ckeypath);
- }
+ if (ckeypath != NULL && cp_chk_path(info.c.key, ckeypath) < 0)
+ goto fail;
if (strcmp(lb_pol, RR) == 0)
info.pol_lb = LB_RR;