07470fa71c2e0a9943ee5b7bfc1f262b4459f1ff
[samba.git] / source4 / lib / http / gensec / ntlm.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    HTTP library - NTLM authentication mechanism gensec module
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 "auth/auth.h"
24 #include "auth/gensec/gensec.h"
25 #include "auth/gensec/gensec_internal.h"
26
27 _PUBLIC_ NTSTATUS gensec_http_ntlm_init(void);
28
29 struct gensec_http_ntlm_state {
30         struct gensec_security *sub;
31 };
32
33 static NTSTATUS gensec_http_ntlm_client_start(struct gensec_security *gensec)
34 {
35         NTSTATUS status;
36         struct gensec_http_ntlm_state *state;
37
38         state = talloc_zero(gensec, struct gensec_http_ntlm_state);
39         if (state == NULL) {
40                 return NT_STATUS_NO_MEMORY;
41         }
42         gensec->private_data = state;
43
44         status = gensec_subcontext_start(state, gensec, &state->sub);
45         if (!NT_STATUS_IS_OK(status)) {
46                 return status;
47         }
48
49         return gensec_start_mech_by_oid(state->sub, GENSEC_OID_NTLMSSP);
50 }
51
52 static NTSTATUS gensec_http_ntlm_update(struct gensec_security *gensec_ctx,
53                                         TALLOC_CTX *mem_ctx,
54                                         struct tevent_context *ev,
55                                         const DATA_BLOB in,
56                                         DATA_BLOB *out)
57 {
58         NTSTATUS status;
59         struct gensec_http_ntlm_state *state;
60         DATA_BLOB ntlm_in;
61
62         state = talloc_get_type_abort(gensec_ctx->private_data,
63                                       struct gensec_http_ntlm_state);
64
65         if (in.length) {
66                 if (strncasecmp((char *)in.data, "NTLM ", 5) != 0) {
67                         return NT_STATUS_INVALID_PARAMETER;
68                 }
69                 ntlm_in = base64_decode_data_blob_talloc(mem_ctx,
70                                                          (char *)&in.data[5]);
71         } else {
72                 ntlm_in = data_blob_null;
73         }
74
75         status = gensec_update_ev(state->sub, mem_ctx, ev, ntlm_in, out);
76         if (NT_STATUS_IS_OK(status) ||
77             NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
78                 char *tmp, *b64;
79                 b64 = base64_encode_data_blob(mem_ctx, *out);
80                 if (b64 == NULL) {
81                         return NT_STATUS_NO_MEMORY;
82                 }
83
84                 tmp = talloc_asprintf(mem_ctx, "NTLM %s", b64);
85                 TALLOC_FREE(b64);
86                 if (tmp == NULL) {
87                         return NT_STATUS_NO_MEMORY;
88                 }
89                 *out = data_blob_string_const(tmp);
90         }
91
92         if (ntlm_in.data) {
93                 data_blob_free(&ntlm_in);
94         }
95
96         return status;
97 }
98
99 static const struct gensec_security_ops gensec_http_ntlm_security_ops = {
100         .name           = "http_ntlm",
101         .auth_type      = 0,
102         .client_start   = gensec_http_ntlm_client_start,
103         .update         = gensec_http_ntlm_update,
104         .enabled        = true,
105         .priority       = GENSEC_EXTERNAL,
106 };
107
108 _PUBLIC_ NTSTATUS gensec_http_ntlm_init(void)
109 {
110         NTSTATUS status;
111
112         status = gensec_register(&gensec_http_ntlm_security_ops);
113         if (!NT_STATUS_IS_OK(status)) {
114                 DEBUG(0, ("Failed to register '%s' gensec backend!\n",
115                                 gensec_http_ntlm_security_ops.name));
116                 return status;
117         }
118
119         return status;
120 }