summaryrefslogtreecommitdiff
path: root/src/dnstlsa.c
blob: 9871fff041e78b072bd33af42b142f50e0ccf0c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <unistd.h>
#include "buffer.h"
#include "stralloc.h"
#include "dnsresolv.h"
#include "dns.h"
#include "exit.h"
#include "logmsg.h"
#include "getoptb.h"
#include "str.h"
#include "byte.h"

#define WHO "dnstlsa"

static stralloc cn = {0};
static stralloc sa = {0};
static stralloc out = {0};

int main(int argc,char **argv)
{
  int r;
  uint16 usage;
  uint16 selector;
  uint16 type;
  char *port = "25";
  char proto[7] = "._tcp.";
  char *host;
  unsigned char ch;
  int opt;
  int i, j, k;
  int verbose = 0;

  if (!argv[1]) 
    logmsg(WHO,100,USAGE,"dnstlsa [-v] [-p port] [-u(dp)|-t(cp)] host (tcp on port 25 is default)" );

  while ((opt = getopt(argc,argv,"vutp:")) != opteof) {
    switch (opt) {
      case 'p': port = optarg; break;
      case 't': break;
      case 'u': str_copy(proto,"._udp."); break;
      case 'v': verbose = 1;
    }
  }
  if (optind < argc)
  host = argv[optind++];

  if (!stralloc_copyb(&sa, "_",1)) logmsg(WHO,111,FATAL,"out of memory");
  if (!stralloc_cats(&sa,port)) logmsg(WHO,111,FATAL,"out of memory");
  if (!stralloc_cats(&sa,proto)) logmsg(WHO,111,FATAL,"out of memory");
  if (!stralloc_cats(&sa,host)) logmsg(WHO,111,FATAL,"out of memory");

  DNS_INIT
  if (dns_cname(&cn,&sa) > 0)
    { if ((r = dns_tlsa(&out,&cn)) < 0) _exit(1); }
	else
    if ((r = dns_tlsa(&out,&sa)) < 0) _exit(1);
  if (!stralloc_0(&sa)) logmsg(WHO,111,FATAL,"out of memory");
  if (verbose) logmsg(WHO,0,INFO,B("checking for TLSA records: ",sa.s,"\n"));

  if (r > 0 && out.len > 4) {
    for (i = 0; i <= out.len; i++) {
      usage = (unsigned char) out.s[i];
      selector = (unsigned char) out.s[i + 1];
      type = (unsigned char) out.s[i + 2];

      if (usage == 0) buffer_puts(buffer_1,"Usage: [0], ");
      if (usage == 1) buffer_puts(buffer_1,"Usage: [1], ");
      if (usage == 2) buffer_puts(buffer_1,"Usage: [2], ");
      if (usage == 3) buffer_puts(buffer_1,"Usage: [3], ");

      if (selector == 0) buffer_puts(buffer_1,"Selector: [0], ");
      if (selector == 1) buffer_puts(buffer_1,"Selector: [1], ");

      if (type == 0) buffer_puts(buffer_1,"Type: [0] ");  // full cert
      if (type == 1) buffer_puts(buffer_1,"Type: [1] ");  // sha256
      if (type == 2) buffer_puts(buffer_1,"Type: [2] ");  // sha512

      /* Staff of Ra
         "(is) six kadams high." However, the builder (h)as 
         to subtract one kadam out of respect for the Hebrew God. */

      for (j = i + 3, k = 0; j <= out.len; ++j) {
        ch = (unsigned char) out.s[j];
        if ((type == 1 && k == 32) || (type == 2 && k == 64)) {
          buffer_putsflush(buffer_1,"\n");
          i = j - 1;  break;
        } else  {
          buffer_put(buffer_1,"0123456789abcdef" + (ch >> 4),1);
          buffer_put(buffer_1,"0123456789abcdef" + (ch & 0x0f),1);
          k++;
        }
      }
    }
  }
 
  _exit(0);
}