📄 nmbd_incomingrequests.c
字号:
/* Unix SMB/CIFS implementation. NBT netbios routines and daemon - version 2 Copyright (C) Andrew Tridgell 1994-1998 Copyright (C) Luke Kenneth Casson Leighton 1994-1998 Copyright (C) Jeremy Allison 1994-2003 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 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. This file contains all the code to process NetBIOS requests coming in on port 137. It does not deal with the code needed to service WINS server requests, but only broadcast and unicast requests.*/#include "includes.h"/****************************************************************************Send a name release response.**************************************************************************/static void send_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, /* Packet to reply to. */ rcode, /* Result code. */ NMB_REL, /* nmbd type code. */ NMB_NAME_RELEASE_OPCODE, /* opcode. */ 0, /* ttl. */ rdata, /* data to send. */ 6); /* data length. */}/****************************************************************************Process a name release packet on a broadcast subnet.Ignore it if it's not one of our names.****************************************************************************/void process_name_release_request(struct subnet_record *subrec, struct packet_struct *p){ struct nmb_packet *nmb = &p->packet.nmb; struct in_addr owner_ip; struct nmb_name *question = &nmb->question.question_name; unstring qname; 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; int rcode = 0; putip((char *)&owner_ip,&nmb->additional->rdata[2]); if(!bcast) { /* We should only get broadcast name release packets here. Anyone trying to release unicast should be going to a WINS server. If the code gets here, then either we are not a wins server and they sent it anyway, or we are a WINS server and the request was malformed. Either way, log an error here. and send an error reply back. */ DEBUG(0,("process_name_release_request: unicast name release request \received for name %s from IP %s on subnet %s. Error - should be sent to WINS server\n", nmb_namestr(question), inet_ntoa(owner_ip), subrec->subnet_name)); send_name_release_response(FMT_ERR, p); return; } DEBUG(3,("process_name_release_request: Name release on name %s, \subnet %s from owner IP %s\n", nmb_namestr(&nmb->question.question_name), subrec->subnet_name, inet_ntoa(owner_ip))); /* If someone is releasing a broadcast group name, just ignore it. */ if( group && !ismyip(owner_ip) ) return; /* * Code to work around a bug in FTP OnNet software NBT implementation. * They do a broadcast name release for WORKGROUP<0> and WORKGROUP<1e> * names and *don't set the group bit* !!!!! */ pull_ascii_nstring(qname, sizeof(qname), question->name); if( !group && !ismyip(owner_ip) && strequal(qname, lp_workgroup()) && ((question->name_type == 0x0) || (question->name_type == 0x1e))) { DEBUG(6,("process_name_release_request: FTP OnNet bug workaround. Ignoring \group release name %s from IP %s on subnet %s with no group bit set.\n", nmb_namestr(question), inet_ntoa(owner_ip), subrec->subnet_name )); return; } namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME); /* We only care about someone trying to release one of our names. */ if( namerec && ( (namerec->data.source == SELF_NAME) || (namerec->data.source == PERMANENT_NAME) ) ) { rcode = ACT_ERR; DEBUG(0, ("process_name_release_request: Attempt to release name %s from IP %s \on subnet %s being rejected as it is one of our names.\n", nmb_namestr(&nmb->question.question_name), inet_ntoa(owner_ip), subrec->subnet_name)); } if(rcode == 0) return; /* Send a NAME RELEASE RESPONSE (pos/neg) see rfc1002.txt 4.2.10-11 */ send_name_release_response(rcode, p);}/****************************************************************************Send a name registration response.**************************************************************************/static void send_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, /* Packet to reply to. */ rcode, /* Result code. */ NMB_REG, /* nmbd type code. */ NMB_NAME_REG_OPCODE, /* opcode. */ ttl, /* ttl. */ rdata, /* data to send. */ 6); /* data length. */}/****************************************************************************Process a name refresh request on a broadcast subnet.**************************************************************************/ void 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; struct in_addr from_ip; putip((char *)&from_ip,&nmb->additional->rdata[2]); if(!bcast) { /* We should only get broadcast name refresh packets here. Anyone trying to refresh unicast should be going to a WINS server. If the code gets here, then either we are not a wins server and they sent it anyway, or we are a WINS server and the request was malformed. Either way, log an error here. and send an error reply back. */ DEBUG(0,("process_name_refresh_request: unicast name registration request \received for name %s from IP %s on subnet %s.\n", nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name)); DEBUG(0,("Error - should be sent to WINS server\n")); send_name_registration_response(FMT_ERR, 0, p); return; } /* Just log a message. We really don't care about broadcast name refreshes. */ DEBUG(3,("process_name_refresh_request: Name refresh for name %s \IP %s on subnet %s\n", nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));} /****************************************************************************Process a name registration request on a broadcast subnet.**************************************************************************/void 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); BOOL group = (nb_flags & NB_GROUP) ? True : False; struct name_record *namerec = NULL; int ttl = nmb->additional->ttl; struct in_addr from_ip; putip((char *)&from_ip,&nmb->additional->rdata[2]); if(!bcast) { /* We should only get broadcast name registration packets here. Anyone trying to register unicast should be going to a WINS server. If the code gets here, then either we are not a wins server and they sent it anyway, or we are a WINS server and the request was malformed. Either way, log an error here. and send an error reply back. */ DEBUG(0,("process_name_registration_request: unicast name registration request \received for name %s from IP %s on subnet %s. Error - should be sent to WINS server\n", nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name)); send_name_registration_response(FMT_ERR, 0, p); return; } DEBUG(3,("process_name_registration_request: Name registration for name %s \IP %s on subnet %s\n", nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name)); /* See if the name already exists. */ namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME); /* * If the name being registered exists and is a WINS_PROXY_NAME * then delete the WINS proxy name entry so we don't reply erroneously * later to queries. */ if((namerec != NULL) && (namerec->data.source == WINS_PROXY_NAME)) { remove_name_from_namelist( subrec, namerec ); namerec = NULL; } if (!group) { /* Unique name. */ if( (namerec != NULL) && ( (namerec->data.source == SELF_NAME) || (namerec->data.source == PERMANENT_NAME) || NAME_GROUP(namerec) ) ) { /* No-one can register one of Samba's names, nor can they register a name that's a group name as a unique name */ send_name_registration_response(ACT_ERR, 0, p); return; } else if(namerec != NULL) { /* Update the namelist record with the new information. */ namerec->data.ip[0] = from_ip; update_name_ttl(namerec, ttl); DEBUG(3,("process_name_registration_request: Updated name record %s \with IP %s on subnet %s\n",nmb_namestr(&namerec->name),inet_ntoa(from_ip), subrec->subnet_name)); return; } } else { /* Group name. */ if( (namerec != NULL) && !NAME_GROUP(namerec) && ( (namerec->data.source == SELF_NAME) || (namerec->data.source == PERMANENT_NAME) ) ) { /* Disallow group names when we have a unique name. */ send_name_registration_response(ACT_ERR, 0, p); return; } }}/****************************************************************************This is used to sort names for a name status into a sensible order.We put our own names first, then in alphabetical order.**************************************************************************/static int status_compare(char *n1,char *n2){ unstring name1, name2; int l1,l2,l3; memset(name1, '\0', sizeof(name1)); memset(name2, '\0', sizeof(name2)); pull_ascii_nstring(name1, sizeof(name1), n1); pull_ascii_nstring(name2, sizeof(name2), n2); n1 = name1; n2 = name2; /* It's a bit tricky because the names are space padded */ for (l1=0;l1<15 && n1[l1] && n1[l1] != ' ';l1++) ; for (l2=0;l2<15 && n2[l2] && n2[l2] != ' ';l2++) ; l3 = strlen(global_myname());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -