swanctl: Allow passing a custom config file for each --load* command
authorTobias Brunner <tobias@strongswan.org>
Mon, 28 May 2018 15:19:22 +0000 (17:19 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 11 Sep 2018 16:14:45 +0000 (18:14 +0200)
Mainly for debugging, but could also be used to e.g. use a separate file
for connections and secrets.

src/swanctl/commands/load_all.c
src/swanctl/commands/load_authorities.c
src/swanctl/commands/load_conns.c
src/swanctl/commands/load_creds.c
src/swanctl/commands/load_pools.c

index 0010ce1..26f043a 100644 (file)
@@ -31,8 +31,8 @@ static int load_all(vici_conn_t *conn)
        bool clear = FALSE, noprompt = FALSE;
        command_format_options_t format = COMMAND_FORMAT_NONE;
        settings_t *cfg;
+       char *arg, *file = SWANCTL_CONF;
        int ret = 0;
-       char *arg;
 
        while (TRUE)
        {
@@ -52,6 +52,9 @@ static int load_all(vici_conn_t *conn)
                        case 'r':
                                format |= COMMAND_FORMAT_RAW;
                                continue;
+                       case 'f':
+                               file = arg;
+                               continue;
                        case EOF:
                                break;
                        default:
@@ -60,10 +63,10 @@ static int load_all(vici_conn_t *conn)
                break;
        }
 
-       cfg = settings_create(SWANCTL_CONF);
+       cfg = settings_create(file);
        if (!cfg)
        {
-               fprintf(stderr, "parsing '%s' failed\n", SWANCTL_CONF);
+               fprintf(stderr, "parsing '%s' failed\n", file);
                return EINVAL;
        }
 
@@ -104,6 +107,7 @@ static void __attribute__ ((constructor))reg()
                        {"noprompt",    'n', 0, "do not prompt for passwords"},
                        {"raw",                 'r', 0, "dump raw response message"},
                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
+                       {"file",                'f', 1, "custom path to swanctl.conf"},
                }
        });
 }
index d82c0f9..61682a3 100644 (file)
@@ -310,7 +310,7 @@ static int load_authorities(vici_conn_t *conn)
 {
        command_format_options_t format = COMMAND_FORMAT_NONE;
        settings_t *cfg;
-       char *arg;
+       char *arg, *file = SWANCTL_CONF;
        int ret;
 
        while (TRUE)
@@ -325,6 +325,9 @@ static int load_authorities(vici_conn_t *conn)
                        case 'r':
                                format |= COMMAND_FORMAT_RAW;
                                continue;
+                       case 'f':
+                               file = arg;
+                               continue;
                        case EOF:
                                break;
                        default:
@@ -333,10 +336,10 @@ static int load_authorities(vici_conn_t *conn)
                break;
        }
 
-       cfg = settings_create(SWANCTL_CONF);
+       cfg = settings_create(file);
        if (!cfg)
        {
-               fprintf(stderr, "parsing '%s' failed\n", SWANCTL_CONF);
+               fprintf(stderr, "parsing '%s' failed\n", file);
                return EINVAL;
        }
 
@@ -360,6 +363,7 @@ static void __attribute__ ((constructor))reg()
                        {"help",                'h', 0, "show usage information"},
                        {"raw",                 'r', 0, "dump raw response message"},
                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
+                       {"file",                'f', 1, "custom path to swanctl.conf"},
                }
        });
 }
index 0518ef5..dad0394 100644 (file)
@@ -425,7 +425,7 @@ static int load_conns(vici_conn_t *conn)
 {
        command_format_options_t format = COMMAND_FORMAT_NONE;
        settings_t *cfg;
-       char *arg;
+       char *arg, *file = SWANCTL_CONF;
        int ret;
 
        while (TRUE)
@@ -440,6 +440,9 @@ static int load_conns(vici_conn_t *conn)
                        case 'r':
                                format |= COMMAND_FORMAT_RAW;
                                continue;
+                       case 'f':
+                               file = arg;
+                               continue;
                        case EOF:
                                break;
                        default:
@@ -448,10 +451,10 @@ static int load_conns(vici_conn_t *conn)
                break;
        }
 
-       cfg = settings_create(SWANCTL_CONF);
+       cfg = settings_create(file);
        if (!cfg)
        {
-               fprintf(stderr, "parsing '%s' failed\n", SWANCTL_CONF);
+               fprintf(stderr, "parsing '%s' failed\n", file);
                return EINVAL;
        }
 
@@ -474,6 +477,7 @@ static void __attribute__ ((constructor))reg()
                        {"help",                'h', 0, "show usage information"},
                        {"raw",                 'r', 0, "dump raw response message"},
                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
+                       {"file",                'f', 1, "custom path to swanctl.conf"},
                }
        });
 }
index 63a3bdc..a9e352f 100644 (file)
@@ -946,7 +946,7 @@ static int load_creds(vici_conn_t *conn)
        bool clear = FALSE, noprompt = FALSE;
        command_format_options_t format = COMMAND_FORMAT_NONE;
        settings_t *cfg;
-       char *arg;
+       char *arg, *file = SWANCTL_CONF;
        int ret;
 
        while (TRUE)
@@ -967,6 +967,9 @@ static int load_creds(vici_conn_t *conn)
                        case 'r':
                                format |= COMMAND_FORMAT_RAW;
                                continue;
+                       case 'f':
+                               file = arg;
+                               continue;
                        case EOF:
                                break;
                        default:
@@ -975,10 +978,10 @@ static int load_creds(vici_conn_t *conn)
                break;
        }
 
-       cfg = settings_create(SWANCTL_CONF);
+       cfg = settings_create(file);
        if (!cfg)
        {
-               fprintf(stderr, "parsing '%s' failed\n", SWANCTL_CONF);
+               fprintf(stderr, "parsing '%s' failed\n", file);
                return EINVAL;
        }
 
@@ -1003,6 +1006,7 @@ static void __attribute__ ((constructor))reg()
                        {"noprompt",    'n', 0, "do not prompt for passwords"},
                        {"raw",                 'r', 0, "dump raw response message"},
                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
+                       {"file",                'f', 1, "custom path to swanctl.conf"},
                }
        });
 }
index feb8d3a..ec9508e 100644 (file)
@@ -251,7 +251,7 @@ static int load_pools(vici_conn_t *conn)
 {
        command_format_options_t format = COMMAND_FORMAT_NONE;
        settings_t *cfg;
-       char *arg;
+       char *arg, *file = SWANCTL_CONF;
        int ret;
 
        while (TRUE)
@@ -266,6 +266,9 @@ static int load_pools(vici_conn_t *conn)
                        case 'r':
                                format |= COMMAND_FORMAT_RAW;
                                continue;
+                       case 'f':
+                               file = arg;
+                               continue;
                        case EOF:
                                break;
                        default:
@@ -274,10 +277,10 @@ static int load_pools(vici_conn_t *conn)
                break;
        }
 
-       cfg = settings_create(SWANCTL_CONF);
+       cfg = settings_create(file);
        if (!cfg)
        {
-               fprintf(stderr, "parsing '%s' failed\n", SWANCTL_CONF);
+               fprintf(stderr, "parsing '%s' failed\n", file);
                return EINVAL;
        }
 
@@ -300,6 +303,7 @@ static void __attribute__ ((constructor))reg()
                        {"help",                'h', 0, "show usage information"},
                        {"raw",                 'r', 0, "dump raw response message"},
                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
+                       {"file",                'f', 1, "custom path to swanctl.conf"},
                }
        });
 }