r6747: first working version of cldapd server. It is missing 'sites' support, and
[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
30 /*
31   fill in the cldap netlogon union for a given version
32 */
33 static NTSTATUS cldapd_netlogon_fill(struct cldap_socket *cldap,
34                                      TALLOC_CTX *mem_ctx,
35                                      const char *domain,
36                                      const char *src_address,
37                                      uint32_t version,
38                                      union nbt_cldap_netlogon *netlogon)
39 {
40         struct ldb_context *samctx;
41         const char *attrs[] = {"realm", "dnsDomain", "objectGUID", "name", NULL};
42         struct ldb_message **res;
43         int ret;
44         const char **services = lp_server_services();
45         uint32_t server_type;
46         const char *pdc_name;
47         struct GUID domain_uuid;
48         const char *realm;
49         const char *dns_domain;
50         const char *pdc_dns_name;
51         const char *flatname;
52         const char *site_name;
53         const char *site_name2;
54         const char *pdc_ip;
55
56         samctx = samdb_connect(mem_ctx);
57         if (samctx == NULL) {
58                 DEBUG(2,("Unable to open sam in cldap netlogon reply\n"));
59                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
60         }
61
62         /* try and find the domain */
63         ret = gendb_search(samctx, samctx, NULL, &res, attrs, 
64                            "(&(dnsDomain=%s)(objectClass=domainDNS))", domain);
65         if (ret != 1) {
66                 DEBUG(2,("Unable to find domain '%s' in sam\n", domain));
67                 return NT_STATUS_NO_SUCH_DOMAIN;
68         }
69
70         server_type      = 
71                 NBT_SERVER_PDC | NBT_SERVER_GC | 
72                 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
73                 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
74                 NBT_SERVER_GOOD_TIMESERV;
75
76         if (lp_parm_bool(-1, "gensec", "krb5", True)) {
77                 server_type |= NBT_SERVER_KDC;
78         }
79         if (str_list_check(services, "ldap")) {
80                 server_type |= NBT_SERVER_LDAP;
81         }
82
83         pdc_name         = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name());
84         domain_uuid      = samdb_result_guid(res[0], "objectGUID");
85         realm            = samdb_result_string(res[0], "realm", lp_realm());
86         dns_domain       = samdb_result_string(res[0], "dnsDomain", lp_realm());
87         pdc_dns_name     = talloc_asprintf(mem_ctx, "%s.%s", 
88                                            lp_netbios_name(), dns_domain);
89         flatname         = samdb_result_string(res[0], "name", lp_workgroup());
90         site_name        = "Default-First-Site-Name";
91         site_name2       = "";
92         pdc_ip           = iface_best_ip(src_address);
93
94         ZERO_STRUCTP(netlogon);
95
96         switch (version & 0xF) {
97         case 0:
98         case 1:
99                 netlogon->logon1.pdc_name    = pdc_name;
100                 netlogon->logon1.unknown     = "";
101                 netlogon->logon1.domain_name = flatname;
102                 netlogon->logon1.nt_version  = 1;
103                 netlogon->logon1.lmnt_token  = 0xFFFF;
104                 netlogon->logon1.lm20_token  = 0xFFFF;
105                 break;
106         case 2:
107         case 3:
108                 netlogon->logon2.pdc_name     = pdc_name;
109                 netlogon->logon2.unknown      = "";
110                 netlogon->logon2.domain_name  = flatname;
111                 netlogon->logon2.domain_uuid  = domain_uuid;
112                 netlogon->logon2.forest       = realm;
113                 netlogon->logon2.dns_domain   = dns_domain;
114                 netlogon->logon2.pdc_dns_name = pdc_dns_name;
115                 netlogon->logon2.pdc_ip       = pdc_ip;
116                 netlogon->logon2.server_type  = server_type;
117                 netlogon->logon2.nt_version   = 3;
118                 netlogon->logon2.lmnt_token   = 0xFFFF;
119                 netlogon->logon2.lm20_token   = 0xFFFF;
120                 break;
121         case 4:
122         case 5:
123         case 6:
124         case 7:
125                 netlogon->logon3.server_type  = server_type;
126                 netlogon->logon3.domain_uuid  = domain_uuid;
127                 netlogon->logon3.forest       = realm;
128                 netlogon->logon3.dns_domain   = dns_domain;
129                 netlogon->logon3.pdc_dns_name = pdc_dns_name;
130                 netlogon->logon3.domain       = flatname;
131                 netlogon->logon3.pdc_name     = pdc_name;
132                 netlogon->logon3.user_name    = "";
133                 netlogon->logon3.site_name    = site_name;
134                 netlogon->logon3.site_name2   = site_name2;
135                 netlogon->logon3.nt_version   = 3;
136                 netlogon->logon3.lmnt_token   = 0xFFFF;
137                 netlogon->logon3.lm20_token   = 0xFFFF;
138                 break;
139         default:
140                 netlogon->logon4.server_type  = server_type;
141                 netlogon->logon4.domain_uuid  = domain_uuid;
142                 netlogon->logon4.forest       = realm;
143                 netlogon->logon4.dns_domain   = dns_domain;
144                 netlogon->logon4.pdc_dns_name = pdc_dns_name;
145                 netlogon->logon4.domain       = flatname;
146                 netlogon->logon4.pdc_name     = lp_netbios_name();
147                 netlogon->logon4.user_name    = "";
148                 netlogon->logon4.site_name    = site_name;
149                 netlogon->logon4.site_name2   = site_name2;
150                 netlogon->logon4.unknown      = 10;
151                 netlogon->logon4.unknown2     = 2;
152                 netlogon->logon4.pdc_ip       = pdc_ip;
153                 netlogon->logon4.nt_version   = 5;
154                 netlogon->logon4.lmnt_token   = 0xFFFF;
155                 netlogon->logon4.lm20_token   = 0xFFFF;
156                 break;
157         }
158
159         return NT_STATUS_OK;
160 }
161
162
163 /*
164   handle incoming cldap requests
165 */
166 void cldapd_netlogon_request(struct cldap_socket *cldap, 
167                              uint32_t message_id,
168                              const char *filter,
169                              const char *src_address, int src_port)
170 {
171         struct ldap_parse_tree *tree;
172         int i;
173         const char *domain = NULL;
174         const char *host = NULL;
175         int version = -1;
176         union nbt_cldap_netlogon netlogon;
177         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
178
179         TALLOC_CTX *tmp_ctx = talloc_new(cldap);
180
181         tree = ldap_parse_filter_string(tmp_ctx, filter);
182         if (tree == NULL) goto failed;
183
184         if (tree->operation != LDAP_OP_AND) goto failed;
185
186         /* extract the query elements */
187         for (i=0;i<tree->u.list.num_elements;i++) {
188                 struct ldap_parse_tree *t = tree->u.list.elements[i];
189                 if (t->operation != LDAP_OP_SIMPLE) goto failed;
190                 if (strcasecmp(t->u.simple.attr, "DnsDomain") == 0) {
191                         domain = talloc_strndup(tmp_ctx, 
192                                                 t->u.simple.value.data,
193                                                 t->u.simple.value.length);
194                 }
195                 if (strcasecmp(t->u.simple.attr, "Host") == 0) {
196                         host = talloc_strndup(tmp_ctx, 
197                                               t->u.simple.value.data,
198                                               t->u.simple.value.length);
199                 }
200                 if (strcasecmp(t->u.simple.attr, "NtVer") == 0 &&
201                     t->u.simple.value.length == 4) {
202                         version = IVAL(t->u.simple.value.data, 0);
203                 }
204         }
205
206         if (domain == NULL || host == NULL || version == -1) {
207                 goto failed;
208         }
209
210         DEBUG(2,("cldap netlogon query domain=%s host=%s version=%d\n",
211                  domain, host, version));
212
213         status = cldapd_netlogon_fill(cldap, tmp_ctx, domain, src_address, 
214                                       version, &netlogon);
215         if (!NT_STATUS_IS_OK(status)) {
216                 goto failed;
217         }
218
219         status = cldap_netlogon_reply(cldap, message_id, src_address, src_port, version,
220                                       &netlogon);
221         if (!NT_STATUS_IS_OK(status)) {
222                 goto failed;
223         }
224
225         talloc_free(tmp_ctx);
226         return;
227         
228 failed:
229         DEBUG(0,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
230                  domain, host, version, nt_errstr(status)));
231         talloc_free(tmp_ctx);
232         cldap_empty_reply(cldap, message_id, src_address, src_port);    
233 }