WHATSNEW: Update release notes.
[Samba.git] / source3 / libads / cldap.c
blobac4e2cb3012ee4f872ba6c459930007c7df623e5
1 /*
2 Samba Unix/Linux SMB client library
3 net ads cldap functions
4 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
5 Copyright (C) 2003 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2008 Guenther Deschner (gd@samba.org)
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
25 do a cldap netlogon query
27 static int send_cldap_netlogon(TALLOC_CTX *mem_ctx, int sock, const char *domain,
28 const char *hostname, unsigned ntversion)
30 ASN1_DATA *data;
31 char ntver[4];
32 #ifdef CLDAP_USER_QUERY
33 char aac[4];
35 SIVAL(aac, 0, 0x00000180);
36 #endif
37 SIVAL(ntver, 0, ntversion);
39 data = asn1_init(mem_ctx);
40 if (data == NULL) {
41 return -1;
44 asn1_push_tag(data,ASN1_SEQUENCE(0));
45 asn1_write_Integer(data, 4);
46 asn1_push_tag(data, ASN1_APPLICATION(3));
47 asn1_write_OctetString(data, NULL, 0);
48 asn1_write_enumerated(data, 0);
49 asn1_write_enumerated(data, 0);
50 asn1_write_Integer(data, 0);
51 asn1_write_Integer(data, 0);
52 asn1_write_BOOLEAN(data, False);
53 asn1_push_tag(data, ASN1_CONTEXT(0));
55 if (domain) {
56 asn1_push_tag(data, ASN1_CONTEXT(3));
57 asn1_write_OctetString(data, "DnsDomain", 9);
58 asn1_write_OctetString(data, domain, strlen(domain));
59 asn1_pop_tag(data);
62 asn1_push_tag(data, ASN1_CONTEXT(3));
63 asn1_write_OctetString(data, "Host", 4);
64 asn1_write_OctetString(data, hostname, strlen(hostname));
65 asn1_pop_tag(data);
67 #ifdef CLDAP_USER_QUERY
68 asn1_push_tag(data, ASN1_CONTEXT(3));
69 asn1_write_OctetString(data, "User", 4);
70 asn1_write_OctetString(data, "SAMBA$", 6);
71 asn1_pop_tag(data);
73 asn1_push_tag(data, ASN1_CONTEXT(3));
74 asn1_write_OctetString(data, "AAC", 4);
75 asn1_write_OctetString(data, aac, 4);
76 asn1_pop_tag(data);
77 #endif
79 asn1_push_tag(data, ASN1_CONTEXT(3));
80 asn1_write_OctetString(data, "NtVer", 5);
81 asn1_write_OctetString(data, ntver, 4);
82 asn1_pop_tag(data);
84 asn1_pop_tag(data);
86 asn1_push_tag(data,ASN1_SEQUENCE(0));
87 asn1_write_OctetString(data, "NetLogon", 8);
88 asn1_pop_tag(data);
89 asn1_pop_tag(data);
90 asn1_pop_tag(data);
92 if (data->has_error) {
93 DEBUG(2,("Failed to build cldap netlogon at offset %d\n", (int)data->ofs));
94 asn1_free(data);
95 return -1;
98 if (write(sock, data->data, data->length) != (ssize_t)data->length) {
99 DEBUG(2,("failed to send cldap query (%s)\n", strerror(errno)));
100 asn1_free(data);
101 return -1;
104 asn1_free(data);
106 return 0;
110 receive a cldap netlogon reply
112 static int recv_cldap_netlogon(TALLOC_CTX *mem_ctx,
113 int sock,
114 uint32_t nt_version,
115 struct netlogon_samlogon_response **reply)
117 int ret;
118 ASN1_DATA *data;
119 DATA_BLOB blob = data_blob_null;
120 DATA_BLOB os1 = data_blob_null;
121 DATA_BLOB os2 = data_blob_null;
122 DATA_BLOB os3 = data_blob_null;
123 int i1;
124 struct netlogon_samlogon_response *r = NULL;
125 NTSTATUS status;
127 fd_set r_fds;
128 struct timeval timeout;
130 blob = data_blob(NULL, 8192);
131 if (blob.data == NULL) {
132 DEBUG(1, ("data_blob failed\n"));
133 errno = ENOMEM;
134 return -1;
137 if (sock < 0 || sock >= FD_SETSIZE) {
138 errno = EBADF;
139 return -1;
142 FD_ZERO(&r_fds);
143 FD_SET(sock, &r_fds);
146 * half the time of a regular ldap timeout, not less than 3 seconds.
148 timeout.tv_sec = MAX(3,lp_ldap_timeout()/2);
149 timeout.tv_usec = 0;
151 ret = sys_select(sock+1, &r_fds, NULL, NULL, &timeout);
152 if (ret == -1) {
153 DEBUG(10, ("select failed: %s\n", strerror(errno)));
154 data_blob_free(&blob);
155 return -1;
158 if (ret == 0) {
159 DEBUG(1,("no reply received to cldap netlogon "
160 "(select timeout %u sec)\n",
161 (unsigned int)timeout.tv_sec));
162 data_blob_free(&blob);
163 return -1;
166 ret = read(sock, blob.data, blob.length);
167 if (ret <= 0) {
168 DEBUG(1,("no reply received to cldap netlogon "
169 "(ret = %d: Error = %s)\n",
170 ret,
171 ret == -1 ? strerror(errno) : "" ));
172 data_blob_free(&blob);
173 return -1;
175 blob.length = ret;
177 data = asn1_init(mem_ctx);
178 if (data == NULL) {
179 data_blob_free(&blob);
180 return -1;
183 asn1_load(data, blob);
184 asn1_start_tag(data, ASN1_SEQUENCE(0));
185 asn1_read_Integer(data, &i1);
186 asn1_start_tag(data, ASN1_APPLICATION(4));
187 asn1_read_OctetString(data, NULL, &os1);
188 asn1_start_tag(data, ASN1_SEQUENCE(0));
189 asn1_start_tag(data, ASN1_SEQUENCE(0));
190 asn1_read_OctetString(data, NULL, &os2);
191 asn1_start_tag(data, ASN1_SET);
192 asn1_read_OctetString(data, NULL, &os3);
193 asn1_end_tag(data);
194 asn1_end_tag(data);
195 asn1_end_tag(data);
196 asn1_end_tag(data);
197 asn1_end_tag(data);
199 if (data->has_error) {
200 data_blob_free(&blob);
201 data_blob_free(&os1);
202 data_blob_free(&os2);
203 data_blob_free(&os3);
204 asn1_free(data);
205 DEBUG(1,("Failed to parse cldap reply\n"));
206 return -1;
209 r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
210 if (!r) {
211 errno = ENOMEM;
212 data_blob_free(&os1);
213 data_blob_free(&os2);
214 data_blob_free(&os3);
215 data_blob_free(&blob);
216 asn1_free(data);
217 return -1;
220 status = pull_netlogon_samlogon_response(&os3, mem_ctx, NULL, r);
221 if (!NT_STATUS_IS_OK(status)) {
222 data_blob_free(&os1);
223 data_blob_free(&os2);
224 data_blob_free(&os3);
225 data_blob_free(&blob);
226 asn1_free(data);
227 TALLOC_FREE(r);
228 return -1;
231 map_netlogon_samlogon_response(r);
233 data_blob_free(&os1);
234 data_blob_free(&os2);
235 data_blob_free(&os3);
236 data_blob_free(&blob);
238 asn1_free(data);
240 if (reply) {
241 *reply = r;
242 } else {
243 TALLOC_FREE(r);
246 return 0;
249 /*******************************************************************
250 do a cldap netlogon query. Always 389/udp
251 *******************************************************************/
253 bool ads_cldap_netlogon(TALLOC_CTX *mem_ctx,
254 const char *server,
255 const char *realm,
256 uint32_t nt_version,
257 struct netlogon_samlogon_response **reply)
259 int sock;
260 int ret;
262 sock = open_udp_socket(server, LDAP_PORT );
263 if (sock == -1) {
264 DEBUG(2,("ads_cldap_netlogon: Failed to open udp socket to %s. "
265 "Error %s\n",
266 server,
267 strerror(errno) ));
268 return False;
271 ret = send_cldap_netlogon(mem_ctx, sock, realm, global_myname(), nt_version);
272 if (ret != 0) {
273 close(sock);
274 return False;
276 ret = recv_cldap_netlogon(mem_ctx, sock, nt_version, reply);
277 close(sock);
279 if (ret == -1) {
280 return False;
283 return True;
286 /*******************************************************************
287 do a cldap netlogon query. Always 389/udp
288 *******************************************************************/
290 bool ads_cldap_netlogon_5(TALLOC_CTX *mem_ctx,
291 const char *server,
292 const char *realm,
293 struct NETLOGON_SAM_LOGON_RESPONSE_EX *reply5)
295 uint32_t nt_version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
296 struct netlogon_samlogon_response *reply = NULL;
297 bool ret;
299 ret = ads_cldap_netlogon(mem_ctx, server, realm, nt_version, &reply);
300 if (!ret) {
301 return false;
304 if (reply->ntver != NETLOGON_NT_VERSION_5EX) {
305 DEBUG(0,("ads_cldap_netlogon_5: nt_version mismatch: 0x%08x\n",
306 reply->ntver));
307 return false;
310 *reply5 = reply->data.nt5_ex;
312 return true;