summaryrefslogtreecommitdiffstats
path: root/librpc/ndr/ndr_ntlmssp.c
blob: d024da5ed8ebf4b793d0580c493b899da8fe6330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
   Unix SMB/CIFS implementation.

   routines for marshalling/unmarshalling special ntlmssp structures

   Copyright (C) Guenther Deschner 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "../librpc/gen_ndr/ndr_ntlmssp.h"

_PUBLIC_ size_t ndr_ntlmssp_string_length(uint32_t negotiate_flags, const char *s)
{
	if (!s) {
		return 0;
	}

	if (negotiate_flags & NTLMSSP_NEGOTIATE_UNICODE) {
		return strlen(s) * 2;
	}

	return strlen(s);
}

_PUBLIC_ uint32_t ndr_ntlmssp_negotiated_string_flags(uint32_t negotiate_flags)
{
	uint32_t flags = LIBNDR_FLAG_STR_NOTERM |
			 LIBNDR_FLAG_STR_CHARLEN |
			 LIBNDR_FLAG_REMAINING;

	if (!(negotiate_flags & NTLMSSP_NEGOTIATE_UNICODE)) {
		flags |= LIBNDR_FLAG_STR_ASCII;
	}

	return flags;
}

_PUBLIC_ enum ndr_err_code ndr_push_AV_PAIR_LIST(struct ndr_push *ndr, int ndr_flags, const struct AV_PAIR_LIST *r)
{
	uint32_t cntr_pair_0;
	if (ndr_flags & NDR_SCALARS) {
		NDR_CHECK(ndr_push_align(ndr, 4));
		for (cntr_pair_0 = 0; cntr_pair_0 < r->count; cntr_pair_0++) {
			NDR_CHECK(ndr_push_AV_PAIR(ndr, NDR_SCALARS, &r->pair[cntr_pair_0]));
		}
	}
	if (ndr_flags & NDR_BUFFERS) {
		for (cntr_pair_0 = 0; cntr_pair_0 < r->count; cntr_pair_0++) {
			NDR_CHECK(ndr_push_AV_PAIR(ndr, NDR_BUFFERS, &r->pair[cntr_pair_0]));
		}
	}
	return NDR_ERR_SUCCESS;
}

_PUBLIC_ enum ndr_err_code ndr_pull_AV_PAIR_LIST(struct ndr_pull *ndr, int ndr_flags, struct AV_PAIR_LIST *r)
{
	uint32_t cntr_pair_0;
	TALLOC_CTX *_mem_save_pair_0;
	if (ndr_flags & NDR_SCALARS) {
		uint32_t offset = 0;
		NDR_CHECK(ndr_pull_align(ndr, 4));
		r->count = 0;
		if (ndr->data_size > 0) {
			NDR_PULL_NEED_BYTES(ndr, 4);
		}
		while (offset + 4 <= ndr->data_size) {
			uint16_t length;
			uint16_t type;
			type = SVAL(ndr->data + offset, 0);
			if (type == MsvAvEOL) {
				r->count++;
				break;
			}
			length = SVAL(ndr->data + offset, 2);
			offset += length + 4;
			r->count++;
		}
		NDR_PULL_ALLOC_N(ndr, r->pair, r->count);
		_mem_save_pair_0 = NDR_PULL_GET_MEM_CTX(ndr);
		NDR_PULL_SET_MEM_CTX(ndr, r->pair, 0);
		for (cntr_pair_0 = 0; cntr_pair_0 < r->count; cntr_pair_0++) {
			NDR_CHECK(ndr_pull_AV_PAIR(ndr, NDR_SCALARS, &r->pair[cntr_pair_0]));
		}
		NDR_PULL_SET_MEM_CTX(ndr, _mem_save_pair_0, 0);
	}
	if (ndr_flags & NDR_BUFFERS) {
		_mem_save_pair_0 = NDR_PULL_GET_MEM_CTX(ndr);
		NDR_PULL_SET_MEM_CTX(ndr, r->pair, 0);
		for (cntr_pair_0 = 0; cntr_pair_0 < r->count; cntr_pair_0++) {
			NDR_CHECK(ndr_pull_AV_PAIR(ndr, NDR_BUFFERS, &r->pair[cntr_pair_0]));
		}
		NDR_PULL_SET_MEM_CTX(ndr, _mem_save_pair_0, 0);
	}
	return NDR_ERR_SUCCESS;
}

_PUBLIC_ void ndr_print_ntlmssp_nt_response(TALLOC_CTX *mem_ctx,
					    const DATA_BLOB *nt_response,
					    bool ntlmv2)
{
	enum ndr_err_code ndr_err;

	if (ntlmv2) {
		struct NTLMv2_RESPONSE nt;
		if (nt_response->length > 24) {
			ndr_err = ndr_pull_struct_blob(nt_response, mem_ctx, &nt,
					(ndr_pull_flags_fn_t)ndr_pull_NTLMv2_RESPONSE);
			if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
				NDR_PRINT_DEBUG(NTLMv2_RESPONSE, &nt);
			}
		}
	} else {
		struct NTLM_RESPONSE nt;
		if (nt_response->length == 24) {
			ndr_err = ndr_pull_struct_blob(nt_response, mem_ctx, &nt,
					(ndr_pull_flags_fn_t)ndr_pull_NTLM_RESPONSE);
			if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
				NDR_PRINT_DEBUG(NTLM_RESPONSE, &nt);
			}
		}
	}
}

_PUBLIC_ void ndr_print_ntlmssp_lm_response(TALLOC_CTX *mem_ctx,
					    const DATA_BLOB *lm_response,
					    bool ntlmv2)
{
	enum ndr_err_code ndr_err;

	if (ntlmv2) {
		struct LMv2_RESPONSE lm;
		if (lm_response->length == 24) {
			ndr_err = ndr_pull_struct_blob(lm_response, mem_ctx, &lm,
					(ndr_pull_flags_fn_t)ndr_pull_LMv2_RESPONSE);
			if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
				NDR_PRINT_DEBUG(LMv2_RESPONSE, &lm);
			}
		}
	} else {
		struct LM_RESPONSE lm;
		if (lm_response->length == 24) {
			ndr_err = ndr_pull_struct_blob(lm_response, mem_ctx, &lm,
					(ndr_pull_flags_fn_t)ndr_pull_LM_RESPONSE);
			if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
				NDR_PRINT_DEBUG(LM_RESPONSE, &lm);
			}
		}
	}
}

_PUBLIC_ void ndr_print_ntlmssp_Version(struct ndr_print *ndr, const char *name, const union ntlmssp_Version *r)
{
	int level;
	level = ndr_print_get_switch_value(ndr, r);
	switch (level) {
		case NTLMSSP_NEGOTIATE_VERSION:
			ndr_print_ntlmssp_VERSION(ndr, name, &r->version);
		break;

		default:
		break;

	}
}