1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-31 00:03:07 +03:00

examples: Reformat knownhosts

Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Andreas Schneider
2018-09-25 14:42:45 +02:00
parent f09ca85ebf
commit e210b61148

View File

@ -32,82 +32,85 @@ clients must be made or how a client should react.
#define strncasecmp _strnicmp #define strncasecmp _strnicmp
#endif #endif
int verify_knownhost(ssh_session session){ int verify_knownhost(ssh_session session)
enum ssh_known_hosts_e state; {
char buf[10]; enum ssh_known_hosts_e state;
unsigned char *hash = NULL; char buf[10];
size_t hlen; unsigned char *hash = NULL;
ssh_key srv_pubkey; size_t hlen;
int rc; ssh_key srv_pubkey;
int rc;
rc = ssh_get_server_publickey(session, &srv_pubkey); rc = ssh_get_server_publickey(session, &srv_pubkey);
if (rc < 0) { if (rc < 0) {
return -1; return -1;
} }
rc = ssh_get_publickey_hash(srv_pubkey, rc = ssh_get_publickey_hash(srv_pubkey,
SSH_PUBLICKEY_HASH_SHA256, SSH_PUBLICKEY_HASH_SHA256,
&hash, &hash,
&hlen); &hlen);
ssh_key_free(srv_pubkey); ssh_key_free(srv_pubkey);
if (rc < 0) { if (rc < 0) {
return -1; return -1;
} }
state = ssh_session_is_known_server(session); state = ssh_session_is_known_server(session);
switch(state){ switch(state) {
case SSH_KNOWN_HOSTS_OK:
break; /* ok */
case SSH_KNOWN_HOSTS_CHANGED: case SSH_KNOWN_HOSTS_CHANGED:
fprintf(stderr,"Host key for server changed : server's one is now :\n"); fprintf(stderr,"Host key for server changed : server's one is now :\n");
ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, hash, hlen); ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, hash, hlen);
ssh_clean_pubkey_hash(&hash); ssh_clean_pubkey_hash(&hash);
fprintf(stderr,"For security reason, connection will be stopped\n"); fprintf(stderr,"For security reason, connection will be stopped\n");
return -1; return -1;
case SSH_KNOWN_HOSTS_OTHER: case SSH_KNOWN_HOSTS_OTHER:
fprintf(stderr,"The host key for this server was not found but an other type of key exists.\n"); fprintf(stderr,"The host key for this server was not found but an other type of key exists.\n");
fprintf(stderr,"An attacker might change the default server key to confuse your client" fprintf(stderr,"An attacker might change the default server key to confuse your client"
"into thinking the key does not exist\n" "into thinking the key does not exist\n"
"We advise you to rerun the client with -d or -r for more safety.\n"); "We advise you to rerun the client with -d or -r for more safety.\n");
return -1; return -1;
case SSH_KNOWN_HOSTS_NOT_FOUND: case SSH_KNOWN_HOSTS_NOT_FOUND:
fprintf(stderr,"Could not find known host file. If you accept the host key here,\n"); fprintf(stderr,"Could not find known host file. If you accept the host key here,\n");
fprintf(stderr,"the file will be automatically created.\n"); fprintf(stderr,"the file will be automatically created.\n");
/* fallback to SSH_SERVER_NOT_KNOWN behavior */ /* fallback to SSH_SERVER_NOT_KNOWN behavior */
FALL_THROUGH; FALL_THROUGH;
case SSH_SERVER_NOT_KNOWN: case SSH_SERVER_NOT_KNOWN:
fprintf(stderr, fprintf(stderr,
"The server is unknown. Do you trust the host key (yes/no)?\n"); "The server is unknown. Do you trust the host key (yes/no)?\n");
ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, hash, hlen); ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, hash, hlen);
if (fgets(buf, sizeof(buf), stdin) == NULL) { if (fgets(buf, sizeof(buf), stdin) == NULL) {
ssh_clean_pubkey_hash(&hash); ssh_clean_pubkey_hash(&hash);
return -1; return -1;
} }
if(strncasecmp(buf,"yes",3)!=0){ if(strncasecmp(buf,"yes",3)!=0){
ssh_clean_pubkey_hash(&hash); ssh_clean_pubkey_hash(&hash);
return -1; return -1;
} }
fprintf(stderr,"This new key will be written on disk for further usage. do you agree ?\n"); fprintf(stderr,"This new key will be written on disk for further usage. do you agree ?\n");
if (fgets(buf, sizeof(buf), stdin) == NULL) { if (fgets(buf, sizeof(buf), stdin) == NULL) {
ssh_clean_pubkey_hash(&hash); ssh_clean_pubkey_hash(&hash);
return -1; return -1;
} }
if(strncasecmp(buf,"yes",3)==0){ if(strncasecmp(buf,"yes",3)==0){
if (ssh_write_knownhost(session) < 0) { if (ssh_write_knownhost(session) < 0) {
ssh_clean_pubkey_hash(&hash); ssh_clean_pubkey_hash(&hash);
fprintf(stderr, "error %s\n", strerror(errno)); fprintf(stderr, "error %s\n", strerror(errno));
return -1; return -1;
}
} }
}
break; break;
case SSH_KNOWN_HOSTS_ERROR: case SSH_KNOWN_HOSTS_ERROR:
ssh_clean_pubkey_hash(&hash); ssh_clean_pubkey_hash(&hash);
fprintf(stderr,"%s",ssh_get_error(session)); fprintf(stderr,"%s",ssh_get_error(session));
return -1; return -1;
} case SSH_KNOWN_HOSTS_OK:
ssh_clean_pubkey_hash(&hash); break; /* ok */
return 0; }
ssh_clean_pubkey_hash(&hash);
return 0;
} }