d134bd6521999f4cd2e20c254cdc7995fc0f9a29
[sfrench/samba-autobuild/.git] / source4 / lib / http / http_auth.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    HTTP library
5
6    Copyright (C) 2014 Samuel Cabrero <samuelcabrero@kernevil.me>
7
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.
12
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.
17
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/>.
20 */
21
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"
31
32 /**
33  * Copy the request headers from src to dst
34  */
35 static NTSTATUS http_copy_header(const struct http_request *src,
36                                  struct http_request *dst)
37 {
38         struct http_header *h;
39
40         dst->type = src->type;
41         dst->major = src->major;
42         dst->minor = src->minor;
43         dst->uri = talloc_strdup(dst, src->uri);
44
45         for (h = src->headers; h != NULL; h = h->next) {
46                 http_add_header(dst, &dst->headers, h->key, h->value);
47         }
48         dst->headers_size = src->headers_size;
49
50         return NT_STATUS_OK;
51 }
52
53 /*
54  * Retrieve the WWW-Authenticate header from server response based on the
55  * authentication scheme being used.
56  */
57 static NTSTATUS http_parse_auth_response(enum http_auth_method auth,
58                                          struct http_request *auth_response,
59                                          DATA_BLOB *in)
60 {
61         struct http_header *h;
62
63         for (h = auth_response->headers; h != NULL; h = h->next) {
64                 if (strncasecmp(h->key, "WWW-Authenticate", 16) == 0) {
65                         switch (auth) {
66                         case HTTP_AUTH_NTLM:
67                                 if (strncasecmp(h->value, "NTLM ", 5) == 0) {
68                                         *in = data_blob_string_const(h->value);
69                                         return NT_STATUS_OK;
70                                 }
71                                 break;
72                         default:
73                                 break;
74                         }
75                 }
76         }
77
78         return NT_STATUS_NOT_SUPPORTED;
79 }
80
81 struct http_auth_state {
82         struct tevent_context *ev;
83
84         struct tstream_context *stream;
85         struct tevent_queue *send_queue;
86
87         enum http_auth_method auth;
88
89         struct gensec_security *gensec_ctx;
90         NTSTATUS gensec_status;
91
92         const struct http_request *original_request;
93         struct http_request *next_request;
94         struct http_request *auth_response;
95 };
96
97
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);
101
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)
110 {
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;
115         NTSTATUS status;
116         const char *mech_name = NULL;
117
118         req = tevent_req_create(mem_ctx, &state, struct http_auth_state);
119         if (req == NULL) {
120                 return NULL;
121         }
122         state->ev = ev;
123         state->stream = stream;
124         state->send_queue = send_queue;
125         state->auth = auth;
126         state->original_request = original_request;
127
128         status = gensec_init();
129         if (tevent_req_nterror(req, status)) {
130                 return tevent_req_post(req, ev);
131         }
132
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);
137         }
138
139         status = gensec_set_credentials(state->gensec_ctx, credentials);
140         if (tevent_req_nterror(req, status)) {
141                 return tevent_req_post(req, ev);
142         }
143
144         switch (state->auth) {
145         case HTTP_AUTH_BASIC:
146                 mech_name = "http_basic";
147                 break;
148         case HTTP_AUTH_NTLM:
149                 mech_name = "http_ntlm";
150                 break;
151         default:
152                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
153                 return tevent_req_post(req, ev);
154         }
155
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);
159         }
160
161         subreq = gensec_update_send(state, state->ev,
162                                     state->gensec_ctx,
163                                     gensec_in);
164         if (tevent_req_nomem(subreq, req)) {
165                 return tevent_req_post(req, ev);
166         }
167         tevent_req_set_callback(subreq, http_send_auth_request_gensec_done, req);
168
169         return req;
170 }
171
172 static void http_send_auth_request_gensec_done(struct tevent_req *subreq)
173 {
174         struct tevent_req *req =
175                 tevent_req_callback_data(subreq,
176                 struct tevent_req);
177         struct http_auth_state *state =
178                 tevent_req_data(req,
179                 struct http_auth_state);
180         DATA_BLOB gensec_out = data_blob_null;
181         NTSTATUS status;
182         int ret;
183
184         TALLOC_FREE(state->auth_response);
185
186         status = gensec_update_recv(subreq, state, &gensec_out);
187         TALLOC_FREE(subreq);
188         state->gensec_status = status;
189         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
190                 status = NT_STATUS_OK;
191         }
192         if (tevent_req_nterror(req, status)) {
193                 return;
194         }
195
196         state->next_request = talloc_zero(state, struct http_request);
197         if (tevent_req_nomem(state->next_request, req)) {
198                 return;
199         }
200
201         status = http_copy_header(state->original_request, state->next_request);
202         if (tevent_req_nterror(req, status)) {
203                 return;
204         }
205
206         if (!NT_STATUS_IS_OK(state->gensec_status)) {
207                 /*
208                  * More preprocessing required before we
209                  * can include the content.
210                  */
211                 ret = http_replace_header(state->next_request,
212                                           &state->next_request->headers,
213                                           "Content-Length", "0");
214                 if (ret != 0) {
215                         tevent_req_oom(req);
216                         return;
217                 }
218         } else {
219                 state->next_request->body = state->original_request->body;
220         }
221
222         if (gensec_out.length > 0) {
223                 ret = http_add_header(state->next_request,
224                                       &state->next_request->headers,
225                                       "Authorization",
226                                       (char *)gensec_out.data);
227                 if (ret != 0) {
228                         tevent_req_oom(req);
229                         return;
230                 }
231                 data_blob_free(&gensec_out);
232         }
233
234         subreq = http_send_request_send(state, state->ev,
235                                         state->stream,
236                                         state->send_queue,
237                                         state->next_request);
238         if (tevent_req_nomem(subreq, req)) {
239                 return;
240         }
241         tevent_req_set_callback(subreq,
242                                 http_send_auth_request_http_req_done,
243                                 req);
244 }
245
246 static void http_send_auth_request_http_req_done(struct tevent_req *subreq)
247 {
248         struct tevent_req *req =
249                 tevent_req_callback_data(subreq,
250                 struct tevent_req);
251         struct http_auth_state *state =
252                 tevent_req_data(req,
253                 struct http_auth_state);
254         NTSTATUS status;
255
256         TALLOC_FREE(state->next_request);
257
258         status = http_send_request_recv(subreq);
259         TALLOC_FREE(subreq);
260         if (tevent_req_nterror(req, status)) {
261                 return;
262         }
263
264         /*
265          * If no more processing required, it is done
266          *
267          * The caller will use http_read_response_send/recv
268          * in order to get the high level response.
269          */
270         if (NT_STATUS_IS_OK(state->gensec_status)) {
271                 tevent_req_done(req);
272                 return;
273         }
274
275         /*
276          * If more processing required, read the response from server
277          *
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.
281          */
282         subreq = http_read_response_send(state, state->ev,
283                                          state->stream,
284                                          UINT16_MAX);
285         if (tevent_req_nomem(subreq, req)) {
286                 return;
287         }
288         tevent_req_set_callback(subreq,
289                                 http_send_auth_request_http_rep_done,
290                                 req);
291 }
292
293 static void http_send_auth_request_http_rep_done(struct tevent_req *subreq)
294 {
295         struct tevent_req *req =
296                 tevent_req_callback_data(subreq,
297                 struct tevent_req);
298         struct http_auth_state *state =
299                 tevent_req_data(req,
300                 struct http_auth_state);
301         DATA_BLOB gensec_in = data_blob_null;
302         NTSTATUS status;
303
304         status = http_read_response_recv(subreq, state,
305                                          &state->auth_response);
306         TALLOC_FREE(subreq);
307         if (tevent_req_nterror(req, status)) {
308                 return;
309         }
310
311         /*
312          * We we asked for up to UINT16_MAX bytes of
313          * content, we don't expect
314          * state->auth_response->remaining_content_length
315          * to be set.
316          *
317          * For now we just ignore any bytes in
318          * state->auth_response->body.
319          */
320         if (state->auth_response->remaining_content_length != 0) {
321                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
322                 return;
323         }
324
325         status = http_parse_auth_response(state->auth,
326                                           state->auth_response,
327                                           &gensec_in);
328         if (tevent_req_nterror(req, status)) {
329                 return;
330         }
331
332         subreq = gensec_update_send(state, state->ev,
333                                     state->gensec_ctx,
334                                     gensec_in);
335         if (tevent_req_nomem(subreq, req)) {
336                 return;
337         }
338         tevent_req_set_callback(subreq, http_send_auth_request_gensec_done, req);
339 }
340
341 NTSTATUS http_send_auth_request_recv(struct tevent_req *req)
342 {
343         NTSTATUS status;
344
345         if (tevent_req_is_nterror(req, &status)) {
346                 tevent_req_received(req);
347                 return status;
348         }
349         tevent_req_received(req);
350
351         return NT_STATUS_OK;
352 }