summaryrefslogtreecommitdiff
path: root/frontends/windows/login.c
blob: ca6c9163da832c0f71bb451b510c00c6edd017bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright 2018 Vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf 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; version 2 of the License.
*
* NetSurf 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * \file
 * This is The win32 API basic authentication login dialog implementation.
 */

#include <stdio.h>

#include "utils/config.h"

#include <windows.h>

#include "utils/log.h"
#include "utils/messages.h"
#include "utils/nsurl.h"
#include "desktop/version.h"

#include "windows/gui.h"
#include "windows/window.h"
#include "windows/login.h"
#include "windows/resourceid.h"

#include "windbg.h"

struct login_ctx {
	char *username;
	char *password;
	char *description;
	nserror (*cb)(const char *username, const char *password, void *cbctx);
	void *cbctx;
};


/**
 * free login dialog context
 */
static nserror
free_loginctx(struct login_ctx *ctx)
{
	free(ctx->username);
	free(ctx->password);
	free(ctx->description);
	free(ctx);

	return NSERROR_OK;
}


/**
 * generate the description of the login request
 */
static nserror
get_login_description(struct nsurl *url,
		      const char *realm,
		      char **out_str)
{
	char *url_s;
	size_t url_l;
	nserror res;
	const char *fmt = "The site %s is requesting your username and password. The realm is \"%s\"";
	char *str = NULL;
	int strlen;

	res = nsurl_get(url, NSURL_SCHEME | NSURL_HOST, &url_s, &url_l);
	if (res != NSERROR_OK) {
		return res;
	}

	strlen = snprintf(str, 0, fmt, url_s, realm) + 1;
	str = malloc(strlen);
	if (str == NULL) {
		res = NSERROR_NOMEM;
	} else {
		snprintf(str, strlen, fmt, url_s, realm);
		*out_str = str;
	}

	free(url_s);

	return res;
}

/**
 * win32 login dialog initialisation handler
 */
static BOOL
login_dialog_init(HWND hwndDlg, WPARAM wParam, LPARAM lParam)
{
	struct login_ctx *ctx;
	HWND hwndOwner;
	RECT rc, rcDlg, rcOwner;
	ctx = (struct login_ctx *)lParam;

	/* make context available in future calls */
	SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);

	/* set default contents */
	SetDlgItemText(hwndDlg, IDC_LOGIN_USERNAME, ctx->username);
	SetDlgItemText(hwndDlg, IDC_LOGIN_PASSWORD, ctx->password);
	SetDlgItemText(hwndDlg, IDC_LOGIN_DESCRIPTION, ctx->description);

	/* Get the owner window and dialog box rectangles. */
	if ((hwndOwner = GetParent(hwndDlg)) == NULL) {
		hwndOwner = GetDesktopWindow();
	}

	GetWindowRect(hwndOwner, &rcOwner);
	GetWindowRect(hwndDlg, &rcDlg);
	CopyRect(&rc, &rcOwner);

	/* Offset the owner and dialog box rectangles so that right
	 * and bottom values represent the width and height, and then
	 * offset the owner again to discard space taken up by the
	 * dialog box.
	 */

	OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
	OffsetRect(&rc, -rc.left, -rc.top);
	OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

	/* The new position is the sum of half the remaining space and
	 * the owner's original position.
	 */
	SetWindowPos(hwndDlg,
		     HWND_TOP,
		     rcOwner.left + (rc.right / 2),
		     rcOwner.top + (rc.bottom / 2),
		     0, 0,          /* Ignores size arguments. */
		     SWP_NOSIZE);

	/* ensure username gets focus */
	if (GetDlgCtrlID((HWND) wParam) != IDC_LOGIN_USERNAME) {
		SetFocus(GetDlgItem(hwndDlg, IDC_LOGIN_USERNAME));
		return FALSE;
	}

	return TRUE;
}


/**
 * win32 login dialog ok handler
 */
static BOOL
login_dialog_ok(HWND hwndDlg, struct login_ctx *ctx)
{
	char username[255];
	char password[255];

	if (GetDlgItemText(hwndDlg,
			   IDC_LOGIN_USERNAME,
			   username,
			   sizeof(username)) == 0) {
		username[0]=0;
	}

	if (GetDlgItemText(hwndDlg,
			   IDC_LOGIN_PASSWORD,
			   password,
			   sizeof(password)) == 0) {
		password[0]=0;
	}

	NSLOG(netsurf, DEBUG,
	      "context %p, user:\"%s\" pw:\"%s\"", ctx, username, password);

	ctx->cb(username, password, ctx->cbctx);

	DestroyWindow(hwndDlg);

	nsw32_del_dialog(hwndDlg);

	free_loginctx(ctx);

	return TRUE;
}


/**
 * win32 login dialog cancel handler
 */
static BOOL
login_dialog_cancel(HWND hwndDlg, struct login_ctx *ctx)
{
	NSLOG(netsurf, DEBUG, "context %p", ctx);

	ctx->cb(NULL, NULL, ctx->cbctx);

	DestroyWindow(hwndDlg);

	nsw32_del_dialog(hwndDlg);

	free_loginctx(ctx);

	return TRUE;
}


/**
 * win32 API callback for login dialog
 */
static BOOL CALLBACK
login_dialog_callback(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	struct login_ctx *ctx;

	LOG_WIN_MSG(hwndDlg, message, wParam, lParam);

	/* obtain login dialog context */
	ctx = (struct login_ctx *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);

	switch (message) {
	case WM_INITDIALOG:
		return login_dialog_init(hwndDlg, wParam, lParam);

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
			return login_dialog_ok(hwndDlg, ctx);

		case IDCANCEL:
			return login_dialog_cancel(hwndDlg, ctx);
		}
	}
	return FALSE;
}


/**
 * Request credentials for http login
 */
nserror
nsw32_401login(nsurl *url,
	       const char *realm,
	       const char *username,
	       const char *password,
	       nserror (*cb)(const char *username,
			     const char *password,
			     void *cbctx),
	       void *cbctx)
{
	HWND hwndDlg;
	struct login_ctx *nctx;
	struct gui_window *gw;
	nserror res;

	/* locate parent window */
	gw = nsws_get_gui_window(GetActiveWindow());
	if (gw == NULL) {
		return NSERROR_INIT_FAILED;
	}

	/* setup context for parameters */
	nctx = calloc(1, sizeof(struct login_ctx));
	if (nctx == NULL) {
		return NSERROR_NOMEM;
	}

	nctx->username = strdup(username);
	nctx->password = strdup(password);
	nctx->cb = cb;
	nctx->cbctx = cbctx;

	res = get_login_description(url, realm, &nctx->description);
	if (res != NSERROR_OK) {
		free_loginctx(nctx);
		return res;
	}

	/* create modeless dialog */
	hwndDlg = CreateDialogParam(NULL,
				    MAKEINTRESOURCE(IDD_LOGIN),
				    gw->main,
				    login_dialog_callback,
				    (LPARAM)nctx);

	nsw32_add_dialog(hwndDlg);

	return NSERROR_OK;
}