vfs_fruit: just log failing AppleDouble conversion
[Samba.git] / libcli / http / http_auth.c
blob3dcf52c626e4ffc350bf1f182d1ef109434e15b8
1 /*
2 Unix SMB/CIFS implementation.
4 HTTP library
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/>.
22 #include "includes.h"
23 #include "http.h"
24 #include "http_internal.h"
25 #include "lib/util/tevent_ntstatus.h"
26 #include "lib/param/param.h"
27 #include "tevent.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/credentials/credentials.h"
30 #include "lib/util/data_blob.h"
32 #undef strcasecmp
33 #undef strncasecmp
35 /**
36 * Copy the request headers from src to dst
38 static NTSTATUS http_copy_header(const struct http_request *src,
39 struct http_request *dst)
41 struct http_header *h;
43 dst->type = src->type;
44 dst->major = src->major;
45 dst->minor = src->minor;
46 dst->uri = talloc_strdup(dst, src->uri);
48 for (h = src->headers; h != NULL; h = h->next) {
49 http_add_header(dst, &dst->headers, h->key, h->value);
51 dst->headers_size = src->headers_size;
53 return NT_STATUS_OK;
57 * Retrieve the WWW-Authenticate header from server response based on the
58 * authentication scheme being used.
60 static NTSTATUS http_parse_auth_response(const DATA_BLOB prefix,
61 struct http_request *auth_response,
62 DATA_BLOB *in)
64 struct http_header *h;
66 for (h = auth_response->headers; h != NULL; h = h->next) {
67 int cmp;
69 cmp = strcasecmp(h->key, "WWW-Authenticate");
70 if (cmp != 0) {
71 continue;
74 cmp = strncasecmp(h->value,
75 (const char *)prefix.data,
76 prefix.length);
77 if (cmp != 0) {
78 continue;
81 *in = data_blob_string_const(h->value);
82 return NT_STATUS_OK;
85 return NT_STATUS_NOT_SUPPORTED;
88 struct http_auth_state {
89 struct tevent_context *ev;
91 struct http_conn *http_conn;
93 enum http_auth_method auth;
94 DATA_BLOB prefix;
96 struct gensec_security *gensec_ctx;
97 NTSTATUS gensec_status;
99 const struct http_request *original_request;
100 struct http_request *next_request;
101 struct http_request *auth_response;
105 static void http_send_auth_request_gensec_done(struct tevent_req *subreq);
106 static void http_send_auth_request_http_req_done(struct tevent_req *subreq);
107 static void http_send_auth_request_http_rep_done(struct tevent_req *subreq);
109 struct tevent_req *http_send_auth_request_send(TALLOC_CTX *mem_ctx,
110 struct tevent_context *ev,
111 struct http_conn *http_conn,
112 const struct http_request *original_request,
113 struct cli_credentials *credentials,
114 struct loadparm_context *lp_ctx,
115 enum http_auth_method auth)
117 struct tevent_req *req = NULL;
118 struct http_auth_state *state = NULL;
119 struct tevent_req *subreq = NULL;
120 DATA_BLOB gensec_in = data_blob_null;
121 NTSTATUS status;
122 struct http_header *h = NULL;
123 const char *mech_name = NULL;
125 req = tevent_req_create(mem_ctx, &state, struct http_auth_state);
126 if (req == NULL) {
127 return NULL;
129 state->ev = ev;
130 state->http_conn = http_conn;
131 state->auth = auth;
132 state->original_request = original_request;
134 status = gensec_init();
135 if (tevent_req_nterror(req, status)) {
136 return tevent_req_post(req, ev);
139 status = gensec_client_start(state, &state->gensec_ctx,
140 lpcfg_gensec_settings(state, lp_ctx));
141 if (tevent_req_nterror(req, status)) {
142 return tevent_req_post(req, ev);
145 status = gensec_set_credentials(state->gensec_ctx, credentials);
146 if (tevent_req_nterror(req, status)) {
147 return tevent_req_post(req, ev);
150 for (h = original_request->headers; h != NULL; h = h->next) {
151 int cmp;
153 cmp = strcasecmp(h->key, "Host");
154 if (cmp != 0) {
155 continue;
158 status = gensec_set_target_service(state->gensec_ctx, "http");
159 if (tevent_req_nterror(req, status)) {
160 return tevent_req_post(req, ev);
163 status = gensec_set_target_hostname(state->gensec_ctx, h->value);
164 if (tevent_req_nterror(req, status)) {
165 return tevent_req_post(req, ev);
167 break;
170 switch (state->auth) {
171 case HTTP_AUTH_BASIC:
172 mech_name = "http_basic";
173 state->prefix = data_blob_string_const("Basic");
174 break;
175 case HTTP_AUTH_NTLM:
176 mech_name = "http_ntlm";
177 state->prefix = data_blob_string_const("NTLM");
178 break;
179 case HTTP_AUTH_NEGOTIATE:
180 mech_name = "http_negotiate";
181 state->prefix = data_blob_string_const("Negotiate");
182 break;
183 default:
184 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
185 return tevent_req_post(req, ev);
188 status = gensec_start_mech_by_name(state->gensec_ctx, mech_name);
189 if (tevent_req_nterror(req, status)) {
190 return tevent_req_post(req, ev);
193 subreq = gensec_update_send(state, state->ev,
194 state->gensec_ctx,
195 gensec_in);
196 if (tevent_req_nomem(subreq, req)) {
197 return tevent_req_post(req, ev);
199 tevent_req_set_callback(subreq, http_send_auth_request_gensec_done, req);
201 return req;
204 static void http_send_auth_request_gensec_done(struct tevent_req *subreq)
206 struct tevent_req *req =
207 tevent_req_callback_data(subreq,
208 struct tevent_req);
209 struct http_auth_state *state =
210 tevent_req_data(req,
211 struct http_auth_state);
212 DATA_BLOB gensec_out = data_blob_null;
213 NTSTATUS status;
214 int ret;
216 TALLOC_FREE(state->auth_response);
218 status = gensec_update_recv(subreq, state, &gensec_out);
219 TALLOC_FREE(subreq);
220 state->gensec_status = status;
221 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
222 status = NT_STATUS_OK;
224 if (tevent_req_nterror(req, status)) {
225 return;
228 state->next_request = talloc_zero(state, struct http_request);
229 if (tevent_req_nomem(state->next_request, req)) {
230 return;
233 status = http_copy_header(state->original_request, state->next_request);
234 if (tevent_req_nterror(req, status)) {
235 return;
238 if (!NT_STATUS_IS_OK(state->gensec_status)) {
240 * More preprocessing required before we
241 * can include the content.
243 ret = http_replace_header(state->next_request,
244 &state->next_request->headers,
245 "Content-Length", "0");
246 if (ret != 0) {
247 tevent_req_oom(req);
248 return;
250 } else {
251 state->next_request->body = state->original_request->body;
254 if (gensec_out.length > 0) {
255 ret = http_add_header(state->next_request,
256 &state->next_request->headers,
257 "Authorization",
258 (char *)gensec_out.data);
259 if (ret != 0) {
260 tevent_req_oom(req);
261 return;
263 data_blob_free(&gensec_out);
266 subreq = http_send_request_send(state, state->ev,
267 state->http_conn,
268 state->next_request);
269 if (tevent_req_nomem(subreq, req)) {
270 return;
272 tevent_req_set_callback(subreq,
273 http_send_auth_request_http_req_done,
274 req);
277 static void http_send_auth_request_http_req_done(struct tevent_req *subreq)
279 struct tevent_req *req =
280 tevent_req_callback_data(subreq,
281 struct tevent_req);
282 struct http_auth_state *state =
283 tevent_req_data(req,
284 struct http_auth_state);
285 NTSTATUS status;
287 TALLOC_FREE(state->next_request);
289 status = http_send_request_recv(subreq);
290 TALLOC_FREE(subreq);
291 if (tevent_req_nterror(req, status)) {
292 return;
296 * If no more processing required, it is done
298 * The caller will use http_read_response_send/recv
299 * in order to get the high level response.
301 if (NT_STATUS_IS_OK(state->gensec_status)) {
302 tevent_req_done(req);
303 return;
307 * If more processing required, read the response from server
309 * We may get an empty RPCH Echo packet from the server
310 * on the "RPC_OUT_DATA" path. We need to consume this
311 * from the socket, but for now we just ignore the bytes.
313 subreq = http_read_response_send(state, state->ev,
314 state->http_conn,
315 UINT16_MAX);
316 if (tevent_req_nomem(subreq, req)) {
317 return;
319 tevent_req_set_callback(subreq,
320 http_send_auth_request_http_rep_done,
321 req);
324 static void http_send_auth_request_http_rep_done(struct tevent_req *subreq)
326 struct tevent_req *req =
327 tevent_req_callback_data(subreq,
328 struct tevent_req);
329 struct http_auth_state *state =
330 tevent_req_data(req,
331 struct http_auth_state);
332 DATA_BLOB gensec_in = data_blob_null;
333 NTSTATUS status;
335 status = http_read_response_recv(subreq, state,
336 &state->auth_response);
337 TALLOC_FREE(subreq);
338 if (tevent_req_nterror(req, status)) {
339 return;
343 * We we asked for up to UINT16_MAX bytes of
344 * content, we don't expect
345 * state->auth_response->remaining_content_length
346 * to be set.
348 * For now we just ignore any bytes in
349 * state->auth_response->body.
351 if (state->auth_response->remaining_content_length != 0) {
352 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
353 return;
356 status = http_parse_auth_response(state->prefix,
357 state->auth_response,
358 &gensec_in);
359 if (tevent_req_nterror(req, status)) {
360 return;
363 subreq = gensec_update_send(state, state->ev,
364 state->gensec_ctx,
365 gensec_in);
366 if (tevent_req_nomem(subreq, req)) {
367 return;
369 tevent_req_set_callback(subreq, http_send_auth_request_gensec_done, req);
372 NTSTATUS http_send_auth_request_recv(struct tevent_req *req)
374 NTSTATUS status;
376 if (tevent_req_is_nterror(req, &status)) {
377 tevent_req_received(req);
378 return status;
380 tevent_req_received(req);
382 return NT_STATUS_OK;