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> |
3e92581e |
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <sys/types.h>
|
a889f40e |
#include "shared/output.h"
|
3e92581e |
#ifndef PACKETSZ
#define PACKETSZ 512
#endif
char *txtquery(const char *domain, unsigned int *ttl)
{ |
f769e183 |
unsigned char answer[PACKETSZ], *answend, *pt; |
5a1034b9 |
char *txt, host[128]; |
f769e183 |
int len, type, qtype;
unsigned int cttl, size, txtlen = 0; |
3e92581e |
|
470168e6 |
*ttl = 0; |
3e92581e |
if(res_init() < 0) { |
0ae41a2d |
logg("^res_init failed\n"); |
3e92581e |
return NULL;
}
|
0ae41a2d |
logg("*Querying %s\n", domain); |
efd68190 |
|
3e92581e |
memset(answer, 0, PACKETSZ); |
4ecce39f |
qtype = T_TXT; |
f769e183 |
if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0 || len > PACKETSZ) { |
4ecce39f |
#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 |
0ae41a2d |
logg("^Can't query %s\n", domain); |
3e92581e |
return NULL; |
4ecce39f |
#endif |
3e92581e |
}
|
f769e183 |
answend = answer + len; |
3e92581e |
pt = answer + sizeof(HEADER);
|
f769e183 |
if((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) { |
0ae41a2d |
logg("^dn_expand failed\n"); |
3e92581e |
return NULL;
}
|
f769e183 |
pt += len;
if(pt > answend-4) {
logg("^Bad (too short) DNS reply\n");
return NULL;
} |
3e92581e |
GETSHORT(type, pt); |
4ecce39f |
if(type != qtype) { |
0ae41a2d |
logg("^Broken DNS reply.\n"); |
3e92581e |
return NULL;
}
pt += INT16SZ; /* class */ |
f769e183 |
size = 0;
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); |
93f9b2df |
if(pt + size < answer || pt + size > answend) { |
f769e183 |
logg("^DNS rr overflow\n");
return NULL;
}
} while(type == T_CNAME); |
3e92581e |
if(type != T_TXT) { |
0ae41a2d |
logg("^Not a TXT record\n"); |
3e92581e |
return NULL;
}
|
f769e183 |
if(!size || (txtlen = *pt) >= size || !txtlen) { |
0ae41a2d |
logg("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size); |
3e92581e |
return NULL;
}
|
c8e620d2 |
if(!(txt = (char *) malloc(txtlen + 1))) |
3e92581e |
return NULL;
|
f769e183 |
memcpy(txt, pt+1, txtlen); |
3e92581e |
txt[txtlen] = 0; |
470168e6 |
*ttl = cttl;
return txt;
}
#elif defined(C_WINDOWS)
/*
* Note: Needs to link with dnsapi.lib.
* The dll behind this library is available from Windows 2000 onward.
* Written by Mark Pizzolato
*/ |
f769e183 |
#include <winsock.h> |
470168e6 |
#include <string.h>
#include <windows.h>
#include <windns.h>
#include "shared/output.h"
char *txtquery(const char *domain, unsigned int *ttl)
{
PDNS_RECORD pDnsRecord;
char *txt = NULL;
*ttl = 0;
mprintf("*Querying %s\n", domain);
if(DnsQuery_UTF8(domain, DNS_TYPE_TEXT, DNS_QUERY_TREAT_AS_FQDN, NULL, &pDnsRecord, NULL) != 0)
return NULL;
|
7d129e7a |
if((pDnsRecord->Data.TXT.dwStringCount > 0) && pDnsRecord->Data.TXT.pStringArray[0]) { |
8ca8a18e |
txt = malloc(strlen(pDnsRecord->Data.TXT.pStringArray[0]) + 1); |
470168e6 |
if(txt)
strcpy(txt, pDnsRecord->Data.TXT.pStringArray[0]);
*ttl = pDnsRecord->dwTtl;
}
DnsRecordListFree(pDnsRecord, DnsFreeRecordList); |
3e92581e |
return txt;
}
#else
char *txtquery(const char *domain, unsigned int *ttl)
{ |
470168e6 |
*ttl = 1; /* ttl of 1 combined with a NULL return distinguishes a failed lookup from DNS queries not being available */ |
3e92581e |
return NULL;
}
#endif |