Fix include paths to new location of libutil.
[kai/samba-autobuild/.git] / source4 / 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 "../lib/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 = iface->nbtsrv->sam_ctx;
60
61         if (!samdb_is_pdc(samctx)) {
62                 DEBUG(2, ("Not a PDC, so not processing LOGON_PRIMARY_QUERY\n"));
63                 return;         
64         }
65
66         partitions_basedn = samdb_partitions_dn(samctx, packet);
67
68         ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
69                            "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
70                            name->name);
71         
72         if (ret != 1) {
73                 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
74                 return;
75         }
76
77         /* setup a GETDC reply */
78         ZERO_STRUCT(netlogon_response);
79         netlogon_response.response_type = NETLOGON_GET_PDC;
80         pdc = &netlogon_response.data.get_pdc;
81
82         pdc->command = NETLOGON_RESPONSE_FROM_PDC;
83         pdc->pdc_name         = lp_netbios_name(iface->nbtsrv->task->lp_ctx);
84         pdc->unicode_pdc_name = pdc->pdc_name;
85         pdc->domain_name      = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
86         pdc->nt_version       = 1;
87         pdc->lmnt_token       = 0xFFFF;
88         pdc->lm20_token       = 0xFFFF;
89
90         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
91                                       packet, 
92                                       lp_netbios_name(iface->nbtsrv->task->lp_ctx),
93                                       netlogon->req.pdc.mailslot_name,
94                                       &netlogon_response);
95 }
96
97
98 /*
99   reply to a ADS style GETDC request
100  */
101 static void nbtd_netlogon_samlogon(struct dgram_mailslot_handler *dgmslot,
102                                    struct nbtd_interface *iface,
103                                    struct nbt_dgram_packet *packet, 
104                                    const struct socket_address *src,
105                                    struct nbt_netlogon_packet *netlogon)
106 {
107         struct nbt_name *name = &packet->data.msg.dest_name;
108         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
109         struct ldb_context *samctx;
110         const char *my_ip = reply_iface->ip_address; 
111         struct dom_sid *sid;
112         struct nbt_netlogon_response netlogon_response;
113         NTSTATUS status;
114
115         if (!my_ip) {
116                 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
117                 return;
118         }
119
120         /* only answer getdc requests on the PDC or LOGON names */
121         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
122                 return;
123         }
124
125         samctx = iface->nbtsrv->sam_ctx;
126
127         if (netlogon->req.logon.sid_size) {
128                 sid = &netlogon->req.logon.sid;
129         } else {
130                 sid = NULL;
131         }
132
133         status = fill_netlogon_samlogon_response(samctx, packet, NULL, name->name, sid, NULL, 
134                                                  netlogon->req.logon.user_name, netlogon->req.logon.acct_control, src->addr, 
135                                                  netlogon->req.logon.nt_version, iface->nbtsrv->task->lp_ctx, &netlogon_response.data.samlogon);
136         if (!NT_STATUS_IS_OK(status)) {
137                 DEBUG(2,("NBT netlogon query failed domain=%s sid=%s version=%d - %s\n",
138                          name->name, dom_sid_string(packet, sid), netlogon->req.logon.nt_version, nt_errstr(status)));
139                 return;
140         }
141
142         netlogon_response.response_type = NETLOGON_SAMLOGON;
143
144         packet->data.msg.dest_name.type = 0;
145
146         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
147                                       packet, 
148                                       lp_netbios_name(iface->nbtsrv->task->lp_ctx),
149                                       netlogon->req.logon.mailslot_name,
150                                       &netlogon_response);
151 }
152
153
154 /*
155   handle incoming netlogon mailslot requests
156 */
157 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
158                                     struct nbt_dgram_packet *packet, 
159                                     struct socket_address *src)
160 {
161         NTSTATUS status = NT_STATUS_NO_MEMORY;
162         struct nbtd_interface *iface = 
163                 talloc_get_type(dgmslot->private, struct nbtd_interface);
164         struct nbt_netlogon_packet *netlogon = 
165                 talloc(dgmslot, struct nbt_netlogon_packet);
166         struct nbtd_iface_name *iname;
167         struct nbt_name *name = &packet->data.msg.dest_name;
168
169         if (netlogon == NULL) goto failed;
170
171         /*
172           see if the we are listening on the destination netbios name
173         */
174         iname = nbtd_find_iname(iface, name, 0);
175         if (iname == NULL) {
176                 status = NT_STATUS_BAD_NETWORK_NAME;
177                 goto failed;
178         }
179
180         DEBUG(2,("netlogon request to %s from %s:%d\n", 
181                  nbt_name_string(netlogon, name), src->addr, src->port));
182         status = dgram_mailslot_netlogon_parse_request(dgmslot, netlogon, packet, netlogon);
183         if (!NT_STATUS_IS_OK(status)) goto failed;
184
185         switch (netlogon->command) {
186         case LOGON_PRIMARY_QUERY:
187                 nbtd_netlogon_getdc(dgmslot, iface, packet, 
188                                     src, netlogon);
189                 break;
190         case LOGON_SAM_LOGON_REQUEST:
191                 nbtd_netlogon_samlogon(dgmslot, iface, packet, 
192                                        src, netlogon);
193                 break;
194         default:
195                 DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
196                          netlogon->command, src->addr, src->port));
197                 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
198                 break;
199         }
200
201         talloc_free(netlogon);
202         return;
203
204 failed:
205         DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
206                  src->addr, src->port, nbt_name_string(netlogon, name),
207                  nt_errstr(status)));
208         talloc_free(netlogon);
209 }