libfreshclam/dns.c
3e92581e
 /*
e1cbc270
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
  *  Copyright (C) 2004-2007 Tomasz Kojm <tkojm@clamav.net>2004 Tomasz Kojm <tkojm@clamav.net>
3e92581e
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
3e92581e
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
f5e4b981
 #include <stdio.h>
 
5cd3f734
 #include "dns.h"
3e92581e
 #ifdef HAVE_RESOLV_H
 
 #include <string.h>
b22aef42
 #include <sys/types.h>
46bd6382
 #ifndef _WIN32
3e92581e
 #include <netinet/in.h>
 #include <arpa/nameser.h>
46bd6382
 #endif
3e92581e
 #include <resolv.h>
 
a889f40e
 #include "shared/output.h"
 
3e92581e
 #ifndef PACKETSZ
 #define PACKETSZ 512
 #endif
 
6df88f29
 char *
288057e9
 dnsquery(const char *domain, int qtype, unsigned int *ttl)
3e92581e
 {
6df88f29
     unsigned char answer[PACKETSZ], *answend, *pt;
     char *txt, host[128];
     int len, type;
     unsigned int cttl, size, txtlen = 0;
 
     if (ttl)
         *ttl = 0;
288057e9
     if (res_init() < 0) {
         logg("^res_init failed\n");
6df88f29
         return NULL;
3e92581e
     }
 
288057e9
     logg("*Querying %s\n", domain);
efd68190
 
288057e9
     memset(answer, 0, PACKETSZ);
     if ((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0 || len > PACKETSZ) {
4ecce39f
 #ifdef FRESHCLAM_DNS_FIX
6df88f29
         /*  The DNS server in the SpeedTouch Alcatel 510 modem can't
          *  handle a TXT-query, but it can resolve an ANY-query to a
          *  TXT-record, so we try an ANY-query now.  The thing we try
          *  to resolve normally only has a TXT-record anyway.  
          */
288057e9
         memset(answer, 0, PACKETSZ);
6df88f29
         if (qtype == T_TXT)
             qtype = T_ANY;
288057e9
         if ((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0) {
             logg("%cCan't query %s\n",
                  (qtype == T_TXT || qtype == T_ANY) ? '^' : '*', domain);
6df88f29
             return NULL;
         }
4ecce39f
 #else
288057e9
         logg("%cCan't query %s\n", (qtype == T_TXT) ? '^' : '*', domain);
6df88f29
         return NULL;
4ecce39f
 #endif
3e92581e
     }
288057e9
     if (qtype != T_TXT && qtype != T_ANY) {
6df88f29
         if (ttl)
             *ttl = 2;
         return NULL;
a3a0367b
     }
3e92581e
 
f769e183
     answend = answer + len;
288057e9
     pt      = answer + sizeof(HEADER);
3e92581e
 
288057e9
     if ((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
         logg("^dn_expand failed\n");
6df88f29
         return NULL;
3e92581e
     }
 
f769e183
     pt += len;
288057e9
     if (pt > answend - 4) {
         logg("^Bad (too short) DNS reply\n");
6df88f29
         return NULL;
f769e183
     }
3e92581e
 
288057e9
     GETSHORT(type, pt);
     if (type != qtype) {
         logg("^Broken DNS reply.\n");
6df88f29
         return NULL;
3e92581e
     }
 
288057e9
     pt += INT16SZ; /* class */
f769e183
     size = 0;
288057e9
     do { /* recurse through CNAME rr's */
6df88f29
         pt += size;
288057e9
         if ((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
             logg("^second dn_expand failed\n");
6df88f29
             return NULL;
         }
         pt += len;
288057e9
         if (pt > answend - 10) {
             logg("^Bad (too short) DNS reply\n");
6df88f29
             return NULL;
         }
288057e9
         GETSHORT(type, pt);
         pt += INT16SZ; /* class */
         GETLONG(cttl, pt);
         GETSHORT(size, pt);
         if (pt + size < answer || pt + size > answend) {
             logg("^DNS rr overflow\n");
6df88f29
             return NULL;
         }
288057e9
     } while (type == T_CNAME);
6df88f29
 
288057e9
     if (type != T_TXT) {
         logg("^Not a TXT record\n");
6df88f29
         return NULL;
3e92581e
     }
 
288057e9
     if (!size || (txtlen = *pt) >= size || !txtlen) {
         logg("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size);
6df88f29
         return NULL;
3e92581e
     }
 
288057e9
     if (!(txt = (char *)malloc(txtlen + 1)))
6df88f29
         return NULL;
3e92581e
 
288057e9
     memcpy(txt, pt + 1, txtlen);
3e92581e
     txt[txtlen] = 0;
6df88f29
     if (ttl)
         *ttl = cttl;
470168e6
 
     return txt;
 }
 
3e92581e
 #else
 
6df88f29
 char *
288057e9
 dnsquery(const char *domain, int qtype, unsigned int *ttl)
3e92581e
 {
6df88f29
     if (ttl)
288057e9
         *ttl = 1; /* ttl of 1 combined with a NULL return distinguishes a failed lookup from DNS queries not being available */
3e92581e
     return NULL;
 }
 
 #endif