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