2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1995
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
32 extern int ClientDGRAM;
34 enum name_search { FIND_SELF, FIND_GLOBAL };
36 extern int DEBUGLEVEL;
39 extern BOOL CanRecurse;
40 extern pstring myname;
41 extern struct in_addr ipzero;
43 /* netbios names database */
44 struct name_record *namelist;
46 #define GET_TTL(ttl) ((ttl)?MIN(ttl,lp_max_ttl()):lp_max_ttl())
49 /****************************************************************************
50 true if two netbios names are equal
51 ****************************************************************************/
52 static BOOL name_equal(struct nmb_name *n1,struct nmb_name *n2)
54 if (n1->name_type != n2->name_type) return(False);
56 return(strequal(n1->name,n2->name) && strequal(n1->scope,n2->scope));
59 /****************************************************************************
60 add a netbios name into the namelist
61 **************************************************************************/
62 static void add_name(struct name_record *n)
64 struct name_record *n2;
74 for (n2 = namelist; n2->next; n2 = n2->next) ;
81 /****************************************************************************
82 remove a name from the namelist. The pointer must be an element just
84 **************************************************************************/
85 void remove_name(struct name_record *n)
87 struct name_record *nlist = namelist;
89 while (nlist && nlist != n) nlist = nlist->next;
93 if (nlist->next) nlist->next->prev = nlist->prev;
94 if (nlist->prev) nlist->prev->next = nlist->next;
99 /****************************************************************************
100 find a name in the domain database namelist
102 FIND_SELF - look for names the samba server has added for itself
103 FIND_GLOBAL - the name can be anyone. first look on the client's
104 subnet, then the server's subnet, then all subnets.
105 **************************************************************************/
106 static struct name_record *find_name_search(struct nmb_name *name,
107 enum name_search search,
110 struct name_record *ret;
112 /* any number of winpopup names can be added. must search by ip as
114 if (name->name_type != 0x3) ip = ipzero;
116 for (ret = namelist; ret; ret = ret->next)
118 if (name_equal(&ret->name,name))
120 /* self search: self names only */
121 if (search == FIND_SELF && ret->source != SELF) continue;
123 if (zero_ip(ip) || ip_equal(ip, ret->ip))
134 /****************************************************************************
135 dump a copy of the name table
136 **************************************************************************/
137 void dump_names(void)
139 struct name_record *n;
140 time_t t = time(NULL);
142 DEBUG(3,("Dump of local name table:\n"));
144 for (n = namelist; n; n = n->next)
146 DEBUG(3,("%s %s TTL=%d NBFLAGS=%2x\n",
149 n->death_time?n->death_time-t:0,
155 /****************************************************************************
156 remove an entry from the name list
157 ****************************************************************************/
158 void remove_netbios_name(char *name,int type, enum name_source source,
162 struct name_record *n;
164 make_nmb_name(&nn, name, type, scope);
165 n = find_name_search(&nn, FIND_GLOBAL, ip);
167 if (n && n->source == source) remove_name(n);
171 /****************************************************************************
172 add an entry to the name list
173 ****************************************************************************/
174 struct name_record *add_netbios_entry(char *name, int type, int nb_flags,
176 enum name_source source,
180 struct name_record *n;
181 struct name_record *n2=NULL;
183 n = (struct name_record *)malloc(sizeof(*n));
184 if (!n) return(NULL);
186 bzero((char *)n,sizeof(*n));
188 make_nmb_name(&n->name,name,type,scope);
190 if ((n2 = find_name_search(&n->name, FIND_GLOBAL, new_only?ipzero:ip)))
193 if (new_only || (n2->source==SELF && source!=SELF)) return n2;
197 if (ttl) n->death_time = time(NULL)+ttl*3;
199 n->nb_flags = nb_flags;
202 if (!n2) add_name(n);
204 DEBUG(3,("Added netbios name %s at %s ttl=%d nb_flags=%2x\n",
205 namestr(&n->name),inet_ntoa(ip),ttl,nb_flags));
211 /****************************************************************************
212 remove an entry from the name list
213 ****************************************************************************/
214 void remove_name_entry(char *name,int type)
216 if (lp_wins_support())
218 /* we are a WINS server. */
219 remove_netbios_name(name,type,SELF,ipzero);
226 queue_netbios_pkt_wins(ClientNMB,NMB_REL,NAME_RELEASE,
233 /****************************************************************************
234 add an entry to the name list
235 ****************************************************************************/
236 void add_name_entry(char *name,int type,int nb_flags)
238 /* always add our own entries */
239 add_netbios_entry(name,type,nb_flags,0,SELF,ipzero,False);
241 if (!lp_wins_support())
246 queue_netbios_pkt_wins(ClientNMB,NMB_REG,NAME_REGISTER,
247 name, type, nb_flags,
253 /****************************************************************************
254 add the magic samba names, useful for finding samba servers
255 **************************************************************************/
256 void add_my_names(void)
262 add_name_entry(myname,0x20,NB_ACTIVE);
263 add_name_entry(myname,0x03,NB_ACTIVE);
264 add_name_entry(myname,0x00,NB_ACTIVE);
265 add_name_entry(myname,0x1f,NB_ACTIVE);
267 add_netbios_entry("*",0x0,NB_ACTIVE,0,SELF,ip,False);
268 add_netbios_entry("__SAMBA__",0x20,NB_ACTIVE,0,SELF,ip,False);
269 add_netbios_entry("__SAMBA__",0x00,NB_ACTIVE,0,SELF,ip,False);
272 /*******************************************************************
274 ******************************************************************/
275 void refresh_my_names(time_t t)
277 static time_t lasttime = 0;
279 if (t - lasttime < REFRESH_TIME)
286 /*******************************************************************
287 expires old names in the namelist
288 ******************************************************************/
289 void expire_names(time_t t)
291 struct name_record *n;
292 struct name_record *next;
294 /* expire old names */
295 for (n = namelist; n; n = next)
297 if (n->death_time && n->death_time < t)
299 DEBUG(3,("Removing dead name %s\n", namestr(&n->name)));
303 if (n->prev) n->prev->next = n->next;
304 if (n->next) n->next->prev = n->prev;
306 if (namelist == n) namelist = n->next;
318 /****************************************************************************
319 response for a reg release received
320 **************************************************************************/
321 void response_name_release(struct packet_struct *p)
323 struct nmb_packet *nmb = &p->packet.nmb;
324 char *name = nmb->question.question_name.name;
325 int type = nmb->question.question_name.name_type;
327 DEBUG(4,("response name release received\n"));
329 if (nmb->header.rcode == 0 && nmb->answers->rdata)
331 struct in_addr found_ip;
332 putip((char*)&found_ip,&nmb->answers->rdata[2]);
334 if (ismyip(found_ip))
336 remove_netbios_name(name,type,SELF,found_ip);
341 DEBUG(1,("name registration for %s rejected!\n",
342 namestr(&nmb->question.question_name)));
347 /****************************************************************************
348 reply to a name release
349 ****************************************************************************/
350 void reply_name_release(struct packet_struct *p)
352 struct nmb_packet *nmb = &p->packet.nmb;
355 int opcode = nmb->header.opcode;
356 int nb_flags = nmb->additional->rdata[0];
357 BOOL bcast = nmb->header.nm_flags.bcast;
358 struct name_record *n;
361 putip((char *)&ip,&nmb->additional->rdata[2]);
363 DEBUG(3,("Name release on name %s rcode=%d\n",
364 namestr(&nmb->question.question_name),rcode));
366 n = find_name_search(&nmb->question.question_name, FIND_GLOBAL, ip);
368 /* XXXX under what conditions should we reject the removal?? */
369 if (n && n->nb_flags == nb_flags && ip_equal(n->ip,ip))
384 putip(&rdata[2],(char *)&ip);
387 /* Send a NAME RELEASE RESPONSE */
388 reply_netbios_packet(p,nmb->header.name_trn_id,rcode,opcode,
389 &nmb->question.question_name,
390 nmb->question.question_type,
391 nmb->question.question_class,
393 rdata, 6 /*success ? 6 : 0*/);
394 /* XXXX reject packet never tested: cannot tell what to do */
398 /****************************************************************************
399 response for a reg request received
400 **************************************************************************/
401 void response_name_reg(struct packet_struct *p)
403 struct nmb_packet *nmb = &p->packet.nmb;
404 char *name = nmb->question.question_name.name;
405 int type = nmb->question.question_name.name_type;
407 DEBUG(4,("response name registration received!\n"));
409 if (nmb->header.rcode == 0 && nmb->answers->rdata)
411 int nb_flags = nmb->answers->rdata[0];
412 struct in_addr found_ip;
413 int ttl = nmb->answers->ttl;
414 enum name_source source = REGISTER;
416 putip((char*)&found_ip,&nmb->answers->rdata[2]);
418 if (ismyip(found_ip)) source = SELF;
420 add_netbios_entry(name,type,nb_flags,ttl,source,found_ip,True);
424 DEBUG(1,("name registration for %s rejected!\n",
425 namestr(&nmb->question.question_name)));
430 /****************************************************************************
431 reply to a reg request
432 **************************************************************************/
433 void reply_name_reg(struct packet_struct *p)
435 struct nmb_packet *nmb = &p->packet.nmb;
436 struct nmb_name *question = &nmb->question.question_name;
437 char *qname = nmb->question.question_name.name;
438 int name_type = nmb->question.question_name.name_type;
440 BOOL bcast = nmb->header.nm_flags.bcast;
442 int ttl = GET_TTL(nmb->additional->ttl);
443 int nb_flags = nmb->additional->rdata[0];
444 BOOL group = (nb_flags&0x80);
446 int opcode = nmb->header.opcode;
447 struct name_record *n = NULL;
450 struct in_addr ip, from_ip;
452 putip((char *)&from_ip,&nmb->additional->rdata[2]);
455 DEBUG(3,("Name registration for name %s at %s rcode=%d\n",
456 namestr(question),inet_ntoa(ip),rcode));
460 /* apparently we should return 255.255.255.255 for group queries
462 ip = *interpret_addr2("255.255.255.255");
465 /* see if the name already exists */
466 n = find_name_search(question, FIND_GLOBAL, from_ip);
470 if (!group && !ip_equal(ip,n->ip) && question->name_type != 0x3)
472 if (n->source == SELF)
480 n->death_time = ttl?p->timestamp+ttl*3:0;
481 DEBUG(3,("%s changed owner to %s\n",
482 namestr(&n->name),inet_ntoa(n->ip)));
487 /* refresh the name */
488 if (n->source != SELF)
490 n->death_time = ttl?p->timestamp + ttl*3:0;
496 /* add the name to our subnet/name database */
497 n = add_netbios_entry(qname,name_type,nb_flags,ttl,REGISTER,ip,False);
502 update_from_reg(nmb->question.question_name.name,
503 nmb->question.question_name.name_type, from_ip);
505 /* XXXX don't know how to reject a name register: stick info in anyway
506 and guess that it doesn't matter if info is there! */
511 putip(&rdata[2],(char *)&ip);
514 /* Send a NAME REGISTRATION RESPONSE */
515 reply_netbios_packet(p,nmb->header.name_trn_id,rcode,opcode,
516 &nmb->question.question_name,
517 nmb->question.question_type,
518 nmb->question.question_class,
520 rdata, 6 /*success ? 6 : 0*/);
524 /****************************************************************************
525 reply to a name status query
526 ****************************************************************************/
527 void reply_name_status(struct packet_struct *p)
529 struct nmb_packet *nmb = &p->packet.nmb;
530 char *qname = nmb->question.question_name.name;
531 int ques_type = nmb->question.question_name.name_type;
532 BOOL wildcard = (qname[0] == '*');
533 char rdata[MAX_DGRAM_SIZE];
534 char *countptr, *buf;
535 int count, names_added;
536 struct name_record *n;
538 DEBUG(3,("Name status for name %s %s\n",
539 namestr(&nmb->question.question_name), inet_ntoa(p->ip)));
541 /* find a name: if it's a wildcard, search the entire database.
542 if not, search for source SELF names only */
543 n = find_name_search(&nmb->question.question_name,
544 wildcard ? FIND_GLOBAL : FIND_SELF, p->ip);
546 if (!wildcard && (!n || n->source != SELF)) return;
548 for (count=0, n = namelist ; n; n = n->next)
550 int name_type = n->name.name_type;
552 if (n->source != SELF) continue;
554 if (name_type >= 0x1b && name_type <= 0x20 &&
555 ques_type >= 0x1b && ques_type <= 0x20)
557 if (!strequal(qname, n->name.name)) continue;
563 /* XXXX hack, we should calculate exactly how many will fit */
564 count = MIN(count,(sizeof(rdata) - 64) / 18);
566 countptr = buf = rdata;
571 for (n = namelist ; n && count >= 0; n = n->next)
573 int name_type = n->name.name_type;
575 if (n->source != SELF) continue;
577 /* start with first bit of putting info in buffer: the name */
580 StrnCpy(buf,n->name.name,15);
583 /* now check if we want to exclude other workgroup names
584 from the response. if we don't exclude them, windows clients
585 get confused and will respond with an error for NET VIEW */
587 if (name_type >= 0x1b && name_type <= 0x20 &&
588 ques_type >= 0x1b && ques_type <= 0x20)
590 if (!strequal(qname, n->name.name)) continue;
593 /* carry on putting name info in buffer */
596 buf[16] = n->nb_flags;
606 DEBUG(3, (("too many names: missing a few!\n")));
609 SCVAL(countptr,0,names_added);
611 /* XXXXXXX we should fill in more fields of the statistics structure */
614 extern int num_good_sends,num_good_receives;
615 SIVAL(buf,20,num_good_sends);
616 SIVAL(buf,24,num_good_receives);
619 SIVAL(buf,46,0xFFB8E5); /* undocumented - used by NT */
623 /* Send a POSITIVE NAME STATUS RESPONSE */
624 reply_netbios_packet(p,nmb->header.name_trn_id,0,0,
625 &nmb->question.question_name,
626 nmb->question.question_type,
627 nmb->question.question_class,
629 rdata,PTR_DIFF(buf,rdata));
633 /***************************************************************************
634 reply to a name query
635 ****************************************************************************/
636 struct name_record *search_for_name(struct nmb_name *question,
637 struct in_addr ip, int Time, int search)
639 int name_type = question->name_type;
640 char *qname = question->name;
641 BOOL dns_type = name_type == 0x20 || name_type == 0;
643 struct name_record *n;
645 DEBUG(3,("Search for %s from %s - ", namestr(question), inet_ntoa(ip)));
647 /* first look up name in cache */
648 n = find_name_search(question,search,ip);
650 /* now try DNS lookup. */
653 struct in_addr dns_ip;
656 /* only do DNS lookups if the query is for type 0x20 or type 0x0 */
659 DEBUG(3,("types 0x20 0x1b 0x0 only: name not found\n"));
663 /* look it up with DNS */
664 a = interpret_addr(qname);
666 putip((char *)&dns_ip,(char *)&a);
670 /* no luck with DNS. We could possibly recurse here XXXX */
671 /* if this isn't a bcast then we should send a negative reply XXXX */
672 DEBUG(3,("no recursion\n"));
673 add_netbios_entry(qname,name_type,NB_ACTIVE,60*60,DNSFAIL,
678 /* add it to our cache of names. give it 2 hours in the cache */
679 n = add_netbios_entry(qname,name_type,NB_ACTIVE,2*60*60,DNS,
682 /* failed to add it? yikes! */
686 /* is our entry already dead? */
689 if (n->death_time < Time) return False;
692 /* it may have been an earlier failure */
693 if (n->source == DNSFAIL)
695 DEBUG(3,("DNSFAIL\n"));
699 DEBUG(3,("OK %s\n",inet_ntoa(n->ip)));
705 /***************************************************************************
706 reply to a name query.
708 with broadcast name queries:
710 - only reply if the query is for one of YOUR names. all other machines on
711 the network will be doing the same thing (that is, only replying to a
712 broadcast query if they own it)
713 NOTE: broadcast name queries should only be sent out by a machine
714 if they HAVEN'T been configured to use WINS. this is generally bad news
715 in a wide area tcp/ip network and should be rectified by the systems
716 administrator. USE WINS! :-)
717 - the exception to this is if the query is for a Primary Domain Controller
718 type name (0x1b), in which case, a reply is sent.
720 - NEVER send a negative response to a broadcast query. no-one else will!
722 with directed name queries:
724 - if you are the WINS server, you are expected to
725 ****************************************************************************/
726 extern void reply_name_query(struct packet_struct *p)
728 struct nmb_packet *nmb = &p->packet.nmb;
729 struct nmb_name *question = &nmb->question.question_name;
730 int name_type = question->name_type;
731 BOOL dns_type = name_type == 0x20 || name_type == 0;
732 BOOL bcast = nmb->header.nm_flags.bcast;
736 struct in_addr retip;
739 struct in_addr gp_ip = *interpret_addr2("255.255.255.255");
742 struct name_record *n;
743 enum name_search search = dns_type || name_type == 0x1b ?
744 FIND_GLOBAL : FIND_SELF;
746 DEBUG(3,("Name query "));
748 if ((n = search_for_name(question,p->ip,p->timestamp, search)))
750 /* don't respond to broadcast queries unless the query is for
751 a name we own or it is for a Primary Domain Controller name */
752 if (bcast && n->source != SELF && name_type != 0x1b)
754 if (!lp_wins_proxy() || same_net(p->ip,n->ip,*iface_nmask(p->ip))) {
755 /* never reply with a negative response to broadcast queries */
761 ttl = n->death_time - p->timestamp;
763 nb_flags = n->nb_flags;
767 if (bcast) return; /* never reply negative response to bcasts */
771 /* if asking for a group name (type 0x1e) return 255.255.255.255 */
772 if (ip_equal(retip, gp_ip) && name_type == 0x1e) retip = gp_ip;
774 /* if the IP is 0 then substitute my IP */
775 if (zero_ip(retip)) retip = *iface_ip(p->ip);
780 DEBUG(3,("OK %s\n",inet_ntoa(retip)));
785 DEBUG(3,("UNKNOWN\n"));
792 putip(&rdata[2],(char *)&retip);
795 reply_netbios_packet(p,nmb->header.name_trn_id,rcode,0,
796 &nmb->question.question_name,
797 nmb->question.question_type,
798 nmb->question.question_class,
800 rdata, success ? 6 : 0);
804 /****************************************************************************
805 response from a name query
806 ****************************************************************************/
807 static void response_netbios_packet(struct packet_struct *p)
809 struct nmb_packet *nmb = &p->packet.nmb;
810 struct nmb_name *question = &nmb->question.question_name;
811 char *qname = question->name;
812 BOOL bcast = nmb->header.nm_flags.bcast;
813 struct name_response_record *n;
815 if (nmb->answers == NULL)
817 DEBUG(3,("NMB packet response from %s (bcast=%s) - UNKNOWN\n",
823 if (nmb->answers->rr_type == NMB_STATUS) {
824 DEBUG(3,("Name status "));
827 if (nmb->answers->rr_type == NMB_QUERY) {
828 DEBUG(3,("Name query "));
831 if (nmb->answers->rr_type == NMB_REG) {
832 DEBUG(3,("Name registration "));
835 if (nmb->answers->rr_type == NMB_REL) {
836 DEBUG(3,("Name release "));
839 DEBUG(3,("response for %s from %s (bcast=%s)\n",
840 namestr(&nmb->answers->rr_name),
844 if (!(n = find_name_query(nmb->header.name_trn_id))) {
845 DEBUG(3,("unknown response (received too late or from nmblookup?)\n"));
849 n->num_msgs++; /* count number of responses received */
853 case MASTER_SERVER_CHECK : DEBUG(4,("MASTER_SVR_CHECK\n")); break;
854 case SERVER_CHECK : DEBUG(4,("SERVER_CHECK\n")); break;
855 case FIND_MASTER : DEBUG(4,("FIND_MASTER\n")); break;
856 case NAME_STATUS_MASTER_CHECK: DEBUG(4,("NAME_STAT_MST_CHK\n")); break;
857 case NAME_STATUS_CHECK : DEBUG(4,("NAME_STATUS_CHECK\n")); break;
858 case CHECK_MASTER : DEBUG(4,("CHECK_MASTER\n")); break;
859 case NAME_CONFIRM_QUERY : DEBUG(4,("NAME_CONFIRM_QUERY\n")); break;
864 case MASTER_SERVER_CHECK:
868 if (nmb->answers->rr_type == NMB_QUERY)
870 enum cmd_type cmd = (n->cmd_type == MASTER_SERVER_CHECK) ?
871 NAME_STATUS_MASTER_CHECK :
873 if (n->num_msgs > 1 && !strequal(qname,n->name.name))
875 /* one subnet, one master browser per workgroup */
876 /* XXXX force an election? */
877 DEBUG(1,("more than one master browser replied!\n"));
880 /* initiate a name status check on the server that replied */
881 queue_netbios_packet(ClientNMB,NMB_STATUS, cmd,
882 nmb->answers->rr_name.name,
883 nmb->answers->rr_name.name_type,0,
884 False,False,n->to_ip);
888 DEBUG(1,("Name query reply has wrong answer rr_type\n"));
893 case NAME_STATUS_MASTER_CHECK:
894 case NAME_STATUS_CHECK:
896 if (nmb->answers->rr_type == NMB_STATUS)
898 /* NMB_STATUS arrives: contains the workgroup name
899 and server name we require */
900 struct nmb_name name;
903 if (interpret_node_status(nmb->answers->rdata,
904 &name,0x1d,serv_name,p->ip))
908 sync_server(n->cmd_type,serv_name,
909 name.name,name.name_type,
915 DEBUG(1,("No 0x1d name type in interpret_node_status()\n"));
920 DEBUG(1,("Name status reply has wrong answer rr_type\n"));
927 /* no action required here. it's when NO responses are received
928 that we need to do something (see expire_name_query_entries) */
930 DEBUG(4, ("Master browser exists for %s at %s\n",
932 inet_ntoa(n->to_ip)));
935 DEBUG(1,("more than one master browser!\n"));
937 if (nmb->answers->rr_type != NMB_QUERY)
939 DEBUG(1,("Name query reply has wrong answer rr_type\n"));
943 case NAME_CONFIRM_QUERY:
945 DEBUG(4, ("Name query at WINS server: %s at %s - ",
947 inet_ntoa(n->to_ip)));
948 if (nmb->header.rcode == 0 && nmb->answers->rdata)
950 int nb_flags = nmb->answers->rdata[0];
951 struct in_addr found_ip;
952 putip((char*)&found_ip,&nmb->answers->rdata[2]);
954 DEBUG(4, (" OK: %s\n", inet_ntoa(found_ip)));
955 add_netbios_entry(nmb->answers->rr_name.name,
956 nmb->answers->rr_name.name_type,
957 nb_flags,GET_TTL(0),STATUS_QUERY,found_ip,False);
961 DEBUG(4, (" NEGATIVE RESPONSE\n"));
968 DEBUG(0,("unknown command received in response_netbios_packet\n"));
975 /****************************************************************************
977 ****************************************************************************/
978 void process_nmb(struct packet_struct *p)
980 struct nmb_packet *nmb = &p->packet.nmb;
984 switch (nmb->header.opcode)
990 if (nmb->header.qdcount==0 || nmb->header.arcount==0) break;
991 if (nmb->header.response)
992 response_name_reg(p);
1000 if (nmb->header.response)
1002 switch (nmb->question.question_type)
1006 response_netbios_packet(p);
1012 else if (nmb->header.qdcount>0)
1014 switch (nmb->question.question_type)
1018 reply_name_query(p);
1023 reply_name_status(p);
1034 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1036 DEBUG(2,("netbios release packet rejected\n"));
1040 if (nmb->header.response)
1041 response_name_release(p);
1043 reply_name_release(p);