From patchwork Tue Oct 2 10:01:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 476 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director11.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id cF92FOXOs1vqQwAAIUCqbw for ; Tue, 02 Oct 2018 16:02:45 -0400 Received: from proxy16.mail.iad3b.rsapps.net ([172.31.255.6]) by director11.mail.ord1d.rsapps.net with LMTP id SFMWEuXOs1tKWQAAvGGmqA ; Tue, 02 Oct 2018 16:02:45 -0400 Received: from smtp5.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy16.mail.iad3b.rsapps.net with LMTP id 8CCmDOXOs1s+TwAAPj+4aA ; Tue, 02 Oct 2018 16:02:45 -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: smtp5.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: 2021d820-c67e-11e8-9ce7-525400155d63-1-1 Received: from [216.105.38.7] ([216.105.38.7:25403] helo=lists.sourceforge.net) by smtp5.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id DD/6F-04066-4EEC3BB5; Tue, 02 Oct 2018 16:02:45 -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 1g7QrI-0003hh-0j; Tue, 02 Oct 2018 20:01:28 +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 1g7QrG-0003hb-6z for openvpn-devel@lists.sourceforge.net; Tue, 02 Oct 2018 20:01:26 +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=m3GHJIMJIibaJAtZMGsD2rvHeZPk+aA2zQTTwqurVB0=; b=CS1F5jxtCe2quJQylQqhH0K8HD RGzONru4CzK2+05tZUSLAXJQnYxgvkgbfAWlp6Oz69UvB9C5u2CfcGe/oZ1PYL+m1ydGRYD0y2VjY m0prZiLn8VL0E2efeC6t0hngRN9HSfk5SWHaQbCWill3QW5MsL99tY3CjsTsa2z0rQFc=; 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=m3GHJIMJIibaJAtZMGsD2rvHeZPk+aA2zQTTwqurVB0=; b=YGFi1+W//0s1tgErYcUDyfzDwG tZc8CbIbvOT+EcXNs3r0uqyH/Ti4bMhHNXa31fxcGuFuv/IXm30BNYGmiRaQi2hM1KidxwRxFDAmo 957POs4n09UQU9RxCTWRPjw65ES6VDJkcQyVesJGpAIQD46qZPmTVtJmIpBiImjTXMcQ=; Received: from mail-io1-f44.google.com ([209.85.166.44]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) id 1g7QrE-00A8Pk-G4 for openvpn-devel@lists.sourceforge.net; Tue, 02 Oct 2018 20:01:26 +0000 Received: by mail-io1-f44.google.com with SMTP id q4-v6so3126229iob.8 for ; Tue, 02 Oct 2018 13:01:24 -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=m3GHJIMJIibaJAtZMGsD2rvHeZPk+aA2zQTTwqurVB0=; b=ET3J8jJHvbPlasmHHY51TLUmtDCPWBWUa9sKeoxuWjVrDtYi8y1/BliFhVcdpD+fkD uxje5zsNfu0VJuMQkn5pHkjE1RqNxEVhkXjE8bQlUwTur+EsbkYUh7tdFeUsywJ+kTCZ 7x87jDsyvpOgLMx15Z4iBV/wxUlUtEak/bq9BdOrx+ZMsbYOGMva97nEVPzieSlg0N6P KPRCyuuIYuF6Gthqd9OXGeDqRpiss/mBqxpb1Vrjg5TwB+XOWwa5ytiv+jpo5pUGCidH NrjuXRWEwiDu4sZrYdDc1AgMuwzETkwjLzZpmfOmrGsu6PJMrzfGXf3mJ/aamzTMavM+ XWHA== 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=m3GHJIMJIibaJAtZMGsD2rvHeZPk+aA2zQTTwqurVB0=; b=ahrm1jZl1ZVV03FK3kd4XweQJP9MpV5Uww5GEdSofVeAAXUm93M+AJxTghGim2KUdq kh+Ha58YRupprxsCvwzC+f/2hTkpBkwYECUPiFo7wpbQHP/4pvCWY2Ix6vu6FB2ENCHT 7IzvpEkf7f2bI5XvETRfaOi0okoAfReToQFKe4rdzDvyW69BO6SnJtwnCWwL/4+trD68 kJEOxxnp3W03/sxur2SLdF2wE7P11VRR94KgJjGO3p8ZiRNyWiHpIYsIKtvw5VznkMuY Ty3znFcS60O4DoKTAeeQjLnzPtbRGPZMgPXzqhFIhPy17l/thFkvJOYSI/gwolxTFeDI +i7g== X-Gm-Message-State: ABuFfoiur1/KHZi/IsbaITE8sj6Rq/7EZK2FtOaa9yZ9kT4/6g2uIo4z mfCQe3sMXDYXjj03nkKSQPLiu2b7Ta8= X-Google-Smtp-Source: ACcGV618rGKgi6WyElRfShbkFuGnuKiNCrNteomly1Lrm35qJ3xbpZBzPPCDZ5921ptzELfiKLYM+g== X-Received: by 2002:a6b:3451:: with SMTP id b78-v6mr11684369ioa.214.1538510478458; Tue, 02 Oct 2018 13:01:18 -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.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Oct 2018 13:01:17 -0700 (PDT) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Tue, 2 Oct 2018 16:01:12 -0400 Message-Id: <1538510474-27602-1-git-send-email-selva.nair@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1538229228-10620-1-git-send-email-selva.nair@gmail.com> References: <1538229228-10620-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.44 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 X-Headers-End: 1g7QrE-00A8Pk-G4 Subject: [Openvpn-devel] [PATCH 1/3] Move get system directory to a separate function 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 Only refactoring to reduce code-duplication, no functional changes. Signed-off-by: Selva Nair --- src/openvpnserv/common.c | 16 +++++++++++++++- src/openvpnserv/interactive.c | 23 ++++++----------------- src/openvpnserv/service.h | 3 +++ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/openvpnserv/common.c b/src/openvpnserv/common.c index dc47666..73c418f 100644 --- a/src/openvpnserv/common.c +++ b/src/openvpnserv/common.c @@ -25,7 +25,7 @@ #include "validate.h" LPCTSTR service_instance = TEXT(""); - +static wchar_t win_sys_path[MAX_PATH]; /* * These are necessary due to certain buggy implementations of (v)snprintf, @@ -285,3 +285,17 @@ utf8to16(const char *utf8) MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, n); return utf16; } + +const wchar_t * +get_win_sys_path(void) +{ + const wchar_t *default_sys_path = L"C:\\Windows\\system32"; + + if (!GetSystemDirectoryW(win_sys_path, _countof(win_sys_path))) + { + wcsncpy(win_sys_path, default_sys_path, _countof(win_sys_path)); + win_sys_path[_countof(win_sys_path) - 1] = L'\0'; + } + + return win_sys_path; +} diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index 861f5e7..0489684 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -934,11 +934,10 @@ RegisterDNS(LPVOID unused) { DWORD err; DWORD i; - WCHAR sys_path[MAX_PATH]; DWORD timeout = RDNS_TIMEOUT * 1000; /* in milliseconds */ - /* default path of ipconfig command */ - WCHAR ipcfg[MAX_PATH] = L"C:\\Windows\\system32\\ipconfig.exe"; + /* path of ipconfig command */ + WCHAR ipcfg[MAX_PATH]; struct { @@ -953,11 +952,8 @@ RegisterDNS(LPVOID unused) HANDLE wait_handles[2] = {rdns_semaphore, exit_event}; - if (GetSystemDirectory(sys_path, MAX_PATH)) - { - swprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe"); - ipcfg[MAX_PATH-1] = L'\0'; - } + swprintf(ipcfg, _countof(ipcfg), L"%s\\%s", get_win_sys_path(), L"ipconfig.exe"); + ipcfg[_countof(ipcfg) - 1] = L'\0'; if (WaitForMultipleObjects(2, wait_handles, FALSE, timeout) == WAIT_OBJECT_0) { @@ -1035,15 +1031,8 @@ netsh_dns_cmd(const wchar_t *action, const wchar_t *proto, const wchar_t *if_nam } /* 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); - } + swprintf(argv0, _countof(argv0), L"%s\\%s", get_win_sys_path(), L"netsh.exe"); + argv0[_countof(argv0) - 1] = L'\0'; /* cmd template: * netsh interface $proto $action dns $if_name $addr [validate=no] diff --git a/src/openvpnserv/service.h b/src/openvpnserv/service.h index af8f37f..23b105f 100644 --- a/src/openvpnserv/service.h +++ b/src/openvpnserv/service.h @@ -96,4 +96,7 @@ DWORD MsgToEventLog(DWORD flags, LPCTSTR lpszMsg, ...); /* Convert a utf8 string to utf16. Caller should free the result */ wchar_t *utf8to16(const char *utf8); +/* return windows system directory as a pointer to a static string */ +const wchar_t *get_win_sys_path(void); + #endif /* ifndef _SERVICE_H */