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