- added a check for broken RH5 include files. With the standard RH5
[kai/samba.git] / source3 / nmbd / nmbd_browsesync.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Andrew Tridgell 1994-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7    Copyright (C) Jeremy Allison 1994-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 */
24
25 #include "includes.h"
26 #include "smb.h"
27
28 extern int DEBUGLEVEL;
29 extern pstring scope;
30 extern struct in_addr ipzero;
31 extern pstring myname;
32 extern fstring myworkgroup;
33
34 /* This is our local master browser list database. */
35 extern struct browse_cache_record *lmb_browserlist;
36
37 static struct work_record *call_work;
38 static struct subnet_record *call_subrec;
39
40 /*******************************************************************
41   This is the NetServerEnum callback.
42   ******************************************************************/
43
44 static void callback(char *sname, uint32 stype, char *comment)
45 {
46   struct work_record *work;
47
48   stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
49
50   if (stype & SV_TYPE_DOMAIN_ENUM) 
51   {
52     /* See if we can find the workgroup on this subnet. */
53     if(( work = find_workgroup_on_subnet( call_subrec, sname )) != NULL)
54     {
55       /* We already know about this workgroup - update the ttl. */
56       update_workgroup_ttl( work, lp_max_ttl() );
57     }
58     else
59     {
60       /* Create the workgroup on the subnet. */
61       create_workgroup_on_subnet( call_subrec, sname, lp_max_ttl() );
62     }
63   }
64   else
65   {
66     /* Server entry. */
67     struct server_record *servrec;
68
69     work = call_work;
70
71     if(( servrec = find_server_in_workgroup( work, sname )) != NULL)
72     {
73       /* Check that this is not a locally known server - if so ignore the
74          entry. */
75       if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY))
76       {
77         /* We already know about this server - update the ttl. */
78         update_server_ttl(servrec, lp_max_ttl() );
79         /* Update the type. */
80         servrec->serv.type = stype;
81       }
82     }
83     else
84     {
85       /* Create the server in the workgroup. */ 
86       create_server_on_workgroup(work, sname,stype,lp_max_ttl(),comment);
87     }
88   }
89 }
90
91 /*******************************************************************
92   Synchronise browse lists with another browse server.
93   Log in on the remote server's SMB port to their IPC$ service,
94   do a NetServerEnum and update our server and workgroup databases.
95 ******************************************************************/
96
97 static void sync_browse_lists(struct subnet_record *subrec, struct work_record *work,
98                        char *name, int nm_type, struct in_addr ip, BOOL local)
99 {
100   extern fstring local_machine;
101   static struct cli_state cli;
102   uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
103
104   DEBUG(2,("%s: sync_browse_lists: Sync browse lists with server %s<%02x> at IP %s for workgroup %s\n",
105      timestring(), name, nm_type, inet_ntoa(ip), work->work_group ));
106
107   /* Check we're not trying to sync with ourselves. This can happen if we are
108      a domain *and* a local master browser. */
109   if(ismyip(ip))
110   {
111     DEBUG(2,("sync_browse_lists: We are both a domain and a local master browser for workgroup %s. \
112 Do not sync with ourselves.\n", work->work_group ));
113     return;
114   }
115
116   if (!cli_initialise(&cli) || !cli_connect(&cli, name, &ip))
117   {
118     DEBUG(0,("sync_browse_lists: Failed to start browse sync with %s\n", name));
119     return;
120   }
121
122   if (!cli_session_request(&cli, name, nm_type, local_machine))
123   {
124     DEBUG(0,("sync_browse_lists: %s rejected the browse sync session\n",name));
125     cli_shutdown(&cli);
126     return;
127   }
128
129   if (!cli_negprot(&cli))
130   {
131     DEBUG(0,("sync_browse_lists: %s rejected the negprot\n",name));
132     cli_shutdown(&cli);
133     return;
134   }
135
136   if (!cli_session_setup(&cli, "", "", 1, "", 0, work->work_group))
137   {
138     DEBUG(0,("sync_browse_lists: %s rejected the browse sync sessionsetup\n", 
139              name));
140     cli_shutdown(&cli);
141     return;
142   }
143
144   if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1))
145   {
146     DEBUG(0,("sync_browse_lists: %s refused browse sync IPC$ connect\n", name));
147     cli_shutdown(&cli);
148     return;
149   }
150
151   call_work = work;
152   call_subrec = subrec;
153
154   /* Fetch a workgroup list. */
155   cli_NetServerEnum(&cli, work->work_group, 
156                     local_type|SV_TYPE_DOMAIN_ENUM,
157                     callback);
158
159   /* Now fetch a server list. */
160   cli_NetServerEnum(&cli, work->work_group, 
161                     local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
162                     callback);
163
164   cli_shutdown(&cli);
165 }
166
167 /****************************************************************************
168 As a domain master browser, do a sync with a local master browser.
169 **************************************************************************/
170
171 static void sync_with_lmb(struct browse_cache_record *browc)
172 {                     
173   struct work_record *work;
174
175   if (!(work = find_workgroup_on_subnet(unicast_subnet, browc->work_group))) {
176       DEBUG(0, ("sync_with_lmb: failed to get a \
177 workgroup for a local master browser cache entry workgroup %s, server %s\n", 
178                 browc->work_group, browc->lmb_name));
179       return;
180   }
181
182   /* We should only be doing this if we are a domain master browser for
183      the given workgroup. Ensure this is so. */
184
185   if(!AM_DOMAIN_MASTER_BROWSER(work))
186   {
187     DEBUG(0,("sync_with_lmb: We are trying to sync with a local master browser %s \
188 for workgroup %s and we are not a domain master browser on this workgroup. Error !\n",
189         browc->lmb_name, browc->work_group));
190     return;
191   }
192
193   DEBUG(2, ("sync_with_lmb: Initiating sync with local master browser %s<0x20> at IP %s for \
194 workgroup %s\n", browc->lmb_name, inet_ntoa(browc->ip), browc->work_group));
195
196   sync_browse_lists(unicast_subnet, work, browc->lmb_name, 0x20, browc->ip, True);
197
198   browc->sync_time += (CHECK_TIME_DMB_TO_LMB_SYNC * 60);
199 }
200
201 /****************************************************************************
202 Sync or expire any local master browsers.
203 **************************************************************************/
204
205 void dmb_expire_and_sync_browser_lists(time_t t)
206 {
207   static time_t last_run = 0;
208   struct browse_cache_record *browc;
209
210   /* Only do this every 20 seconds. */  
211   if (t - last_run < 20) 
212    return;
213
214   last_run = t;
215
216   expire_lmb_browsers(t);
217
218   for (browc = lmb_browserlist; browc; browc = browc->next)
219   {
220     if (browc->sync_time < t)
221       sync_with_lmb(browc);
222   }
223 }
224
225 /****************************************************************************
226 As a local master browser, send an announce packet to the domain master browser.
227 **************************************************************************/
228
229 static void announce_local_master_browser_to_domain_master_browser( struct work_record *work)
230 {
231   pstring outbuf;
232   char *p;
233
234   if(ismyip(work->dmb_addr))
235   {
236     DEBUG(2,("announce_local_master_browser_to_domain_master_browser: We are both a domain \
237 and a local master browser for workgroup %s. \
238 Do not announce to ourselves.\n", work->work_group ));
239     return;
240   }
241
242   bzero(outbuf,sizeof(outbuf));
243   p = outbuf;
244   CVAL(p,0) = ANN_MasterAnnouncement;
245   p++;
246
247   StrnCpy(p,myname,15);
248   strupper(p);
249   p = skip_string(p,1);
250
251   DEBUG(4,("announce_local_master_browser_to_domain_master_browser: Sending local master announce \
252 to %s for workgroup %s.\n", namestr(&work->dmb_name), work->work_group ));
253
254   send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
255           myname, 0x0, work->dmb_name.name, 0x0, work->dmb_addr, FIRST_SUBNET->myip);
256
257 }
258
259 /****************************************************************************
260 As a local master browser, do a sync with a domain master browser.
261 **************************************************************************/
262
263 static void sync_with_dmb(struct work_record *work)
264 {
265   DEBUG(2, ("sync_with_dmb: Initiating sync with domain master browser %s at IP %s for \
266 workgroup %s\n", namestr(&work->dmb_name), inet_ntoa(work->dmb_addr), work->work_group));
267
268   sync_browse_lists(unicast_subnet, work, work->dmb_name.name, work->dmb_name.name_type, 
269                     work->dmb_addr, False);
270 }
271
272 /****************************************************************************
273   Function called when a node status query to a domain master browser IP succeeds.
274 ****************************************************************************/
275
276 static void domain_master_node_status_success(struct subnet_record *subrec,
277                                               struct userdata_struct *userdata,
278                                               struct res_rec *answers,
279                                               struct in_addr from_ip)
280 {
281   struct work_record *work = find_workgroup_on_subnet( subrec, userdata->data);
282
283   if(work == NULL)
284   {
285     DEBUG(0,("domain_master_node_status_success: Unable to find workgroup %s on subnet %s.\n",
286               userdata->data, subrec->subnet_name));
287     return;
288   }
289
290   DEBUG(3,("domain_master_node_status_success: Success in node status for workgroup %s from ip %s\n",
291             work->work_group, inet_ntoa(from_ip) ));
292
293   /* Go through the list of names found at answers->rdata and look for
294      the first SERVER<0x20> name. */
295
296   if(answers->rdata != NULL)
297   {
298     char *p = answers->rdata;
299     int numnames = CVAL(p, 0);
300
301     p += 1;
302
303     while (numnames--)
304     {
305       char qname[17];
306       uint16 nb_flags;
307       int name_type;
308
309       StrnCpy(qname,p,15);
310       name_type = CVAL(p,15);
311       nb_flags = get_nb_flags(&p[16]);
312       trim_string(qname,NULL," ");
313
314       p += 18;
315
316       if(!(nb_flags & NB_GROUP) && (name_type == 0x20))
317       {
318         struct nmb_name nmbname;
319
320         make_nmb_name(&nmbname, qname, name_type, scope);
321
322         /* Copy the dmb name and IP address
323            into the workgroup struct. */
324
325         work->dmb_name = nmbname;
326         putip((char *)&work->dmb_addr, &from_ip);
327
328         /* Do the local master browser announcement to the domain
329            master browser name and IP. */
330         announce_local_master_browser_to_domain_master_browser( work );
331
332         /* Now synchronise lists with the domain master browser. */
333         sync_with_dmb(work);
334         break;
335       }
336     }
337   }
338   else
339     DEBUG(0,("domain_master_node_status_success: Failed to find a SERVER<0x20> \
340 name in reply from IP %s.\n", inet_ntoa(from_ip) ));
341 }
342
343 /****************************************************************************
344   Function called when a node status query to a domain master browser IP fails.
345 ****************************************************************************/
346
347 static void domain_master_node_status_fail(struct subnet_record *subrec,
348                        struct response_record *rrec)
349 {
350   struct userdata_struct *userdata = rrec->userdata;
351
352   DEBUG(0,("domain_master_node_status_fail: Doing a node status request to \
353 the domain master browser for workgroup %s at IP %s failed. Cannot sync browser \
354 lists.\n", userdata->data, inet_ntoa(rrec->packet->ip) ));
355
356 }
357
358 /****************************************************************************
359   Function called when a query for a WORKGROUP<1b> name succeeds.
360 ****************************************************************************/
361
362 static void find_domain_master_name_query_success(struct subnet_record *subrec,
363                         struct userdata_struct *userdata_in,
364                         struct nmb_name *q_name, struct in_addr answer_ip, struct res_rec *rrec)
365 {
366   /* 
367    * Unfortunately, finding the IP address of the Domain Master Browser,
368    * as we have here, is not enough. We need to now do a sync to the
369    * SERVERNAME<0x20> NetBIOS name, as only recent NT servers will
370    * respond to the SMBSERVER name. To get this name from IP
371    * address we do a Node status request, and look for the first
372    * NAME<0x20> in the response, and take that as the server name.
373    * We also keep a cache of the Domain Master Browser name for this
374    * workgroup in the Workgroup struct, so that if the same IP addess
375    * is returned every time, we don't need to do the node status
376    * request.
377    */
378
379   struct work_record *work;
380   struct nmb_name nmbname;
381   struct userdata_struct *userdata;
382
383   if (!(work = find_workgroup_on_subnet(subrec, q_name->name))) {
384       DEBUG(0, ("find_domain_master_name_query_success: failed to find \
385 workgroup %s\n", q_name->name ));
386     return;
387   }
388
389   /* First check if we already have a dmb for this workgroup. */
390
391   if(!ip_equal(work->dmb_addr, ipzero) && ip_equal(work->dmb_addr, answer_ip))
392   {
393     /* Do the local master browser announcement to the domain
394        master browser name and IP. */
395     announce_local_master_browser_to_domain_master_browser( work );
396
397     /* Now synchronise lists with the domain master browser. */
398     sync_with_dmb(work);
399     return;
400   }
401   else
402     putip((char *)&work->dmb_addr, &ipzero);
403
404   /* Now initiate the node status request. */
405   bzero((char *)&nmbname, sizeof(nmbname));
406   nmbname.name[0] = '*';
407
408   /* Put the workgroup name into the userdata so we know
409      what workgroup we're talking to when the reply comes
410      back. */
411
412   /* Setup the userdata_struct - this is copied so we can use
413      a stack variable for this. */
414   if((userdata = (struct userdata_struct *)malloc(sizeof(struct userdata_struct) + sizeof(fstring)+1)) == NULL)
415   {
416     DEBUG(0, ("find_domain_master_name_query_success: malloc fail.\n"));
417     return;
418   }
419
420   userdata->copy_fn = NULL;
421   userdata->free_fn = NULL;
422   userdata->userdata_len = strlen(work->work_group)+1;
423   strcpy(userdata->data, work->work_group);
424
425   node_status( subrec, &nmbname, answer_ip, 
426                domain_master_node_status_success,
427                domain_master_node_status_fail,
428                userdata);
429
430   free((char *)userdata);
431 }
432
433 /****************************************************************************
434   Function called when a query for a WORKGROUP<1b> name fails.
435   ****************************************************************************/
436 static void find_domain_master_name_query_fail(struct subnet_record *subrec,
437                                     struct response_record *rrec,
438                                     struct nmb_name *question_name, int fail_code)
439 {
440   DEBUG(0,("find_domain_master_name_query_fail: Unable to find the Domain Master \
441 Browser name %s for the workgroup %s. Unable to sync browse lists in this workgroup.\n",
442         namestr(question_name), question_name->name ));
443 }
444
445 /****************************************************************************
446 As a local master browser for a workgroup find the domain master browser
447 name, announce ourselves as local master browser to it and then pull the
448 full domain browse lists from it onto the given subnet.
449 **************************************************************************/
450
451 void announce_and_sync_with_domain_master_browser( struct subnet_record *subrec,
452                                                    struct work_record *work)
453 {
454   struct nmb_name nmbname;
455
456   /* Only do this if we are using a WINS server. */
457   if(we_are_a_wins_client() == False)
458   {
459     DEBUG(10,("announce_and_sync_with_domain_master_browser: Ignoring as we are not a WINS client.\n"));
460     return;
461   }
462
463   make_nmb_name(&nmbname,work->work_group,0x1b,scope);
464
465   /* First, query for the WORKGROUP<1b> name from the WINS server. */
466   query_name(unicast_subnet, nmbname.name, nmbname.name_type,
467              find_domain_master_name_query_success,
468              find_domain_master_name_query_fail,
469              NULL);
470
471 }
472
473 /****************************************************************************
474   Function called when a node status query to a domain master browser IP succeeds.
475   This function is only called on query to a Samba 1.9.18 or above WINS server.
476
477   Note that adding the workgroup name is enough for this workgroup to be
478   browsable by clients, as clients query the WINS server or broadcast 
479   nets for the WORKGROUP<1b> name when they want to browse a workgroup
480   they are not in. We do not need to do a sync with this Domain Master
481   Browser in order for our browse clients to see machines in this workgroup.
482   JRA.
483 ****************************************************************************/
484
485 static void get_domain_master_name_node_status_success(struct subnet_record *subrec,
486                                               struct userdata_struct *userdata,
487                                               struct res_rec *answers,
488                                               struct in_addr from_ip)
489 {
490   struct work_record *work;
491
492   DEBUG(3,("get_domain_master_name_node_status_success: Success in node status from ip %s\n",
493             inet_ntoa(from_ip) ));
494
495   /* 
496    * Go through the list of names found at answers->rdata and look for
497    * the first WORKGROUP<0x1b> name.
498    */
499
500   if(answers->rdata != NULL)
501   {
502     char *p = answers->rdata;
503     int numnames = CVAL(p, 0);
504
505     p += 1;
506
507     while (numnames--)
508     {
509       char qname[17];
510       uint16 nb_flags;
511       int name_type;
512
513       StrnCpy(qname,p,15);
514       name_type = CVAL(p,15);
515       nb_flags = get_nb_flags(&p[16]);
516       trim_string(qname,NULL," ");
517
518       p += 18;
519
520       if(!(nb_flags & NB_GROUP) && (name_type == 0x1b))
521       {
522
523         DEBUG(5,("get_domain_master_name_node_status_success: IP %s is a domain \
524 master browser for workgroup %s. Adding this name.\n", inet_ntoa(from_ip), qname ));
525
526         /* 
527          * If we don't already know about this workgroup, add it
528          * to the workgroup list on the unicast_subnet.
529          */
530         if((work = find_workgroup_on_subnet( subrec, qname)) == NULL)
531         {
532           /* 
533            * Add it - with an hour in the cache.
534            */
535           if((work = create_workgroup_on_subnet(subrec, qname, 60*60))==NULL)
536             return;
537         }
538         break;
539       }
540     }
541   }
542   else
543     DEBUG(0,("get_domain_master_name_node_status_success: Failed to find a WORKGROUP<0x1b> \
544 name in reply from IP %s.\n", inet_ntoa(from_ip) ));
545 }
546
547 /****************************************************************************
548   Function called when a node status query to a domain master browser IP fails.
549 ****************************************************************************/
550
551 static void get_domain_master_name_node_status_fail(struct subnet_record *subrec,
552                        struct response_record *rrec)
553 {
554   DEBUG(0,("get_domain_master_name_node_status_fail: Doing a node status request to \
555 the domain master browser at IP %s failed. Cannot get workgroup name.\n", 
556       inet_ntoa(rrec->packet->ip) ));
557
558 }
559 /****************************************************************************
560   Function called when a query for *<1b> name succeeds.
561 ****************************************************************************/
562
563 static void find_all_domain_master_names_query_success(struct subnet_record *subrec,
564                         struct userdata_struct *userdata_in,
565                         struct nmb_name *q_name, struct in_addr answer_ip, struct res_rec *rrec)
566 {
567   /* 
568    * We now have a list of all the domain master browsers for all workgroups
569    * that have registered with the WINS server. Now do a node status request
570    * to each one and look for the first 1b name in the reply. This will be
571    * the workgroup name that we will add to the unicast subnet as a 'non-local'
572    * workgroup.
573    */
574
575   struct nmb_name nmbname;
576   struct in_addr send_ip;
577   int i;
578
579   DEBUG(5,("find_all_domain_master_names_query_succes: Got answer from WINS server of %d \
580 IP addresses for Domain Master Browsers.\n", rrec->rdlength / 6 ));
581
582   for(i = 0; i < rrec->rdlength / 6; i++)
583   {
584     /* Initiate the node status requests. */
585     bzero((char *)&nmbname, sizeof(nmbname));
586     nmbname.name[0] = '*';
587
588     putip((char *)&send_ip, (char *)&rrec->rdata[(i*6) + 2]);
589
590     /* 
591      * Don't send node status requests to ourself.
592      */
593
594     if(ismyip( send_ip ))
595     {
596       DEBUG(5,("find_all_domain_master_names_query_succes: Not sending node status \
597 to our own IP %s.\n", inet_ntoa(send_ip) ));
598       continue;
599     }
600
601     DEBUG(5,("find_all_domain_master_names_query_succes: sending node status request to \
602 IP %s.\n", inet_ntoa(send_ip) ));
603
604     node_status( subrec, &nmbname, send_ip, 
605                  get_domain_master_name_node_status_success,
606                  get_domain_master_name_node_status_fail,
607                  NULL);
608   }
609 }
610
611 /****************************************************************************
612   Function called when a query for *<1b> name fails.
613   ****************************************************************************/
614 static void find_all_domain_master_names_query_fail(struct subnet_record *subrec,
615                                     struct response_record *rrec,
616                                     struct nmb_name *question_name, int fail_code)
617 {
618   DEBUG(10,("find_domain_master_name_query_fail: WINS server did not reply to a query \
619 for name %s. This means it is probably not a Samba 1.9.18 or above WINS server.\n",
620         namestr(question_name) ));
621 }
622
623 /****************************************************************************
624  If we are a domain master browser on the unicast subnet, do a query to the
625  WINS server for the *<1b> name. This will only work to a Samba WINS server,
626  so ignore it if we fail. If we succeed, contact each of the IP addresses in
627  turn and do a node status request to them. If this succeeds then look for a
628  <1b> name in the reply - this is the workgroup name. Add this to the unicast
629  subnet. This is expensive, so we only do this every 15 minutes.
630 **************************************************************************/
631
632 void collect_all_workgroup_names_from_wins_server(time_t t)
633 {
634   static time_t lastrun = 0;
635   struct work_record *work;
636   struct nmb_name nmbname;
637
638   /* Only do this if we are using a WINS server. */
639   if(we_are_a_wins_client() == False)
640     return;
641
642   /* Check to see if we are a domain master browser on the unicast subnet. */
643   if((work = find_workgroup_on_subnet( unicast_subnet, myworkgroup)) == NULL)
644   {
645     DEBUG(0,("collect_all_workgroup_names_from_wins_server: Cannot find my workgroup %s on subnet %s.\n",
646               myworkgroup, unicast_subnet->subnet_name ));
647     return;
648   }
649
650   if(!AM_DOMAIN_MASTER_BROWSER(work))
651     return;
652
653   if ((lastrun != 0) && (t < lastrun + (15 * 60)))
654     return;
655      
656   lastrun = t;
657
658   make_nmb_name(&nmbname,"*",0x1b,scope);
659
660   /* First, query for the *<1b> name from the WINS server. */
661   query_name(unicast_subnet, nmbname.name, nmbname.name_type,
662              find_all_domain_master_names_query_success,
663              find_all_domain_master_names_query_fail,
664              NULL);
665