Develop and Download Open Source Software

Browse Subversion Repository

Contents of /branches/ssh_chacha20poly1305/ttssh2/ttxssh/auth.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2739 - (show annotations) (download) (as text)
Wed Dec 1 15:37:49 2004 UTC (19 years, 4 months ago) by yutakakn
Original Path: ttssh2/trunk/ttxssh/auth.c
File MIME type: text/x-csrc
File size: 25165 byte(s)
SSH2自動ログイン機能を追加。
現状、パスワード認証のみに対応。
・コマンドライン
  /ssh /auth=認証メソッド /user=ユーザ名 /passwd=パスワード

1 /*
2 Copyright (c) 1998-2001, Robert O'Callahan
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer.
10
11 Redistributions in binary form must reproduce the above copyright notice, this list
12 of conditions and the following disclaimer in the documentation and/or other materials
13 provided with the distribution.
14
15 The name of Robert O'Callahan may not be used to endorse or promote products derived from
16 this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "ttxssh.h"
30 #include "util.h"
31
32 #include <io.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <errno.h>
36
37 #include "resource.h"
38 #include "keyfiles.h"
39
40 #define AUTH_START_USER_AUTH_ON_ERROR_END 1
41
42 #define MAX_AUTH_CONTROL IDC_SSHUSETIS
43
44 static void destroy_malloced_string(char FAR * FAR * str)
45 {
46 if (*str != NULL) {
47 memset(*str, 0, strlen(*str));
48 free(*str);
49 *str = NULL;
50 }
51 }
52
53 static int auth_types_to_control_IDs[] = {
54 -1, IDC_SSHUSERHOSTS, IDC_SSHUSERSA, IDC_SSHUSEPASSWORD,
55 IDC_SSHUSERHOSTS, IDC_SSHUSETIS, -1
56 };
57
58 static LRESULT CALLBACK password_wnd_proc(HWND control, UINT msg,
59 WPARAM wParam, LPARAM lParam)
60 {
61 switch (msg) {
62 case WM_CHAR:
63 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0) {
64 char chars[] = { (char) wParam, 0 };
65
66 SendMessage(control, EM_REPLACESEL, (WPARAM) TRUE,
67 (LPARAM) (char FAR *) chars);
68 return 0;
69 }
70 }
71
72 return CallWindowProc((WNDPROC) GetWindowLong(control, GWL_USERDATA),
73 control, msg, wParam, lParam);
74 }
75
76 static void init_password_control(HWND dlg)
77 {
78 HWND passwordControl = GetDlgItem(dlg, IDC_SSHPASSWORD);
79
80 SetWindowLong(passwordControl, GWL_USERDATA,
81 SetWindowLong(passwordControl, GWL_WNDPROC,
82 (LONG) password_wnd_proc));
83
84 SetFocus(passwordControl);
85 }
86
87 static void set_auth_options_status(HWND dlg, int controlID)
88 {
89 BOOL RSA_enabled = controlID == IDC_SSHUSERSA;
90 BOOL rhosts_enabled = controlID == IDC_SSHUSERHOSTS;
91 BOOL TIS_enabled = controlID == IDC_SSHUSETIS;
92 int i;
93
94 CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL, controlID);
95
96 EnableWindow(GetDlgItem(dlg, IDC_SSHPASSWORDCAPTION), !TIS_enabled);
97 EnableWindow(GetDlgItem(dlg, IDC_SSHPASSWORD), !TIS_enabled);
98
99 for (i = IDC_CHOOSERSAFILE; i <= IDC_RSAFILENAME; i++) {
100 EnableWindow(GetDlgItem(dlg, i), RSA_enabled);
101 }
102
103 for (i = IDC_LOCALUSERNAMELABEL; i <= IDC_HOSTRSAFILENAME; i++) {
104 EnableWindow(GetDlgItem(dlg, i), rhosts_enabled);
105 }
106 }
107
108 static void init_auth_machine_banner(PTInstVar pvar, HWND dlg)
109 {
110 char buf[1024] = "Logging in to ";
111
112 if (strlen(buf) + strlen(SSH_get_host_name(pvar)) < sizeof(buf) - 2) {
113 strcat(buf, SSH_get_host_name(pvar));
114 }
115 SetDlgItemText(dlg, IDC_SSHAUTHBANNER, buf);
116 }
117
118 static void update_server_supported_types(PTInstVar pvar, HWND dlg)
119 {
120 int supported_methods = pvar->auth_state.supported_types;
121 int cur_control = -1;
122 int control;
123 HWND focus = GetFocus();
124
125 if (supported_methods == 0) {
126 return;
127 }
128
129 for (control = IDC_SSHUSEPASSWORD; control <= MAX_AUTH_CONTROL;
130 control++) {
131 BOOL enabled = FALSE;
132 int method;
133 HWND item = GetDlgItem(dlg, control);
134
135 if (item != NULL) {
136 for (method = 0; method <= SSH_AUTH_MAX; method++) {
137 if (auth_types_to_control_IDs[method] == control
138 && (supported_methods & (1 << method)) != 0) {
139 enabled = TRUE;
140 }
141 }
142
143 EnableWindow(item, enabled);
144
145 if (IsDlgButtonChecked(dlg, control)) {
146 cur_control = control;
147 }
148 }
149 }
150
151 if (cur_control >= 0) {
152 if (!IsWindowEnabled(GetDlgItem(dlg, cur_control))) {
153 do {
154 cur_control++;
155 if (cur_control > MAX_AUTH_CONTROL) {
156 cur_control = IDC_SSHUSEPASSWORD;
157 }
158 } while (!IsWindowEnabled(GetDlgItem(dlg, cur_control)));
159
160 set_auth_options_status(dlg, cur_control);
161
162 if (focus != NULL && !IsWindowEnabled(focus)) {
163 SetFocus(GetDlgItem(dlg, cur_control));
164 }
165 }
166 }
167 }
168
169 static void init_auth_dlg(PTInstVar pvar, HWND dlg)
170 {
171 int default_method = pvar->session_settings.DefaultAuthMethod;
172
173 init_auth_machine_banner(pvar, dlg);
174 init_password_control(dlg);
175
176 if (pvar->auth_state.failed_method != SSH_AUTH_NONE) {
177 /* must be retrying a failed attempt */
178 SetDlgItemText(dlg, IDC_SSHAUTHBANNER2,
179 "Authentication failed. Please retry.");
180 SetWindowText(dlg, "Retrying SSH Authentication");
181 default_method = pvar->auth_state.failed_method;
182 }
183
184 set_auth_options_status(dlg,
185 auth_types_to_control_IDs[default_method]);
186
187 if (default_method == SSH_AUTH_TIS) {
188 /* we disabled the password control, so fix the focus */
189 SetFocus(GetDlgItem(dlg, IDC_SSHUSETIS));
190 }
191
192 if (pvar->auth_state.user != NULL) {
193 SetDlgItemText(dlg, IDC_SSHUSERNAME, pvar->auth_state.user);
194 EnableWindow(GetDlgItem(dlg, IDC_SSHUSERNAME), FALSE);
195 EnableWindow(GetDlgItem(dlg, IDC_SSHUSERNAMELABEL), FALSE);
196 } else if (pvar->session_settings.DefaultUserName[0] != 0) {
197 SetDlgItemText(dlg, IDC_SSHUSERNAME,
198 pvar->session_settings.DefaultUserName);
199 } else {
200 SetFocus(GetDlgItem(dlg, IDC_SSHUSERNAME));
201 }
202
203 SetDlgItemText(dlg, IDC_RSAFILENAME,
204 pvar->session_settings.DefaultRSAPrivateKeyFile);
205 SetDlgItemText(dlg, IDC_HOSTRSAFILENAME,
206 pvar->session_settings.DefaultRhostsHostPrivateKeyFile);
207 SetDlgItemText(dlg, IDC_LOCALUSERNAME,
208 pvar->session_settings.DefaultRhostsLocalUserName);
209
210 update_server_supported_types(pvar, dlg);
211
212 // SSH2 autologin (2004.12.1 yutaka)
213 // ���[�U�A�p�X���[�h�A�F�����\�b�h���������������A������������OK�{�^�������������B
214 if (pvar->ssh2_autologin == 1) {
215 SetDlgItemText(dlg, IDC_SSHUSERNAME, pvar->ssh2_username);
216 EnableWindow(GetDlgItem(dlg, IDC_SSHUSERNAME), FALSE);
217 EnableWindow(GetDlgItem(dlg, IDC_SSHUSERNAMELABEL), FALSE);
218
219 SetDlgItemText(dlg, IDC_SSHPASSWORD, pvar->ssh2_password);
220 EnableWindow(GetDlgItem(dlg, IDC_SSHPASSWORD), FALSE);
221 EnableWindow(GetDlgItem(dlg, IDC_SSHPASSWORDCAPTION), FALSE);
222
223 if (pvar->auth_state.cur_cred.method == SSH_AUTH_PASSWORD) {
224 CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL, IDC_SSHUSEPASSWORD);
225 } else {
226 // TODO
227
228 }
229 }
230
231 }
232
233 static char FAR *alloc_control_text(HWND ctl)
234 {
235 int len = GetWindowTextLength(ctl);
236 char FAR *result = malloc(len + 1);
237
238 if (result != NULL) {
239 GetWindowText(ctl, result, len + 1);
240 result[len] = 0;
241 }
242
243 return result;
244 }
245
246 static int get_key_file_name(HWND parent, char FAR * buf, int bufsize)
247 {
248 #ifdef TERATERM32
249 OPENFILENAME params;
250 char fullname_buf[2048] = "identity";
251
252 params.lStructSize = sizeof(OPENFILENAME);
253 params.hwndOwner = parent;
254 params.lpstrFilter = NULL;
255 params.lpstrCustomFilter = NULL;
256 params.nFilterIndex = 0;
257 buf[0] = 0;
258 params.lpstrFile = fullname_buf;
259 params.nMaxFile = sizeof(fullname_buf);
260 params.lpstrFileTitle = NULL;
261 params.lpstrInitialDir = NULL;
262 params.lpstrTitle = "Choose a file with the RSA private key";
263 params.Flags =
264 OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
265 params.lpstrDefExt = NULL;
266
267 if (GetOpenFileName(&params) != 0) {
268 copy_teraterm_dir_relative_path(buf, bufsize, fullname_buf);
269 return 1;
270 } else {
271 return 0;
272 }
273 #else
274 return 0;
275 #endif
276 }
277
278 static void choose_RSA_key_file(HWND dlg)
279 {
280 char buf[1024];
281
282 if (get_key_file_name(dlg, buf, sizeof(buf))) {
283 SetDlgItemText(dlg, IDC_RSAFILENAME, buf);
284 }
285 }
286
287 static void choose_host_RSA_key_file(HWND dlg)
288 {
289 char buf[1024];
290
291 if (get_key_file_name(dlg, buf, sizeof(buf))) {
292 SetDlgItemText(dlg, IDC_HOSTRSAFILENAME, buf);
293 }
294 }
295
296 static BOOL end_auth_dlg(PTInstVar pvar, HWND dlg)
297 {
298 int method = SSH_AUTH_PASSWORD;
299 char FAR *password =
300 alloc_control_text(GetDlgItem(dlg, IDC_SSHPASSWORD));
301 CRYPTKeyPair FAR *key_pair = NULL;
302
303 if (IsDlgButtonChecked(dlg, IDC_SSHUSERSA)) {
304 method = SSH_AUTH_RSA;
305 } else if (IsDlgButtonChecked(dlg, IDC_SSHUSERHOSTS)) {
306 if (GetWindowTextLength(GetDlgItem(dlg, IDC_HOSTRSAFILENAME)) > 0) {
307 method = SSH_AUTH_RHOSTS_RSA;
308 } else {
309 method = SSH_AUTH_RHOSTS;
310 }
311 } else if (IsDlgButtonChecked(dlg, IDC_SSHUSETIS)) {
312 method = SSH_AUTH_TIS;
313 }
314
315 if (method == SSH_AUTH_RSA || method == SSH_AUTH_RHOSTS_RSA) {
316 char buf[2048];
317 int file_ctl_ID =
318 method == SSH_AUTH_RSA ? IDC_RSAFILENAME : IDC_HOSTRSAFILENAME;
319
320 buf[0] = 0;
321 GetDlgItemText(dlg, file_ctl_ID, buf, sizeof(buf));
322 if (buf[0] == 0) {
323 notify_nonfatal_error(pvar,
324 "You must specify a file containing the RSA private key.");
325 SetFocus(GetDlgItem(dlg, file_ctl_ID));
326 destroy_malloced_string(&password);
327 return FALSE;
328 } else {
329 BOOL invalid_passphrase = FALSE;
330
331 key_pair = KEYFILES_read_private_key(pvar, buf, password,
332 &invalid_passphrase,
333 FALSE);
334
335 if (key_pair == NULL) {
336 if (invalid_passphrase) {
337 HWND passwordCtl = GetDlgItem(dlg, IDC_SSHPASSWORD);
338
339 SetFocus(passwordCtl);
340 SendMessage(passwordCtl, EM_SETSEL, 0, -1);
341 } else {
342 SetFocus(GetDlgItem(dlg, file_ctl_ID));
343 }
344 destroy_malloced_string(&password);
345 return FALSE;
346 }
347 }
348 }
349
350 /* from here on, we cannot fail, so just munge cur_cred in place */
351 pvar->auth_state.cur_cred.method = method;
352 pvar->auth_state.cur_cred.key_pair = key_pair;
353 /* we can't change the user name once it's set. It may already have
354 been sent to the server, and it can only be sent once. */
355 if (pvar->auth_state.user == NULL) {
356 pvar->auth_state.user =
357 alloc_control_text(GetDlgItem(dlg, IDC_SSHUSERNAME));
358 }
359 if (method == SSH_AUTH_PASSWORD) {
360 pvar->auth_state.cur_cred.password = password;
361 } else {
362 destroy_malloced_string(&password);
363 }
364 if (method == SSH_AUTH_RHOSTS || method == SSH_AUTH_RHOSTS_RSA) {
365 if (pvar->session_settings.DefaultAuthMethod != SSH_AUTH_RHOSTS) {
366 notify_nonfatal_error(pvar,
367 "Rhosts authentication will probably fail because it was not "
368 "the default authentication method.\n"
369 "To use Rhosts authentication "
370 "in TTSSH, you need to set it to be the default by restarting\n"
371 "TTSSH and selecting \"SSH Authentication...\" from the Setup menu"
372 "before connecting.");
373 }
374
375 pvar->auth_state.cur_cred.rhosts_client_user =
376 alloc_control_text(GetDlgItem(dlg, IDC_LOCALUSERNAME));
377 }
378 pvar->auth_state.auth_dialog = NULL;
379
380 GetDlgItemText(dlg, IDC_RSAFILENAME,
381 pvar->session_settings.DefaultRSAPrivateKeyFile,
382 sizeof(pvar->session_settings.
383 DefaultRSAPrivateKeyFile));
384 GetDlgItemText(dlg, IDC_HOSTRSAFILENAME,
385 pvar->session_settings.DefaultRhostsHostPrivateKeyFile,
386 sizeof(pvar->session_settings.
387 DefaultRhostsHostPrivateKeyFile));
388 GetDlgItemText(dlg, IDC_LOCALUSERNAME,
389 pvar->session_settings.DefaultRhostsLocalUserName,
390 sizeof(pvar->session_settings.
391 DefaultRhostsLocalUserName));
392
393 if (SSHv1(pvar)) {
394 SSH_notify_user_name(pvar);
395 SSH_notify_cred(pvar);
396 } else {
397 // for SSH2(yutaka)
398 do_SSH2_userauth(pvar);
399 }
400
401 EndDialog(dlg, 1);
402 return TRUE;
403 }
404
405 static BOOL CALLBACK auth_dlg_proc(HWND dlg, UINT msg, WPARAM wParam,
406 LPARAM lParam)
407 {
408 const int IDC_TIMER1 = 300;
409 const int autologin_timeout = 1000; // �~���b
410 PTInstVar pvar;
411
412 switch (msg) {
413 case WM_INITDIALOG:
414 pvar = (PTInstVar) lParam;
415 pvar->auth_state.auth_dialog = dlg;
416 SetWindowLong(dlg, DWL_USER, lParam);
417
418 init_auth_dlg(pvar, dlg);
419
420 // SSH2 autologin���L�����������A�^�C�}���d�|�����B (2004.12.1 yutaka)
421 if (pvar->ssh2_autologin == 1) {
422 SetTimer(dlg, IDC_TIMER1, autologin_timeout, 0);
423 }
424 return FALSE; /* because we set the focus */
425
426 case WM_TIMER:
427 KillTimer(dlg, IDC_TIMER1);
428 SendMessage(dlg, WM_COMMAND, IDOK, 0);
429 return TRUE;
430
431 case WM_COMMAND:
432 pvar = (PTInstVar) GetWindowLong(dlg, DWL_USER);
433
434 switch (LOWORD(wParam)) {
435 case IDOK:
436 return end_auth_dlg(pvar, dlg);
437
438 case IDCANCEL: /* kill the connection */
439 pvar->auth_state.auth_dialog = NULL;
440 notify_closed_connection(pvar);
441 EndDialog(dlg, 0);
442 return TRUE;
443
444 case IDC_SSHUSEPASSWORD:
445 case IDC_SSHUSERSA:
446 case IDC_SSHUSERHOSTS:
447 case IDC_SSHUSETIS:
448 set_auth_options_status(dlg, LOWORD(wParam));
449 return TRUE;
450
451 case IDC_CHOOSERSAFILE:
452 choose_RSA_key_file(dlg);
453 return TRUE;
454
455 case IDC_CHOOSEHOSTRSAFILE:
456 choose_host_RSA_key_file(dlg);
457 return TRUE;
458
459 default:
460 return FALSE;
461 }
462
463 default:
464 return FALSE;
465 }
466 }
467
468 char FAR *AUTH_get_user_name(PTInstVar pvar)
469 {
470 return pvar->auth_state.user;
471 }
472
473 int AUTH_set_supported_auth_types(PTInstVar pvar, int types)
474 {
475 char buf[1024];
476
477 _snprintf(buf, sizeof(buf),
478 "Server reports supported authentication method mask = %d",
479 types);
480 buf[sizeof(buf) - 1] = 0;
481 notify_verbose_message(pvar, buf, LOG_LEVEL_VERBOSE);
482
483 if (SSHv1(pvar)) {
484 types &= (1 << SSH_AUTH_PASSWORD) | (1 << SSH_AUTH_RSA)
485 | (1 << SSH_AUTH_RHOSTS_RSA) | (1 << SSH_AUTH_RHOSTS)
486 | (1 << SSH_AUTH_TIS);
487 } else {
488 // for SSH2(yutaka)
489 types &= (1 << SSH_AUTH_PASSWORD);
490 // types &= (1 << SSH_AUTH_PASSWORD) | (1 << SSH_AUTH_RSA)
491 // | (1 << SSH_AUTH_DSA);
492 }
493 pvar->auth_state.supported_types = types;
494
495 if (types == 0) {
496 notify_fatal_error(pvar,
497 "Server does not support any of the authentication options\n"
498 "provided by TTSSH. This connection will now close.");
499 return 0;
500 } else {
501 if (pvar->auth_state.auth_dialog != NULL) {
502 update_server_supported_types(pvar,
503 pvar->auth_state.auth_dialog);
504 }
505
506 return 1;
507 }
508 }
509
510 static void start_user_auth(PTInstVar pvar)
511 {
512 // �F���_�C�A���O���\�������� (2004.12.1 yutaka)
513 PostMessage(pvar->NotificationWindow, WM_COMMAND, (WPARAM) ID_SSHAUTH,
514 (LPARAM) NULL);
515 pvar->auth_state.cur_cred.method = SSH_AUTH_NONE;
516 }
517
518 static void try_default_auth(PTInstVar pvar)
519 {
520 if (pvar->session_settings.TryDefaultAuth) {
521 switch (pvar->session_settings.DefaultAuthMethod) {
522 case SSH_AUTH_RSA:{
523 BOOL invalid_passphrase;
524 char password[] = "";
525
526 pvar->auth_state.cur_cred.key_pair
527 =
528 KEYFILES_read_private_key(pvar,
529 pvar->session_settings.
530 DefaultRSAPrivateKeyFile,
531 password,
532 &invalid_passphrase, TRUE);
533 if (pvar->auth_state.cur_cred.key_pair == NULL) {
534 return;
535 } else {
536 pvar->auth_state.cur_cred.method = SSH_AUTH_RSA;
537 }
538 break;
539 }
540
541 case SSH_AUTH_RHOSTS:
542 if (pvar->session_settings.
543 DefaultRhostsHostPrivateKeyFile[0] != 0) {
544 BOOL invalid_passphrase;
545 char password[] = "";
546
547 pvar->auth_state.cur_cred.key_pair
548 =
549 KEYFILES_read_private_key(pvar,
550 pvar->session_settings.
551 DefaultRhostsHostPrivateKeyFile,
552 password,
553 &invalid_passphrase, TRUE);
554 if (pvar->auth_state.cur_cred.key_pair == NULL) {
555 return;
556 } else {
557 pvar->auth_state.cur_cred.method = SSH_AUTH_RHOSTS_RSA;
558 }
559 } else {
560 pvar->auth_state.cur_cred.method = SSH_AUTH_RHOSTS;
561 }
562
563 pvar->auth_state.cur_cred.rhosts_client_user =
564 _strdup(pvar->session_settings.DefaultRhostsLocalUserName);
565 break;
566
567 case SSH_AUTH_PASSWORD:
568 pvar->auth_state.cur_cred.password = _strdup("");
569 pvar->auth_state.cur_cred.method = SSH_AUTH_PASSWORD;
570 break;
571
572 case SSH_AUTH_TIS:
573 default:
574 return;
575 }
576
577 pvar->auth_state.user =
578 _strdup(pvar->session_settings.DefaultUserName);
579 }
580 }
581
582 void AUTH_notify_end_error(PTInstVar pvar)
583 {
584 if ((pvar->auth_state.flags & AUTH_START_USER_AUTH_ON_ERROR_END) != 0) {
585 start_user_auth(pvar);
586 pvar->auth_state.flags &= ~AUTH_START_USER_AUTH_ON_ERROR_END;
587 }
588 }
589
590 void AUTH_advance_to_next_cred(PTInstVar pvar)
591 {
592 pvar->auth_state.failed_method = pvar->auth_state.cur_cred.method;
593
594 if (pvar->auth_state.cur_cred.method == SSH_AUTH_NONE) {
595 try_default_auth(pvar);
596
597 if (pvar->auth_state.cur_cred.method == SSH_AUTH_NONE) {
598 if (pvar->err_msg != NULL) {
599 pvar->auth_state.flags |=
600 AUTH_START_USER_AUTH_ON_ERROR_END;
601 } else {
602 // �������F���_�C�A���O���o�������� (2004.12.1 yutaka)
603 // �R�}���h���C���w������������
604 start_user_auth(pvar);
605 }
606 }
607 } else {
608 // �������F���_�C�A���O���o�������� (2004.12.1 yutaka)
609 // �R�}���h���C���w������(/auth=xxxx)������
610 start_user_auth(pvar);
611 }
612 }
613
614 static void init_TIS_dlg(PTInstVar pvar, HWND dlg)
615 {
616 init_auth_machine_banner(pvar, dlg);
617 init_password_control(dlg);
618
619 if (pvar->auth_state.TIS_prompt != NULL) {
620 if (strlen(pvar->auth_state.TIS_prompt) > 10000) {
621 pvar->auth_state.TIS_prompt[10000] = 0;
622 }
623 SetDlgItemText(dlg, IDC_SSHAUTHBANNER2,
624 pvar->auth_state.TIS_prompt);
625 destroy_malloced_string(&pvar->auth_state.TIS_prompt);
626 }
627 }
628
629 static BOOL end_TIS_dlg(PTInstVar pvar, HWND dlg)
630 {
631 char FAR *password =
632 alloc_control_text(GetDlgItem(dlg, IDC_SSHPASSWORD));
633
634 pvar->auth_state.cur_cred.method = SSH_AUTH_TIS;
635 pvar->auth_state.cur_cred.password = password;
636 pvar->auth_state.auth_dialog = NULL;
637
638 SSH_notify_cred(pvar);
639
640 EndDialog(dlg, 1);
641 return TRUE;
642 }
643
644 static BOOL CALLBACK TIS_dlg_proc(HWND dlg, UINT msg, WPARAM wParam,
645 LPARAM lParam)
646 {
647 PTInstVar pvar;
648
649 switch (msg) {
650 case WM_INITDIALOG:
651 pvar = (PTInstVar) lParam;
652 pvar->auth_state.auth_dialog = dlg;
653 SetWindowLong(dlg, DWL_USER, lParam);
654
655 init_TIS_dlg(pvar, dlg);
656 return FALSE; /* because we set the focus */
657
658 case WM_COMMAND:
659 pvar = (PTInstVar) GetWindowLong(dlg, DWL_USER);
660
661 switch (LOWORD(wParam)) {
662 case IDOK:
663 return end_TIS_dlg(pvar, dlg);
664
665 case IDCANCEL: /* kill the connection */
666 pvar->auth_state.auth_dialog = NULL;
667 notify_closed_connection(pvar);
668 EndDialog(dlg, 0);
669 return TRUE;
670
671 default:
672 return FALSE;
673 }
674
675 default:
676 return FALSE;
677 }
678 }
679
680 void AUTH_do_cred_dialog(PTInstVar pvar)
681 {
682 if (pvar->auth_state.auth_dialog == NULL) {
683 HWND cur_active = GetActiveWindow();
684 DLGPROC dlg_proc;
685 LPCTSTR dialog_template;
686
687 switch (pvar->auth_state.mode) {
688 case TIS_AUTH_MODE:
689 dialog_template = MAKEINTRESOURCE(IDD_SSHTISAUTH);
690 dlg_proc = TIS_dlg_proc;
691 break;
692 case GENERIC_AUTH_MODE:
693 default:
694 dialog_template = MAKEINTRESOURCE(IDD_SSHAUTH);
695 dlg_proc = auth_dlg_proc;
696 }
697
698 if (!DialogBoxParam(hInst, dialog_template,
699 cur_active !=
700 NULL ? cur_active : pvar->NotificationWindow,
701 dlg_proc, (LPARAM) pvar) == -1) {
702 notify_fatal_error(pvar,
703 "Unable to display authentication dialog box.\n"
704 "Connection terminated.");
705 }
706 }
707 }
708
709 static void init_default_auth_dlg(PTInstVar pvar, HWND dlg)
710 {
711 switch (pvar->settings.DefaultAuthMethod) {
712 case SSH_AUTH_RSA:
713 CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
714 IDC_SSHUSERSA);
715 break;
716 case SSH_AUTH_RHOSTS:
717 case SSH_AUTH_RHOSTS_RSA:
718 CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
719 IDC_SSHUSERHOSTS);
720 break;
721 case SSH_AUTH_TIS:
722 CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
723 IDC_SSHUSETIS);
724 break;
725 case SSH_AUTH_PASSWORD:
726 default:
727 CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
728 IDC_SSHUSEPASSWORD);
729 }
730
731 SetDlgItemText(dlg, IDC_SSHUSERNAME, pvar->settings.DefaultUserName);
732 SetDlgItemText(dlg, IDC_RSAFILENAME,
733 pvar->settings.DefaultRSAPrivateKeyFile);
734 SetDlgItemText(dlg, IDC_HOSTRSAFILENAME,
735 pvar->settings.DefaultRhostsHostPrivateKeyFile);
736 SetDlgItemText(dlg, IDC_LOCALUSERNAME,
737 pvar->settings.DefaultRhostsLocalUserName);
738 }
739
740 static BOOL end_default_auth_dlg(PTInstVar pvar, HWND dlg)
741 {
742 if (IsDlgButtonChecked(dlg, IDC_SSHUSERSA)) {
743 pvar->settings.DefaultAuthMethod = SSH_AUTH_RSA;
744 } else if (IsDlgButtonChecked(dlg, IDC_SSHUSERHOSTS)) {
745 if (GetWindowTextLength(GetDlgItem(dlg, IDC_HOSTRSAFILENAME)) > 0) {
746 pvar->settings.DefaultAuthMethod = SSH_AUTH_RHOSTS_RSA;
747 } else {
748 pvar->settings.DefaultAuthMethod = SSH_AUTH_RHOSTS;
749 }
750 } else if (IsDlgButtonChecked(dlg, IDC_SSHUSETIS)) {
751 pvar->settings.DefaultAuthMethod = SSH_AUTH_TIS;
752 } else {
753 pvar->settings.DefaultAuthMethod = SSH_AUTH_PASSWORD;
754 }
755
756 GetDlgItemText(dlg, IDC_SSHUSERNAME, pvar->settings.DefaultUserName,
757 sizeof(pvar->settings.DefaultUserName));
758 GetDlgItemText(dlg, IDC_RSAFILENAME,
759 pvar->settings.DefaultRSAPrivateKeyFile,
760 sizeof(pvar->settings.DefaultRSAPrivateKeyFile));
761 GetDlgItemText(dlg, IDC_HOSTRSAFILENAME,
762 pvar->settings.DefaultRhostsHostPrivateKeyFile,
763 sizeof(pvar->settings.DefaultRhostsHostPrivateKeyFile));
764 GetDlgItemText(dlg, IDC_LOCALUSERNAME,
765 pvar->settings.DefaultRhostsLocalUserName,
766 sizeof(pvar->settings.DefaultRhostsLocalUserName));
767
768 EndDialog(dlg, 1);
769 return TRUE;
770 }
771
772 static BOOL CALLBACK default_auth_dlg_proc(HWND dlg, UINT msg,
773 WPARAM wParam, LPARAM lParam)
774 {
775 PTInstVar pvar;
776
777 switch (msg) {
778 case WM_INITDIALOG:
779 pvar = (PTInstVar) lParam;
780 SetWindowLong(dlg, DWL_USER, lParam);
781
782 init_default_auth_dlg(pvar, dlg);
783 return TRUE; /* because we do not set the focus */
784
785 case WM_COMMAND:
786 pvar = (PTInstVar) GetWindowLong(dlg, DWL_USER);
787
788 switch (LOWORD(wParam)) {
789 case IDOK:
790 return end_default_auth_dlg(pvar, dlg);
791
792 case IDCANCEL:
793 EndDialog(dlg, 0);
794 return TRUE;
795
796 case IDC_CHOOSERSAFILE:
797 choose_RSA_key_file(dlg);
798 return TRUE;
799
800 case IDC_CHOOSEHOSTRSAFILE:
801 choose_host_RSA_key_file(dlg);
802 return TRUE;
803
804 default:
805 return FALSE;
806 }
807
808 default:
809 return FALSE;
810 }
811 }
812
813 void AUTH_init(PTInstVar pvar)
814 {
815 pvar->auth_state.failed_method = SSH_AUTH_NONE;
816 pvar->auth_state.auth_dialog = NULL;
817 pvar->auth_state.user = NULL;
818 pvar->auth_state.flags = 0;
819 pvar->auth_state.TIS_prompt = NULL;
820 pvar->auth_state.supported_types = 0;
821 pvar->auth_state.cur_cred.method = SSH_AUTH_NONE;
822 pvar->auth_state.cur_cred.password = NULL;
823 pvar->auth_state.cur_cred.rhosts_client_user = NULL;
824 pvar->auth_state.cur_cred.key_pair = NULL;
825 AUTH_set_generic_mode(pvar);
826 }
827
828 void AUTH_set_generic_mode(PTInstVar pvar)
829 {
830 pvar->auth_state.mode = GENERIC_AUTH_MODE;
831 destroy_malloced_string(&pvar->auth_state.TIS_prompt);
832 }
833
834 void AUTH_set_TIS_mode(PTInstVar pvar, char FAR * prompt, int len)
835 {
836 if (pvar->auth_state.cur_cred.method == SSH_AUTH_TIS) {
837 pvar->auth_state.mode = TIS_AUTH_MODE;
838
839 destroy_malloced_string(&pvar->auth_state.TIS_prompt);
840 pvar->auth_state.TIS_prompt = malloc(len + 1);
841 memcpy(pvar->auth_state.TIS_prompt, prompt, len);
842 pvar->auth_state.TIS_prompt[len] = 0;
843 } else {
844 AUTH_set_generic_mode(pvar);
845 }
846 }
847
848 void AUTH_do_default_cred_dialog(PTInstVar pvar)
849 {
850 HWND cur_active = GetActiveWindow();
851
852 if (DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SSHAUTHSETUP),
853 cur_active !=
854 NULL ? cur_active : pvar->NotificationWindow,
855 default_auth_dlg_proc, (LPARAM) pvar) == -1) {
856 notify_nonfatal_error(pvar,
857 "Unable to display authentication setup dialog box.");
858 }
859 }
860
861 void AUTH_destroy_cur_cred(PTInstVar pvar)
862 {
863 destroy_malloced_string(&pvar->auth_state.cur_cred.password);
864 destroy_malloced_string(&pvar->auth_state.cur_cred.rhosts_client_user);
865 if (pvar->auth_state.cur_cred.key_pair != NULL) {
866 CRYPT_free_key_pair(pvar->auth_state.cur_cred.key_pair);
867 pvar->auth_state.cur_cred.key_pair = NULL;
868 }
869 }
870
871 static char FAR *get_auth_method_name(SSHAuthMethod auth)
872 {
873 switch (auth) {
874 case SSH_AUTH_PASSWORD:
875 return "password";
876 case SSH_AUTH_RSA:
877 return "RSA";
878 case SSH_AUTH_RHOSTS:
879 return "rhosts";
880 case SSH_AUTH_RHOSTS_RSA:
881 return "rhosts with RSA";
882 case SSH_AUTH_TIS:
883 return "challenge/response (TIS)";
884 default:
885 return "unknown method";
886 }
887 }
888
889 void AUTH_get_auth_info(PTInstVar pvar, char FAR * dest, int len)
890 {
891 if (pvar->auth_state.user == NULL) {
892 strncpy(dest, "None", len);
893 } else if (pvar->auth_state.cur_cred.method != SSH_AUTH_NONE) {
894 _snprintf(dest, len, "User '%s', using %s", pvar->auth_state.user,
895 get_auth_method_name(pvar->auth_state.cur_cred.method));
896 } else {
897 _snprintf(dest, len, "User '%s', using %s", pvar->auth_state.user,
898 get_auth_method_name(pvar->auth_state.failed_method));
899 }
900
901 dest[len - 1] = 0;
902 }
903
904 void AUTH_notify_disconnecting(PTInstVar pvar)
905 {
906 if (pvar->auth_state.auth_dialog != NULL) {
907 PostMessage(pvar->auth_state.auth_dialog, WM_COMMAND, IDCANCEL, 0);
908 /* the main window might not go away if it's not enabled. (see vtwin.cpp) */
909 EnableWindow(pvar->NotificationWindow, TRUE);
910 }
911 }
912
913 void AUTH_end(PTInstVar pvar)
914 {
915 destroy_malloced_string(&pvar->auth_state.user);
916 destroy_malloced_string(&pvar->auth_state.TIS_prompt);
917
918 AUTH_destroy_cur_cred(pvar);
919 }
920
921 /*
922 * $Log: not supported by cvs2svn $
923 */

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26