Revert addition of 'mailslot' parameter.
[kai/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    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "nbt_server/nbt_server.h"
25 #include "lib/socket/socket.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "auth/auth.h"
29 #include "util/util_ldb.h"
30 #include "param/param.h"
31 #include "smbd/service_task.h"
32 #include "cldap_server/cldap_server.h"
33 #include "libcli/security/security.h"
34
35 /*
36   reply to a GETDC request
37  */
38 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot, 
39                                 struct nbtd_interface *iface,
40                                 struct nbt_dgram_packet *packet, 
41                                 const struct socket_address *src,
42                                 struct nbt_netlogon_packet *netlogon)
43 {
44         struct nbt_name *name = &packet->data.msg.dest_name;
45         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
46         struct nbt_netlogon_response_from_pdc *pdc;
47         const char *ref_attrs[] = {"nETBIOSName", NULL};
48         struct ldb_message **ref_res;
49         struct ldb_context *samctx;
50         struct ldb_dn *partitions_basedn;
51         struct nbt_netlogon_response netlogon_response;
52         int ret;
53
54         /* only answer getdc requests on the PDC or LOGON names */
55         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
56                 return;
57         }
58
59         samctx = samdb_connect(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx, anonymous_session(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx));
60         if (samctx == NULL) {
61                 DEBUG(2,("Unable to open sam in getdc reply\n"));
62                 return;
63         }
64
65         if (!samdb_is_pdc(samctx)) {
66                 DEBUG(2, ("Not a PDC, so not processing LOGON_PRIMARY_QUERY\n"));
67                 return;         
68         }
69
70         partitions_basedn = samdb_partitions_dn(samctx, packet);
71
72         ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
73                            "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
74                            name->name);
75         
76         if (ret != 1) {
77                 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
78                 return;
79         }
80
81         /* setup a GETDC reply */
82         ZERO_STRUCT(netlogon_response);
83         netlogon_response.response_type = NETLOGON_GET_PDC;
84         pdc = &netlogon_response.get_pdc;
85
86         pdc->command = NETLOGON_RESPONSE_FROM_PDC;
87         pdc->pdc_name         = lp_netbios_name(iface->nbtsrv->task->lp_ctx);
88         pdc->unicode_pdc_name = pdc->pdc_name;
89         pdc->domain_name      = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
90         pdc->nt_version       = 1;
91         pdc->lmnt_token       = 0xFFFF;
92         pdc->lm20_token       = 0xFFFF;
93
94         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
95                                       packet, 
96                                       lp_netbios_name(iface->nbtsrv->task->lp_ctx),
97                                       netlogon->req.pdc.mailslot_name,
98                                       &netlogon_response);
99 }
100
101
102 /*
103   reply to a ADS style GETDC request
104  */
105 static void nbtd_netlogon_samlogon(struct dgram_mailslot_handler *dgmslot,
106                                    struct nbtd_interface *iface,
107                                    struct nbt_dgram_packet *packet, 
108                                    const struct socket_address *src,
109                                    struct nbt_netlogon_packet *netlogon)
110 {
111         struct nbt_name *name = &packet->data.msg.dest_name;
112         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
113         struct ldb_context *samctx;
114         const char *my_ip = reply_iface->ip_address; 
115         struct dom_sid *sid;
116         struct nbt_netlogon_response netlogon_response;
117         NTSTATUS status;
118
119         if (!my_ip) {
120                 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
121                 return;
122         }
123
124         /* only answer getdc requests on the PDC or LOGON names */
125         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
126                 return;
127         }
128
129         samctx = samdb_connect(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx, anonymous_session(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx));
130         if (samctx == NULL) {
131                 DEBUG(2,("Unable to open sam in getdc reply\n"));
132                 return;
133         }
134
135         if (netlogon->req.logon.sid_size) {
136                 if (strcasecmp(dgmslot->mailslot_name, NBT_MAILSLOT_NTLOGON) == 0) {
137                         DEBUG(2,("NBT netlogon query failed because SID specified in request to NTLOGON\n"));
138                         /* SID not permitted on NTLOGON (for some reason...) */ 
139                         return;
140                 }
141                 sid = &netlogon->req.logon.sid;
142         } else {
143                 sid = NULL;
144         }
145
146         status = fill_netlogon_samlogon_response(samctx, packet, NULL, name->name, sid, NULL, 
147                                                  netlogon->req.logon.user_name, src->addr, 
148                                                  netlogon->req.logon.nt_version, iface->nbtsrv->task->lp_ctx, &netlogon_response.samlogon);
149         if (!NT_STATUS_IS_OK(status)) {
150                 DEBUG(2,("NBT netlogon query failed domain=%s sid=%s version=%d - %s\n",
151                          name->name, dom_sid_string(packet, sid), netlogon->req.logon.nt_version, nt_errstr(status)));
152                 return;
153         }
154
155         netlogon_response.response_type = NETLOGON_SAMLOGON;
156
157         packet->data.msg.dest_name.type = 0;
158
159         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
160                                       packet, 
161                                       lp_netbios_name(iface->nbtsrv->task->lp_ctx),
162                                       netlogon->req.logon.mailslot_name,
163                                       &netlogon_response);
164 }
165
166
167 /*
168   handle incoming netlogon mailslot requests
169 */
170 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
171                                     struct nbt_dgram_packet *packet, 
172                                     struct socket_address *src)
173 {
174         NTSTATUS status = NT_STATUS_NO_MEMORY;
175         struct nbtd_interface *iface = 
176                 talloc_get_type(dgmslot->private, struct nbtd_interface);
177         struct nbt_netlogon_packet *netlogon = 
178                 talloc(dgmslot, struct nbt_netlogon_packet);
179         struct nbtd_iface_name *iname;
180         struct nbt_name *name = &packet->data.msg.dest_name;
181
182         if (netlogon == NULL) goto failed;
183
184         /*
185           see if the we are listening on the destination netbios name
186         */
187         iname = nbtd_find_iname(iface, name, 0);
188         if (iname == NULL) {
189                 status = NT_STATUS_BAD_NETWORK_NAME;
190                 goto failed;
191         }
192
193         DEBUG(2,("netlogon request to %s from %s:%d\n", 
194                  nbt_name_string(netlogon, name), src->addr, src->port));
195         status = dgram_mailslot_netlogon_parse_request(dgmslot, netlogon, packet, netlogon);
196         if (!NT_STATUS_IS_OK(status)) goto failed;
197
198         switch (netlogon->command) {
199         case LOGON_PRIMARY_QUERY:
200                 nbtd_netlogon_getdc(dgmslot, iface, packet, 
201                                     src, netlogon);
202                 break;
203         case LOGON_SAM_LOGON_REQUEST:
204                 nbtd_netlogon_samlogon(dgmslot, iface, packet, 
205                                        src, netlogon);
206                 break;
207         default:
208                 DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
209                          netlogon->command, src->addr, src->port));
210                 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
211                 break;
212         }
213
214         talloc_free(netlogon);
215         return;
216
217 failed:
218         DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
219                  src->addr, src->port, nbt_name_string(netlogon, name),
220                  nt_errstr(status)));
221         talloc_free(netlogon);
222 }