This is *not* a big change (although it looks like one).
[ira/wip.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 int DEBUGLEVEL;
28
29 extern char **my_netbios_names;
30 extern pstring myname;
31 extern fstring myworkgroup;
32
33 extern uint16 samba_nb_type; /* Samba's NetBIOS type. */
34
35 /****************************************************************************
36  Fail funtion when registering my netbios names.
37   **************************************************************************/
38
39 static void my_name_register_failed(struct subnet_record *subrec,
40                               struct response_record *rrec, struct nmb_name *nmbname)
41 {
42   DEBUG(0,("my_name_register_failed: Failed to register my name %s on subnet %s.\n",
43             namestr(nmbname), subrec->subnet_name));
44 }
45
46 /****************************************************************************
47   Add my workgroup and my given names to the subnet lists.
48   Also add the magic Samba names.
49   **************************************************************************/
50
51 BOOL register_my_workgroup_and_names()
52 {
53   struct subnet_record *subrec;
54   struct work_record *work;
55   int i;
56
57   for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
58   {
59     /* Create the workgroup on the subnet. */
60     if((work = create_workgroup_on_subnet(subrec, myworkgroup, PERMANENT_TTL)) == NULL)
61     {
62       DEBUG(0,("register_my_workgroup_and_names: Failed to create my workgroup %s on subnet %s. \
63 Exiting.\n", myworkgroup, subrec->subnet_name));
64       return False;
65     }
66
67     /* Each subnet entry, except for the remote_announce_broadcast subnet
68        and the wins_server_subnet has the magic Samba names. */
69     add_samba_names_to_subnet(subrec);
70
71     /* Register all our names including aliases. */
72     for (i=0; my_netbios_names[i]; i++) 
73     {
74       register_name(subrec, my_netbios_names[i],0x20,samba_nb_type,
75                     NULL,
76                     my_name_register_failed, NULL);
77       register_name(subrec, my_netbios_names[i],0x03,samba_nb_type,
78                     NULL,
79                     my_name_register_failed, NULL);
80       register_name(subrec, my_netbios_names[i],0x00,samba_nb_type,
81                     NULL,
82                     my_name_register_failed, NULL);
83     }
84
85     /* Initiate election processing, register the workgroup names etc. */
86     initiate_myworkgroup_startup(subrec, work);
87   }
88
89   /* If we are not a WINS client, we still need to add the magic Samba
90      names and the netbios names to the unicast subnet directly. This is
91      to allow unicast node status requests and queries to still work
92      in a broadcast only environment. */
93
94   if(we_are_a_wins_client() == False)
95   {
96     add_samba_names_to_subnet(unicast_subnet);
97
98     for (i=0; my_netbios_names[i]; i++)
99     {
100       add_name_to_subnet(unicast_subnet, my_netbios_names[i],0x20,samba_nb_type, PERMANENT_TTL,
101                      SELF_NAME, 1, &FIRST_SUBNET->myip);
102
103       add_name_to_subnet(unicast_subnet, my_netbios_names[i],0x3,samba_nb_type, PERMANENT_TTL,
104                      SELF_NAME, 1, &FIRST_SUBNET->myip);
105
106       add_name_to_subnet(unicast_subnet, my_netbios_names[i],0x0,samba_nb_type, PERMANENT_TTL,
107                      SELF_NAME, 1, &FIRST_SUBNET->myip);
108     }
109   }
110
111   return True;
112 }
113
114 /****************************************************************************
115   Remove all the names we registered.
116 **************************************************************************/
117
118 void release_my_names()
119 {
120   struct subnet_record *subrec;
121
122   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
123   {
124     struct name_record *namerec, *nextnamerec;
125
126     for (namerec = subrec->namelist; namerec; namerec = nextnamerec)
127     {
128       nextnamerec = namerec->next;
129       if ((namerec->source == SELF_NAME) && !NAME_IS_DEREGISTERING(namerec))
130         release_name(subrec, namerec, standard_success_release,
131                      NULL, NULL);
132     }
133   }
134 }
135
136 /*******************************************************************
137   Refresh our registered names.
138   ******************************************************************/
139
140 void refresh_my_names(time_t t)
141 {
142   struct subnet_record *subrec;
143
144   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
145   {
146     struct name_record *namerec;
147           
148     for (namerec = subrec->namelist; namerec; namerec = namerec->next)
149     {
150       /* Each SELF name has an individual time to be refreshed. */
151       if ((namerec->source == SELF_NAME) && (namerec->refresh_time < t) && 
152           (namerec->death_time != PERMANENT_TTL))
153       {
154         /* We cheat here and pretend the refresh is going to be
155            successful & update the refresh times. This stops
156            multiple refresh calls being done. We actually
157            deal with refresh failure in the fail_fn.
158          */
159         refresh_name(subrec, namerec, NULL, NULL, NULL);
160         namerec->death_time += lp_max_ttl();
161         namerec->refresh_time += lp_max_ttl();
162       }
163     }
164   }
165 }