Fixed numbering
[TortoiseGit.git] / src / SshAskPass / SshAskPass.cpp
blobb070efaec3c48be6e8db3df1dbbb581f2ae02167
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // SshAskPass.cpp : Defines the entry point for the application.
23 #include "stdafx.h"
24 #include "SshAskPass.h"
25 #include <stdio.h>
26 #include "propsys.h"
27 #include "PropKey.h"
28 #include <atlbase.h>
30 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
32 #define MAX_LOADSTRING 100
34 // Global Variables:
35 HINSTANCE hInst; // current instance
36 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
37 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
39 // Forward declarations of functions included in this code module:
40 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
42 TCHAR g_Promptphrase[] = _T("Enter your OpenSSH passphrase:");
43 TCHAR *g_Prompt = NULL;
45 TCHAR g_PassWord[MAX_LOADSTRING];
47 int APIENTRY _tWinMain(HINSTANCE /*hInstance*/,
48 HINSTANCE /*hPrevInstance*/,
49 LPTSTR lpCmdLine,
50 int /*nCmdShow*/)
52 SetDllDirectory(L"");
54 if( _tcslen(lpCmdLine) == 0 )
56 g_Prompt = g_Promptphrase;
58 else
60 g_Prompt = lpCmdLine;
63 TCHAR *yesno=_T("(yes/no)");
64 size_t lens = _tcslen(yesno);
65 TCHAR *p = lpCmdLine;
66 BOOL bYesNo=FALSE;
68 while(*p)
70 if (_tcsncicmp(p, yesno, lens) == 0)
72 bYesNo = TRUE;
73 break;
75 p++;
78 if(bYesNo)
80 if (::MessageBox(NULL, lpCmdLine, _T("TortoiseGit - git CLI stdin wrapper"), MB_YESNO|MB_ICONQUESTION) == IDYES)
82 _tprintf(_T("yes"));
84 else
86 _tprintf(_T("no"));
88 return 0;
90 else
92 if(DialogBox(hInst, MAKEINTRESOURCE(IDD_ASK_PASSWORD), NULL, About) == IDOK)
94 _tprintf(_T("%s\n"), g_PassWord);
95 return 0;
97 _tprintf(_T("\n"));
98 return -1;
102 void MarkWindowAsUnpinnable(HWND hWnd)
104 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
106 HMODULE hShell = AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
108 if (hShell) {
109 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
110 if (pfnSHGPSFW) {
111 IPropertyStore *pps;
112 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
113 if (SUCCEEDED(hr)) {
114 PROPVARIANT var;
115 var.vt = VT_BOOL;
116 var.boolVal = VARIANT_TRUE;
117 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
118 pps->Release();
121 FreeLibrary(hShell);
125 // Message handler for password box.
126 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM /*lParam*/)
128 switch (message)
130 case WM_INITDIALOG:
132 MarkWindowAsUnpinnable(hDlg);
133 RECT rect;
134 ::GetWindowRect(hDlg,&rect);
135 DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
136 DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
138 DWORD x,y;
139 x=(dwWidth - (rect.right-rect.left))/2;
140 y=(dwHeight - (rect.bottom-rect.top))/2;
142 ::MoveWindow(hDlg,x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
143 HWND title=::GetDlgItem(hDlg,IDC_STATIC_TITLE);
144 ::SetWindowText(title,g_Prompt);
146 TCHAR *pass =_T("pass");
147 size_t passlens = _tcslen(pass);
148 TCHAR *p = g_Prompt;
149 bool password = false;
150 while (*p)
152 if (_tcsncicmp(p, pass, passlens) == 0)
154 password = true;
155 break;
157 p++;
159 if (!password)
160 SendMessage(::GetDlgItem(hDlg, IDC_PASSWORD), EM_SETPASSWORDCHAR, 0, 0);
162 return (INT_PTR)TRUE;
164 case WM_COMMAND:
166 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
168 if( LOWORD(wParam) == IDOK )
170 HWND password = ::GetDlgItem(hDlg,IDC_PASSWORD);
171 ::GetWindowText(password,g_PassWord,MAX_LOADSTRING);
173 EndDialog(hDlg, LOWORD(wParam));
174 return (INT_PTR)TRUE;
176 break;
178 return (INT_PTR)FALSE;