将PAM配置为在登录try 失败后等待一段指定时间后才显示新的登录提示后,我想使用一个简单的程序来测试此行为,该程序将try 连接到远程服务器并测量显示登录提示之间的时间间隔:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <libssh/libssh.h>
#define BILLION 1000000000.0
int main(void) {
struct timespec start, finish;
ssh_session session;
int rc;
int session_status;
const char *server_banner;
session = ssh_new();
const char *host = "10.11.12.13";
const char *username = "baduser";
const char *password = "badpass";
ssh_options_set(session, SSH_OPTIONS_HOST, host);
ssh_options_set(session, SSH_OPTIONS_USER, username);
clock_gettime(CLOCK_REALTIME, &start);
rc = ssh_connect(session);
session_status = ssh_get_status(session);
printf("Session status: %d\n", session_status);
server_banner = ssh_get_serverbanner(session);
printf("Server banner: %s\n", server_banner);
if (rc != SSH_OK) {
fprintf(stderr, "Error connecting to %s: %s\n", host, ssh_get_error(session));
ssh_free(session);
exit(-1);
}
printf("Return Code is: %d\n", rc);
if (ssh_userauth_password(session, NULL, password) != SSH_AUTH_ERROR) {
fprintf(stderr, "Authentication succeeded with incorrect password.\n");
}
else {
fprintf(stderr, "Authentication failed with incorrect password.\n");
}
clock_gettime(CLOCK_REALTIME, &finish);
ssh_disconnect(session);
ssh_free(session);
double time_spent = (finish.tv_sec - start.tv_sec) + (finish.tv_nsec - start.tv_nsec) / BILLION;
printf("Elapsed time: %.4f seconds.\n", time_spent);
return (0);
}
但是,上面的代码返回以下输出:
Session status: 0
Server banner: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
Return Code is: 0
Authentication succeeded with incorrect password.
Elapsed time: 2.2801 seconds.
令人惊讶的是,会话状态表明连接成功,而且,Authentication succeeded with incorrect password.
表示程序能够与服务器连接.这不是真的,因为我从未与服务器交换过密钥.此外,服务器上没有与连接可能使用的任何用户名匹配的用户.
请帮助找出这里发生了什么:如何让程序接收SSH会话的正确状态(连接超时或连接被拒绝,这是从控制台访问服务器时通常会发生的情况).