2 Unix SMB/CIFS implementation.
4 CLDAP server structures
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
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/>.
23 #include "../libcli/netlogon.h"
28 #define DEBUGLVL(x) false
32 NTSTATUS push_netlogon_samlogon_response(DATA_BLOB *data, TALLOC_CTX *mem_ctx,
33 struct smb_iconv_convenience *iconv_convenience,
34 struct netlogon_samlogon_response *response)
36 enum ndr_err_code ndr_err;
37 if (response->ntver == NETLOGON_NT_VERSION_1) {
38 ndr_err = ndr_push_struct_blob(data, mem_ctx,
41 (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_NT40);
42 } else if (response->ntver & NETLOGON_NT_VERSION_5EX) {
43 ndr_err = ndr_push_struct_blob(data, mem_ctx,
46 (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX_with_flags);
47 } else if (response->ntver & NETLOGON_NT_VERSION_5) {
48 ndr_err = ndr_push_struct_blob(data, mem_ctx,
51 (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE);
53 DEBUG(0, ("Asked to push unknown netlogon response type 0x%02x\n", response->ntver));
54 return NT_STATUS_INVALID_PARAMETER;
56 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
57 DEBUG(2,("failed to push netlogon response of type 0x%02x\n",
59 return ndr_map_error2ntstatus(ndr_err);
64 NTSTATUS pull_netlogon_samlogon_response(DATA_BLOB *data, TALLOC_CTX *mem_ctx,
65 struct smb_iconv_convenience *iconv_convenience,
66 struct netlogon_samlogon_response *response)
69 enum ndr_err_code ndr_err;
71 if (data->length < 8) {
72 return NT_STATUS_BUFFER_TOO_SMALL;
76 if (SVAL(data->data, data->length - 4) != 0xffff) {
77 return NT_STATUS_INVALID_NETWORK_RESPONSE;
80 if (SVAL(data->data, data->length - 2) != 0xffff) {
81 return NT_STATUS_INVALID_NETWORK_RESPONSE;
84 ntver = IVAL(data->data, data->length - 8);
86 if (ntver == NETLOGON_NT_VERSION_1) {
87 ndr_err = ndr_pull_struct_blob_all(data, mem_ctx,
90 (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_NT40);
91 response->ntver = NETLOGON_NT_VERSION_1;
92 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && DEBUGLEVEL >= 10) {
93 NDR_PRINT_DEBUG(NETLOGON_SAM_LOGON_RESPONSE_NT40, &response->nt4);
96 } else if (ntver & NETLOGON_NT_VERSION_5EX) {
98 ndr = ndr_pull_init_blob(data, mem_ctx, iconv_convenience);
100 return NT_STATUS_NO_MEMORY;
102 ndr_err = ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX_with_flags(ndr, NDR_SCALARS|NDR_BUFFERS, &response->nt5_ex, ntver);
103 if (ndr->offset < ndr->data_size) {
104 ndr_err = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
105 "not all bytes consumed ofs[%u] size[%u]",
106 ndr->offset, ndr->data_size);
108 response->ntver = NETLOGON_NT_VERSION_5EX;
109 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && DEBUGLEVEL >= 10) {
110 NDR_PRINT_DEBUG(NETLOGON_SAM_LOGON_RESPONSE_EX, &response->nt5_ex);
113 } else if (ntver & NETLOGON_NT_VERSION_5) {
114 ndr_err = ndr_pull_struct_blob_all(data, mem_ctx,
117 (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE);
118 response->ntver = NETLOGON_NT_VERSION_5;
119 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && DEBUGLEVEL >= 10) {
120 NDR_PRINT_DEBUG(NETLOGON_SAM_LOGON_RESPONSE, &response->nt5);
123 DEBUG(2,("failed to parse netlogon response of type 0x%02x - unknown response type\n",
125 dump_data(10, data->data, data->length);
126 return NT_STATUS_INVALID_NETWORK_RESPONSE;
129 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
130 DEBUG(2,("failed to parse netlogon response of type 0x%02x\n",
132 dump_data(10, data->data, data->length);
133 return ndr_map_error2ntstatus(ndr_err);
139 void map_netlogon_samlogon_response(struct netlogon_samlogon_response *response)
141 struct NETLOGON_SAM_LOGON_RESPONSE_EX response_5_ex;
142 switch (response->ntver) {
143 case NETLOGON_NT_VERSION_5EX:
145 case NETLOGON_NT_VERSION_5:
146 ZERO_STRUCT(response_5_ex);
147 response_5_ex.command = response->nt5.command;
148 response_5_ex.pdc_name = response->nt5.pdc_name;
149 response_5_ex.user_name = response->nt5.user_name;
150 response_5_ex.domain = response->nt5.domain_name;
151 response_5_ex.domain_uuid = response->nt5.domain_uuid;
152 response_5_ex.forest = response->nt5.forest;
153 response_5_ex.dns_domain = response->nt5.dns_domain;
154 response_5_ex.pdc_dns_name = response->nt5.pdc_dns_name;
155 response_5_ex.sockaddr.pdc_ip = response->nt5.pdc_ip;
156 response_5_ex.server_type = response->nt5.server_type;
157 response_5_ex.nt_version = response->nt5.nt_version;
158 response_5_ex.lmnt_token = response->nt5.lmnt_token;
159 response_5_ex.lm20_token = response->nt5.lm20_token;
160 response->ntver = NETLOGON_NT_VERSION_5EX;
161 response->nt5_ex = response_5_ex;
164 case NETLOGON_NT_VERSION_1:
165 ZERO_STRUCT(response_5_ex);
166 response_5_ex.command = response->nt4.command;
167 response_5_ex.pdc_name = response->nt4.server;
168 response_5_ex.user_name = response->nt4.user_name;
169 response_5_ex.domain = response->nt4.domain;
170 response_5_ex.nt_version = response->nt4.nt_version;
171 response_5_ex.lmnt_token = response->nt4.lmnt_token;
172 response_5_ex.lm20_token = response->nt4.lm20_token;
173 response->ntver = NETLOGON_NT_VERSION_5EX;
174 response->nt5_ex = response_5_ex;
180 NTSTATUS push_nbt_netlogon_response(DATA_BLOB *data, TALLOC_CTX *mem_ctx,
181 struct smb_iconv_convenience *iconv_convenience,
182 struct nbt_netlogon_response *response)
184 NTSTATUS status = NT_STATUS_INVALID_NETWORK_RESPONSE;
185 enum ndr_err_code ndr_err;
186 switch (response->response_type) {
187 case NETLOGON_GET_PDC:
188 ndr_err = ndr_push_struct_blob(data, mem_ctx, iconv_convenience, &response->get_pdc,
189 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_response_from_pdc);
190 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
191 status = ndr_map_error2ntstatus(ndr_err);
192 DEBUG(0,("Failed to parse netlogon packet of length %d: %s\n",
193 (int)data->length, nt_errstr(status)));
195 file_save("netlogon.dat", data->data, data->length);
199 status = NT_STATUS_OK;
201 case NETLOGON_SAMLOGON:
202 status = push_netlogon_samlogon_response(data, mem_ctx, iconv_convenience, &response->samlogon);
209 NTSTATUS pull_nbt_netlogon_response(DATA_BLOB *data, TALLOC_CTX *mem_ctx,
210 struct smb_iconv_convenience *iconv_convenience,
211 struct nbt_netlogon_response *response)
213 NTSTATUS status = NT_STATUS_INVALID_NETWORK_RESPONSE;
214 enum netlogon_command command;
215 enum ndr_err_code ndr_err;
216 if (data->length < 4) {
217 return NT_STATUS_INVALID_NETWORK_RESPONSE;
220 command = SVAL(data->data, 0);
223 case NETLOGON_RESPONSE_FROM_PDC:
224 ndr_err = ndr_pull_struct_blob_all(data, mem_ctx, iconv_convenience, &response->get_pdc,
225 (ndr_pull_flags_fn_t)ndr_pull_nbt_netlogon_response_from_pdc);
226 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
227 status = ndr_map_error2ntstatus(ndr_err);
228 DEBUG(0,("Failed to parse netlogon packet of length %d: %s\n",
229 (int)data->length, nt_errstr(status)));
231 file_save("netlogon.dat", data->data, data->length);
235 status = NT_STATUS_OK;
236 response->response_type = NETLOGON_GET_PDC;
238 case LOGON_SAM_LOGON_RESPONSE:
239 case LOGON_SAM_LOGON_PAUSE_RESPONSE:
240 case LOGON_SAM_LOGON_USER_UNKNOWN:
241 case LOGON_SAM_LOGON_RESPONSE_EX:
242 case LOGON_SAM_LOGON_PAUSE_RESPONSE_EX:
243 case LOGON_SAM_LOGON_USER_UNKNOWN_EX:
244 status = pull_netlogon_samlogon_response(data, mem_ctx, iconv_convenience, &response->samlogon);
245 response->response_type = NETLOGON_SAMLOGON;
248 /* These levels are queries, not responses */
249 case LOGON_PRIMARY_QUERY:
250 case NETLOGON_ANNOUNCE_UAS:
251 case LOGON_SAM_LOGON_REQUEST:
252 status = NT_STATUS_INVALID_NETWORK_RESPONSE;