diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-02-10 11:11:02 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-02-10 11:11:02 +0100 |
commit | 38f9998e9aaee0e8a486cad3f715fad4558a88df (patch) | |
tree | aba08b0d8f415d8b7e6e27f6ac29aec2595f30b3 | |
parent | a2768dd44037e594ca1b52c0261bad4b88a857ee (diff) | |
download | gnutls-38f9998e9aaee0e8a486cad3f715fad4558a88df.tar.gz |
use getservbyport() to obtain the service name.
-rw-r--r-- | src/cli.c | 34 | ||||
-rw-r--r-- | src/socket.c | 25 | ||||
-rw-r--r-- | src/socket.h | 1 |
3 files changed, 31 insertions, 29 deletions
@@ -35,6 +35,7 @@ #include <unistd.h> #include <stdint.h> #include <fcntl.h> +#include <netdb.h> #include <gnutls/gnutls.h> #include <gnutls/abstract.h> @@ -446,15 +447,41 @@ read_yesno (const char *input_str) return 0; } +/* converts a textual service or port to + * a service. + */ +static const char* port_to_service(const char* sport) +{ +unsigned int port; +struct servent * sr; + + port = atoi(sport); + if (port == 0) return sport; + + port = htons(port); + + sr = getservbyport(port, udp?"udp":"tcp"); + if (sr == NULL) + { + fprintf(stderr, "Warning: getservbyport() failed. Using port number as service.\n"); + return sport; + } + + return sr->s_name; +} + static int cert_verify_callback (gnutls_session_t session) { int rc; unsigned int status = 0; int ssh = ENABLED_OPT(SSH); + const char* txt_service; if (!x509_cafile && !pgp_keyring) return 0; + + txt_service = port_to_service(service); rc = cert_verify(session, hostname); if (rc == 0) @@ -488,12 +515,12 @@ cert_verify_callback (gnutls_session_t session) return -1; } - rc = gnutls_verify_stored_pubkey(NULL, hostname, service, GNUTLS_CRT_X509, + rc = gnutls_verify_stored_pubkey(NULL, hostname, txt_service, GNUTLS_CRT_X509, cert, 0); if (rc == GNUTLS_E_NO_CERTIFICATE_FOUND) { print_cert_info_compact(session); - fprintf(stderr, "Host %s has never been contacted before.\n", hostname); + fprintf(stderr, "Host %s (%s) has never been contacted before.\n", hostname, txt_service); if (status == 0) fprintf(stderr, "Its certificate is valid for %s.\n", hostname); @@ -521,7 +548,8 @@ cert_verify_callback (gnutls_session_t session) if (rc != 0) { - rc = gnutls_store_pubkey(NULL, hostname, service, GNUTLS_CRT_X509, cert, 0, 0); + rc = gnutls_store_pubkey(NULL, hostname, txt_service, GNUTLS_CRT_X509, + cert, 0, 0); if (rc < 0) fprintf(stderr, "Could not store key: %s\n", gnutls_strerror(rc)); } diff --git a/src/socket.c b/src/socket.c index 3dc722b648..8d92232820 100644 --- a/src/socket.c +++ b/src/socket.c @@ -223,28 +223,3 @@ sockets_init (void) #endif } - -/* converts a service name or a port (in string) to a - * port number. The protocol is assumed to be TCP. - * - * returns -1 on error; - */ -int -service_to_port (const char *service) -{ - int port; - struct servent *server_port; - - port = atoi (service); - if (port != 0) - return port; - - server_port = getservbyname (service, "tcp"); - if (server_port == NULL) - { - perror ("getservbyname()"); - return (-1); - } - - return ntohs (server_port->s_port); -} diff --git a/src/socket.h b/src/socket.h index 4846465092..141bacb829 100644 --- a/src/socket.h +++ b/src/socket.h @@ -20,4 +20,3 @@ void socket_connect (const socket_st * hd); void socket_bye (socket_st * socket); void sockets_init (void); -int service_to_port (const char *service); |