summaryrefslogtreecommitdiff
path: root/source3/nmbd/nmbd_mynames.c
blob: aac188db2bfb68c0c4f4095fda0f9c2210b29ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* 
   Unix SMB/CIFS implementation.
   NBT netbios routines and daemon - version 2
   Copyright (C) Andrew Tridgell 1994-1998
   Copyright (C) Luke Kenneth Casson Leighton 1994-1998
   Copyright (C) Jeremy Allison 1994-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

#include "includes.h"

extern char **my_netbios_names;
extern fstring global_myworkgroup;

extern uint16 samba_nb_type; /* Samba's NetBIOS type. */

/****************************************************************************
 Fail funtion when registering my netbios names.
  **************************************************************************/

static void my_name_register_failed(struct subnet_record *subrec,
                              struct response_record *rrec, struct nmb_name *nmbname)
{
  DEBUG(0,("my_name_register_failed: Failed to register my name %s on subnet %s.\n",
            nmb_namestr(nmbname), subrec->subnet_name));
}


/****************************************************************************
  Add my workgroup and my given names to one subnet
  Also add the magic Samba names.
  **************************************************************************/
void register_my_workgroup_one_subnet(struct subnet_record *subrec)
{
	int i;

	struct work_record *work;

	/* Create the workgroup on the subnet. */
	if((work = create_workgroup_on_subnet(subrec, global_myworkgroup, 
					      PERMANENT_TTL)) == NULL) {
		DEBUG(0,("register_my_workgroup_and_names: Failed to create my workgroup %s on subnet %s. \
Exiting.\n", global_myworkgroup, subrec->subnet_name));
		return;
	}

	/* Each subnet entry, except for the wins_server_subnet has
           the magic Samba names. */
	add_samba_names_to_subnet(subrec);

	/* Register all our names including aliases. */
	for (i=0; my_netbios_names[i]; i++) {
		register_name(subrec, my_netbios_names[i],0x20,samba_nb_type,
			      NULL,
			      my_name_register_failed, NULL);
		register_name(subrec, my_netbios_names[i],0x03,samba_nb_type,
			      NULL,
			      my_name_register_failed, NULL);
		register_name(subrec, my_netbios_names[i],0x00,samba_nb_type,
			      NULL,
			      my_name_register_failed, NULL);
	}
	
	/* Initiate election processing, register the workgroup names etc. */
	initiate_myworkgroup_startup(subrec, work);
}

/*******************************************************************
 Utility function to add a name to the unicast subnet, or add in
 our IP address if it already exists.
******************************************************************/

static void insert_refresh_name_into_unicast( struct subnet_record *subrec,
                                                struct nmb_name *nmbname, uint16 nb_type )
{
  struct name_record *namerec;

  if (!we_are_a_wins_client()) {
    insert_permanent_name_into_unicast(subrec, nmbname, nb_type);
    return;
  }

  if((namerec = find_name_on_subnet(unicast_subnet, nmbname, FIND_SELF_NAME)) == NULL)
  {
    /* The name needs to be created on the unicast subnet. */
    (void)add_name_to_subnet( unicast_subnet, nmbname->name,
                              nmbname->name_type, nb_type,
                              MIN(lp_max_ttl(), MAX_REFRESH_TIME), SELF_NAME, 1, &subrec->myip);
  }
  else
  {
    /* The name already exists on the unicast subnet. Add our local
       IP for the given broadcast subnet to the name. */
    add_ip_to_name_record( namerec, subrec->myip);
  }
}

/****************************************************************************
  Add my workgroup and my given names to the subnet lists.
  Also add the magic Samba names.
  **************************************************************************/

BOOL register_my_workgroup_and_names(void)
{
  struct subnet_record *subrec;
  int i;

  for(subrec = FIRST_SUBNET; 
      subrec; 
      subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
  {
	  register_my_workgroup_one_subnet(subrec);
  }

  /* We still need to add the magic Samba
     names and the netbios names to the unicast subnet directly. This is
     to allow unicast node status requests and queries to still work
     in a broadcast only environment. */

  add_samba_names_to_subnet(unicast_subnet);

  for (i=0; my_netbios_names[i]; i++)
  {
    for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
    {
      /*
       * Ensure all the IP addresses are added if we are multihomed.
       */
      struct nmb_name nmbname;

      make_nmb_name(&nmbname, my_netbios_names[i],0x20);
      insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);

      make_nmb_name(&nmbname, my_netbios_names[i],0x3);
      insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);

      make_nmb_name(&nmbname, my_netbios_names[i],0x0);
      insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);
    }
  }

  /*
   * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
   * also for the same reasons.
   */

  for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
  {
    /*
     * Ensure all the IP addresses are added if we are multihomed.
     */
    struct nmb_name nmbname;

    make_nmb_name(&nmbname, global_myworkgroup, 0x0);
    insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);

    make_nmb_name(&nmbname, global_myworkgroup, 0x1e);
    insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
  }

  /*
   * We need to add the Samba names to the remote broadcast subnet,
   * as NT 4.x does directed broadcast requests to the *<0x0> name.
   */
  add_samba_names_to_subnet(remote_broadcast_subnet);

  return True;
}

/****************************************************************************
  Remove all the names we registered.
**************************************************************************/

void release_my_names(void)
{
#if 0 /*JRR: do WINS server only, otherwise clients ignore us when we come back up*/
  struct subnet_record *subrec;

  for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
#else
  struct subnet_record *subrec = unicast_subnet;
#endif
  {
    struct name_record *namerec, *nextnamerec;

    for (namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
         namerec;
         namerec = nextnamerec)
    {
      nextnamerec = (struct name_record *)ubi_trNext( namerec );
      if( (namerec->data.source == SELF_NAME)
       && !NAME_IS_DEREGISTERING(namerec) )
        release_name( subrec, namerec, standard_success_release,
                      NULL, NULL);
    }
  }
}

/*******************************************************************
  Refresh our registered names.
  ******************************************************************/

void refresh_my_names(time_t t)
{
  struct subnet_record *subrec;

  for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
  {
    struct name_record *namerec;
	  
    /* B nodes don't send out name refresh requests, see RFC 1001, 15.5.1 */
    if (subrec != unicast_subnet)
      continue;
          
    for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
         namerec;
         namerec = (struct name_record *)ubi_trNext( namerec ) )
    {
      /* Each SELF name has an individual time to be refreshed. */
      if( (namerec->data.source == SELF_NAME)
       && (namerec->data.refresh_time < t)
       && ( namerec->data.death_time != PERMANENT_TTL) )
      {
        /* We cheat here and pretend the refresh is going to be
           successful & update the refresh times. This stops
           multiple refresh calls being done. We actually
           deal with refresh failure in the fail_fn.
         */
        if( !is_refresh_already_queued( subrec, namerec) )
          refresh_name( subrec, namerec, NULL, NULL, NULL );
        namerec->data.death_time = t + lp_max_ttl();
        namerec->data.refresh_time = t + MIN(lp_max_ttl(), MAX_REFRESH_TIME);
      }
    }
  }
}