d1f001e333db1cab94603b5c4cd3fc278f02fc72
[jelmer/samba4-debian.git] / source / cldap_server / netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CLDAP server - netlogon handling
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 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 #include "includes.h"
23 #include "libcli/ldap/ldap_ndr.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
28 #include "smbd/service_task.h"
29 #include "cldap_server/cldap_server.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "auth/auth.h"
33 #include "ldb_wrap.h"
34 #include "system/network.h"
35 #include "lib/socket/netif.h"
36 #include "param/param.h"
37
38 /*
39   fill in the cldap netlogon union for a given version
40 */
41 static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
42                                      TALLOC_CTX *mem_ctx,
43                                      const char *domain,
44                                      const char *domain_guid,
45                                      const char *user,
46                                      const char *src_address,
47                                      uint32_t version,
48                                      struct loadparm_context *lp_ctx,
49                                      union nbt_cldap_netlogon *netlogon)
50 {
51         const char *ref_attrs[] = {"nETBIOSName", "dnsRoot", "ncName", NULL};
52         const char *dom_attrs[] = {"objectGUID", NULL};
53         struct ldb_result *ref_res = NULL, *dom_res = NULL;
54         int ret;
55         const char **services = lp_server_services(lp_ctx);
56         uint32_t server_type;
57         const char *pdc_name;
58         struct GUID domain_uuid;
59         const char *realm;
60         const char *dns_domain;
61         const char *pdc_dns_name;
62         const char *flatname;
63         const char *server_site;
64         const char *client_site;
65         const char *pdc_ip;
66         struct ldb_dn *partitions_basedn;
67
68         partitions_basedn = samdb_partitions_dn(cldapd->samctx, mem_ctx);
69
70         /* the domain has an optional trailing . */
71         if (domain && domain[strlen(domain)-1] == '.') {
72                 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
73         }
74
75         if (domain) {
76                 struct ldb_dn *dom_dn;
77                 /* try and find the domain */
78
79                 ret = ldb_search_exp_fmt(cldapd->samctx, mem_ctx, &ref_res, 
80                                          partitions_basedn, LDB_SCOPE_ONELEVEL, 
81                                          ref_attrs, 
82                                          "(&(&(objectClass=crossRef)(dnsRoot=%s))(nETBIOSName=*))",
83                                          domain);
84         
85                 if (ret != LDB_SUCCESS) {
86                         DEBUG(2,("Unable to find referece to '%s' in sam: %s\n",
87                                  domain, 
88                                  ldb_errstring(cldapd->samctx)));
89                         return NT_STATUS_NO_SUCH_DOMAIN;
90                 } else if (ref_res->count == 1) {
91                         talloc_steal(mem_ctx, dom_res);
92                         dom_dn = ldb_msg_find_attr_as_dn(cldapd->samctx, mem_ctx, ref_res->msgs[0], "ncName");
93                         if (!dom_dn) {
94                                 return NT_STATUS_NO_SUCH_DOMAIN;
95                         }
96                         ret = ldb_search(cldapd->samctx, dom_dn,
97                                          LDB_SCOPE_BASE, "objectClass=domain", 
98                                          dom_attrs, &dom_res);
99                         if (ret != LDB_SUCCESS) {
100                                 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n", domain, ldb_dn_get_linearized(dom_dn), ldb_errstring(cldapd->samctx)));
101                                 return NT_STATUS_NO_SUCH_DOMAIN;
102                         }
103                         talloc_steal(mem_ctx, dom_res);
104                         if (dom_res->count != 1) {
105                                 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n", domain, ldb_dn_get_linearized(dom_dn)));
106                                 return NT_STATUS_NO_SUCH_DOMAIN;
107                         }
108                 } else if (ref_res->count > 1) {
109                         talloc_free(ref_res);
110                         return NT_STATUS_NO_SUCH_DOMAIN;
111                 }
112         }
113
114         if ((dom_res == NULL || dom_res->count == 0) && domain_guid) {
115                 ref_res = NULL;
116
117                 ret = ldb_search_exp_fmt(cldapd->samctx, mem_ctx, &dom_res,
118                                  NULL, LDB_SCOPE_SUBTREE, 
119                                  dom_attrs, 
120                                  "(&(objectClass=domainDNS)(objectGUID=%s))", 
121                                  domain_guid);
122                 
123                 if (ret != LDB_SUCCESS) {
124                         DEBUG(2,("Unable to find referece to GUID '%s' in sam: %s\n",
125                                  domain_guid, 
126                                  ldb_errstring(cldapd->samctx)));
127                         return NT_STATUS_NO_SUCH_DOMAIN;
128                 } else if (dom_res->count == 1) {
129                         /* try and find the domain */
130                         ret = ldb_search_exp_fmt(cldapd->samctx, mem_ctx, &ref_res,
131                                                  partitions_basedn, LDB_SCOPE_ONELEVEL, 
132                                                  ref_attrs, 
133                                                  "(&(objectClass=crossRef)(ncName=%s))", 
134                                                  ldb_dn_get_linearized(dom_res->msgs[0]->dn));
135                         
136                         if (ret != LDB_SUCCESS) {
137                                 DEBUG(2,("Unable to find referece to '%s' in sam: %s\n",
138                                          ldb_dn_get_linearized(dom_res->msgs[0]->dn), 
139                                          ldb_errstring(cldapd->samctx)));
140                                 return NT_STATUS_NO_SUCH_DOMAIN;
141                                 
142                         } else if (ref_res->count != 1) {
143                                 DEBUG(2,("Unable to find referece to '%s' in sam\n",
144                                          ldb_dn_get_linearized(dom_res->msgs[0]->dn)));
145                                 return NT_STATUS_NO_SUCH_DOMAIN;
146                         }
147                 } else if (dom_res->count > 1) {
148                         talloc_free(ref_res);
149                         return NT_STATUS_NO_SUCH_DOMAIN;
150                 }
151         }
152
153         if ((ref_res == NULL || ref_res->count == 0)) {
154                 DEBUG(2,("Unable to find domain reference with name %s or GUID {%s}\n", domain, domain_guid));
155                 return NT_STATUS_NO_SUCH_DOMAIN;
156         }
157
158         if ((dom_res == NULL || dom_res->count == 0)) {
159                 DEBUG(2,("Unable to find domain with name %s or GUID {%s}\n", domain, domain_guid));
160                 return NT_STATUS_NO_SUCH_DOMAIN;
161         }
162
163         server_type      = 
164                 NBT_SERVER_PDC | NBT_SERVER_GC | 
165                 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
166                 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
167                 NBT_SERVER_GOOD_TIMESERV;
168
169         if (str_list_check(services, "ldap")) {
170                 server_type |= NBT_SERVER_LDAP;
171         }
172
173         if (str_list_check(services, "kdc")) {
174                 server_type |= NBT_SERVER_KDC;
175         }
176
177         pdc_name         = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name(lp_ctx));
178         domain_uuid      = samdb_result_guid(dom_res->msgs[0], "objectGUID");
179         realm            = samdb_result_string(ref_res->msgs[0], "dnsRoot", lp_realm(lp_ctx));
180         dns_domain       = samdb_result_string(ref_res->msgs[0], "dnsRoot", lp_realm(lp_ctx));
181         pdc_dns_name     = talloc_asprintf(mem_ctx, "%s.%s", 
182                                            strlower_talloc(mem_ctx, 
183                                                            lp_netbios_name(lp_ctx)), 
184                                            dns_domain);
185
186         flatname         = samdb_result_string(ref_res->msgs[0], "nETBIOSName", 
187                                                lp_workgroup(lp_ctx));
188         server_site      = "Default-First-Site-Name";
189         client_site      = "Default-First-Site-Name";
190         pdc_ip           = iface_best_ip(lp_ctx, src_address);
191
192         ZERO_STRUCTP(netlogon);
193
194         switch (version & 0xF) {
195         case 0:
196         case 1:
197                 netlogon->logon1.type        = (user?19+2:19);
198                 netlogon->logon1.pdc_name    = pdc_name;
199                 netlogon->logon1.user_name   = user;
200                 netlogon->logon1.domain_name = flatname;
201                 netlogon->logon1.nt_version  = 1;
202                 netlogon->logon1.lmnt_token  = 0xFFFF;
203                 netlogon->logon1.lm20_token  = 0xFFFF;
204                 break;
205         case 2:
206         case 3:
207                 netlogon->logon3.type         = (user?19+2:19);
208                 netlogon->logon3.pdc_name     = pdc_name;
209                 netlogon->logon3.user_name    = user;
210                 netlogon->logon3.domain_name  = flatname;
211                 netlogon->logon3.domain_uuid  = domain_uuid;
212                 netlogon->logon3.forest       = realm;
213                 netlogon->logon3.dns_domain   = dns_domain;
214                 netlogon->logon3.pdc_dns_name = pdc_dns_name;
215                 netlogon->logon3.pdc_ip       = pdc_ip;
216                 netlogon->logon3.server_type  = server_type;
217                 netlogon->logon3.lmnt_token   = 0xFFFF;
218                 netlogon->logon3.lm20_token   = 0xFFFF;
219                 break;
220         case 4:
221         case 5:
222         case 6:
223         case 7:
224                 netlogon->logon5.type         = (user?23+2:23);
225                 netlogon->logon5.server_type  = server_type;
226                 netlogon->logon5.domain_uuid  = domain_uuid;
227                 netlogon->logon5.forest       = realm;
228                 netlogon->logon5.dns_domain   = dns_domain;
229                 netlogon->logon5.pdc_dns_name = pdc_dns_name;
230                 netlogon->logon5.domain       = flatname;
231                 netlogon->logon5.pdc_name     = lp_netbios_name(lp_ctx);
232                 netlogon->logon5.user_name    = user;
233                 netlogon->logon5.server_site  = server_site;
234                 netlogon->logon5.client_site  = client_site;
235                 netlogon->logon5.lmnt_token   = 0xFFFF;
236                 netlogon->logon5.lm20_token   = 0xFFFF;
237                 break;
238         default:
239                 netlogon->logon13.type         = (user?23+2:23);
240                 netlogon->logon13.server_type  = server_type;
241                 netlogon->logon13.domain_uuid  = domain_uuid;
242                 netlogon->logon13.forest       = realm;
243                 netlogon->logon13.dns_domain   = dns_domain;
244                 netlogon->logon13.pdc_dns_name = pdc_dns_name;
245                 netlogon->logon13.domain       = flatname;
246                 netlogon->logon13.pdc_name     = lp_netbios_name(lp_ctx);
247                 netlogon->logon13.user_name    = user;
248                 netlogon->logon13.server_site  = server_site;
249                 netlogon->logon13.client_site  = client_site;
250                 netlogon->logon13.unknown      = 10;
251                 netlogon->logon13.unknown2     = 2;
252                 netlogon->logon13.pdc_ip       = pdc_ip;
253                 netlogon->logon13.lmnt_token   = 0xFFFF;
254                 netlogon->logon13.lm20_token   = 0xFFFF;
255                 break;
256         }
257
258         return NT_STATUS_OK;
259 }
260
261
262 /*
263   handle incoming cldap requests
264 */
265 void cldapd_netlogon_request(struct cldap_socket *cldap, 
266                              uint32_t message_id,
267                              struct ldb_parse_tree *tree,
268                              struct socket_address *src)
269 {
270         struct cldapd_server *cldapd = talloc_get_type(cldap->incoming.private, struct cldapd_server);
271         int i;
272         const char *domain = NULL;
273         const char *host = NULL;
274         const char *user = NULL;
275         const char *domain_guid = NULL;
276         const char *domain_sid = NULL;
277         int acct_control = -1;
278         int version = -1;
279         union nbt_cldap_netlogon netlogon;
280         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
281
282         TALLOC_CTX *tmp_ctx = talloc_new(cldap);
283
284         if (tree->operation != LDB_OP_AND) goto failed;
285
286         /* extract the query elements */
287         for (i=0;i<tree->u.list.num_elements;i++) {
288                 struct ldb_parse_tree *t = tree->u.list.elements[i];
289                 if (t->operation != LDB_OP_EQUALITY) goto failed;
290                 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
291                         domain = talloc_strndup(tmp_ctx, 
292                                                 (const char *)t->u.equality.value.data,
293                                                 t->u.equality.value.length);
294                 }
295                 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
296                         host = talloc_strndup(tmp_ctx, 
297                                               (const char *)t->u.equality.value.data,
298                                               t->u.equality.value.length);
299                 }
300                 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
301                         NTSTATUS enc_status;
302                         struct GUID guid;
303                         enc_status = ldap_decode_ndr_GUID(tmp_ctx, 
304                                                           t->u.equality.value, &guid);
305                         if (NT_STATUS_IS_OK(enc_status)) {
306                                 domain_guid = GUID_string(tmp_ctx, &guid);
307                         }
308                 }
309                 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
310                         domain_sid = talloc_strndup(tmp_ctx, 
311                                                     (const char *)t->u.equality.value.data,
312                                                     t->u.equality.value.length);
313                 }
314                 if (strcasecmp(t->u.equality.attr, "User") == 0) {
315                         user = talloc_strndup(tmp_ctx, 
316                                               (const char *)t->u.equality.value.data,
317                                               t->u.equality.value.length);
318                 }
319                 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
320                     t->u.equality.value.length == 4) {
321                         version = IVAL(t->u.equality.value.data, 0);
322                 }
323                 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
324                     t->u.equality.value.length == 4) {
325                         acct_control = IVAL(t->u.equality.value.data, 0);
326                 }
327         }
328
329         if (domain_guid == NULL && domain == NULL) {
330                 domain = lp_realm(cldapd->task->lp_ctx);
331         }
332
333         if (version == -1) {
334                 goto failed;
335         }
336
337         DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
338                  domain, host, user, version, domain_guid));
339
340         status = cldapd_netlogon_fill(cldapd, tmp_ctx, domain, domain_guid, 
341                                       user, src->addr, 
342                                       version, cldapd->task->lp_ctx, &netlogon);
343         if (!NT_STATUS_IS_OK(status)) {
344                 goto failed;
345         }
346
347         status = cldap_netlogon_reply(cldap, message_id, src, version,
348                                       &netlogon);
349         if (!NT_STATUS_IS_OK(status)) {
350                 goto failed;
351         }
352
353         talloc_free(tmp_ctx);
354         return;
355         
356 failed:
357         DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
358                  domain, host, version, nt_errstr(status)));
359         talloc_free(tmp_ctx);
360         cldap_empty_reply(cldap, message_id, src);      
361 }