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