]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
danetool: Allow specifying a service name into port option
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 18 Apr 2016 14:03:37 +0000 (16:03 +0200)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 18 Apr 2016 14:25:27 +0000 (16:25 +0200)
This makes the tool similar to gnutls-cli.

src/danetool-args.def
src/danetool.c
src/socket.c

index 0f55023fad23d3a6fdac0d55e6e07adaf4c983b4..b31e298a269756ec109be93aa7cbe289dc509761 100644 (file)
@@ -84,9 +84,9 @@ flag = {
 
 flag = {
     name      = port;
-    arg-type  = number;
-    descrip   = "Specify the port number for the DANE data";
-    default-value = 443;
+    arg-type  = string;
+    descrip   = "The port or service to connect to, for DANE data";
+    default-value = "443";
     doc      = "";
 };
 
index 49f0b2029e9788d87d749ff287e7cfbde4880af0..bea7bcf74162ce7fe3fa52921d4a6e45b4c6d644 100644 (file)
 #include "certtool-common.h"
 #include "socket.h"
 
-static const char* obtain_cert(const char *hostname, const char *proto, unsigned int port,
+static const char *obtain_cert(const char *hostname, const char *proto, const char *service,
                                const char *app_proto, unsigned quiet);
 static void cmd_parser(int argc, char **argv);
 static void dane_info(const char *host, const char *proto,
-                     unsigned int port, unsigned int ca,
+                     const char *service, unsigned int ca,
                      unsigned int domain, common_info_st * cinfo);
 
 static void dane_check(const char *host, const char *proto,
-                      unsigned int port, common_info_st * cinfo);
+                      const char *service, common_info_st * cinfo);
 
 FILE *outfile;
 static gnutls_digest_algorithm_t default_dig;
@@ -90,7 +90,7 @@ static void cmd_parser(int argc, char **argv)
        int ret, privkey_op = 0;
        common_info_st cinfo;
        const char *proto = "tcp";
-       unsigned int port = 443;
+       const char *service = "443";
 
        optionProcess(&danetoolOptions, argc, argv);
 
@@ -159,19 +159,20 @@ static void cmd_parser(int argc, char **argv)
                cinfo.cert = OPT_ARG(LOAD_CERTIFICATE);
 
        if (HAVE_OPT(PORT)) {
-               port = OPT_VALUE_PORT;
+               service = OPT_ARG(PORT);
        } else {
                if (HAVE_OPT(STARTTLS_PROTO))
-                       port = starttls_proto_to_port(OPT_ARG(STARTTLS_PROTO));
+                       service = starttls_proto_to_service(OPT_ARG(STARTTLS_PROTO));
        }
+
        if (HAVE_OPT(PROTO))
                proto = OPT_ARG(PROTO);
 
        if (HAVE_OPT(TLSA_RR))
-               dane_info(OPT_ARG(HOST), proto, port,
+               dane_info(OPT_ARG(HOST), proto, service,
                          HAVE_OPT(CA), ENABLED_OPT(DOMAIN), &cinfo);
        else if (HAVE_OPT(CHECK))
-               dane_check(OPT_ARG(CHECK), proto, port, &cinfo);
+               dane_check(OPT_ARG(CHECK), proto, service, &cinfo);
        else
                USAGE(1);
 
@@ -185,7 +186,7 @@ static void cmd_parser(int argc, char **argv)
 
 #define MAX_CLIST_SIZE 32
 static void dane_check(const char *host, const char *proto,
-                      unsigned int port, common_info_st * cinfo)
+                      const char *service, common_info_st * cinfo)
 {
 #ifdef HAVE_DANE
        dane_state_t s;
@@ -203,6 +204,7 @@ static void dane_check(const char *host, const char *proto,
        gnutls_x509_crt_t *clist = NULL;
        unsigned int clist_size = 0;
        gnutls_datum_t certs[MAX_CLIST_SIZE];
+       int port = service_to_port(service, proto);
 
        if (ENABLED_OPT(LOCAL_DNS))
                flags = 0;
@@ -221,7 +223,7 @@ static void dane_check(const char *host, const char *proto,
                if (HAVE_OPT(APP_PROTO))
                        app_proto = OPT_ARG(APP_PROTO);
 
-               cinfo->cert = obtain_cert(host, proto, port, app_proto, HAVE_OPT(QUIET));
+               cinfo->cert = obtain_cert(host, proto, service, app_proto, HAVE_OPT(QUIET));
                del = 1;
        }
 
@@ -444,7 +446,7 @@ static void dane_check(const char *host, const char *proto,
 }
 
 static void dane_info(const char *host, const char *proto,
-                     unsigned int port, unsigned int ca,
+                     const char *service, unsigned int ca,
                      unsigned int domain, common_info_st * cinfo)
 {
        gnutls_pubkey_t pubkey;
@@ -454,11 +456,10 @@ static void dane_info(const char *host, const char *proto,
        int ret;
        unsigned int usage, selector, type;
        size_t size;
+       int port = service_to_port(service, proto);
 
        if (proto == NULL)
                proto = "tcp";
-       if (port == 0)
-               port = 443;
 
        crt = load_cert(0, cinfo);
        if (crt != NULL && HAVE_OPT(X509)) {
@@ -660,16 +661,15 @@ static int get_cert(socket_st *hd, const char *hostname, unsigned udp, int fd)
        return 0;
 }
 
-static const char *obtain_cert(const char *hostname, const char *proto, unsigned port,
+static const char *obtain_cert(const char *hostname, const char *proto, const char *service,
                                const char *app_proto, unsigned quiet)
 {
        socket_st hd;
-       char txt_port[16];
+       const char *txt_service;
        unsigned udp = 0;
        static char tmpfile[32];
        int fd, ret;
        const char *str = "Obtaining certificate from";
-       const char *service;
 
        if (strcmp(proto, "udp") == 0)
                udp = 1;
@@ -681,14 +681,13 @@ static const char *obtain_cert(const char *hostname, const char *proto, unsigned
        strcpy(tmpfile, "danetool-certXXXXXX");
 
        sockets_init();
-       snprintf(txt_port, sizeof(txt_port), "%u", port);
+       txt_service = port_to_service(service, proto);
 
        if (quiet)
                str = NULL;
-       service = port_to_service(txt_port, proto);
-       socket_open(&hd, hostname, service, udp, str);
+       socket_open(&hd, hostname, txt_service, udp, str);
 
-       if (app_proto == NULL) app_proto = service;
+       if (app_proto == NULL) app_proto = txt_service;
        socket_starttls(&hd, app_proto);
 
        umask(066);
index e8eb315841409d170f3e85936b04397c1aaecdbb..24be51a93fb0ba6cb3f564bfb6cbb26ed5f97463 100644 (file)
@@ -338,7 +338,7 @@ socket_open(socket_st * hd, const char *hostname, const char *service,
 #endif
 
        if (msg != NULL)
-               printf("Resolving '%s'...\n", a_hostname);
+               printf("Resolving '%s:%s'...\n", a_hostname,service);
 
        /* get server name */
        memset(&hints, 0, sizeof(hints));
@@ -472,7 +472,7 @@ int service_to_port(const char *service, const char *proto)
 
        sr = getservbyname(service, proto);
        if (sr == NULL) {
-               fprintf(stderr, "Warning: getservbyname() failed.\n");
+               fprintf(stderr, "Warning: getservbyname() failed for '%s/%s'.\n", service, proto);
                exit(1);
        }