From da9ba5d86be2c99089a571e73d1d62317bac8393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= Date: Fri, 13 Aug 2021 22:08:01 +0200 Subject: [PATCH] git-extra: include an simple Win32 `git askpass` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a drop-in replacement for `git gui--askpass`. Since we added an option to use an external `ssh` found on the `PATH` (#367) we'll need an `askpass` implementation that can be called by any windows application even without understanding shebang lines. `git gui--askpass` probably also has the same problems with screenreaders that `git gui--askyesno` had(#234), so we'll likely get improved accessibility as a positive side-effect. Signed-off-by: Matthias Aßhauer --- git-extra/Makefile | 8 +++ git-extra/PKGBUILD | 11 +++- git-extra/git-askpass.c | 109 +++++++++++++++++++++++++++++++++++++++ git-extra/git-askpass.h | 3 ++ git-extra/git-askpass.rc | 18 +++++++ 5 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 git-extra/git-askpass.c create mode 100644 git-extra/git-askpass.h create mode 100644 git-extra/git-askpass.rc diff --git a/git-extra/Makefile b/git-extra/Makefile index 3dcbb270ec..935ad87350 100644 --- a/git-extra/Makefile +++ b/git-extra/Makefile @@ -7,6 +7,7 @@ CXXFLAGS ?= -Wall all: $(BUILDDIR)/create-shortcut.exe $(BUILDDIR)/WhoUses.exe \ $(BUILDDIR)/blocked-file-util.exe $(BUILDDIR)/proxy-lookup.exe \ $(BUILDDIR)/git-askyesno.exe \ + $(BUILDDIR)/git-askpass.exe \ $(BUILDDIR)/git-credential-helper-selector.exe $(BUILDDIR)/create-shortcut.exe: $(BUILDDIR)/create-shortcut.o @@ -44,6 +45,13 @@ $(BUILDDIR)/git-credential-helper-selector.exe: \ $(BUILDDIR)/git-credential-helper-selector.o \ $(BUILDDIR)/git-credential-helper-selector.res $(CC) -municode $(CFLAGS) -o $@ $^ -lgdi32 -lcomctl32 + +$(BUILDDIR)/git-askpass.o: CFLAGS += -DUNICODE + +$(BUILDDIR)/git-askpass.exe: \ + $(BUILDDIR)/git-askpass.o \ + $(BUILDDIR)/git-askpass.res + $(CC) -municode $(CFLAGS) -o $@ $^ clean: $(RM) $(BUILDDIR)/create-shortcut.exe $(BUILDDIR)/create-shortcut.o $(RM) $(BUILDDIR)/WhoUses.exe $(BUILDDIR)/WhoUses.o \ diff --git a/git-extra/PKGBUILD b/git-extra/PKGBUILD index 553bbcc71e..136f959a00 100644 --- a/git-extra/PKGBUILD +++ b/git-extra/PKGBUILD @@ -52,7 +52,10 @@ source=('inputrc' 'update-via-pacman.bat' 'git-credential-helper-selector.c' 'git-credential-helper-selector.manifest' - 'git-credential-helper-selector.rc') + 'git-credential-helper-selector.rc' + 'git-askpass.c' + 'git-askpass.h' + 'git-askpass.rc') sha256sums=('2a8b8630bc2b0c6667790d1ba11534cdf685fff58b026cc4e01b21a10085db1d' 'c26d22aaa1d3dc615d474e0d60c5c0f19598d61d9205e19ec87aac1b28bb07c1' '640d04d2a2da709419188a986d5e5550ad30df23c7ea9be1a389c37a6b917994' @@ -83,7 +86,10 @@ sha256sums=('2a8b8630bc2b0c6667790d1ba11534cdf685fff58b026cc4e01b21a10085db1d' '25c4cff9fa0d82d87ce7f9cf34d2558dd051a398d33e6c3440e9371ebb72471e' 'febd9504ed7a9e07771d9ae35ddface253a6160a8df9f4c18e4111eb45386712' '247dc84535c89d2e00c6a741d4317cd76ae5958c91ac867e2e74d4e73bc3c998' - '78307cd6c04a16240f68197e9155697bc75d5df8e4c8514ff67712bb93a1cd4a') + '78307cd6c04a16240f68197e9155697bc75d5df8e4c8514ff67712bb93a1cd4a' + 'f2f64cc0d49598089a84de72ce78f3e5949f6c2d6c09a499d3c3760fcf6aef57' + '51e31c6ce824f66b9a8310f1ac10ccdc06b60967a557c996868bc0d9c9866ccf' + '0dd30dc3acd406e70b5ff08fb0bf180ec3782119455989af18c05cf59d09df64') prepare() { test $startdir/$pkgname.install -nt $startdir/$pkgname.install.in && @@ -128,6 +134,7 @@ package() { install -m755 $builddir/proxy-lookup.exe $pkgdir${MINGW_PREFIX}/bin install -m755 $builddir/git-askyesno.exe $pkgdir${MINGW_PREFIX}/bin install -m755 $builddir/git-credential-helper-selector.exe $pkgdir${MINGW_PREFIX}/bin + install -m755 $builddir/git-askpass.exe $pkgdir${MINGW_PREFIX}/bin install -m755 git-prompt.sh $pkgdir/etc/profile.d install -m755 aliases.sh $pkgdir/etc/profile.d install -m755 env.sh $pkgdir/etc/profile.d diff --git a/git-extra/git-askpass.c b/git-extra/git-askpass.c new file mode 100644 index 0000000000..2975d07fd2 --- /dev/null +++ b/git-extra/git-askpass.c @@ -0,0 +1,109 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include "git-askpass.h" + +INT_PTR CALLBACK PasswordProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) +{ + wchar_t *lpszPassword = NULL; + WORD cchPassword; + + switch (message) + { + case WM_INITDIALOG: + /* Display the prompt */ + SetDlgItemTextW(hDlg, IDC_PROMPT, (wchar_t *) lParam); + if (GetDlgCtrlID((HWND) wParam) != IDE_PASSWORDEDIT) { + SetFocus(GetDlgItem(hDlg, IDE_PASSWORDEDIT)); + return FALSE; + } + return TRUE; + case WM_COMMAND: + switch(wParam) + { + case IDOK: + /* Get number of characters. */ + cchPassword = (WORD) SendDlgItemMessage(hDlg, + IDE_PASSWORDEDIT, + EM_LINELENGTH, + (WPARAM) 0, + (LPARAM) 0); + + lpszPassword = (wchar_t *) malloc(sizeof(wchar_t) * (cchPassword + 1)); + if (!lpszPassword) { + MessageBoxW(NULL, L"Out of memory asking for a password", L"Error!", MB_OK); + EndDialog(hDlg, FALSE); + return TRUE; + } + /* Put the number of characters into first word of buffer. */ + *((LPWORD)lpszPassword) = cchPassword; + + /* Get the characters. */ + SendDlgItemMessageW(hDlg, + IDE_PASSWORDEDIT, + EM_GETLINE, + (WPARAM) 0, /* line 0 */ + (LPARAM) lpszPassword); + + /* Null-terminate the string. */ + lpszPassword[cchPassword] = 0; + + wprintf(L"%ls\n", lpszPassword); + + EndDialog(hDlg, TRUE); + free(lpszPassword); + return TRUE; + + case IDCANCEL: + EndDialog(hDlg, FALSE); + return TRUE; + } + return 0; + } + return FALSE; + + UNREFERENCED_PARAMETER(lParam); +} + +int wmain(int argc, wchar_t **wargv) +{ + INT_PTR res; + wchar_t *prompt = NULL; + + if (argc < 2) { + MessageBoxW(NULL, L"Usage: git askpass ", L"Error!", MB_OK); + return 1; + } + + if (argc > 2) { + size_t count = wcslen(wargv[1]), i; + + for (i = 2; i < argc; i++) + count += 1 + wcslen(wargv[i]); + + prompt = malloc((count + 1) * sizeof(*prompt)); + if (!prompt) { + MessageBoxW(NULL, L"Out of memory asking for a password", L"Error!", MB_OK); + return 1; + } + + wcscpy(prompt, wargv[1]); + count = wcslen(wargv[1]); + + for (i = 2; i < argc; i++) { + prompt[count++] = L' '; + wcscpy(prompt + count, wargv[i]); + count += wcslen(wargv[i]); + } + } + + res = DialogBoxParamW(NULL, /* application instance */ + MAKEINTRESOURCEW(IDD_PASSWORD), /* dialog box resource */ + NULL, /* owner window */ + PasswordProc, /* dialog box window procedure */ + (LPARAM) (prompt ? prompt : wargv[1])); + + free(prompt); + return !res; +} diff --git a/git-extra/git-askpass.h b/git-extra/git-askpass.h new file mode 100644 index 0000000000..770652d810 --- /dev/null +++ b/git-extra/git-askpass.h @@ -0,0 +1,3 @@ +#define IDD_PASSWORD 129 +#define IDE_PASSWORDEDIT 1000 +#define IDC_PROMPT 1001 diff --git a/git-extra/git-askpass.rc b/git-extra/git-askpass.rc new file mode 100644 index 0000000000..60f29f03f9 --- /dev/null +++ b/git-extra/git-askpass.rc @@ -0,0 +1,18 @@ +#include "git-askpass.h" +#include "windows.h" + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_PASSWORD DIALOGEX 0, 0, 253, 94 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION +CAPTION "Git for Windows" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + DEFPUSHBUTTON "OK",IDOK,141,75,50,14 + PUSHBUTTON "Cancel",IDCANCEL,195,75,50,14 + EDITTEXT IDE_PASSWORDEDIT,7,43,239,14,ES_PASSWORD | ES_AUTOHSCROLL + LTEXT "Enter Password",IDC_PROMPT,7,7,239,32,SS_NOPREFIX +END