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