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