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