This is a first step toward moving long namelists into a database. I
[kai/samba.git] / source / 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 fstring global_myworkgroup;
31 extern pstring scope;
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(void)
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, global_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", global_myworkgroup, subrec->subnet_name));
64       return False;
65     }
66
67     /* Each subnet entry, except for the wins_server_subnet has the magic Samba names. */
68     add_samba_names_to_subnet(subrec);
69
70     /* Register all our names including aliases. */
71     for (i=0; my_netbios_names[i]; i++) 
72     {
73       register_name(subrec, my_netbios_names[i],0x20,samba_nb_type,
74                     NULL,
75                     my_name_register_failed, NULL);
76       register_name(subrec, my_netbios_names[i],0x03,samba_nb_type,
77                     NULL,
78                     my_name_register_failed, NULL);
79       register_name(subrec, my_netbios_names[i],0x00,samba_nb_type,
80                     NULL,
81                     my_name_register_failed, NULL);
82     }
83
84     /* Initiate election processing, register the workgroup names etc. */
85     initiate_myworkgroup_startup(subrec, work);
86   }
87
88   /* If we are not a WINS client, we still need to add the magic Samba
89      names and the netbios names to the unicast subnet directly. This is
90      to allow unicast node status requests and queries to still work
91      in a broadcast only environment. */
92
93   if(we_are_a_wins_client() == False)
94   {
95     add_samba_names_to_subnet(unicast_subnet);
96
97     for (i=0; my_netbios_names[i]; i++)
98     {
99       for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
100       {
101         /*
102          * Ensure all the IP addresses are added if we are multihomed.
103          */
104         struct nmb_name nmbname;
105
106         make_nmb_name(&nmbname, my_netbios_names[i],0x20, scope);
107         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
108
109         make_nmb_name(&nmbname, my_netbios_names[i],0x3, scope);
110         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
111
112         make_nmb_name(&nmbname, my_netbios_names[i],0x0, scope);
113         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
114       }
115     }
116
117     /*
118      * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
119      * also for the same reasons.
120      */
121
122     for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
123     {
124       /*
125        * Ensure all the IP addresses are added if we are multihomed.
126        */
127       struct nmb_name nmbname;
128
129       make_nmb_name(&nmbname, global_myworkgroup, 0x0, scope);
130       insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
131
132       make_nmb_name(&nmbname, global_myworkgroup, 0x1e, scope);
133       insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
134     }
135   }
136
137   /*
138    * We need to add the Samba names to the remote broadcast subnet,
139    * as NT 4.x does directed broadcast requests to the *<0x0> name.
140    */
141   add_samba_names_to_subnet(remote_broadcast_subnet);
142
143   return True;
144 }
145
146 /****************************************************************************
147   Remove all the names we registered.
148 **************************************************************************/
149
150 void release_my_names(void)
151 {
152   struct subnet_record *subrec;
153
154   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
155   {
156     struct name_record *namerec, *nextnamerec;
157
158     for (namerec = subrec->namelist; namerec; namerec = nextnamerec)
159     {
160       nextnamerec = namerec->next;
161       if( (namerec->data.source == SELF_NAME)
162        && !NAME_IS_DEREGISTERING(namerec) )
163         release_name(subrec, namerec, standard_success_release,
164                      NULL, NULL);
165     }
166   }
167 }
168
169 /*******************************************************************
170   Refresh our registered names.
171   ******************************************************************/
172
173 void refresh_my_names(time_t t)
174 {
175   struct subnet_record *subrec;
176
177   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
178   {
179     struct name_record *namerec;
180           
181     for (namerec = subrec->namelist; namerec; namerec = namerec->next)
182     {
183       /* Each SELF name has an individual time to be refreshed. */
184       if( (namerec->data.source == SELF_NAME)
185        && (namerec->data.refresh_time < t)
186        && ( namerec->data.death_time != PERMANENT_TTL) )
187       {
188         /* We cheat here and pretend the refresh is going to be
189            successful & update the refresh times. This stops
190            multiple refresh calls being done. We actually
191            deal with refresh failure in the fail_fn.
192          */
193         if( !is_refresh_already_queued( subrec, namerec) )
194           refresh_name( subrec, namerec, NULL, NULL, NULL );
195         namerec->data.death_time += lp_max_ttl();
196         namerec->data.refresh_time += lp_max_ttl();
197       }
198     }
199   }
200 }