New ssh_get_local_hostname()

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Pavol Žáčik <pzacik@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2026-01-27 21:38:57 +01:00
parent ce0b616bc6
commit 59ed66b684
6 changed files with 38 additions and 23 deletions

View File

@@ -45,6 +45,7 @@ extern "C" {
/* gets the user home dir. */ /* gets the user home dir. */
char *ssh_get_user_home_dir(ssh_session session); char *ssh_get_user_home_dir(ssh_session session);
char *ssh_get_local_username(void); char *ssh_get_local_username(void);
char *ssh_get_local_hostname(void);
int ssh_file_readaccess_ok(const char *file); int ssh_file_readaccess_ok(const char *file);
int ssh_dir_writeable(const char *path); int ssh_dir_writeable(const char *path);

View File

@@ -198,7 +198,7 @@ int
ssh_gssapi_handle_userauth(ssh_session session, const char *user, ssh_gssapi_handle_userauth(ssh_session session, const char *user,
uint32_t n_oid, ssh_string *oids) uint32_t n_oid, ssh_string *oids)
{ {
char hostname[NI_MAXHOST] = {0}; char *hostname = NULL;
OM_uint32 maj_stat, min_stat; OM_uint32 maj_stat, min_stat;
size_t i; size_t i;
gss_OID_set supported; /* oids supported by server */ gss_OID_set supported; /* oids supported by server */
@@ -210,14 +210,6 @@ ssh_gssapi_handle_userauth(ssh_session session, const char *user,
int rc; int rc;
char err_msg[SSH_ERRNO_MSG_MAX] = {0}; char err_msg[SSH_ERRNO_MSG_MAX] = {0};
rc = gethostname(hostname, 64);
if (rc != 0) {
SSH_LOG(SSH_LOG_TRACE,
"Error getting hostname: %s",
ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
return SSH_ERROR;
}
/* Destroy earlier GSSAPI context if any */ /* Destroy earlier GSSAPI context if any */
ssh_gssapi_free(session); ssh_gssapi_free(session);
rc = ssh_gssapi_init(session); rc = ssh_gssapi_init(session);
@@ -284,7 +276,16 @@ ssh_gssapi_handle_userauth(ssh_session session, const char *user,
return SSH_OK; return SSH_OK;
} }
hostname = ssh_get_local_hostname();
if (hostname == NULL) {
SSH_LOG(SSH_LOG_TRACE,
"Error getting hostname: %s",
ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
return SSH_ERROR;
}
rc = ssh_gssapi_import_name(session->gssapi, hostname); rc = ssh_gssapi_import_name(session->gssapi, hostname);
SAFE_FREE(hostname);
if (rc != SSH_OK) { if (rc != SSH_OK) {
ssh_auth_reply_default(session, 0); ssh_auth_reply_default(session, 0);
gss_release_oid_set(&min_stat, &both_supported); gss_release_oid_set(&min_stat, &both_supported);

View File

@@ -421,7 +421,7 @@ int ssh_server_gss_kex_process_init(ssh_session session, ssh_buffer packet)
gss_name_t client_name = GSS_C_NO_NAME; gss_name_t client_name = GSS_C_NO_NAME;
OM_uint32 ret_flags = 0; OM_uint32 ret_flags = 0;
gss_buffer_desc mic = GSS_C_EMPTY_BUFFER, msg = GSS_C_EMPTY_BUFFER; gss_buffer_desc mic = GSS_C_EMPTY_BUFFER, msg = GSS_C_EMPTY_BUFFER;
char hostname[NI_MAXHOST] = {0}; char *hostname = NULL;
char err_msg[SSH_ERRNO_MSG_MAX] = {0}; char err_msg[SSH_ERRNO_MSG_MAX] = {0};
rc = ssh_buffer_unpack(packet, "S", &otoken); rc = ssh_buffer_unpack(packet, "S", &otoken);
@@ -538,8 +538,8 @@ int ssh_server_gss_kex_process_init(ssh_session session, ssh_buffer packet)
goto error; goto error;
} }
rc = gethostname(hostname, 64); hostname = ssh_get_local_hostname();
if (rc != 0) { if (hostname == NULL) {
SSH_LOG(SSH_LOG_TRACE, SSH_LOG(SSH_LOG_TRACE,
"Error getting hostname: %s", "Error getting hostname: %s",
ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX)); ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
@@ -547,6 +547,7 @@ int ssh_server_gss_kex_process_init(ssh_session session, ssh_buffer packet)
} }
rc = ssh_gssapi_import_name(session->gssapi, hostname); rc = ssh_gssapi_import_name(session->gssapi, hostname);
SAFE_FREE(hostname);
if (rc != SSH_OK) { if (rc != SSH_OK) {
goto error; goto error;
} }

View File

@@ -615,10 +615,10 @@ int ssh_publickey_to_file(ssh_session session,
FILE *fp = NULL; FILE *fp = NULL;
char *user = NULL; char *user = NULL;
char buffer[1024]; char buffer[1024];
char host[256]; char *host = NULL;
unsigned char *pubkey_64 = NULL; unsigned char *pubkey_64 = NULL;
size_t len; size_t len;
int rc;
if(session==NULL) if(session==NULL)
return SSH_ERROR; return SSH_ERROR;
if(file==NULL || pubkey==NULL){ if(file==NULL || pubkey==NULL){
@@ -636,8 +636,8 @@ int ssh_publickey_to_file(ssh_session session,
return SSH_ERROR; return SSH_ERROR;
} }
rc = gethostname(host, sizeof(host)); host = ssh_get_local_hostname();
if (rc < 0) { if (host == NULL) {
SAFE_FREE(user); SAFE_FREE(user);
SAFE_FREE(pubkey_64); SAFE_FREE(pubkey_64);
return SSH_ERROR; return SSH_ERROR;
@@ -651,6 +651,7 @@ int ssh_publickey_to_file(ssh_session session,
SAFE_FREE(pubkey_64); SAFE_FREE(pubkey_64);
SAFE_FREE(user); SAFE_FREE(user);
SAFE_FREE(host);
SSH_LOG(SSH_LOG_RARE, "Trying to write public key file: %s", file); SSH_LOG(SSH_LOG_RARE, "Trying to write public key file: %s", file);
SSH_LOG(SSH_LOG_PACKET, "public key file content: %s", buffer); SSH_LOG(SSH_LOG_PACKET, "public key file content: %s", buffer);

View File

@@ -1233,6 +1233,18 @@ char *ssh_path_expand_tilde(const char *d)
return r; return r;
} }
char *ssh_get_local_hostname(void)
{
char host[NI_MAXHOST] = {0};
int rc;
rc = gethostname(host, sizeof(host));
if (rc != 0) {
return NULL;
}
return strdup(host);
}
/** @internal /** @internal
* @brief expands a string in function of session options * @brief expands a string in function of session options
* @param[in] s Format string to expand. Known parameters: * @param[in] s Format string to expand. Known parameters:
@@ -1249,7 +1261,6 @@ char *ssh_path_expand_tilde(const char *d)
*/ */
char *ssh_path_expand_escape(ssh_session session, const char *s) char *ssh_path_expand_escape(ssh_session session, const char *s)
{ {
char host[NI_MAXHOST] = {0};
char *buf = NULL; char *buf = NULL;
char *r = NULL; char *r = NULL;
char *x = NULL; char *x = NULL;
@@ -1313,9 +1324,7 @@ char *ssh_path_expand_escape(ssh_session session, const char *s)
x = ssh_get_local_username(); x = ssh_get_local_username();
break; break;
case 'l': case 'l':
if (gethostname(host, sizeof(host) == 0)) { x = ssh_get_local_hostname();
x = strdup(host);
}
break; break;
case 'h': case 'h':
if (session->opts.host) { if (session->opts.host) {

View File

@@ -2684,7 +2684,7 @@ int ssh_pki_export_pubkey_file(const ssh_key key,
const char *filename) const char *filename)
{ {
char key_buf[MAX_LINE_SIZE]; char key_buf[MAX_LINE_SIZE];
char host[256]; char *host = NULL;
char *b64_key = NULL; char *b64_key = NULL;
char *user = NULL; char *user = NULL;
FILE *fp = NULL; FILE *fp = NULL;
@@ -2699,8 +2699,8 @@ int ssh_pki_export_pubkey_file(const ssh_key key,
return SSH_ERROR; return SSH_ERROR;
} }
rc = gethostname(host, sizeof(host)); host = ssh_get_local_hostname();
if (rc < 0) { if (host == NULL) {
free(user); free(user);
return SSH_ERROR; return SSH_ERROR;
} }
@@ -2708,6 +2708,7 @@ int ssh_pki_export_pubkey_file(const ssh_key key,
rc = ssh_pki_export_pubkey_base64(key, &b64_key); rc = ssh_pki_export_pubkey_base64(key, &b64_key);
if (rc < 0) { if (rc < 0) {
free(user); free(user);
free(host);
return SSH_ERROR; return SSH_ERROR;
} }
@@ -2718,6 +2719,7 @@ int ssh_pki_export_pubkey_file(const ssh_key key,
user, user,
host); host);
free(user); free(user);
free(host);
free(b64_key); free(b64_key);
if (rc < 0) { if (rc < 0) {
return SSH_ERROR; return SSH_ERROR;