2 Unix SMB/CIFS implementation.
4 server side dcerpc authentication code
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Stefan (metze) Metzmacher 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 startup the cryptographic side of an authenticated dcerpc server
29 NTSTATUS dcesrv_crypto_select_type(struct dcesrv_connection *dce_conn,
30 struct dcesrv_auth *auth)
33 if (auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_INTEGRITY &&
34 auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
35 DEBUG(2,("auth_level %d not supported in dcesrv auth\n",
36 auth->auth_info->auth_level));
37 return NT_STATUS_INVALID_PARAMETER;
40 if (auth->gensec_security != NULL) {
42 * this this function should not be called
43 * twice per dcesrv_connection!
45 * so we need to find out the right
46 * dcerpc error to return
50 status = gensec_server_start(&auth->gensec_security);
51 if (!NT_STATUS_IS_OK(status)) {
52 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
56 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type,
57 auth->auth_info->auth_level);
59 if (!NT_STATUS_IS_OK(status)) {
60 DEBUG(1, ("Failed to start GENSEC mech-specific server code (%d): %s\n",
61 (int)auth->auth_info->auth_type,
70 parse any auth information from a dcerpc bind request
71 return False if we can't handle the auth request for some
72 reason (in which case we send a bind_nak)
74 BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
76 struct dcerpc_packet *pkt = &call->pkt;
77 struct dcesrv_connection *dce_conn = call->conn;
80 if (pkt->u.bind.auth_info.length == 0) {
81 dce_conn->auth_state.auth_info = NULL;
85 dce_conn->auth_state.auth_info = talloc_p(dce_conn->mem_ctx, struct dcerpc_auth);
86 if (!dce_conn->auth_state.auth_info) {
90 status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
92 dce_conn->auth_state.auth_info,
93 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
94 if (!NT_STATUS_IS_OK(status)) {
98 status = dcesrv_crypto_select_type(dce_conn, &dce_conn->auth_state);
99 if (!NT_STATUS_IS_OK(status)) {
107 add any auth information needed in a bind ack, and process the authentication
108 information found in the bind.
110 BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct dcerpc_packet *pkt)
112 struct dcesrv_connection *dce_conn = call->conn;
115 if (!call->conn->auth_state.gensec_security) {
119 status = gensec_update(dce_conn->auth_state.gensec_security,
121 dce_conn->auth_state.auth_info->credentials,
122 &dce_conn->auth_state.auth_info->credentials);
124 if (NT_STATUS_IS_OK(status)) {
125 status = gensec_session_info(dce_conn->auth_state.gensec_security,
126 &dce_conn->auth_state.session_info);
127 if (!NT_STATUS_IS_OK(status)) {
128 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
132 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
133 dce_conn->auth_state.auth_info->auth_pad_length = 0;
134 dce_conn->auth_state.auth_info->auth_reserved = 0;
137 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
144 process the final stage of a auth request
146 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
148 struct dcerpc_packet *pkt = &call->pkt;
149 struct dcesrv_connection *dce_conn = call->conn;
152 /* We can't work without an existing gensec state, and an new blob to feed it */
153 if (!dce_conn->auth_state.auth_info ||
154 !dce_conn->auth_state.gensec_security ||
155 pkt->u.auth.auth_info.length == 0) {
159 status = ndr_pull_struct_blob(&pkt->u.auth.auth_info,
161 dce_conn->auth_state.auth_info,
162 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
163 if (!NT_STATUS_IS_OK(status)) {
167 /* Pass the extra data we got from the client down to gensec for processing */
168 status = gensec_update(dce_conn->auth_state.gensec_security,
170 dce_conn->auth_state.auth_info->credentials,
171 &dce_conn->auth_state.auth_info->credentials);
172 if (NT_STATUS_IS_OK(status)) {
173 status = gensec_session_info(dce_conn->auth_state.gensec_security,
174 &dce_conn->auth_state.session_info);
175 if (!NT_STATUS_IS_OK(status)) {
176 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
181 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n",
190 check credentials on a request
192 BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
194 struct dcerpc_packet *pkt = &call->pkt;
195 struct dcesrv_connection *dce_conn = call->conn;
197 struct dcerpc_auth auth;
198 struct ndr_pull *ndr;
201 if (!dce_conn->auth_state.auth_info ||
202 !dce_conn->auth_state.gensec_security) {
206 auth_blob.length = 8 + pkt->auth_length;
208 /* check for a valid length */
209 if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
214 pkt->u.request.stub_and_verifier.data +
215 pkt->u.request.stub_and_verifier.length - auth_blob.length;
216 pkt->u.request.stub_and_verifier.length -= auth_blob.length;
218 /* pull the auth structure */
219 ndr = ndr_pull_init_blob(&auth_blob, call->mem_ctx);
224 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
225 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
228 status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
229 if (!NT_STATUS_IS_OK(status)) {
233 /* check signature or unseal the packet */
234 switch (dce_conn->auth_state.auth_info->auth_level) {
235 case DCERPC_AUTH_LEVEL_PRIVACY:
236 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
238 pkt->u.request.stub_and_verifier.data,
239 pkt->u.request.stub_and_verifier.length,
241 full_packet->length-auth.credentials.length,
245 case DCERPC_AUTH_LEVEL_INTEGRITY:
246 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
248 pkt->u.request.stub_and_verifier.data,
249 pkt->u.request.stub_and_verifier.length,
251 full_packet->length-auth.credentials.length,
256 status = NT_STATUS_INVALID_LEVEL;
260 /* remove the indicated amount of padding */
261 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
264 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
266 return NT_STATUS_IS_OK(status);
271 push a signed or sealed dcerpc request packet into a blob
273 BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
274 DATA_BLOB *blob, struct dcerpc_packet *pkt)
276 struct dcesrv_connection *dce_conn = call->conn;
278 struct ndr_push *ndr;
280 /* non-signed packets are simple */
281 if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
282 status = dcerpc_push_auth(blob, call->mem_ctx, pkt, NULL);
283 return NT_STATUS_IS_OK(status);
286 ndr = ndr_push_init_ctx(call->mem_ctx);
291 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
292 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
295 status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
296 if (!NT_STATUS_IS_OK(status)) {
300 /* pad to 8 byte multiple */
301 dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 8);
302 ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
305 dce_conn->auth_state.auth_info->credentials
306 = data_blob_talloc(call->mem_ctx, NULL,
307 gensec_sig_size(dce_conn->auth_state.gensec_security));
309 /* add the auth verifier */
310 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
311 if (!NT_STATUS_IS_OK(status)) {
315 /* extract the whole packet as a blob */
316 *blob = ndr_push_blob(ndr);
318 /* fill in the fragment length and auth_length, we can't fill
319 in these earlier as we don't know the signature length (it
320 could be variable length) */
321 dcerpc_set_frag_length(blob, blob->length);
322 dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
324 /* sign or seal the packet */
325 switch (dce_conn->auth_state.auth_info->auth_level) {
326 case DCERPC_AUTH_LEVEL_PRIVACY:
327 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
329 ndr->data + DCERPC_REQUEST_LENGTH,
330 ndr->offset - DCERPC_REQUEST_LENGTH,
332 blob->length - dce_conn->auth_state.auth_info->credentials.length,
333 &dce_conn->auth_state.auth_info->credentials);
336 case DCERPC_AUTH_LEVEL_INTEGRITY:
337 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
339 ndr->data + DCERPC_REQUEST_LENGTH,
340 ndr->offset - DCERPC_REQUEST_LENGTH,
342 blob->length - dce_conn->auth_state.auth_info->credentials.length,
343 &dce_conn->auth_state.auth_info->credentials);
347 status = NT_STATUS_INVALID_LEVEL;
351 if (!NT_STATUS_IS_OK(status)) {
355 memcpy(blob->data + blob->length - dce_conn->auth_state.auth_info->credentials.length,
356 dce_conn->auth_state.auth_info->credentials.data, dce_conn->auth_state.auth_info->credentials.length);
358 data_blob_free(&dce_conn->auth_state.auth_info->credentials);