Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8087 - (hide annotations) (download) (as text)
Sun Sep 8 02:29:51 2019 UTC (4 years, 7 months ago) by yutakapon
Original Path: trunk/ttssh2/ttxssh/ttxssh.c
File MIME type: text/x-csrc
File size: 175254 byte(s)
non-fatalおよびfatalメッセージボックスの表示に同一メッセージが複数含まれることがある問題を修正した。

branches/ttssh_improvedからリビジョン8053をマージ:
・non-fatalおよびfatal時のエラーメッセージを登録する際、同一メッセージがすでに
登録されていた場合は追加しないようにした。ユーザに同じメッセージを何回見せても
意味がない(むしろ心証がよくない)。
・メモリ確保できない場合の処理を追加した。

........

1 maya 3227 /*
2 doda 6841 * Copyright (c) 1998-2001, Robert O'Callahan
3 zmatsuo 7605 * (C) 2004-2019 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     /* Tera Term extension mechanism
31     Robert O'Callahan (roc+tt@cs.cmu.edu)
32    
33     Tera Term by Takashi Teranishi (teranishi@rikaxp.riken.go.jp)
34     */
35    
36 zmatsuo 7554 #include "teraterm_conf.h"
37 maya 3227 #include "ttxssh.h"
38     #include "fwdui.h"
39     #include "util.h"
40     #include "ssh.h"
41     #include "ttcommon.h"
42 r850 3298 #include "ttlib.h"
43 yutakapon 5545 #include "keyfiles.h"
44 doda 6250 #include "arc4random.h"
45 zmatsuo 7605 #include "auth.h"
46 maya 3227
47     #include <stdlib.h>
48     #include <stdio.h>
49     #include <string.h>
50     #include <io.h>
51     #include <fcntl.h>
52     #include <sys/stat.h>
53     #include <time.h>
54 doda 3232 #include <mbstring.h>
55 maya 3227
56     #include "resource.h"
57     #include <commctrl.h>
58     #include <commdlg.h>
59     #include <winsock2.h>
60 yasuhide 8064 static char *ProtocolFamilyList[] = { "AUTO", "IPv6", "IPv4", NULL };
61 maya 3227
62 zmatsuo 7542 #include <lmcons.h>
63 maya 3227
64     // include OpenSSL header file
65     #include <openssl/evp.h>
66     #include <openssl/rsa.h>
67     #include <openssl/dsa.h>
68     #include <openssl/bn.h>
69     #include <openssl/pem.h>
70     #include <openssl/rand.h>
71     #include <openssl/rc4.h>
72     #include <openssl/md5.h>
73    
74     // include ZLib header file
75     #include <zlib.h>
76    
77     #include "buffer.h"
78     #include "cipher.h"
79 maya 4321 #include "key.h"
80 zmatsuo 7560 #include "dlglib.h"
81 maya 3227
82     #include "sftp.h"
83    
84     #include "compat_w95.h"
85 zmatsuo 7554 #include "compat_win.h"
86 maya 3227
87 maya 6625 #include "libputty.h"
88 yutakapon 4533
89 zmatsuo 7560 #undef DialogBoxParam
90     #define DialogBoxParam(p1,p2,p3,p4,p5) \
91     TTDialogBoxParam(p1,p2,p3,p4,p5)
92     #undef EndDialog
93     #define EndDialog(p1,p2) \
94     TTEndDialog(p1, p2)
95    
96 maya 3227 #define MATCH_STR(s, o) strncmp((s), (o), NUM_ELEM(o) - 1)
97     #define MATCH_STR_I(s, o) _strnicmp((s), (o), NUM_ELEM(o) - 1)
98    
99     /* This extension implements SSH, so we choose a load order in the
100     "protocols" range. */
101     #define ORDER 2500
102    
103     static HICON SecureLargeIcon = NULL;
104     static HICON SecureSmallIcon = NULL;
105 doda 6691 static HICON SecureNotifyIcon = NULL;
106     static HICON OldNotifyIcon = NULL;
107 maya 3227
108 doda 6801 static TInstVar *pvar;
109 maya 3227
110     typedef struct {
111 maya 4307 int cnt;
112     HWND dlg;
113 maya 4378 ssh_keytype type;
114 maya 3227 } cbarg_t;
115    
116     /* WIN32 allows multiple instances of a DLL */
117     static TInstVar InstVar;
118    
119     /*
120     This code makes lots of assumptions about the order in which Tera Term
121     does things, and how. A key assumption is that the Notification window
122     passed into WSAAsyncSelect is the main terminal window. We also assume
123     that the socket used in the first WSAconnect is the main session socket.
124     */
125    
126     static void init_TTSSH(PTInstVar pvar)
127     {
128     pvar->socket = INVALID_SOCKET;
129     pvar->OldLargeIcon = NULL;
130     pvar->OldSmallIcon = NULL;
131     pvar->NotificationWindow = NULL;
132     pvar->hostdlg_activated = FALSE;
133     pvar->socket = INVALID_SOCKET;
134     pvar->NotificationWindow = NULL;
135     pvar->protocol_major = 0;
136     pvar->protocol_minor = 0;
137    
138     PKT_init(pvar);
139     SSH_init(pvar);
140     CRYPT_init(pvar);
141     AUTH_init(pvar);
142     HOSTS_init(pvar);
143     FWD_init(pvar);
144     FWDUI_init(pvar);
145    
146     ssh_heartbeat_lock_initialize();
147     }
148    
149     static void uninit_TTSSH(PTInstVar pvar)
150     {
151     halt_ssh_heartbeat_thread(pvar);
152    
153     ssh2_channel_free();
154    
155     SSH_end(pvar);
156     PKT_end(pvar);
157     AUTH_end(pvar);
158     CRYPT_end(pvar);
159     HOSTS_end(pvar);
160     FWD_end(pvar);
161     FWDUI_end(pvar);
162    
163     if (pvar->OldLargeIcon != NULL) {
164     PostMessage(pvar->NotificationWindow, WM_SETICON, ICON_BIG,
165     (LPARAM) pvar->OldLargeIcon);
166     pvar->OldLargeIcon = NULL;
167     }
168     if (pvar->OldSmallIcon != NULL) {
169     PostMessage(pvar->NotificationWindow, WM_SETICON, ICON_SMALL,
170     (LPARAM) pvar->OldSmallIcon);
171     pvar->OldSmallIcon = NULL;
172     }
173 doda 6691 if (OldNotifyIcon) {
174     SetCustomNotifyIcon(OldNotifyIcon);
175     }
176 maya 3227
177     ssh_heartbeat_lock_finalize();
178     }
179    
180 doda 6801 static void PASCAL TTXInit(PTTSet ts, PComVar cv)
181 maya 3227 {
182     pvar->settings = *pvar->ts_SSH;
183     pvar->ts = ts;
184     pvar->cv = cv;
185     pvar->fatal_error = FALSE;
186     pvar->showing_err = FALSE;
187     pvar->err_msg = NULL;
188    
189     init_TTSSH(pvar);
190     }
191    
192 maya 4378 static void normalize_generic_order(char *buf, char default_strings[], int default_strings_len)
193     {
194     char listed[max(KEX_DH_MAX,max(SSH_CIPHER_MAX,max(KEY_MAX,max(HMAC_MAX,COMP_MAX)))) + 1];
195     char allowed[max(KEX_DH_MAX,max(SSH_CIPHER_MAX,max(KEY_MAX,max(HMAC_MAX,COMP_MAX)))) + 1];
196 maya 6031 int i, j, k=-1;
197 maya 4378
198     memset(listed, 0, sizeof(listed));
199     memset(allowed, 0, sizeof(allowed));
200 yutakapon 6508
201     // �����������������������X�g�������B
202 maya 4378 for (i = 0; i < default_strings_len ; i++) {
203     allowed[default_strings[i]] = 1;
204     }
205    
206 yutakapon 6508 // �w�����������������������A���������������������A�d���������������������B
207     //
208     // ex. (i=5 ����������������)
209     // i=012345
210     // >:=9<87;A@?B3026(\0)
211     // i+1
212     // <------------>
213     // ��
214     // >:=9<7;A@?B3026(\0)
215     //
216 maya 4378 for (i = 0; buf[i] != 0; i++) {
217     int num = buf[i] - '0';
218    
219     if (num < 0 || num > default_strings_len
220     || !allowed[num]
221     || listed[num]) {
222     memmove(buf + i, buf + i + 1, strlen(buf + i + 1) + 1);
223     i--;
224     } else {
225     listed[num] = 1;
226     }
227 maya 6031
228 yutakapon 6508 // disabled line���������A���u���o���������B
229 maya 6031 if (num == 0) {
230     k = i;
231     }
232 maya 4378 }
233    
234 yutakapon 6508 // �w���������������������������Adisabled line�����O���}�������B
235     //
236     // ex. (Z���}������)
237     // k
238     // >:=9<87;A@?B3026(\0)
239     // k+1
240     // <---->
241     // �� k
242     // >:=9<87;A@?B30026(\0)
243     // �� k
244     // >:=9<87;A@?B3Z026(\0)
245     //
246 maya 6031 for (j = 0; j < default_strings_len && default_strings[j] != 0; j++) {
247     int num = default_strings[j];
248    
249     if (!listed[num] && k >= 0) {
250 yutakapon 6508 int copylen = strlen(buf + k + 1) + 1;
251    
252     memmove(buf + k + 1, buf + k, copylen);
253     buf[k + 1 + copylen] = '\0'; // �I�[���Y�������t�����B
254 maya 6031 buf[k] = num + '0';
255     k++;
256     i++;
257     }
258     }
259     if (k < 0) {
260     j = 0;
261     }
262     else {
263     j++;
264     }
265 yutakapon 6508
266     // disabled line�������������������A�����������������������B
267 maya 6031 for (; j < default_strings_len ; j++) {
268     int num = default_strings[j];
269    
270     if (!listed[num]) {
271     buf[i] = num + '0';
272 doda 7454 listed[num] = 1;
273 maya 6031 i++;
274     }
275     }
276 maya 4378
277     buf[i] = 0;
278     }
279    
280 maya 3227 /*
281     * Remove unsupported cipher or duplicated cipher.
282     * Add unspecified ciphers at the end of list.
283     */
284 doda 6801 static void normalize_cipher_order(char *buf)
285 maya 3227 {
286     /* SSH_CIPHER_NONE means that all ciphers below that one are disabled.
287     We *never* allow no encryption. */
288 maya 4378 static char default_strings[] = {
289 doda 6985 SSH2_CIPHER_AES256_GCM,
290 doda 4433 SSH2_CIPHER_CAMELLIA256_CTR,
291 maya 3227 SSH2_CIPHER_AES256_CTR,
292 doda 4433 SSH2_CIPHER_CAMELLIA256_CBC,
293 maya 3227 SSH2_CIPHER_AES256_CBC,
294 doda 4433 SSH2_CIPHER_CAMELLIA192_CTR,
295 maya 3227 SSH2_CIPHER_AES192_CTR,
296 doda 4433 SSH2_CIPHER_CAMELLIA192_CBC,
297 maya 3227 SSH2_CIPHER_AES192_CBC,
298 doda 6985 SSH2_CIPHER_AES128_GCM,
299 doda 4433 SSH2_CIPHER_CAMELLIA128_CTR,
300 maya 3227 SSH2_CIPHER_AES128_CTR,
301 doda 4433 SSH2_CIPHER_CAMELLIA128_CBC,
302 maya 3227 SSH2_CIPHER_AES128_CBC,
303 doda 3850 SSH2_CIPHER_3DES_CTR,
304 maya 3227 SSH2_CIPHER_3DES_CBC,
305 doda 3850 SSH2_CIPHER_BLOWFISH_CTR,
306 maya 3227 SSH2_CIPHER_BLOWFISH_CBC,
307 doda 3850 SSH2_CIPHER_CAST128_CTR,
308 maya 3227 SSH2_CIPHER_CAST128_CBC,
309     SSH_CIPHER_3DES,
310     SSH_CIPHER_NONE,
311 maya 5917 SSH2_CIPHER_ARCFOUR256,
312     SSH2_CIPHER_ARCFOUR128,
313     SSH2_CIPHER_ARCFOUR,
314     SSH_CIPHER_BLOWFISH,
315 maya 3227 SSH_CIPHER_DES,
316 maya 4432 0, 0, 0 // Dummy for SSH_CIPHER_IDEA, SSH_CIPHER_TSS, SSH_CIPHER_RC4
317 maya 3227 };
318    
319 maya 4378 normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings));
320 maya 3227 }
321    
322 doda 6801 static void normalize_kex_order(char *buf)
323 yutakapon 4367 {
324     static char default_strings[] = {
325     KEX_ECDH_SHA2_256,
326     KEX_ECDH_SHA2_384,
327     KEX_ECDH_SHA2_521,
328 doda 6310 KEX_DH_GRP18_SHA512,
329     KEX_DH_GRP16_SHA512,
330 doda 6263 KEX_DH_GRP14_SHA256,
331 yutakapon 4367 KEX_DH_GEX_SHA256,
332     KEX_DH_GEX_SHA1,
333     KEX_DH_GRP14_SHA1,
334     KEX_DH_GRP1_SHA1,
335     KEX_DH_NONE,
336     };
337    
338     normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings));
339     }
340    
341 doda 6801 static void normalize_host_key_order(char *buf)
342 yutakapon 4367 {
343     static char default_strings[] = {
344     KEY_ECDSA256,
345     KEY_ECDSA384,
346     KEY_ECDSA521,
347 yutakapon 5545 KEY_ED25519,
348 yutakapon 4367 KEY_RSA,
349     KEY_DSA,
350     KEY_NONE,
351     };
352    
353     normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings));
354     }
355    
356 doda 6801 static void normalize_mac_order(char *buf)
357 yutakapon 4367 {
358     static char default_strings[] = {
359 doda 6969 HMAC_SHA2_512_EtM,
360     HMAC_SHA2_256_EtM,
361     HMAC_SHA1_EtM,
362 doda 4434 HMAC_SHA2_512,
363     HMAC_SHA2_256,
364 yutakapon 4367 HMAC_SHA1,
365 doda 6969 HMAC_RIPEMD160_EtM,
366 doda 4423 HMAC_RIPEMD160,
367 doda 6969 HMAC_MD5_EtM,
368 yutakapon 4367 HMAC_MD5,
369 doda 4426 HMAC_NONE,
370 doda 6969 HMAC_SHA1_96_EtM,
371     HMAC_MD5_96_EtM,
372 doda 4434 HMAC_SHA1_96,
373     HMAC_MD5_96,
374 doda 5016 0, // Dummy for HMAC_SHA2_512_96,
375     0, // Dummy for HMAC_SHA2_256_96,
376 yutakapon 4367 };
377    
378     normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings));
379     }
380    
381 doda 6801 static void normalize_comp_order(char *buf)
382 yutakapon 4367 {
383     static char default_strings[] = {
384 doda 4371 COMP_DELAYED,
385     COMP_ZLIB,
386 maya 4378 COMP_NOCOMP,
387 yutakapon 4367 COMP_NONE,
388     };
389    
390     normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings));
391     }
392    
393    
394 maya 3227 /* Remove local settings from the shared memory block. */
395     static void clear_local_settings(PTInstVar pvar)
396     {
397     pvar->ts_SSH->TryDefaultAuth = FALSE;
398     }
399    
400 doda 6801 static BOOL read_BOOL_option(PCHAR fileName, char *keyName, BOOL def)
401 maya 3227 {
402     char buf[1024];
403    
404     buf[0] = 0;
405     GetPrivateProfileString("TTSSH", keyName, "", buf, sizeof(buf),
406     fileName);
407     if (buf[0] == 0) {
408     return def;
409     } else {
410     return atoi(buf) != 0 ||
411     _stricmp(buf, "yes") == 0 ||
412     _stricmp(buf, "y") == 0;
413     }
414     }
415    
416 doda 6801 static void read_string_option(PCHAR fileName, char *keyName,
417     char *def, char *buf, int bufSize)
418 maya 3227 {
419    
420     buf[0] = 0;
421     GetPrivateProfileString("TTSSH", keyName, def, buf, bufSize, fileName);
422     }
423    
424     static void read_ssh_options(PTInstVar pvar, PCHAR fileName)
425     {
426     char buf[1024];
427 doda 6801 TS_SSH *settings = pvar->ts_SSH;
428 maya 3227
429     #define READ_STD_STRING_OPTION(name) \
430     read_string_option(fileName, #name, "", settings->name, sizeof(settings->name))
431    
432     settings->Enabled = read_BOOL_option(fileName, "Enabled", FALSE);
433    
434     buf[0] = 0;
435     GetPrivateProfileString("TTSSH", "Compression", "", buf, sizeof(buf),
436     fileName);
437     settings->CompressionLevel = atoi(buf);
438     if (settings->CompressionLevel < 0 || settings->CompressionLevel > 9) {
439     settings->CompressionLevel = 0;
440     }
441    
442     READ_STD_STRING_OPTION(DefaultUserName);
443 zmatsuo 7632 settings->DefaultUserType = GetPrivateProfileInt("TTSSH", "DefaultUserType", 1, fileName);
444    
445 maya 3227 READ_STD_STRING_OPTION(DefaultForwarding);
446     READ_STD_STRING_OPTION(DefaultRhostsLocalUserName);
447     READ_STD_STRING_OPTION(DefaultRhostsHostPrivateKeyFile);
448     READ_STD_STRING_OPTION(DefaultRSAPrivateKeyFile);
449    
450     READ_STD_STRING_OPTION(CipherOrder);
451     normalize_cipher_order(settings->CipherOrder);
452    
453 yutakapon 4367 // KEX order
454     READ_STD_STRING_OPTION(KexOrder);
455     normalize_kex_order(settings->KexOrder);
456     // Host Key algorithm order
457     READ_STD_STRING_OPTION(HostKeyOrder);
458     normalize_host_key_order(settings->HostKeyOrder);
459 doda 6821 // HMAC order
460 yutakapon 4367 READ_STD_STRING_OPTION(MacOrder);
461     normalize_mac_order(settings->MacOrder);
462     // Compression algorithm order
463     READ_STD_STRING_OPTION(CompOrder);
464     normalize_comp_order(settings->CompOrder);
465    
466 maya 3227 read_string_option(fileName, "KnownHostsFiles", "ssh_known_hosts",
467     settings->KnownHostsFiles,
468     sizeof(settings->KnownHostsFiles));
469    
470     buf[0] = 0;
471     GetPrivateProfileString("TTSSH", "DefaultAuthMethod", "", buf,
472     sizeof(buf), fileName);
473     settings->DefaultAuthMethod = atoi(buf);
474     if (settings->DefaultAuthMethod != SSH_AUTH_PASSWORD
475     && settings->DefaultAuthMethod != SSH_AUTH_RSA
476     && settings->DefaultAuthMethod != SSH_AUTH_TIS // add (2005.3.12 yutaka)
477     && settings->DefaultAuthMethod != SSH_AUTH_RHOSTS
478     && settings->DefaultAuthMethod != SSH_AUTH_PAGEANT) {
479     /* this default can never be SSH_AUTH_RHOSTS_RSA because that is not a
480     selection in the dialog box; SSH_AUTH_RHOSTS_RSA is automatically chosen
481     when the dialog box has rhosts selected and an host private key file
482     is supplied. */
483     settings->DefaultAuthMethod = SSH_AUTH_PASSWORD;
484     }
485    
486     buf[0] = 0;
487     GetPrivateProfileString("TTSSH", "LogLevel", "", buf, sizeof(buf),
488     fileName);
489     settings->LogLevel = atoi(buf);
490    
491     buf[0] = 0;
492     GetPrivateProfileString("TTSSH", "WriteBufferSize", "", buf,
493     sizeof(buf), fileName);
494     settings->WriteBufferSize = atoi(buf);
495     if (settings->WriteBufferSize <= 0) {
496     settings->WriteBufferSize = (PACKET_MAX_SIZE / 2); // 2MB
497     }
498    
499     // SSH protocol version (2004.10.11 yutaka)
500     // default is SSH2 (2004.11.30 yutaka)
501     settings->ssh_protocol_version = GetPrivateProfileInt("TTSSH", "ProtocolVersion", 2, fileName);
502    
503     // SSH heartbeat time(second) (2004.12.11 yutaka)
504     settings->ssh_heartbeat_overtime = GetPrivateProfileInt("TTSSH", "HeartBeat", 60, fileName);
505    
506     // �p�X���[�h�F�����������J���F�����g���p�X���[�h����������������������������������
507     // �\���B(2006.8.5 yutaka)
508     settings->remember_password = GetPrivateProfileInt("TTSSH", "RememberPassword", 1, fileName);
509    
510     // �������F���_�C�A���O���T�|�[�g�������������\�b�h���`�F�b�N���A
511     // ���������\�b�h���O���C�A�E�g���� (2007.9.24 maya)
512     settings->CheckAuthListFirst = read_BOOL_option(fileName, "CheckAuthListFirst", FALSE);
513    
514     // 768bit ������ RSA ���������T�[�o�����������L�������� (2008.9.11 maya)
515     settings->EnableRsaShortKeyServer = read_BOOL_option(fileName, "EnableRsaShortKeyServer", FALSE);
516    
517     // agent forward ���L�������� (2008.11.25 maya)
518     settings->ForwardAgent = read_BOOL_option(fileName, "ForwardAgent", FALSE);
519    
520 maya 4229 // agent forward �m�F���L��������
521     settings->ForwardAgentConfirm = read_BOOL_option(fileName, "ForwardAgentConfirm", TRUE);
522    
523 doda 6663 // agent forward �m�F���L��������
524     settings->ForwardAgentNotify = read_BOOL_option(fileName, "ForwardAgentNotify", TRUE);
525    
526 doda 4531 // �z�X�g���� DNS �����`�F�b�N (RFC 4255)
527 doda 4619 settings->VerifyHostKeyDNS = read_BOOL_option(fileName, "VerifyHostKeyDNS", TRUE);
528 doda 4531
529 doda 5261 // icon
530     GetPrivateProfileString("TTSSH", "SSHIcon", "", buf, sizeof(buf), fileName);
531     if ((_stricmp(buf, "old") == 0) ||
532     (_stricmp(buf, "yellow") == 0) ||
533     (_stricmp(buf, "securett_yellow") == 0)) {
534     settings->IconID = IDI_SECURETT_YELLOW;
535     }
536 maya 6386 else if ((_stricmp(buf, "green") == 0) ||
537     (_stricmp(buf, "securett_green") == 0)) {
538     settings->IconID = IDI_SECURETT_GREEN;
539     }
540 doda 5261 else {
541     settings->IconID = IDI_SECURETT;
542     }
543    
544 yutakapon 5620 // �G���[�������x�������|�b�v�A�b�v���b�Z�[�W���}�~���� (2014.6.26 yutaka)
545     settings->DisablePopupMessage = GetPrivateProfileInt("TTSSH", "DisablePopupMessage", 0, fileName);
546    
547 doda 5793 READ_STD_STRING_OPTION(X11Display);
548    
549 yutakapon 5849 settings->UpdateHostkeys = GetPrivateProfileInt("TTSSH", "UpdateHostkeys", 0, fileName);
550 yutakapon 5839
551 doda 5900 settings->GexMinimalGroupSize = GetPrivateProfileInt("TTSSH", "GexMinimalGroupSize", 0, fileName);
552    
553 doda 7048 settings->AuthBanner = GetPrivateProfileInt("TTSSH", "AuthBanner", 1, fileName);
554    
555 maya 3227 clear_local_settings(pvar);
556     }
557    
558     static void write_ssh_options(PTInstVar pvar, PCHAR fileName,
559 doda 6801 TS_SSH *settings, BOOL copy_forward)
560 maya 3227 {
561     char buf[1024];
562    
563     WritePrivateProfileString("TTSSH", "Enabled",
564     settings->Enabled ? "1" : "0", fileName);
565    
566     _itoa(settings->CompressionLevel, buf, 10);
567     WritePrivateProfileString("TTSSH", "Compression", buf, fileName);
568    
569 zmatsuo 7632 _itoa(settings->DefaultUserType, buf, 10);
570     WritePrivateProfileString("TTSSH", "DefaultUserType", buf, fileName);
571 maya 3227 WritePrivateProfileString("TTSSH", "DefaultUserName",
572     settings->DefaultUserName, fileName);
573    
574     if (copy_forward) {
575     WritePrivateProfileString("TTSSH", "DefaultForwarding",
576 maya 5960 settings->DefaultForwarding, fileName);
577 maya 3227 }
578    
579     WritePrivateProfileString("TTSSH", "CipherOrder",
580     settings->CipherOrder, fileName);
581    
582 yutakapon 4367 WritePrivateProfileString("TTSSH", "KexOrder",
583     settings->KexOrder, fileName);
584    
585     WritePrivateProfileString("TTSSH", "HostKeyOrder",
586     settings->HostKeyOrder, fileName);
587    
588     WritePrivateProfileString("TTSSH", "MacOrder",
589     settings->MacOrder, fileName);
590    
591     WritePrivateProfileString("TTSSH", "CompOrder",
592     settings->CompOrder, fileName);
593    
594 maya 3227 WritePrivateProfileString("TTSSH", "KnownHostsFiles",
595     settings->KnownHostsFiles, fileName);
596    
597     WritePrivateProfileString("TTSSH", "DefaultRhostsLocalUserName",
598     settings->DefaultRhostsLocalUserName,
599     fileName);
600    
601     WritePrivateProfileString("TTSSH", "DefaultRhostsHostPrivateKeyFile",
602     settings->DefaultRhostsHostPrivateKeyFile,
603     fileName);
604    
605     WritePrivateProfileString("TTSSH", "DefaultRSAPrivateKeyFile",
606     settings->DefaultRSAPrivateKeyFile,
607     fileName);
608    
609     _itoa(settings->DefaultAuthMethod, buf, 10);
610     WritePrivateProfileString("TTSSH", "DefaultAuthMethod", buf, fileName);
611    
612     _itoa(settings->LogLevel, buf, 10);
613     WritePrivateProfileString("TTSSH", "LogLevel", buf, fileName);
614    
615     _itoa(settings->WriteBufferSize, buf, 10);
616     WritePrivateProfileString("TTSSH", "WriteBufferSize", buf, fileName);
617    
618     // SSH protocol version (2004.10.11 yutaka)
619     WritePrivateProfileString("TTSSH", "ProtocolVersion",
620     settings->ssh_protocol_version==2 ? "2" : "1",
621     fileName);
622    
623     // SSH heartbeat time(second) (2004.12.11 yutaka)
624     _snprintf_s(buf, sizeof(buf), _TRUNCATE,
625     "%d", settings->ssh_heartbeat_overtime);
626     WritePrivateProfileString("TTSSH", "HeartBeat", buf, fileName);
627    
628     // Remember password (2006.8.5 yutaka)
629     WritePrivateProfileString("TTSSH", "RememberPassword",
630     settings->remember_password ? "1" : "0",
631     fileName);
632    
633     // �������F���_�C�A���O���T�|�[�g�������������\�b�h���`�F�b�N���A
634     // ���������\�b�h���O���C�A�E�g���� (2007.9.24 maya)
635     WritePrivateProfileString("TTSSH", "CheckAuthListFirst",
636     settings->CheckAuthListFirst ? "1" : "0", fileName);
637    
638     // 768bit ������ RSA ���������T�[�o�����������L�������� (2008.9.11 maya)
639     WritePrivateProfileString("TTSSH", "EnableRsaShortKeyServer",
640     settings->EnableRsaShortKeyServer ? "1" : "0", fileName);
641    
642     // agent forward ���L�������� (2008.11.25 maya)
643     WritePrivateProfileString("TTSSH", "ForwardAgent",
644     settings->ForwardAgent ? "1" : "0", fileName);
645 maya 4229
646     // agent forward �m�F���L��������
647     WritePrivateProfileString("TTSSH", "ForwardAgentConfirm",
648     settings->ForwardAgentConfirm ? "1" : "0", fileName);
649 doda 4531
650 doda 6663 // agent forward ���m���L��������
651     WritePrivateProfileString("TTSSH", "ForwardAgentNotify",
652     settings->ForwardAgentNotify ? "1" : "0", fileName);
653    
654 doda 4531 // �z�X�g���� DNS �����`�F�b�N (RFC 4255)
655     WritePrivateProfileString("TTSSH", "VerifyHostKeyDNS",
656     settings->VerifyHostKeyDNS ? "1" : "0", fileName);
657 doda 5294
658     // SSH �A�C�R��
659 maya 6386 if (settings->IconID==IDI_SECURETT_YELLOW) {
660     WritePrivateProfileString("TTSSH", "SSHIcon", "yellow", fileName);
661     }
662     else if (settings->IconID==IDI_SECURETT_GREEN) {
663     WritePrivateProfileString("TTSSH", "SSHIcon", "green", fileName);
664     }
665     else {
666     WritePrivateProfileString("TTSSH", "SSHIcon", "Default", fileName);
667     }
668 yutakapon 5620
669     _itoa(settings->DisablePopupMessage, buf, 10);
670     WritePrivateProfileString("TTSSH", "DisablePopupMessage", buf, fileName);
671 doda 5793
672     WritePrivateProfileString("TTSSH", "X11Display", settings->X11Display, fileName);
673 yutakapon 5839
674 yutakapon 5849 _snprintf_s(buf, sizeof(buf), _TRUNCATE,
675     "%d", settings->UpdateHostkeys);
676     WritePrivateProfileString("TTSSH", "UpdateHostkeys", buf, fileName);
677 doda 5900
678     _itoa_s(settings->GexMinimalGroupSize, buf, sizeof(buf), 10);
679     WritePrivateProfileString("TTSSH", "GexMinimalGroupSize", buf, fileName);
680 doda 7048
681     _itoa_s(settings->AuthBanner, buf, sizeof(buf), 10);
682     WritePrivateProfileString("TTSSH", "AuthBanner", buf, fileName);
683 maya 3227 }
684    
685    
686     /* find free port in all protocol family */
687     static unsigned short find_local_port(PTInstVar pvar)
688     {
689     int tries;
690     SOCKET connecter;
691     struct addrinfo hints;
692 doda 6801 struct addrinfo *res;
693     struct addrinfo *res0;
694 maya 3227 unsigned short port;
695     char pname[NI_MAXHOST];
696    
697     if (pvar->session_settings.DefaultAuthMethod != SSH_AUTH_RHOSTS) {
698     return 0;
699     }
700    
701     /* The random numbers here are only used to try to get fresh
702     ports across runs (dangling ports can cause bind errors
703     if we're unlucky). They do not need to be (and are not)
704     cryptographically strong.
705     */
706     srand((unsigned) GetTickCount());
707    
708     for (tries = 20; tries > 0; tries--) {
709     memset(&hints, 0, sizeof(hints));
710     hints.ai_family = pvar->ts->ProtocolFamily;
711     hints.ai_flags = AI_PASSIVE;
712     hints.ai_socktype = SOCK_STREAM;
713     port = (unsigned) rand() % 512 + 512;
714     _snprintf_s(pname, sizeof(pname), _TRUNCATE, "%d", (int) port);
715     if (getaddrinfo(NULL, pname, &hints, &res0)) {
716     return 0;
717     /* NOT REACHED */
718     }
719    
720     for (res = res0; res; res = res->ai_next) {
721     if (res->ai_family == AF_INET || res->ai_family == AF_INET6)
722     continue;
723    
724     connecter =
725     socket(res->ai_family, res->ai_socktype, res->ai_protocol);
726     if (connecter == INVALID_SOCKET) {
727     freeaddrinfo(res0);
728     return 0;
729     }
730    
731     if (bind(connecter, res->ai_addr, res->ai_addrlen) !=
732     SOCKET_ERROR) {
733     return port;
734     freeaddrinfo(res0);
735     closesocket(connecter);
736     } else if (WSAGetLastError() != WSAEADDRINUSE) {
737     closesocket(connecter);
738     freeaddrinfo(res0);
739     return 0;
740     }
741    
742     closesocket(connecter);
743     }
744     freeaddrinfo(res0);
745     }
746    
747     return 0;
748     }
749    
750 doda 6801 static int PASCAL TTXconnect(SOCKET s,
751     const struct sockaddr *name,
752 maya 3227 int namelen)
753     {
754 maya 4584 if (pvar->socket == INVALID_SOCKET || pvar->socket != s) {
755 maya 3227 struct sockaddr_storage ss;
756     int len;
757    
758     pvar->socket = s;
759    
760     memset(&ss, 0, sizeof(ss));
761     switch (pvar->ts->ProtocolFamily) {
762     case AF_INET:
763     len = sizeof(struct sockaddr_in);
764 doda 6801 ((struct sockaddr_in *) &ss)->sin_family = AF_INET;
765     ((struct sockaddr_in *) &ss)->sin_addr.s_addr = INADDR_ANY;
766     ((struct sockaddr_in *) &ss)->sin_port =
767 maya 3227 htons(find_local_port(pvar));
768     break;
769     case AF_INET6:
770     len = sizeof(struct sockaddr_in6);
771 doda 6801 ((struct sockaddr_in6 *) &ss)->sin6_family = AF_INET6;
772 zmatsuo 7197 ((struct sockaddr_in6 *) &ss)->sin6_addr = in6addr_any;
773 doda 6801 ((struct sockaddr_in6 *) &ss)->sin6_port =
774 maya 3227 htons(find_local_port(pvar));
775     break;
776     default:
777 maya 4586 /* UNSPEC */
778 zmatsuo 7197 len = sizeof(ss);
779     ss.ss_family = AF_UNSPEC;
780 maya 3227 break;
781     }
782    
783 doda 6801 bind(s, (struct sockaddr *) &ss, len);
784 maya 3227 }
785    
786     return (pvar->Pconnect) (s, name, namelen);
787     }
788    
789 doda 6801 static int PASCAL TTXWSAAsyncSelect(SOCKET s, HWND hWnd, u_int wMsg,
790 maya 3227 long lEvent)
791     {
792     if (s == pvar->socket) {
793     pvar->notification_events = lEvent;
794     pvar->notification_msg = wMsg;
795    
796     if (pvar->NotificationWindow == NULL) {
797     pvar->NotificationWindow = hWnd;
798 yutakapon 8086 // AUTH_advance_to_next_cred()�������o�������������B
799     // NotificationWindow���n���h�������������������A�����^�C�~���O����
800     // �F���_�C�A���O���o���������������BProxy��NAT�o�R���T�[�o������
801     // �������������A���������f�����������������A�F���_�C�A���O��
802     // �\�������������������������B
803 maya 3227 }
804     }
805    
806     return (pvar->PWSAAsyncSelect) (s, hWnd, wMsg, lEvent);
807     }
808    
809 doda 6801 static int PASCAL TTXrecv(SOCKET s, char *buf, int len, int flags)
810 maya 3227 {
811     if (s == pvar->socket) {
812     int ret;
813    
814     ssh_heartbeat_lock();
815     ret = PKT_recv(pvar, buf, len);
816     ssh_heartbeat_unlock();
817     return (ret);
818    
819     } else {
820     return (pvar->Precv) (s, buf, len, flags);
821     }
822     }
823    
824 doda 6801 static int PASCAL TTXsend(SOCKET s, char const *buf, int len,
825 maya 3227 int flags)
826     {
827     if (s == pvar->socket) {
828     ssh_heartbeat_lock();
829     SSH_send(pvar, buf, len);
830     ssh_heartbeat_unlock();
831     return len;
832     } else {
833     return (pvar->Psend) (s, buf, len, flags);
834     }
835     }
836    
837     void notify_established_secure_connection(PTInstVar pvar)
838     {
839     int fuLoad = LR_DEFAULTCOLOR;
840    
841 yutakapon 6286 if (IsWindowsNT4()) {
842 maya 3227 fuLoad = LR_VGACOLOR;
843     }
844    
845     // LoadIcon �������� LoadImage ���g�����������A
846     // 16x16 ���A�C�R���������I�������������������� (2006.8.9 maya)
847     if (SecureLargeIcon == NULL) {
848 doda 5261 SecureLargeIcon = LoadImage(hInst, MAKEINTRESOURCE(pvar->settings.IconID),
849 maya 3227 IMAGE_ICON, 0, 0, fuLoad);
850     }
851     if (SecureSmallIcon == NULL) {
852 doda 5261 SecureSmallIcon = LoadImage(hInst, MAKEINTRESOURCE(pvar->settings.IconID),
853 maya 3227 IMAGE_ICON, 16, 16, fuLoad);
854     }
855    
856     if (SecureLargeIcon != NULL && SecureSmallIcon != NULL) {
857     pvar->OldLargeIcon =
858 maya 3454 (HICON) SendMessage(pvar->NotificationWindow, WM_GETICON,
859     ICON_BIG, 0);
860 maya 3227 pvar->OldSmallIcon =
861     (HICON) SendMessage(pvar->NotificationWindow, WM_GETICON,
862     ICON_SMALL, 0);
863    
864     PostMessage(pvar->NotificationWindow, WM_SETICON, ICON_BIG,
865     (LPARAM) SecureLargeIcon);
866     PostMessage(pvar->NotificationWindow, WM_SETICON, ICON_SMALL,
867     (LPARAM) SecureSmallIcon);
868     }
869    
870 doda 6691 if (IsWindows2000()) {
871     if (SecureNotifyIcon == NULL) {
872     SecureNotifyIcon = LoadImage(hInst, MAKEINTRESOURCE(pvar->settings.IconID),
873     IMAGE_ICON, 0, 0, LR_VGACOLOR | LR_SHARED);
874     }
875     OldNotifyIcon = GetCustomNotifyIcon();
876     SetCustomNotifyIcon(SecureNotifyIcon);
877     }
878    
879 doda 6809 logputs(LOG_LEVEL_VERBOSE, "Entering secure mode");
880 maya 3227 }
881    
882 doda 6801 void notify_closed_connection(PTInstVar pvar, char *send_msg)
883 maya 3227 {
884 maya 5678 SSH_notify_disconnecting(pvar, send_msg);
885 maya 3227 AUTH_notify_disconnecting(pvar);
886     HOSTS_notify_disconnecting(pvar);
887    
888     PostMessage(pvar->NotificationWindow, WM_USER_COMMNOTIFY,
889     pvar->socket, MAKELPARAM(FD_CLOSE, 0));
890     }
891    
892 yutakapon 8087 // non-fatal������fatal�����G���[���b�Z�[�W���o���������B
893     // ���x�A�o�������b�Z�[�W���������A���s���������������������B
894 doda 6801 static void add_err_msg(PTInstVar pvar, char *msg)
895 maya 3227 {
896     if (pvar->err_msg != NULL) {
897 yutakapon 8087 int buf_len;
898     char *buf;
899 maya 3227
900 yutakapon 8087 // �������������b�Z�[�W���o�^�����������������������B
901     if (strstr(pvar->err_msg, msg))
902     return;
903    
904     buf_len = strlen(pvar->err_msg) + 3 + strlen(msg);
905     buf = malloc(buf_len);
906     // ���������m���������������������������B
907     if (buf == NULL)
908     return;
909    
910 maya 3227 strncpy_s(buf, buf_len, pvar->err_msg, _TRUNCATE);
911     strncat_s(buf, buf_len, "\n\n", _TRUNCATE);
912     strncat_s(buf, buf_len, msg, _TRUNCATE);
913     free(pvar->err_msg);
914     pvar->err_msg = buf;
915     } else {
916 yutakapon 8087 // ���������m�����������������A_strdup()��NULL�������B
917 maya 3227 pvar->err_msg = _strdup(msg);
918     }
919     }
920    
921 doda 6801 void notify_nonfatal_error(PTInstVar pvar, char *msg)
922 maya 3227 {
923     if (!pvar->showing_err) {
924     // �������������������m���E�B���h�E�����������A�f�X�N�g�b�v���I�[�i�[������
925     // ���b�Z�[�W�{�b�N�X���o���������B(2006.6.11 yutaka)
926     if (pvar->NotificationWindow == NULL) {
927 doda 6688 UTIL_get_lang_msg("MSG_NONFATAL_ERROR", pvar,
928 maya 3227 "Tera Term: not fatal error");
929     MessageBox(NULL, msg, pvar->ts->UIMsg, MB_OK|MB_ICONINFORMATION);
930     msg[0] = '\0';
931    
932     } else {
933     PostMessage(pvar->NotificationWindow, WM_COMMAND,
934     ID_SSHASYNCMESSAGEBOX, 0);
935     }
936     }
937     if (msg[0] != 0) {
938 doda 6809 logputs(LOG_LEVEL_ERROR, msg);
939 maya 3227 add_err_msg(pvar, msg);
940     }
941     }
942    
943 doda 6801 void notify_fatal_error(PTInstVar pvar, char *msg, BOOL send_disconnect)
944 maya 3227 {
945     if (msg[0] != 0) {
946 doda 6809 logputs(LOG_LEVEL_FATAL, msg);
947 maya 3227 add_err_msg(pvar, msg);
948     }
949    
950     if (!pvar->fatal_error) {
951     pvar->fatal_error = TRUE;
952    
953 maya 5678 if (send_disconnect) {
954     SSH_notify_disconnecting(pvar, msg);
955     }
956 maya 3227 AUTH_notify_disconnecting(pvar);
957     HOSTS_notify_disconnecting(pvar);
958    
959     PostMessage(pvar->NotificationWindow, WM_USER_COMMNOTIFY,
960     pvar->socket, MAKELPARAM(FD_CLOSE,
961     (pvar->PWSAGetLastError) ()));
962     }
963     }
964    
965 doda 6809 void logputs(int level, char *msg)
966 maya 3227 {
967 doda 6823 if (level <= pvar->settings.LogLevel) {
968 doda 7016 char buf[4096];
969 maya 3227 int file;
970    
971     get_teraterm_dir_relative_name(buf, NUM_ELEM(buf), "TTSSH.LOG");
972     file = _open(buf, _O_RDWR | _O_APPEND | _O_CREAT | _O_TEXT,
973     _S_IREAD | _S_IWRITE);
974    
975     if (file >= 0) {
976 doda 6778 char *strtime = mctimelocal("%Y-%m-%d %H:%M:%S.%NZ", TRUE);
977 maya 4546 DWORD processid;
978 maya 3227 char tmp[26];
979 maya 4546
980     _write(file, strtime, strlen(strtime));
981 maya 3227 GetWindowThreadProcessId(pvar->cv->HWin, &processid);
982     _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, " [%lu] ",processid);
983     _write(file, tmp, strlen(tmp));
984     _write(file, msg, strlen(msg));
985     _write(file, "\n", 1);
986     _close(file);
987     }
988     }
989     }
990    
991 zmatsuo 7648 #if defined(_MSC_VER)
992     void logprintf(int level, _Printf_format_string_ const char *fmt, ...)
993     #else
994     void logprintf(int level, const char *fmt, ...)
995     #endif
996 doda 6051 {
997     char buff[4096];
998     va_list params;
999    
1000 doda 6823 if (level <= pvar->settings.LogLevel) {
1001 doda 6051 va_start(params, fmt);
1002     vsnprintf_s(buff, sizeof(buff), _TRUNCATE, fmt, params);
1003     va_end(params);
1004    
1005 doda 6809 logputs(level, buff);
1006 doda 6051 }
1007     }
1008    
1009 yutakapon 6302 static void format_line_hexdump(char *buf, int buflen, int addr, int *bytes, int byte_cnt)
1010     {
1011     int i, c;
1012     char tmp[128];
1013    
1014     buf[0] = 0;
1015    
1016     /* �������A�h���X�\�� */
1017     _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, "%08X : ", addr);
1018     strncat_s(buf, buflen, tmp, _TRUNCATE);
1019    
1020     /* �o�C�i���\���i4�o�C�g�������������}���j*/
1021     for (i = 0; i < byte_cnt; i++) {
1022     if (i > 0 && i % 4 == 0) {
1023     strncat_s(buf, buflen, " ", _TRUNCATE);
1024     }
1025    
1026     _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, "%02X", bytes[i]);
1027     strncat_s(buf, buflen, tmp, _TRUNCATE);
1028     }
1029    
1030     /* ASCII�\���������������������� */
1031     _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, " %*s%*s", (16 - byte_cnt) * 2 + 1, " ", (16 - byte_cnt + 3) / 4, " ");
1032     strncat_s(buf, buflen, tmp, _TRUNCATE);
1033    
1034     /* ASCII�\�� */
1035     for (i = 0; i < byte_cnt; i++) {
1036     c = bytes[i];
1037     if (isprint(c)) {
1038     _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, "%c", c);
1039     strncat_s(buf, buflen, tmp, _TRUNCATE);
1040     }
1041     else {
1042     strncat_s(buf, buflen, ".", _TRUNCATE);
1043     }
1044     }
1045    
1046     //strncat_s(buf, buflen, "\n", _TRUNCATE);
1047     }
1048    
1049 zmatsuo 7648 #if defined(_MSC_VER)
1050     void logprintf_hexdump(int level, const char *data, int len, _Printf_format_string_ const char *fmt, ...)
1051     #else
1052     void logprintf_hexdump(int level, const char *data, int len, const char *fmt, ...)
1053     #endif
1054 yutakapon 6302 {
1055     char buff[4096];
1056     va_list params;
1057     int c, addr;
1058     int bytes[16], *ptr;
1059     int byte_cnt;
1060     int i;
1061    
1062 doda 6823 if (level <= pvar->settings.LogLevel) {
1063 yutakapon 6302 va_start(params, fmt);
1064     vsnprintf_s(buff, sizeof(buff), _TRUNCATE, fmt, params);
1065     va_end(params);
1066    
1067 doda 6809 logputs(level, buff);
1068 yutakapon 6302
1069     addr = 0;
1070     byte_cnt = 0;
1071     ptr = bytes;
1072     for (i = 0; i < len; i++) {
1073     c = data[i];
1074     *ptr++ = c & 0xff;
1075     byte_cnt++;
1076    
1077     if (byte_cnt == 16) {
1078     format_line_hexdump(buff, sizeof(buff), addr, bytes, byte_cnt);
1079 doda 6809 logputs(level, buff);
1080 yutakapon 6302
1081     addr += 16;
1082     byte_cnt = 0;
1083     ptr = bytes;
1084     }
1085     }
1086    
1087     if (byte_cnt > 0) {
1088     format_line_hexdump(buff, sizeof(buff), addr, bytes, byte_cnt);
1089 doda 6809 logputs(level, buff);
1090 yutakapon 6302 }
1091     }
1092     }
1093    
1094 doda 6801 static void PASCAL TTXOpenTCP(TTXSockHooks *hooks)
1095 maya 3227 {
1096     if (pvar->settings.Enabled) {
1097     // TCPLocalEcho/TCPCRSend ������������ (maya 2007.4.25)
1098 doda 3579 pvar->origDisableTCPEchoCR = pvar->ts->DisableTCPEchoCR;
1099 maya 3227 pvar->ts->DisableTCPEchoCR = TRUE;
1100    
1101     pvar->session_settings = pvar->settings;
1102    
1103 doda 6809 logputs(LOG_LEVEL_VERBOSE, "---------------------------------------------------------------------");
1104     logputs(LOG_LEVEL_VERBOSE, "Initiating SSH session");
1105 maya 3227
1106     FWDUI_load_settings(pvar);
1107    
1108     pvar->cv->TelAutoDetect = FALSE;
1109     /* This next line should not be needed because Tera Term's
1110     CommLib should find ts->Telnet == 0 ... but we'll do this
1111     just to be on the safe side. */
1112     pvar->cv->TelFlag = FALSE;
1113 doda 3495 pvar->cv->TelLineMode = FALSE;
1114 maya 3227
1115     pvar->Precv = *hooks->Precv;
1116     pvar->Psend = *hooks->Psend;
1117     pvar->PWSAAsyncSelect = *hooks->PWSAAsyncSelect;
1118     pvar->Pconnect = *hooks->Pconnect;
1119     pvar->PWSAGetLastError = *hooks->PWSAGetLastError;
1120    
1121     *hooks->Precv = TTXrecv;
1122     *hooks->Psend = TTXsend;
1123     *hooks->PWSAAsyncSelect = TTXWSAAsyncSelect;
1124     *hooks->Pconnect = TTXconnect;
1125    
1126     SSH_open(pvar);
1127     HOSTS_open(pvar);
1128     FWDUI_open(pvar);
1129    
1130     // ������ myproposal �����f���������A�������O�����������B (2006.6.26 maya)
1131     SSH2_update_cipher_myproposal(pvar);
1132 yutakapon 4367 SSH2_update_kex_myproposal(pvar);
1133     SSH2_update_host_key_myproposal(pvar);
1134     SSH2_update_hmac_myproposal(pvar);
1135 maya 3227 SSH2_update_compression_myproposal(pvar);
1136     }
1137     }
1138    
1139 doda 6801 static void PASCAL TTXCloseTCP(TTXSockHooks *hooks)
1140 maya 3227 {
1141     if (pvar->session_settings.Enabled) {
1142     pvar->socket = INVALID_SOCKET;
1143    
1144 doda 6809 logputs(LOG_LEVEL_VERBOSE, "Terminating SSH session...");
1145 maya 3227
1146     *hooks->Precv = pvar->Precv;
1147     *hooks->Psend = pvar->Psend;
1148     *hooks->PWSAAsyncSelect = pvar->PWSAAsyncSelect;
1149     *hooks->Pconnect = pvar->Pconnect;
1150 doda 3579
1151     pvar->ts->DisableTCPEchoCR = pvar->origDisableTCPEchoCR;
1152 maya 3227 }
1153    
1154     uninit_TTSSH(pvar);
1155     init_TTSSH(pvar);
1156     }
1157    
1158     static void enable_dlg_items(HWND dlg, int from, int to, BOOL enabled)
1159     {
1160     for (; from <= to; from++) {
1161     EnableWindow(GetDlgItem(dlg, from), enabled);
1162     }
1163     }
1164    
1165     // C-p/C-n/C-b/C-f/C-a/C-e ���T�|�[�g (2007.9.5 maya)
1166     // C-d/C-k ���T�|�[�g (2007.10.3 yutaka)
1167     // �h���b�v�_�E���������G�f�B�b�g�R���g���[����
1168     // �T�u�N���X�������������E�C���h�E�v���V�[�W��
1169     WNDPROC OrigHostnameEditProc; // Original window procedure
1170     LRESULT CALLBACK HostnameEditProc(HWND dlg, UINT msg,
1171 maya 5868 WPARAM wParam, LPARAM lParam)
1172 maya 3227 {
1173     HWND parent;
1174     int max, select, len;
1175     char *str, *orgstr;
1176    
1177     switch (msg) {
1178     // �L�[�����������������m����
1179     case WM_KEYDOWN:
1180     if (GetKeyState(VK_CONTROL) < 0) {
1181     switch (wParam) {
1182     case 0x50: // Ctrl+p ... up
1183     parent = GetParent(dlg);
1184     select = SendMessage(parent, CB_GETCURSEL, 0, 0);
1185     if (select > 0) {
1186     PostMessage(parent, CB_SETCURSEL, select - 1, 0);
1187     }
1188     return 0;
1189     case 0x4e: // Ctrl+n ... down
1190     parent = GetParent(dlg);
1191     max = SendMessage(parent, CB_GETCOUNT, 0, 0);
1192     select = SendMessage(parent, CB_GETCURSEL, 0, 0);
1193     if (select < max - 1) {
1194     PostMessage(parent, CB_SETCURSEL, select + 1, 0);
1195     }
1196     return 0;
1197     case 0x42: // Ctrl+b ... left
1198     SendMessage(dlg, EM_GETSEL, 0, (LPARAM)&select);
1199     PostMessage(dlg, EM_SETSEL, select-1, select-1);
1200     return 0;
1201     case 0x46: // Ctrl+f ... right
1202     SendMessage(dlg, EM_GETSEL, 0, (LPARAM)&select);
1203     max = GetWindowTextLength(dlg) ;
1204     PostMessage(dlg, EM_SETSEL, select+1, select+1);
1205     return 0;
1206     case 0x41: // Ctrl+a ... home
1207     PostMessage(dlg, EM_SETSEL, 0, 0);
1208     return 0;
1209     case 0x45: // Ctrl+e ... end
1210     max = GetWindowTextLength(dlg) ;
1211     PostMessage(dlg, EM_SETSEL, max, max);
1212     return 0;
1213    
1214     case 0x44: // Ctrl+d
1215     case 0x4b: // Ctrl+k
1216     case 0x55: // Ctrl+u
1217     SendMessage(dlg, EM_GETSEL, 0, (LPARAM)&select);
1218     max = GetWindowTextLength(dlg);
1219     max++; // '\0'
1220     orgstr = str = malloc(max);
1221     if (str != NULL) {
1222     len = GetWindowText(dlg, str, max);
1223     if (select >= 0 && select < len) {
1224     if (wParam == 0x44) { // �J�[�\���z����������������������
1225     memmove(&str[select], &str[select + 1], len - select - 1);
1226     str[len - 1] = '\0';
1227    
1228     } else if (wParam == 0x4b) { // �J�[�\�������s��������������
1229     str[select] = '\0';
1230    
1231     }
1232     }
1233    
1234     if (wParam == 0x55) { // �J�[�\����������������������
1235     if (select >= len) {
1236     str[0] = '\0';
1237     } else {
1238     str = &str[select];
1239     }
1240     select = 0;
1241     }
1242    
1243     SetWindowText(dlg, str);
1244     SendMessage(dlg, EM_SETSEL, select, select);
1245     free(orgstr);
1246     return 0;
1247     }
1248     break;
1249     }
1250     }
1251     break;
1252    
1253     // �����L�[��������������������������������������������
1254     case WM_CHAR:
1255     switch (wParam) {
1256     case 0x01:
1257     case 0x02:
1258     case 0x04:
1259     case 0x05:
1260     case 0x06:
1261     case 0x0b:
1262     case 0x0e:
1263     case 0x10:
1264     case 0x15:
1265     return 0;
1266     }
1267     }
1268    
1269     return CallWindowProc(OrigHostnameEditProc, dlg, msg, wParam, lParam);
1270     }
1271    
1272 zmatsuo 7896 static INT_PTR CALLBACK TTXHostDlg(HWND dlg, UINT msg, WPARAM wParam,
1273     LPARAM lParam)
1274 maya 3227 {
1275     static char *ssh_version[] = {"SSH1", "SSH2", NULL};
1276     PGetHNRec GetHNRec;
1277     char EntName[128];
1278     char TempHost[HostNameMaxLength + 1];
1279     WORD i, j, w;
1280     WORD ComPortTable[MAXCOMPORT];
1281     static char *ComPortDesc[MAXCOMPORT];
1282     int comports;
1283     BOOL Ok;
1284     char uimsg[MAX_UIMSG];
1285     static HWND hwndHostname = NULL; // HOSTNAME dropdown
1286     static HWND hwndHostnameEdit = NULL; // Edit control on HOSTNAME dropdown
1287    
1288     switch (msg) {
1289     case WM_INITDIALOG:
1290     GetHNRec = (PGetHNRec) lParam;
1291 zmatsuo 7896 SetWindowLongPtr(dlg, DWLP_USER, lParam);
1292 maya 3227
1293     GetWindowText(dlg, uimsg, sizeof(uimsg));
1294     UTIL_get_lang_msg("DLG_HOST_TITLE", pvar, uimsg);
1295     SetWindowText(dlg, pvar->ts->UIMsg);
1296     GetDlgItemText(dlg, IDC_HOSTNAMELABEL, uimsg, sizeof(uimsg));
1297     UTIL_get_lang_msg("DLG_HOST_TCPIPHOST", pvar, uimsg);
1298     SetDlgItemText(dlg, IDC_HOSTNAMELABEL, pvar->ts->UIMsg);
1299     GetDlgItemText(dlg, IDC_HISTORY, uimsg, sizeof(uimsg));
1300     UTIL_get_lang_msg("DLG_HOST_TCPIPHISTORY", pvar, uimsg);
1301     SetDlgItemText(dlg, IDC_HISTORY, pvar->ts->UIMsg);
1302     GetDlgItemText(dlg, IDC_SERVICELABEL, uimsg, sizeof(uimsg));
1303     UTIL_get_lang_msg("DLG_HOST_TCPIPSERVICE", pvar, uimsg);
1304     SetDlgItemText(dlg, IDC_SERVICELABEL, pvar->ts->UIMsg);
1305     GetDlgItemText(dlg, IDC_HOSTOTHER, uimsg, sizeof(uimsg));
1306     UTIL_get_lang_msg("DLG_HOST_TCPIPOTHER", pvar, uimsg);
1307     SetDlgItemText(dlg, IDC_HOSTOTHER, pvar->ts->UIMsg);
1308     GetDlgItemText(dlg, IDC_HOSTTCPPORTLABEL, uimsg, sizeof(uimsg));
1309     UTIL_get_lang_msg("DLG_HOST_TCPIPPORT", pvar, uimsg);
1310     SetDlgItemText(dlg, IDC_HOSTTCPPORTLABEL, pvar->ts->UIMsg);
1311     GetDlgItemText(dlg, IDC_SSH_VERSION_LABEL, uimsg, sizeof(uimsg));
1312     UTIL_get_lang_msg("DLG_HOST_TCPIPSSHVERSION", pvar, uimsg);
1313     SetDlgItemText(dlg, IDC_SSH_VERSION_LABEL, pvar->ts->UIMsg);
1314     GetDlgItemText(dlg, IDC_HOSTTCPPROTOCOLLABEL, uimsg, sizeof(uimsg));
1315     UTIL_get_lang_msg("DLG_HOST_TCPIPPROTOCOL", pvar, uimsg);
1316     SetDlgItemText(dlg, IDC_HOSTTCPPROTOCOLLABEL, pvar->ts->UIMsg);
1317     GetDlgItemText(dlg, IDC_HOSTSERIAL, uimsg, sizeof(uimsg));
1318     UTIL_get_lang_msg("DLG_HOST_SERIAL", pvar, uimsg);
1319     SetDlgItemText(dlg, IDC_HOSTSERIAL, pvar->ts->UIMsg);
1320     GetDlgItemText(dlg, IDC_HOSTCOMLABEL, uimsg, sizeof(uimsg));
1321     UTIL_get_lang_msg("DLG_HOST_SERIALPORT", pvar, uimsg);
1322     SetDlgItemText(dlg, IDC_HOSTCOMLABEL, pvar->ts->UIMsg);
1323     GetDlgItemText(dlg, IDC_HOSTHELP, uimsg, sizeof(uimsg));
1324     UTIL_get_lang_msg("DLG_HOST_HELP", pvar, uimsg);
1325     SetDlgItemText(dlg, IDC_HOSTHELP, pvar->ts->UIMsg);
1326     GetDlgItemText(dlg, IDOK, uimsg, sizeof(uimsg));
1327     UTIL_get_lang_msg("BTN_OK", pvar, uimsg);
1328     SetDlgItemText(dlg, IDOK, pvar->ts->UIMsg);
1329     GetDlgItemText(dlg, IDCANCEL, uimsg, sizeof(uimsg));
1330     UTIL_get_lang_msg("BTN_CANCEL", pvar, uimsg);
1331     SetDlgItemText(dlg, IDCANCEL, pvar->ts->UIMsg);
1332    
1333     // �z�X�g�q�X�g�����`�F�b�N�{�b�N�X������ (2005.10.21 yutaka)
1334     if (pvar->ts->HistoryList > 0) {
1335     SendMessage(GetDlgItem(dlg, IDC_HISTORY), BM_SETCHECK, BST_CHECKED, 0);
1336     } else {
1337     SendMessage(GetDlgItem(dlg, IDC_HISTORY), BM_SETCHECK, BST_UNCHECKED, 0);
1338     }
1339    
1340 yutakapon 4860 // �t�@�C�����������O�t���p�C�v�������ATCP/IP�����������B
1341     if (GetHNRec->PortType == IdFile ||
1342     GetHNRec->PortType == IdNamedPipe
1343     )
1344 maya 3227 GetHNRec->PortType = IdTCPIP;
1345    
1346     strncpy_s(EntName, sizeof(EntName), "Host", _TRUNCATE);
1347    
1348     i = 1;
1349     do {
1350     _snprintf_s(&EntName[4], sizeof(EntName)-4, _TRUNCATE, "%d", i);
1351     GetPrivateProfileString("Hosts", EntName, "",
1352     TempHost, sizeof(TempHost),
1353     GetHNRec->SetupFN);
1354     if (strlen(TempHost) > 0)
1355     SendDlgItemMessage(dlg, IDC_HOSTNAME, CB_ADDSTRING,
1356     0, (LPARAM) TempHost);
1357     i++;
1358 maya 4963 } while (i <= MAXHOSTLIST);
1359 maya 3227
1360     SendDlgItemMessage(dlg, IDC_HOSTNAME, EM_LIMITTEXT,
1361     HostNameMaxLength - 1, 0);
1362    
1363     SendDlgItemMessage(dlg, IDC_HOSTNAME, CB_SETCURSEL, 0, 0);
1364    
1365     // C-n/C-p ���������T�u�N���X�� (2007.9.4 maya)
1366     hwndHostname = GetDlgItem(dlg, IDC_HOSTNAME);
1367     hwndHostnameEdit = GetWindow(hwndHostname, GW_CHILD);
1368 zmatsuo 7896 OrigHostnameEditProc = (WNDPROC)GetWindowLongPtr(hwndHostnameEdit, GWLP_WNDPROC);
1369     SetWindowLongPtr(hwndHostnameEdit, GWLP_WNDPROC, (LONG_PTR)HostnameEditProc);
1370 maya 3227
1371     CheckRadioButton(dlg, IDC_HOSTTELNET, IDC_HOSTOTHER,
1372     pvar->settings.Enabled ? IDC_HOSTSSH : GetHNRec->
1373     Telnet ? IDC_HOSTTELNET : IDC_HOSTOTHER);
1374     SendDlgItemMessage(dlg, IDC_HOSTTCPPORT, EM_LIMITTEXT, 5, 0);
1375     SetDlgItemInt(dlg, IDC_HOSTTCPPORT, GetHNRec->TCPPort, FALSE);
1376     for (i = 0; ProtocolFamilyList[i]; ++i) {
1377     SendDlgItemMessage(dlg, IDC_HOSTTCPPROTOCOL, CB_ADDSTRING,
1378     0, (LPARAM) ProtocolFamilyList[i]);
1379     }
1380     SendDlgItemMessage(dlg, IDC_HOSTTCPPROTOCOL, EM_LIMITTEXT,
1381     ProtocolFamilyMaxLength - 1, 0);
1382     SendDlgItemMessage(dlg, IDC_HOSTTCPPROTOCOL, CB_SETCURSEL, 0, 0);
1383    
1384     /////// SSH version
1385     for (i = 0; ssh_version[i]; ++i) {
1386     SendDlgItemMessage(dlg, IDC_SSH_VERSION, CB_ADDSTRING,
1387     0, (LPARAM) ssh_version[i]);
1388     }
1389     SendDlgItemMessage(dlg, IDC_SSH_VERSION, EM_LIMITTEXT,
1390     NUM_ELEM(ssh_version) - 1, 0);
1391    
1392     if (pvar->settings.ssh_protocol_version == 1) {
1393     SendDlgItemMessage(dlg, IDC_SSH_VERSION, CB_SETCURSEL, 0, 0); // SSH1
1394     } else {
1395     SendDlgItemMessage(dlg, IDC_SSH_VERSION, CB_SETCURSEL, 1, 0); // SSH2
1396     }
1397    
1398     if (IsDlgButtonChecked(dlg, IDC_HOSTSSH)) {
1399     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, TRUE); // enabled
1400     } else {
1401     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, FALSE); // disabled
1402     }
1403     /////// SSH version
1404    
1405    
1406     j = 0;
1407     w = 1;
1408     if ((comports=DetectComPorts(ComPortTable, GetHNRec->MaxComPort, ComPortDesc)) >= 0) {
1409     for (i=0; i<comports; i++) {
1410     // MaxComPort ���z�����|�[�g���\��������
1411     if (ComPortTable[i] > GetHNRec->MaxComPort) {
1412     continue;
1413     }
1414    
1415     // �g�p�����|�[�g���\��������
1416     if (CheckCOMFlag(ComPortTable[i]) == 1) {
1417     continue;
1418     }
1419    
1420     _snprintf_s(EntName, sizeof(EntName), _TRUNCATE, "COM%d", ComPortTable[i]);
1421     if (ComPortDesc[i] != NULL) {
1422     strncat_s(EntName, sizeof(EntName), ": ", _TRUNCATE);
1423     strncat_s(EntName, sizeof(EntName), ComPortDesc[i], _TRUNCATE);
1424     }
1425     SendDlgItemMessage(dlg, IDC_HOSTCOM, CB_ADDSTRING,
1426     0, (LPARAM)EntName);
1427     j++;
1428     if (GetHNRec->ComPort == ComPortTable[i])
1429     w = j;
1430     }
1431    
1432     } else {
1433     for (i = 1; i <= GetHNRec->MaxComPort; i++) {
1434     // �g�p�����|�[�g���\��������
1435     if (CheckCOMFlag(i) == 1) {
1436     continue;
1437     }
1438    
1439     _snprintf_s(EntName, sizeof(EntName), _TRUNCATE, "COM%d", i);
1440     SendDlgItemMessage(dlg, IDC_HOSTCOM, CB_ADDSTRING,
1441     0, (LPARAM) EntName);
1442     j++;
1443     if (GetHNRec->ComPort == i)
1444     w = j;
1445     }
1446     }
1447    
1448     if (j > 0)
1449     SendDlgItemMessage(dlg, IDC_HOSTCOM, CB_SETCURSEL, w - 1, 0);
1450     else { /* All com ports are already used */
1451     GetHNRec->PortType = IdTCPIP;
1452     enable_dlg_items(dlg, IDC_HOSTSERIAL, IDC_HOSTSERIAL, FALSE);
1453     }
1454    
1455     CheckRadioButton(dlg, IDC_HOSTTCPIP, IDC_HOSTSERIAL,
1456     IDC_HOSTTCPIP + GetHNRec->PortType - 1);
1457    
1458     if (GetHNRec->PortType == IdTCPIP) {
1459     enable_dlg_items(dlg, IDC_HOSTCOMLABEL, IDC_HOSTCOM, FALSE);
1460    
1461     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, TRUE);
1462     enable_dlg_items(dlg, IDC_SSH_VERSION_LABEL, IDC_SSH_VERSION_LABEL, TRUE);
1463    
1464     enable_dlg_items(dlg, IDC_HISTORY, IDC_HISTORY, TRUE); // enabled
1465     }
1466     else {
1467     enable_dlg_items(dlg, IDC_HOSTNAMELABEL, IDC_HOSTTCPPORT,
1468     FALSE);
1469     enable_dlg_items(dlg, IDC_HOSTTCPPROTOCOLLABEL,
1470     IDC_HOSTTCPPROTOCOL, FALSE);
1471    
1472     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, FALSE); // disabled
1473     enable_dlg_items(dlg, IDC_SSH_VERSION_LABEL, IDC_SSH_VERSION_LABEL, FALSE); // disabled (2004.11.23 yutaka)
1474    
1475     enable_dlg_items(dlg, IDC_HISTORY, IDC_HISTORY, FALSE); // disabled
1476     }
1477    
1478     // Host dialog���t�H�[�J�X�������� (2004.10.2 yutaka)
1479     if (GetHNRec->PortType == IdTCPIP) {
1480     HWND hwnd = GetDlgItem(dlg, IDC_HOSTNAME);
1481     SetFocus(hwnd);
1482     } else {
1483     HWND hwnd = GetDlgItem(dlg, IDC_HOSTCOM);
1484     SetFocus(hwnd);
1485     }
1486    
1487 zmatsuo 7592 CenterWindow(dlg, GetParent(dlg));
1488    
1489 maya 3227 // SetFocus()���t�H�[�J�X���������������AFALSE�������K�v�������B
1490     // TRUE���������ATABSTOP�������������������R���g���[�����I�������B
1491     // (2004.11.23 yutaka)
1492     return FALSE;
1493     //return TRUE;
1494    
1495     case WM_COMMAND:
1496     switch (LOWORD(wParam)) {
1497     case IDOK:
1498 zmatsuo 7896 GetHNRec = (PGetHNRec) GetWindowLongPtr(dlg, DWLP_USER);
1499 maya 3227 if (GetHNRec != NULL) {
1500     if (IsDlgButtonChecked(dlg, IDC_HOSTTCPIP)) {
1501     char afstr[BUFSIZ];
1502     i = GetDlgItemInt(dlg, IDC_HOSTTCPPORT, &Ok, FALSE);
1503     if (Ok) {
1504     GetHNRec->TCPPort = i;
1505     } else {
1506     UTIL_get_lang_msg("MSG_TCPPORT_NAN_ERROR", pvar,
1507     "The TCP port must be a number.");
1508     MessageBox(dlg, pvar->ts->UIMsg,
1509     "Tera Term", MB_OK | MB_ICONEXCLAMATION);
1510     return TRUE;
1511     }
1512     #define getaf(str) \
1513     ((strcmp((str), "IPv6") == 0) ? AF_INET6 : \
1514     ((strcmp((str), "IPv4") == 0) ? AF_INET : AF_UNSPEC))
1515     memset(afstr, 0, sizeof(afstr));
1516     GetDlgItemText(dlg, IDC_HOSTTCPPROTOCOL, afstr,
1517     sizeof(afstr));
1518     GetHNRec->ProtocolFamily = getaf(afstr);
1519     GetHNRec->PortType = IdTCPIP;
1520     GetDlgItemText(dlg, IDC_HOSTNAME, GetHNRec->HostName,
1521     HostNameMaxLength);
1522     pvar->hostdlg_activated = TRUE;
1523     pvar->hostdlg_Enabled = FALSE;
1524     if (IsDlgButtonChecked(dlg, IDC_HOSTTELNET)) {
1525     GetHNRec->Telnet = TRUE;
1526     } else if (IsDlgButtonChecked(dlg, IDC_HOSTSSH)) {
1527     pvar->hostdlg_Enabled = TRUE;
1528    
1529     // check SSH protocol version
1530     memset(afstr, 0, sizeof(afstr));
1531     GetDlgItemText(dlg, IDC_SSH_VERSION, afstr, sizeof(afstr));
1532     if (_stricmp(afstr, "SSH1") == 0) {
1533     pvar->settings.ssh_protocol_version = 1;
1534     } else {
1535     pvar->settings.ssh_protocol_version = 2;
1536     }
1537     }
1538 doda 3541 else { // IDC_HOSTOTHER
1539     GetHNRec->Telnet = FALSE;
1540     }
1541 maya 3227
1542     // host history check button
1543     if (SendMessage(GetDlgItem(dlg, IDC_HISTORY), BM_GETCHECK, 0, 0) == BST_CHECKED) {
1544     pvar->ts->HistoryList = 1;
1545     } else {
1546     pvar->ts->HistoryList = 0;
1547     }
1548    
1549     } else {
1550     GetHNRec->PortType = IdSerial;
1551     GetHNRec->HostName[0] = 0;
1552     memset(EntName, 0, sizeof(EntName));
1553     GetDlgItemText(dlg, IDC_HOSTCOM, EntName,
1554     sizeof(EntName) - 1);
1555     if (strncmp(EntName, "COM", 3) == 0 && EntName[3] != '\0') {
1556     GetHNRec->ComPort = atoi(&EntName[3]);
1557     if (GetHNRec->ComPort > GetHNRec->MaxComPort)
1558     GetHNRec->ComPort = 1;
1559     } else {
1560     GetHNRec->ComPort = 1;
1561     }
1562     }
1563     }
1564 zmatsuo 7896 SetWindowLongPtr(hwndHostnameEdit, GWLP_WNDPROC, (LONG_PTR)OrigHostnameEditProc);
1565 maya 3227 EndDialog(dlg, 1);
1566     return TRUE;
1567    
1568     case IDCANCEL:
1569 zmatsuo 7896 SetWindowLongPtr(hwndHostnameEdit, GWLP_WNDPROC, (LONG_PTR)OrigHostnameEditProc);
1570 maya 3227 EndDialog(dlg, 0);
1571     return TRUE;
1572    
1573     case IDC_HOSTTCPIP:
1574     enable_dlg_items(dlg, IDC_HOSTNAMELABEL, IDC_HOSTTCPPORT,
1575     TRUE);
1576     enable_dlg_items(dlg, IDC_HOSTTCPPROTOCOLLABEL,
1577     IDC_HOSTTCPPROTOCOL, TRUE);
1578     enable_dlg_items(dlg, IDC_HOSTCOMLABEL, IDC_HOSTCOM, FALSE);
1579    
1580     enable_dlg_items(dlg, IDC_SSH_VERSION_LABEL, IDC_SSH_VERSION_LABEL, TRUE); // disabled (2004.11.23 yutaka)
1581     if (IsDlgButtonChecked(dlg, IDC_HOSTSSH)) {
1582     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, TRUE);
1583     } else {
1584     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, FALSE); // disabled
1585     }
1586    
1587     enable_dlg_items(dlg, IDC_HISTORY, IDC_HISTORY, TRUE); // disabled
1588    
1589     return TRUE;
1590    
1591     case IDC_HOSTSERIAL:
1592     enable_dlg_items(dlg, IDC_HOSTCOMLABEL, IDC_HOSTCOM, TRUE);
1593     enable_dlg_items(dlg, IDC_HOSTNAMELABEL, IDC_HOSTTCPPORT,
1594     FALSE);
1595     enable_dlg_items(dlg, IDC_HOSTTCPPROTOCOLLABEL,
1596     IDC_HOSTTCPPROTOCOL, FALSE);
1597     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, FALSE); // disabled
1598     enable_dlg_items(dlg, IDC_SSH_VERSION_LABEL, IDC_SSH_VERSION_LABEL, FALSE); // disabled (2004.11.23 yutaka)
1599    
1600     enable_dlg_items(dlg, IDC_HISTORY, IDC_HISTORY, FALSE); // disabled
1601    
1602     return TRUE;
1603    
1604     case IDC_HOSTSSH:
1605     enable_dlg_items(dlg, IDC_SSH_VERSION,
1606     IDC_SSH_VERSION, TRUE);
1607     goto hostssh_enabled;
1608    
1609     case IDC_HOSTTELNET:
1610     case IDC_HOSTOTHER:
1611     enable_dlg_items(dlg, IDC_SSH_VERSION, IDC_SSH_VERSION, FALSE); // disabled
1612     hostssh_enabled:
1613    
1614 zmatsuo 7896 GetHNRec = (PGetHNRec) GetWindowLongPtr(dlg, DWLP_USER);
1615 maya 3227
1616     if (IsDlgButtonChecked(dlg, IDC_HOSTTELNET)) {
1617     if (GetHNRec != NULL)
1618     SetDlgItemInt(dlg, IDC_HOSTTCPPORT, GetHNRec->TelPort,
1619     FALSE);
1620     } else if (IsDlgButtonChecked(dlg, IDC_HOSTSSH)) {
1621     SetDlgItemInt(dlg, IDC_HOSTTCPPORT, 22, FALSE);
1622     }
1623     return TRUE;
1624    
1625     case IDC_HOSTCOM:
1626     if(HIWORD(wParam) == CBN_DROPDOWN) {
1627     HWND hostcom = GetDlgItem(dlg, IDC_HOSTCOM);
1628     int count = SendMessage(hostcom, CB_GETCOUNT, 0, 0);
1629     int i, len, max_len = 0;
1630     char *lbl;
1631     HDC TmpDC = GetDC(hostcom);
1632     SIZE s;
1633     for (i=0; i<count; i++) {
1634     len = SendMessage(hostcom, CB_GETLBTEXTLEN, i, 0);
1635     lbl = (char *)calloc(len+1, sizeof(char));
1636     SendMessage(hostcom, CB_GETLBTEXT, i, (LPARAM)lbl);
1637     GetTextExtentPoint32(TmpDC, lbl, len, &s);
1638     if (s.cx > max_len)
1639     max_len = s.cx;
1640     free(lbl);
1641     }
1642     SendMessage(hostcom, CB_SETDROPPEDWIDTH,
1643     max_len + GetSystemMetrics(SM_CXVSCROLL), 0);
1644     }
1645     break;
1646    
1647     case IDC_HOSTHELP:
1648     PostMessage(GetParent(dlg), WM_USER_DLGHELP2, 0, 0);
1649     }
1650     }
1651     return FALSE;
1652     }
1653    
1654 zmatsuo 7560 static void UTIL_SetDialogFont()
1655     {
1656 zmatsuo 7706 SetDialogFont(pvar->ts->DialogFontName, pvar->ts->DialogFontPoint, pvar->ts->DialogFontCharSet,
1657     pvar->ts->UILanguageFile, "TTSSH", "DLG_TAHOMA_FONT");
1658 zmatsuo 7560 }
1659    
1660 doda 6801 static BOOL PASCAL TTXGetHostName(HWND parent, PGetHNRec rec)
1661 maya 3227 {
1662 zmatsuo 7706 SetDialogFont(pvar->ts->DialogFontName, pvar->ts->DialogFontPoint, pvar->ts->DialogFontCharSet,
1663 zmatsuo 7589 pvar->ts->UILanguageFile, "TTSSH", "DLG_SYSTEM_FONT");
1664 maya 3227 return (BOOL) DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_HOSTDLG),
1665 zmatsuo 7560 parent, TTXHostDlg, (LPARAM)rec);
1666 maya 3227 }
1667    
1668 doda 6801 static void PASCAL TTXGetUIHooks(TTXUIHooks *hooks)
1669 maya 3227 {
1670     *hooks->GetHostName = TTXGetHostName;
1671     }
1672    
1673 doda 6801 static void PASCAL TTXReadINIFile(PCHAR fileName, PTTSet ts)
1674 maya 3227 {
1675     (pvar->ReadIniFile) (fileName, ts);
1676     read_ssh_options(pvar, fileName);
1677     pvar->settings = *pvar->ts_SSH;
1678 doda 6809 logputs(LOG_LEVEL_VERBOSE, "Reading INI file");
1679 maya 3227 FWDUI_load_settings(pvar);
1680     }
1681    
1682 doda 6801 static void PASCAL TTXWriteINIFile(PCHAR fileName, PTTSet ts)
1683 maya 3227 {
1684     (pvar->WriteIniFile) (fileName, ts);
1685     *pvar->ts_SSH = pvar->settings;
1686     clear_local_settings(pvar);
1687 doda 6809 logputs(LOG_LEVEL_VERBOSE, "Writing INI file");
1688 maya 3227 write_ssh_options(pvar, fileName, pvar->ts_SSH, TRUE);
1689     }
1690    
1691     static void read_ssh_options_from_user_file(PTInstVar pvar,
1692 doda 6801 char *user_file_name)
1693 maya 3227 {
1694     if (user_file_name[0] == '.') {
1695     read_ssh_options(pvar, user_file_name);
1696     } else {
1697     char buf[1024];
1698    
1699     get_teraterm_dir_relative_name(buf, sizeof(buf), user_file_name);
1700     read_ssh_options(pvar, buf);
1701     }
1702    
1703     pvar->settings = *pvar->ts_SSH;
1704     FWDUI_load_settings(pvar);
1705     }
1706    
1707     // Percent-encode������������src���f�R�[�h����dst���R�s�[�����B
1708     // dstlen��dst���T�C�Y�B�����������������������A�����������������������B
1709     static void percent_decode(char *dst, int dstlen, char *src) {
1710     if (src == NULL || dst == NULL || dstlen < 1) {
1711     return;
1712     }
1713    
1714     while (*src != 0 && dstlen > 1) {
1715     if (*src == '%' && isxdigit(*(src+1)) && isxdigit(*(src+2))) {
1716     src++; *dst = (isalpha(*src) ? (*src|0x20) - 'a' + 10 : *src - '0') << 4;
1717     src++; *dst |= (isalpha(*src) ? (*src|0x20) - 'a' + 10 : *src - '0');
1718     src++; dst++;
1719     }
1720     else {
1721     *dst++ = *src++;
1722     }
1723     dstlen--;
1724     }
1725     *dst = 0;
1726     return;
1727     }
1728    
1729 doda 5799 void add_forward_param(PTInstVar pvar, char *param)
1730     {
1731     if (pvar->settings.DefaultForwarding[0] == 0) {
1732     strncpy_s(pvar->settings.DefaultForwarding,
1733     sizeof(pvar->settings.DefaultForwarding),
1734     param, _TRUNCATE);
1735     } else {
1736     strncat_s(pvar->settings.DefaultForwarding,
1737     sizeof(pvar->settings.DefaultForwarding),
1738     ";", _TRUNCATE);
1739     strncat_s(pvar->settings.DefaultForwarding,
1740     sizeof(pvar->settings.DefaultForwarding),
1741     param, _TRUNCATE);
1742     }
1743     }
1744    
1745 doda 6801 static void PASCAL TTXParseParam(PCHAR param, PTTSet ts, PCHAR DDETopic) {
1746 maya 5869 int param_len=strlen(param);
1747     int opt_len = param_len+1;
1748     char *option = (char *)calloc(opt_len, sizeof(char));
1749 maya 5882 char *option2 = (char *)calloc(opt_len, sizeof(char));
1750 maya 5869 int action;
1751 maya 5874 PCHAR start, cur, next;
1752 maya 5908 size_t i;
1753 maya 5869
1754     if (pvar->hostdlg_activated) {
1755     pvar->settings.Enabled = pvar->hostdlg_Enabled;
1756     }
1757    
1758 maya 5874 /* the first term shuld be executable filename of Tera Term */
1759     start = GetParam(option, opt_len, param);
1760    
1761     cur = start;
1762 maya 5869 while (next = GetParam(option, opt_len, cur)) {
1763 doda 6140 DequoteParam(option, opt_len, option);
1764 maya 5869 action = OPTION_NONE;
1765    
1766     if ((option[0] == '-' || option[0] == '/')) {
1767     if (MATCH_STR(option + 1, "ssh") == 0) {
1768     if (MATCH_STR(option + 4, "-f=") == 0) {
1769 doda 6140 strncpy_s(option2, opt_len, option + 7, _TRUNCATE);
1770 maya 5882 read_ssh_options_from_user_file(pvar, option2);
1771 maya 5869 action = OPTION_CLEAR;
1772     } else if (MATCH_STR(option + 4, "-consume=") == 0) {
1773 doda 6140 strncpy_s(option2, opt_len, option + 13, _TRUNCATE);
1774 maya 5882 read_ssh_options_from_user_file(pvar, option2);
1775     DeleteFile(option2);
1776 maya 5869 action = OPTION_CLEAR;
1777     }
1778    
1779     // ttermpro.exe �� /F= �w������ TTSSH ������������ (2006.10.11 maya)
1780     } else if (MATCH_STR_I(option + 1, "f=") == 0) {
1781 doda 6140 strncpy_s(option2, opt_len, option + 3, _TRUNCATE);
1782 maya 5882 read_ssh_options_from_user_file(pvar, option2);
1783 maya 5869 // Tera Term���������������K�v������������������
1784     }
1785     }
1786    
1787     switch (action) {
1788     case OPTION_CLEAR:
1789     memset(cur, ' ', next-cur);
1790     break;
1791     case OPTION_REPLACE:
1792     memset(cur, ' ', next-cur);
1793     memcpy(cur+1, option, strlen(option));
1794     break;
1795     }
1796    
1797     cur = next;
1798     }
1799    
1800 maya 5874 cur = start;
1801 maya 5869 while (next = GetParam(option, opt_len, cur)) {
1802 doda 6140 DequoteParam(option, opt_len, option);
1803 maya 5869 action = OPTION_NONE;
1804    
1805     if ((option[0] == '-' || option[0] == '/')) {
1806     action = OPTION_CLEAR;
1807     if (MATCH_STR(option + 1, "ssh") == 0) {
1808     if (option[4] == 0) {
1809     pvar->settings.Enabled = 1;
1810     } else if (MATCH_STR(option + 4, "-L") == 0 ||
1811 doda 6846 MATCH_STR(option + 4, "-R") == 0 ||
1812     MATCH_STR(option + 4, "-D") == 0) {
1813 doda 5959 char *p = option + 5;
1814     option2[0] = *p;
1815     i = 1;
1816     while (*++p) {
1817     if (*p == ';' || *p == ',') {
1818     option2[i] = 0;
1819     add_forward_param(pvar, option2);
1820     i = 1;
1821 maya 5908 }
1822 doda 5959 else {
1823     option2[i++] = *p;
1824     }
1825 maya 5908 }
1826 doda 5959 if (i > 1) {
1827     option2[i] = 0;
1828     add_forward_param(pvar, option2);
1829     }
1830 maya 5869 } else if (MATCH_STR(option + 4, "-X") == 0) {
1831     add_forward_param(pvar, "X");
1832 doda 5959 if (option+6 != 0) {
1833     strncpy_s(pvar->settings.X11Display,
1834     sizeof(pvar->settings.X11Display),
1835     option + 6, _TRUNCATE);
1836     }
1837 doda 6473 } else if (strcmp(option + 4, "-v") == 0) {
1838 maya 5869 pvar->settings.LogLevel = LOG_LEVEL_VERBOSE;
1839     } else if (_stricmp(option + 4, "-autologin") == 0 ||
1840     _stricmp(option + 4, "-autologon") == 0) {
1841     pvar->settings.TryDefaultAuth = TRUE;
1842     } else if (MATCH_STR_I(option + 4, "-agentconfirm=") == 0) {
1843     if ((_stricmp(option+18, "off") == 0) ||
1844     (_stricmp(option+18, "no") == 0) ||
1845     (_stricmp(option+18, "false") == 0) ||
1846     (_stricmp(option+18, "0") == 0) ||
1847     (_stricmp(option+18, "n") == 0)) {
1848     pvar->settings.ForwardAgentConfirm = 0;
1849     }
1850     else {
1851     pvar->settings.ForwardAgentConfirm = 1;
1852     }
1853 doda 6473 } else if (strcmp(option + 4, "-a") == 0) {
1854 maya 5869 pvar->settings.ForwardAgent = FALSE;
1855 doda 6473 } else if (strcmp(option + 4, "-A") == 0) {
1856 maya 5869 pvar->settings.ForwardAgent = TRUE;
1857    
1858     } else if (MATCH_STR(option + 4, "-C=") == 0) {
1859     pvar->settings.CompressionLevel = atoi(option+7);
1860     if (pvar->settings.CompressionLevel < 0) {
1861     pvar->settings.CompressionLevel = 0;
1862     }
1863     else if (pvar->settings.CompressionLevel > 9) {
1864     pvar->settings.CompressionLevel = 9;
1865     }
1866 doda 6473 } else if (strcmp(option + 4, "-C") == 0) {
1867 maya 5869 pvar->settings.CompressionLevel = 6;
1868 doda 6473 } else if (strcmp(option + 4, "-c") == 0) {
1869 maya 5869 pvar->settings.CompressionLevel = 0;
1870     } else if (MATCH_STR_I(option + 4, "-icon=") == 0) {
1871     if ((_stricmp(option+10, "old") == 0) ||
1872     (_stricmp(option+10, "yellow") == 0) ||
1873     (_stricmp(option+10, "securett_yellow") == 0)) {
1874     pvar->settings.IconID = IDI_SECURETT_YELLOW;
1875     }
1876 maya 6386 else if ((_stricmp(option+10, "green") == 0) ||
1877     (_stricmp(option+10, "securett_green") == 0)) {
1878     pvar->settings.IconID = IDI_SECURETT_GREEN;
1879     }
1880 maya 5869 else {
1881     pvar->settings.IconID = IDI_SECURETT;
1882     }
1883 doda 6717 } else if (MATCH_STR(option + 4, "-subsystem=") == 0) {
1884     pvar->use_subsystem = TRUE;
1885     strncpy_s(pvar->subsystem_name,
1886     sizeof(pvar->subsystem_name),
1887     option + 15, _TRUNCATE);
1888 doda 7364 } else if (strcmp(option + 4, "-N") == 0) {
1889 doda 7363 pvar->nosession = TRUE;
1890 maya 5869
1891     // /ssh1 �� /ssh2 �I�v�V�������V�K���� (2006.9.16 maya)
1892 doda 6473 } else if (strcmp(option + 4, "1") == 0) {
1893 maya 5869 pvar->settings.Enabled = 1;
1894     pvar->settings.ssh_protocol_version = 1;
1895 doda 6473 } else if (strcmp(option + 4, "2") == 0) {
1896 maya 5869 pvar->settings.Enabled = 1;
1897     pvar->settings.ssh_protocol_version = 2;
1898    
1899     } else {
1900     char buf[1024];
1901    
1902     UTIL_get_lang_msg("MSG_UNKNOWN_OPTION_ERROR", pvar,
1903     "Unrecognized command-line option: %s");
1904     _snprintf_s(buf, sizeof(buf), _TRUNCATE, pvar->ts->UIMsg, option);
1905    
1906     MessageBox(NULL, buf, "TTSSH", MB_OK | MB_ICONEXCLAMATION);
1907     }
1908    
1909     // ttermpro.exe �� /T= �w�������p�������A������������ (2006.10.19 maya)
1910     } else if (MATCH_STR_I(option + 1, "t=") == 0) {
1911     if (strcmp(option + 3, "2") == 0) {
1912     pvar->settings.Enabled = 1;
1913     // /t=2��ttssh�������g������������
1914     } else {
1915     pvar->settings.Enabled = 0;
1916     action = OPTION_NONE; // Tera Term������������������������
1917     }
1918    
1919     // /1 ������ /2 �I�v�V�������V�K���� (2004.10.3 yutaka)
1920 doda 6473 } else if (strcmp(option + 1, "1") == 0) {
1921 maya 5869 // command line: /ssh /1 is SSH1 only
1922     pvar->settings.ssh_protocol_version = 1;
1923    
1924 doda 6473 } else if (strcmp(option + 1, "2") == 0) {
1925 maya 5869 // command line: /ssh /2 is SSH2 & SSH1
1926     pvar->settings.ssh_protocol_version = 2;
1927    
1928 doda 6473 } else if (strcmp(option + 1, "nossh") == 0) {
1929 maya 5869 // '/nossh' �I�v�V�����������B
1930     // TERATERM.INI ��SSH���L�������������������A������Cygterm���N��������������
1931     // �����������������B(2004.10.11 yutaka)
1932     pvar->settings.Enabled = 0;
1933    
1934 doda 6473 } else if (strcmp(option + 1, "telnet") == 0) {
1935 maya 5869 // '/telnet' ���w�������������������� '/nossh' ��������
1936     // SSH������������ (2006.9.16 maya)
1937     pvar->settings.Enabled = 0;
1938     // Tera Term �� Telnet �t���O���t����
1939     pvar->ts->Telnet = 1;
1940    
1941 doda 6473 } else if (MATCH_STR(option + 1, "auth=") == 0) {
1942 maya 5869 // SSH2�������O�C���I�v�V����������
1943     //
1944     // SYNOPSIS: /ssh /auth=passowrd /user=���[�U�� /passwd=�p�X���[�h
1945     // /ssh /auth=publickey /user=���[�U�� /passwd=�p�X���[�h /keyfile=�p�X
1946 doda 6472 // EXAMPLE: /ssh /auth=password /user=nike /passwd="a b""c" ; �p�X���[�h: �ua b"c�v
1947 maya 5869 // /ssh /auth=publickey /user=foo /passwd=bar /keyfile=d:\tmp\id_rsa
1948 doda 6472 // NOTICE: �p�X���[�h���p�X���������Z�~�R���������������������_�u���N�H�[�g " ������
1949     // �p�X���[�h���_�u���N�H�[�g�����������������A�������_�u���N�H�[�g "" ���u��������
1950 maya 5869 //
1951     pvar->ssh2_autologin = 1; // for SSH2 (2004.11.30 yutaka)
1952    
1953 doda 6473 if (_stricmp(option + 6, "password") == 0) { // �p�X���[�h
1954 maya 5869 //pvar->auth_state.cur_cred.method = SSH_AUTH_PASSWORD;
1955     pvar->ssh2_authmethod = SSH_AUTH_PASSWORD;
1956    
1957 doda 7101 } else if (_stricmp(option + 6, "keyboard-interactive") == 0) { // keyboard-interactive�F��
1958     //pvar->auth_state.cur_cred.method = SSH_AUTH_TIS;
1959     pvar->ssh2_authmethod = SSH_AUTH_TIS;
1960    
1961 doda 6473 } else if (_stricmp(option + 6, "challenge") == 0) { // keyboard-interactive�F��
1962 maya 5869 //pvar->auth_state.cur_cred.method = SSH_AUTH_TIS;
1963     pvar->ssh2_authmethod = SSH_AUTH_TIS;
1964    
1965 doda 6473 } else if (_stricmp(option + 6, "publickey") == 0) { // ���J���F��
1966 maya 5869 //pvar->auth_state.cur_cred.method = SSH_AUTH_RSA;
1967     pvar->ssh2_authmethod = SSH_AUTH_RSA;
1968    
1969 doda 6473 } else if (_stricmp(option + 6, "pageant") == 0) { // ���J���F�� by Pageant
1970 maya 5869 //pvar->auth_state.cur_cred.method = SSH_AUTH_RSA;
1971     pvar->ssh2_authmethod = SSH_AUTH_PAGEANT;
1972    
1973     } else {
1974     // TODO:
1975     }
1976    
1977     } else if (MATCH_STR(option + 1, "user=") == 0) {
1978 doda 6140 _snprintf_s(pvar->ssh2_username, sizeof(pvar->ssh2_username), _TRUNCATE, "%s", option+6);
1979 maya 5869
1980     } else if (MATCH_STR(option + 1, "passwd=") == 0) {
1981 doda 6140 _snprintf_s(pvar->ssh2_password, sizeof(pvar->ssh2_password), _TRUNCATE, "%s", option+8);
1982 maya 5869
1983     } else if (MATCH_STR(option + 1, "keyfile=") == 0) {
1984 doda 6140 _snprintf_s(pvar->ssh2_keyfile, sizeof(pvar->ssh2_keyfile), _TRUNCATE, "%s", option+9);
1985 maya 5869
1986 doda 6473 } else if (strcmp(option + 1, "ask4passwd") == 0) {
1987 maya 5869 // �p�X���[�h������ (2006.9.18 maya)
1988     pvar->ask4passwd = 1;
1989    
1990 doda 6473 } else if (strcmp(option + 1, "nosecuritywarning") == 0) {
1991 maya 5869 // known_hosts�`�F�b�N���������B���Y�I�v�V�������g�����A�Z�L�����e�B������������
1992     // �����A�B���I�v�V���������������B
1993     // (2009.10.4 yutaka)
1994     pvar->nocheck_known_hosts = TRUE;
1995    
1996     }
1997     else { // Other (not ttssh) option
1998     action = OPTION_NONE; // ttssh���I�v�V������������������������
1999     }
2000    
2001     // �p�X���[�h�������������������O�C��������������
2002     // /auth ���F�����\�b�h���w�������������p������ (2006.9.18 maya)
2003     if (pvar->ask4passwd == 1) {
2004     pvar->ssh2_autologin = 0;
2005     }
2006    
2007     }
2008     else if ((MATCH_STR_I(option, "ssh://") == 0) ||
2009     (MATCH_STR_I(option, "ssh1://") == 0) ||
2010     (MATCH_STR_I(option, "ssh2://") == 0) ||
2011     (MATCH_STR_I(option, "slogin://") == 0) ||
2012     (MATCH_STR_I(option, "slogin1://") == 0) ||
2013     (MATCH_STR_I(option, "slogin2://") == 0)) {
2014     //
2015     // ssh://user@host/ ����URL�`�����T�|�[�g
2016     // ���{�I�������� telnet:// URL��������
2017     //
2018     // �Q�l:
2019     // RFC3986: Uniform Resource Identifier (URI): Generic Syntax
2020     // RFC4248: The telnet URI Scheme
2021     //
2022     char *p, *p2, *p3;
2023     int optlen, hostlen;
2024    
2025     optlen = strlen(option);
2026    
2027     // ������':'���O�����������������������A������ssh�v���g�R���o�[�W������������
2028     p = _mbschr(option, ':');
2029     switch (*(p-1)) {
2030     case '1':
2031     pvar->settings.ssh_protocol_version = 1;
2032     break;
2033     case '2':
2034     pvar->settings.ssh_protocol_version = 2;
2035     break;
2036     }
2037    
2038     // authority part �����|�C���^������
2039     p += 3;
2040    
2041     // path part ������������
2042     if ((p2 = _mbschr(p, '/')) != NULL) {
2043     *p2 = 0;
2044     }
2045    
2046     // '@'�������������A���������O�����[�U����
2047     if ((p2 = _mbschr(p, '@')) != NULL) {
2048     *p2 = 0;
2049     // ':'���~���p�X���[�h
2050     if ((p3 = _mbschr(p, ':')) != NULL) {
2051     *p3 = 0;
2052     percent_decode(pvar->ssh2_password, sizeof(pvar->ssh2_password), p3 + 1);
2053     }
2054     percent_decode(pvar->ssh2_username, sizeof(pvar->ssh2_username), p);
2055     // p �� host part ������('@'����������)����������������
2056     p = p2 + 1;
2057     }
2058    
2059     // host part �� option �����������������Ascheme part ������
2060     // port�w����������������port���������������������m��������������
2061     hostlen = strlen(p);
2062     memmove_s(option, optlen, p, hostlen);
2063     option[hostlen] = 0;
2064    
2065     // �|�[�g�w������������":22"������
2066     if (option[0] == '[' && option[hostlen-1] == ']' || // IPv6 raw address without port
2067     option[0] != '[' && _mbschr(option, ':') == NULL) { // hostname or IPv4 raw address without port
2068     memcpy_s(option+hostlen, optlen-hostlen, ":22", 3);
2069     hostlen += 3;
2070     }
2071    
2072     // �|�[�g�w�����������������X�y�[�X������
2073     memset(option+hostlen, ' ', optlen-hostlen);
2074    
2075     pvar->settings.Enabled = 1;
2076    
2077     action = OPTION_REPLACE;
2078     }
2079     else if (_mbschr(option, '@') != NULL) {
2080     //
2081     // user@host �`�����T�|�[�g
2082     // ����������ssh�������T�|�[�g�����A���[�U����ttssh���������B
2083     // (ssh�������O -- ttssh�������W������������)
2084     // �����I��telnet authentication option���T�|�[�g��������
2085     // Tera Term�{�����������������������\���B
2086     //
2087     char *p;
2088     p = _mbschr(option, '@');
2089     *p = 0;
2090    
2091     strncpy_s(pvar->ssh2_username, sizeof(pvar->ssh2_username), option, _TRUNCATE);
2092    
2093     // ���[�U���������X�y�[�X�������B
2094     // ������TTX��Tera Term�{�������������������X�y�[�X�����������������A
2095     // �z�X�g�����������l�����K�v�������B
2096     memset(option, ' ', p-option+1);
2097    
2098     action = OPTION_REPLACE;
2099     }
2100    
2101    
2102     switch (action) {
2103     case OPTION_CLEAR:
2104     memset(cur, ' ', next-cur);
2105     break;
2106     case OPTION_REPLACE:
2107     memset(cur, ' ', next-cur);
2108     memcpy(cur+1, option, strlen(option));
2109     break;
2110     }
2111    
2112     cur = next;
2113     }
2114    
2115     free(option);
2116    
2117     FWDUI_load_settings(pvar);
2118    
2119     (pvar->ParseParam) (param, ts, DDETopic);
2120     }
2121 maya 3227
2122 doda 6801 static void PASCAL TTXGetSetupHooks(TTXSetupHooks *hooks)
2123 maya 3227 {
2124     pvar->ReadIniFile = *hooks->ReadIniFile;
2125     pvar->WriteIniFile = *hooks->WriteIniFile;
2126     pvar->ParseParam = *hooks->ParseParam;
2127    
2128     *hooks->ReadIniFile = TTXReadINIFile;
2129     *hooks->WriteIniFile = TTXWriteINIFile;
2130     *hooks->ParseParam = TTXParseParam;
2131     }
2132    
2133 doda 6801 static void PASCAL TTXSetWinSize(int rows, int cols)
2134 maya 3227 {
2135     SSH_notify_win_size(pvar, cols, rows);
2136     }
2137    
2138     static void insertMenuBeforeItem(HMENU menu, WORD beforeItemID, WORD flags,
2139 doda 6801 WORD newItemID, char *text)
2140 maya 3227 {
2141     int i, j;
2142    
2143     for (i = GetMenuItemCount(menu) - 1; i >= 0; i--) {
2144     HMENU submenu = GetSubMenu(menu, i);
2145    
2146     for (j = GetMenuItemCount(submenu) - 1; j >= 0; j--) {
2147     if (GetMenuItemID(submenu, j) == beforeItemID) {
2148     InsertMenu(submenu, j, MF_BYPOSITION | flags, newItemID, text);
2149     return;
2150     }
2151     }
2152     }
2153     }
2154    
2155 doda 4463 #define GetFileMenu(menu) GetSubMenuByChildID(menu, 50110) // ID_FILE_NEWCONNECTION
2156     #define GetEditMenu(menu) GetSubMenuByChildID(menu, 50210) // ID_EDIT_COPY2
2157     #define GetSetupMenu(menu) GetSubMenuByChildID(menu, 50310) // ID_SETUP_TERMINAL
2158     #define GetControlMenu(menu) GetSubMenuByChildID(menu, 50410) // ID_CONTROL_RESETTERMINAL
2159     #define GetHelpMenu(menu) GetSubMenuByChildID(menu, 50990) // ID_HELP_ABOUT
2160    
2161     HMENU GetSubMenuByChildID(HMENU menu, UINT id) {
2162     int i, j, items, subitems, cur_id;
2163     HMENU m;
2164    
2165     items = GetMenuItemCount(menu);
2166    
2167     for (i=0; i<items; i++) {
2168     if (m = GetSubMenu(menu, i)) {
2169     subitems = GetMenuItemCount(m);
2170     for (j=0; j<subitems; j++) {
2171     cur_id = GetMenuItemID(m, j);
2172     if (cur_id == id) {
2173     return m;
2174     }
2175     }
2176     }
2177     }
2178     return NULL;
2179     }
2180    
2181 doda 6801 static void PASCAL TTXModifyMenu(HMENU menu)
2182 maya 3227 {
2183 doda 4463 pvar->FileMenu = GetFileMenu(menu);
2184    
2185 maya 3227 /* inserts before ID_HELP_ABOUT */
2186     UTIL_get_lang_msg("MENU_ABOUT", pvar, "About &TTSSH...");
2187     insertMenuBeforeItem(menu, 50990, MF_ENABLED, ID_ABOUTMENU, pvar->ts->UIMsg);
2188    
2189     /* inserts before ID_SETUP_TCPIP */
2190     UTIL_get_lang_msg("MENU_SSH", pvar, "SS&H...");
2191     insertMenuBeforeItem(menu, 50360, MF_ENABLED, ID_SSHSETUPMENU, pvar->ts->UIMsg);
2192     /* inserts before ID_SETUP_TCPIP */
2193     UTIL_get_lang_msg("MENU_SSH_AUTH", pvar, "SSH &Authentication...");
2194     insertMenuBeforeItem(menu, 50360, MF_ENABLED, ID_SSHAUTHSETUPMENU, pvar->ts->UIMsg);
2195     /* inserts before ID_SETUP_TCPIP */
2196     UTIL_get_lang_msg("MENU_SSH_FORWARD", pvar, "SSH F&orwarding...");
2197     insertMenuBeforeItem(menu, 50360, MF_ENABLED, ID_SSHFWDSETUPMENU, pvar->ts->UIMsg);
2198     UTIL_get_lang_msg("MENU_SSH_KEYGEN", pvar, "SSH KeyGe&nerator...");
2199     insertMenuBeforeItem(menu, 50360, MF_ENABLED, ID_SSHKEYGENMENU, pvar->ts->UIMsg);
2200    
2201     /* inserts before ID_FILE_CHANGEDIR */
2202     UTIL_get_lang_msg("MENU_SSH_SCP", pvar, "SS&H SCP...");
2203 doda 4463 insertMenuBeforeItem(menu, 50170, MF_GRAYED, ID_SSHSCPMENU, pvar->ts->UIMsg);
2204 maya 3227 }
2205    
2206 doda 6801 static void PASCAL TTXModifyPopupMenu(HMENU menu) {
2207 doda 4463 if (menu == pvar->FileMenu) {
2208 maya 6997 if (pvar->cv->Ready && pvar->socket != INVALID_SOCKET)
2209 doda 4463 EnableMenuItem(menu, ID_SSHSCPMENU, MF_BYCOMMAND | MF_ENABLED);
2210     else
2211     EnableMenuItem(menu, ID_SSHSCPMENU, MF_BYCOMMAND | MF_GRAYED);
2212     }
2213     }
2214    
2215 maya 6132 static void about_dlg_set_abouttext(PTInstVar pvar, HWND dlg, digest_algorithm dgst_alg)
2216 maya 3227 {
2217 maya 6135 char buf[1024], buf2[2048];
2218 yutakapon 5575 char *fp = NULL;
2219 maya 3227
2220     // TTSSH�_�C�A���O���\������SSH������������ (2004.10.30 yutaka)
2221     if (pvar->socket != INVALID_SOCKET) {
2222 maya 6135 buf2[0] = '\0';
2223 maya 6132
2224 maya 3227 if (SSHv1(pvar)) {
2225 maya 6135 UTIL_get_lang_msg("DLG_ABOUT_SERVERID", pvar, "Server ID:");
2226     strncat_s(buf2, sizeof(buf2), pvar->ts->UIMsg, _TRUNCATE);
2227     strncat_s(buf2, sizeof(buf2), " ", _TRUNCATE);
2228 maya 3227 SSH_get_server_ID_info(pvar, buf, sizeof(buf));
2229 maya 6135 strncat_s(buf2, sizeof(buf2), buf, _TRUNCATE);
2230     strncat_s(buf2, sizeof(buf2), "\r\n", _TRUNCATE);
2231    
2232 maya 3227 UTIL_get_lang_msg("DLG_ABOUT_PROTOCOL", pvar, "Using protocol:");
2233 maya 6135 strncat_s(buf2, sizeof(buf2), pvar->ts->UIMsg, _TRUNCATE);
2234     strncat_s(buf2, sizeof(buf2),