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