From patchwork Sat Sep 29 03:53:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 473 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director8.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id iJh6I0eEr1vobgAAIUCqbw for ; Sat, 29 Sep 2018 09:55:19 -0400 Received: from proxy18.mail.ord1d.rsapps.net ([172.30.191.6]) by director8.mail.ord1d.rsapps.net with LMTP id IGAlI0eEr1voDgAAfY0hYg ; Sat, 29 Sep 2018 09:55:19 -0400 Received: from smtp25.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy18.mail.ord1d.rsapps.net with LMTP id CK7vIkeEr1syBAAATCaURg ; Sat, 29 Sep 2018 09:55:19 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp25.gate.ord1d.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Suspicious-Flag: YES X-Classification-ID: 4c9cf326-c3ef-11e8-9be0-52540081550e-1-1 Received: from [216.105.38.7] ([216.105.38.7:34001] helo=lists.sourceforge.net) by smtp25.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id BD/BC-10957-7448FAB5; Sat, 29 Sep 2018 09:55:19 -0400 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1g6Fh6-0002Tr-So; Sat, 29 Sep 2018 13:54:04 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1g6Fh5-0002Td-Sa for openvpn-devel@lists.sourceforge.net; Sat, 29 Sep 2018 13:54:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=p7kEILhf5IziBr0MJZ3RC+UjF79jYgqwyM2VfMdd38Y=; b=VO75aPn0wVS7BT7S+2Qa9N+Rwr dsAI82UgAWdsMo6DaJpregGE5KHoQ6St/VajerAwajlCwBIxfVG3KpKzafLZNvYczyNa5/jBlgaHA pId6ZBAWrmc6fb322hlRH4TQ0vuoVW2eLqopRUjsNVpSkyXp+YgVKF9ajAkIHAUD6/OU=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version: Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=p7kEILhf5IziBr0MJZ3RC+UjF79jYgqwyM2VfMdd38Y=; b=ZBTskKpB47x1KgGyoTkQ5eVYX6 xo9mjGnegf/lmBbruJ4BlqDTkDWJdmTq7DHO7ZT5Bfli9VhV6nxUEw/JcjREfLaYfzXjqundOFZjC 5P4ugkqjRMFydJaA3t1x4J/dCeexvMP8POGDIrtUT693GT3QdH42Pp2u9l0ZHpbdJZyY=; Received: from mail-io1-f65.google.com ([209.85.166.65]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) id 1g6Fh4-0096Yf-7Y for openvpn-devel@lists.sourceforge.net; Sat, 29 Sep 2018 13:54:03 +0000 Received: by mail-io1-f65.google.com with SMTP id z16-v6so572002iol.6 for ; Sat, 29 Sep 2018 06:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=p7kEILhf5IziBr0MJZ3RC+UjF79jYgqwyM2VfMdd38Y=; b=adt5FP9lEUpRYpPh2OFlzFIvOUizd5RZK2lTfIWfW8Xpt4zPmKNDStY8w1ENmit8u/ H2IJXBK2Y+/JmhpC4N9nSwBiwmH75Ez99NpXWLu+caMAHBW1fDHt+6Yge9tMNUtcs90Z KliXs5BNsrdadsNUmywlhMdlEu1rrw0lkGS8ifA1QadRbzDw711VZeeyy3+HNSjO42Lu AsdY7bvHHJOgzq1i5+BVUYUMfDsiArQGyWxEu29CdeC/vftSo/de5nuJ2BOF4/WiGeFz h4Nnfyo9TXV+3AB3fKaTgDqKR//e1SeIb0nQHGJ6ymeNk7yQMeqkNSo93+VU5cOxiux2 3jLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=p7kEILhf5IziBr0MJZ3RC+UjF79jYgqwyM2VfMdd38Y=; b=WgdEK3KahxbMTaKHWVmq/3DB+E6Z+N0ddt5vM4m1Al0PKhbohZvmhDe7nZ7sLzga00 1qYfU+os6ZzBlXr9b9IcMNZ/FfT1l6+K0+VhSLSZtN1eG/KqjiAqax2QLi71OFbLr4EV MFDj7zTuxh450fnJ+k2rtIOVtwf0QzPJsSdDcEMbb/ZJAcjeWeojy9jhslptTltr4xMw maezj8/EjVmIW7wUuL2fkW4RpmcXJElAftL/IuzYlJzrdnAcm2xOaOcU2MUbrszi7ynq Zrlpav+xYJvkoEzw/LrGLo3CFHGjbx5QoDXIRujsIT7T5qK5P3w2lzOTZW0C0xHlgqew Ogtw== X-Gm-Message-State: ABuFfohiMORdwPMC1IuUPhboqsk1hLOmT5edVaDI0IlbsbidPDI63O7E lkAyktDur3/ORwKo2beH4w+7enIFGzmdjw== X-Google-Smtp-Source: ACcGV62ovEF/OUpBI8Lko1Sz1vR2CwTpwbOSjB1dX1FkWif45RGx1Yx0CVNtRGce1s3g2BtUZx2mJg== X-Received: by 2002:a6b:be83:: with SMTP id o125-v6mr1974848iof.173.1538229236219; Sat, 29 Sep 2018 06:53:56 -0700 (PDT) Received: from saturn.home.sansel.ca (CPE40167ea0e1c2-CM788df74daaa0.cpe.net.cable.rogers.com. [99.228.215.92]) by smtp.gmail.com with ESMTPSA id w196-v6sm2299207itb.9.2018.09.29.06.53.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 29 Sep 2018 06:53:55 -0700 (PDT) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Sat, 29 Sep 2018 09:53:48 -0400 Message-Id: <1538229228-10620-1-git-send-email-selva.nair@gmail.com> X-Mailer: git-send-email 2.1.4 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [209.85.166.65 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (selva.nair[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.166.65 listed in wl.mailspike.net] X-Headers-End: 1g6Fh4-0096Yf-7Y Subject: [Openvpn-devel] [PATCH] Enable dhcp on tap adapter using interactive service X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Selva Nair Currently, if dhcp on the TAP interface is disabled, OpenVPN on Windows tries to enable it using netsh but that succeeds only when run with admin privileges. When interactive service is available, delegate this task to the service. Trac #1111 Tested on Windows 7 Signed-off-by: Selva Nair --- include/openvpn-msg.h | 8 ++++++- src/openvpn/tun.c | 53 ++++++++++++++++++++++++++++++++++++++++++- src/openvpnserv/interactive.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/include/openvpn-msg.h b/include/openvpn-msg.h index 82ecfe8..66177a2 100644 --- a/include/openvpn-msg.h +++ b/include/openvpn-msg.h @@ -37,7 +37,8 @@ typedef enum { msg_flush_neighbors, msg_add_block_dns, msg_del_block_dns, - msg_register_dns + msg_register_dns, + msg_enable_dhcp, } message_type_t; typedef struct { @@ -111,4 +112,9 @@ typedef struct { interface_t iface; } block_dns_message_t; +typedef struct { + message_header_t header; + interface_t iface; +} enable_dhcp_message_t; + #endif /* ifndef OPENVPN_MSG_H_ */ diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 50f158c..9bf7b27 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -5203,6 +5203,49 @@ netsh_enable_dhcp(const struct tuntap_options *to, argv_reset(&argv); } +/* Enable dhcp on tap adapter using iservice */ +static bool +service_enable_dhcp(const struct tuntap *tt) +{ + DWORD len; + bool ret = false; + ack_message_t ack; + struct gc_arena gc = gc_new(); + HANDLE pipe = tt->options.msg_channel; + + enable_dhcp_message_t dhcp = { + .header = { + msg_enable_dhcp, + sizeof(enable_dhcp_message_t), + 0 + }, + .iface = { .index = tt->adapter_index, .name = "" } + }; + + if (!WriteFile(pipe, &dhcp, sizeof(dhcp), &len, NULL) + || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL)) + { + msg(M_WARN, "TUN: could not talk to service: %s [%lu]", + strerror_win32(GetLastError(), &gc), GetLastError()); + goto out; + } + + if (ack.error_number != NO_ERROR) + { + msg(M_NONFATAL, "TUN: enabling dhcp using service failed: %s [status=%u if_index=%d]", + strerror_win32(ack.error_number, &gc), ack.error_number, dhcp.iface.index); + } + else + { + msg(M_INFO, "DHCP enabled on interface %d using service", dhcp.iface.index); + ret = true; + } + +out: + gc_free(&gc); + return ret; +} + /* * Return a TAP name for netsh commands. */ @@ -5683,7 +5726,15 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun */ if (dhcp_status(tt->adapter_index) == DHCP_STATUS_DISABLED) { - netsh_enable_dhcp(&tt->options, tt->actual_name); + /* try using the service if available, else directly execute netsh */ + if (tt->options.msg_channel) + { + service_enable_dhcp(tt); + } + else + { + netsh_enable_dhcp(&tt->options, tt->actual_name); + } } dhcp_masq = true; dhcp_masq_post = true; diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index 861f5e7..d0bb120 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -1176,6 +1176,50 @@ out: return err; } +static DWORD +HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) +{ + DWORD err = 0; + DWORD timeout = 5000; /* in milli seconds */ + wchar_t argv0[MAX_PATH+1]; + + /* Path of netsh */ + int n = GetSystemDirectory(argv0, MAX_PATH); + if (n > 0 && n < MAX_PATH) /* got system directory */ + { + wcsncat(argv0, L"\\netsh.exe", MAX_PATH - n - 1); + } + else + { + wcsncpy(argv0, L"C:\\Windows\\system32\\netsh.exe", MAX_PATH); + } + + /* cmd template: + * netsh interface ipv4 set address name=$if_index source=dhcp + */ + const wchar_t *fmt = L"netsh interface ipv4 set address name=\"%d\" source=dhcp"; + + /* max cmdline length in wchars -- include room for if index */ + size_t ncmdline = wcslen(fmt) + 10 + 1; + wchar_t *cmdline = malloc(ncmdline*sizeof(wchar_t)); + if (!cmdline) + { + err = ERROR_OUTOFMEMORY; + return err; + } + + openvpn_sntprintf(cmdline, ncmdline, fmt, dhcp->iface.index); + + err = ExecCommand(argv0, cmdline, timeout); + + /* Note: This could fail if dhcp is already enabled, so the caller + * may not want to treat errors as FATAL. + */ + + free(cmdline); + return err; +} + static VOID HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists_t *lists) { @@ -1187,6 +1231,7 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists flush_neighbors_message_t flush_neighbors; block_dns_message_t block_dns; dns_cfg_message_t dns; + enable_dhcp_message_t dhcp; } msg; ack_message_t ack = { .header = { @@ -1247,6 +1292,13 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists ack.error_number = HandleDNSConfigMessage(&msg.dns, lists); break; + case msg_enable_dhcp: + if (msg.header.size == sizeof(msg.dhcp)) + { + ack.error_number = HandleEnableDHCPMessage(&msg.dhcp); + } + break; + default: ack.error_number = ERROR_MESSAGE_TYPE; MsgToEventLog(MSG_FLAGS_ERROR, TEXT("Unknown message type %d"), msg.header.type);