#include "includes.h"
#define WINS_LIST "wins.dat"
#define WINS_VERSION 1
extern struct in_addr ipzero;
static void wins_hook(char *operation, struct name_record *namerec, int ttl)
{
pstring command;
char *cmd = lp_wins_hook();
char *p;
int i;
if (!cmd || !*cmd) return;
for (p=namerec->name.name; *p; p++) {
if (!(isalnum((int)*p) || strchr("._-",*p))) {
DEBUG(3,("not calling wins hook for invalid name %s\n", nmb_namestr(&namerec->name)));
return;
}
}
p = command;
p += slprintf(p, sizeof(command)-1, "%s %s %s %02x %d",
cmd,
operation,
namerec->name.name,
namerec->name.name_type,
ttl);
for (i=0;i<namerec->data.num_ips;i++) {
p += slprintf(p, sizeof(command) - (p-command) -1, " %s", inet_ntoa(namerec->data.ip[i]));
}
DEBUG(3,("calling wins hook for %s\n", nmb_namestr(&namerec->name)));
smbrun(command, NULL);
}
static unsigned wins_hash(void)
{
int i;
unsigned ret = iface_hash();
extern char **my_netbios_names;
for (i=0;my_netbios_names[i];i++)
ret ^= str_checksum(my_netbios_names[i]);
ret ^= str_checksum(lp_workgroup());
return ret;
}
BOOL packet_is_for_wins_server(struct packet_struct *packet)
{
struct nmb_packet *nmb = &packet->packet.nmb;
if((wins_server_subnet == NULL) || (nmb->header.nm_flags.bcast == True))
{
DEBUG(10, ("packet_is_for_wins_server: failing WINS test #1.\n"));
return False;
}
if (nmb->question.question_type != QUESTION_TYPE_NB_QUERY)
return False;
switch(nmb->header.opcode)
{
case NMB_WACK_OPCODE:
DEBUG(10, ("packet_is_for_wins_server: failing WINS test #2 (WACK).\n"));
return False;
case NMB_NAME_REG_OPCODE:
case NMB_NAME_MULTIHOMED_REG_OPCODE:
case NMB_NAME_REFRESH_OPCODE_8:
case NMB_NAME_REFRESH_OPCODE_9:
if(nmb->header.response)
{
DEBUG(10, ("packet_is_for_wins_server: failing WINS test #3 (response = 1).\n"));
return False;
}
break;
case NMB_NAME_RELEASE_OPCODE:
if(nmb->header.response)
{
DEBUG(10, ("packet_is_for_wins_server: failing WINS test #4 (response = 1).\n"));
return False;
}
break;
case NMB_NAME_QUERY_OPCODE:
if(!nmb->header.response && !nmb->header.nm_flags.recursion_desired)
{
DEBUG(10, ("packet_is_for_wins_server: failing WINS test #5 (response = 1).\n"));
return False;
}
break;
}
return True;
}
static int get_ttl_from_packet(struct nmb_packet *nmb)
{
int ttl = nmb->additional->ttl;
if(ttl < lp_min_wins_ttl() )
ttl = lp_min_wins_ttl();
if(ttl > lp_max_wins_ttl() )
ttl = lp_max_wins_ttl();
return ttl;
}
BOOL initialise_wins(void)
{
time_t time_now = time(NULL);
FILE *fp;
pstring line;
if(!lp_we_are_a_wins_server())
return True;
add_samba_names_to_subnet(wins_server_subnet);
if((fp = sys_fopen(lock_path(WINS_LIST),"r")) == NULL)
{
DEBUG(2,("initialise_wins: Can't open wins database file %s. Error was %s\n",
WINS_LIST, strerror(errno) ));
return True;
}
while (!feof(fp))
{
pstring name_str, ip_str, ttl_str, nb_flags_str;
unsigned int num_ips;
pstring name;
struct in_addr *ip_list;
int type = 0;
int nb_flags;
int ttl;
char *ptr;
char *p;
BOOL got_token;
BOOL was_ip;
int i;
unsigned hash;
int version;
if (!fgets_slash(line,sizeof(pstring),fp))
continue;
if (*line == '#')
continue;
if (strncmp(line,"VERSION ", 8) == 0) {
if (sscanf(line,"VERSION %d %u", &version, &hash) != 2 ||
version != WINS_VERSION ||
hash != wins_hash()) {
DEBUG(0,("Discarding invalid wins.dat file [%s]\n",line));
fclose(fp);
return True;
}
continue;
}
ptr = line;
if (!next_token(&ptr,name_str,NULL,sizeof(name_str)))
{
DEBUG(0,("initialise_wins: Failed to parse name when parsing line %s\n", line ));
continue;
}
if (!next_token(&ptr,ttl_str,NULL,sizeof(ttl_str)))
{
DEBUG(0,("initialise_wins: Failed to parse time to live when parsing line %s\n", line ));
continue;
}
num_ips = 0;
do
{
got_token = next_token(&ptr,ip_str,NULL,sizeof(ip_str));
was_ip = False;
if(got_token && strchr(ip_str, '.'))
{
num_ips++;
was_ip = True;
}
} while( got_token && was_ip);
if(num_ips == 0)
{
DEBUG(0,("initialise_wins: Missing IP address when parsing line %s\n", line ));
continue;
}
if(!got_token)
{
DEBUG(0,("initialise_wins: Missing nb_flags when parsing line %s\n", line ));
continue;
}
if((ip_list = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr))) == NULL)
{
DEBUG(0,("initialise_wins: Malloc fail !\n"));
return False;
}
ptr = line;
next_token(&ptr,name_str,NULL,sizeof(name_str));
next_token(&ptr,ttl_str,NULL,sizeof(ttl_str));
for(i = 0; i < num_ips; i++)
{
next_token(&ptr, ip_str, NULL, sizeof(ip_str));
ip_list[i] = *interpret_addr2(ip_str);
}
next_token(&ptr,nb_flags_str,NULL, sizeof(nb_flags_str));
if(nb_flags_str[strlen(nb_flags_str)-1] == 'S')
{
DEBUG(5,("initialise_wins: Ignoring SELF name %s\n", line));
SAFE_FREE(ip_list);
continue;
}
if(nb_flags_str[strlen(nb_flags_str)-1] == 'R')
nb_flags_str[strlen(nb_flags_str)-1] = '\0';
pstrcpy(name,name_str);
if((p = strchr(name,'#')) != NULL)
{
*p = 0;
sscanf(p+1,"%x",&type);
}
sscanf(nb_flags_str,"%x",&nb_flags);
sscanf(ttl_str,"%d",&ttl);
if ((ttl - 60) > time_now || ttl == PERMANENT_TTL)
{
if(ttl != PERMANENT_TTL)
ttl -= time_now;
DEBUG( 4, ("initialise_wins: add name: %s#%02x ttl = %d first IP %s flags = %2x\n",
name, type, ttl, inet_ntoa(ip_list[0]), nb_flags));
(void)add_name_to_subnet( wins_server_subnet, name, type, nb_flags,
ttl, REGISTER_NAME, num_ips, ip_list );
}
else
{
DEBUG(4, ("initialise_wins: not adding name (ttl problem) %s#%02x ttl = %d first IP %s flags = %2x\n",
name, type, ttl, inet_ntoa(ip_list[0]), nb_flags));
}
SAFE_FREE(ip_list);
}
fclose(fp);
return True;
}
static void send_wins_wack_response(int ttl, struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
unsigned char rdata[2];
rdata[0] = rdata[1] = 0;
rdata[0] = (nmb->header.opcode & 0xF) << 3;
if (nmb->header.nm_flags.authoritative &&
nmb->header.response) rdata[0] |= 0x4;
if (nmb->header.nm_flags.trunc) rdata[0] |= 0x2;
if (nmb->header.nm_flags.recursion_desired) rdata[0] |= 0x1;
if (nmb->header.nm_flags.recursion_available &&
nmb->header.response) rdata[1] |= 0x80;
if (nmb->header.nm_flags.bcast) rdata[1] |= 0x10;
reply_netbios_packet(p,
0,
NMB_WAIT_ACK,
NMB_WACK_OPCODE,
ttl,
(char *)rdata,
2);
}
static void send_wins_name_registration_response(int rcode, int ttl, struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
char rdata[6];
memcpy(&rdata[0], &nmb->additional->rdata[0], 6);
reply_netbios_packet(p,
rcode,
WINS_REG,
NMB_NAME_REG_OPCODE,
ttl,
rdata,
6);
}
void wins_process_name_refresh_request(struct subnet_record *subrec,
struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
BOOL bcast = nmb->header.nm_flags.bcast;
uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
BOOL group = (nb_flags & NB_GROUP) ? True : False;
struct name_record *namerec = NULL;
int ttl = get_ttl_from_packet(nmb);
struct in_addr from_ip;
putip((char *)&from_ip,&nmb->additional->rdata[2]);
if(bcast)
{
DEBUG(0,("wins_process_name_refresh_request: broadcast name refresh request \
received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
return;
}
DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s \
IP %s\n", nmb_namestr(question), inet_ntoa(from_ip) ));
namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
if(namerec == NULL)
{
DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s and \
the name does not exist. Treating as registration.\n", nmb_namestr(question) ));
wins_process_name_registration_request(subrec,p);
return;
}
if((namerec != NULL) && ((group && !NAME_GROUP(namerec)) || (!group && NAME_GROUP(namerec))) )
{
DEBUG(3,("wins_process_name_refresh_request: Name %s group bit = %s \
does not match group bit in WINS for this name.\n", nmb_namestr(question), group ? "True" : "False" ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
if((!group || (group && (question->name_type == 0x1c))) && find_ip_in_name_record(namerec, from_ip ))
{
update_name_ttl(namerec, ttl);
send_wins_name_registration_response(0, ttl, p);
wins_hook("refresh", namerec, ttl);
return;
}
else if(group)
{
update_name_ttl(namerec, ttl);
send_wins_name_registration_response(0, ttl, p);
return;
}
else if(!group && (question->name_type == 0x1d))
{
send_wins_name_registration_response(0, ttl, p);
return;
}
else
{
DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s with IP %s and \
is IP is not known to the name.\n", nmb_namestr(question), inet_ntoa(from_ip) ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
}
static void wins_register_query_success(struct subnet_record *subrec,
struct userdata_struct *userdata,
struct nmb_name *question_name,
struct in_addr ip,
struct res_rec *answers)
{
struct packet_struct *orig_reg_packet;
memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
DEBUG(3,("wins_register_query_success: Original client at IP %s still wants the \
name %s. Rejecting registration request.\n", inet_ntoa(ip), nmb_namestr(question_name) ));
send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
orig_reg_packet->locked = False;
free_packet(orig_reg_packet);
}
static void wins_register_query_fail(struct subnet_record *subrec,
struct response_record *rrec,
struct nmb_name *question_name,
int rcode)
{
struct userdata_struct *userdata = rrec->userdata;
struct packet_struct *orig_reg_packet;
struct name_record *namerec = NULL;
memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
namerec = find_name_on_subnet(subrec, question_name, FIND_ANY_NAME);
if( (namerec != NULL)
&& (namerec->data.source == REGISTER_NAME)
&& ip_equal(rrec->packet->ip, *namerec->data.ip) )
{
remove_name_from_namelist( subrec, namerec);
namerec = NULL;
}
if(namerec == NULL)
wins_process_name_registration_request(subrec, orig_reg_packet);
else
DEBUG(2,("wins_register_query_fail: The state of the WINS database changed between \
querying for name %s in order to replace it and this reply.\n", nmb_namestr(question_name) ));
orig_reg_packet->locked = False;
free_packet(orig_reg_packet);
}
void wins_process_name_registration_request(struct subnet_record *subrec,
struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
BOOL bcast = nmb->header.nm_flags.bcast;
uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
int ttl = get_ttl_from_packet(nmb);
struct name_record *namerec = NULL;
struct in_addr from_ip;
BOOL registering_group_name = (nb_flags & NB_GROUP) ? True : False;
putip((char *)&from_ip,&nmb->additional->rdata[2]);
if(bcast)
{
DEBUG(0,("wins_process_name_registration_request: broadcast name registration request \
received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
return;
}
DEBUG(3,("wins_process_name_registration_request: %s name registration for name %s \
IP %s\n", registering_group_name ? "Group" : "Unique", nmb_namestr(question), inet_ntoa(from_ip) ));
namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
if( (namerec != NULL)
&& ( (namerec->data.source == DNS_NAME)
|| (namerec->data.source == DNSFAIL_NAME) ) )
{
DEBUG(5,("wins_process_name_registration_request: Name (%s) in WINS was \
a dns lookup - removing it.\n", nmb_namestr(question) ));
remove_name_from_namelist( subrec, namerec );
namerec = NULL;
}
if((namerec != NULL) && (namerec->data.source != REGISTER_NAME))
{
DEBUG( 3, ( "wins_process_name_registration_request: Attempt \
to register name %s. Name already exists in WINS with source type %d.\n",
nmb_namestr(question), namerec->data.source ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
if(registering_group_name && (question->name_type != 0x1c))
from_ip = *interpret_addr2("255.255.255.255");
if(!registering_group_name && (question->name_type == 0x1d))
{
DEBUG(3,("wins_process_name_registration_request: Ignoring request \
to register name %s from IP %s.\n", nmb_namestr(question), inet_ntoa(p->ip) ));
send_wins_name_registration_response(0, ttl, p);
return;
}
if((namerec != NULL) && NAME_GROUP(namerec))
{
if(registering_group_name)
{
DEBUG(3,("wins_process_name_registration_request: Adding IP %s to group name %s.\n",
inet_ntoa(from_ip), nmb_namestr(question) ));
if(!find_ip_in_name_record(namerec, from_ip))
add_ip_to_name_record(namerec, from_ip);
update_name_ttl(namerec, ttl);
send_wins_name_registration_response(0, ttl, p);
return;
}
else
{
DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
already exists in WINS as a GROUP name.\n", nmb_namestr(question) ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
}
if((namerec != NULL) && (is_myname(namerec->name.name)) )
{
if(!ismyip(from_ip))
{
DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
is one of our (WINS server) names. Denying registration.\n", nmb_namestr(question) ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
else
{
update_name_ttl(namerec, ttl);
send_wins_name_registration_response(0, ttl, p);
wins_hook("refresh", namerec, ttl);
return;
}
}
if( !registering_group_name
&& (namerec != NULL)
&& (namerec->data.num_ips == 1)
&& ip_equal( namerec->data.ip[0], from_ip ) )
{
update_name_ttl( namerec, ttl );
send_wins_name_registration_response( 0, ttl, p );
wins_hook("refresh", namerec, ttl);
return;
}
if( namerec != NULL )
{
long *ud[(sizeof(struct userdata_struct) + sizeof(struct packet_struct *))/sizeof(long *) + 1];
struct userdata_struct *userdata = (struct userdata_struct *)ud;
send_wins_wack_response(60, p);
p->locked = True;
userdata = (struct userdata_struct *)ud;
userdata->copy_fn = NULL;
userdata->free_fn = NULL;
userdata->userdata_len = sizeof(struct packet_struct *);
memcpy(userdata->data, (char *)&p, sizeof(struct packet_struct *) );
query_name_from_wins_server( *namerec->data.ip,
question->name,
question->name_type,
wins_register_query_success,
wins_register_query_fail,
userdata );
return;
}
(void)add_name_to_subnet( subrec, question->name, question->name_type,
nb_flags, ttl, REGISTER_NAME, 1, &from_ip );
if ((namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME))) {
wins_hook("add", namerec, ttl);
}
send_wins_name_registration_response(0, ttl, p);
}
static void wins_multihomed_register_query_success(struct subnet_record *subrec,
struct userdata_struct *userdata,
struct nmb_name *question_name,
struct in_addr ip,
struct res_rec *answers)
{
struct packet_struct *orig_reg_packet;
struct nmb_packet *nmb;
struct name_record *namerec = NULL;
struct in_addr from_ip;
int ttl;
memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
nmb = &orig_reg_packet->packet.nmb;
putip((char *)&from_ip,&nmb->additional->rdata[2]);
ttl = get_ttl_from_packet(nmb);
namerec = find_name_on_subnet(subrec, question_name, FIND_ANY_NAME);
if( (namerec == NULL) || (namerec->data.source != REGISTER_NAME) )
{
DEBUG(3,("wins_multihomed_register_query_success: name %s is not in the correct state to add \
a subsequent IP addess.\n", nmb_namestr(question_name) ));
send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
orig_reg_packet->locked = False;
free_packet(orig_reg_packet);
return;
}
if(!find_ip_in_name_record(namerec, from_ip))
add_ip_to_name_record(namerec, from_ip);
update_name_ttl(namerec, ttl);
send_wins_name_registration_response(0, ttl, orig_reg_packet);
wins_hook("add", namerec, ttl);
orig_reg_packet->locked = False;
free_packet(orig_reg_packet);
}
static void wins_multihomed_register_query_fail(struct subnet_record *subrec,
struct response_record *rrec,
struct nmb_name *question_name,
int rcode)
{
struct userdata_struct *userdata = rrec->userdata;
struct packet_struct *orig_reg_packet;
memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
DEBUG(3,("wins_multihomed_register_query_fail: Registering machine at IP %s failed to answer \
query successfully for name %s.\n", inet_ntoa(orig_reg_packet->ip), nmb_namestr(question_name) ));
send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
orig_reg_packet->locked = False;
free_packet(orig_reg_packet);
return;
}
void wins_process_multihomed_name_registration_request( struct subnet_record *subrec,
struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
BOOL bcast = nmb->header.nm_flags.bcast;
uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
int ttl = get_ttl_from_packet(nmb);
struct name_record *namerec = NULL;
struct in_addr from_ip;
BOOL group = (nb_flags & NB_GROUP) ? True : False;;
putip((char *)&from_ip,&nmb->additional->rdata[2]);
if(bcast)
{
DEBUG(0,("wins_process_multihomed_name_registration_request: broadcast name registration request \
received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
return;
}
if(group)
{
DEBUG(0,("wins_process_multihomed_name_registration_request: group name registration request \
received for name %s from IP %s on subnet %s. Errror - group names should not be multihomed.\n",
nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
return;
}
DEBUG(3,("wins_process_multihomed_name_registration_request: name registration for name %s \
IP %s\n", nmb_namestr(question), inet_ntoa(from_ip) ));
if(question->name_type == 0x1d)
{
DEBUG(3,("wins_process_multihomed_name_registration_request: Ignoring request \
to register name %s from IP %s.", nmb_namestr(question), inet_ntoa(p->ip) ));
send_wins_name_registration_response(0, ttl, p);
return;
}
namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
if( (namerec != NULL)
&& ( (namerec->data.source == DNS_NAME)
|| (namerec->data.source == DNSFAIL_NAME) ) )
{
DEBUG(5,("wins_process_multihomed_name_registration_request: Name (%s) in WINS was a dns lookup \
- removing it.\n", nmb_namestr(question) ));
remove_name_from_namelist( subrec, namerec);
namerec = NULL;
}
if( (namerec != NULL) && (namerec->data.source != REGISTER_NAME) )
{
DEBUG( 3, ( "wins_process_multihomed_name_registration_request: Attempt \
to register name %s. Name already exists in WINS with source type %d.\n",
nmb_namestr(question), namerec->data.source ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
if((namerec != NULL) && NAME_GROUP(namerec))
{
DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
already exists in WINS as a GROUP name.\n", nmb_namestr(question) ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
if((namerec != NULL) && (is_myname(namerec->name.name)) )
{
if(!ismyip(from_ip))
{
DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
is one of our (WINS server) names. Denying registration.\n", nmb_namestr(question) ));
send_wins_name_registration_response(RFS_ERR, 0, p);
return;
}
else
{
if(!find_ip_in_name_record(namerec, from_ip)) {
add_ip_to_name_record(namerec, from_ip);
wins_hook("add", namerec, ttl);
} else {
wins_hook("refresh", namerec, ttl);
}
update_name_ttl(namerec, ttl);
send_wins_name_registration_response(0, ttl, p);
return;
}
}
if((namerec != NULL) && find_ip_in_name_record(namerec, from_ip))
{
update_name_ttl(namerec, ttl);
send_wins_name_registration_response(0, ttl, p);
wins_hook("refresh", namerec, ttl);
return;
}
if(namerec != NULL)
{
long *ud[(sizeof(struct userdata_struct) + sizeof(struct packet_struct *))/sizeof(long *) + 1];
struct userdata_struct *userdata = (struct userdata_struct *)ud;
send_wins_wack_response(60, p);
p->locked = True;
userdata = (struct userdata_struct *)ud;
userdata->copy_fn = NULL;
userdata->free_fn = NULL;
userdata->userdata_len = sizeof(struct packet_struct *);
memcpy(userdata->data, (char *)&p, sizeof(struct packet_struct *) );
query_name_from_wins_server( namerec->data.ip[0],
question->name,
question->name_type,
wins_multihomed_register_query_success,
wins_multihomed_register_query_fail,
userdata );
return;
}
(void)add_name_to_subnet( subrec, question->name, question->name_type,
nb_flags, ttl, REGISTER_NAME, 1, &from_ip );
if ((namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME))) {
wins_hook("add", namerec, ttl);
}
send_wins_name_registration_response(0, ttl, p);
}
static void process_wins_dmb_query_request(struct subnet_record *subrec,
struct packet_struct *p)
{
struct name_record *namerec = NULL;
char *prdata;
int num_ips;
num_ips = 0;
for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
namerec;
namerec = (struct name_record *)ubi_trNext( namerec ) )
{
if( namerec->name.name_type == 0x1b )
num_ips += namerec->data.num_ips;
}
if(num_ips == 0)
{
send_wins_name_query_response(NAM_ERR, p, NULL);
return;
}
if((prdata = (char *)malloc( num_ips * 6 )) == NULL)
{
DEBUG(0,("process_wins_dmb_query_request: Malloc fail !.\n"));
return;
}
num_ips = 0;
for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
namerec;
namerec = (struct name_record *)ubi_trNext( namerec ) )
{
if(namerec->name.name_type == 0x1b)
{
int i;
for(i = 0; i < namerec->data.num_ips; i++)
{
set_nb_flags(&prdata[num_ips * 6],namerec->data.nb_flags);
putip((char *)&prdata[(num_ips * 6) + 2], &namerec->data.ip[i]);
num_ips++;
}
}
}
reply_netbios_packet(p,
0,
WINS_QUERY,
NMB_NAME_QUERY_OPCODE,
lp_min_wins_ttl(),
prdata,
num_ips*6);
SAFE_FREE(prdata);
}
void send_wins_name_query_response(int rcode, struct packet_struct *p,
struct name_record *namerec)
{
char rdata[6];
char *prdata = rdata;
int reply_data_len = 0;
int ttl = 0;
int i;
memset(rdata,'\0',6);
if(rcode == 0)
{
ttl = (namerec->data.death_time != PERMANENT_TTL) ?
namerec->data.death_time - p->timestamp : lp_max_wins_ttl();
if( namerec->data.num_ips == 1 )
prdata = rdata;
else
{
if((prdata = (char *)malloc( namerec->data.num_ips * 6 )) == NULL)
{
DEBUG(0,("send_wins_name_query_response: malloc fail !\n"));
return;
}
}
for(i = 0; i < namerec->data.num_ips; i++)
{
set_nb_flags(&prdata[i*6],namerec->data.nb_flags);
putip((char *)&prdata[2+(i*6)], &namerec->data.ip[i]);
}
sort_query_replies(prdata, i, p->ip);
reply_data_len = namerec->data.num_ips * 6;
}
reply_netbios_packet(p,
rcode,
WINS_QUERY,
NMB_NAME_QUERY_OPCODE,
ttl,
prdata,
reply_data_len);
if((prdata != rdata) && (prdata != NULL))
SAFE_FREE(prdata);
}
void wins_process_name_query_request(struct subnet_record *subrec,
struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
struct name_record *namerec = NULL;
DEBUG(3,("wins_process_name_query: name query for name %s from IP %s\n",
nmb_namestr(question), inet_ntoa(p->ip) ));
if(strequal( question->name, "*") && (question->name_type == 0x1b))
{
process_wins_dmb_query_request( subrec, p);
return;
}
namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
if(namerec != NULL)
{
if( namerec->data.source == DNSFAIL_NAME )
{
DEBUG(3,("wins_process_name_query: name query for name %s returning DNS fail.\n",
nmb_namestr(question) ));
send_wins_name_query_response(NAM_ERR, p, namerec);
return;
}
if( (namerec->data.death_time != PERMANENT_TTL)
&& (namerec->data.death_time < p->timestamp) )
{
DEBUG(3,("wins_process_name_query: name query for name %s - name expired. Returning fail.\n",
nmb_namestr(question) ));
send_wins_name_query_response(NAM_ERR, p, namerec);
return;
}
DEBUG(3,("wins_process_name_query: name query for name %s returning first IP %s.\n",
nmb_namestr(question), inet_ntoa(namerec->data.ip[0]) ));
send_wins_name_query_response(0, p, namerec);
return;
}
if(lp_dns_proxy() &&
((question->name_type == 0x20) || question->name_type == 0))
{
DEBUG(3,("wins_process_name_query: name query for name %s not found - doing dns lookup.\n",
nmb_namestr(question) ));
queue_dns_query(p, question, &namerec);
return;
}
send_wins_name_query_response(NAM_ERR, p, NULL);
}
static void send_wins_name_release_response(int rcode, struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
char rdata[6];
memcpy(&rdata[0], &nmb->additional->rdata[0], 6);
reply_netbios_packet(p,
rcode,
NMB_REL,
NMB_NAME_RELEASE_OPCODE,
0,
rdata,
6);
}
void wins_process_name_release_request(struct subnet_record *subrec,
struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
BOOL bcast = nmb->header.nm_flags.bcast;
uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
struct name_record *namerec = NULL;
struct in_addr from_ip;
BOOL releasing_group_name = (nb_flags & NB_GROUP) ? True : False;;
putip((char *)&from_ip,&nmb->additional->rdata[2]);
if(bcast)
{
DEBUG(0,("wins_process_name_release_request: broadcast name registration request \
received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
return;
}
DEBUG(3,("wins_process_name_release_request: %s name release for name %s \
IP %s\n", releasing_group_name ? "Group" : "Unique", nmb_namestr(question), inet_ntoa(from_ip) ));
if(!releasing_group_name && (question->name_type == 0x1d))
{
DEBUG(3,("wins_process_name_release_request: Ignoring request \
to release name %s from IP %s.", nmb_namestr(question), inet_ntoa(p->ip) ));
send_wins_name_release_response(0, p);
return;
}
namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
if( (namerec == NULL)
|| ((namerec != NULL) && (namerec->data.source != REGISTER_NAME)) )
{
send_wins_name_release_response(NAM_ERR, p);
return;
}
if(releasing_group_name && (question->name_type != 0x1c))
{
send_wins_name_release_response(0, p);
return;
}
if(!find_ip_in_name_record(namerec, from_ip))
{
DEBUG(3,("wins_process_name_release_request: Refusing request to \
release name %s as IP %s is not one of the known IP's for this name.\n",
nmb_namestr(question), inet_ntoa(from_ip) ));
send_wins_name_release_response(NAM_ERR, p);
return;
}
send_wins_name_release_response(0, p);
remove_ip_from_name_record(namerec, from_ip);
wins_hook("delete", namerec, 0);
if (namerec->data.num_ips == 0)
remove_name_from_namelist(subrec, namerec);
}
void initiate_wins_processing(time_t t)
{
static time_t lasttime = 0;
if (!lasttime)
lasttime = t;
if (t - lasttime < 20)
return;
lasttime = t;
if(!lp_we_are_a_wins_server())
return;
expire_names_on_subnet(wins_server_subnet, t);
if(wins_server_subnet->namelist_changed)
wins_write_database(True);
wins_server_subnet->namelist_changed = False;
}
void wins_write_database(BOOL background)
{
struct name_record *namerec;
pstring fname, fnamenew;
FILE *fp;
if(!lp_we_are_a_wins_server())
return;
if (background) {
CatchChild();
if (sys_fork()) {
return;
}
}
slprintf(fname,sizeof(fname)-1,"%s/%s", lp_lockdir(), WINS_LIST);
all_string_sub(fname,"//", "/", 0);
slprintf(fnamenew,sizeof(fnamenew)-1,"%s.%u", fname, (unsigned int)sys_getpid());
if((fp = sys_fopen(fnamenew,"w")) == NULL)
{
DEBUG(0,("wins_write_database: Can't open %s. Error was %s\n", fnamenew, strerror(errno)));
if (background) {
_exit(0);
}
return;
}
DEBUG(4,("wins_write_database: Dump of WINS name list.\n"));
fprintf(fp,"VERSION %d %u\n", WINS_VERSION, wins_hash());
for( namerec
= (struct name_record *)ubi_trFirst( wins_server_subnet->namelist );
namerec;
namerec = (struct name_record *)ubi_trNext( namerec ) )
{
int i;
struct tm *tm;
DEBUGADD(4,("%-19s ", nmb_namestr(&namerec->name) ));
if( namerec->data.death_time != PERMANENT_TTL )
{
char *ts, *nl;
tm = LocalTime(&namerec->data.death_time);
ts = asctime(tm);
nl = strrchr( ts, '\n' );
if( NULL != nl )
*nl = '\0';
DEBUGADD(4,("TTL = %s ", ts ));
}
else
DEBUGADD(4,("TTL = PERMANENT "));
for (i = 0; i < namerec->data.num_ips; i++)
DEBUGADD(4,("%15s ", inet_ntoa(namerec->data.ip[i]) ));
DEBUGADD(4,("%2x\n", namerec->data.nb_flags ));
if( namerec->data.source == REGISTER_NAME )
{
fprintf(fp, "\"%s#%02x\" %d ",
namerec->name.name,namerec->name.name_type,
(int)namerec->data.death_time);
for (i = 0; i < namerec->data.num_ips; i++)
fprintf( fp, "%s ", inet_ntoa( namerec->data.ip[i] ) );
fprintf( fp, "%2xR\n", namerec->data.nb_flags );
}
}
fclose(fp);
chmod(fnamenew,0644);
unlink(fname);
rename(fnamenew,fname);
if (background) {
_exit(0);
}
}