d47c45a875cc116fef672e1c71309c039fe32706
[samba.git] / source / nbt_server / dgram / netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT datagram netlogon server
5
6    Copyright (C) Andrew Tridgell        2005
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 #include "includes.h"
24 #include "dlinklist.h"
25 #include "nbt_server/nbt_server.h"
26 #include "smbd/service_task.h"
27 #include "lib/socket/socket.h"
28
29 /*
30   reply to a GETDC request
31  */
32 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot, 
33                                 struct nbt_dgram_packet *packet, 
34                                 const char *src_address, int src_port,
35                                 struct nbt_netlogon_packet *netlogon)
36 {
37         struct nbt_name *name = &packet->data.msg.dest_name;
38         struct nbt_netlogon_packet reply;
39         struct nbt_netlogon_response_from_pdc *pdc;
40
41         /* only answer getdc requests on the PDC or LOGON names */
42         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
43                 return;
44         }
45
46         /* setup a GETDC reply */
47         ZERO_STRUCT(reply);
48         reply.command = NETLOGON_RESPONSE_FROM_PDC;
49         pdc = &reply.req.response;
50
51         pdc->pdc_name         = lp_netbios_name();
52         pdc->unicode_pdc_name = pdc->pdc_name;
53         pdc->domain_name      = lp_workgroup();
54         pdc->nt_version       = 1;
55         pdc->lmnt_token       = 0xFFFF;
56         pdc->lm20_token       = 0xFFFF;
57
58
59         packet->data.msg.dest_name.type = 0;
60
61         dgram_mailslot_netlogon_reply(dgmslot->dgmsock, 
62                                       packet, 
63                                       netlogon->req.pdc.mailslot_name,
64                                       &reply);
65 }
66
67
68 /*
69   reply to a ADS style GETDC request
70  */
71 static void nbtd_netlogon_getdc2(struct dgram_mailslot_handler *dgmslot, 
72                                  struct nbt_dgram_packet *packet, 
73                                  const char *src_address, int src_port,
74                                  struct nbt_netlogon_packet *netlogon)
75 {
76         struct nbt_name *name = &packet->data.msg.dest_name;
77         struct nbt_netlogon_packet reply;
78         struct nbt_netlogon_response_from_pdc2 *pdc;
79         struct ldb_context *samctx;
80         const char *attrs[] = {"realm", "dnsDomain", "objectGUID", NULL};
81         struct ldb_message **res;
82         int ret;
83
84         /* only answer getdc requests on the PDC or LOGON names */
85         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
86                 return;
87         }
88
89         samctx = samdb_connect(packet);
90         if (samctx == NULL) {
91                 DEBUG(2,("Unable to open sam in getdc reply\n"));
92                 return;
93         }
94
95         /* try and find the domain */
96         ret = gendb_search(samctx, samctx, NULL, &res, attrs, 
97                            "(&(name=%s)(objectClass=domainDNS))", name->name);
98         if (ret != 1) {
99                 DEBUG(2,("Unable to find domain '%s' in sam\n", name->name));
100                 return;
101         }
102
103         /* setup a GETDC reply */
104         ZERO_STRUCT(reply);
105         if (netlogon->req.pdc2.user_name[0]) {
106                 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
107         } else {
108                 reply.command = NETLOGON_RESPONSE_FROM_PDC2;
109         }
110         pdc = &reply.req.response2;
111
112         /* TODO: accurately depict which services we are running */
113         pdc->server_type      = 
114                 NBT_SERVER_PDC | NBT_SERVER_GC | NBT_SERVER_LDAP |
115                 NBT_SERVER_DS | NBT_SERVER_KDC | NBT_SERVER_TIMESERV |
116                 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | NBT_SERVER_GOOD_TIMESERV;
117
118         pdc->domain_uuid      = samdb_result_guid(res[0], "objectGUID");
119         pdc->forest           = samdb_result_string(res[0], "realm", lp_realm());
120         pdc->dns_domain       = samdb_result_string(res[0], "dnsDomain", lp_realm());
121
122         /* TODO: get our full DNS name from somewhere else */
123         pdc->pdc_dns_name     = talloc_asprintf(packet, "%s.%s", 
124                                                 lp_netbios_name(), pdc->dns_domain);
125         pdc->domain           = name->name;
126         pdc->pdc_name         = lp_netbios_name();
127         pdc->user_name        = netlogon->req.pdc2.user_name;
128         /* TODO: we need to make sure these are in our DNS zone */
129         pdc->site_name        = "Default-First-Site-Name";
130         pdc->site_name2       = "Default-First-Site-Name";
131         pdc->unknown          = 0x10; /* what is this? */
132         pdc->unknown2         = 2; /* and this ... */
133         pdc->pdc_ip           = socket_get_my_addr(dgmslot->dgmsock->sock, packet);
134         pdc->nt_version       = 13;
135         pdc->lmnt_token       = 0xFFFF;
136         pdc->lm20_token       = 0xFFFF;
137
138         packet->data.msg.dest_name.type = 0;
139
140         dgram_mailslot_netlogon_reply(dgmslot->dgmsock, 
141                                       packet, 
142                                       netlogon->req.pdc2.mailslot_name,
143                                       &reply);
144 }
145
146
147 /*
148   handle incoming netlogon mailslot requests
149 */
150 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
151                                     struct nbt_dgram_packet *packet, 
152                                     const char *src_address, int src_port)
153 {
154         NTSTATUS status = NT_STATUS_NO_MEMORY;
155         struct nbtd_interface *iface = 
156                 talloc_get_type(dgmslot->private, struct nbtd_interface);
157         struct nbt_netlogon_packet *netlogon = 
158                 talloc(dgmslot, struct nbt_netlogon_packet);
159         struct nbtd_iface_name *iname;
160         struct nbt_name *name = &packet->data.msg.dest_name;
161
162         if (netlogon == NULL) goto failed;
163
164         /*
165           see if the we are listening on the destination netbios name
166         */
167         iname = nbtd_find_iname(iface, name, 0);
168         if (iname == NULL) {
169                 status = NT_STATUS_BAD_NETWORK_NAME;
170                 goto failed;
171         }
172
173         DEBUG(2,("netlogon request to %s from %s:%d\n", 
174                  nbt_name_string(netlogon, name), src_address, src_port));
175         status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
176         if (!NT_STATUS_IS_OK(status)) goto failed;
177
178         NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
179
180         switch (netlogon->command) {
181         case NETLOGON_QUERY_FOR_PDC:
182                 nbtd_netlogon_getdc(dgmslot, packet, src_address, src_port, netlogon);
183                 break;
184         case NETLOGON_QUERY_FOR_PDC2:
185                 nbtd_netlogon_getdc2(dgmslot, packet, src_address, src_port, netlogon);
186                 break;
187         default:
188                 DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
189                          netlogon->command, src_address, src_port));
190                 break;
191         }
192
193         talloc_free(netlogon);
194         return;
195
196 failed:
197         DEBUG(2,("nbtd netlogon handler failed from %s:%d - %s\n",
198                  src_address, src_port, nt_errstr(status)));
199         talloc_free(netlogon);
200 }