Initial import
[samba] / source / nmbd / nmbd_logonnames.c
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #include "includes.h"
25
26 extern struct in_addr allones_ip;
27
28 extern uint16 samba_nb_type; /* Samba's NetBIOS type. */
29
30 /****************************************************************************
31   Fail to become a Logon server on a subnet.
32 ****************************************************************************/
33
34 static void become_logon_server_fail(struct subnet_record *subrec,
35                                       struct response_record *rrec,
36                                       struct nmb_name *fail_name)
37 {
38         unstring failname;
39         struct work_record *work;
40         struct server_record *servrec;
41
42         pull_ascii_nstring(failname, sizeof(failname), fail_name->name);
43         work = find_workgroup_on_subnet(subrec, failname);
44         if(!work) {
45                 DEBUG(0,("become_logon_server_fail: Error - cannot find \
46 workgroup %s on subnet %s\n", failname, subrec->subnet_name));
47                 return;
48         }
49
50         if((servrec = find_server_in_workgroup( work, global_myname())) == NULL) {
51                 DEBUG(0,("become_logon_server_fail: Error - cannot find server %s \
52 in workgroup %s on subnet %s\n",
53                         global_myname(), failname, subrec->subnet_name));
54                 work->log_state = LOGON_NONE;
55                 return;
56         }
57
58         /* Set the state back to LOGON_NONE. */
59         work->log_state = LOGON_NONE;
60
61         servrec->serv.type &= ~SV_TYPE_DOMAIN_CTRL;
62
63         DEBUG(0,("become_logon_server_fail: Failed to become a domain master for \
64 workgroup %s on subnet %s. Couldn't register name %s.\n",
65                 work->work_group, subrec->subnet_name, nmb_namestr(fail_name)));
66
67 }
68
69 /****************************************************************************
70   Become a Logon server on a subnet.
71   ****************************************************************************/
72
73 static void become_logon_server_success(struct subnet_record *subrec,
74                                         struct userdata_struct *userdata,
75                                         struct nmb_name *registered_name,
76                                         uint16 nb_flags,
77                                         int ttl, struct in_addr registered_ip)
78 {
79         unstring reg_name;
80         struct work_record *work;
81         struct server_record *servrec;
82
83         pull_ascii_nstring(reg_name, sizeof(reg_name), registered_name->name);
84         work = find_workgroup_on_subnet( subrec, reg_name);
85         if(!work) {
86                 DEBUG(0,("become_logon_server_success: Error - cannot find \
87 workgroup %s on subnet %s\n", reg_name, subrec->subnet_name));
88                 return;
89         }
90
91         if((servrec = find_server_in_workgroup( work, global_myname())) == NULL) {
92                 DEBUG(0,("become_logon_server_success: Error - cannot find server %s \
93 in workgroup %s on subnet %s\n",
94                         global_myname(), reg_name, subrec->subnet_name));
95                 work->log_state = LOGON_NONE;
96                 return;
97         }
98
99         /* Set the state in the workgroup structure. */
100         work->log_state = LOGON_SRV; /* Become domain master. */
101
102         /* Update our server status. */
103         servrec->serv.type |= (SV_TYPE_NT|SV_TYPE_DOMAIN_MEMBER);
104         /* To allow Win95 policies to load we need to set type domain
105                 controller.
106         */
107         servrec->serv.type |= SV_TYPE_DOMAIN_CTRL;
108
109         /* Tell the namelist writer to write out a change. */
110         subrec->work_changed = True;
111
112         /*
113          * Add the WORKGROUP<1C> name to the UNICAST subnet with the IP address
114          * for this subnet so we will respond to queries on this name.
115          */
116
117         {
118                 struct nmb_name nmbname;
119                 make_nmb_name(&nmbname,lp_workgroup(),0x1c);
120                 insert_permanent_name_into_unicast(subrec, &nmbname, 0x1c);
121         }
122
123         DEBUG(0,("become_logon_server_success: Samba is now a logon server \
124 for workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
125 }
126
127 /*******************************************************************
128   Become a logon server by attempting to register the WORKGROUP<1c>
129   group name.
130 ******************************************************************/
131
132 static void become_logon_server(struct subnet_record *subrec,
133                                 struct work_record *work)
134 {
135         DEBUG(2,("become_logon_server: Atempting to become logon server for workgroup %s \
136 on subnet %s\n", work->work_group,subrec->subnet_name));
137
138         DEBUG(3,("become_logon_server: go to first stage: register %s<1c> name\n",
139                 work->work_group));
140         work->log_state = LOGON_WAIT;
141
142         register_name(subrec, work->work_group,0x1c,samba_nb_type|NB_GROUP,
143                         become_logon_server_success,
144                         become_logon_server_fail, NULL);
145 }
146
147 /*****************************************************************************
148   Add the internet group <1c> logon names by unicast and broadcast.
149   ****************************************************************************/
150
151 void add_logon_names(void)
152 {
153         struct subnet_record *subrec;
154
155         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
156                 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
157
158                 if (work && (work->log_state == LOGON_NONE)) {
159                         struct nmb_name nmbname;
160                         make_nmb_name(&nmbname,lp_workgroup(),0x1c);
161
162                         if (find_name_on_subnet(subrec, &nmbname, FIND_SELF_NAME) == NULL) {
163                                 if( DEBUGLVL( 0 ) ) {
164                                         dbgtext( "add_domain_logon_names:\n" );
165                                         dbgtext( "Attempting to become logon server " );
166                                         dbgtext( "for workgroup %s ", lp_workgroup() );
167                                         dbgtext( "on subnet %s\n", subrec->subnet_name );
168                                 }
169                                 become_logon_server(subrec, work);
170                         }
171                 }
172         }
173 }