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