freshclam/dns.c
881069d7
 /*
  *  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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
881069d7
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
ced75eaf
 #include <stdio.h>
 
881069d7
 #ifdef HAVE_RESOLV_H
 
 #include <string.h>
67bc1b48
 #include <sys/types.h>
881069d7
 #include <netinet/in.h>
 #include <arpa/nameser.h>
 #include <resolv.h>
 #include <sys/types.h>
 
a43f5fa4
 #include "shared/memory.h"
 #include "shared/output.h"
 
b0674c6e
 #include "dns.h"
881069d7
 
 #ifndef PACKETSZ
 #define PACKETSZ 512
 #endif
 
 char *txtquery(const char *domain, unsigned int *ttl)
 {
 	unsigned char answer[PACKETSZ], host[128], *pt, *txt;
5472004a
 	int len, exp, cttl, size, txtlen, type, qtype;
881069d7
 
 
     if(res_init() < 0) {
f1c4563e
 	logg("^res_init failed\n");
881069d7
 	return NULL;
     }
 
f1c4563e
     logg("*Querying %s\n", domain);
e1b40b1b
 
881069d7
     memset(answer, 0, PACKETSZ);
5472004a
     qtype = T_TXT;
     if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0) {
 #ifdef FRESHCLAM_DNS_FIX
 	/*  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);
 	qtype=T_ANY;
 	if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0) {
 	    logg("^Can't query %s\n", domain);
 	    return NULL;
 	}
 #else
f1c4563e
 	logg("^Can't query %s\n", domain);
881069d7
 	return NULL;
5472004a
 #endif
881069d7
     }
 
     pt = answer + sizeof(HEADER);
 
     if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0) {
f1c4563e
 	logg("^dn_expand failed\n");
881069d7
 	return NULL;
     }
 
     pt += exp;
 
     GETSHORT(type, pt);
5472004a
     if(type != qtype) {
f1c4563e
 	logg("^Broken DNS reply.\n");
881069d7
 	return NULL;
     }
 
     pt += INT16SZ; /* class */
 
     if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0) {
f1c4563e
 	logg("^second dn_expand failed\n");
881069d7
 	return NULL;
     }
 
     pt += exp;
     GETSHORT(type, pt);
     if(type != T_TXT) {
f1c4563e
 	logg("^Not a TXT record\n");
881069d7
 	return NULL;
     }
 
     pt += INT16SZ; /* class */
     GETLONG(cttl, pt);
     *ttl = cttl;
     GETSHORT(size, pt);
     txtlen = *pt;
 
     if(txtlen >= size || !txtlen) {
f1c4563e
 	logg("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size);
881069d7
 	return NULL;
     }
 
     if(!(txt = mmalloc(txtlen + 1)))
 	return NULL;
 
     pt++;
     strncpy(txt, pt, txtlen);
     txt[txtlen] = 0;
 
     return txt;
 }
 
 #else
 
 char *txtquery(const char *domain, unsigned int *ttl)
 {
     return NULL;
 }
 
 #endif