]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
use getservbyport() to obtain the service name.
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Fri, 10 Feb 2012 10:11:02 +0000 (11:11 +0100)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Fri, 10 Feb 2012 10:11:02 +0000 (11:11 +0100)
src/cli.c
src/socket.c
src/socket.h

index c9c70309d9445932ce6a531e985da33755627a03..9eb2a6506c49712fc25320528f0499df6e57e476 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -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));
         }
index 3dc722b6487e9f62a4dff5abf9fb3575b08d50eb..8d922328208437c9ae1b4b50bbf367d391afff66 100644 (file)
@@ -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);
-}
index 4846465092a595604ea7272d3faec84f473f8c02..141bacb8295657628ad6d49f0f705ccec2e11415 100644 (file)
@@ -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);