John Reillys fix for de-registering broadcast names (NT doesn't do this).
[bbaumbach/samba-autobuild/.git] / source / nmbd / nmbd_mynames.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
27 extern int DEBUGLEVEL;
28
29 extern char **my_netbios_names;
30 extern fstring global_myworkgroup;
31
32 extern uint16 samba_nb_type; /* Samba's NetBIOS type. */
33
34 /****************************************************************************
35  Fail funtion when registering my netbios names.
36   **************************************************************************/
37
38 static void my_name_register_failed(struct subnet_record *subrec,
39                               struct response_record *rrec, struct nmb_name *nmbname)
40 {
41   DEBUG(0,("my_name_register_failed: Failed to register my name %s on subnet %s.\n",
42             nmb_namestr(nmbname), subrec->subnet_name));
43 }
44
45
46 /****************************************************************************
47   Add my workgroup and my given names to one subnet
48   Also add the magic Samba names.
49   **************************************************************************/
50 void register_my_workgroup_one_subnet(struct subnet_record *subrec)
51 {
52         int i;
53
54         struct work_record *work;
55
56         /* Create the workgroup on the subnet. */
57         if((work = create_workgroup_on_subnet(subrec, global_myworkgroup, 
58                                               PERMANENT_TTL)) == NULL) {
59                 DEBUG(0,("register_my_workgroup_and_names: Failed to create my workgroup %s on subnet %s. \
60 Exiting.\n", global_myworkgroup, subrec->subnet_name));
61                 return;
62         }
63
64         /* Each subnet entry, except for the wins_server_subnet has
65            the magic Samba names. */
66         add_samba_names_to_subnet(subrec);
67
68         /* Register all our names including aliases. */
69         for (i=0; my_netbios_names[i]; i++) {
70                 register_name(subrec, my_netbios_names[i],0x20,samba_nb_type,
71                               NULL,
72                               my_name_register_failed, NULL);
73                 register_name(subrec, my_netbios_names[i],0x03,samba_nb_type,
74                               NULL,
75                               my_name_register_failed, NULL);
76                 register_name(subrec, my_netbios_names[i],0x00,samba_nb_type,
77                               NULL,
78                               my_name_register_failed, NULL);
79         }
80         
81         /* Initiate election processing, register the workgroup names etc. */
82         initiate_myworkgroup_startup(subrec, work);
83 }
84
85
86 /****************************************************************************
87   Add my workgroup and my given names to the subnet lists.
88   Also add the magic Samba names.
89   **************************************************************************/
90
91 BOOL register_my_workgroup_and_names(void)
92 {
93   struct subnet_record *subrec;
94   int i;
95
96   for(subrec = FIRST_SUBNET; 
97       subrec; 
98       subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
99   {
100           register_my_workgroup_one_subnet(subrec);
101   }
102
103   /* If we are not a WINS client, we still need to add the magic Samba
104      names and the netbios names to the unicast subnet directly. This is
105      to allow unicast node status requests and queries to still work
106      in a broadcast only environment. */
107
108   if(we_are_a_wins_client() == False)
109   {
110     add_samba_names_to_subnet(unicast_subnet);
111
112     for (i=0; my_netbios_names[i]; i++)
113     {
114       for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
115       {
116         /*
117          * Ensure all the IP addresses are added if we are multihomed.
118          */
119         struct nmb_name nmbname;
120
121         make_nmb_name(&nmbname, my_netbios_names[i],0x20);
122         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
123
124         make_nmb_name(&nmbname, my_netbios_names[i],0x3);
125         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
126
127         make_nmb_name(&nmbname, my_netbios_names[i],0x0);
128         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
129       }
130     }
131
132     /*
133      * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
134      * also for the same reasons.
135      */
136
137     for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
138     {
139       /*
140        * Ensure all the IP addresses are added if we are multihomed.
141        */
142       struct nmb_name nmbname;
143
144       make_nmb_name(&nmbname, global_myworkgroup, 0x0);
145       insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
146
147       make_nmb_name(&nmbname, global_myworkgroup, 0x1e);
148       insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
149     }
150   }
151
152   /*
153    * We need to add the Samba names to the remote broadcast subnet,
154    * as NT 4.x does directed broadcast requests to the *<0x0> name.
155    */
156   add_samba_names_to_subnet(remote_broadcast_subnet);
157
158   return True;
159 }
160
161 /****************************************************************************
162   Remove all the names we registered.
163 **************************************************************************/
164
165 void release_my_names(void)
166 {
167 #if 0 /*JRR: do WINS server only, otherwise clients ignore us when we come back up*/
168   struct subnet_record *subrec;
169
170   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
171 #else
172   struct subnet_record *subrec = unicast_subnet;
173 #endif
174   {
175     struct name_record *namerec, *nextnamerec;
176
177     for (namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
178          namerec;
179          namerec = nextnamerec)
180     {
181       nextnamerec = (struct name_record *)ubi_trNext( namerec );
182       if( (namerec->data.source == SELF_NAME)
183        && !NAME_IS_DEREGISTERING(namerec) )
184         release_name( subrec, namerec, standard_success_release,
185                       NULL, NULL);
186     }
187   }
188 }
189
190 /*******************************************************************
191   Refresh our registered names.
192   ******************************************************************/
193
194 void refresh_my_names(time_t t)
195 {
196   struct subnet_record *subrec;
197
198   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
199   {
200     struct name_record *namerec;
201           
202     /* B nodes don't send out name refresh requests, see RFC 1001, 15.5.1 */
203     if (subrec != unicast_subnet)
204       continue;
205           
206     for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
207          namerec;
208          namerec = (struct name_record *)ubi_trNext( namerec ) )
209     {
210       /* Each SELF name has an individual time to be refreshed. */
211       if( (namerec->data.source == SELF_NAME)
212        && (namerec->data.refresh_time < t)
213        && ( namerec->data.death_time != PERMANENT_TTL) )
214       {
215         /* We cheat here and pretend the refresh is going to be
216            successful & update the refresh times. This stops
217            multiple refresh calls being done. We actually
218            deal with refresh failure in the fail_fn.
219          */
220         if( !is_refresh_already_queued( subrec, namerec) )
221           refresh_name( subrec, namerec, NULL, NULL, NULL );
222         namerec->data.death_time += lp_max_ttl();
223         namerec->data.refresh_time += MIN(lp_max_ttl(), MAX_REFRESH_TIME);
224       }
225     }
226   }
227 }