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