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

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    NBT netbios routines to synchronise browse lists
  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. */
  22.  
  23. /* We *must have REPLACE_GETPASS defined here before the includes. */
  24. #define REPLACE_GETPASS
  25. #include "includes.h"
  26.  
  27. extern int ClientNMB;
  28. extern int ClientDGRAM;
  29.  
  30. extern int DEBUGLEVEL;
  31.  
  32. extern pstring myname;
  33.  
  34. extern int name_type;
  35. extern int max_protocol;
  36. extern struct in_addr dest_ip;
  37. extern int pid;
  38. extern int gid;
  39. extern int uid;
  40. extern int mid;
  41. extern BOOL got_pass;
  42. extern BOOL have_ip;
  43. extern pstring workgroup;
  44. extern pstring service;
  45. extern pstring desthost;
  46. extern BOOL connect_as_ipc;
  47.  
  48. /****************************************************************************
  49. fudge for getpass function
  50. ****************************************************************************/
  51. char *getsmbpass(char *pass)
  52. {
  53.     return "dummy"; /* return anything: it should be ignored anyway */
  54. }
  55.  
  56. /****************************************************************************
  57. adds information retrieved from a NetServerEnum call
  58. ****************************************************************************/
  59. static BOOL add_info(struct subnet_record *d, struct work_record *work, int servertype)
  60. {
  61.   char *rparam = NULL;
  62.   char *rdata = NULL;
  63.   int rdrcnt,rprcnt;
  64.   char *p;
  65.   pstring param;
  66.   int uLevel = 1;
  67.   int count = -1;
  68.   
  69.   /* now send a SMBtrans command with api ServerEnum? */
  70.   p = param;
  71.   SSVAL(p,0,0x68); /* api number */
  72.   p += 2;
  73.   strcpy(p,"WrLehDz");
  74.   p = skip_string(p,1);
  75.   
  76.   strcpy(p,"B16BBDz");
  77.   
  78.   p = skip_string(p,1);
  79.   SSVAL(p,0,uLevel);
  80.   SSVAL(p,2,BUFFER_SIZE - SAFETY_MARGIN); /* buf length */
  81.   p += 4;
  82.   SIVAL(p,0,servertype);
  83.   p += 4;
  84.   
  85.   pstrcpy(p, work->work_group);
  86.   p = skip_string(p,1);
  87.   
  88.   if (cli_call_api(PTR_DIFF(p,param),0, 8,BUFFER_SIZE - SAFETY_MARGIN,
  89.            &rprcnt,&rdrcnt, param,NULL,
  90.            &rparam,&rdata))
  91.     {
  92.       int res = SVAL(rparam,0);
  93.       int converter=SVAL(rparam,2);
  94.       int i;
  95.       
  96.       if (res == 0)
  97.     {
  98.       count=SVAL(rparam,4);
  99.       p = rdata;
  100.       
  101.       for (i = 0;i < count;i++, p += 26)
  102.         {
  103.           char *sname = p;
  104.           uint32 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
  105.           int comment_offset = IVAL(p,22) & 0xFFFF;
  106.           char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
  107.           
  108.           struct work_record *w = work;
  109.           
  110.           DEBUG(4, ("\t%-16.16s     %08x    %s\n", sname, stype, cmnt));
  111.           
  112.           if (stype & SV_TYPE_DOMAIN_ENUM)
  113.         {
  114.           /* creates workgroup on remote subnet */
  115.           if ((w = find_workgroupstruct(d,sname,True)))
  116.             {
  117.               announce_request(w, d->bcast_ip);
  118.             }
  119.         }
  120.           
  121.           if (w)
  122.             add_server_entry(d,w,sname,stype,lp_max_ttl(),cmnt,False);
  123.         }
  124.     }
  125.     }
  126.   
  127.   if (rparam) free(rparam);
  128.   if (rdata) free(rdata);
  129.   
  130.   return(True);
  131. }
  132.  
  133.  
  134. /*******************************************************************
  135.   synchronise browse lists with another browse server.
  136.  
  137.   log in on the remote server's SMB port to their IPC$ service,
  138.   do a NetServerEnum and update our server and workgroup databases.
  139.   ******************************************************************/
  140. void sync_browse_lists(struct subnet_record *d, struct work_record *work,
  141.         char *name, int nm_type, struct in_addr ip, BOOL local)
  142. {
  143.   uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
  144.  
  145.   if (!d || !work ) return;
  146.  
  147.   if(d != wins_subnet) {
  148.       DEBUG(0,
  149.         ("sync_browse_lists: ERROR sync requested on non-WINS subnet.\n"));
  150.       return;
  151.   }
  152.  
  153.   pid = getpid();
  154.   uid = getuid();
  155.   gid = getgid();
  156.   mid = pid + 100;
  157.   name_type = nm_type;
  158.   
  159.   got_pass = True;
  160.   
  161.   DEBUG(0,("sync_browse_lists: Sync browse lists with %s for %s %s\n",
  162.         name, work->work_group, inet_ntoa(ip)));
  163.   
  164.   strcpy(workgroup,work->work_group);
  165.   fstrcpy(desthost,name);
  166.   dest_ip = ip;
  167.   
  168.   if (zero_ip(dest_ip)) return;
  169.   have_ip = True;
  170.   
  171.   connect_as_ipc = True;
  172.   
  173.   /* connect as server and get domains, then servers */
  174.   
  175.   sprintf(service,"\\\\%s\\IPC$", name);
  176.   strupper(service);
  177.   
  178.   if (cli_open_sockets(SMB_PORT))
  179.     {
  180.       if (cli_send_login(NULL,NULL,True,True))
  181.       {
  182.         add_info(d, work, local_type|SV_TYPE_DOMAIN_ENUM);
  183.         if(local)
  184.           add_info(d, work, SV_TYPE_LOCAL_LIST_ONLY);
  185.         else
  186.           add_info(d, work, SV_TYPE_ALL);
  187.       }
  188.       
  189.     close_sockets();
  190.   }
  191. }
  192.