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