diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e1d7ca3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/*.sdf +/*.suo +/Debug +/ipch +/Release +/x64 +/*.opensdf diff --git a/Examples/EAP-TLS Userdata.xml b/Examples/EAP-TLS Userdata.xml new file mode 100644 index 0000000..90cd7c1 Binary files /dev/null and b/Examples/EAP-TLS Userdata.xml differ diff --git a/README.md b/README.md index 354729f..90ff2a1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,30 @@ # WLANSetEAPUserData -Windows helper utility to set user data for a given WLAN connection +Windows helper utility to set user data for a given WLAN profile + +##Usage +``` +WLANSetEAPUserData +``` + +##Parameters +Parameter | Explanation +----------------|------------ +`profile` | The name of the network profile (not neccessarely the same as SSID) +`flags` | Flags to pass to `WlanSetProfileEapXmlUserData()` function call (decimal number: 0=Current User, 1=All Users) +`user data URI` | User data XML URI. Can be a path to an XML file, web URL where user data XML can be loaded from, etc. + +##Return codes +Value | Meaning +------|-------- + 0 | Success (on at least one WLAN interface) + 100 | CommandLineToArgvW() failed + 101 | Not enough arguments + 200 | CoInitialize() failed + 300 | CoCreateInstance(CLSID_DOMDocument2) + 301 | IXMLDOMDocument::load() failed + 302 | IXMLDOMDocument::load() reported an error in the XML document + 304 | IXMLDOMDocument::get_xml() failed + 400 | WlanOpenHandle() failed + 401 | WlanEnumInterfaces() failed + 402 | WlanSetProfileEapXmlUserData() failed on all WLAN interfaces for the given profile + 403 | No ready WLAN interfaces found diff --git a/WLANSetEAPUserData.sln b/WLANSetEAPUserData.sln new file mode 100644 index 0000000..4e15c01 --- /dev/null +++ b/WLANSetEAPUserData.sln @@ -0,0 +1,26 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WLANSetEAPUserData", "WLANSetEAPUserData\WLANSetEAPUserData.vcxproj", "{EC3084A5-CA18-498B-A962-5321AE37789B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|Win32.ActiveCfg = Debug|Win32 + {EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|Win32.Build.0 = Debug|Win32 + {EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|x64.ActiveCfg = Debug|x64 + {EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|x64.Build.0 = Debug|x64 + {EC3084A5-CA18-498B-A962-5321AE37789B}.Release|Win32.ActiveCfg = Release|Win32 + {EC3084A5-CA18-498B-A962-5321AE37789B}.Release|Win32.Build.0 = Release|Win32 + {EC3084A5-CA18-498B-A962-5321AE37789B}.Release|x64.ActiveCfg = Release|x64 + {EC3084A5-CA18-498B-A962-5321AE37789B}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/WLANSetEAPUserData/.gitignore b/WLANSetEAPUserData/.gitignore new file mode 100644 index 0000000..bfa1472 --- /dev/null +++ b/WLANSetEAPUserData/.gitignore @@ -0,0 +1,5 @@ +/*.user +/Debug +/Release +/x64 +/*.aps diff --git a/WLANSetEAPUserData/WLANSetEAPUserData.cpp b/WLANSetEAPUserData/WLANSetEAPUserData.cpp new file mode 100644 index 0000000..1979368 --- /dev/null +++ b/WLANSetEAPUserData/WLANSetEAPUserData.cpp @@ -0,0 +1,157 @@ +/* + Copyright 2016 Simon Rozman + + This file is part of WLANSetEAPUserData. + + WLANSetEAPUserData is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + WLANSetEAPUserData is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with WLANSetEAPUserData. If not, see . +*/ + +#include "WLANSetEAPUserData.h" +#include +#include +#include +#include +#include + +#ifdef _DEBUG +#pragma comment(lib, "comsuppwd.lib") +#else +#pragma comment(lib, "comsuppw.lib") +#endif +#pragma comment(lib, "msxml6.lib") +#pragma comment(lib, "Wlanapi.lib") + +using namespace std; + + +/// +/// Main program body +/// +int CALLBACK WinMain( + _In_ HINSTANCE hInstance, + _In_ HINSTANCE hPrevInstance, + _In_ LPSTR lpCmdLine, + _In_ int nCmdShow +) +{ + UNREFERENCED_PARAMETER(hInstance ); + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine ); + UNREFERENCED_PARAMETER(nCmdShow ); + + // Get command line arguments. (As Unicode, please.) + int nArgs; + unique_ptr > pwcArglist(CommandLineToArgvW(GetCommandLineW(), &nArgs)); + if (!pwcArglist) { + //_ftprintf(stderr, _T("CommandLineToArgvW() failed (error %u).\n"), GetLastError()); + return 100; + } + if (nArgs <= 3) { + //_ftprintf(stderr, _T("Not enough arguments.\n")); + return 101; + } + + // Initialize COM. + com_initializer com_init(NULL); + if (FAILED(com_init.status())) { + //_ftprintf(stderr, _T("CoInitialize() failed (error 0x%08x).\n"), com_init.status()); + return 200; + } + + // Load user data XML into memory. + // Use MSXML6 IXMLDOMDocument to load XML to offload charset detection. + _bstr_t user_data; + { + // Create XML document. + _com_ptr_t<_com_IIID > doc; + HRESULT hr = doc.CreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_ALL); + if (FAILED(hr)) { + //_ftprintf(stderr, _T("CoCreateInstance(CLSID_DOMDocument2) failed (error 0x%08x).\n"), hr); + return 300; + } + doc->put_async(VARIANT_FALSE); + doc->put_validateOnParse(VARIANT_FALSE); + + // Load XML from user data file. + VARIANT_BOOL succeeded = VARIANT_FALSE; + hr = doc->load(_variant_t(pwcArglist[3]), &succeeded); + if (FAILED(hr)) { + //_ftprintf(stderr, _T("IXMLDOMDocument::load(%ls) failed (error 0x%08x).\n"), pwcArglist[3], hr); + return 301; + } else if (!succeeded) { + //_ftprintf(stderr, _T("IXMLDOMDocument::load(%ls) failed.\n"), pwcArglist[3]); + return 302; + } + + // Get document XML. + BSTR bstr; + hr = doc->get_xml(&bstr); + if (FAILED(hr)) { + //_ftprintf(stderr, _T("IXMLDOMDocument::get_xml() failed (error 0x%08x).\n"), hr); + return 304; + } + user_data.Attach(bstr); + } + + // Open WLAN handle. + DWORD dwNegotiatedVersion; + unique_ptr wlan; + { + HANDLE hWlan; + DWORD dwResult = WlanOpenHandle(WLAN_API_MAKE_VERSION(2, 0), NULL, &dwNegotiatedVersion, &hWlan); + if (dwResult != ERROR_SUCCESS) { + //_ftprintf(stderr, _T("WlanOpenHandle() failed (error %u).\n"), dwResult); + return 400; + } + wlan.reset(hWlan); + } + + // Get a list of WLAN interfaces. + unique_ptr > interfaces; + { + WLAN_INTERFACE_INFO_LIST *pInterfaceList; + DWORD dwResult = WlanEnumInterfaces(wlan.get(), NULL, &pInterfaceList); + if (dwResult != ERROR_SUCCESS) { + //_ftprintf(stderr, _T("WlanEnumInterfaces() failed (error %u).\n"), dwResult); + return 401; + } + interfaces.reset(pInterfaceList); + } + + // Iterate over all WLAN interfaces. + bool success = false; + for (DWORD i = 0; i < interfaces->dwNumberOfItems; i++) { + if (interfaces->InterfaceInfo[i].isState == wlan_interface_state_not_ready) { + // This interface is not ready. + continue; + } + + // Set user data. + DWORD dwResult = WlanSetProfileEapXmlUserData( + wlan.get(), + &(interfaces->InterfaceInfo[i].InterfaceGuid), + pwcArglist[1], + wcstoul(pwcArglist[2], NULL, 10), + user_data, + NULL); + if (dwResult == ERROR_SUCCESS) { + // At least one interface/profile succeeded. + success = true; + } else { + //_ftprintf(stderr, _T("WlanSetProfileEapXmlUserData() failed (error %u).\n"), dwResult); + } + } + + return success ? 0 : interfaces->dwNumberOfItems ? 402 : 403; +} diff --git a/WLANSetEAPUserData/WLANSetEAPUserData.h b/WLANSetEAPUserData/WLANSetEAPUserData.h new file mode 100644 index 0000000..93d149f --- /dev/null +++ b/WLANSetEAPUserData/WLANSetEAPUserData.h @@ -0,0 +1,161 @@ +/* + Copyright 2016 Simon Rozman + + This file is part of WLANSetEAPUserData. + + WLANSetEAPUserData is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + WLANSetEAPUserData is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with WLANSetEAPUserData. If not, see . +*/ + +#pragma once + +#include + + +/// +/// Deleter for unique_ptr using LocalFree +/// +template +struct LocalFree_delete +{ + /// + /// Frees memory + /// + /// \sa [LocalFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730.aspx) + /// + inline void operator()(_Ty *_Ptr) const + { + LocalFree(_Ptr); + } +}; + + +/// +/// Deleter for unique_ptr to array of unknown size using LocalFree +/// +template +struct LocalFree_delete<_Ty[]> +{ + /// + /// Frees memory + /// + inline void operator()(_Ty *_Ptr) const + { + LocalFree(_Ptr); + } + + /// + /// Frees memory + /// + /// \sa [LocalFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730.aspx) + /// + template + inline void operator()(_Other *) const + { + LocalFree(_Ptr); + } +}; + + +#include + +/// +/// Context scope automatic COM (un)initialization +/// +class com_initializer +{ +public: + /// + /// Initializes the COM library on the current thread and identifies the concurrency model as single-thread apartment (STA). + /// + /// \sa [CoInitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543.aspx) + /// + inline com_initializer(_In_opt_ LPVOID pvReserved) + { + m_result = CoInitialize(pvReserved); + } + + + /// + /// Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required. + /// + /// \sa [CoInitializeEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms695279.aspx) + /// + inline com_initializer(_In_opt_ LPVOID pvReserved, _In_ DWORD dwCoInit) + { + m_result = CoInitializeEx(pvReserved, dwCoInit); + } + + + /// + /// Uninitializes COM. + /// + /// \sa [CoUninitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms688715.aspx) + /// + virtual ~com_initializer() + { + if (SUCCEEDED(m_result)) + CoUninitialize(); + } + + + /// + /// Return result of `CoInitialize()` call. + /// + /// \sa [CoInitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543.aspx) + /// + inline HRESULT status() const + { + return m_result; + } + +protected: + HRESULT m_result; ///< Result of CoInitialize call +}; + + +#include + +/// +/// Deleter for unique_ptr using WlanCloseHandle +/// +struct WlanCloseHandle_delete +{ + /// + /// Closes the WLAN handle + /// + /// \sa [WlanCloseHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706610.aspx) + /// + inline void operator()(void *_Ptr) const + { + WlanCloseHandle(_Ptr, NULL); + } +}; + + +/// +/// Deleter for unique_ptr using WlanFreeMemory +/// +template +struct WlanFreeMemory_delete +{ + /// + /// Frees memory + /// + /// \sa [WlanFreeMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706722.aspx) + /// + void operator()(_Ty *_Ptr) const + { + WlanFreeMemory(_Ptr); + } +}; diff --git a/WLANSetEAPUserData/WLANSetEAPUserData.rc b/WLANSetEAPUserData/WLANSetEAPUserData.rc new file mode 100644 index 0000000..3ebdcdd Binary files /dev/null and b/WLANSetEAPUserData/WLANSetEAPUserData.rc differ diff --git a/WLANSetEAPUserData/WLANSetEAPUserData.vcxproj b/WLANSetEAPUserData/WLANSetEAPUserData.vcxproj new file mode 100644 index 0000000..36c54a9 --- /dev/null +++ b/WLANSetEAPUserData/WLANSetEAPUserData.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {EC3084A5-CA18-498B-A962-5321AE37789B} + Win32Proj + WLANSetEAPUserData + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + true + Unicode + + + Application + false + true + Unicode + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level4 + Disabled + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + + + + + Level4 + + + Full + true + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + AnySuitable + Speed + true + + + Windows + true + true + true + + + + + Level4 + + + Full + true + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + AnySuitable + Speed + true + + + Windows + true + true + true + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WLANSetEAPUserData/WLANSetEAPUserData.vcxproj.filters b/WLANSetEAPUserData/WLANSetEAPUserData.vcxproj.filters new file mode 100644 index 0000000..081ed2c --- /dev/null +++ b/WLANSetEAPUserData/WLANSetEAPUserData.vcxproj.filters @@ -0,0 +1,35 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + Header Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/WLANSetEAPUserData/resource.h b/WLANSetEAPUserData/resource.h new file mode 100644 index 0000000..5e3aa0c --- /dev/null +++ b/WLANSetEAPUserData/resource.h @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by WLANSetEAPUserData.rc + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif