freshclam/dns.c
3e92581e
 /*
  *  Copyright (C) 2004 Tomasz Kojm <tkojm@clamav.net>
  *
  *  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 *
 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;
     if (res_init () < 0)
     {
         logg ("^res_init failed\n");
         return NULL;
3e92581e
     }
 
6df88f29
     logg ("*Querying %s\n", domain);
efd68190
 
6df88f29
     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.  
          */
         memset (answer, 0, PACKETSZ);
         if (qtype == T_TXT)
             qtype = T_ANY;
         if ((len = res_query (domain, C_IN, qtype, answer, PACKETSZ)) < 0)
         {
             logg ("%cCan't query %s\n",
                   (qtype == T_TXT || qtype == T_ANY) ? '^' : '*', domain);
             return NULL;
         }
4ecce39f
 #else
6df88f29
         logg ("%cCan't query %s\n", (qtype == T_TXT) ? '^' : '*', domain);
         return NULL;
4ecce39f
 #endif
3e92581e
     }
6df88f29
     if (qtype != T_TXT && qtype != T_ANY)
     {
         if (ttl)
             *ttl = 2;
         return NULL;
a3a0367b
     }
3e92581e
 
f769e183
     answend = answer + len;
6df88f29
     pt = answer + sizeof (HEADER);
3e92581e
 
6df88f29
     if ((len = dn_expand (answer, answend, pt, host, sizeof (host))) < 0)
     {
         logg ("^dn_expand failed\n");
         return NULL;
3e92581e
     }
 
f769e183
     pt += len;
6df88f29
     if (pt > answend - 4)
     {
         logg ("^Bad (too short) DNS reply\n");
         return NULL;
f769e183
     }
3e92581e
 
6df88f29
     GETSHORT (type, pt);
     if (type != qtype)
     {
         logg ("^Broken DNS reply.\n");
         return NULL;
3e92581e
     }
 
6df88f29
     pt += INT16SZ;              /* class */
f769e183
     size = 0;
6df88f29
     do
     {                           /* recurse through CNAME rr's */
         pt += size;
         if ((len = dn_expand (answer, answend, pt, host, sizeof (host))) < 0)
         {
             logg ("^second dn_expand failed\n");
             return NULL;
         }
         pt += len;
         if (pt > answend - 10)
         {
             logg ("^Bad (too short) DNS reply\n");
             return NULL;
         }
         GETSHORT (type, pt);
         pt += INT16SZ;          /* class */
         GETLONG (cttl, pt);
         GETSHORT (size, pt);
         if (pt + size < answer || pt + size > answend)
         {
             logg ("^DNS rr overflow\n");
             return NULL;
         }
     }
     while (type == T_CNAME);
 
     if (type != T_TXT)
     {
         logg ("^Not a TXT record\n");
         return NULL;
3e92581e
     }
 
6df88f29
     if (!size || (txtlen = *pt) >= size || !txtlen)
     {
         logg ("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size);
         return NULL;
3e92581e
     }
 
6df88f29
     if (!(txt = (char *) malloc (txtlen + 1)))
         return NULL;
3e92581e
 
6df88f29
     memcpy (txt, pt + 1, txtlen);
3e92581e
     txt[txtlen] = 0;
6df88f29
     if (ttl)
         *ttl = cttl;
470168e6
 
     return txt;
 }
 
3e92581e
 #else
 
6df88f29
 char *
 dnsquery (const char *domain, int qtype, unsigned int *ttl)
3e92581e
 {
6df88f29
     if (ttl)
         *ttl = 1;               /* ttl of 1 combined with a NULL return distinguishes a failed lookup from DNS queries not being available */
3e92581e
     return NULL;
 }
 
 #endif