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