From dee6cbec255c3a8af7f79fd7e401d231a5cbbb93 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Wed, 16 Jan 2019 11:26:56 +0100 Subject: tools: Allow enrollment with destination IPCP The irm enroll tool had a semantic to enroll with a layer name, but this is not checked. Now the enroll command will retrieve the correct layer name that the IPCP got from the actual enrollment procedure. The irm enroll now has two string parameters, a dst and a layer, which cannot be both NULL. If only dst is specified, the IPCP will enroll with that name; autobind will bind with the layer name. If only layer is specified, the IPCP will enroll with the layer name, and perform a check that the layer name retrieved from enrollment is indeed the layer name before possibly autobinding. If both dst and layer are specified, the IPCP will enroll with dst and perform a check that the enrollment was in the expected layer. Basically only specifying the layer name is a shorthand for dst == layer. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/tools/irm/irm_ipcp_enroll.c | 63 ++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 11 deletions(-) (limited to 'src/tools/irm/irm_ipcp_enroll.c') diff --git a/src/tools/irm/irm_ipcp_enroll.c b/src/tools/irm/irm_ipcp_enroll.c index 5b6caf55..082f2c52 100644 --- a/src/tools/irm/irm_ipcp_enroll.c +++ b/src/tools/irm/irm_ipcp_enroll.c @@ -46,24 +46,46 @@ #include -#define NORMAL "normal" +#define NORMAL "unicast" #define BROADCAST "broadcast" static void usage(void) { printf("Usage: irm ipcp enroll\n" " name \n" - " layer \n" - " [type [TYPE], default = normal]\n" + " [layer ]\n" + " [dst ]\n" + " [type [TYPE], default = " NORMAL "]\n" " [autobind]\n" "where TYPE = {" NORMAL " " BROADCAST "}\n"); } +static int get_layer_name(const char * ipcp, + char * layer_name) +{ + struct ipcp_info * ipcps; + size_t len; + size_t i; + + len = irm_list_ipcps(&ipcps); + for (i = 0; i < len; i++) + if (strcmp(ipcps[i].name, ipcp) == 0) { + strcpy(layer_name, ipcps[i].layer); + free(ipcps); + return 0; + } + + free(ipcps); + + return -1; +} + int do_enroll_ipcp(int argc, char ** argv) { char * ipcp = NULL; char * layer = NULL; + char * dst = NULL; struct ipcp_info * ipcps; pid_t pid = -1; ssize_t len = 0; @@ -81,6 +103,8 @@ int do_enroll_ipcp(int argc, ipcp_type = *(argv + 1); } else if (matches(*argv, "layer") == 0) { layer = *(argv + 1); + } else if (matches(*argv, "dst") == 0) { + dst = *(argv + 1); } else if (matches(*argv, "autobind") == 0) { autobind = true; cargs = 1; @@ -94,11 +118,14 @@ int do_enroll_ipcp(int argc, argv += cargs; } - if (layer == NULL || ipcp == NULL) { + if ((layer == NULL && dst == NULL) || ipcp == NULL) { usage(); return -1; } + if (dst == NULL) + dst = layer; + if (strcmp(ipcp_type, NORMAL) == 0) type = IPCP_NORMAL; else if (strcmp(ipcp_type, BROADCAST) == 0) @@ -121,22 +148,36 @@ int do_enroll_ipcp(int argc, for (i = 0; i < len; i++) { if (ipcps[i].type != type) continue; + if (wildcard_match(ipcps[i].name, ipcp) == 0) { + char enr_layer[LAYER_NAME_SIZE]; + pid = ipcps[i].pid; - if (autobind && irm_bind_process(pid, ipcp)) { - printf("Failed to bind %d to %s.\n", pid, ipcp); + + if (irm_enroll_ipcp(pid, dst)) { + printf("Failed to enroll IPCP.\n"); goto fail; } - if (irm_enroll_ipcp(pid, layer)) { - if (autobind) - irm_unbind_process(pid, ipcp); + if (get_layer_name(ipcps[i].name, enr_layer)) { + printf("Could not get layer name.\n"); + goto fail; + } + + if (layer != NULL && strcmp(enr_layer, layer)) { + printf("Enrollment destination does not " + "match requested layer.\n"); + goto fail; + } + + if (autobind && irm_bind_process(pid, ipcp)) { + printf("Failed to bind %d to %s.\n", pid, ipcp); goto fail; } - if (autobind && irm_bind_process(pid, layer)) { + if (autobind && irm_bind_process(pid, enr_layer)) { printf("Failed to bind %d to %s.\n", - pid, layer); + pid, enr_layer); goto fail; } } -- cgit v1.2.3