Merge branch 'v3-2-test' of ssh://jra@git.samba.org/data/git/samba into v3-2-test
[samba.git] / source3 / nmbd / nmbd_mynames.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6    Copyright (C) Jeremy Allison 1994-2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20    
21 */
22
23 #include "includes.h"
24
25 extern uint16 samba_nb_type; /* Samba's NetBIOS type. */
26
27 /****************************************************************************
28  Fail funtion when registering my netbios names.
29 **************************************************************************/
30
31 static void my_name_register_failed(struct subnet_record *subrec,
32                               struct response_record *rrec, struct nmb_name *nmbname)
33 {
34         DEBUG(0,("my_name_register_failed: Failed to register my name %s on subnet %s.\n",
35                 nmb_namestr(nmbname), subrec->subnet_name));
36 }
37
38
39 /****************************************************************************
40   Add my workgroup and my given names to one subnet
41   Also add the magic Samba names.
42 **************************************************************************/
43
44 void register_my_workgroup_one_subnet(struct subnet_record *subrec)
45 {
46         int i;
47
48         struct work_record *work;
49
50         /* Create the workgroup on the subnet. */
51         if((work = create_workgroup_on_subnet(subrec, lp_workgroup(), 
52                                               PERMANENT_TTL)) == NULL) {
53                 DEBUG(0,("register_my_workgroup_and_names: Failed to create my workgroup %s on subnet %s. \
54 Exiting.\n", lp_workgroup(), subrec->subnet_name));
55                 return;
56         }
57
58         /* Each subnet entry, except for the wins_server_subnet has
59            the magic Samba names. */
60         add_samba_names_to_subnet(subrec);
61
62         /* Register all our names including aliases. */
63         for (i=0; my_netbios_names(i); i++) {
64                 register_name(subrec, my_netbios_names(i),0x20,samba_nb_type,
65                               NULL,
66                               my_name_register_failed, NULL);
67                 register_name(subrec, my_netbios_names(i),0x03,samba_nb_type,
68                               NULL,
69                               my_name_register_failed, NULL);
70                 register_name(subrec, my_netbios_names(i),0x00,samba_nb_type,
71                               NULL,
72                               my_name_register_failed, NULL);
73         }
74         
75         /* Initiate election processing, register the workgroup names etc. */
76         initiate_myworkgroup_startup(subrec, work);
77 }
78
79 /*******************************************************************
80  Utility function to add a name to the unicast subnet, or add in
81  our IP address if it already exists.
82 ******************************************************************/
83
84 static void insert_refresh_name_into_unicast( struct subnet_record *subrec,
85                                                 struct nmb_name *nmbname, uint16 nb_type )
86 {
87         struct name_record *namerec;
88
89         if (!we_are_a_wins_client()) {
90                 insert_permanent_name_into_unicast(subrec, nmbname, nb_type);
91                 return;
92         }
93
94         if((namerec = find_name_on_subnet(unicast_subnet, nmbname, FIND_SELF_NAME)) == NULL) {
95                 unstring name;
96                 pull_ascii_nstring(name, sizeof(name), nmbname->name);
97                 /* The name needs to be created on the unicast subnet. */
98                 (void)add_name_to_subnet( unicast_subnet, name,
99                                 nmbname->name_type, nb_type,
100                                 MIN(lp_max_ttl(), MAX_REFRESH_TIME), SELF_NAME, 1, &subrec->myip);
101         } else {
102                 /* The name already exists on the unicast subnet. Add our local
103                         IP for the given broadcast subnet to the name. */
104                 add_ip_to_name_record( namerec, subrec->myip);
105         }
106 }
107
108 /****************************************************************************
109   Add my workgroup and my given names to the subnet lists.
110   Also add the magic Samba names.
111 **************************************************************************/
112
113 bool register_my_workgroup_and_names(void)
114 {
115         struct subnet_record *subrec;
116         int i;
117         const char **cluster_addresses = NULL;
118
119         for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
120                 register_my_workgroup_one_subnet(subrec);
121         }
122
123         /* We still need to add the magic Samba
124                 names and the netbios names to the unicast subnet directly. This is
125                 to allow unicast node status requests and queries to still work
126                 in a broadcast only environment. */
127
128         add_samba_names_to_subnet(unicast_subnet);
129
130         for (i=0; my_netbios_names(i); i++) {
131                 for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
132                         /*
133                          * Ensure all the IP addresses are added if we are multihomed.
134                          */
135                         struct nmb_name nmbname;
136
137                         make_nmb_name(&nmbname, my_netbios_names(i),0x20);
138                         insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);
139
140                         make_nmb_name(&nmbname, my_netbios_names(i),0x3);
141                         insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);
142
143                         make_nmb_name(&nmbname, my_netbios_names(i),0x0);
144                         insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);
145                 }
146         }
147
148         /*
149          * add in any cluster addresses. We need to response to these,
150          * but not listen on them. This allows us to run nmbd on every
151          * node in the cluster, and have all of them register with a
152          * WINS server correctly
153          */
154         if (lp_clustering()) {
155                 cluster_addresses = lp_cluster_addresses();
156         }
157         if (cluster_addresses) {
158                 int a, n;
159                 unsigned name_types[] = {0x20, 0x3, 0x0};
160                 
161                 for (i=0; my_netbios_names(i); i++) {
162                         for(subrec = FIRST_SUBNET; subrec; subrec = subrec->next) {
163                                 for (n=0;n<ARRAY_SIZE(name_types);n++) {
164                                         struct name_record *namerec;
165                                         struct nmb_name nmbname;
166                                         struct in_addr ip;
167                                         make_nmb_name(&nmbname, my_netbios_names(i), name_types[n]);
168                                         namerec = find_name_on_subnet(unicast_subnet, &nmbname, FIND_SELF_NAME);
169                                         if (namerec == NULL) continue;
170                                         for (a=0;cluster_addresses[a];a++) {
171                                                 add_ip_to_name_record(namerec, *interpret_addr2(&ip, cluster_addresses[a]));
172                                         }
173                                 }
174                         }
175                 }
176         }
177
178         /*
179          * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
180          * also for the same reasons.
181          */
182
183         for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
184                 /*
185                  * Ensure all the IP addresses are added if we are multihomed.
186                  */
187                 struct nmb_name nmbname;
188
189                 make_nmb_name(&nmbname, lp_workgroup(), 0x0);
190                 insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
191
192                 make_nmb_name(&nmbname, lp_workgroup(), 0x1e);
193                 insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
194         }
195
196         /*
197          * We need to add the Samba names to the remote broadcast subnet,
198          * as NT 4.x does directed broadcast requests to the *<0x0> name.
199          */
200
201         add_samba_names_to_subnet(remote_broadcast_subnet);
202
203         return True;
204 }
205
206 /****************************************************************************
207   Remove all the names we registered.
208 **************************************************************************/
209
210 void release_wins_names(void)
211 {
212         struct subnet_record *subrec = unicast_subnet;
213         struct name_record *namerec, *nextnamerec;
214
215         for (namerec = subrec->namelist; namerec; namerec = nextnamerec) {
216                 nextnamerec = namerec->next;
217                 if( (namerec->data.source == SELF_NAME)
218                     && !NAME_IS_DEREGISTERING(namerec) )
219                         release_name( subrec, namerec, standard_success_release,
220                                       NULL, NULL);
221         }
222 }
223
224 /*******************************************************************
225   Refresh our registered names with WINS
226 ******************************************************************/
227
228 void refresh_my_names(time_t t)
229 {
230         struct name_record *namerec;
231
232         if (wins_srv_count() < 1)
233                 return;
234
235         for (namerec = unicast_subnet->namelist; namerec; namerec = namerec->next) {
236                 /* Each SELF name has an individual time to be refreshed. */
237                 if ((namerec->data.source == SELF_NAME) &&
238                     (namerec->data.refresh_time < t) &&
239                     (namerec->data.death_time != PERMANENT_TTL)) {
240                         /* We cheat here and pretend the refresh is going to be
241                            successful & update the refresh times. This stops
242                            multiple refresh calls being done. We actually
243                            deal with refresh failure in the fail_fn.
244                         */
245                         if (!is_refresh_already_queued(unicast_subnet, namerec)) {
246                                 wins_refresh_name(namerec);
247                         }
248                         namerec->data.death_time = t + lp_max_ttl();
249                         namerec->data.refresh_time = t + MIN(lp_max_ttl()/2, MAX_REFRESH_TIME);
250                 }
251         }
252 }