From 3ceee41d33d4ce6547df164ba84cd3369405306b Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Fri, 20 Apr 2018 17:33:07 +0200 Subject: [PATCH] OpenVPN: hostname support OpenVPN sends the default gateway's MAC address, if the option --push-peer-info is enabled. It also sends the client's environment variables whose names start with "UV_". This commit adds some lines of code in OvsBeginIPCAsyncConnectionIfEmpty(), in order to set the hostname to "UV_HOSTNAME"'s value, which is defined by the user on their device. In case "UV_HOSTNAME" is not available, "IV_HWADDR"'s value (the default gateway's MAC address) is used instead. OvsParseOptions() has been adapted into a new function called OvsParsePeerInfo(), in order to parse the peer info string. --- src/Cedar/Interop_OpenVPN.c | 54 +++++++++++++++++++++++++++++++++++++ src/Cedar/Interop_OpenVPN.h | 1 + 2 files changed, 55 insertions(+) diff --git a/src/Cedar/Interop_OpenVPN.c b/src/Cedar/Interop_OpenVPN.c index 366ce95f..d248259b 100644 --- a/src/Cedar/Interop_OpenVPN.c +++ b/src/Cedar/Interop_OpenVPN.c @@ -673,6 +673,7 @@ void OvsBeginIPCAsyncConnectionIfEmpty(OPENVPN_SERVER *s, OPENVPN_SESSION *se, O if (se->IpcAsync == NULL) { + LIST *pi; IPC_PARAM p; ETHERIP_ID id; @@ -702,6 +703,24 @@ void OvsBeginIPCAsyncConnectionIfEmpty(OPENVPN_SERVER *s, OPENVPN_SESSION *se, O StrCpy(p.CryptName, sizeof(p.CryptName), c->CipherEncrypt->Name); } + // OpenVPN sends the default gateway's MAC address, + // if the option --push-peer-info is enabled. + // It also sends all of the client's environment + // variables whose names start with "UV_". + pi = OvsParsePeerInfo(c->ClientKey.PeerInfo); + + // Check presence of custom hostname + if (OvsHasOption(pi, "UV_HOSTNAME")) + { + StrCpy(p.ClientHostname, sizeof(p.ClientHostname), IniStrValue(pi, "UV_HOSTNAME")); + } + else // Use the default gateway's MAC address + { + StrCpy(p.ClientHostname, sizeof(p.ClientHostname), IniStrValue(pi, "IV_HWADDR")); + } + + OvsFreeOptions(pi); + if (se->Mode == OPENVPN_MODE_L3) { // L3 Mode @@ -1000,6 +1019,41 @@ LIST *OvsParseOptions(char *str) return o; } +// Parse the peer info string +LIST *OvsParsePeerInfo(char *str) +{ + LIST *o = NewListFast(NULL); + TOKEN_LIST *t; + + t = ParseTokenWithoutNullStr(str, "\n"); + if (t != NULL) + { + UINT i; + + for (i = 0;i < t->NumTokens;i++) + { + char key[MAX_SIZE]; + char value[MAX_SIZE]; + char *line = t->Token[i]; + Trim(line); + + if (GetKeyAndValue(line, key, sizeof(key), value, sizeof(value), "=\t")) + { + INI_ENTRY *e = ZeroMalloc(sizeof(INI_ENTRY)); + + e->Key = CopyStr(key); + e->Value = CopyStr(value); + + Add(o, e); + } + } + + FreeToken(t); + } + + return o; +} + // Release the option list void OvsFreeOptions(LIST *o) { diff --git a/src/Cedar/Interop_OpenVPN.h b/src/Cedar/Interop_OpenVPN.h index 4157206a..a1b3901a 100644 --- a/src/Cedar/Interop_OpenVPN.h +++ b/src/Cedar/Interop_OpenVPN.h @@ -362,6 +362,7 @@ BUF *OvsBuildKeyMethod2(OPENVPN_KEY_METHOD_2 *d); void OvsWriteStringToBuf(BUF *b, char *str, UINT max_size); LIST *OvsParseOptions(char *str); +LIST *OvsParsePeerInfo(char *str); void OvsFreeOptions(LIST *o); LIST *OvsNewOptions(); void OvsAddOption(LIST *o, char *key, char *value);