LCOV - code coverage report
Current view: top level - source3/nmbd - nmbd_mynames.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 75 113 66.4 %
Date: 2021-09-23 10:06:22 Functions: 8 9 88.9 %

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

Generated by: LCOV version 1.13