2 Unix SMB/CIFS implementation.
6 Copyright (C) 2014 Samuel Cabrero <samuelcabrero@kernevil.me>
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/>.
24 #include "http_internal.h"
25 #include "lib/util/tevent_ntstatus.h"
26 #include "lib/param/param.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/credentials/credentials.h"
30 #include "lib/util/data_blob.h"
33 * Copy the request headers from src to dst
35 static NTSTATUS http_copy_header(const struct http_request *src,
36 struct http_request *dst)
38 struct http_header *h;
40 dst->type = src->type;
41 dst->major = src->major;
42 dst->minor = src->minor;
43 dst->uri = talloc_strdup(dst, src->uri);
45 for (h = src->headers; h != NULL; h = h->next) {
46 http_add_header(dst, &dst->headers, h->key, h->value);
48 dst->headers_size = src->headers_size;
54 * Retrieve the WWW-Authenticate header from server response based on the
55 * authentication scheme being used.
57 static NTSTATUS http_parse_auth_response(enum http_auth_method auth,
58 struct http_request *auth_response,
61 struct http_header *h;
63 for (h = auth_response->headers; h != NULL; h = h->next) {
64 if (strncasecmp(h->key, "WWW-Authenticate", 16) == 0) {
67 if (strncasecmp(h->value, "NTLM ", 5) == 0) {
68 *in = data_blob_string_const(h->value);
78 return NT_STATUS_NOT_SUPPORTED;
81 struct http_auth_state {
82 struct tevent_context *ev;
84 struct tstream_context *stream;
85 struct tevent_queue *send_queue;
87 enum http_auth_method auth;
89 struct gensec_security *gensec_ctx;
90 NTSTATUS gensec_status;
92 const struct http_request *original_request;
93 struct http_request *next_request;
94 struct http_request *auth_response;
98 static void http_send_auth_request_gensec_done(struct tevent_req *subreq);
99 static void http_send_auth_request_http_req_done(struct tevent_req *subreq);
100 static void http_send_auth_request_http_rep_done(struct tevent_req *subreq);
102 struct tevent_req *http_send_auth_request_send(TALLOC_CTX *mem_ctx,
103 struct tevent_context *ev,
104 struct tstream_context *stream,
105 struct tevent_queue *send_queue,
106 const struct http_request *original_request,
107 struct cli_credentials *credentials,
108 struct loadparm_context *lp_ctx,
109 enum http_auth_method auth)
111 struct tevent_req *req = NULL;
112 struct http_auth_state *state = NULL;
113 struct tevent_req *subreq = NULL;
114 DATA_BLOB gensec_in = data_blob_null;
116 const char *mech_name = NULL;
118 req = tevent_req_create(mem_ctx, &state, struct http_auth_state);
123 state->stream = stream;
124 state->send_queue = send_queue;
126 state->original_request = original_request;
128 status = gensec_init();
129 if (tevent_req_nterror(req, status)) {
130 return tevent_req_post(req, ev);
133 status = gensec_client_start(state, &state->gensec_ctx,
134 lpcfg_gensec_settings(state, lp_ctx));
135 if (tevent_req_nterror(req, status)) {
136 return tevent_req_post(req, ev);
139 status = gensec_set_credentials(state->gensec_ctx, credentials);
140 if (tevent_req_nterror(req, status)) {
141 return tevent_req_post(req, ev);
144 switch (state->auth) {
145 case HTTP_AUTH_BASIC:
146 mech_name = "http_basic";
149 mech_name = "http_ntlm";
152 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
153 return tevent_req_post(req, ev);
156 status = gensec_start_mech_by_name(state->gensec_ctx, mech_name);
157 if (tevent_req_nterror(req, status)) {
158 return tevent_req_post(req, ev);
161 subreq = gensec_update_send(state, state->ev,
164 if (tevent_req_nomem(subreq, req)) {
165 return tevent_req_post(req, ev);
167 tevent_req_set_callback(subreq, http_send_auth_request_gensec_done, req);
172 static void http_send_auth_request_gensec_done(struct tevent_req *subreq)
174 struct tevent_req *req =
175 tevent_req_callback_data(subreq,
177 struct http_auth_state *state =
179 struct http_auth_state);
180 DATA_BLOB gensec_out = data_blob_null;
184 TALLOC_FREE(state->auth_response);
186 status = gensec_update_recv(subreq, state, &gensec_out);
188 state->gensec_status = status;
189 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
190 status = NT_STATUS_OK;
192 if (tevent_req_nterror(req, status)) {
196 state->next_request = talloc_zero(state, struct http_request);
197 if (tevent_req_nomem(state->next_request, req)) {
201 status = http_copy_header(state->original_request, state->next_request);
202 if (tevent_req_nterror(req, status)) {
206 if (!NT_STATUS_IS_OK(state->gensec_status)) {
208 * More preprocessing required before we
209 * can include the content.
211 ret = http_replace_header(state->next_request,
212 &state->next_request->headers,
213 "Content-Length", "0");
219 state->next_request->body = state->original_request->body;
222 if (gensec_out.length > 0) {
223 ret = http_add_header(state->next_request,
224 &state->next_request->headers,
226 (char *)gensec_out.data);
231 data_blob_free(&gensec_out);
234 subreq = http_send_request_send(state, state->ev,
237 state->next_request);
238 if (tevent_req_nomem(subreq, req)) {
241 tevent_req_set_callback(subreq,
242 http_send_auth_request_http_req_done,
246 static void http_send_auth_request_http_req_done(struct tevent_req *subreq)
248 struct tevent_req *req =
249 tevent_req_callback_data(subreq,
251 struct http_auth_state *state =
253 struct http_auth_state);
256 TALLOC_FREE(state->next_request);
258 status = http_send_request_recv(subreq);
260 if (tevent_req_nterror(req, status)) {
265 * If no more processing required, it is done
267 * The caller will use http_read_response_send/recv
268 * in order to get the high level response.
270 if (NT_STATUS_IS_OK(state->gensec_status)) {
271 tevent_req_done(req);
276 * If more processing required, read the response from server
278 * We may get an empty RPCH Echo packet from the server
279 * on the "RPC_OUT_DATA" path. We need to consume this
280 * from the socket, but for now we just ignore the bytes.
282 subreq = http_read_response_send(state, state->ev,
285 if (tevent_req_nomem(subreq, req)) {
288 tevent_req_set_callback(subreq,
289 http_send_auth_request_http_rep_done,
293 static void http_send_auth_request_http_rep_done(struct tevent_req *subreq)
295 struct tevent_req *req =
296 tevent_req_callback_data(subreq,
298 struct http_auth_state *state =
300 struct http_auth_state);
301 DATA_BLOB gensec_in = data_blob_null;
304 status = http_read_response_recv(subreq, state,
305 &state->auth_response);
307 if (tevent_req_nterror(req, status)) {
312 * We we asked for up to UINT16_MAX bytes of
313 * content, we don't expect
314 * state->auth_response->remaining_content_length
317 * For now we just ignore any bytes in
318 * state->auth_response->body.
320 if (state->auth_response->remaining_content_length != 0) {
321 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
325 status = http_parse_auth_response(state->auth,
326 state->auth_response,
328 if (tevent_req_nterror(req, status)) {
332 subreq = gensec_update_send(state, state->ev,
335 if (tevent_req_nomem(subreq, req)) {
338 tevent_req_set_callback(subreq, http_send_auth_request_gensec_done, req);
341 NTSTATUS http_send_auth_request_recv(struct tevent_req *req)
345 if (tevent_req_is_nterror(req, &status)) {
346 tevent_req_received(req);
349 tevent_req_received(req);