📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

167行目: 167行目:
また、ビルドされたDLLファイルは、以下に示すWebサイトからダウンロードできる。<br>
また、ビルドされたDLLファイルは、以下に示すWebサイトからダウンロードできる。<br>
https://download.csdn.net/download/sdhongjun/15682389<br>
https://download.csdn.net/download/sdhongjun/15682389<br>
<br><br>
== libSSHの使用例 ==
<syntaxhighlight lang="c++">
#include <QCoreApplication>
#include <libssh/libssh.h>
int VerifyKnownsHost(ssh_session session);
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // SSHセッションの作成
    ssh_session ssh_session = ssh_new();
    if (my_ssh_session == NULL) {
      // 作成に失敗した場合
      return -1;
    }
    // SSHセッションの設定
    QString host = "<リモートPCのIPアドレス または ホスト名>";
    QString user = "<リモートPCのユーザ名>";
    QString port = "<SSHポート番号  例: 22>";
    ssh_options_set(ssh_session, SSH_OPTIONS_HOST, host.toUtf8().data());
    ssh_options_set(ssh_session, SSH_OPTIONS_USER, user.toUtf8().data());
    ssh_options_set(ssh_session, SSH_OPTIONS_PORT_STR, port.toUtf8().data());
    // SSH接続
    int rc = ssh_connect(ssh_session);
    if (rc != SSH_OK) {
      // 接続に失敗した場合の処理
      fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(ssh_session));
      ssh_free(ssh_session);
      return -1;
    }
    // Verify the server's identity
    // For the source code of verify_knownhost(), check previous example
    if(VerifyKnownsHost(ssh_session) < 0)
    {
      if(ssh_session != nullptr)
      {
          ssh_disconnect(ssh_session);
          ssh_free(ssh_session);
      }
      return -1;
    }
    // 公開鍵認証
    // 秘密鍵の設定
    const char *private_key_path = "<秘密鍵のパス  例: /home/user/sshkey/id_rsa";
    // 秘密鍵のパスフレーズを設定していない場合
    rc = ssh_userauth_privatekey_file(my_ssh_session, nullptr, private_key_path, nullptr);
    // 秘密鍵のパスフレーズを設定している場合
    rc = ssh_userauth_privatekey_file(my_ssh_session, nullptr, private_key_path, "<秘密鍵のパスフレーズ>");
    if (rc != SSH_AUTH_SUCCESS) {
      // 認証に失敗した場合の処理
      fprintf(stderr, "Error authenticating with private key: %s\n", ssh_get_error(my_ssh_session));
      ssh_disconnect(my_ssh_session);
      ssh_free(my_ssh_session);
      return -1;
    }
    // SSHセッションを使用して任意の処理を実行
    // scpコマンドの実行、または、リモート先で任意のコマンドの実行等
    // ...略
    // SSH接続の切断
    ssh_disconnect(ssh_session);
    // SSHセッションの解放
    ssh_free(ssh_session);
    return 0;
}
int VerifyKnownsHost(ssh_session ssh_session, QString &strErrMsg)
{
    // Authenticating the server.
    ssh_key srv_pubkey = {};
    if(ssh_get_server_publickey(ssh_session, &srv_pubkey) < 0)
    {
      strErrMsg = tr("Failed to get public key.");
      return -1;
    }
    unsigned char *hash = nullptr;
    size_t        hlen  = 0L;
    auto iRet = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA256, &hash, &hlen);
    ssh_key_free(srv_pubkey);
    if(iRet < 0)
    {
      strErrMsg = tr("Failed to get public key hash.");
      return -1;
    }
    auto state = ssh_session_is_known_server(ssh_session);
    if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OK)
    {  // Authentication Successful
    }
    else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_CHANGED)
    {
      QString strHexa = ssh_get_hexa(hash, hlen);
      // print string in reverse order
      strErrMsg = tr("Host key for server changed:") + "<br>" +
                  tr("For security reasons, connection will be stopped.") + "<br><br>" +
                  tr("Public key hash:") + "<br>" + strHexa + "<br>" + hlen;
      ssh_clean_pubkey_hash(&hash);
      return -1;
    }
    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>" +
                  tr("An attacker might change the default server key to confuse your client into") + "<br>" +
                  tr("thinking the key does not exist");
      ssh_clean_pubkey_hash(&hash);
      return -1;
    }
    else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_NOT_FOUND)
    {
      /* FALL THROUGH to SSH_KNOWN_HOSTS_UNKNOWN behavior */
      QString strHexa = ssh_get_hexa(hash, hlen);
      QString strAddHostMessage = tr("Could not find known host file.") + "\n" +
                                  tr("If you accept the host key here, the file will be automatically created.") + "\n\n" +
                                  tr("The server is unknown. Do you trust the host key?") + "\n" +
                                  tr("Public key hash: ") + "\n" + strHexa;
      auto ret = QMessageBox(QMessageBox::Warning, QMessageBox::tr("Add Host"), strAddHostMessage,
                              QMessageBox::Yes | QMessageBox::No, nullptr).exec();
      ssh_clean_pubkey_hash(&hash);
      if(ret == QMessageBox::No)
      {
          strErrMsg = tr("To connect, please add host key.");
          return -1;
      }
      else
      {
          iRet = ssh_session_update_known_hosts(ssh_session);
          if(iRet < 0)
          {
            strErrMsg = tr("Failed to update host key.");
            return -1;
          }
      }
    }
    else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_UNKNOWN)
    {
      QString strHexa = ssh_get_hexa(hash, hlen);
      QString strAddHostMessage = tr("The server is unknown. Do you trust the host key?") + "\n" +
                                  tr("Public key hash: ") + "\n" + strHexa;
      auto msgRet = QMessageBox(QMessageBox::Warning, QMessageBox::tr("Add Host"), strAddHostMessage,
                                QMessageBox::Yes | QMessageBox::No, nullptr).exec();
      ssh_clean_pubkey_hash(&hash);
      if(msgRet == QMessageBox::No)
      {
          strErrMsg = tr("To connect, please add host key.");
          return -1;
      }
      else
      {
          iRet = ssh_session_update_known_hosts(ssh_session);
          if(iRet < 0)
          {
            strErrMsg = tr("Failed to update host key.");
            return -1;
          }
      }
    }
    else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_ERROR)
    {
      strErrMsg = tr("There was an error in checking the host.");
      ssh_clean_pubkey_hash(&hash);
      return -1;
    }
    ssh_clean_pubkey_hash(&hash);
    return 0;
}
</syntaxhighlight>
<br><br>
<br><br>