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