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