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