2 Unix SMB/CIFS implementation.
4 server side dcerpc authentication code
6 Copyright (C) Andrew Tridgell 2003
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.
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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 parse any auth information from a dcerpc bind request
28 return False if we can't handle the auth request for some
29 reason (in which case we send a bind_nak)
31 BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
33 struct dcerpc_packet *pkt = &call->pkt;
34 struct dcesrv_connection *dce_conn = call->conn;
37 if (pkt->u.bind.auth_info.length == 0) {
38 dce_conn->auth_state.auth_info = NULL;
42 dce_conn->auth_state.auth_info = talloc_p(dce_conn->mem_ctx, struct dcerpc_auth);
43 if (!dce_conn->auth_state.auth_info) {
47 status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
49 dce_conn->auth_state.auth_info,
50 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
51 if (!NT_STATUS_IS_OK(status)) {
55 if (dce_conn->auth_state.auth_info->auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
56 /* only do NTLMSSP for now */
57 DEBUG(2,("auth_type %d not supported\n", dce_conn->auth_state.auth_info->auth_type));
61 if (dce_conn->auth_state.auth_info->auth_level != DCERPC_AUTH_LEVEL_INTEGRITY &&
62 dce_conn->auth_state.auth_info->auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
63 DEBUG(2,("auth_level %d not supported\n", dce_conn->auth_state.auth_info->auth_level));
67 status = auth_ntlmssp_start(&dce_conn->auth_state.ntlmssp_state);
68 if (!NT_STATUS_IS_OK(status)) {
69 DEBUG(2, ("Failed to start NTLMSSP subsystem!\n"));
77 add any auth information needed in a bind ack
79 BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct dcerpc_packet *pkt)
81 struct dcesrv_connection *dce_conn = call->conn;
84 if (!call->conn->auth_state.ntlmssp_state) {
88 status = auth_ntlmssp_update(dce_conn->auth_state.ntlmssp_state,
90 dce_conn->auth_state.auth_info->credentials,
91 &dce_conn->auth_state.auth_info->credentials);
92 if (!NT_STATUS_IS_OK(status) &&
93 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
94 DEBUG(2, ("Failed to start NTLMSSP process NTLMSSP negotiate: %s\n", nt_errstr(status)));
98 dce_conn->auth_state.auth_info->auth_pad_length = 0;
99 dce_conn->auth_state.auth_info->auth_reserved = 0;
106 process the final stage of a NTLMSSP auth request
108 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
110 struct dcerpc_packet *pkt = &call->pkt;
111 struct dcesrv_connection *dce_conn = call->conn;
114 if (!dce_conn->auth_state.auth_info ||
115 !dce_conn->auth_state.ntlmssp_state ||
116 pkt->u.auth.auth_info.length == 0) {
120 status = ndr_pull_struct_blob(&pkt->u.auth.auth_info,
122 dce_conn->auth_state.auth_info,
123 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
124 if (!NT_STATUS_IS_OK(status)) {
128 if (dce_conn->auth_state.auth_info->auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
131 if (dce_conn->auth_state.auth_info->auth_level != DCERPC_AUTH_LEVEL_INTEGRITY &&
132 dce_conn->auth_state.auth_info->auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
136 status = auth_ntlmssp_update(dce_conn->auth_state.ntlmssp_state,
138 dce_conn->auth_state.auth_info->credentials,
139 &dce_conn->auth_state.auth_info->credentials);
140 if (!NT_STATUS_IS_OK(status)) {
141 DEBUG(4, ("User failed to authenticated with NTLMSSP: %s\n", nt_errstr(status)));
150 check credentials on a request
152 BOOL dcesrv_auth_request(struct dcesrv_call_state *call)
154 struct dcerpc_packet *pkt = &call->pkt;
155 struct dcesrv_connection *dce_conn = call->conn;
157 struct dcerpc_auth auth;
158 struct ndr_pull *ndr;
161 if (!dce_conn->auth_state.auth_info ||
162 !dce_conn->auth_state.ntlmssp_state) {
166 auth_blob.length = 8 + pkt->auth_length;
168 /* check for a valid length */
169 if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
174 pkt->u.request.stub_and_verifier.data +
175 pkt->u.request.stub_and_verifier.length - auth_blob.length;
176 pkt->u.request.stub_and_verifier.length -= auth_blob.length;
178 /* pull the auth structure */
179 ndr = ndr_pull_init_blob(&auth_blob, call->mem_ctx);
184 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
185 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
188 status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
189 if (!NT_STATUS_IS_OK(status)) {
193 /* check signature or unseal the packet */
194 switch (dce_conn->auth_state.auth_info->auth_level) {
195 case DCERPC_AUTH_LEVEL_PRIVACY:
196 status = ntlmssp_unseal_packet(dce_conn->auth_state.ntlmssp_state->ntlmssp_state,
198 pkt->u.request.stub_and_verifier.data,
199 pkt->u.request.stub_and_verifier.length,
203 case DCERPC_AUTH_LEVEL_INTEGRITY:
204 status = ntlmssp_check_packet(dce_conn->auth_state.ntlmssp_state->ntlmssp_state,
206 pkt->u.request.stub_and_verifier.data,
207 pkt->u.request.stub_and_verifier.length,
212 status = NT_STATUS_INVALID_LEVEL;
216 /* remove the indicated amount of paddiing */
217 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
220 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
222 return NT_STATUS_IS_OK(status);
227 push a signed or sealed dcerpc request packet into a blob
229 BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
230 DATA_BLOB *blob, struct dcerpc_packet *pkt)
232 struct dcesrv_connection *dce_conn = call->conn;
234 struct ndr_push *ndr;
236 /* non-signed packets are simple */
237 if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.ntlmssp_state) {
238 status = dcerpc_push_auth(blob, call->mem_ctx, pkt, NULL);
239 return NT_STATUS_IS_OK(status);
242 ndr = ndr_push_init_ctx(call->mem_ctx);
247 if (pkt->drep[0] & DCERPC_DREP_LE) {
248 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
251 status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
252 if (!NT_STATUS_IS_OK(status)) {
256 /* pad to 8 byte multiple */
257 dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 8);
258 ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
260 /* sign or seal the packet */
261 switch (dce_conn->auth_state.auth_info->auth_level) {
262 case DCERPC_AUTH_LEVEL_PRIVACY:
263 status = ntlmssp_seal_packet(dce_conn->auth_state.ntlmssp_state->ntlmssp_state,
265 ndr->data + DCERPC_REQUEST_LENGTH,
266 ndr->offset - DCERPC_REQUEST_LENGTH,
267 &dce_conn->auth_state.auth_info->credentials);
270 case DCERPC_AUTH_LEVEL_INTEGRITY:
271 status = ntlmssp_sign_packet(dce_conn->auth_state.ntlmssp_state->ntlmssp_state,
273 ndr->data + DCERPC_REQUEST_LENGTH,
274 ndr->offset - DCERPC_REQUEST_LENGTH,
275 &dce_conn->auth_state.auth_info->credentials);
278 status = NT_STATUS_INVALID_LEVEL;
282 if (!NT_STATUS_IS_OK(status)) {
286 /* add the auth verifier */
287 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
288 if (!NT_STATUS_IS_OK(status)) {
292 /* extract the whole packet as a blob */
293 *blob = ndr_push_blob(ndr);
295 /* fill in the fragment length and auth_length, we can't fill
296 in these earlier as we don't know the signature length (it
297 could be variable length) */
298 dcerpc_set_frag_length(blob, blob->length);
299 dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
301 data_blob_free(&dce_conn->auth_state.auth_info->credentials);