lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[samba.git] / auth / gensec / external.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    SASL/EXTERNAL authentication.
5
6    Copyright (C) Howard Chu <hyc@symas.com> 2013
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 <tevent.h>
24 #include "lib/util/tevent_ntstatus.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27 #include "auth/gensec/gensec_internal.h"
28 #include "auth/gensec/gensec_proto.h"
29 #include "auth/gensec/gensec_toplevel_proto.h"
30
31 /* SASL/EXTERNAL is essentially a no-op; it is only usable when the transport
32  * layer is already mutually authenticated.
33  */
34
35 NTSTATUS gensec_external_init(TALLOC_CTX *ctx);
36
37 static NTSTATUS gensec_external_start(struct gensec_security *gensec_security)
38 {
39         if (gensec_security->want_features & GENSEC_FEATURE_SIGN)
40                 return NT_STATUS_INVALID_PARAMETER;
41         if (gensec_security->want_features & GENSEC_FEATURE_SEAL)
42                 return NT_STATUS_INVALID_PARAMETER;
43
44         return NT_STATUS_OK;
45 }
46
47 struct gensec_external_update_state {
48         DATA_BLOB out;
49 };
50
51 static struct tevent_req *gensec_external_update_send(TALLOC_CTX *mem_ctx,
52                                         struct tevent_context *ev,
53                                         struct gensec_security *gensec_security,
54                                         const DATA_BLOB in)
55 {
56         struct tevent_req *req;
57         struct gensec_external_update_state *state = NULL;
58
59         req = tevent_req_create(mem_ctx, &state,
60                                 struct gensec_external_update_state);
61         if (req == NULL) {
62                 return NULL;
63         }
64
65         state->out = data_blob_talloc(state, "", 0);
66         if (tevent_req_nomem(state->out.data, req)) {
67                 return tevent_req_post(req, ev);
68         }
69
70         tevent_req_done(req);
71         return tevent_req_post(req, ev);
72 }
73
74 static NTSTATUS gensec_external_update_recv(struct tevent_req *req,
75                                             TALLOC_CTX *out_mem_ctx,
76                                             DATA_BLOB *out)
77 {
78         struct gensec_external_update_state *state =
79                 tevent_req_data(req,
80                 struct gensec_external_update_state);
81         NTSTATUS status;
82
83         *out = data_blob_null;
84
85         if (tevent_req_is_nterror(req, &status)) {
86                 tevent_req_received(req);
87                 return status;
88         }
89
90         *out = state->out;
91         tevent_req_received(req);
92         return NT_STATUS_OK;
93 }
94
95 /* We have no features */
96 static bool gensec_external_have_feature(struct gensec_security *gensec_security,
97                                      uint32_t feature)
98 {
99         return false;
100 }
101
102 static const struct gensec_security_ops gensec_external_ops = {
103         .name             = "sasl-EXTERNAL",
104         .sasl_name        = "EXTERNAL",
105         .client_start     = gensec_external_start,
106         .update_send      = gensec_external_update_send,
107         .update_recv      = gensec_external_update_recv,
108         .have_feature     = gensec_external_have_feature,
109         .enabled          = true,
110         .priority         = GENSEC_EXTERNAL
111 };
112
113
114 NTSTATUS gensec_external_init(TALLOC_CTX *ctx)
115 {
116         NTSTATUS ret;
117
118         ret = gensec_register(&gensec_external_ops);
119         if (!NT_STATUS_IS_OK(ret)) {
120                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
121                          gensec_external_ops.name));
122         }
123         return ret;
124 }