2 Unix SMB/CIFS implementation.
4 HTTP library - Basic authentication mechanism gensec module
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/>.
23 #include "auth/auth.h"
24 #include "auth/gensec/gensec.h"
25 #include "auth/gensec/gensec_internal.h"
26 #include "auth/credentials/credentials.h"
27 #include "lib/util/base64.h"
29 _PUBLIC_ NTSTATUS gensec_http_basic_init(void);
31 struct gensec_http_basic_state {
33 GENSEC_HTTP_BASIC_START,
34 GENSEC_HTTP_BASIC_DONE,
35 GENSEC_HTTP_BASIC_ERROR,
39 static NTSTATUS gensec_http_basic_client_start(struct gensec_security *gensec)
41 struct gensec_http_basic_state *state;
43 state = talloc_zero(gensec, struct gensec_http_basic_state);
45 return NT_STATUS_NO_MEMORY;
47 gensec->private_data = state;
49 state->step = GENSEC_HTTP_BASIC_START;
54 static NTSTATUS gensec_http_basic_update(struct gensec_security *gensec_ctx,
56 struct tevent_context *ev,
60 struct gensec_http_basic_state *state;
61 struct cli_credentials *creds;
64 state = talloc_get_type_abort(gensec_ctx->private_data,
65 struct gensec_http_basic_state);
66 creds = gensec_get_credentials(gensec_ctx);
68 switch (gensec_ctx->gensec_role) {
70 switch (state->step) {
71 case GENSEC_HTTP_BASIC_START:
72 tmp = talloc_asprintf(mem_ctx, "%s\\%s:%s",
73 cli_credentials_get_domain(creds),
74 cli_credentials_get_username(creds),
75 cli_credentials_get_password(creds));
77 state->step = GENSEC_HTTP_BASIC_ERROR;
78 return NT_STATUS_NO_MEMORY;
80 *out = data_blob_string_const(tmp);
82 b64 = base64_encode_data_blob(mem_ctx, *out);
84 state->step = GENSEC_HTTP_BASIC_ERROR;
85 return NT_STATUS_NO_MEMORY;
89 tmp = talloc_asprintf(mem_ctx, "Basic %s", b64);
91 state->step = GENSEC_HTTP_BASIC_ERROR;
92 return NT_STATUS_NO_MEMORY;
96 *out = data_blob_string_const(tmp);
97 state->step = GENSEC_HTTP_BASIC_DONE;
100 case GENSEC_HTTP_BASIC_DONE:
101 case GENSEC_HTTP_BASIC_ERROR:
105 state->step = GENSEC_HTTP_BASIC_ERROR;
106 return NT_STATUS_INTERNAL_ERROR;
109 state->step = GENSEC_HTTP_BASIC_ERROR;
110 return NT_STATUS_NOT_IMPLEMENTED;
113 state->step = GENSEC_HTTP_BASIC_ERROR;
114 return NT_STATUS_INTERNAL_ERROR;
117 static const struct gensec_security_ops gensec_http_basic_security_ops = {
118 .name = "http_basic",
120 .client_start = gensec_http_basic_client_start,
121 .update = gensec_http_basic_update,
123 .priority = GENSEC_EXTERNAL,
126 _PUBLIC_ NTSTATUS gensec_http_basic_init(void)
130 status = gensec_register(&gensec_http_basic_security_ops);
131 if (!NT_STATUS_IS_OK(status)) {
132 DEBUG(0, ("Failed to register '%s' gensec backend!\n",
133 gensec_http_basic_security_ops.name));