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); From patchwork Tue Oct 2 10:01:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 474 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director8.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id uFEQLOTOs1t5TwAAIUCqbw for ; Tue, 02 Oct 2018 16:02:44 -0400 Received: from proxy2.mail.iad3b.rsapps.net ([172.31.255.6]) by director8.mail.ord1d.rsapps.net with LMTP id MCW4KeTOs1seTAAAfY0hYg ; Tue, 02 Oct 2018 16:02:44 -0400 Received: from smtp39.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy2.mail.iad3b.rsapps.net with LMTP id kAjtJOTOs1tLRwAAvAZTew ; Tue, 02 Oct 2018 16:02:44 -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: smtp39.gate.iad3b.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: 1fbe85f4-c67e-11e8-9673-5254002be87c-1-1 Received: from [216.105.38.7] ([216.105.38.7:2767] helo=lists.sourceforge.net) by smtp39.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 68/4F-31203-4EEC3BB5; Tue, 02 Oct 2018 16:02:44 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1g7QrK-0005mE-1D; Tue, 02 Oct 2018 20:01:30 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1g7QrI-0005m8-3J for openvpn-devel@lists.sourceforge.net; Tue, 02 Oct 2018 20:01:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To: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:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=ud5z99or8qgt9LuidPJlO3bMXFQbHP1x8vuywVmQK/A=; b=AKOHbpV+w7biHDG6ouMuOSSaVa CyfptKOJcXWAdYfYhGbwQ5qKSpuLfXCKI/rHyb+plHf3rVw30l2GKhqXmPY/7GejPDf6sjykzTrNo SYsaUkmEKGQQmWOQTDBOZbf7ZChSEKM0Bnz33x5TR/FZ4B6kbvAiuyvLoXgDRv3rMbxQ=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To: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:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=ud5z99or8qgt9LuidPJlO3bMXFQbHP1x8vuywVmQK/A=; b=JUQTYWSX1sh5PABgc5bQTmYV4B 1hWDGU6iTOKew+jl9FA4Zgv/7MoZVQedzyB4QN2V4MA/r9+Q2ymu1FSI52ybnVHTWgbi0aBKvzEaJ gI+BKqD7rzgLe4Mc21h92iHexSYlcJ+AGXOrH0q4S+bJT1bWHZ0iu4bNf12nAfRB6YII=; Received: from mail-io1-f65.google.com ([209.85.166.65]) by sfi-mx-4.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) id 1g7QrG-00B61O-LX for openvpn-devel@lists.sourceforge.net; Tue, 02 Oct 2018 20:01:28 +0000 Received: by mail-io1-f65.google.com with SMTP id k19-v6so3149672iom.1 for ; Tue, 02 Oct 2018 13:01:26 -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:in-reply-to:references; bh=ud5z99or8qgt9LuidPJlO3bMXFQbHP1x8vuywVmQK/A=; b=By6VKe2b/jOAJ3/6BtCuMfWAEc5YVeR+jxmSE7NhD+TqzRpx/q2yKuGAGZssey/B07 tQKskCe4EhbkK405t1Qje8yGUgfVA32Bp4VdJfA56Wa8JT0SjLUwPDHbwKke+tPCcEYP HoY5wjB6EexHV44qy8oIlAWHY4js7Ixq0uzYmOE1hoc61hd7+Y0/SOTBaC6+n0QUoLht U1KNsvVntyhyWfq/fa8T+fE8eoojCBhBBni6yzOkpCK/LVEGC4gh2kSnITm4XS4EdMTH dQnOZqfexJ8uJRshodWVbah8KKGR6ZO46cKcI6C27o4cpzb51FrCbvtMMc3xr9OD8zHW A0ww== 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:in-reply-to :references; bh=ud5z99or8qgt9LuidPJlO3bMXFQbHP1x8vuywVmQK/A=; b=Gm6OaE9rZILn1OGgd2H4Jz4Jycu5qyuzq5Qpt0JC0plabYLYsoDc1uHmaI+tGo0a+q epXk+Wr7L4fKYwwH11YjRTDG7/WQQcyeqRYwtPOLfW3MCiKxg5zxvdM1YmdGNpmVy/3+ RjsZoCvw21ESqZnYy0ThegWrC3OlHvOT4YtfN87Y5QKAGNrSjG2LIiYRMGSiPR4Ahn/j f2gEWNHIPd6qZgX8/G9zVwWMgaBRLJ5cb6RVfyMi4ywtCigWKvHiI9CwyZ0NzM0f4mmt 0XRBh+SIKvB4w3D1M+4vPAwAXcvQi7W/GlywL9aXiJlUhBDNhve3gpdpT+lN+FNN0x5k 8qdA== X-Gm-Message-State: ABuFfoiRr1oNaCkJxHduqPLVeWK23ZdOdgnvjTrn+7W2+I6SYpi4m6mk Zlfs1cQ+DhoEZIb2j+4dATDnA6YJX4A= X-Google-Smtp-Source: ACcGV60PRWHwDiMbIaZ9Z4HUmem+uxqVT7E0lZtpp6zWjdSiJmJ1VnyVZKEcYZ3IiOXsrJr1z0Ap5g== X-Received: by 2002:a6b:1a11:: with SMTP id a17-v6mr11296203ioa.223.1538510480581; Tue, 02 Oct 2018 13:01:20 -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 o201-v6sm6342961ito.41.2018.10.02.13.01.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Oct 2018 13:01:20 -0700 (PDT) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Tue, 2 Oct 2018 16:01:13 -0400 Message-Id: <1538510474-27602-2-git-send-email-selva.nair@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1538510474-27602-1-git-send-email-selva.nair@gmail.com> References: <1538229228-10620-1-git-send-email-selva.nair@gmail.com> <1538510474-27602-1-git-send-email-selva.nair@gmail.com> 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] 0.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1g7QrG-00B61O-LX Subject: [Openvpn-devel] [PATCH 2/3 v2] 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 Acked-by: Gert Doering --- v2 changes: As suggested by Lev - In comment, clarify the 10 chars room is for printing 32 bit int - Use get_win_sys_path() added in the accompanying patch (1 of 3) Refactoring of writing to the message channel is in the following patch. include/openvpn-msg.h | 8 ++++++- src/openvpn/tun.c | 53 ++++++++++++++++++++++++++++++++++++++++++- src/openvpnserv/interactive.c | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 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..a2d5315 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, "Enable_dhcp: 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 0489684..c0fdc19 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -1165,6 +1165,45 @@ 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]; + + /* Path of netsh */ + swprintf(argv0, _countof(argv0), L"%s\\%s", get_win_sys_path(), L"netsh.exe"); + argv0[_countof(argv0) - 1] = L'\0'; + + /* 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: + * 10 chars for 32 bit int in decimal and +1 for NUL + */ + 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) { @@ -1176,6 +1215,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 = { @@ -1236,6 +1276,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); From patchwork Tue Oct 2 10:01:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 475 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id INz4LeTOs1scAgAAIUCqbw for ; Tue, 02 Oct 2018 16:02:44 -0400 Received: from proxy14.mail.iad3b.rsapps.net ([172.31.255.6]) by director7.mail.ord1d.rsapps.net with LMTP id +K9KK+TOs1sbSgAAovjBpQ ; Tue, 02 Oct 2018 16:02:44 -0400 Received: from smtp4.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy14.mail.iad3b.rsapps.net with LMTP id KNJnJeTOs1ttKQAA+7ETDg ; Tue, 02 Oct 2018 16:02:44 -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: smtp4.gate.iad3b.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: 1fbeca32-c67e-11e8-932b-525400789c6c-1-1 Received: from [216.105.38.7] ([216.105.38.7:40462] helo=lists.sourceforge.net) by smtp4.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id A6/FB-19525-4EEC3BB5; Tue, 02 Oct 2018 16:02:44 -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 1g7QrM-0003hz-2u; Tue, 02 Oct 2018 20:01:32 +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 1g7QrK-0003ht-4I for openvpn-devel@lists.sourceforge.net; Tue, 02 Oct 2018 20:01:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To: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:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=oLDAowQl7r64OYgzV/vxXLGqWWq8p/Di+9+Aksy1hCs=; b=SzRVzy8GF1rrgzNs6hXn8D14MW gAz2sR5N+8Dovd08/Hy7C2jwgjOMRb6dR3TvOw3Ytt+32S9nRZiCwFeE2u330mNB5K5Z2YHK77sl+ HKAj0FemJO2QM+4WRxFuQ4qZ2/RD3q8iTpLs9ECF+GU/eH6daA601G1cTG0Vn2n6QBHE=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To: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:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=oLDAowQl7r64OYgzV/vxXLGqWWq8p/Di+9+Aksy1hCs=; b=c5w+0HzPsYCvmGLvMzINY9Wgcx LvQmgcQi1b7WD8Q1wQu88fjtNgaFHqCTa5uTeNzxyK2sPAwPs581Irg9e3/1cyDDzU7k8oduhVcAA ShMPWtYm7URtjyRo9If/wobkzfq/92MFEB8hp23YOvkMZyDroOuXjy5xWRHggxpe7+No=; Received: from mail-io1-f47.google.com ([209.85.166.47]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) id 1g7QrI-00Ggl0-Aj for openvpn-devel@lists.sourceforge.net; Tue, 02 Oct 2018 20:01:30 +0000 Received: by mail-io1-f47.google.com with SMTP id z16-v6so3129971iol.6 for ; Tue, 02 Oct 2018 13:01:28 -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:in-reply-to:references; bh=oLDAowQl7r64OYgzV/vxXLGqWWq8p/Di+9+Aksy1hCs=; b=P8mhiNpbs0gnzvBqiP0WRKNy1lQe+GtYFK99soAzn8lNHzLCby8gMDMtepwJiXgAcB oNWLGsfUyjCjkiGrD9njeAYrVRK/0ddHZ0VeseuAUUKmHasGJMX73JzhJ2wj9pzS6ITB sONrJuTMxnQBsD1k7WOX07zlL2G0UIk0VMEPSqeip9RuSYqJQE05HvPR5WiL4NT1NEQa CeZBYpS0/SHg+nMOuKbYRyB5xkFVo0Q4h52AMdPvgt8PNHDUkeDaGUIjvVZf0CYCLjJQ BENbdOCcfbfjBOhAVoQel1ZaIH/AOJYGfYetWWzo7yOv3ppxGJE1BZTgh6JtmVH5vrrv ASYA== 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:in-reply-to :references; bh=oLDAowQl7r64OYgzV/vxXLGqWWq8p/Di+9+Aksy1hCs=; b=HJmAK0IhlKz5XyxWsBPUjMMp9nTsNj7Hn+t4LXhvwiPKUrQ5R0MX55jVaF4iLGDj3Q pq4dVpDATlgYW9FBZ2lKSI8T+tWlETqXtUy5uCJvYGeeiC1Jyn9DvWvjRs9ntJCdTBnQ +DhvxIeAFTkMlRynMCwvJG5gK0cBZG6rXlhhPVxgxN4NEJBWl83SQMuWliciaBajf+Bf aQ1SAVdmmGJfbyU0WOC7KOvDVjrkuDkZLlkWhN2ml5B0IorazQZyRh//PWZW3spARfmY IjlhndNPuVoFhIa3Dw8EtFWlaKya+QHKmVKbHqtu/i7Ziu1XnA6U0fpopP2Cj+IRXomJ 0nIg== X-Gm-Message-State: ABuFfohVnFLaSwnSFjRYlP+dCKzDwE8xyCRmeJwSLbzGPTB5qsydq3lh ojzzkP5zAbT4mKd0VrnXwXTyVofcBhw= X-Google-Smtp-Source: ACcGV62HxQm96r8Ire+FHk5yf1nhoj7BNZ72LLZ5JqZMaQSjzSaJkmuFsrdYDM4IjegA3CcRPH8AnQ== X-Received: by 2002:a6b:6a02:: with SMTP id x2-v6mr8557869iog.38.1538510482254; Tue, 02 Oct 2018 13:01:22 -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 o201-v6sm6342961ito.41.2018.10.02.13.01.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Oct 2018 13:01:21 -0700 (PDT) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Tue, 2 Oct 2018 16:01:14 -0400 Message-Id: <1538510474-27602-3-git-send-email-selva.nair@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1538510474-27602-1-git-send-email-selva.nair@gmail.com> References: <1538229228-10620-1-git-send-email-selva.nair@gmail.com> <1538510474-27602-1-git-send-email-selva.nair@gmail.com> 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.47 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (selva.nair[at]gmail.com) -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.166.47 listed in wl.mailspike.net] -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 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1g7QrI-00Ggl0-Aj Subject: [Openvpn-devel] [PATCH 3/3] Refactor sending commands to 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 Move writing the message buffer to the interactive service pipe and reading acknowledgement to a function. A minor bug in open_tun where the ack data could be read even after a communication error is fixed. Signed-off-by: Selva Nair Acked-by: Gert Doering --- src/openvpn/route.c | 6 +----- src/openvpn/tun.c | 34 +++++++++------------------------- src/openvpn/win32.c | 27 ++++++++++++++++++++++----- src/openvpn/win32.h | 9 +++++++++ 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/openvpn/route.c b/src/openvpn/route.c index ff39230..8a3e8b4 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -2991,16 +2991,12 @@ del_route_ipapi(const struct route_ipv4 *r, const struct tuntap *tt) static bool do_route_service(const bool add, const route_message_t *rt, const size_t size, HANDLE pipe) { - DWORD len; bool ret = false; ack_message_t ack; struct gc_arena gc = gc_new(); - if (!WriteFile(pipe, rt, size, &len, NULL) - || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL)) + if (!send_msg_iservice(pipe, rt, size, &ack, "ROUTE")) { - msg(M_WARN, "ROUTE: could not talk to service: %s [%lu]", - strerror_win32(GetLastError(), &gc), GetLastError()); goto out; } diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index a2d5315..948fd17 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -82,7 +82,6 @@ static DWORD get_adapter_index_flexible(const char *name); static bool do_address_service(const bool add, const short family, const struct tuntap *tt) { - DWORD len; bool ret = false; ack_message_t ack; struct gc_arena gc = gc_new(); @@ -115,11 +114,8 @@ do_address_service(const bool add, const short family, const struct tuntap *tt) addr.prefix_len = tt->netbits_ipv6; } - if (!WriteFile(pipe, &addr, sizeof(addr), &len, NULL) - || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL)) + if (!send_msg_iservice(pipe, &addr, sizeof(addr), &ack, "TUN")) { - msg(M_WARN, "TUN: could not talk to service: %s [%lu]", - strerror_win32(GetLastError(), &gc), GetLastError()); goto out; } @@ -141,7 +137,6 @@ out: static bool do_dns6_service(bool add, const struct tuntap *tt) { - DWORD len; bool ret = false; ack_message_t ack; struct gc_arena gc = gc_new(); @@ -185,11 +180,8 @@ do_dns6_service(bool add, const struct tuntap *tt) msg(D_LOW, "%s IPv6 dns servers on '%s' (if_index = %d) using service", (add ? "Setting" : "Deleting"), dns.iface.name, dns.iface.index); - if (!WriteFile(pipe, &dns, sizeof(dns), &len, NULL) - || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL)) + if (!send_msg_iservice(pipe, &dns, sizeof(dns), &ack, "TUN")) { - msg(M_WARN, "TUN: could not talk to service: %s [%lu]", - strerror_win32(GetLastError(), &gc), GetLastError()); goto out; } @@ -5222,11 +5214,8 @@ service_enable_dhcp(const struct tuntap *tt) .iface = { .index = tt->adapter_index, .name = "" } }; - if (!WriteFile(pipe, &dhcp, sizeof(dhcp), &len, NULL) - || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL)) + if (!send_msg_iservice(pipe, &dhcp, sizeof(dhcp), &ack, "Enable_dhcp")) { - msg(M_WARN, "Enable_dhcp: could not talk to service: %s [%lu]", - strerror_win32(GetLastError(), &gc), GetLastError()); goto out; } @@ -5461,18 +5450,16 @@ fork_dhcp_action(struct tuntap *tt) static void register_dns_service(const struct tuntap *tt) { - DWORD len; HANDLE msg_channel = tt->options.msg_channel; ack_message_t ack; struct gc_arena gc = gc_new(); message_header_t rdns = { msg_register_dns, sizeof(message_header_t), 0 }; - if (!WriteFile(msg_channel, &rdns, sizeof(rdns), &len, NULL) - || !ReadFile(msg_channel, &ack, sizeof(ack), &len, NULL)) + if (!send_msg_iservice(msg_channel, &rdns, sizeof(rdns), &ack, "Register_dns")) { - msg(M_WARN, "Register_dns: could not talk to service: %s [status=0x%lx]", - strerror_win32(GetLastError(), &gc), GetLastError()); + gc_free(&gc); + return; } else if (ack.error_number != NO_ERROR) @@ -5936,14 +5923,11 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun .iface = { .index = index, .name = "" } }; - if (!WriteFile(tt->options.msg_channel, &msg, sizeof(msg), &len, NULL) - || !ReadFile(tt->options.msg_channel, &ack, sizeof(ack), &len, NULL)) + if (send_msg_iservice(tt->options.msg_channel, &msg, sizeof(msg), + &ack, "TUN")) { - msg(M_WARN, "TUN: could not talk to service: %s [%lu]", - strerror_win32(GetLastError(), &gc), GetLastError()); + status = ack.error_number; } - - status = ack.error_number; } else { diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index 3905524..e43296e 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -1264,7 +1264,6 @@ win_get_tempdir(void) static bool win_block_dns_service(bool add, int index, const HANDLE pipe) { - DWORD len; bool ret = false; ack_message_t ack; struct gc_arena gc = gc_new(); @@ -1278,11 +1277,8 @@ win_block_dns_service(bool add, int index, const HANDLE pipe) .iface = { .index = index, .name = "" } }; - if (!WriteFile(pipe, &data, sizeof(data), &len, NULL) - || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL)) + if (!send_msg_iservice(pipe, &data, sizeof(data), &ack, "Block_DNS")) { - msg(M_WARN, "Block_DNS: could not talk to service: %s [%lu]", - strerror_win32(GetLastError(), &gc), GetLastError()); goto out; } @@ -1473,4 +1469,25 @@ win32_version_string(struct gc_arena *gc, bool add_name) return (const char *)out.data; } +bool +send_msg_iservice(HANDLE pipe, const void *data, size_t size, + ack_message_t *ack, const char *context) +{ + struct gc_arena gc = gc_new(); + DWORD len; + bool ret = true; + + if (!WriteFile(pipe, data, size, &len, NULL) + || !ReadFile(pipe, ack, sizeof(*ack), &len, NULL)) + { + msg(M_WARN, "%s: could not talk to service: %s [%lu]", + context? context : "Unknown", + strerror_win32(GetLastError(), &gc), GetLastError()); + ret = false; + } + + gc_free(&gc); + return ret; +} + #endif /* ifdef _WIN32 */ diff --git a/src/openvpn/win32.h b/src/openvpn/win32.h index 4b99a5e..b5cbe25 100644 --- a/src/openvpn/win32.h +++ b/src/openvpn/win32.h @@ -26,6 +26,7 @@ #define OPENVPN_WIN32_H #include "mtu.h" +#include "openvpn-msg.h" /* location of executables */ #define SYS_PATH_ENV_VAR_NAME "SystemRoot" /* environmental variable name that normally contains the system path */ @@ -307,5 +308,13 @@ int win32_version_info(void); */ const char *win32_version_string(struct gc_arena *gc, bool add_name); +/* + * Send the |size| bytes in buffer |data| to the interactive service |pipe| + * and read the result in |ack|. Returns false on communication error. + * The string in |context| is used to prefix error messages. + */ +bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, + ack_message_t *ack, const char *context); + #endif /* ifndef OPENVPN_WIN32_H */ #endif /* ifdef _WIN32 */