Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2784 - (hide annotations) (download) (as text)
Thu Jan 27 13:30:33 2005 UTC (19 years, 2 months ago) by yutakakn
Original Path: ttssh2/trunk/ttxssh/auth.c
File MIME type: text/x-csrc
File size: 28416 byte(s)
公開鍵認証自動ログインをサポート。
/auth=publickey, /keyfile オプションを新規追加した。
また、空白を含む引数をサポート。

1 yutakakn 2728 /*
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 yutakakn 2739
212 yutakakn 2784 // SSH2 autologin
213 yutakakn 2739 // ���[�U�A�p�X���[�h�A�F�����\�b�h���������������A������������OK�{�^�������������B
214 yutakakn 2784 //
215     // (2004.12.1 yutaka)
216     // (2005.1.26 yutaka) ���J���F���T�|�[�g
217 yutakakn 2739 if (pvar->ssh2_autologin == 1) {
218     SetDlgItemText(dlg, IDC_SSHUSERNAME, pvar->ssh2_username);
219     EnableWindow(GetDlgItem(dlg, IDC_SSHUSERNAME), FALSE);
220     EnableWindow(GetDlgItem(dlg, IDC_SSHUSERNAMELABEL), FALSE);
221    
222     SetDlgItemText(dlg, IDC_SSHPASSWORD, pvar->ssh2_password);
223     EnableWindow(GetDlgItem(dlg, IDC_SSHPASSWORD), FALSE);
224     EnableWindow(GetDlgItem(dlg, IDC_SSHPASSWORDCAPTION), FALSE);
225    
226 yutakakn 2784 if (pvar->ssh2_authmethod == SSH_AUTH_PASSWORD) {
227 yutakakn 2739 CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL, IDC_SSHUSEPASSWORD);
228 yutakakn 2784
229     } else if (pvar->ssh2_authmethod == SSH_AUTH_RSA) {
230     CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL, IDC_SSHUSERSA);
231    
232     SetDlgItemText(dlg, IDC_RSAFILENAME, pvar->ssh2_keyfile);
233     EnableWindow(GetDlgItem(dlg, IDC_CHOOSERSAFILE), FALSE);
234     EnableWindow(GetDlgItem(dlg, IDC_RSAFILENAME), FALSE);
235    
236 yutakakn 2739 } else {
237     // TODO
238    
239     }
240     }
241    
242 yutakakn 2728 }
243    
244     static char FAR *alloc_control_text(HWND ctl)
245     {
246     int len = GetWindowTextLength(ctl);
247     char FAR *result = malloc(len + 1);
248    
249     if (result != NULL) {
250     GetWindowText(ctl, result, len + 1);
251     result[len] = 0;
252     }
253    
254     return result;
255     }
256    
257     static int get_key_file_name(HWND parent, char FAR * buf, int bufsize)
258     {
259     #ifdef TERATERM32
260     OPENFILENAME params;
261     char fullname_buf[2048] = "identity";
262    
263 yutakakn 2762 ZeroMemory(&params, sizeof(params));
264 yutakakn 2728 params.lStructSize = sizeof(OPENFILENAME);
265     params.hwndOwner = parent;
266 yutakakn 2762 // �t�B���^������ (2004.12.19 yutaka)
267     params.lpstrFilter = "identity(RSA1)\0identity\0id_rsa(SSH2)\0id_rsa\0id_dsa(SSH2)\0id_dsa\0all(*.*)\0*.*\0\0";
268 yutakakn 2728 params.lpstrCustomFilter = NULL;
269     params.nFilterIndex = 0;
270     buf[0] = 0;
271     params.lpstrFile = fullname_buf;
272     params.nMaxFile = sizeof(fullname_buf);
273     params.lpstrFileTitle = NULL;
274     params.lpstrInitialDir = NULL;
275     params.lpstrTitle = "Choose a file with the RSA private key";
276     params.Flags =
277     OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
278     params.lpstrDefExt = NULL;
279    
280     if (GetOpenFileName(&params) != 0) {
281     copy_teraterm_dir_relative_path(buf, bufsize, fullname_buf);
282     return 1;
283     } else {
284     return 0;
285     }
286     #else
287     return 0;
288     #endif
289     }
290    
291     static void choose_RSA_key_file(HWND dlg)
292     {
293     char buf[1024];
294    
295     if (get_key_file_name(dlg, buf, sizeof(buf))) {
296     SetDlgItemText(dlg, IDC_RSAFILENAME, buf);
297     }
298     }
299    
300     static void choose_host_RSA_key_file(HWND dlg)
301     {
302     char buf[1024];
303    
304     if (get_key_file_name(dlg, buf, sizeof(buf))) {
305     SetDlgItemText(dlg, IDC_HOSTRSAFILENAME, buf);
306     }
307     }
308    
309     static BOOL end_auth_dlg(PTInstVar pvar, HWND dlg)
310     {
311     int method = SSH_AUTH_PASSWORD;
312     char FAR *password =
313     alloc_control_text(GetDlgItem(dlg, IDC_SSHPASSWORD));
314     CRYPTKeyPair FAR *key_pair = NULL;
315    
316     if (IsDlgButtonChecked(dlg, IDC_SSHUSERSA)) {
317     method = SSH_AUTH_RSA;
318     } else if (IsDlgButtonChecked(dlg, IDC_SSHUSERHOSTS)) {
319     if (GetWindowTextLength(GetDlgItem(dlg, IDC_HOSTRSAFILENAME)) > 0) {
320     method = SSH_AUTH_RHOSTS_RSA;
321     } else {
322     method = SSH_AUTH_RHOSTS;
323     }
324     } else if (IsDlgButtonChecked(dlg, IDC_SSHUSETIS)) {
325     method = SSH_AUTH_TIS;
326     }
327    
328     if (method == SSH_AUTH_RSA || method == SSH_AUTH_RHOSTS_RSA) {
329     char buf[2048];
330     int file_ctl_ID =
331     method == SSH_AUTH_RSA ? IDC_RSAFILENAME : IDC_HOSTRSAFILENAME;
332    
333     buf[0] = 0;
334     GetDlgItemText(dlg, file_ctl_ID, buf, sizeof(buf));
335     if (buf[0] == 0) {
336     notify_nonfatal_error(pvar,
337 yutakakn 2762 "You must specify a file containing the RSA/DSA private key.");
338 yutakakn 2728 SetFocus(GetDlgItem(dlg, file_ctl_ID));
339     destroy_malloced_string(&password);
340     return FALSE;
341 yutakakn 2762 }
342    
343     if (SSHv1(pvar)) {
344 yutakakn 2728 BOOL invalid_passphrase = FALSE;
345    
346     key_pair = KEYFILES_read_private_key(pvar, buf, password,
347     &invalid_passphrase,
348     FALSE);
349    
350     if (key_pair == NULL) {
351     if (invalid_passphrase) {
352     HWND passwordCtl = GetDlgItem(dlg, IDC_SSHPASSWORD);
353    
354     SetFocus(passwordCtl);
355     SendMessage(passwordCtl, EM_SETSEL, 0, -1);
356     } else {
357     SetFocus(GetDlgItem(dlg, file_ctl_ID));
358     }
359     destroy_malloced_string(&password);
360     return FALSE;
361     }
362 yutakakn 2762
363     } else { // SSH2(yutaka)
364     BOOL invalid_passphrase = FALSE;
365 yutakakn 2769 char errmsg[256];
366 yutakakn 2762
367 yutakakn 2769 memset(errmsg, 0, sizeof(errmsg));
368 yutakakn 2784 //GetCurrentDirectory(sizeof(errmsg), errmsg);
369 yutakakn 2769
370 yutakakn 2762 key_pair = read_SSH2_private_key(pvar, buf, password,
371     &invalid_passphrase,
372 yutakakn 2769 FALSE,
373     errmsg,
374     sizeof(errmsg)
375     );
376 yutakakn 2762
377     if (key_pair == NULL) { // read error
378 yutakakn 2769 char buf[1024];
379     _snprintf(buf, sizeof(buf), "read error SSH2 private key file\r\n%s", errmsg);
380     notify_nonfatal_error(pvar, buf);
381 yutakakn 2762 destroy_malloced_string(&password);
382     return FALSE;
383     }
384    
385 yutakakn 2728 }
386 yutakakn 2762
387 yutakakn 2728 }
388    
389     /* from here on, we cannot fail, so just munge cur_cred in place */
390     pvar->auth_state.cur_cred.method = method;
391     pvar->auth_state.cur_cred.key_pair = key_pair;
392     /* we can't change the user name once it's set. It may already have
393     been sent to the server, and it can only be sent once. */
394     if (pvar->auth_state.user == NULL) {
395     pvar->auth_state.user =
396     alloc_control_text(GetDlgItem(dlg, IDC_SSHUSERNAME));
397     }
398     if (method == SSH_AUTH_PASSWORD) {
399     pvar->auth_state.cur_cred.password = password;
400     } else {
401     destroy_malloced_string(&password);
402     }
403     if (method == SSH_AUTH_RHOSTS || method == SSH_AUTH_RHOSTS_RSA) {
404     if (pvar->session_settings.DefaultAuthMethod != SSH_AUTH_RHOSTS) {
405     notify_nonfatal_error(pvar,
406     "Rhosts authentication will probably fail because it was not "
407     "the default authentication method.\n"
408     "To use Rhosts authentication "
409     "in TTSSH, you need to set it to be the default by restarting\n"
410     "TTSSH and selecting \"SSH Authentication...\" from the Setup menu"
411     "before connecting.");
412     }
413    
414     pvar->auth_state.cur_cred.rhosts_client_user =
415     alloc_control_text(GetDlgItem(dlg, IDC_LOCALUSERNAME));
416     }
417     pvar->auth_state.auth_dialog = NULL;
418    
419     GetDlgItemText(dlg, IDC_RSAFILENAME,
420     pvar->session_settings.DefaultRSAPrivateKeyFile,
421     sizeof(pvar->session_settings.
422     DefaultRSAPrivateKeyFile));
423     GetDlgItemText(dlg, IDC_HOSTRSAFILENAME,
424     pvar->session_settings.DefaultRhostsHostPrivateKeyFile,
425     sizeof(pvar->session_settings.
426     DefaultRhostsHostPrivateKeyFile));
427     GetDlgItemText(dlg, IDC_LOCALUSERNAME,
428     pvar->session_settings.DefaultRhostsLocalUserName,
429     sizeof(pvar->session_settings.
430     DefaultRhostsLocalUserName));
431    
432     if (SSHv1(pvar)) {
433     SSH_notify_user_name(pvar);
434     SSH_notify_cred(pvar);
435     } else {
436     // for SSH2(yutaka)
437     do_SSH2_userauth(pvar);
438     }
439    
440     EndDialog(dlg, 1);
441     return TRUE;
442     }
443    
444     static BOOL CALLBACK auth_dlg_proc(HWND dlg, UINT msg, WPARAM wParam,
445     LPARAM lParam)
446     {
447 yutakakn 2739 const int IDC_TIMER1 = 300;
448 yutakakn 2752 const int autologin_timeout = 10; // �~���b
449 yutakakn 2728 PTInstVar pvar;
450    
451     switch (msg) {
452     case WM_INITDIALOG:
453     pvar = (PTInstVar) lParam;
454     pvar->auth_state.auth_dialog = dlg;
455     SetWindowLong(dlg, DWL_USER, lParam);
456    
457     init_auth_dlg(pvar, dlg);
458 yutakakn 2739
459     // SSH2 autologin���L�����������A�^�C�}���d�|�����B (2004.12.1 yutaka)
460     if (pvar->ssh2_autologin == 1) {
461     SetTimer(dlg, IDC_TIMER1, autologin_timeout, 0);
462     }
463 yutakakn 2728 return FALSE; /* because we set the focus */
464    
465 yutakakn 2739 case WM_TIMER:
466 yutakakn 2752 pvar = (PTInstVar) GetWindowLong(dlg, DWL_USER);
467     // �F�������������������A�F���f�[�^�����M�����B�����������A�������B(2004.12.16 yutaka)
468     if (!(pvar->ssh_state.status_flags & STATUS_DONT_SEND_USER_NAME)) {
469     KillTimer(dlg, IDC_TIMER1);
470     SendMessage(dlg, WM_COMMAND, IDOK, 0);
471     }
472 yutakakn 2739 return TRUE;
473    
474 yutakakn 2728 case WM_COMMAND:
475     pvar = (PTInstVar) GetWindowLong(dlg, DWL_USER);
476    
477     switch (LOWORD(wParam)) {
478     case IDOK:
479 yutakakn 2783 // �F�������������������A�F���f�[�^�����M�����B�����������A�������B(2001.1.25 yutaka)
480     if ((pvar->ssh_state.status_flags & STATUS_DONT_SEND_USER_NAME)) {
481     return FALSE;
482     }
483 yutakakn 2728 return end_auth_dlg(pvar, dlg);
484    
485     case IDCANCEL: /* kill the connection */
486     pvar->auth_state.auth_dialog = NULL;
487     notify_closed_connection(pvar);
488     EndDialog(dlg, 0);
489     return TRUE;
490    
491     case IDC_SSHUSEPASSWORD:
492     case IDC_SSHUSERSA:
493     case IDC_SSHUSERHOSTS:
494     case IDC_SSHUSETIS:
495     set_auth_options_status(dlg, LOWORD(wParam));
496     return TRUE;
497    
498     case IDC_CHOOSERSAFILE:
499     choose_RSA_key_file(dlg);
500     return TRUE;
501    
502     case IDC_CHOOSEHOSTRSAFILE:
503     choose_host_RSA_key_file(dlg);
504     return TRUE;
505    
506     default:
507     return FALSE;
508     }
509    
510     default:
511     return FALSE;
512     }
513     }
514    
515     char FAR *AUTH_get_user_name(PTInstVar pvar)
516     {
517     return pvar->auth_state.user;
518     }
519    
520     int AUTH_set_supported_auth_types(PTInstVar pvar, int types)
521     {
522     char buf[1024];
523    
524     _snprintf(buf, sizeof(buf),
525     "Server reports supported authentication method mask = %d",
526     types);
527     buf[sizeof(buf) - 1] = 0;
528     notify_verbose_message(pvar, buf, LOG_LEVEL_VERBOSE);
529    
530     if (SSHv1(pvar)) {
531     types &= (1 << SSH_AUTH_PASSWORD) | (1 << SSH_AUTH_RSA)
532     | (1 << SSH_AUTH_RHOSTS_RSA) | (1 << SSH_AUTH_RHOSTS)
533     | (1 << SSH_AUTH_TIS);
534     } else {
535     // for SSH2(yutaka)
536 yutakakn 2762 // types &= (1 << SSH_AUTH_PASSWORD);
537     // ���J���F�����L�������� (2004.12.18 yutaka)
538     types &= (1 << SSH_AUTH_PASSWORD) | (1 << SSH_AUTH_RSA)
539     | (1 << SSH_AUTH_DSA);
540 yutakakn 2728 }
541     pvar->auth_state.supported_types = types;
542    
543     if (types == 0) {
544     notify_fatal_error(pvar,
545     "Server does not support any of the authentication options\n"
546     "provided by TTSSH. This connection will now close.");
547     return 0;
548     } else {
549     if (pvar->auth_state.auth_dialog != NULL) {
550     update_server_supported_types(pvar,
551     pvar->auth_state.auth_dialog);
552     }
553    
554     return 1;
555     }
556     }
557    
558     static void start_user_auth(PTInstVar pvar)
559     {
560 yutakakn 2739 // �F���_�C�A���O���\�������� (2004.12.1 yutaka)
561 yutakakn 2728 PostMessage(pvar->NotificationWindow, WM_COMMAND, (WPARAM) ID_SSHAUTH,
562     (LPARAM) NULL);
563     pvar->auth_state.cur_cred.method = SSH_AUTH_NONE;
564     }
565    
566     static void try_default_auth(PTInstVar pvar)
567     {
568     if (pvar->session_settings.TryDefaultAuth) {
569     switch (pvar->session_settings.DefaultAuthMethod) {
570     case SSH_AUTH_RSA:{
571     BOOL invalid_passphrase;
572     char password[] = "";
573    
574     pvar->auth_state.cur_cred.key_pair
575     =
576     KEYFILES_read_private_key(pvar,
577     pvar->session_settings.
578     DefaultRSAPrivateKeyFile,
579     password,
580     &invalid_passphrase, TRUE);
581     if (pvar->auth_state.cur_cred.key_pair == NULL) {
582     return;
583     } else {
584     pvar->auth_state.cur_cred.method = SSH_AUTH_RSA;
585     }
586     break;
587     }
588    
589     case SSH_AUTH_RHOSTS:
590     if (pvar->session_settings.
591     DefaultRhostsHostPrivateKeyFile[0] != 0) {
592     BOOL invalid_passphrase;
593     char password[] = "";
594    
595     pvar->auth_state.cur_cred.key_pair
596     =
597     KEYFILES_read_private_key(pvar,
598     pvar->session_settings.
599     DefaultRhostsHostPrivateKeyFile,
600     password,
601     &invalid_passphrase, TRUE);
602     if (pvar->auth_state.cur_cred.key_pair == NULL) {
603     return;
604     } else {
605     pvar->auth_state.cur_cred.method = SSH_AUTH_RHOSTS_RSA;
606     }
607     } else {
608     pvar->auth_state.cur_cred.method = SSH_AUTH_RHOSTS;
609     }
610    
611     pvar->auth_state.cur_cred.rhosts_client_user =
612     _strdup(pvar->session_settings.DefaultRhostsLocalUserName);
613     break;
614    
615     case SSH_AUTH_PASSWORD:
616     pvar->auth_state.cur_cred.password = _strdup("");
617     pvar->auth_state.cur_cred.method = SSH_AUTH_PASSWORD;
618     break;
619    
620     case SSH_AUTH_TIS:
621     default:
622     return;
623     }
624    
625     pvar->auth_state.user =
626     _strdup(pvar->session_settings.DefaultUserName);
627     }
628     }
629    
630     void AUTH_notify_end_error(PTInstVar pvar)
631     {
632     if ((pvar->auth_state.flags & AUTH_START_USER_AUTH_ON_ERROR_END) != 0) {
633     start_user_auth(pvar);
634     pvar->auth_state.flags &= ~AUTH_START_USER_AUTH_ON_ERROR_END;
635     }
636     }
637    
638     void AUTH_advance_to_next_cred(PTInstVar pvar)
639     {
640     pvar->auth_state.failed_method = pvar->auth_state.cur_cred.method;
641    
642     if (pvar->auth_state.cur_cred.method == SSH_AUTH_NONE) {
643     try_default_auth(pvar);
644    
645     if (pvar->auth_state.cur_cred.method == SSH_AUTH_NONE) {
646     if (pvar->err_msg != NULL) {
647     pvar->auth_state.flags |=
648     AUTH_START_USER_AUTH_ON_ERROR_END;
649     } else {
650 yutakakn 2739 // �������F���_�C�A���O���o�������� (2004.12.1 yutaka)
651     // �R�}���h���C���w������������
652 yutakakn 2728 start_user_auth(pvar);
653     }
654     }
655     } else {
656 yutakakn 2739 // �������F���_�C�A���O���o�������� (2004.12.1 yutaka)
657     // �R�}���h���C���w������(/auth=xxxx)������
658 yutakakn 2728 start_user_auth(pvar);
659     }
660     }
661    
662     static void init_TIS_dlg(PTInstVar pvar, HWND dlg)
663     {
664     init_auth_machine_banner(pvar, dlg);
665     init_password_control(dlg);
666    
667     if (pvar->auth_state.TIS_prompt != NULL) {
668     if (strlen(pvar->auth_state.TIS_prompt) > 10000) {
669     pvar->auth_state.TIS_prompt[10000] = 0;
670     }
671     SetDlgItemText(dlg, IDC_SSHAUTHBANNER2,
672     pvar->auth_state.TIS_prompt);
673     destroy_malloced_string(&pvar->auth_state.TIS_prompt);
674     }
675     }
676    
677     static BOOL end_TIS_dlg(PTInstVar pvar, HWND dlg)
678     {
679     char FAR *password =
680     alloc_control_text(GetDlgItem(dlg, IDC_SSHPASSWORD));
681    
682     pvar->auth_state.cur_cred.method = SSH_AUTH_TIS;
683     pvar->auth_state.cur_cred.password = password;
684     pvar->auth_state.auth_dialog = NULL;
685    
686     SSH_notify_cred(pvar);
687    
688     EndDialog(dlg, 1);
689     return TRUE;
690     }
691    
692     static BOOL CALLBACK TIS_dlg_proc(HWND dlg, UINT msg, WPARAM wParam,
693     LPARAM lParam)
694     {
695     PTInstVar pvar;
696    
697     switch (msg) {
698     case WM_INITDIALOG:
699     pvar = (PTInstVar) lParam;
700     pvar->auth_state.auth_dialog = dlg;
701     SetWindowLong(dlg, DWL_USER, lParam);
702    
703     init_TIS_dlg(pvar, dlg);
704     return FALSE; /* because we set the focus */
705    
706     case WM_COMMAND:
707     pvar = (PTInstVar) GetWindowLong(dlg, DWL_USER);
708    
709     switch (LOWORD(wParam)) {
710     case IDOK:
711     return end_TIS_dlg(pvar, dlg);
712    
713     case IDCANCEL: /* kill the connection */
714     pvar->auth_state.auth_dialog = NULL;
715     notify_closed_connection(pvar);
716     EndDialog(dlg, 0);
717     return TRUE;
718    
719     default:
720     return FALSE;
721     }
722    
723     default:
724     return FALSE;
725     }
726     }
727    
728     void AUTH_do_cred_dialog(PTInstVar pvar)
729     {
730     if (pvar->auth_state.auth_dialog == NULL) {
731     HWND cur_active = GetActiveWindow();
732     DLGPROC dlg_proc;
733     LPCTSTR dialog_template;
734    
735     switch (pvar->auth_state.mode) {
736     case TIS_AUTH_MODE:
737     dialog_template = MAKEINTRESOURCE(IDD_SSHTISAUTH);
738     dlg_proc = TIS_dlg_proc;
739     break;
740     case GENERIC_AUTH_MODE:
741     default:
742     dialog_template = MAKEINTRESOURCE(IDD_SSHAUTH);
743     dlg_proc = auth_dlg_proc;
744     }
745    
746     if (!DialogBoxParam(hInst, dialog_template,
747     cur_active !=
748     NULL ? cur_active : pvar->NotificationWindow,
749     dlg_proc, (LPARAM) pvar) == -1) {
750     notify_fatal_error(pvar,
751     "Unable to display authentication dialog box.\n"
752     "Connection terminated.");
753     }
754     }
755     }
756    
757     static void init_default_auth_dlg(PTInstVar pvar, HWND dlg)
758     {
759     switch (pvar->settings.DefaultAuthMethod) {
760     case SSH_AUTH_RSA:
761     CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
762     IDC_SSHUSERSA);
763     break;
764     case SSH_AUTH_RHOSTS:
765     case SSH_AUTH_RHOSTS_RSA:
766     CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
767     IDC_SSHUSERHOSTS);
768     break;
769     case SSH_AUTH_TIS:
770     CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
771     IDC_SSHUSETIS);
772     break;
773     case SSH_AUTH_PASSWORD:
774     default:
775     CheckRadioButton(dlg, IDC_SSHUSEPASSWORD, MAX_AUTH_CONTROL,
776     IDC_SSHUSEPASSWORD);
777     }
778    
779     SetDlgItemText(dlg, IDC_SSHUSERNAME, pvar->settings.DefaultUserName);
780     SetDlgItemText(dlg, IDC_RSAFILENAME,
781     pvar->settings.DefaultRSAPrivateKeyFile);
782     SetDlgItemText(dlg, IDC_HOSTRSAFILENAME,
783     pvar->settings.DefaultRhostsHostPrivateKeyFile);
784     SetDlgItemText(dlg, IDC_LOCALUSERNAME,
785     pvar->settings.DefaultRhostsLocalUserName);
786     }
787    
788     static BOOL end_default_auth_dlg(PTInstVar pvar, HWND dlg)
789     {
790     if (IsDlgButtonChecked(dlg, IDC_SSHUSERSA)) {
791     pvar->settings.DefaultAuthMethod = SSH_AUTH_RSA;
792     } else if (IsDlgButtonChecked(dlg, IDC_SSHUSERHOSTS)) {
793     if (GetWindowTextLength(GetDlgItem(dlg, IDC_HOSTRSAFILENAME)) > 0) {
794     pvar->settings.DefaultAuthMethod = SSH_AUTH_RHOSTS_RSA;
795     } else {
796     pvar->settings.DefaultAuthMethod = SSH_AUTH_RHOSTS;
797     }
798     } else if (IsDlgButtonChecked(dlg, IDC_SSHUSETIS)) {
799     pvar->settings.DefaultAuthMethod = SSH_AUTH_TIS;
800     } else {
801     pvar->settings.DefaultAuthMethod = SSH_AUTH_PASSWORD;
802     }
803    
804     GetDlgItemText(dlg, IDC_SSHUSERNAME, pvar->settings.DefaultUserName,
805     sizeof(pvar->settings.DefaultUserName));
806     GetDlgItemText(dlg, IDC_RSAFILENAME,
807     pvar->settings.DefaultRSAPrivateKeyFile,
808     sizeof(pvar->settings.DefaultRSAPrivateKeyFile));
809     GetDlgItemText(dlg, IDC_HOSTRSAFILENAME,
810     pvar->settings.DefaultRhostsHostPrivateKeyFile,
811     sizeof(pvar->settings.DefaultRhostsHostPrivateKeyFile));
812     GetDlgItemText(dlg, IDC_LOCALUSERNAME,
813     pvar->settings.DefaultRhostsLocalUserName,
814     sizeof(pvar->settings.DefaultRhostsLocalUserName));
815    
816     EndDialog(dlg, 1);
817     return TRUE;
818     }
819    
820     static BOOL CALLBACK default_auth_dlg_proc(HWND dlg, UINT msg,
821     WPARAM wParam, LPARAM lParam)
822     {
823     PTInstVar pvar;
824    
825     switch (msg) {
826     case WM_INITDIALOG:
827     pvar = (PTInstVar) lParam;
828     SetWindowLong(dlg, DWL_USER, lParam);
829    
830     init_default_auth_dlg(pvar, dlg);
831     return TRUE; /* because we do not set the focus */
832    
833     case WM_COMMAND:
834     pvar = (PTInstVar) GetWindowLong(dlg, DWL_USER);
835    
836     switch (LOWORD(wParam)) {
837     case IDOK:
838     return end_default_auth_dlg(pvar, dlg);
839    
840     case IDCANCEL:
841     EndDialog(dlg, 0);
842     return TRUE;
843    
844     case IDC_CHOOSERSAFILE:
845     choose_RSA_key_file(dlg);
846     return TRUE;
847    
848     case IDC_CHOOSEHOSTRSAFILE:
849     choose_host_RSA_key_file(dlg);
850     return TRUE;
851    
852     default:
853     return FALSE;
854     }
855    
856     default:
857     return FALSE;
858     }
859     }
860    
861     void AUTH_init(PTInstVar pvar)
862     {
863     pvar->auth_state.failed_method = SSH_AUTH_NONE;
864     pvar->auth_state.auth_dialog = NULL;
865     pvar->auth_state.user = NULL;
866     pvar->auth_state.flags = 0;
867     pvar->auth_state.TIS_prompt = NULL;
868     pvar->auth_state.supported_types = 0;
869     pvar->auth_state.cur_cred.method = SSH_AUTH_NONE;
870     pvar->auth_state.cur_cred.password = NULL;
871     pvar->auth_state.cur_cred.rhosts_client_user = NULL;
872     pvar->auth_state.cur_cred.key_pair = NULL;
873     AUTH_set_generic_mode(pvar);
874     }
875    
876     void AUTH_set_generic_mode(PTInstVar pvar)
877     {
878     pvar->auth_state.mode = GENERIC_AUTH_MODE;
879     destroy_malloced_string(&pvar->auth_state.TIS_prompt);
880     }
881    
882     void AUTH_set_TIS_mode(PTInstVar pvar, char FAR * prompt, int len)
883     {
884     if (pvar->auth_state.cur_cred.method == SSH_AUTH_TIS) {
885     pvar->auth_state.mode = TIS_AUTH_MODE;
886    
887     destroy_malloced_string(&pvar->auth_state.TIS_prompt);
888     pvar->auth_state.TIS_prompt = malloc(len + 1);
889     memcpy(pvar->auth_state.TIS_prompt, prompt, len);
890     pvar->auth_state.TIS_prompt[len] = 0;
891     } else {
892     AUTH_set_generic_mode(pvar);
893     }
894     }
895    
896     void AUTH_do_default_cred_dialog(PTInstVar pvar)
897     {
898     HWND cur_active = GetActiveWindow();
899    
900     if (DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SSHAUTHSETUP),
901     cur_active !=
902     NULL ? cur_active : pvar->NotificationWindow,
903     default_auth_dlg_proc, (LPARAM) pvar) == -1) {
904     notify_nonfatal_error(pvar,
905     "Unable to display authentication setup dialog box.");
906     }
907     }
908    
909     void AUTH_destroy_cur_cred(PTInstVar pvar)
910     {
911     destroy_malloced_string(&pvar->auth_state.cur_cred.password);
912     destroy_malloced_string(&pvar->auth_state.cur_cred.rhosts_client_user);
913     if (pvar->auth_state.cur_cred.key_pair != NULL) {
914     CRYPT_free_key_pair(pvar->auth_state.cur_cred.key_pair);
915     pvar->auth_state.cur_cred.key_pair = NULL;
916     }
917     }
918    
919     static char FAR *get_auth_method_name(SSHAuthMethod auth)
920     {
921     switch (auth) {
922     case SSH_AUTH_PASSWORD:
923     return "password";
924     case SSH_AUTH_RSA:
925     return "RSA";
926     case SSH_AUTH_RHOSTS:
927     return "rhosts";
928     case SSH_AUTH_RHOSTS_RSA:
929     return "rhosts with RSA";
930     case SSH_AUTH_TIS:
931     return "challenge/response (TIS)";
932     default:
933     return "unknown method";
934     }
935     }
936    
937     void AUTH_get_auth_info(PTInstVar pvar, char FAR * dest, int len)
938     {
939 yutakakn 2782 char *method = "unknown";
940    
941 yutakakn 2728 if (pvar->auth_state.user == NULL) {
942     strncpy(dest, "None", len);
943     } else if (pvar->auth_state.cur_cred.method != SSH_AUTH_NONE) {
944 yutakakn 2762 if (SSHv1(pvar)) {
945     _snprintf(dest, len, "User '%s', using %s", pvar->auth_state.user,
946     get_auth_method_name(pvar->auth_state.cur_cred.method));
947    
948     } else { // SSH2:�F�����\�b�h������ (2004.12.23 yutaka)
949     if (pvar->auth_state.cur_cred.method == SSH_AUTH_PASSWORD) {
950 yutakakn 2782 // keyboard-interactive���\�b�h������ (2005.1.24 yutaka)
951     if (pvar->keyboard_interactive_done == 1) {
952     method = "keyboard-interactive";
953     } else {
954     method = get_auth_method_name(pvar->auth_state.cur_cred.method);
955     }
956     _snprintf(dest, len, "User '%s', using %s", pvar->auth_state.user, method);
957    
958 yutakakn 2762 } else {
959     if (pvar->auth_state.cur_cred.key_pair->RSA_key != NULL) {
960     method = "RSA";
961     } else if (pvar->auth_state.cur_cred.key_pair->DSA_key != NULL) {
962     method = "DSA";
963     }
964     _snprintf(dest, len, "User '%s', using %s", pvar->auth_state.user, method);
965     }
966    
967     }
968    
969 yutakakn 2728 } else {
970     _snprintf(dest, len, "User '%s', using %s", pvar->auth_state.user,
971     get_auth_method_name(pvar->auth_state.failed_method));
972     }
973    
974     dest[len - 1] = 0;
975     }
976    
977     void AUTH_notify_disconnecting(PTInstVar pvar)
978     {
979     if (pvar->auth_state.auth_dialog != NULL) {
980     PostMessage(pvar->auth_state.auth_dialog, WM_COMMAND, IDCANCEL, 0);
981     /* the main window might not go away if it's not enabled. (see vtwin.cpp) */
982     EnableWindow(pvar->NotificationWindow, TRUE);
983     }
984     }
985    
986     void AUTH_end(PTInstVar pvar)
987     {
988     destroy_malloced_string(&pvar->auth_state.user);
989     destroy_malloced_string(&pvar->auth_state.TIS_prompt);
990    
991     AUTH_destroy_cur_cred(pvar);
992     }
993 yutakakn 2739
994     /*
995     * $Log: not supported by cvs2svn $
996 yutakakn 2784 * Revision 1.7 2005/01/25 13:38:22 yutakakn
997     * SSH�F���_�C�A���O���ARhosts/TIS���O���[�������O���AEnter�L�[�������������A
998     * �A�v���P�[�V�����G���[���������������������B
999     *
1000 yutakakn 2783 * Revision 1.6 2005/01/24 14:07:07 yutakakn
1001     * �Ekeyboard-interactive�F�����T�|�[�g�����B
1002     * �@�����������Ateraterm.ini�� "KeyboardInteractive" �G���g�������������B
1003     * �E�o�[�W�����_�C�A���O�� OpenSSL�o�[�W���� ������
1004     *
1005 yutakakn 2782 * Revision 1.5 2004/12/27 14:35:41 yutakakn
1006     * SSH2�����������������s�����G���[���b�Z�[�W�����������B
1007     *
1008 yutakakn 2769 * Revision 1.4 2004/12/22 17:28:14 yutakakn
1009     * SSH2���J���F��(RSA/DSA)���T�|�[�g�����B
1010     *
1011 yutakakn 2762 * Revision 1.3 2004/12/16 13:01:09 yutakakn
1012     * SSH�������O�C�����A�v���P�[�V�����G���[�������������C�������B
1013     *
1014 yutakakn 2752 * Revision 1.2 2004/12/01 15:37:49 yutakakn
1015     * SSH2�������O�C���@�\�������B
1016     * �����A�p�X���[�h�F�������������B
1017     * �E�R�}���h���C��
1018     * /ssh /auth=�F�����\�b�h /user=���[�U�� /passwd=�p�X���[�h
1019     *
1020 yutakakn 2739 */

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