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