Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/ttssh2/ttxssh/hosts.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9165 - (hide annotations) (download) (as text)
Thu Feb 18 14:37:05 2021 UTC (3 years, 1 month ago) by nmaya
File MIME type: text/x-csrc
File size: 68515 byte(s)
Visual C 2015 から tmpnam() がフルパスを返すようになったため、バージョン判定の分岐を追加

https://docs.microsoft.com/en-us/cpp/porting/visual-cpp-change-history-2003-2015?view=msvc-160
tmpnam Generates Usable File Names
1 maya 3227 /*
2 doda 6841 * Copyright (c) 1998-2001, Robert O'Callahan
3 nmaya 9048 * (C) 2004- TeraTerm Project
4 doda 6841 * All rights reserved.
5     *
6     * Redistribution and use in source and binary forms, with or without
7     * modification, are permitted provided that the following conditions
8     * are met:
9     *
10     * 1. Redistributions of source code must retain the above copyright
11     * notice, this list of conditions and the following disclaimer.
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the distribution.
15     * 3. The name of the author may not be used to endorse or promote products
16     * derived from this software without specific prior written permission.
17     *
18     * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21     * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28     */
29 maya 3227
30     /*
31     This code is copyright (C) 1998-1999 Robert O'Callahan.
32     See LICENSE.TXT for the license.
33     */
34     #include "ttxssh.h"
35     #include "util.h"
36     #include "resource.h"
37     #include "matcher.h"
38     #include "ssh.h"
39 maya 4304 #include "key.h"
40 maya 3227 #include "hosts.h"
41 doda 4569 #include "dns.h"
42 zmatsuo 7560 #include "dlglib.h"
43 zmatsuo 7714 #include "compat_win.h"
44 maya 3227
45     #include <openssl/bn.h>
46     #include <openssl/evp.h>
47     #include <openssl/rsa.h>
48     #include <openssl/dsa.h>
49    
50     #include <fcntl.h>
51     #include <io.h>
52     #include <errno.h>
53     #include <sys/stat.h>
54     #include <direct.h>
55 doda 4531 #include <memory.h>
56 maya 3227
57 zmatsuo 8542 #include "codeconv.h"
58     #include "layer_for_unicode.h"
59 zmatsuo 8593 #include "asprintf.h"
60 doda 4531
61 zmatsuo 7560 #undef DialogBoxParam
62     #define DialogBoxParam(p1,p2,p3,p4,p5) \
63     TTDialogBoxParam(p1,p2,p3,p4,p5)
64     #undef EndDialog
65     #define EndDialog(p1,p2) \
66     TTEndDialog(p1, p2)
67 maya 3227
68     // BASE64�\���������i��������'='�����������������j
69     static char base64[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
70    
71    
72 yutakapon 8093 BOOL HOSTS_resume_session_after_known_hosts(PTInstVar pvar);
73     void HOSTS_cancel_session_after_known_hosts(PTInstVar pvar);
74    
75    
76 doda 6801 static char **parse_multi_path(char *buf)
77 maya 3227 {
78     int i;
79     int ch;
80     int num_paths = 1;
81 doda 6801 char ** result;
82 maya 3227 int last_path_index;
83    
84     for (i = 0; (ch = buf[i]) != 0; i++) {
85     if (ch == ';') {
86     num_paths++;
87     }
88     }
89    
90     result =
91 doda 6801 (char **) malloc(sizeof(char *) * (num_paths + 1));
92 maya 3227
93     last_path_index = 0;
94     num_paths = 0;
95     for (i = 0; (ch = buf[i]) != 0; i++) {
96     if (ch == ';') {
97     buf[i] = 0;
98     result[num_paths] = _strdup(buf + last_path_index);
99     num_paths++;
100     buf[i] = ch;
101     last_path_index = i + 1;
102     }
103     }
104     if (i > last_path_index) {
105     result[num_paths] = _strdup(buf + last_path_index);
106     num_paths++;
107     }
108     result[num_paths] = NULL;
109     return result;
110     }
111    
112     void HOSTS_init(PTInstVar pvar)
113     {
114     pvar->hosts_state.prefetched_hostname = NULL;
115 maya 6147 key_init(&pvar->hosts_state.hostkey);
116 maya 3227 pvar->hosts_state.hosts_dialog = NULL;
117     pvar->hosts_state.file_names = NULL;
118 yutakapon 8093
119     /*
120     * �O�����I�v�V�����w��(/nosecuritywarning)���c���������������������������B
121     */
122     pvar->nocheck_known_hosts = FALSE;
123 maya 3227 }
124    
125     void HOSTS_open(PTInstVar pvar)
126     {
127     pvar->hosts_state.file_names =
128     parse_multi_path(pvar->session_settings.KnownHostsFiles);
129     }
130    
131     //
132     // known_hosts�t�@�C�������e�������� pvar->hosts_state.file_data ����������
133     //
134 doda 6801 static int begin_read_file(PTInstVar pvar, char *name,
135 maya 3227 int suppress_errors)
136     {
137     int fd;
138     int length;
139     int amount_read;
140     char buf[2048];
141    
142     get_teraterm_dir_relative_name(buf, sizeof(buf), name);
143     fd = _open(buf, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY);
144     if (fd == -1) {
145     if (!suppress_errors) {
146     if (errno == ENOENT) {
147     UTIL_get_lang_msg("MSG_HOSTS_READ_ENOENT_ERROR", pvar,
148     "An error occurred while trying to read a known_hosts file.\n"
149     "The specified filename does not exist.");
150     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
151     } else {
152     UTIL_get_lang_msg("MSG_HOSTS_READ_ERROR", pvar,
153     "An error occurred while trying to read a known_hosts file.");
154     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
155     }
156     }
157     return 0;
158     }
159    
160     length = (int) _lseek(fd, 0, SEEK_END);
161     _lseek(fd, 0, SEEK_SET);
162    
163     if (length >= 0 && length < 0x7FFFFFFF) {
164     pvar->hosts_state.file_data = malloc(length + 1);
165     if (pvar->hosts_state.file_data == NULL) {
166     if (!suppress_errors) {
167     UTIL_get_lang_msg("MSG_HOSTS_ALLOC_ERROR", pvar,
168     "Memory ran out while trying to allocate space to read a known_hosts file.");
169     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
170     }
171     _close(fd);
172     return 0;
173     }
174     } else {
175     if (!suppress_errors) {
176     UTIL_get_lang_msg("MSG_HOSTS_READ_ERROR", pvar,
177     "An error occurred while trying to read a known_hosts file.");
178     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
179     }
180     _close(fd);
181     return 0;
182     }
183    
184     amount_read = _read(fd, pvar->hosts_state.file_data, length);
185     pvar->hosts_state.file_data[length] = 0;
186    
187     _close(fd);
188    
189     if (amount_read != length) {
190     if (!suppress_errors) {
191     UTIL_get_lang_msg("MSG_HOSTS_READ_ERROR", pvar,
192     "An error occurred while trying to read a known_hosts file.");
193     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
194     }
195     free(pvar->hosts_state.file_data);
196     pvar->hosts_state.file_data = NULL;
197     return 0;
198     } else {
199     return 1;
200     }
201     }
202    
203     static int end_read_file(PTInstVar pvar, int suppress_errors)
204     {
205     free(pvar->hosts_state.file_data);
206     pvar->hosts_state.file_data = NULL;
207     return 1;
208     }
209    
210     static int begin_read_host_files(PTInstVar pvar, int suppress_errors)
211     {
212     pvar->hosts_state.file_num = 0;
213     pvar->hosts_state.file_data = NULL;
214     return 1;
215     }
216    
217     // MIME64�����������X�L�b�v����
218 doda 6801 static int eat_base64(char *data)
219 maya 3227 {
220     int index = 0;
221     int ch;
222    
223     for (;;) {
224     ch = data[index];
225     if (ch == '=' || strchr(base64, ch)) {
226     // BASE64���\�������������������� index ���i����
227     index++;
228     } else {
229     break;
230     }
231     }
232    
233     return index;
234     }
235    
236 doda 6801 static int eat_spaces(char *data)
237 maya 3227 {
238     int index = 0;
239     int ch;
240    
241     while ((ch = data[index]) == ' ' || ch == '\t') {
242     index++;
243     }
244     return index;
245     }
246    
247 doda 6801 static int eat_digits(char *data)
248 maya 3227 {
249     int index = 0;
250     int ch;
251    
252     while ((ch = data[index]) >= '0' && ch <= '9') {
253     index++;
254     }
255     return index;
256     }
257    
258 doda 6801 static int eat_to_end_of_line(char *data)
259 maya 3227 {
260     int index = 0;
261     int ch;
262    
263     while ((ch = data[index]) != '\n' && ch != '\r' && ch != 0) {
264     index++;
265     }
266    
267     while ((ch = data[index]) == '\n' || ch == '\r') {
268     index++;
269     }
270    
271     return index;
272     }
273    
274 doda 6801 static int eat_to_end_of_pattern(char *data)
275 maya 3227 {
276     int index = 0;
277     int ch;
278    
279     while (ch = data[index], is_pattern_char(ch)) {
280     index++;
281     }
282    
283     return index;
284     }
285    
286     // SSH2���� BASE64 �`�����i�[����������
287 doda 6621 static Key *parse_base64data(char *data)
288 maya 3227 {
289     int count;
290     unsigned char *blob = NULL;
291     int len, n;
292     Key *key = NULL;
293     char ch;
294    
295     // BASE64���������T�C�Y������
296     count = eat_base64(data);
297     len = 2 * count;
298     blob = malloc(len);
299     if (blob == NULL)
300     goto error;
301    
302     // BASE64�f�R�[�h
303     ch = data[count];
304     data[count] = '\0'; // ���������s�R�[�h������������������������������������
305 doda 6621 n = b64decode(blob, len, data);
306 maya 3227 data[count] = ch;
307     if (n < 0) {
308     goto error;
309     }
310    
311     key = key_from_blob(blob, n);
312     if (key == NULL)
313     goto error;
314    
315     error:
316     if (blob != NULL)
317     free(blob);
318    
319     return (key);
320     }
321    
322    
323 doda 6801 static char *parse_bignum(char *data)
324 maya 3227 {
325     uint32 digits = 0;
326     BIGNUM *num = BN_new();
327     BIGNUM *billion = BN_new();
328     BIGNUM *digits_num = BN_new();
329     BN_CTX *ctx = BN_CTX_new();
330 doda 6801 char *result;
331 maya 3227 int ch;
332     int leftover_digits = 1;
333    
334 yutakapon 8316 // BN_CTX_init������ OpenSSL 1.1.0 �������������B
335     // OpenSSL 1.0.2�����_�������� deprecated �����������B
336 maya 3227 BN_set_word(num, 0);
337     BN_set_word(billion, 1000000000L);
338    
339     while ((ch = *data) >= '0' && ch <= '9') {
340     if (leftover_digits == 1000000000L) {
341     BN_set_word(digits_num, digits);
342     BN_mul(num, num, billion, ctx);
343     BN_add(num, num, digits_num);
344     leftover_digits = 1;
345     digits = 0;
346     }
347    
348     digits = digits * 10 + ch - '0';
349     leftover_digits *= 10;
350     data++;
351     }
352    
353     BN_set_word(digits_num, digits);
354     BN_set_word(billion, leftover_digits);
355     BN_mul(num, num, billion, ctx);
356     BN_add(num, num, digits_num);
357    
358 doda 6801 result = (char *) malloc(2 + BN_num_bytes(num));
359 maya 3227 set_ushort16_MSBfirst(result, BN_num_bits(num));
360     BN_bn2bin(num, result + 2);
361    
362     BN_CTX_free(ctx);
363     BN_free(digits_num);
364     BN_free(num);
365     BN_free(billion);
366    
367     return result;
368     }
369    
370     //
371     // known_hosts�t�@�C�������e���������A�w�������z�X�g�����J�����T���B
372     //
373 doda 6801 static int check_host_key(PTInstVar pvar, char *hostname,
374     unsigned short tcpport, char *data,
375 maya 6152 Key *key)
376 maya 3227 {
377     int index = eat_spaces(data);
378     int matched = 0;
379     int keybits = 0;
380    
381     if (data[index] == '#') {
382     return index + eat_to_end_of_line(data + index);
383     }
384    
385     /* if we find an empty line, then it won't have any patterns matching the hostname
386     and so we skip it */
387     index--;
388     do {
389     int negated;
390     int bracketed;
391     char *end_bracket;
392     int host_matched = 0;
393     unsigned short keyfile_port = 22;
394    
395     index++;
396     negated = data[index] == '!';
397    
398     if (negated) {
399     index++;
400     bracketed = data[index] == '[';
401     if (bracketed) {
402     end_bracket = strstr(data + index + 1, "]:");
403     if (end_bracket != NULL) {
404     *end_bracket = ' ';
405     index++;
406     }
407     }
408     host_matched = match_pattern(data + index, hostname);
409     if (bracketed && end_bracket != NULL) {
410     *end_bracket = ']';
411     keyfile_port = atoi(end_bracket + 2);
412     }
413     if (host_matched && keyfile_port == tcpport) {
414     return index + eat_to_end_of_line(data + index);
415     }
416     } else {
417     bracketed = data[index] == '[';
418     if (bracketed) {
419     end_bracket = strstr(data + index + 1, "]:");
420     if (end_bracket != NULL) {
421     *end_bracket = ' ';
422     index++;
423     }
424     }
425     host_matched = match_pattern(data + index, hostname);
426     if (bracketed && end_bracket != NULL) {
427     *end_bracket = ']';
428     keyfile_port = atoi(end_bracket + 2);
429     }
430     if (host_matched && keyfile_port == tcpport) {
431     matched = 1;
432     }
433     }
434    
435     index += eat_to_end_of_pattern(data + index);
436     } while (data[index] == ',');
437    
438     if (!matched) {
439     return index + eat_to_end_of_line(data + index);
440     } else {
441     // ���������������t�H�[�}�b�g��������
442     // �����A���������v�����G���g�����������������������B
443     /*
444     [SSH1]
445     192.168.1.2 1024 35 13032....
446    
447     [SSH2]
448     192.168.1.2 ssh-rsa AAAAB3NzaC1....
449     192.168.1.2 ssh-dss AAAAB3NzaC1....
450     192.168.1.2 rsa AAAAB3NzaC1....
451     192.168.1.2 dsa AAAAB3NzaC1....
452     192.168.1.2 rsa1 AAAAB3NzaC1....
453     */
454     int rsa1_key_bits;
455    
456     index += eat_spaces(data + index);
457    
458     rsa1_key_bits = atoi(data + index);
459     if (rsa1_key_bits > 0) { // RSA1������
460     if (!SSHv1(pvar)) { // SSH2��������������������
461     return index + eat_to_end_of_line(data + index);
462     }
463    
464 maya 6152 key->type = KEY_RSA1;
465 maya 3227
466 maya 6152 key->bits = rsa1_key_bits;
467 maya 3227 index += eat_digits(data + index);
468     index += eat_spaces(data + index);
469    
470 maya 6152 key->exp = parse_bignum(data + index);
471 maya 3227 index += eat_digits(data + index);
472     index += eat_spaces(data + index);
473    
474 maya 6152 key->mod = parse_bignum(data + index);
475 maya 3227 } else {
476     char *cp, *p;
477 maya 6152 Key *key2;
478     ssh_keytype key_type;
479 maya 3227
480     if (!SSHv2(pvar)) { // SSH1��������������������
481     return index + eat_to_end_of_line(data + index);
482     }
483    
484     cp = data + index;
485     p = strchr(cp, ' ');
486     if (p == NULL) {
487     return index + eat_to_end_of_line(data + index);
488     }
489     index += (p - cp); // setup index
490     *p = '\0';
491 maya 6152 key_type = get_keytype_from_name(cp);
492 maya 3227 *p = ' ';
493    
494     index += eat_spaces(data + index); // update index
495    
496 doda 6621 // base64 decode
497     key2 = parse_base64data(data + index);
498 maya 6152 if (key2 == NULL) {
499 maya 3227 return index + eat_to_end_of_line(data + index);
500     }
501    
502     // setup
503 maya 6152 key->type = key2->type;
504     key->dsa = key2->dsa;
505     key->rsa = key2->rsa;
506     key->ecdsa = key2->ecdsa;
507     key->ed25519_pk = key2->ed25519_pk;
508 maya 3227
509     index += eat_base64(data + index);
510     index += eat_spaces(data + index);
511    
512     // Key�\�������g���������� (2008.3.2 yutaka)
513 maya 6152 free(key2);
514 maya 3227 }
515    
516     return index + eat_to_end_of_line(data + index);
517     }
518     }
519    
520     //
521     // known_hosts�t�@�C�������z�X�g�������v�����s������
522 maya 6152 // return_always
523     // 0: �������������T��
524     // 1: 1�s�����T��������
525 maya 3227 //
526     static int read_host_key(PTInstVar pvar,
527 doda 6801 char *hostname, unsigned short tcpport,
528 maya 6152 int suppress_errors, int return_always,
529     Key *key)
530 maya 3227 {
531     int i;
532     int while_flg;
533    
534     for (i = 0; hostname[i] != 0; i++) {
535     int ch = hostname[i];
536    
537     if (!is_pattern_char(ch) || ch == '*' || ch == '?') {
538     if (!suppress_errors) {
539     UTIL_get_lang_msg("MSG_HOSTS_HOSTNAME_INVALID_ERROR", pvar,
540     "The host name contains an invalid character.\n"
541     "This session will be terminated.");
542 maya 5678 notify_fatal_error(pvar, pvar->ts->UIMsg, TRUE);
543 maya 3227 }
544     return 0;
545     }
546     }
547    
548     if (i == 0) {
549     if (!suppress_errors) {
550     UTIL_get_lang_msg("MSG_HOSTS_HOSTNAME_EMPTY_ERROR", pvar,
551     "The host name should not be empty.\n"
552     "This session will be terminated.");
553 maya 5678 notify_fatal_error(pvar, pvar->ts->UIMsg, TRUE);
554 maya 3227 }
555     return 0;
556     }
557    
558     // hostkey type is KEY_UNSPEC.
559 maya 6152 key_init(key);
560 maya 3227
561     do {
562     if (pvar->hosts_state.file_data == NULL
563     || pvar->hosts_state.file_data[pvar->hosts_state.file_data_index] == 0) {
564 doda 6801 char *filename;
565 maya 3227 int keep_going = 1;
566    
567     if (pvar->hosts_state.file_data != NULL) {
568     end_read_file(pvar, suppress_errors);
569     }
570    
571     do {
572     filename =
573     pvar->hosts_state.file_names[pvar->hosts_state.file_num];
574    
575     if (filename == NULL) {
576     return 1;
577     } else {
578     pvar->hosts_state.file_num++;
579    
580     if (filename[0] != 0) {
581     if (begin_read_file(pvar, filename, suppress_errors)) {
582     pvar->hosts_state.file_data_index = 0;
583     keep_going = 0;
584     }
585     }
586     }
587     } while (keep_going);
588     }
589    
590     pvar->hosts_state.file_data_index +=
591     check_host_key(pvar, hostname, tcpport,
592     pvar->hosts_state.file_data +
593 maya 6152 pvar->hosts_state.file_data_index,
594     key);
595 maya 3227
596     if (!return_always) {
597     // �L�����L�[��������������
598 maya 6152 while_flg = (key->type == KEY_UNSPEC);
599 maya 3227 }
600     else {
601     while_flg = 0;
602     }
603     } while (while_flg);
604    
605     return 1;
606     }
607    
608     static void finish_read_host_files(PTInstVar pvar, int suppress_errors)
609     {
610     if (pvar->hosts_state.file_data != NULL) {
611     end_read_file(pvar, suppress_errors);
612     }
613     }
614    
615     // �T�[�o�����������O���Aknown_hosts�t�@�C�������z�X�g���J�������������������B
616 doda 6801 void HOSTS_prefetch_host_key(PTInstVar pvar, char *hostname, unsigned short tcpport)
617 maya 3227 {
618 maya 6152 Key key; // known_hosts���o�^������������
619    
620 maya 3227 if (!begin_read_host_files(pvar, 1)) {
621     return;
622     }
623    
624 maya 6152 memset(&key, 0, sizeof(key));
625     if (!read_host_key(pvar, hostname, tcpport, 1, 0, &key)) {
626 maya 3227 return;
627     }
628    
629 maya 6152 key_copy(&pvar->hosts_state.hostkey, &key);
630     key_init(&key);
631    
632 maya 3227 free(pvar->hosts_state.prefetched_hostname);
633     pvar->hosts_state.prefetched_hostname = _strdup(hostname);
634    
635     finish_read_host_files(pvar, 1);
636     }
637    
638 yutakapon 5846
639     // known_hosts�t�@�C�������Y�������L�[���������������B
640     //
641     // return:
642     // *keyptr != NULL ��������
643     //
644 doda 6801 static int parse_hostkey_file(PTInstVar pvar, char *hostname,
645     unsigned short tcpport, char *data, Key **keyptr)
646 yutakapon 5846 {
647     int index = eat_spaces(data);
648     int matched = 0;
649     int keybits = 0;
650     ssh_keytype ktype;
651 yutakapon 5847 Key *key;
652 yutakapon 5846
653     *keyptr = NULL;
654    
655     if (data[index] == '#') {
656     return index + eat_to_end_of_line(data + index);
657     }
658    
659     /* if we find an empty line, then it won't have any patterns matching the hostname
660     and so we skip it */
661     index--;
662     do {
663     int negated;
664     int bracketed;
665     char *end_bracket;
666     int host_matched = 0;
667     unsigned short keyfile_port = 22;
668    
669     index++;
670     negated = data[index] == '!';
671    
672     if (negated) {
673     index++;
674     bracketed = data[index] == '[';
675     if (bracketed) {
676     end_bracket = strstr(data + index + 1, "]:");
677     if (end_bracket != NULL) {
678     *end_bracket = ' ';
679     index++;
680     }
681     }
682     host_matched = match_pattern(data + index, hostname);
683     if (bracketed && end_bracket != NULL) {
684     *end_bracket = ']';
685     keyfile_port = atoi(end_bracket + 2);
686     }
687     if (host_matched && keyfile_port == tcpport) {
688     return index + eat_to_end_of_line(data + index);
689     }
690     }
691     else {
692     bracketed = data[index] == '[';
693     if (bracketed) {
694     end_bracket = strstr(data + index + 1, "]:");
695     if (end_bracket != NULL) {
696     *end_bracket = ' ';
697     index++;
698     }
699     }
700     host_matched = match_pattern(data + index, hostname);
701     if (bracketed && end_bracket != NULL) {
702     *end_bracket = ']';
703     keyfile_port = atoi(end_bracket + 2);
704     }
705     if (host_matched && keyfile_port == tcpport) {
706     matched = 1;
707     }
708     }
709    
710     index += eat_to_end_of_pattern(data + index);
711     } while (data[index] == ',');
712    
713     if (!matched) {
714     return index + eat_to_end_of_line(data + index);
715     }
716     else {
717     // ���������������t�H�[�}�b�g��������
718     // �����A���������v�����G���g�����������������������B
719     /*
720     [SSH1]
721     192.168.1.2 1024 35 13032....
722    
723     [SSH2]
724     192.168.1.2 ssh-rsa AAAAB3NzaC1....
725     192.168.1.2 ssh-dss AAAAB3NzaC1....
726     192.168.1.2 rsa AAAAB3NzaC1....
727     192.168.1.2 dsa AAAAB3NzaC1....
728     192.168.1.2 rsa1 AAAAB3NzaC1....
729     */
730     int rsa1_key_bits;
731    
732     index += eat_spaces(data + index);
733    
734     rsa1_key_bits = atoi(data + index);
735     if (rsa1_key_bits > 0) { // RSA1������
736     if (!SSHv1(pvar)) { // SSH2��������������������
737     return index + eat_to_end_of_line(data + index);
738     }
739 yutakapon 5847
740     key = key_new(KEY_RSA1);
741     key->bits = rsa1_key_bits;
742    
743 yutakapon 5846 index += eat_digits(data + index);
744     index += eat_spaces(data + index);
745 yutakapon 5847 key->exp = parse_bignum(data + index);
746 yutakapon 5846
747     index += eat_digits(data + index);
748     index += eat_spaces(data + index);
749 yutakapon 5847 key->mod = parse_bignum(data + index);
750 yutakapon 5846
751 yutakapon 5847 // setup
752     *keyptr = key;
753    
754 yutakapon 5846 }
755     else {
756     char *cp, *p;
757    
758     if (!SSHv2(pvar)) { // SSH1��������������������
759     return index + eat_to_end_of_line(data + index);
760     }
761    
762     cp = data + index;
763     p = strchr(cp, ' ');
764     if (p == NULL) {
765     return index + eat_to_end_of_line(data + index);
766     }
767     index += (p - cp); // setup index
768     *p = '\0';
769     ktype = get_keytype_from_name(cp);
770     *p = ' ';
771    
772     index += eat_spaces(data + index); // update index
773    
774 doda 6621 // base64 decode
775     key = parse_base64data(data + index);
776 yutakapon 5846 if (key == NULL) {
777     return index + eat_to_end_of_line(data + index);
778     }
779    
780     // setup
781     *keyptr = key;
782    
783     index += eat_base64(data + index);
784     index += eat_spaces(data + index);
785     }
786    
787     return index + eat_to_end_of_line(data + index);
788     }
789     }
790    
791     // known_hosts�t�@�C�������z�X�g���J�������������B
792     // �������������������������������AHost key rotation�p���V�K���p�������B
793     //
794     // return 1: success
795     // 0: fail
796     int HOSTS_hostkey_foreach(PTInstVar pvar, hostkeys_foreach_fn *callback, void *ctx)
797     {
798     int success = 0;
799     int suppress_errors = 1;
800     unsigned short tcpport;
801 doda 6801 char *filename;
802 yutakapon 5846 char *hostname;
803     Key *key;
804    
805     if (!begin_read_host_files(pvar, 1)) {
806     goto error;
807     }
808    
809     // Host key rotation�����Aknown_hosts �t�@�C�������������������A
810     // ������������1�������t�@�C�������������i2�������t�@�C����ReadOnly�������j�B
811     filename = pvar->hosts_state.file_names[pvar->hosts_state.file_num];
812     pvar->hosts_state.file_num++;
813    
814     pvar->hosts_state.file_data_index = -1;
815     if (filename[0] != 0) {
816     if (begin_read_file(pvar, filename, suppress_errors)) {
817     pvar->hosts_state.file_data_index = 0;
818     }
819     }
820     if (pvar->hosts_state.file_data_index == -1)
821     goto error;
822    
823     // ���������������z�X�g�����|�[�g�����B
824     hostname = pvar->ssh_state.hostname;
825     tcpport = pvar->ssh_state.tcpport;
826    
827     // known_hosts�t�@�C�������e�������� pvar->hosts_state.file_data �������������������B
828     // ������ \0 �B
829     while (pvar->hosts_state.file_data[pvar->hosts_state.file_data_index] != 0) {
830     key = NULL;
831    
832     pvar->hosts_state.file_data_index +=
833     parse_hostkey_file(pvar, hostname, tcpport,
834     pvar->hosts_state.file_data +
835     pvar->hosts_state.file_data_index,
836     &key);
837    
838 yutakapon 5847 // �Y�����������������������A�R�[���o�b�N�����������o���B
839 yutakapon 5846 if (key != NULL) {
840 yutakapon 5847 if (callback(key, ctx) == 0)
841     key_free(key);
842 yutakapon 5846 }
843     }
844    
845     success = 1;
846    
847     error:
848     finish_read_host_files(pvar, 1);
849    
850     return (success);
851     }
852    
853    
854 doda 6801 static BOOL equal_mp_ints(unsigned char *num1,
855     unsigned char *num2)
856 maya 3227 {
857     if (num1 == NULL || num2 == NULL) {
858     return FALSE;
859     } else {
860     uint32 bytes = (get_ushort16_MSBfirst(num1) + 7) / 8;
861    
862     if (bytes != (get_ushort16_MSBfirst(num2) + 7) / 8) {
863     return FALSE; /* different byte lengths */
864     } else {
865     return memcmp(num1 + 2, num2 + 2, bytes) == 0;
866     }
867     }
868     }
869    
870 yutakapon 5843
871     // ���J�������r���s���B
872     //
873     // return
874 maya 4332 // -1 ... �����^������
875     // 0 ... ����������
876     // 1 ... ������
877 yutakapon 5843 int HOSTS_compare_public_key(Key *src, Key *key)
878 maya 3227 {
879     int bits;
880 doda 6801 unsigned char *exp;
881     unsigned char *mod;
882 maya 4321 const EC_GROUP *group;
883     const EC_POINT *pa, *pb;
884 yutakapon 5545 Key *a, *b;
885 yutakapon 8316 BIGNUM *e = NULL, *n = NULL;
886     BIGNUM *se = NULL, *sn = NULL;
887     BIGNUM *p, *q, *g, *pub_key;
888     BIGNUM *sp, *sq, *sg, *spub_key;
889 maya 3227
890 yutakapon 5843 if (src->type != key->type) {
891 maya 4332 return -1;
892     }
893    
894 maya 4321 switch (key->type) {
895     case KEY_RSA1: // SSH1 host public key
896 maya 3227 bits = key->bits;
897     exp = key->exp;
898     mod = key->mod;
899    
900     /* just check for equal exponent and modulus */
901 yutakapon 5843 return equal_mp_ints(exp, src->exp)
902     && equal_mp_ints(mod, src->mod);
903 maya 3227 /*
904     return equal_mp_ints(exp, pvar->hosts_state.key_exp)
905 yutakapon 5843 && equal_mp_ints(mod, pvar->hosts_state.key_mod);
906     */
907 maya 3227
908 maya 4321 case KEY_RSA: // SSH2 RSA host public key
909 yutakapon 8316 RSA_get0_key(key->rsa, &n, &e, NULL);
910     RSA_get0_key(src->rsa, &sn, &se, NULL);
911 yutakapon 5843 return key->rsa != NULL && src->rsa != NULL &&
912 yutakapon 8316 BN_cmp(e, se) == 0 &&
913     BN_cmp(n, sn) == 0;
914 maya 3227
915 maya 4321 case KEY_DSA: // SSH2 DSA host public key
916 yutakapon 8316 DSA_get0_pqg(key->dsa, &p, &q, &g);
917     DSA_get0_pqg(src->dsa, &sp, &sq, &sg);
918     DSA_get0_key(key->dsa, &pub_key, NULL);
919     DSA_get0_key(src->dsa, &spub_key, NULL);
920 yutakapon 5843 return key->dsa != NULL && src->dsa &&
921 yutakapon 8316 BN_cmp(p, sp) == 0 &&
922     BN_cmp(q, sq) == 0 &&
923     BN_cmp(g, sg) == 0 &&
924     BN_cmp(pub_key, spub_key) == 0;
925 maya 3227
926 maya 4321 case KEY_ECDSA256:
927     case KEY_ECDSA384:
928     case KEY_ECDSA521:
929 yutakapon 5843 if (key->ecdsa == NULL || src->ecdsa == NULL) {
930 maya 4321 return FALSE;
931     }
932     group = EC_KEY_get0_group(key->ecdsa);
933     pa = EC_KEY_get0_public_key(key->ecdsa),
934 yutakapon 5843 pb = EC_KEY_get0_public_key(src->ecdsa);
935 maya 4321 return EC_POINT_cmp(group, pa, pb, NULL) == 0;
936    
937 yutakapon 5545 case KEY_ED25519:
938     a = key;
939 yutakapon 5843 b = src;
940 yutakapon 5545 return a->ed25519_pk != NULL && b->ed25519_pk != NULL &&
941 yutakapon 5843 memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) == 0;
942 yutakapon 5545
943 maya 4321 default:
944 maya 4307 return FALSE;
945     }
946 yutakapon 5843 }
947 maya 3227
948 maya 6152 #if 0
949     // pvar->hosts_state.hostkey ���n���������J����������������������
950 yutakapon 5843 // -1 ... �����^������
951     // 0 ... ����������
952     // 1 ... ������
953     static int match_key(PTInstVar pvar, Key *key)
954     {
955     return HOSTS_compare_public_key(&pvar->hosts_state.hostkey, key);
956 maya 3227 }
957 maya 6152 #endif
958 maya 3227
959 maya 6132 static void hosts_dlg_set_fingerprint(PTInstVar pvar, HWND dlg, digest_algorithm dgst_alg)
960     {
961     char *fp = NULL;
962    
963     // fingerprint����������
964     switch (dgst_alg) {
965     case SSH_DIGEST_MD5:
966     fp = key_fingerprint(&pvar->hosts_state.hostkey, SSH_FP_HEX, dgst_alg);
967 maya 6158 if (fp != NULL) {
968     SendMessage(GetDlgItem(dlg, IDC_FINGER_PRINT), WM_SETTEXT, 0, (LPARAM)fp);
969     free(fp);
970     }
971 maya 6132 break;
972     case SSH_DIGEST_SHA256:
973 maya 6158 default:
974     fp = key_fingerprint(&pvar->hosts_state.hostkey, SSH_FP_BASE64, SSH_DIGEST_SHA256);
975     if (fp != NULL) {
976     SendMessage(GetDlgItem(dlg, IDC_FINGER_PRINT), WM_SETTEXT, 0, (LPARAM)fp);
977     free(fp);
978     }
979 maya 6132 break;
980     }
981    
982     // �r�W���A����fingerprint���\������
983     fp = key_fingerprint(&pvar->hosts_state.hostkey, SSH_FP_RANDOMART, dgst_alg);
984 maya 6158 if (fp != NULL) {
985     SendMessage(GetDlgItem(dlg, IDC_FP_RANDOMART), WM_SETTEXT, 0, (LPARAM)fp);
986     free(fp);
987     }
988 maya 6132 }
989    
990 maya 3227 static void init_hosts_dlg(PTInstVar pvar, HWND dlg)
991     {
992 zmatsuo 8542 wchar_t buf[MAX_UIMSG];
993 zmatsuo 8593 wchar_t *buf2;
994 zmatsuo 8542 wchar_t *hostW;
995 maya 3227
996 zmatsuo 8593 // �z�X�g�����u������
997     _GetDlgItemTextW(dlg, IDC_HOSTWARNING, buf, _countof(buf));
998 zmatsuo 8542 hostW = ToWcharA(pvar->hosts_state.prefetched_hostname);
999 zmatsuo 8593 aswprintf(&buf2, buf, hostW);
1000 zmatsuo 8542 free(hostW);
1001     _SetDlgItemTextW(dlg, IDC_HOSTWARNING, buf2);
1002 zmatsuo 8593 free(buf2);
1003 maya 3227
1004 zmatsuo 7714 pvar->hFontFixed = UTIL_get_lang_fixedfont(dlg, pvar->ts->UILanguageFile);
1005     if (pvar->hFontFixed != NULL) {
1006     SendDlgItemMessage(dlg, IDC_FP_RANDOMART, WM_SETFONT,
1007     (WPARAM)pvar->hFontFixed, MAKELPARAM(TRUE,0));
1008     }
1009 maya 3227
1010 maya 6145 CheckDlgButton(dlg, IDC_FP_HASH_ALG_SHA256, TRUE);
1011     hosts_dlg_set_fingerprint(pvar, dlg, SSH_DIGEST_SHA256);
1012 maya 3227 }
1013    
1014 doda 6801 static int print_mp_int(char *buf, unsigned char *mp)
1015 maya 3227 {
1016     int i = 0, j, k;
1017     BIGNUM *num = BN_new();
1018     int ch;
1019    
1020     BN_bin2bn(mp + 2, (get_ushort16_MSBfirst(mp) + 7) / 8, num);
1021    
1022     do {
1023     buf[i] = (char) ((BN_div_word(num, 10)) + '0');
1024     i++;
1025     } while (!BN_is_zero(num));
1026    
1027     /* we need to reverse the digits */
1028     for (j = 0, k = i - 1; j < k; j++, k--) {
1029     ch = buf[j];
1030     buf[j] = buf[k];
1031     buf[k] = ch;
1032     }
1033    
1034     buf[i] = 0;
1035     return i;
1036     }
1037    
1038     //
1039     // known_hosts �t�@�C�������������G���g�������������B
1040     //
1041 doda 6801 static char *format_host_key(PTInstVar pvar)
1042 maya 3227 {
1043     int host_len = strlen(pvar->hosts_state.prefetched_hostname);
1044     char *result = NULL;
1045     int index;
1046 maya 4378 ssh_keytype type = pvar->hosts_state.hostkey.type;
1047 maya 3227
1048 maya 4321 switch (type) {
1049     case KEY_RSA1:
1050     {
1051 maya 3227 int result_len = host_len + 50 + 8 +
1052     get_ushort16_MSBfirst(pvar->hosts_state.hostkey.exp) / 3 +
1053     get_ushort16_MSBfirst(pvar->hosts_state.hostkey.mod) / 3;
1054 doda 6801 result = (char *) malloc(result_len);
1055 maya 3227
1056     if (pvar->ssh_state.tcpport == 22) {
1057     strncpy_s(result, result_len, pvar->hosts_state.prefetched_hostname, _TRUNCATE);
1058     index = host_len;
1059     }
1060     else {
1061     _snprintf_s(result, result_len, _TRUNCATE, "[%s]:%d",
1062     pvar->hosts_state.prefetched_hostname,
1063     pvar->ssh_state.tcpport);
1064     index = strlen(result);
1065     }
1066    
1067 yutakapon 7889 // ��2����(sizeOfBuffer)���w�������������A�������o�b�t�@�T�C�Y����
1068     // �����������������������C�������B
1069     // �|�[�g������22���O�������AVS2005��debug build�����Aadd_host_key()��
1070     // free(keydata)���A���������u�u���[�N�|�C���g���������������B�q�[�v������������������
1071     // �����������l�����������B�v���������O�����������B
1072     // release build�����������������B
1073     _snprintf_s(result + index, result_len - index, _TRUNCATE,
1074 maya 3227 " %d ", pvar->hosts_state.hostkey.bits);
1075     index += strlen(result + index);
1076     index += print_mp_int(result + index, pvar->hosts_state.hostkey.exp);
1077     result[index] = ' ';
1078     index++;
1079     index += print_mp_int(result + index, pvar->hosts_state.hostkey.mod);
1080     strncpy_s(result + index, result_len - index, " \r\n", _TRUNCATE);
1081    
1082 maya 4321 break;
1083     }
1084    
1085     case KEY_RSA:
1086     case KEY_DSA:
1087     case KEY_ECDSA256:
1088     case KEY_ECDSA384:
1089     case KEY_ECDSA521:
1090 yutakapon 5545 case KEY_ED25519:
1091 maya 4321 {
1092 maya 3227 Key *key = &pvar->hosts_state.hostkey;
1093     char *blob = NULL;
1094     int blen, uulen, msize;
1095     char *uu = NULL;
1096     int n;
1097    
1098     key_to_blob(key, &blob, &blen);
1099     uulen = 2 * blen;
1100     uu = malloc(uulen);
1101     if (uu == NULL) {
1102     goto error;
1103     }
1104     n = uuencode(blob, blen, uu, uulen);
1105     if (n > 0) {
1106     msize = host_len + 50 + uulen;
1107     result = malloc(msize);
1108     if (result == NULL) {
1109     goto error;
1110     }
1111    
1112     // setup
1113     if (pvar->ssh_state.tcpport == 22) {
1114     _snprintf_s(result, msize, _TRUNCATE, "%s %s %s\r\n",
1115     pvar->hosts_state.prefetched_hostname,
1116     get_sshname_from_key(key),
1117     uu);
1118     } else {
1119     _snprintf_s(result, msize, _TRUNCATE, "[%s]:%d %s %s\r\n",
1120     pvar->hosts_state.prefetched_hostname,
1121     pvar->ssh_state.tcpport,
1122     get_sshname_from_key(key),
1123     uu);
1124     }
1125     }
1126     error:
1127     if (blob != NULL)
1128     free(blob);
1129     if (uu != NULL)
1130     free(uu);
1131    
1132 maya 4321 break;
1133     }
1134    
1135     default:
1136 maya 3227 return NULL;
1137    
1138     }
1139    
1140     return result;
1141     }
1142    
1143 doda 6801 static char *format_specified_host_key(Key *key, char *hostname, unsigned short tcpport)
1144 yutakapon 5849 {
1145     int host_len = strlen(hostname);
1146     char *result = NULL;
1147     int index;
1148     ssh_keytype type = key->type;
1149    
1150     switch (type) {
1151     case KEY_RSA1:
1152     {
1153     int result_len = host_len + 50 + 8 +
1154     get_ushort16_MSBfirst(key->exp) / 3 +
1155     get_ushort16_MSBfirst(key->mod) / 3;
1156 doda 6801 result = (char *) malloc(result_len);
1157 yutakapon 5849
1158     if (tcpport == 22) {
1159     strncpy_s(result, result_len, hostname, _TRUNCATE);
1160     index = host_len;
1161     }
1162     else {
1163     _snprintf_s(result, result_len, _TRUNCATE, "[%s]:%d",
1164     hostname,
1165     tcpport);
1166     index = strlen(result);
1167     }
1168    
1169     _snprintf_s(result + index, result_len - host_len, _TRUNCATE,
1170     " %d ", key->bits);
1171     index += strlen(result + index);
1172     index += print_mp_int(result + index, key->exp);
1173     result[index] = ' ';
1174     index++;
1175     index += print_mp_int(result + index, key->mod);
1176     strncpy_s(result + index, result_len - index, " \r\n", _TRUNCATE);
1177    
1178     break;
1179     }
1180    
1181     case KEY_RSA:
1182     case KEY_DSA:
1183     case KEY_ECDSA256:
1184     case KEY_ECDSA384:
1185     case KEY_ECDSA521:
1186     case KEY_ED25519:
1187     {
1188     //Key *key = &pvar->hosts_state.hostkey;
1189     char *blob = NULL;
1190     int blen, uulen, msize;
1191     char *uu = NULL;
1192     int n;
1193    
1194     key_to_blob(key, &blob, &blen);
1195     uulen = 2 * blen;
1196     uu = malloc(uulen);
1197     if (uu == NULL) {
1198     goto error;
1199     }
1200     n = uuencode(blob, blen, uu, uulen);
1201     if (n > 0) {
1202     msize = host_len + 50 + uulen;
1203     result = malloc(msize);
1204     if (result == NULL) {
1205     goto error;
1206     }
1207    
1208     // setup
1209     if (tcpport == 22) {
1210     _snprintf_s(result, msize, _TRUNCATE, "%s %s %s\r\n",
1211     hostname,
1212     get_sshname_from_key(key),
1213     uu);
1214     }
1215     else {
1216     _snprintf_s(result, msize, _TRUNCATE, "[%s]:%d %s %s\r\n",
1217     hostname,
1218     tcpport,
1219     get_sshname_from_key(key),
1220     uu);
1221     }
1222     }
1223     error:
1224     if (blob != NULL)
1225     free(blob);
1226     if (uu != NULL)
1227     free(uu);
1228    
1229     break;
1230     }
1231    
1232     default:
1233     return NULL;
1234    
1235     }
1236    
1237     return result;
1238     }
1239    
1240 maya 3227 static void add_host_key(PTInstVar pvar)
1241     {
1242 doda 6801 char *name = NULL;
1243 maya 3227
1244     if ( pvar->hosts_state.file_names != NULL)
1245     name = pvar->hosts_state.file_names[0];
1246    
1247     if (name == NULL || name[0] == 0) {
1248     UTIL_get_lang_msg("MSG_HOSTS_FILE_UNSPECIFY_ERROR", pvar,
1249     "The host and its key cannot be added, because no known-hosts file has been specified.\n"
1250     "Restart Tera Term and specify a read/write known-hosts file in the TTSSH Setup dialog box.");
1251     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1252     } else {
1253 doda 6801 char *keydata = format_host_key(pvar);
1254 maya 3227 int length = strlen(keydata);
1255     int fd;
1256     int amount_written;
1257     int close_result;
1258     char buf[FILENAME_MAX];
1259    
1260     get_teraterm_dir_relative_name(buf, sizeof(buf), name);
1261     fd = _open(buf,
1262     _O_APPEND | _O_CREAT | _O_WRONLY | _O_SEQUENTIAL | _O_BINARY,
1263     _S_IREAD | _S_IWRITE);
1264     if (fd == -1) {
1265     if (errno == EACCES) {
1266     UTIL_get_lang_msg("MSG_HOSTS_WRITE_EACCES_ERROR", pvar,
1267     "An error occurred while trying to write the host key.\n"
1268     "You do not have permission to write to the known-hosts file.");
1269     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1270     } else {
1271     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1272     "An error occurred while trying to write the host key.\n"
1273     "The host key could not be written.");
1274     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1275     }
1276     return;
1277     }
1278    
1279     amount_written = _write(fd, keydata, length);
1280     free(keydata);
1281     close_result = _close(fd);
1282    
1283     if (amount_written != length || close_result == -1) {
1284     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1285     "An error occurred while trying to write the host key.\n"
1286     "The host key could not be written.");
1287     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1288     }
1289     }
1290     }
1291    
1292 yutakapon 5849 // �w�������L�[�� known_hosts �����������B
1293     void HOSTS_add_host_key(PTInstVar pvar, Key *key)
1294     {
1295 doda 6801 char *name = NULL;
1296 yutakapon 5849 char *hostname;
1297     unsigned short tcpport;
1298    
1299     hostname = pvar->ssh_state.hostname;
1300     tcpport = pvar->ssh_state.tcpport;
1301    
1302     if (pvar->hosts_state.file_names != NULL)
1303     name = pvar->hosts_state.file_names[0];
1304    
1305     if (name == NULL || name[0] == 0) {
1306     UTIL_get_lang_msg("MSG_HOSTS_FILE_UNSPECIFY_ERROR", pvar,
1307     "The host and its key cannot be added, because no known-hosts file has been specified.\n"
1308     "Restart Tera Term and specify a read/write known-hosts file in the TTSSH Setup dialog box.");
1309     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1310     }
1311     else {
1312 doda 6801 char *keydata = format_specified_host_key(key, hostname, tcpport);
1313 yutakapon 5849 int length = strlen(keydata);
1314     int fd;
1315     int amount_written;
1316     int close_result;
1317     char buf[FILENAME_MAX];
1318    
1319     get_teraterm_dir_relative_name(buf, sizeof(buf), name);
1320     fd = _open(buf,
1321     _O_APPEND | _O_CREAT | _O_WRONLY | _O_SEQUENTIAL | _O_BINARY,
1322     _S_IREAD | _S_IWRITE);
1323     if (fd == -1) {
1324     if (errno == EACCES) {
1325     UTIL_get_lang_msg("MSG_HOSTS_WRITE_EACCES_ERROR", pvar,
1326     "An error occurred while trying to write the host key.\n"
1327     "You do not have permission to write to the known-hosts file.");
1328     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1329     }
1330     else {
1331     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1332     "An error occurred while trying to write the host key.\n"
1333     "The host key could not be written.");
1334     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1335     }
1336     return;
1337     }
1338    
1339     amount_written = _write(fd, keydata, length);
1340     free(keydata);
1341     close_result = _close(fd);
1342    
1343     if (amount_written != length || close_result == -1) {
1344     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1345     "An error occurred while trying to write the host key.\n"
1346     "The host key could not be written.");
1347     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1348     }
1349     }
1350     }
1351    
1352 maya 3227 //
1353     // �����z�X�g�����e���������L�[����������
1354     // add_host_key ����������������
1355     //
1356     static void delete_different_key(PTInstVar pvar)
1357     {
1358 doda 6801 char *name = pvar->hosts_state.file_names[0];
1359 maya 3227
1360     if (name == NULL || name[0] == 0) {
1361     UTIL_get_lang_msg("MSG_HOSTS_FILE_UNSPECIFY_ERROR", pvar,
1362     "The host and its key cannot be added, because no known-hosts file has been specified.\n"
1363     "Restart Tera Term and specify a read/write known-hosts file in the TTSSH Setup dialog box.");
1364     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1365     }
1366     else {
1367 maya 6152 Key key; // known_hosts���o�^������������
1368 maya 3227 int length;
1369     char filename[MAX_PATH];
1370 nmaya 9165 #if _MSC_VER < 1900 // less than VSC2015(VC14.0)
1371 maya 3227 char tmp[L_tmpnam];
1372 nmaya 9165 #endif
1373 maya 3227 int fd;
1374     int amount_written = 0;
1375     int close_result;
1376     int data_index = 0;
1377     char buf[FILENAME_MAX];
1378    
1379     // �������������t�@�C�����J��
1380 nmaya 9165 #if _MSC_VER < 1900 // less than VSC2015(VC14.0)
1381 maya 3227 _getcwd(filename, sizeof(filename));
1382 nmaya 9165 tmpnam_s(tmp, sizeof(tmp));
1383 maya 3227 strcat_s(filename, sizeof(filename), tmp);
1384 nmaya 9165 #else // VSC2015(VC14.0) or later
1385     tmpnam_s(filename, sizeof(filename));
1386     #endif
1387 maya 3227 fd = _open(filename,
1388     _O_CREAT | _O_WRONLY | _O_SEQUENTIAL | _O_BINARY | _O_TRUNC,
1389     _S_IREAD | _S_IWRITE);
1390    
1391     if (fd == -1) {
1392     if (errno == EACCES) {
1393     UTIL_get_lang_msg("MSG_HOSTS_WRITE_EACCES_ERROR", pvar,
1394     "An error occurred while trying to write the host key.\n"
1395     "You do not have permission to write to the known-hosts file.");
1396     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1397     } else {
1398     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1399     "An error occurred while trying to write the host key.\n"
1400     "The host key could not be written.");
1401     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1402     }
1403     return;
1404     }
1405    
1406 maya 6152 // �t�@�C��������������
1407 maya 6147 memset(&key, 0, sizeof(key));
1408 maya 3227 begin_read_host_files(pvar, 0);
1409     do {
1410     int host_index = 0;
1411     int matched = 0;
1412     int keybits = 0;
1413 doda 6801 char *data;
1414 maya 3227 int do_write = 0;
1415     length = amount_written = 0;
1416    
1417 maya 6152 if (!read_host_key(pvar, pvar->ssh_state.hostname, pvar->ssh_state.tcpport, 0, 1, &key)) {
1418 maya 3227 break;
1419     }
1420    
1421     if (data_index == pvar->hosts_state.file_data_index) {
1422     // index ���i������ == ��������������
1423     break;
1424     }
1425    
1426     data = pvar->hosts_state.file_data + data_index;
1427     host_index = eat_spaces(data);
1428    
1429     if (data[host_index] == '#') {
1430     do_write = 1;
1431     }
1432     else {
1433     // �z�X�g������
1434     host_index--;
1435     do {
1436     int negated;
1437     int bracketed;
1438     char *end_bracket;
1439     int host_matched = 0;
1440     unsigned short keyfile_port = 22;
1441    
1442     host_index++;
1443     negated = data[host_index] == '!';
1444    
1445     if (negated) {
1446     host_index++;
1447     bracketed = data[host_index] == '[';
1448     if (bracketed) {
1449     end_bracket = strstr(data + host_index + 1, "]:");
1450     if (end_bracket != NULL) {
1451     *end_bracket = ' ';
1452     host_index++;
1453     }
1454     }
1455     host_matched = match_pattern(data + host_index, pvar->ssh_state.hostname);
1456     if (bracketed && end_bracket != NULL) {
1457     *end_bracket = ']';
1458     keyfile_port = atoi(end_bracket + 2);
1459     }
1460     if (host_matched && keyfile_port == pvar->ssh_state.tcpport) {
1461     matched = 0;
1462     // �����o�[�W�����`�F�b�N�������� host_index ���i��������������
1463     host_index--;
1464     do {
1465     host_index++;
1466     host_index += eat_to_end_of_pattern(data + host_index);
1467     } while (data[host_index] == ',');
1468     break;
1469     }
1470     }
1471     else {
1472     bracketed = data[host_index] == '[';
1473     if (bracketed) {
1474     end_bracket = strstr(data + host_index + 1, "]:");
1475     if (end_bracket != NULL) {
1476     *end_bracket = ' ';
1477     host_index++;
1478     }
1479     }
1480     host_matched = match_pattern(data + host_index, pvar->ssh_state.hostname);
1481     if (bracketed && end_bracket != NULL) {
1482     *end_bracket = ']';
1483     keyfile_port = atoi(end_bracket + 2);
1484     }
1485     if (host_matched && keyfile_port == pvar->ssh_state.tcpport) {
1486     matched = 1;
1487     }
1488     }
1489     host_index += eat_to_end_of_pattern(data + host_index);
1490     } while (data[host_index] == ',');
1491    
1492     // �z�X�g������������
1493 maya 4332 if (!matched) {
1494 maya 3227 do_write = 1;
1495     }
1496 maya 4332 // �z�X�g��������
1497 maya 3227 else {
1498 maya 4332 // �����`�������� or ���v�����L�[
1499 maya 6152 if (HOSTS_compare_public_key(&pvar->hosts_state.hostkey, &key) != 0) {
1500 maya 4332 do_write = 1;
1501 maya 3227 }
1502 maya 4332 // �����`�������������v�������L�[���X�L�b�v������
1503 maya 3227 }
1504     }
1505    
1506     // ������������
1507     if (do_write) {
1508     length = pvar->hosts_state.file_data_index - data_index;
1509     amount_written =
1510     _write(fd, pvar->hosts_state.file_data + data_index,
1511     length);
1512    
1513     if (amount_written != length) {
1514     goto error1;
1515     }
1516     }
1517     data_index = pvar->hosts_state.file_data_index;
1518     } while (1); // ������������
1519    
1520     error1:
1521     close_result = _close(fd);
1522     if (amount_written != length || close_result == -1) {
1523     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1524     "An error occurred while trying to write the host key.\n"
1525     "The host key could not be written.");
1526     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1527     goto error2;
1528     }
1529    
1530     // �������������t�@�C���������l�[��
1531     get_teraterm_dir_relative_name(buf, sizeof(buf), name);
1532     _unlink(buf);
1533     rename(filename, buf);
1534    
1535     error2:
1536     _unlink(filename);
1537    
1538     finish_read_host_files(pvar, 0);
1539 yutakapon 5545
1540     // ���������������������������B
1541 maya 6147 key_init(&key);
1542 maya 3227 }
1543     }
1544    
1545 yutakapon 5849
1546     void HOSTS_delete_all_hostkeys(PTInstVar pvar)
1547     {
1548 doda 6801 char *name = pvar->hosts_state.file_names[0];
1549 yutakapon 5849 char *hostname;
1550     unsigned short tcpport;
1551    
1552     hostname = pvar->ssh_state.hostname;
1553     tcpport = pvar->ssh_state.tcpport;
1554    
1555     if (name == NULL || name[0] == 0) {
1556     UTIL_get_lang_msg("MSG_HOSTS_FILE_UNSPECIFY_ERROR", pvar,
1557     "The host and its key cannot be added, because no known-hosts file has been specified.\n"
1558     "Restart Tera Term and specify a read/write known-hosts file in the TTSSH Setup dialog box.");
1559     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1560     }
1561     else {
1562 maya 6152 Key key; // known_hosts���o�^������������
1563 yutakapon 5849 int length;
1564     char filename[MAX_PATH];
1565 nmaya 9165 #if _MSC_VER < 1900 // less than VSC2015(VC14.0)
1566 yutakapon 5849 char tmp[L_tmpnam];
1567 nmaya 9165 #endif
1568 yutakapon 5849 int fd;
1569     int amount_written = 0;
1570     int close_result;
1571     int data_index = 0;
1572     char buf[FILENAME_MAX];
1573    
1574     // �������������t�@�C�����J��
1575 nmaya 9165 #if _MSC_VER < 1900 // less than VSC2015(VC14.0)
1576 yutakapon 5849 _getcwd(filename, sizeof(filename));
1577     tmpnam_s(tmp, sizeof(tmp));
1578     strcat_s(filename, sizeof(filename), tmp);
1579 nmaya 9165 #else // VSC2015(VC14.0) or later
1580     tmpnam_s(filename, sizeof(filename));
1581     #endif
1582 yutakapon 5849 fd = _open(filename,
1583     _O_CREAT | _O_WRONLY | _O_SEQUENTIAL | _O_BINARY | _O_TRUNC,
1584     _S_IREAD | _S_IWRITE);
1585    
1586     if (fd == -1) {
1587     if (errno == EACCES) {
1588     UTIL_get_lang_msg("MSG_HOSTS_WRITE_EACCES_ERROR", pvar,
1589     "An error occurred while trying to write the host key.\n"
1590     "You do not have permission to write to the known-hosts file.");
1591     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1592     }
1593     else {
1594     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1595     "An error occurred while trying to write the host key.\n"
1596     "The host key could not be written.");
1597     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1598     }
1599     return;
1600     }
1601    
1602 maya 6152 // �t�@�C��������������
1603 maya 6147 memset(&key, 0, sizeof(key));
1604 yutakapon 5849 begin_read_host_files(pvar, 0);
1605     do {
1606     int host_index = 0;
1607     int matched = 0;
1608     int keybits = 0;
1609 doda 6801 char *data;
1610 yutakapon 5849 int do_write = 0;
1611     length = amount_written = 0;
1612    
1613 maya 6152 if (!read_host_key(pvar, pvar->ssh_state.hostname, pvar->ssh_state.tcpport, 0, 1, &key)) {
1614 yutakapon 5849 break;
1615     }
1616    
1617     if (data_index == pvar->hosts_state.file_data_index) {
1618     // index ���i������ == ��������������
1619     break;
1620     }
1621    
1622     data = pvar->hosts_state.file_data + data_index;
1623     host_index = eat_spaces(data);
1624    
1625     if (data[host_index] == '#') {
1626     do_write = 1;
1627     }
1628     else {
1629     // �z�X�g������
1630     host_index--;
1631     do {
1632     int negated;
1633     int bracketed;
1634     char *end_bracket;
1635     int host_matched = 0;
1636     unsigned short keyfile_port = 22;
1637    
1638     host_index++;
1639     negated = data[host_index] == '!';
1640    
1641     if (negated) {
1642     host_index++;
1643     bracketed = data[host_index] == '[';
1644     if (bracketed) {
1645     end_bracket = strstr(data + host_index + 1, "]:");
1646     if (end_bracket != NULL) {
1647     *end_bracket = ' ';
1648     host_index++;
1649     }
1650     }
1651     host_matched = match_pattern(data + host_index, pvar->ssh_state.hostname);
1652     if (bracketed && end_bracket != NULL) {
1653     *end_bracket = ']';
1654     keyfile_port = atoi(end_bracket + 2);
1655     }
1656     if (host_matched && keyfile_port == pvar->ssh_state.tcpport) {
1657     matched = 0;
1658     // �����o�[�W�����`�F�b�N�������� host_index ���i��������������
1659     host_index--;
1660     do {
1661     host_index++;
1662     host_index += eat_to_end_of_pattern(data + host_index);
1663     } while (data[host_index] == ',');
1664     break;
1665     }
1666     }
1667     else {
1668     bracketed = data[host_index] == '[';
1669     if (bracketed) {
1670     end_bracket = strstr(data + host_index + 1, "]:");
1671     if (end_bracket != NULL) {
1672     *end_bracket = ' ';
1673     host_index++;
1674     }
1675     }
1676     host_matched = match_pattern(data + host_index, pvar->ssh_state.hostname);
1677     if (bracketed && end_bracket != NULL) {
1678     *end_bracket = ']';
1679     keyfile_port = atoi(end_bracket + 2);
1680     }
1681     if (host_matched && keyfile_port == pvar->ssh_state.tcpport) {
1682     matched = 1;
1683     }
1684     }
1685     host_index += eat_to_end_of_pattern(data + host_index);
1686     } while (data[host_index] == ',');
1687    
1688     // �z�X�g������������
1689     if (!matched) {
1690     do_write = 1;
1691     }
1692     // �z�X�g��������
1693     else {
1694     // ���������������������B
1695    
1696     }
1697     }
1698    
1699     // ������������
1700     if (do_write) {
1701     length = pvar->hosts_state.file_data_index - data_index;
1702     amount_written =
1703     _write(fd, pvar->hosts_state.file_data + data_index,
1704     length);
1705    
1706     if (amount_written != length) {
1707     goto error1;
1708     }
1709     }
1710     data_index = pvar->hosts_state.file_data_index;
1711     } while (1); // ������������
1712    
1713     error1:
1714     close_result = _close(fd);
1715     if (amount_written != length || close_result == -1) {
1716     UTIL_get_lang_msg("MSG_HOSTS_WRITE_ERROR", pvar,
1717     "An error occurred while trying to write the host key.\n"
1718     "The host key could not be written.");
1719     notify_nonfatal_error(pvar, pvar->ts->UIMsg);
1720     goto error2;
1721     }
1722    
1723     // �������������t�@�C���������l�[��
1724     get_teraterm_dir_relative_name(buf, sizeof(buf), name);
1725     _unlink(buf);
1726     rename(filename, buf);
1727    
1728     error2:
1729     _unlink(filename);
1730    
1731     finish_read_host_files(pvar, 0);
1732    
1733     // ���������������������������B
1734 maya 6147 key_init(&key);
1735 yutakapon 5849 }
1736     }
1737    
1738    
1739 maya 3227 //
1740     // Unknown host���z�X�g���J���� known_hosts �t�@�C����������������������
1741     // ���[�U���m�F�������B
1742     // TODO: finger print���\�����s���B
1743     // (2006.3.25 yutaka)
1744     //
1745 zmatsuo 7896 static INT_PTR CALLBACK hosts_add_dlg_proc(HWND dlg, UINT msg, WPARAM wParam,
1746     LPARAM lParam)
1747 maya 3227 {
1748 zmatsuo 8542 static const DlgTextInfo text_info[] = {
1749 zmatsuo 8240 { 0, "DLG_UNKNOWNHOST_TITLE" },
1750     { IDC_HOSTWARNING, "DLG_UNKNOWNHOST_WARNING" },
1751     { IDC_HOSTWARNING2, "DLG_UNKNOWNHOST_WARNING2" },
1752     { IDC_HOSTFINGERPRINT, "DLG_UNKNOWNHOST_FINGERPRINT" },
1753     { IDC_FP_HASH_ALG, "DLG_UNKNOWNHOST_FP_HASH_ALGORITHM" },
1754     { IDC_ADDTOKNOWNHOSTS, "DLG_UNKNOWNHOST_ADD" },
1755     { IDC_CONTINUE, "BTN_CONTINUE" },
1756     { IDCANCEL, "BTN_DISCONNECT" },
1757     };
1758 maya 3227 PTInstVar pvar;
1759    
1760     switch (msg) {
1761     case WM_INITDIALOG:
1762     pvar = (PTInstVar) lParam;
1763     pvar->hosts_state.hosts_dialog = dlg;
1764 zmatsuo 7896 SetWindowLongPtr(dlg, DWLP_USER, lParam);
1765 maya 3227
1766     // �����E�u���������� init_hosts_dlg �����������������A�����O���Z�b�g�����K�v������
1767 doda 8485 SetI18nDlgStrs("TTSSH", dlg, text_info, _countof(text_info), pvar->ts->UILanguageFile);
1768 maya 3227
1769 doda 4559 switch (pvar->dns_key_check) {
1770     case DNS_VERIFY_NOTFOUND:
1771 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_NOTFOUND", pvar, "No host key fingerprint found in DNS.");
1772 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1773     break;
1774     case DNS_VERIFY_MATCH:
1775     case DNS_VERIFY_AUTH_MATCH:
1776 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_MATCH", pvar, "Matching host key fingerprint found in DNS.");
1777 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1778     break;
1779     case DNS_VERIFY_MISMATCH:
1780     case DNS_VERIFY_AUTH_MISMATCH:
1781 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_MISMATCH", pvar, "Mismatching host key fingerprint found in DNS.");
1782 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1783     break;
1784     case DNS_VERIFY_DIFFERENTTYPE:
1785     case DNS_VERIFY_AUTH_DIFFERENTTYPE:
1786 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_DIFFTYPE", pvar, "Mismatching host key type found in DNS.");
1787 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1788     break;
1789     }
1790    
1791     switch (pvar->dns_key_check) {
1792     case DNS_VERIFY_MATCH:
1793     case DNS_VERIFY_MISMATCH:
1794     case DNS_VERIFY_DIFFERENTTYPE:
1795 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_DNSSEC_NG", pvar, "Found insecure fingerprint in DNS.");
1796 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPDNSSEC, pvar->ts->UIMsg);
1797     break;
1798     case DNS_VERIFY_AUTH_MATCH:
1799     case DNS_VERIFY_AUTH_MISMATCH:
1800     case DNS_VERIFY_AUTH_DIFFERENTTYPE:
1801 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_DNSSEC_OK", pvar, "Found secure fingerprint in DNS.");
1802 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPDNSSEC, pvar->ts->UIMsg);
1803     break;
1804     }
1805    
1806 maya 3227 init_hosts_dlg(pvar, dlg);
1807     // add host check box���`�F�b�N���f�t�H���g������������
1808     SendMessage(GetDlgItem(dlg, IDC_ADDTOKNOWNHOSTS), BM_SETCHECK, BST_CHECKED, 0);
1809    
1810 zmatsuo 7592 CenterWindow(dlg, GetParent(dlg));
1811    
1812 maya 3227 return TRUE; /* because we do not set the focus */
1813    
1814     case WM_COMMAND:
1815 zmatsuo 7896 pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
1816 maya 3227
1817     switch (LOWORD(wParam)) {
1818     case IDC_CONTINUE:
1819 yutakapon 5562 // �F�������T�[�o�������f�������������A�L�����Z�������������B(2014.3.31 yutaka)
1820     if (!pvar->cv->Ready) {
1821     goto canceled;
1822     }
1823    
1824 maya 3227 if (IsDlgButtonChecked(dlg, IDC_ADDTOKNOWNHOSTS)) {
1825     add_host_key(pvar);
1826     }
1827    
1828 yutakapon 8093 /*
1829     * known_hosts�_�C�A���O���������������~��������
1830     * SSH�T�[�o�����l�S�V�G�[�V���������J�������B
1831     */
1832     HOSTS_resume_session_after_known_hosts(pvar);
1833 maya 3227
1834     pvar->hosts_state.hosts_dialog = NULL;
1835    
1836     EndDialog(dlg, 1);
1837     return TRUE;
1838    
1839     case IDCANCEL: /* kill the connection */
1840 yutakapon 5562 canceled:
1841 yutakapon 8093 /*
1842     * known_hosts���L�����Z�����������A���J�p�����\�[�X���j�����������B
1843     */
1844     HOSTS_cancel_session_after_known_hosts(pvar);
1845    
1846 maya 3227 pvar->hosts_state.hosts_dialog = NULL;
1847 maya 5678 notify_closed_connection(pvar, "authentication cancelled");
1848 maya 3227 EndDialog(dlg, 0);
1849     return TRUE;
1850    
1851 yutakapon 8093 case IDCLOSE:
1852     /*
1853     * known_hosts�����T�[�o�������l�b�g���[�N���f�����������A
1854     * �_�C�A���O�������������B
1855     */
1856     HOSTS_cancel_session_after_known_hosts(pvar);
1857    
1858     pvar->hosts_state.hosts_dialog = NULL;
1859     EndDialog(dlg, 0);
1860     return TRUE;
1861    
1862 maya 6132 case IDC_FP_HASH_ALG_MD5:
1863     hosts_dlg_set_fingerprint(pvar, dlg, SSH_DIGEST_MD5);
1864     return TRUE;
1865    
1866     case IDC_FP_HASH_ALG_SHA256:
1867     hosts_dlg_set_fingerprint(pvar, dlg, SSH_DIGEST_SHA256);
1868     return TRUE;
1869    
1870 maya 3227 default:
1871     return FALSE;
1872     }
1873    
1874 zmatsuo 7714 case WM_DPICHANGED:
1875     pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
1876     if (pvar->hFontFixed != NULL) {
1877     DeleteObject(pvar->hFontFixed);
1878     }
1879     pvar->hFontFixed = UTIL_get_lang_fixedfont(dlg, pvar->ts->UILanguageFile);
1880     if (pvar->hFontFixed != NULL) {
1881     SendDlgItemMessage(dlg, IDC_FP_RANDOMART, WM_SETFONT,
1882     (WPARAM)pvar->hFontFixed, MAKELPARAM(TRUE,0));
1883     }
1884     return FALSE;
1885    
1886     case WM_DESTROY:
1887     pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
1888     if (pvar->hFontFixed != NULL) {
1889     DeleteObject(pvar->hFontFixed);
1890     pvar->hFontFixed = NULL;
1891     }
1892     return FALSE;
1893    
1894 maya 3227 default:
1895     return FALSE;
1896     }
1897     }
1898    
1899     //
1900     // �u�����������m�F�_�C�A���O������
1901     //
1902 zmatsuo 7896 static INT_PTR CALLBACK hosts_replace_dlg_proc(HWND dlg, UINT msg, WPARAM wParam,
1903     LPARAM lParam)
1904 maya 3227 {
1905 zmatsuo 8542 static const DlgTextInfo text_info[] = {
1906 zmatsuo 8240 { 0, "DLG_UNKNOWNHOST_TITLE" },
1907     { IDC_HOSTWARNING, "DLG_DIFFERENTKEY_WARNING" },
1908     { IDC_HOSTWARNING2, "DLG_DIFFERENTKEY_WARNING2" },
1909     { IDC_HOSTFINGERPRINT, "DLG_DIFFERENTKEY_FINGERPRINT" },
1910     { IDC_FP_HASH_ALG, "DLG_DIFFERENTKEY_FP_HASH_ALGORITHM" },
1911     { IDC_ADDTOKNOWNHOSTS, "DLG_DIFFERENTKEY_REPLACE" },
1912     { IDC_CONTINUE, "BTN_CONTINUE" },
1913     { IDCANCEL, "BTN_DISCONNECT" },
1914     };
1915 maya 3227 PTInstVar pvar;
1916    
1917     switch (msg) {
1918     case WM_INITDIALOG:
1919     pvar = (PTInstVar) lParam;
1920     pvar->hosts_state.hosts_dialog = dlg;
1921 zmatsuo 7896 SetWindowLongPtr(dlg, DWLP_USER, lParam);
1922 maya 3227
1923     // �����E�u���������� init_hosts_dlg �����������������A�����O���Z�b�g�����K�v������
1924 doda 8485 SetI18nDlgStrs("TTSSH", dlg, text_info, _countof(text_info), pvar->ts->UILanguageFile);
1925 maya 3227
1926 doda 4559 switch (pvar->dns_key_check) {
1927     case DNS_VERIFY_NOTFOUND:
1928 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_NOTFOUND", pvar, "No host key fingerprint found in DNS.");
1929 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1930     break;
1931     case DNS_VERIFY_MATCH:
1932     case DNS_VERIFY_AUTH_MATCH:
1933 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_MATCH", pvar, "Matching host key fingerprint found in DNS.");
1934 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1935     break;
1936     case DNS_VERIFY_MISMATCH:
1937     case DNS_VERIFY_AUTH_MISMATCH:
1938 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_MISMATCH", pvar, "Mismatching host key fingerprint found in DNS.");
1939 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1940     break;
1941     case DNS_VERIFY_DIFFERENTTYPE:
1942     case DNS_VERIFY_AUTH_DIFFERENTTYPE:
1943 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_DIFFTYPE", pvar, "Mismatching host key type found in DNS.");
1944 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
1945     break;
1946     }
1947    
1948     switch (pvar->dns_key_check) {
1949     case DNS_VERIFY_MATCH:
1950     case DNS_VERIFY_MISMATCH:
1951     case DNS_VERIFY_DIFFERENTTYPE:
1952 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_DNSSEC_NG", pvar, "Found insecure fingerprint in DNS.");
1953 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPDNSSEC, pvar->ts->UIMsg);
1954     break;
1955     case DNS_VERIFY_AUTH_MATCH:
1956     case DNS_VERIFY_AUTH_MISMATCH:
1957     case DNS_VERIFY_AUTH_DIFFERENTTYPE:
1958 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_DNSSEC_OK", pvar, "Found secure fingerprint in DNS.");
1959 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPDNSSEC, pvar->ts->UIMsg);
1960     break;
1961     }
1962    
1963 maya 3227 init_hosts_dlg(pvar, dlg);
1964 zmatsuo 7592 CenterWindow(dlg, GetParent(dlg));
1965 maya 3227 // �f�t�H���g���`�F�b�N����������
1966     return TRUE; /* because we do not set the focus */
1967    
1968     case WM_COMMAND:
1969 zmatsuo 7896 pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
1970 maya 3227
1971     switch (LOWORD(wParam)) {
1972     case IDC_CONTINUE:
1973 yutakapon 5562 // �F�������T�[�o�������f�������������A�L�����Z�������������B(2014.3.31 yutaka)
1974     if (!pvar->cv->Ready) {
1975     goto canceled;
1976     }
1977    
1978 maya 3227 if (IsDlgButtonChecked(dlg, IDC_ADDTOKNOWNHOSTS)) {
1979     add_host_key(pvar);
1980     delete_different_key(pvar);
1981     }
1982    
1983 yutakapon 8093 /*
1984     * known_hosts�_�C�A���O���������������~��������
1985     * SSH�T�[�o�����l�S�V�G�[�V���������J�������B
1986     */
1987     HOSTS_resume_session_after_known_hosts(pvar);
1988 maya 3227
1989     pvar->hosts_state.hosts_dialog = NULL;
1990    
1991     EndDialog(dlg, 1);
1992     return TRUE;
1993    
1994     case IDCANCEL: /* kill the connection */
1995 yutakapon 5562 canceled:
1996 yutakapon 8093 /*
1997     * known_hosts���L�����Z�����������A���J�p�����\�[�X���j�����������B
1998     */
1999     HOSTS_cancel_session_after_known_hosts(pvar);
2000    
2001 maya 3227 pvar->hosts_state.hosts_dialog = NULL;
2002 maya 5678 notify_closed_connection(pvar, "authentication cancelled");
2003 maya 3227 EndDialog(dlg, 0);
2004     return TRUE;
2005    
2006 yutakapon 8093 case IDCLOSE:
2007     /*
2008     * known_hosts�����T�[�o�������l�b�g���[�N���f�����������A
2009     * �_�C�A���O�������������B
2010     */
2011     HOSTS_cancel_session_after_known_hosts(pvar);
2012    
2013     pvar->hosts_state.hosts_dialog = NULL;
2014     EndDialog(dlg, 0);
2015     return TRUE;
2016    
2017 maya 6132 case IDC_FP_HASH_ALG_MD5:
2018     hosts_dlg_set_fingerprint(pvar, dlg, SSH_DIGEST_MD5);
2019     return TRUE;
2020    
2021     case IDC_FP_HASH_ALG_SHA256:
2022     hosts_dlg_set_fingerprint(pvar, dlg, SSH_DIGEST_SHA256);
2023     return TRUE;
2024    
2025 maya 3227 default:
2026     return FALSE;
2027     }
2028    
2029 zmatsuo 7714 case WM_DPICHANGED:
2030     pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
2031     if (pvar->hFontFixed != NULL) {
2032     DeleteObject(pvar->hFontFixed);
2033     }
2034     pvar->hFontFixed = UTIL_get_lang_fixedfont(dlg, pvar->ts->UILanguageFile);
2035     if (pvar->hFontFixed != NULL) {
2036     SendDlgItemMessage(dlg, IDC_FP_RANDOMART, WM_SETFONT,
2037     (WPARAM)pvar->hFontFixed, MAKELPARAM(TRUE,0));
2038     }
2039     return FALSE;
2040    
2041     case WM_DESTROY:
2042     pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
2043     if (pvar->hFontFixed != NULL) {
2044     DeleteObject(pvar->hFontFixed);
2045     pvar->hFontFixed = NULL;
2046     }
2047     return FALSE;
2048    
2049 maya 3227 default:
2050     return FALSE;
2051     }
2052     }
2053    
2054 maya 4332 //
2055     // �����z�X�g�����`�����������������m�F�_�C�A���O������
2056     //
2057 zmatsuo 7896 static INT_PTR CALLBACK hosts_add2_dlg_proc(HWND dlg, UINT msg, WPARAM wParam,
2058     LPARAM lParam)
2059 maya 4332 {
2060 zmatsuo 8542 static const DlgTextInfo text_info[] = {
2061 zmatsuo 8240 { 0, "DLG_DIFFERENTTYPEKEY_TITLE" },
2062     { IDC_HOSTWARNING, "DLG_DIFFERENTTYPEKEY_WARNING" },
2063     { IDC_HOSTWARNING2, "DLG_DIFFERENTTYPEKEY_WARNING2" },
2064     { IDC_HOSTFINGERPRINT, "DLG_DIFFERENTTYPEKEY_FINGERPRINT" },
2065     { IDC_FP_HASH_ALG, "DLG_DIFFERENTTYPEKEY_FP_HASH_ALGORITHM" },
2066     { IDC_ADDTOKNOWNHOSTS, "DLG_DIFFERENTTYPEKEY_ADD" },
2067     { IDC_CONTINUE, "BTN_CONTINUE" },
2068     { IDCANCEL, "BTN_DISCONNECT" },
2069     };
2070 maya 4332 PTInstVar pvar;
2071    
2072     switch (msg) {
2073     case WM_INITDIALOG:
2074     pvar = (PTInstVar) lParam;
2075     pvar->hosts_state.hosts_dialog = dlg;
2076 zmatsuo 7896 SetWindowLongPtr(dlg, DWLP_USER, lParam);
2077 maya 4332
2078     // �����E�u���������� init_hosts_dlg �����������������A�����O���Z�b�g�����K�v������
2079 doda 8485 SetI18nDlgStrs("TTSSH", dlg, text_info, _countof(text_info), pvar->ts->UILanguageFile);
2080 maya 4332
2081 doda 4559 switch (pvar->dns_key_check) {
2082     case DNS_VERIFY_NOTFOUND:
2083 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_NOTFOUND", pvar, "No host key fingerprint found in DNS.");
2084 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
2085     break;
2086     case DNS_VERIFY_MATCH:
2087     case DNS_VERIFY_AUTH_MATCH:
2088 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_MATCH", pvar, "Matching host key fingerprint found in DNS.");
2089 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
2090     break;
2091     case DNS_VERIFY_MISMATCH:
2092     case DNS_VERIFY_AUTH_MISMATCH:
2093 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_MISMATCH", pvar, "Mismatching host key fingerprint found in DNS.");
2094 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
2095     break;
2096     case DNS_VERIFY_DIFFERENTTYPE:
2097     case DNS_VERIFY_AUTH_DIFFERENTTYPE:
2098 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_SSHFP_DIFFTYPE", pvar, "Mismatching host key type found in DNS.");
2099 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPCHECK, pvar->ts->UIMsg);
2100     break;
2101     }
2102    
2103     switch (pvar->dns_key_check) {
2104     case DNS_VERIFY_MATCH:
2105     case DNS_VERIFY_MISMATCH:
2106     case DNS_VERIFY_DIFFERENTTYPE:
2107 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_DNSSEC_NG", pvar, "Found insecure fingerprint in DNS.");
2108 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPDNSSEC, pvar->ts->UIMsg);
2109     break;
2110     case DNS_VERIFY_AUTH_MATCH:
2111     case DNS_VERIFY_AUTH_MISMATCH:
2112     case DNS_VERIFY_AUTH_DIFFERENTTYPE:
2113 maya 4602 UTIL_get_lang_msg("DLG_HOSTKEY_DNSSEC_OK", pvar, "Found secure fingerprint in DNS.");
2114 doda 4559 SetDlgItemText(dlg, IDC_HOSTSSHFPDNSSEC, pvar->ts->UIMsg);
2115     break;
2116     }
2117    
2118 maya 4332 init_hosts_dlg(pvar, dlg);
2119 zmatsuo 7592 CenterWindow(dlg, GetParent(dlg));
2120 maya 4332 // add host check box ���f�t�H���g�� off ������
2121     // SendMessage(GetDlgItem(dlg, IDC_ADDTOKNOWNHOSTS), BM_SETCHECK, BST_CHECKED, 0);
2122    
2123     return TRUE; /* because we do not set the focus */
2124    
2125     case WM_COMMAND:
2126 zmatsuo 7896 pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
2127 maya 4332
2128     switch (LOWORD(wParam)) {
2129     case IDC_CONTINUE:
2130 yutakapon 5562 // �F�������T�[�o�������f�������������A�L�����Z�������������B(2014.3.31 yutaka)
2131     if (!pvar->cv->Ready) {
2132     goto canceled;
2133     }
2134    
2135 maya 4332 if (IsDlgButtonChecked(dlg, IDC_ADDTOKNOWNHOSTS)) {
2136     add_host_key(pvar);
2137     }
2138    
2139 yutakapon 8093 /*
2140     * known_hosts�_�C�A���O���������������~��������
2141     * SSH�T�[�o�����l�S�V�G�[�V���������J�������B
2142     */
2143     HOSTS_resume_session_after_known_hosts(pvar);
2144 maya 4332
2145     pvar->hosts_state.hosts_dialog = NULL;
2146    
2147     EndDialog(dlg, 1);
2148     return TRUE;
2149    
2150     case IDCANCEL: /* kill the connection */
2151 yutakapon 5562 canceled:
2152 yutakapon 8093 /*
2153     * known_hosts���L�����Z�����������A���J�p�����\�[�X���j�����������B
2154     */
2155     HOSTS_cancel_session_after_known_hosts(pvar);
2156    
2157 maya 4332 pvar->hosts_state.hosts_dialog = NULL;
2158 maya 5678 notify_closed_connection(pvar, "authentication cancelled");
2159 maya 4332 EndDialog(dlg, 0);
2160     return TRUE;
2161    
2162 yutakapon 8093 case IDCLOSE:
2163     /*
2164     * known_hosts�����T�[�o�������l�b�g���[�N���f�����������A
2165     * �_�C�A���O�������������B
2166     */
2167     HOSTS_cancel_session_after_known_hosts(pvar);
2168    
2169     pvar->hosts_state.hosts_dialog = NULL;
2170     EndDialog(dlg, 0);
2171     return TRUE;
2172    
2173 maya 6132 case IDC_FP_HASH_ALG_MD5:
2174     hosts_dlg_set_fingerprint(pvar, dlg, SSH_DIGEST_MD5);
2175     return TRUE;
2176    
2177     case IDC_FP_HASH_ALG_SHA256:
2178     hosts_dlg_set_fingerprint(pvar, dlg, SSH_DIGEST_SHA256);
2179     return TRUE;
2180    
2181 maya 4332 default:
2182     return FALSE;
2183     }
2184    
2185 zmatsuo 7714 case WM_DPICHANGED:
2186     pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
2187     if (pvar->hFontFixed != NULL) {
2188     DeleteObject(pvar->hFontFixed);
2189     }
2190     pvar->hFontFixed = UTIL_get_lang_fixedfont(dlg, pvar->ts->UILanguageFile);
2191     if (pvar->hFontFixed != NULL) {
2192     SendDlgItemMessage(dlg, IDC_FP_RANDOMART, WM_SETFONT,
2193     (WPARAM)pvar->hFontFixed, MAKELPARAM(TRUE,0));
2194     }
2195     return FALSE;
2196    
2197     case WM_DESTROY:
2198     pvar = (PTInstVar) GetWindowLongPtr(dlg, DWLP_USER);
2199     if (pvar->hFontFixed != NULL) {
2200     DeleteObject(pvar->hFontFixed);
2201     pvar->hFontFixed = NULL;
2202     }
2203     return FALSE;
2204    
2205 maya 4332 default:
2206     return FALSE;
2207     }
2208     }
2209    
2210 maya 3227 void HOSTS_do_unknown_host_dialog(HWND wnd, PTInstVar pvar)
2211     {
2212     if (pvar->hosts_state.hosts_dialog == NULL) {
2213 yutakapon 8093 /* known_hosts�������������AID_SSHASYNCMESSAGEBOX ���g����
2214     * MessageBox ���\�������������A�I�[�i�[����(no owner)�����������A
2215     * MessageBox ��Tera Term�������B���������������B
2216     * ���������� GetActiveWindow() �������o�����Aknown_hosts�_�C�A���O��
2217     * �I�[�i�[�� MessageBox ���������ATera Term�������B�����������B
2218     * �������Aknown_hosts�_�C�A���O���I�[�i�[������ Tera Term ���w������
2219     * �����������B
2220     */
2221     HWND cur_active = NULL;
2222 maya 3227
2223     DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SSHUNKNOWNHOST),
2224     cur_active != NULL ? cur_active : wnd,
2225     hosts_add_dlg_proc, (LPARAM) pvar);
2226     }
2227     }
2228    
2229 maya 4330 void HOSTS_do_different_key_dialog(HWND wnd, PTInstVar pvar)
2230 maya 3227 {
2231     if (pvar->hosts_state.hosts_dialog == NULL) {
2232 yutakapon 8093 /* known_hosts�������������AID_SSHASYNCMESSAGEBOX ���g����
2233     * MessageBox ���\�������������A�I�[�i�[����(no owner)�����������A
2234     * MessageBox ��Tera Term�������B���������������B
2235     * ���������� GetActiveWindow() �������o�����Aknown_hosts�_�C�A���O��
2236     * �I�[�i�[�� MessageBox ���������ATera Term�������B�����������B
2237     * �������Aknown_hosts�_�C�A���O���I�[�i�[������ Tera Term ���w������
2238     * �����������B
2239     */
2240     HWND cur_active = NULL;
2241 maya 3227
2242 maya 4330 DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SSHDIFFERENTKEY),
2243 maya 3227 cur_active != NULL ? cur_active : wnd,
2244     hosts_replace_dlg_proc, (LPARAM) pvar);
2245     }
2246     }
2247    
2248 maya 4332 void HOSTS_do_different_type_key_dialog(HWND wnd, PTInstVar pvar)
2249     {
2250     if (pvar->hosts_state.hosts_dialog == NULL) {
2251 yutakapon 8093 /* known_hosts�������������AID_SSHASYNCMESSAGEBOX ���g����
2252     * MessageBox ���\�������������A�I�[�i�[����(no owner)�����������A
2253     * MessageBox ��Tera Term�������B���������������B
2254     * ���������� GetActiveWindow() �������o�����Aknown_hosts�_�C�A���O��
2255     * �I�[�i�[�� MessageBox ���������ATera Term�������B�����������B
2256     * �������Aknown_hosts�_�C�A���O���I�[�i�[������ Tera Term ���w������
2257     * �����������B
2258     */
2259     HWND cur_active = NULL;
2260 maya 4332
2261     DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SSHDIFFERENTTYPEKEY),
2262     cur_active != NULL ? cur_active : wnd,
2263     hosts_add2_dlg_proc, (LPARAM) pvar);
2264     }
2265     }
2266    
2267 yutakapon 8093 /*
2268     * �T�[�o�����������������z�X�g���J�������������`�F�b�N���A
2269     * �K�v�������� known_hosts �_�C�A���O�������o���B
2270     *
2271     * hostname: ���������z�X�g��
2272     * tcpport: �������|�[�g����
2273     * key: �T�[�o���������J��
2274     *
2275     * return:
2276     * TRUE: known_hosts�_�C�A���O�������o�����s�v
2277     * FALSE: known_hosts�_�C�A���O�������o����
2278     *
2279     */
2280 doda 6801 BOOL HOSTS_check_host_key(PTInstVar pvar, char *hostname, unsigned short tcpport, Key *key)
2281 maya 3227 {
2282 doda 4559 int found_different_key = 0, found_different_type_key = 0;
2283 maya 6152 Key key2; // known_hosts���o�^������������
2284 yutakapon 8093 DWORD id;
2285 maya 3227
2286 doda 4559 pvar->dns_key_check = DNS_VERIFY_NONE;
2287    
2288 maya 3227 // ������ known_hosts �t�@�C�������z�X�g���J�����������������������A���������r�����B
2289     if (pvar->hosts_state.prefetched_hostname != NULL
2290     && _stricmp(pvar->hosts_state.prefetched_hostname, hostname) == 0
2291 maya 6152 && HOSTS_compare_public_key(&pvar->hosts_state.hostkey, key) == 1) {
2292 maya 3227
2293 yutakapon 8093 // ���������������B
2294     return TRUE;
2295 maya 3227 }
2296    
2297     // �������������������������A�������_���t�@�C��������������
2298 maya 6152 memset(&key2, 0, sizeof(key2));
2299 maya 3227 if (begin_read_host_files(pvar, 0)) {
2300     do {
2301 maya 6152 if (!read_host_key(pvar, hostname, tcpport, 0, 0, &key2)) {
2302 maya 3227 break;
2303     }
2304    
2305 maya 6152 if (key2.type != KEY_UNSPEC) {
2306     int match = HOSTS_compare_public_key(&key2, key);
2307 maya 4332 if (match == 1) {
2308 maya 3227 finish_read_host_files(pvar, 0);
2309     // ���������G���g�����Q�������A���v�����L�[�������������������B
2310 maya 6998 // About TTSSH �_�C�A���O�����\�����������A�������������������B
2311     key_copy(&pvar->hosts_state.hostkey, key);
2312    
2313 maya 3227 return TRUE;
2314 maya 4332 }
2315     else if (match == 0) {
2316 maya 3227 // �L�[�� known_hosts ���������������A�L�[�����e���������B
2317     found_different_key = 1;
2318     }
2319 maya 4332 else {
2320     // �L�[���`������������
2321     found_different_type_key = 1;
2322     }
2323 maya 3227 }
2324 maya 6152 } while (key2.type != KEY_UNSPEC); // �L�[�����������������������[�v����
2325 maya 3227
2326 maya 6152 key_init(&key2);
2327 maya 3227 finish_read_host_files(pvar, 0);
2328     }
2329    
2330     // known_hosts �������������L�[���������t�@�C�������������������A�������������������B
2331 maya 6147 key_copy(&pvar->hosts_state.hostkey, key);
2332 maya 6145
2333 maya 3227 free(pvar->hosts_state.prefetched_hostname);
2334     pvar->hosts_state.prefetched_hostname = _strdup(hostname);
2335    
2336 yutakapon 3631 // "/nosecuritywarning"���w�����������������A�_�C�A���O���\���������� return success �����B
2337     if (pvar->nocheck_known_hosts == TRUE) {
2338 yutakapon 8093 // ���������������B
2339 yutakapon 3631 return TRUE;
2340     }
2341    
2342 doda 4542 if (pvar->settings.VerifyHostKeyDNS && !is_numeric_hostname(hostname)) {
2343 maya 6063 pvar->dns_key_check = verify_hostkey_dns(pvar, hostname, key);
2344 doda 4542 }
2345    
2346 maya 3227 // known_hosts�_�C�A���O�������I���\�������A�������_�����������[�U���m�F
2347     // �������K�v�����������A�����R�[�������X�����B
2348     // ����������known_hosts���m�F�����������A�T�[�o�����[�U���������������������������������B
2349     // (2007.10.1 yutaka)
2350 yutakapon 8093 /*
2351     * known_hosts�_�C�A���O�����������\�����������������������B
2352     * known_hosts�_�C�A���O���\�������������������A�T�[�o�������f���s�����A
2353     * TTXCloseTCP�������o�����ATTSSH�����\�[�X�������������������B
2354     * SSH�n���h����������known_hosts�_�C�A���O���o�����~���������������A
2355     * �������J�����s���A�N�Z�X���������B
2356     * (2019.9.3 yutaka)
2357     */
2358 maya 3227 if (found_different_key) {
2359 yutakapon 8093 // TTXProcessCommand ���� HOSTS_do_different_key_dialog() �������o���B
2360     id = ID_SSHDIFFERENTKEY;
2361 maya 4332 }
2362     else if (found_different_type_key) {
2363 yutakapon 8093 // TTXProcessCommand ���� HOSTS_do_different_type_key_dialog() �������o���B
2364     id = ID_SSHDIFFERENT_TYPE_KEY;
2365 maya 4332 }
2366     else {
2367 yutakapon 8093 // TTXProcessCommand ���� HOSTS_do_unknown_host_dialog() �������o���B
2368     id = ID_SSHUNKNOWNHOST;
2369 maya 3227 }
2370    
2371 yutakapon 8093 PostMessage(pvar->NotificationWindow, WM_COMMAND, id, 0);
2372    
2373     logprintf(LOG_LEVEL_INFO, "Calling known_hosts dialog...(%s)",
2374     id == ID_SSHDIFFERENTKEY ? "SSHDIFFERENTKEY" :
2375     id == ID_SSHDIFFERENT_TYPE_KEY ? "SSHDIFFERENT_TYPE_KEY" : "SSHUNKNOWNHOST"
2376     );
2377    
2378     return FALSE;
2379 maya 3227 }
2380    
2381 yutakapon 8093 /*
2382     * known_hosts�_�C�A���O�����[�U���F���ASSH�T�[�o�����l�S�V�G�[�V���������J�����B
2383     */
2384     BOOL HOSTS_resume_session_after_known_hosts(PTInstVar pvar)
2385     {
2386     enum ssh_kex_known_hosts type;
2387     int ret = FALSE;
2388    
2389     type = pvar->contents_after_known_hosts.kex_type;
2390     if (type == SSH1_PUBLIC_KEY_KNOWN_HOSTS) {
2391     ret = handle_server_public_key_after_known_hosts(pvar);
2392    
2393     } else if (type == SSH2_DH_KEX_REPLY_KNOWN_HOSTS) {
2394     ret = handle_SSH2_dh_kex_reply_after_known_hosts(pvar);
2395    
2396     } else if (type == SSH2_DH_GEX_REPLY_KNOWN_HOSTS) {
2397     ret = handle_SSH2_dh_gex_reply_after_known_hosts(pvar);
2398    
2399     } else if (type == SSH2_ECDH_KEX_REPLY_KNOWN_HOSTS) {
2400     ret = handle_SSH2_ecdh_kex_reply_after_known_hosts(pvar);
2401    
2402     }
2403    
2404     return (ret);
2405     }
2406    
2407     /*
2408     * known_hosts�_�C�A���O��SSH�������L�����Z������
2409     */
2410     void HOSTS_cancel_session_after_known_hosts(PTInstVar pvar)
2411     {
2412     enum ssh_kex_known_hosts type;
2413    
2414     type = pvar->contents_after_known_hosts.kex_type;
2415     if (type != NONE_KNOWN_HOSTS) {
2416     handle_SSH2_canel_reply_after_known_hosts(pvar);
2417     }
2418    
2419     return;
2420     }
2421    
2422    
2423 maya 3227 void HOSTS_notify_disconnecting(PTInstVar pvar)
2424     {
2425     if (pvar->hosts_state.hosts_dialog != NULL) {
2426 doda 4531 PostMessage(pvar->hosts_state.hosts_dialog, WM_COMMAND, IDCANCEL, 0);
2427 maya 3227 /* the main window might not go away if it's not enabled. (see vtwin.cpp) */
2428     EnableWindow(pvar->NotificationWindow, TRUE);
2429     }
2430     }
2431    
2432 yutakapon 8093 // TCP�Z�b�V�������N���[�Y�����������Aknown_hosts�_�C�A���O���������������w�����o���B
2433     // HOSTS_notify_disconnecting()�����������A�_�C�A���O���������������A
2434     // SSH�T�[�o�����m���o�������B
2435     void HOSTS_notify_closing_on_exit(PTInstVar pvar)
2436     {
2437