📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 207行目: | 207行目: | ||
#include <libssh/libssh.h> | #include <libssh/libssh.h> | ||
int VerifyKnownsHost(ssh_session | int VerifyKnownsHost(ssh_session my_ssh_session, QString &strErrMsg); | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
| 214行目: | 214行目: | ||
// SSHセッションの作成 | // SSHセッションの作成 | ||
ssh_session | ssh_session my_ssh_session = ssh_new(); | ||
if (my_ssh_session == NULL) { | if (my_ssh_session == NULL) { | ||
// | // SSHセッションの作成に失敗した場合 | ||
return -1; | return -1; | ||
} | } | ||
| 225行目: | 225行目: | ||
QString port = "<SSHポート番号 例: 22>"; | QString port = "<SSHポート番号 例: 22>"; | ||
ssh_options_set( | ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host.toUtf8().data()); | ||
ssh_options_set( | ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user.toUtf8().data()); | ||
ssh_options_set( | ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT_STR, port.toUtf8().data()); | ||
// SSH接続 | // SSH接続 | ||
int rc = ssh_connect( | int rc = ssh_connect(my_ssh_session); | ||
if (rc != SSH_OK) { | if (rc != SSH_OK) { | ||
// 接続に失敗した場合 | // 接続に失敗した場合 | ||
fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error( | fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session)); | ||
ssh_free( | ssh_free(my_ssh_session); | ||
return -1; | return -1; | ||
| 241行目: | 241行目: | ||
// ~/.sshディレクトリ等にあるファイルに記述されているサーバのIDを検証 | // ~/.sshディレクトリ等にあるファイルに記述されているサーバのIDを検証 | ||
QString strErrMsg = ""; | QString strErrMsg = ""; | ||
if(VerifyKnownsHost( | if(VerifyKnownsHost(my_ssh_session, strErrMsg) < 0) { | ||
fprintf(stderr, "%s\n", strErrMsg.toUtf8().constData(); | fprintf(stderr, "%s\n", strErrMsg.toUtf8().constData(); | ||
if( | if(my_ssh_session != nullptr) | ||
{ | { | ||
ssh_disconnect( | ssh_disconnect(my_ssh_session); | ||
ssh_free( | ssh_free(my_ssh_session); | ||
} | } | ||
| 258行目: | 257行目: | ||
// 秘密鍵のパスフレーズを設定していない場合 | // 秘密鍵のパスフレーズを設定していない場合 | ||
rc = ssh_userauth_privatekey_file( | rc = ssh_userauth_privatekey_file(my_ssh_session, nullptr, private_key_path, nullptr); | ||
// 秘密鍵のパスフレーズを設定している場合 | // 秘密鍵のパスフレーズを設定している場合 | ||
rc = ssh_userauth_privatekey_file( | rc = ssh_userauth_privatekey_file(my_ssh_session, nullptr, private_key_path, "<秘密鍵のパスフレーズ>"); | ||
if (rc != SSH_AUTH_SUCCESS) { | if (rc != SSH_AUTH_SUCCESS) { | ||
| 276行目: | 275行目: | ||
// SSH接続の切断 | // SSH接続の切断 | ||
ssh_disconnect( | ssh_disconnect(my_ssh_session); | ||
// SSHセッションの解放 | // SSHセッションの解放 | ||
ssh_free( | ssh_free(my_ssh_session); | ||
return 0; | return 0; | ||
} | } | ||
int VerifyKnownsHost(ssh_session | int VerifyKnownsHost(ssh_session my_ssh_session, QString &strErrMsg) | ||
{ | { | ||
// Authenticating the server. | // Authenticating the server. | ||
ssh_key srv_pubkey = {}; | ssh_key srv_pubkey = {}; | ||
if(ssh_get_server_publickey( | if(ssh_get_server_publickey(my_ssh_session, &srv_pubkey) < 0) { | ||
strErrMsg = tr("Failed to get public key."); | strErrMsg = tr("Failed to get public key."); | ||
| 301行目: | 299行目: | ||
ssh_key_free(srv_pubkey); | ssh_key_free(srv_pubkey); | ||
if(iRet < 0) | if (iRet < 0) { | ||
strErrMsg = tr("Failed to get public key hash."); | strErrMsg = tr("Failed to get public key hash."); | ||
return -1; | return -1; | ||
} | } | ||
auto state = ssh_session_is_known_server( | auto state = ssh_session_is_known_server(my_ssh_session); | ||
if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OK) | if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OK) { | ||
// Authentication Successful | |||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_CHANGED) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_CHANGED) { | ||
QString strHexa = ssh_get_hexa(hash, hlen); | QString strHexa = ssh_get_hexa(hash, hlen); | ||
| 325行目: | 320行目: | ||
return -1; | return -1; | ||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OTHER) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OTHER) | ||
{ | { | ||
strErrMsg = tr("The host key for this server was not found but an other type of key exists.") + "<br>" + | strErrMsg = tr("The host key for this server was not found but an other type of key exists.") + "<br>" + | ||
| 348行目: | 343行目: | ||
ssh_clean_pubkey_hash(&hash); | ssh_clean_pubkey_hash(&hash); | ||
if(ret == QMessageBox::No) | if(ret == QMessageBox::No) { | ||
strErrMsg = tr("To connect, please add host key."); | strErrMsg = tr("To connect, please add host key."); | ||
return -1; | return -1; | ||
} | } | ||
else | else { | ||
iRet = ssh_session_update_known_hosts(my_ssh_session); | |||
iRet = ssh_session_update_known_hosts( | if(iRet < 0) { | ||
if(iRet < 0) | |||
strErrMsg = tr("Failed to update host key."); | strErrMsg = tr("Failed to update host key."); | ||
return -1; | return -1; | ||
} | } | ||
} | } | ||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_UNKNOWN) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_UNKNOWN) { | ||
QString strHexa = ssh_get_hexa(hash, hlen); | QString strHexa = ssh_get_hexa(hash, hlen); | ||
QString strAddHostMessage = tr("The server is unknown. Do you trust the host key?") + "\n" + | QString strAddHostMessage = tr("The server is unknown. Do you trust the host key?") + "\n" + | ||
| 375行目: | 364行目: | ||
ssh_clean_pubkey_hash(&hash); | ssh_clean_pubkey_hash(&hash); | ||
if(msgRet == QMessageBox::No) | if (msgRet == QMessageBox::No) { | ||
strErrMsg = tr("To connect, please add host key."); | strErrMsg = tr("To connect, please add host key."); | ||
return -1; | return -1; | ||
} | } | ||
else | else { | ||
iRet = ssh_session_update_known_hosts(my_ssh_session); | |||
iRet = ssh_session_update_known_hosts( | if (iRet < 0) { | ||
if(iRet < 0) | |||
strErrMsg = tr("Failed to update host key."); | strErrMsg = tr("Failed to update host key."); | ||
return -1; | return -1; | ||
} | } | ||
} | } | ||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_ERROR) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_ERROR) { | ||
strErrMsg = tr("There was an error in checking the host."); | strErrMsg = tr("There was an error in checking the host."); | ||