| 1 |
/* |
| 2 |
* (C) 2022- TeraTerm Project |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* 3. The name of the author may not be used to endorse or promote products |
| 15 |
* derived from this software without specific prior written permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 18 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
/* |
| 30 |
* ttxssh.dll �����C���^�[�t�F�C�X |
| 31 |
* - ttssh2/ttxssh/ttxssh.def �Q�� |
| 32 |
* |
| 33 |
* TODO |
| 34 |
* - unicode(wchar_t) filename |
| 35 |
* - init()/uninit() per ssh connect/disconnect |
| 36 |
*/ |
| 37 |
|
| 38 |
#define _CRTDBG_MAP_ALLOC |
| 39 |
#include <stdlib.h> |
| 40 |
#include <crtdbg.h> |
| 41 |
#include <windows.h> |
| 42 |
|
| 43 |
#include "codeconv.h" |
| 44 |
|
| 45 |
#include "scp.h" |
| 46 |
|
| 47 |
typedef int (CALLBACK *PSSH_start_scp)(char *, char *); |
| 48 |
typedef int (CALLBACK * PSSH_scp_sending_status)(void); |
| 49 |
typedef size_t (CALLBACK *PSSH_GetKnownHostsFileName)(wchar_t *, size_t); |
| 50 |
|
| 51 |
static HMODULE h = NULL; |
| 52 |
static PSSH_start_scp start_scp = NULL; |
| 53 |
static PSSH_start_scp receive_file = NULL; |
| 54 |
static PSSH_scp_sending_status scp_sending_status = NULL; |
| 55 |
static PSSH_GetKnownHostsFileName GetKnownHostsFileName; |
| 56 |
|
| 57 |
/** |
| 58 |
* @brief SCP�������A�h���X������ |
| 59 |
* @retval TRUE ok |
| 60 |
* @retval FALSE dll������/dll��scp���M���������������� |
| 61 |
*/ |
| 62 |
static BOOL ScpInit(void) |
| 63 |
{ |
| 64 |
if (h == NULL) { |
| 65 |
if ((h = GetModuleHandle("ttxssh.dll")) == NULL) { |
| 66 |
return FALSE; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (start_scp == NULL) { |
| 71 |
start_scp = (PSSH_start_scp)GetProcAddress(h, "TTXScpSendfile"); |
| 72 |
if (start_scp == NULL) { |
| 73 |
return FALSE; |
| 74 |
} |
| 75 |
} |
| 76 |
if (scp_sending_status == NULL) { |
| 77 |
scp_sending_status = (PSSH_scp_sending_status)GetProcAddress(h, "TTXScpSendingStatus"); |
| 78 |
if (scp_sending_status == NULL) { |
| 79 |
return FALSE; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if (receive_file == NULL) { |
| 84 |
receive_file = (PSSH_start_scp)GetProcAddress(h, "TTXScpReceivefile"); |
| 85 |
if (receive_file == NULL) { |
| 86 |
return FALSE; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (GetKnownHostsFileName == NULL) { |
| 91 |
GetKnownHostsFileName = (PSSH_GetKnownHostsFileName)GetProcAddress(h, "TTXReadKnownHostsFile"); |
| 92 |
if (GetKnownHostsFileName == NULL) { |
| 93 |
return FALSE; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return TRUE; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* �t�@�C�������M���� |
| 102 |
* @param local ���[�J��(PC,Windows)�����t�@�C�� |
| 103 |
* �t�H���_���w���������� |
| 104 |
* @param remote �����[�g(ssh�T�[�o�[)�����t�H���_ (or �t�@�C����?) |
| 105 |
* L""���z�[���f�B���N�g�� |
| 106 |
* @return TRUE ok(���N�G�X�g������) |
| 107 |
* @return FALSE ng |
| 108 |
*/ |
| 109 |
BOOL ScpSend(const wchar_t *local, const wchar_t *remote) |
| 110 |
{ |
| 111 |
if (start_scp == NULL) { |
| 112 |
ScpInit(); |
| 113 |
} |
| 114 |
if (start_scp == NULL) { |
| 115 |
return FALSE; |
| 116 |
} |
| 117 |
char *localU8 = ToU8W(local); |
| 118 |
char *remoteU8 = ToU8W(remote); |
| 119 |
BOOL r = (BOOL)start_scp(localU8, remoteU8); |
| 120 |
free(localU8); |
| 121 |
free(remoteU8); |
| 122 |
return r; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* �t�@�C�����M���� |
| 127 |
* @retval FALSE ���M���������� |
| 128 |
* @retval TRUE ���M�� |
| 129 |
*/ |
| 130 |
BOOL ScpGetStatus(void) |
| 131 |
{ |
| 132 |
if (scp_sending_status == NULL) { |
| 133 |
ScpInit(); |
| 134 |
} |
| 135 |
if (scp_sending_status == NULL) { |
| 136 |
return FALSE; |
| 137 |
} |
| 138 |
BOOL r = (BOOL)scp_sending_status(); |
| 139 |
return r; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* �t�@�C�������M���� |
| 144 |
*/ |
| 145 |
BOOL ScpReceive(const wchar_t *remotefile, const wchar_t *localfile) |
| 146 |
{ |
| 147 |
if (receive_file == NULL) { |
| 148 |
ScpInit(); |
| 149 |
} |
| 150 |
if (receive_file == NULL) { |
| 151 |
return FALSE; |
| 152 |
} |
| 153 |
char *localU8 = ToU8W(localfile); |
| 154 |
char *remoteU8 = ToU8W(remotefile); |
| 155 |
BOOL r = (BOOL)receive_file(remoteU8, localU8); |
| 156 |
free(localU8); |
| 157 |
free(remoteU8); |
| 158 |
return r; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* knownhost�t�@�C���������� |
| 163 |
* �s�v����������free()�������� |
| 164 |
*/ |
| 165 |
BOOL TTXSSHGetKnownHostsFileName(wchar_t **filename) |
| 166 |
{ |
| 167 |
if (GetKnownHostsFileName == NULL) { |
| 168 |
ScpInit(); |
| 169 |
} |
| 170 |
if (GetKnownHostsFileName == NULL) { |
| 171 |
*filename = NULL; |
| 172 |
return FALSE; |
| 173 |
} |
| 174 |
|
| 175 |
size_t size = GetKnownHostsFileName(NULL, 0); |
| 176 |
if (size == 0) { |
| 177 |
*filename = NULL; |
| 178 |
return FALSE; |
| 179 |
} |
| 180 |
wchar_t *f = (wchar_t *)malloc(sizeof(wchar_t) * size); |
| 181 |
if (f == NULL) { |
| 182 |
*filename = NULL; |
| 183 |
return FALSE; |
| 184 |
} |
| 185 |
GetKnownHostsFileName(f, size); |
| 186 |
|
| 187 |
*filename = f; |
| 188 |
return TRUE; |
| 189 |
} |