title: Server with ssh public key authentication with libssh
body: <p>Can someone give an example with server with ssh public key authentication with libssh? I found this <a href="https://github.com/substack/libssh/blob/master/examples/samplesshd-tty.c" rel="nofollow">https://github.com/substack/libssh/blob/master/examples/samplesshd-tty.c</a>, but it's authentication with password. May be someone seen example like this, but with public key authentication on libssh. Or may be someone can change the code below to get it.</p>
  
  <p>I added this to switch(ssh_message_subtype(message))</p>
  
  <pre><code>                  case SSH_AUTH_METHOD_PUBLICKEY:
                          printf("User %s wants to auth with key %s\n",
                                 ssh_message_auth_user(message),
                                 ssh_message_auth_pubkey(message));
                          if(authenticate_pubkey(session, message)){
                              ssh_message_auth_reply_success(message,0);
                              ssh_message_free(message);
                              return 1;
                          }
                          ssh_message_auth_set_methods(message,
                                                       SSH_AUTH_METHOD_PUBLICKEY);
                          // not authenticated, send default message
                          ssh_message_reply_default(message);
                          break;
  </code></pre>
  
  <p>and this</p>
  
  <pre><code>static int authenticate_pubkey(ssh_session session, ssh_message message)
  {
      int rc;
      std::string us =  ssh_message_auth_user(message);
      rc = ssh_userauth_publickey_auto(session, ssh_message_auth_user(message), NULL);
      if (rc == SSH_AUTH_ERROR)
      {
          fprintf(stderr, "Authentication failed: %s\n",
                  ssh_get_error(session));
          return SSH_AUTH_ERROR;
      }
      return rc;
  }
  </code></pre>