home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / samba-1.9.17p4 / source / namepacket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-30  |  16.9 KB  |  643 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    NBT netbios routines and daemon - version 2
  5.    Copyright (C) Andrew Tridgell 1994-1997
  6.    
  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.
  11.    
  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.
  16.    
  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.
  20.    
  21.    Revision History:
  22.  
  23.    14 jan 96: lkcl@pires.co.uk
  24.    added multiple workgroup domain master support
  25.  
  26. */
  27.  
  28. #include "includes.h"
  29.  
  30. extern int ClientNMB;
  31. extern int ClientDGRAM;
  32.  
  33. extern int DEBUGLEVEL;
  34.  
  35. extern int num_response_packets;
  36.  
  37. BOOL CanRecurse = True;
  38. extern pstring scope;
  39. extern struct in_addr wins_ip;
  40. extern struct in_addr loopback_ip;
  41.  
  42. static uint16 name_trn_id=0;
  43.  
  44.  
  45. /***************************************************************************
  46.   updates the unique transaction identifier
  47.   **************************************************************************/
  48. void debug_browse_data(char *outbuf, int len)
  49. {
  50.     int i,j;
  51.     for (i = 0; i < len; i+= 16)
  52.       {
  53.     DEBUG(4, ("%3x char ", i));
  54.     
  55.     for (j = 0; j < 16; j++)
  56.       {
  57.         unsigned char x = outbuf[i+j];
  58.         if (x < 32 || x > 127) x = '.';
  59.         
  60.         if (i+j >= len) break;
  61.         DEBUG(4, ("%c", x));
  62.       }
  63.     
  64.     DEBUG(4, (" hex ", i));
  65.     
  66.     for (j = 0; j < 16; j++)
  67.       {
  68.         if (i+j >= len) break;
  69.         DEBUG(4, (" %02x", (unsigned char)outbuf[i+j]));
  70.       }
  71.     
  72.     DEBUG(4, ("\n"));
  73.       }
  74.     
  75. }
  76.  
  77.  
  78. /***************************************************************************
  79.   updates the unique transaction identifier
  80.   **************************************************************************/
  81. static void update_name_trn_id(void)
  82. {
  83.   if (!name_trn_id)
  84.   {
  85.     name_trn_id = (time(NULL)%(unsigned)0x7FFF) + (getpid()%(unsigned)100);
  86.   }
  87.   name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
  88. }
  89.  
  90.  
  91. /****************************************************************************
  92.   initiate a netbios packet
  93.   ****************************************************************************/
  94. void initiate_netbios_packet(uint16 *id,
  95.                  int fd,int quest_type,char *name,int name_type,
  96.                  int nb_flags,BOOL bcast,BOOL recurse,
  97.                  struct in_addr to_ip)
  98. {
  99.   struct packet_struct p;
  100.   struct nmb_packet *nmb = &p.packet.nmb;
  101.   struct res_rec additional_rec;
  102.   char *packet_type = "unknown";
  103.   int opcode = -1;
  104.  
  105.   if (!id) return;
  106.  
  107.   if (quest_type == NMB_STATUS) { packet_type = "nmb_status"; opcode = 0; }
  108.   if (quest_type == NMB_QUERY ) { packet_type = "nmb_query"; opcode = 0; }
  109.   if (quest_type == NMB_REG   ) { packet_type = "nmb_reg"; opcode = 5; }
  110.   if (quest_type == NMB_REG_REFRESH ) { packet_type = "nmb_reg_refresh"; opcode = 9; }
  111.   if (quest_type == NMB_REL   ) { packet_type = "nmb_rel"; opcode = 6; }
  112.   
  113.   DEBUG(4,("initiating netbios packet: %s %s(%x) (bcast=%s) %s\n",
  114.        packet_type, name, name_type, BOOLSTR(bcast), inet_ntoa(to_ip)));
  115.  
  116.   if (opcode == -1) return;
  117.  
  118.   bzero((char *)&p,sizeof(p));
  119.  
  120.   if (*id == 0xffff) {
  121.     update_name_trn_id();
  122.     *id = name_trn_id; /* allow resending with same id */
  123.   }
  124.  
  125.   nmb->header.name_trn_id = *id;
  126.   nmb->header.opcode = opcode;
  127.   nmb->header.response = False;
  128.  
  129.   nmb->header.nm_flags.bcast = bcast;
  130.   nmb->header.nm_flags.recursion_available = False;
  131.   nmb->header.nm_flags.recursion_desired = recurse;
  132.   nmb->header.nm_flags.trunc = False;
  133.   nmb->header.nm_flags.authoritative = False;
  134.  
  135.   nmb->header.rcode = 0;
  136.   nmb->header.qdcount = 1;
  137.   nmb->header.ancount = 0;
  138.   nmb->header.nscount = 0;
  139.   nmb->header.arcount = (quest_type==NMB_REG || 
  140.              quest_type==NMB_REL ||
  141.              quest_type==NMB_REG_REFRESH) ? 1 : 0;
  142.   
  143.   make_nmb_name(&nmb->question.question_name,name,name_type,scope);
  144.   
  145.   nmb->question.question_type = quest_type == NMB_STATUS ? 0x21 : 0x20;
  146.   nmb->question.question_class = 0x1;
  147.   
  148.   if (quest_type == NMB_REG ||
  149.       quest_type == NMB_REG_REFRESH ||
  150.       quest_type == NMB_REL)
  151.   {
  152.       nmb->additional = &additional_rec;
  153.       bzero((char *)nmb->additional,sizeof(*nmb->additional));
  154.       
  155.       nmb->additional->rr_name  = nmb->question.question_name;
  156.       nmb->additional->rr_type  = 0x20;
  157.       nmb->additional->rr_class = 0x1;
  158.       
  159.       if (quest_type == NMB_REG || quest_type == NMB_REG_REFRESH)
  160.         nmb->additional->ttl = lp_max_ttl();
  161.       else
  162.         nmb->additional->ttl = 0;
  163.  
  164.       nmb->additional->rdlength = 6;
  165.       nmb->additional->rdata[0] = nb_flags;
  166.       putip(&nmb->additional->rdata[2],(char *)iface_ip(to_ip));
  167.   }
  168.   
  169.   p.ip = to_ip;
  170.   p.port = NMB_PORT;
  171.   p.fd = fd;
  172.   p.timestamp = time(NULL);
  173.   p.packet_type = NMB_PACKET;
  174.   
  175.   debug_nmb_packet(&p);
  176.   
  177.   if (!send_packet(&p)) {
  178.     DEBUG(3,("send_packet to %s %d failed\n",inet_ntoa(p.ip),p.port));
  179.     *id = 0xffff;
  180.   }
  181.   
  182.   return;
  183. }
  184.  
  185.  
  186. /****************************************************************************
  187.   reply to a netbios name packet.  see rfc1002.txt
  188.   ****************************************************************************/
  189. void reply_netbios_packet(struct packet_struct *p1,int trn_id,
  190.                 int rcode, int rcv_code, int opcode,
  191.                 BOOL recursion_available,
  192.                 BOOL recursion_desired,
  193.                 struct nmb_name *rr_name,int rr_type,int rr_class,int ttl,
  194.                 char *data,int len)
  195. {
  196.   struct packet_struct p;
  197.   struct nmb_packet *nmb = &p.packet.nmb;
  198.   struct res_rec answers;
  199.   char *packet_type = "unknown";
  200.   
  201.   p = *p1;
  202.  
  203.   switch (rcv_code)
  204.   {
  205.     case NMB_STATUS:
  206.     {
  207.       packet_type = "nmb_status";
  208.       break;
  209.     }
  210.     case NMB_QUERY:
  211.     {
  212.       packet_type = "nmb_query";
  213.       break;
  214.     }
  215.     case NMB_REG:
  216.     {
  217.       packet_type = "nmb_reg";
  218.       break;
  219.     }
  220.     case NMB_REL:
  221.     {
  222.       packet_type = "nmb_rel";
  223.       break;
  224.     }
  225.     case NMB_WAIT_ACK:
  226.     {
  227.       packet_type = "nmb_wack";
  228.       break;
  229.     }
  230.     default:
  231.     {
  232.       DEBUG(1,("replying netbios packet: %s %s %s\n",
  233.                 packet_type, namestr(rr_name), inet_ntoa(p.ip)));
  234.  
  235.       return;
  236.     }
  237.   }
  238.  
  239.   DEBUG(4,("replying netbios packet: %s %s %s\n",
  240.        packet_type, namestr(rr_name), inet_ntoa(p.ip)));
  241.  
  242.   nmb->header.name_trn_id = trn_id;
  243.   nmb->header.opcode = opcode;
  244.   nmb->header.response = True;
  245.   nmb->header.nm_flags.bcast = False;
  246.   nmb->header.nm_flags.recursion_available = recursion_available;
  247.   nmb->header.nm_flags.recursion_desired = recursion_desired;
  248.   nmb->header.nm_flags.trunc = False;
  249.   nmb->header.nm_flags.authoritative = True;
  250.   
  251.   nmb->header.qdcount = 0;
  252.   nmb->header.ancount = 1;
  253.   nmb->header.nscount = 0;
  254.   nmb->header.arcount = 0;
  255.   nmb->header.rcode = rcode;
  256.   
  257.   bzero((char*)&nmb->question,sizeof(nmb->question));
  258.   
  259.   nmb->answers = &answers;
  260.   bzero((char*)nmb->answers,sizeof(*nmb->answers));
  261.   
  262.   nmb->answers->rr_name  = *rr_name;
  263.   nmb->answers->rr_type  = rr_type;
  264.   nmb->answers->rr_class = rr_class;
  265.   nmb->answers->ttl      = ttl;
  266.   
  267.   if (data && len)
  268.     {
  269.       nmb->answers->rdlength = len;
  270.       memcpy(nmb->answers->rdata, data, len);
  271.     }
  272.   
  273.   p.packet_type = NMB_PACKET;
  274.   
  275.   debug_nmb_packet(&p);
  276.   
  277.   send_packet(&p);
  278. }
  279.  
  280.  
  281. /*******************************************************************
  282.   the global packet linked-list. incoming entries are added to the
  283.   end of this list.  it is supposed to remain fairly short so we
  284.   won't bother with an end pointer.
  285.   ******************************************************************/
  286. static struct packet_struct *packet_queue = NULL;
  287.  
  288. /*******************************************************************
  289.   queue a packet into the packet queue
  290.   ******************************************************************/
  291. void queue_packet(struct packet_struct *packet)
  292. {
  293.   struct packet_struct *p;
  294.  
  295.   if (!packet_queue) {
  296.     packet->prev = NULL;
  297.     packet->next = NULL;
  298.     packet_queue = packet;
  299.     return;
  300.   }
  301.   
  302.   /* find the bottom */
  303.   for (p=packet_queue;p->next;p=p->next) ;
  304.  
  305.   p->next = packet;
  306.   packet->next = NULL;
  307.   packet->prev = p;
  308. }
  309.  
  310. /****************************************************************************
  311.   determine if a packet is for us. Note that to have any chance of
  312.   being efficient we need to drop as many packets as possible at this
  313.   stage as subsequent processing is expensive. 
  314.  
  315.   We also must make absolutely sure we don't tread on another machines
  316.   property by answering a packet that is not for us.
  317.   ****************************************************************************/
  318. static BOOL listening(struct packet_struct *p,struct nmb_name *n)
  319. {
  320.   struct subnet_record *d;
  321.   struct name_record *n1;
  322.  
  323.   /* We explicitly don't search WINS here - this will be done
  324.      in find_name_search if it was a packet from a non-local subnet. */
  325.   d = find_subnet(p->ip);
  326.  
  327.   n1 = find_name_search(&d,n,FIND_LOCAL|FIND_WINS|FIND_SELF,p->ip);
  328.  
  329.   return (n1 != NULL);
  330. }
  331.  
  332.  
  333. /****************************************************************************
  334.   process udp 138 datagrams
  335.   ****************************************************************************/
  336. static void process_dgram(struct packet_struct *p)
  337. {
  338.   char *buf;
  339.   char *buf2;
  340.   int len;
  341.   struct dgram_packet *dgram = &p->packet.dgram;
  342.  
  343.   /* if we aren't listening to the destination name then ignore the packet */
  344.   if (!listening(p,&dgram->dest_name))
  345.   {
  346.     DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s(%x) from %s\n",
  347.            dgram->dest_name.name, dgram->dest_name.name_type, inet_ntoa(p->ip)));
  348.     return;
  349.   }
  350.  
  351.   if (dgram->header.msg_type != 0x10 &&
  352.       dgram->header.msg_type != 0x11 &&
  353.       dgram->header.msg_type != 0x12) 
  354.   {
  355.     /* don't process error packets etc yet */
  356.     DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s(%d) from %s as it is \
  357.            an error packet of type %x\n",
  358.            dgram->dest_name.name, dgram->dest_name.name_type, 
  359.            inet_ntoa(p->ip), dgram->header.msg_type));
  360.     return;
  361.   }
  362.  
  363.   buf = &dgram->data[0];
  364.   buf -= 4; /* XXXX for the pseudo tcp length - 
  365.            someday I need to get rid of this */
  366.  
  367.   if (CVAL(buf,smb_com) != SMBtrans) return;
  368.  
  369.   len = SVAL(buf,smb_vwv11);
  370.   buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
  371.  
  372.   DEBUG(4,("process_dgram: datagram from %s to %s for %s of type %d len=%d\n",
  373.        namestr(&dgram->source_name),namestr(&dgram->dest_name),
  374.        smb_buf(buf),CVAL(buf2,0),len));
  375.  
  376.  
  377.   if (len <= 0) return;
  378.  
  379.    /* datagram packet received for the browser mailslot */
  380.    if (strequal(smb_buf(buf),BROWSE_MAILSLOT)) {
  381.      process_browse_packet(p,buf2,len);
  382.      return;
  383.    }
  384.  
  385.    /* datagram packet received for the domain log on mailslot */
  386.    if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT)) {
  387.      process_logon_packet(p,buf2,len);
  388.      return;
  389.    }
  390. }
  391.  
  392. /****************************************************************************
  393.   process a nmb packet
  394.   ****************************************************************************/
  395. static void process_nmb(struct packet_struct *p)
  396. {
  397.   struct nmb_packet *nmb = &p->packet.nmb;
  398.  
  399.   debug_nmb_packet(p);
  400.  
  401.   switch (nmb->header.opcode) 
  402.   {
  403.     case 8: /* what is this?? */
  404.     case NMB_REG:
  405.     case NMB_REG_REFRESH:
  406.     {
  407.     if (nmb->header.response)
  408.         {
  409.           if (nmb->header.ancount ==0) break;
  410.       response_netbios_packet(p); /* response to registration dealt 
  411.                      with here */
  412.         }
  413.     else
  414.         {
  415.           if (nmb->header.qdcount==0 || nmb->header.arcount==0) break;
  416.       reply_name_reg(p);
  417.         }
  418.     break;
  419.     }
  420.       
  421.     case 0:
  422.     {
  423.       if (nmb->header.response)
  424.       {
  425.         switch (nmb->question.question_type)
  426.           {
  427.           case 0x0:
  428.         {
  429.           response_netbios_packet(p);
  430.           break;
  431.         }
  432.           }
  433.         return;
  434.       }
  435.       else if (nmb->header.qdcount>0) 
  436.       {
  437.         switch (nmb->question.question_type)
  438.           {
  439.           case NMB_QUERY:
  440.         {
  441.           reply_name_query(p);
  442.           break;
  443.         }
  444.           case NMB_STATUS:
  445.         {
  446.           reply_name_status(p);
  447.           break;
  448.         }
  449.           }
  450.         return;
  451.       }
  452.     break;
  453.       }
  454.       
  455.     case NMB_REL:
  456.     {
  457.     if (nmb->header.response)
  458.         {
  459.           if (nmb->header.ancount ==0) break;
  460.       response_netbios_packet(p); /* response to release dealt 
  461.                      with here */
  462.         }
  463.     else
  464.         {
  465.           if (nmb->header.qdcount==0 || nmb->header.arcount==0) break;
  466.       reply_name_release(p);
  467.         }
  468.       break;
  469.     }
  470.   }
  471. }
  472.  
  473.  
  474. /*******************************************************************
  475.   run elements off the packet queue till its empty
  476.   ******************************************************************/
  477. void run_packet_queue()
  478. {
  479.   struct packet_struct *p;
  480.  
  481.   while ((p=packet_queue))
  482.     {
  483.       switch (p->packet_type)
  484.     {
  485.     case NMB_PACKET:
  486.       process_nmb(p);
  487.       break;
  488.       
  489.     case DGRAM_PACKET:
  490.       process_dgram(p);
  491.       break;
  492.     }
  493.       
  494.       packet_queue = packet_queue->next;
  495.       if (packet_queue) packet_queue->prev = NULL;
  496.       free_packet(p);
  497.     }
  498. }
  499.  
  500. /****************************************************************************
  501.   listens for NMB or DGRAM packets, and queues them
  502.   ***************************************************************************/
  503. void listen_for_packets(BOOL run_election)
  504. {
  505.     fd_set fds;
  506.     int selrtn;
  507.     struct timeval timeout;
  508.  
  509.     FD_ZERO(&fds);
  510.     FD_SET(ClientNMB,&fds);
  511.     FD_SET(ClientDGRAM,&fds);
  512.  
  513.     /* during elections and when expecting a netbios response packet we
  514.     need to send election packets at tighter intervals 
  515.  
  516.     ideally it needs to be the interval (in ms) between time now and
  517.     the time we are expecting the next netbios packet */
  518.  
  519.     timeout.tv_sec = (run_election||num_response_packets) ? 1:NMBD_SELECT_LOOP;
  520.     timeout.tv_usec = 0;
  521.  
  522.         /* We can only take term signals when we are in the select. */
  523.         BlockSignals(False, SIGTERM);
  524.     selrtn = sys_select(&fds,&timeout);
  525.         BlockSignals(True, SIGTERM);
  526.  
  527.     if (FD_ISSET(ClientNMB,&fds))
  528.     {
  529.         struct packet_struct *packet = read_packet(ClientNMB, NMB_PACKET);
  530.         if (packet)
  531.         {
  532.             if ((ip_equal(loopback_ip, packet->ip) || 
  533.                  ismyip(packet->ip)) && 
  534.                  packet->port == NMB_PORT)
  535.             {
  536.                 DEBUG(7,("discarding own packet from %s:%d\n",
  537.                           inet_ntoa(packet->ip),packet->port));      
  538.                 free_packet(packet);
  539.             }
  540.             else
  541.             {
  542.                 queue_packet(packet);
  543.             }
  544.         }
  545.     }
  546.  
  547.     if (FD_ISSET(ClientDGRAM,&fds))
  548.     {
  549.         struct packet_struct *packet = read_packet(ClientDGRAM, DGRAM_PACKET);
  550.         if (packet)
  551.         {
  552.             if ((ip_equal(loopback_ip, packet->ip) || 
  553.                  ismyip(packet->ip)) && 
  554.                 packet->port == DGRAM_PORT)
  555.             {
  556.                 DEBUG(7,("discarding own packet from %s:%d\n",
  557.                           inet_ntoa(packet->ip),packet->port));      
  558.                 free_packet(packet);
  559.             }
  560.             else
  561.             {
  562.                 queue_packet(packet);
  563.             }
  564.         }
  565.     }
  566. }
  567.  
  568.  
  569.  
  570. /****************************************************************************
  571.   construct and send a netbios DGRAM
  572.  
  573.   Note that this currently sends all answers to port 138. thats the
  574.   wrong things to do! I should send to the requestors port. XXX
  575.   **************************************************************************/
  576. BOOL send_mailslot_reply(BOOL unique, char *mailslot,int fd,char *buf,int len,char *srcname,
  577.              char *dstname,int src_type,int dest_type,
  578.              struct in_addr dest_ip,struct in_addr src_ip)
  579. {
  580.   struct packet_struct p;
  581.   struct dgram_packet *dgram = &p.packet.dgram;
  582.   char *ptr,*p2;
  583.   char tmp[4];
  584.  
  585.   /* ha ha. no. do NOT send packets to 255.255.255.255: it's a pseudo address */
  586.   if (ip_equal(wins_ip, dest_ip)) return False;
  587.  
  588.   bzero((char *)&p,sizeof(p));
  589.  
  590.   update_name_trn_id();
  591.  
  592.   /* DIRECT GROUP or UNIQUE datagram */
  593.   dgram->header.msg_type = unique ? 0x10 : 0x11; 
  594.   dgram->header.flags.node_type = M_NODE;
  595.   dgram->header.flags.first = True;
  596.   dgram->header.flags.more = False;
  597.   dgram->header.dgm_id = name_trn_id;
  598.   dgram->header.source_ip = src_ip;
  599.   dgram->header.source_port = DGRAM_PORT;
  600.   dgram->header.dgm_length = 0; /* let build_dgram() handle this */
  601.   dgram->header.packet_offset = 0;
  602.   
  603.   make_nmb_name(&dgram->source_name,srcname,src_type,scope);
  604.   make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
  605.  
  606.   ptr = &dgram->data[0];
  607.  
  608.   /* now setup the smb part */
  609.   ptr -= 4; /* XXX ugliness because of handling of tcp SMB length */
  610.   memcpy(tmp,ptr,4);
  611.   set_message(ptr,17,17 + len,True);
  612.   memcpy(ptr,tmp,4);
  613.  
  614.   CVAL(ptr,smb_com) = SMBtrans;
  615.   SSVAL(ptr,smb_vwv1,len);
  616.   SSVAL(ptr,smb_vwv11,len);
  617.   SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
  618.   SSVAL(ptr,smb_vwv13,3);
  619.   SSVAL(ptr,smb_vwv14,1);
  620.   SSVAL(ptr,smb_vwv15,1);
  621.   SSVAL(ptr,smb_vwv16,2);
  622.   p2 = smb_buf(ptr);
  623.   strcpy(p2,mailslot);
  624.   p2 = skip_string(p2,1);
  625.  
  626.   memcpy(p2,buf,len);
  627.   p2 += len;
  628.  
  629.   dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length */
  630.  
  631.   p.ip = dest_ip;
  632.   p.port = DGRAM_PORT;
  633.   p.fd = ClientDGRAM;
  634.   p.timestamp = time(NULL);
  635.   p.packet_type = DGRAM_PACKET;
  636.  
  637.   DEBUG(4,("send mailslot %s from %s %s", mailslot,
  638.                     inet_ntoa(src_ip),namestr(&dgram->source_name)));
  639.   DEBUG(4,("to %s %s\n", inet_ntoa(dest_ip),namestr(&dgram->dest_name)));
  640.  
  641.   return(send_packet(&p));
  642. }
  643.