Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 1 | /* Simple Osmocom System Monitor (osysmon): Support for OpenVPN monitoring */ |
| 2 | |
| 3 | /* (C) 2019 by sysmocom - s.f.m.c. GmbH. |
| 4 | * Author: Max Suraev |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 22 | * MA 02110-1301, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdbool.h> |
| 27 | #include <ctype.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <osmocom/core/utils.h> |
| 31 | #include <osmocom/vty/vty.h> |
| 32 | #include <osmocom/vty/command.h> |
| 33 | #include <osmocom/core/msgb.h> |
| 34 | #include <osmocom/netif/stream.h> |
| 35 | |
| 36 | #include "osysmon.h" |
| 37 | #include "client.h" |
| 38 | #include "value_node.h" |
| 39 | |
| 40 | /*********************************************************************** |
| 41 | * Data model |
| 42 | ***********************************************************************/ |
| 43 | |
| 44 | #define OVPN_LOG(ctx, vpn, fmt, args...) \ |
| 45 | fprintf(stderr, "OpenVPN [%s]: " fmt, make_authority(ctx, vpn->cfg), ##args) |
| 46 | |
| 47 | /* max number of csv in response */ |
| 48 | #define MAX_RESP_COMPONENTS 6 |
| 49 | |
| 50 | /* a single OpenVPN management interface client */ |
| 51 | struct openvpn_client { |
| 52 | /* links to osysmon.openvpn_clients */ |
| 53 | struct llist_head list; |
| 54 | struct host_cfg *cfg; |
| 55 | struct osmo_stream_cli *mgmt; |
| 56 | /* fields below are parsed from response to 'state' command on mgmt interface */ |
| 57 | struct host_cfg *rem_cfg; |
| 58 | char *tun_ip; |
| 59 | bool connected; |
| 60 | }; |
| 61 | |
| 62 | static char *parse_state(struct msgb *msg, struct openvpn_client *vpn) |
| 63 | { |
| 64 | char tmp[128]; |
| 65 | char *tok; |
| 66 | unsigned int i = 0; |
| 67 | uint8_t *m = msgb_data(msg); |
Pau Espin Pedrol | b910132 | 2019-03-15 20:09:26 +0100 | [diff] [blame^] | 68 | unsigned int truncated_len = OSMO_MIN(sizeof(tmp) - 1, msgb_length(msg)); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 69 | |
Pau Espin Pedrol | b910132 | 2019-03-15 20:09:26 +0100 | [diff] [blame^] | 70 | if (msgb_length(msg) > truncated_len) |
| 71 | OVPN_LOG(msg, vpn, "received message too long (%d >= %u), truncating...\n", msgb_length(msg), truncated_len); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 72 | |
| 73 | if (msgb_length(msg) > 0) { |
| 74 | if (!isdigit(m[0])) /* skip OpenVPN greetings and alike */ |
| 75 | return NULL; |
| 76 | } else { |
| 77 | OVPN_LOG(msg, vpn, "received message is empty, ignoring...\n"); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Pau Espin Pedrol | b910132 | 2019-03-15 20:09:26 +0100 | [diff] [blame^] | 81 | memcpy(tmp, m, truncated_len); |
| 82 | tmp[truncated_len] = '\0'; |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 83 | |
| 84 | for (tok = strtok(tmp, ","); tok && i < MAX_RESP_COMPONENTS; tok = strtok(NULL, ",")) { |
| 85 | /* The string format is documented in https://openvpn.net/community-resources/management-interface/ */ |
| 86 | if (tok) { /* Parse csv string and pick interesting tokens while ignoring the rest. */ |
| 87 | switch (i++) { |
| 88 | case 1: |
| 89 | update_name(vpn->rem_cfg, tok); |
| 90 | break; |
| 91 | case 3: |
| 92 | osmo_talloc_replace_string(vpn->rem_cfg, &vpn->tun_ip, tok); |
| 93 | break; |
| 94 | case 4: |
| 95 | update_host(vpn->rem_cfg, tok); |
| 96 | break; |
| 97 | case 5: |
| 98 | vpn->rem_cfg->remote_port = atoi(tok); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | static struct openvpn_client *openvpn_client_find_or_make(const struct osysmon_state *os, |
| 107 | const char *host, uint16_t port) |
| 108 | { |
| 109 | struct openvpn_client *vpn; |
| 110 | llist_for_each_entry(vpn, &os->openvpn_clients, list) { |
| 111 | if (match_config(vpn->cfg, host, MATCH_HOST) && vpn->cfg->remote_port == port) |
| 112 | return vpn; |
| 113 | } |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Max | 6cbdcaf | 2019-02-21 10:06:19 +0100 | [diff] [blame] | 118 | static int disconnect_cb(struct osmo_stream_cli *conn) |
| 119 | { |
| 120 | struct openvpn_client *vpn = osmo_stream_cli_get_data(conn); |
| 121 | |
Pau Espin Pedrol | 53f5073 | 2019-03-15 19:18:18 +0100 | [diff] [blame] | 122 | update_name(vpn->rem_cfg, "disconnected from openvpn management socket"); |
Max | 6cbdcaf | 2019-02-21 10:06:19 +0100 | [diff] [blame] | 123 | vpn->connected = false; |
| 124 | talloc_free(vpn->tun_ip); |
| 125 | talloc_free(vpn->rem_cfg->remote_host); |
| 126 | vpn->tun_ip = NULL; |
| 127 | vpn->rem_cfg->remote_host = NULL; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 132 | static int connect_cb(struct osmo_stream_cli *conn) |
| 133 | { |
| 134 | struct openvpn_client *vpn = osmo_stream_cli_get_data(conn); |
| 135 | |
Pau Espin Pedrol | 53f5073 | 2019-03-15 19:18:18 +0100 | [diff] [blame] | 136 | update_name(vpn->rem_cfg, "connected to openvpn management socket"); |
Max | 6cbdcaf | 2019-02-21 10:06:19 +0100 | [diff] [blame] | 137 | vpn->connected = true; |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int read_cb(struct osmo_stream_cli *conn) |
| 143 | { |
| 144 | int bytes; |
| 145 | struct openvpn_client *vpn = osmo_stream_cli_get_data(conn); |
| 146 | struct msgb *msg = msgb_alloc(1024, "OpenVPN"); |
| 147 | if (!msg) { |
| 148 | OVPN_LOG(conn, vpn, "unable to allocate message in callback\n"); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | bytes = osmo_stream_cli_recv(conn, msg); |
| 153 | if (bytes < 0) |
Pau Espin Pedrol | 53f5073 | 2019-03-15 19:18:18 +0100 | [diff] [blame] | 154 | OVPN_LOG(msg, vpn, "read on openvpn management socket failed (%d)\n", bytes); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 155 | else |
| 156 | parse_state(msg, vpn); |
| 157 | |
| 158 | msgb_free(msg); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static bool openvpn_client_create(struct osysmon_state *os, const char *name, const char *host, uint16_t port) |
| 164 | { |
| 165 | struct openvpn_client *vpn = openvpn_client_find_or_make(os, host, port); |
| 166 | if (vpn) |
| 167 | return true; |
| 168 | |
| 169 | vpn = talloc_zero(os, struct openvpn_client); |
| 170 | if (!vpn) |
| 171 | return false; |
| 172 | |
| 173 | vpn->connected = false; |
| 174 | |
| 175 | vpn->cfg = host_cfg_alloc(vpn, name, host, port); |
| 176 | if (!vpn->cfg) |
| 177 | goto dealloc; |
| 178 | |
Pau Espin Pedrol | 53f5073 | 2019-03-15 19:18:18 +0100 | [diff] [blame] | 179 | vpn->rem_cfg = host_cfg_alloc(vpn, "connecting to openvpn management socket", NULL, 0); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 180 | if (!vpn->rem_cfg) |
| 181 | goto dealloc; |
| 182 | |
| 183 | vpn->mgmt = make_tcp_client(vpn->cfg); |
| 184 | if (!vpn->mgmt) { |
| 185 | OVPN_LOG(vpn->rem_cfg, vpn, "failed to create TCP client\n"); |
| 186 | goto dealloc; |
| 187 | } |
| 188 | |
| 189 | /* Wait for 1 minute before attempting to reconnect to management interface */ |
Pau Espin Pedrol | 0813db3 | 2019-03-15 19:27:04 +0100 | [diff] [blame] | 190 | osmo_stream_cli_set_reconnect_timeout(vpn->mgmt, 5); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 191 | osmo_stream_cli_set_read_cb(vpn->mgmt, read_cb); |
| 192 | osmo_stream_cli_set_connect_cb(vpn->mgmt, connect_cb); |
Max | 6cbdcaf | 2019-02-21 10:06:19 +0100 | [diff] [blame] | 193 | osmo_stream_cli_set_disconnect_cb(vpn->mgmt, disconnect_cb); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 194 | |
| 195 | if (osmo_stream_cli_open(vpn->mgmt) < 0) { |
Pau Espin Pedrol | 53f5073 | 2019-03-15 19:18:18 +0100 | [diff] [blame] | 196 | OVPN_LOG(vpn->rem_cfg, vpn, "failed to connect to management socket\n"); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 197 | goto dealloc; |
| 198 | } |
| 199 | |
| 200 | osmo_stream_cli_set_data(vpn->mgmt, vpn); |
| 201 | llist_add_tail(&vpn->list, &os->openvpn_clients); |
| 202 | |
| 203 | return true; |
| 204 | |
| 205 | dealloc: |
| 206 | talloc_free(vpn); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | static void openvpn_client_destroy(struct openvpn_client *vpn) |
| 211 | { |
| 212 | if (!vpn) |
| 213 | return; |
| 214 | |
| 215 | osmo_stream_cli_destroy(vpn->mgmt); |
| 216 | llist_del(&vpn->list); |
| 217 | talloc_free(vpn); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /*********************************************************************** |
| 222 | * VTY |
| 223 | ***********************************************************************/ |
| 224 | |
| 225 | #define OPENVPN_STR "Configure OpenVPN management interface address\n" |
| 226 | |
| 227 | DEFUN(cfg_openvpn, cfg_openvpn_cmd, |
| 228 | "openvpn HOST <1-65535>", |
| 229 | OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n") |
| 230 | { |
| 231 | uint16_t port = atoi(argv[1]); |
| 232 | |
| 233 | if (!openvpn_client_create(g_oss, "OpenVPN", argv[0], port)) { |
| 234 | vty_out(vty, "Failed to create OpenVPN client for %s:%u%s", argv[0], port, VTY_NEWLINE); |
| 235 | return CMD_WARNING; |
| 236 | } |
| 237 | |
| 238 | return CMD_SUCCESS; |
| 239 | } |
| 240 | |
| 241 | DEFUN(cfg_no_openvpn, cfg_no_openvpn_cmd, |
| 242 | "no openvpn HOST <1-65535>", |
| 243 | NO_STR OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n") |
| 244 | { |
| 245 | uint16_t port = atoi(argv[1]); |
| 246 | struct openvpn_client *vpn = openvpn_client_find_or_make(g_oss, argv[0], port); |
| 247 | if (!vpn) { |
| 248 | vty_out(vty, "OpenVPN client %s:%u doesn't exist%s", argv[0], port, VTY_NEWLINE); |
| 249 | return CMD_WARNING; |
| 250 | } |
| 251 | |
| 252 | openvpn_client_destroy(vpn); |
| 253 | |
| 254 | return CMD_SUCCESS; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /*********************************************************************** |
| 259 | * Runtime Code |
| 260 | ***********************************************************************/ |
| 261 | |
| 262 | static int openvpn_client_poll(struct openvpn_client *vpn, struct value_node *parent) |
| 263 | { |
| 264 | char *remote = make_authority(parent, vpn->rem_cfg); |
| 265 | struct value_node *vn_host = value_node_find_or_add(parent, make_authority(parent, vpn->cfg)); |
| 266 | struct msgb *msg = msgb_alloc(128, "state"); |
| 267 | if (!msg) { |
| 268 | value_node_add(vn_host, "msgb", "memory allocation failure"); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | if (vpn->rem_cfg->name) |
| 273 | value_node_add(vn_host, "status", vpn->rem_cfg->name); |
| 274 | |
| 275 | if (vpn->tun_ip) |
| 276 | value_node_add(vn_host, "tunnel", vpn->tun_ip); |
| 277 | |
| 278 | if (remote) |
| 279 | value_node_add(vn_host, "remote", remote); |
| 280 | |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 281 | if (vpn->connected) { /* re-trigger state command */ |
| 282 | msgb_printf(msg, "state\n"); |
| 283 | osmo_stream_cli_send(vpn->mgmt, msg); |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* called once on startup before config file parsing */ |
| 290 | int osysmon_openvpn_init() |
| 291 | { |
| 292 | install_element(CONFIG_NODE, &cfg_openvpn_cmd); |
| 293 | install_element(CONFIG_NODE, &cfg_no_openvpn_cmd); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* called periodically */ |
| 299 | int osysmon_openvpn_poll(struct value_node *parent) |
| 300 | { |
| 301 | int num_vpns = llist_count(&g_oss->openvpn_clients); |
| 302 | if (num_vpns) { |
| 303 | struct value_node *vn_vpn = value_node_add(parent, "OpenVPN", NULL); |
| 304 | struct openvpn_client *vpn; |
| 305 | llist_for_each_entry(vpn, &g_oss->openvpn_clients, list) |
| 306 | openvpn_client_poll(vpn, vn_vpn); |
| 307 | } |
| 308 | |
| 309 | return num_vpns; |
| 310 | } |