CredImport finished and renamed to CredWrite

This commit is contained in:
Simon Rozman 2016-05-17 14:47:41 +02:00
parent fda660a752
commit ef1e46aa78
12 changed files with 139 additions and 67 deletions

View File

@ -1,57 +0,0 @@
/*
Copyright 2015-2016 Amebis
Copyright 2016 GÉANT
This file is part of GEANTLink.
GEANTLink 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.
GEANTLink 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 GEANTLink. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
using namespace std;
using namespace winstd;
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);
int nArgs;
unique_ptr<LPWSTR[], LocalFree_delete<LPWSTR[]> > pwcArglist(CommandLineToArgvW(GetCommandLineW(), &nArgs));
if (pwcArglist == NULL) {
OutputDebugStr(_T("CommandLineToArgvW failed (error %i).\n"), GetLastError());
return 1;
}
if (nArgs < 3) {
OutputDebugStr(_T("Not enough parameters.\n"));
return -1;
}
// Decode password (Base64 >> UTF-8 >> UTF-16).
sanitizing_vector<char> password_utf8;
{
base64_dec dec;
bool is_last;
dec.decode(password_utf8, is_last, pwcArglist[2], (size_t)-1);
}
sanitizing_wstring password;
MultiByteToWideChar(CP_UTF8, 0, password_utf8.data(), (int)password_utf8.size(), password);
return 0;
}

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>..\include;..\lib\WinStd\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\lib\WinStd\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />

View File

@ -21,7 +21,7 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{2D3CE079-7EB1-4F47-B79E-F0310671ECCB}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>CredImport</RootNamespace>
<RootNamespace>CredWrite</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -53,25 +53,25 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\include\Win32.props" />
<Import Project="..\include\Debug.props" />
<Import Project="CredImport.props" />
<Import Project="CredWrite.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\include\x64.props" />
<Import Project="..\include\Debug.props" />
<Import Project="CredImport.props" />
<Import Project="CredWrite.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\include\Win32.props" />
<Import Project="..\include\Release.props" />
<Import Project="CredImport.props" />
<Import Project="CredWrite.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\include\x64.props" />
<Import Project="..\include\Release.props" />
<Import Project="CredImport.props" />
<Import Project="CredWrite.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
@ -95,7 +95,7 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="CredImport.rc" />
<ResourceCompile Include="CredWrite.rc" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\lib\WinStd\build\WinStd.vcxproj">

View File

@ -28,7 +28,7 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="CredImport.rc">
<ResourceCompile Include="CredWrite.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>

106
CredWrite/Main.cpp Normal file
View File

@ -0,0 +1,106 @@
/*
Copyright 2015-2016 Amebis
Copyright 2016 GÉANT
This file is part of GEANTLink.
GEANTLink 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.
GEANTLink 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 GEANTLink. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
using namespace std;
using namespace winstd;
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);
int nArgs;
unique_ptr<LPWSTR[], LocalFree_delete<LPWSTR[]> > pwcArglist(CommandLineToArgvW(GetCommandLineW(), &nArgs));
if (pwcArglist == NULL) {
OutputDebugStr(_T("CommandLineToArgvW failed (error %u).\n"), GetLastError());
return 1;
}
if (nArgs < 3) {
OutputDebugStr(_T("Not enough parameters.\n"));
return -1;
}
// Generate target name (aka realm).
tstring target_name(_T(PRODUCT_NAME_STR) _T("/"));
if (nArgs > 3) {
// User explicitly set the realm.
target_name += pwcArglist[3];
} else {
// Get the realm from user name.
LPCTSTR domain = _tcschr(pwcArglist[1], _T('@'));
target_name += domain ? ++domain : _T("*");
}
assert(target_name.length() < CRED_MAX_GENERIC_TARGET_NAME_LENGTH);
// Prepare password.
string password_enc_utf8;
{
// Convert Base64 >> UTF-8.
sanitizing_vector<char> password_utf8;
base64_dec dec;
bool is_last;
dec.decode(password_utf8, is_last, pwcArglist[2], (size_t)-1);
// Convert UTF-8 >> UTF-16.
sanitizing_wstring password;
MultiByteToWideChar(CP_UTF8, 0, password_utf8.data(), (int)password_utf8.size(), password);
// Encrypt the password.
wstring password_enc;
CRED_PROTECTION_TYPE cpt;
if (!CredProtect(TRUE, password.data(), (DWORD)password.size(), password_enc, &cpt)) {
OutputDebugStr(_T("CredProtect failed (error %u).\n"), GetLastError());
return 2;
}
// Convert UTF-16 >> UTF-8.
WideCharToMultiByte(CP_UTF8, 0, password_enc.data(), (int)password_enc.size(), password_enc_utf8, NULL, NULL);
}
assert(password_enc_utf8.size()*sizeof(char) < CRED_MAX_CREDENTIAL_BLOB_SIZE);
// Write credentials.
CREDENTIAL cred = {
0, // Flags
CRED_TYPE_GENERIC, // Type
(LPWSTR)target_name.c_str(), // TargetName
_T(""), // Comment
{ 0, 0 }, // LastWritten
(DWORD)password_enc_utf8.size()*sizeof(char), // CredentialBlobSize
(LPBYTE)password_enc_utf8.data(), // CredentialBlob
CRED_PERSIST_ENTERPRISE, // Persist
0, // AttributeCount
NULL, // Attributes
NULL, // TargetAlias
pwcArglist[1] // UserName
};
if (!CredWrite(&cred, 0)) {
OutputDebugStr(_T("CredWrite failed (error %u).\n"), GetLastError());
return 3;
}
return 0;
}

20
CredWrite/README.md Normal file
View File

@ -0,0 +1,20 @@
#CredWrite
Imports given credentials to Windows Credential Manager for GEANTLink use
##Usage
```
CredWrite <username> <password> [<realm>]
```
- `username` - a user name usually of the form user@domain
- `password` - Base64 encoded UTF-8 user password
- `realm` - A realm ID to allow grouping of credentials over different WLAN profiles (optional, default is domain part of `username`)
The credentials are stored to Windows Credential Manager in invoking user's roaming profile.
Return codes:
- -1 = Invalid parameters
- 0 = Success
- 1 = Error parsing command line
- 2 = Error encrypting password
- 3 = Error writing credentials to Credential Manager

View File

@ -20,7 +20,10 @@
#pragma once
#include "../include/Version.h"
#include <WinStd/Base64.h>
#include <WinStd/Cred.h>
#include <WinStd/Win.h>
#include <tchar.h>

View File

@ -16,7 +16,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinStd", "lib\WinStd\build\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Utilities", "Utilities", "{7B5EC9B7-208C-426A-941D-DAF9271BD4A4}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CredImport", "CredImport\CredImport.vcxproj", "{2D3CE079-7EB1-4F47-B79E-F0310671ECCB}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CredWrite", "CredWrite\CredWrite.vcxproj", "{2D3CE079-7EB1-4F47-B79E-F0310671ECCB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

@ -1 +1 @@
Subproject commit 2cb830b341ea49e9c69ec332b060e165ad75e1a7
Subproject commit f6029b2f04f52ae2b692980cb4d5734031568d91