vfs_time_audit: Remove unnecessary return; statement
[Samba.git] / source3 / auth / auth_samba4.c
blob6093de4d9226b15870381bf25186618cfac42f82
1 /*
2 Unix SMB/CIFS implementation.
3 Authenticate against Samba4's auth subsystem
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Andrew Bartlett 2010
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "source3/include/auth.h"
23 #include "source4/auth/auth.h"
24 #include "auth/auth_sam_reply.h"
25 #include "param/param.h"
26 #include "source4/lib/events/events.h"
27 #include "source4/lib/messaging/messaging.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/credentials/credentials.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_AUTH
34 /*
35 * This hook is currently unused, as all NTLM logins go via the hooks
36 * provided by make_auth4_context_s4() below.
38 * This is only left in case we find a way that it might become useful
39 * in future. Importantly, this routine returns the information
40 * needed for a NETLOGON SamLogon, not what is needed to establish a
41 * session.
44 static NTSTATUS check_samba4_security(const struct auth_context *auth_context,
45 void *my_private_data,
46 TALLOC_CTX *mem_ctx,
47 const struct auth_usersupplied_info *user_info,
48 struct auth_serversupplied_info **server_info)
50 TALLOC_CTX *frame = talloc_stackframe();
51 struct netr_SamInfo3 *info3 = NULL;
52 NTSTATUS nt_status;
53 struct auth_user_info_dc *user_info_dc;
54 struct auth4_context *auth4_context;
55 struct loadparm_context *lp_ctx;
57 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
58 if (lp_ctx == NULL) {
59 DEBUG(10, ("loadparm_init_s3 failed\n"));
60 talloc_free(frame);
61 return NT_STATUS_INVALID_SERVER_STATE;
64 /* We create a private tevent context here to avoid nested loops in
65 * the s3 one, as that may not be expected */
66 nt_status = auth_context_create(mem_ctx,
67 s4_event_context_init(frame), NULL,
68 lp_ctx,
69 &auth4_context);
70 NT_STATUS_NOT_OK_RETURN(nt_status);
72 nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
73 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
75 nt_status = auth_check_password(auth4_context, auth4_context, user_info, &user_info_dc);
76 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
78 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
79 user_info_dc,
80 &info3);
81 if (NT_STATUS_IS_OK(nt_status)) {
82 /* We need the strings from the server_info to be valid as long as the info3 is around */
83 talloc_steal(info3, user_info_dc);
85 talloc_free(auth4_context);
87 if (!NT_STATUS_IS_OK(nt_status)) {
88 goto done;
91 nt_status = make_server_info_info3(mem_ctx, user_info->client.account_name,
92 user_info->mapped.domain_name, server_info,
93 info3);
94 if (!NT_STATUS_IS_OK(nt_status)) {
95 DEBUG(10, ("make_server_info_info3 failed: %s\n",
96 nt_errstr(nt_status)));
97 TALLOC_FREE(frame);
98 return nt_status;
101 nt_status = NT_STATUS_OK;
103 done:
104 TALLOC_FREE(frame);
105 return nt_status;
108 /* Hook to allow GENSEC to handle blob-based authentication
109 * mechanisms, without directly linking the mechansim code */
110 static NTSTATUS prepare_gensec(TALLOC_CTX *mem_ctx,
111 struct gensec_security **gensec_context)
113 NTSTATUS status;
114 struct loadparm_context *lp_ctx;
115 struct tevent_context *event_ctx;
116 TALLOC_CTX *frame = talloc_stackframe();
117 struct gensec_security *gensec_ctx;
118 struct imessaging_context *msg_ctx;
119 struct cli_credentials *server_credentials;
120 struct server_id *server_id;
122 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
123 if (lp_ctx == NULL) {
124 DEBUG(1, ("loadparm_init_s3 failed\n"));
125 TALLOC_FREE(frame);
126 return NT_STATUS_INVALID_SERVER_STATE;
128 event_ctx = s4_event_context_init(frame);
129 if (event_ctx == NULL) {
130 DEBUG(1, ("s4_event_context_init failed\n"));
131 TALLOC_FREE(frame);
132 return NT_STATUS_INVALID_SERVER_STATE;
135 server_id = new_server_id_task(frame);
136 if (server_id == NULL) {
137 DEBUG(1, ("new_server_id_task failed\n"));
138 TALLOC_FREE(frame);
139 return NT_STATUS_INVALID_SERVER_STATE;
142 msg_ctx = imessaging_init(frame,
143 lp_ctx,
144 *server_id,
145 event_ctx, true);
146 if (msg_ctx == NULL) {
147 DEBUG(1, ("imessaging_init failed\n"));
148 TALLOC_FREE(frame);
149 return NT_STATUS_INVALID_SERVER_STATE;
152 talloc_reparent(frame, msg_ctx, server_id);
154 server_credentials
155 = cli_credentials_init(frame);
156 if (!server_credentials) {
157 DEBUG(1, ("Failed to init server credentials"));
158 TALLOC_FREE(frame);
159 return NT_STATUS_INVALID_SERVER_STATE;
162 cli_credentials_set_conf(server_credentials, lp_ctx);
163 status = cli_credentials_set_machine_account(server_credentials, lp_ctx);
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
166 talloc_free(server_credentials);
167 server_credentials = NULL;
170 status = samba_server_gensec_start(mem_ctx,
171 event_ctx, msg_ctx,
172 lp_ctx, server_credentials, "cifs",
173 &gensec_ctx);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
176 TALLOC_FREE(frame);
177 return status;
180 talloc_reparent(frame, gensec_ctx, msg_ctx);
181 talloc_reparent(frame, gensec_ctx, event_ctx);
182 talloc_reparent(frame, gensec_ctx, lp_ctx);
183 talloc_reparent(frame, gensec_ctx, server_credentials);
185 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
186 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
188 *gensec_context = gensec_ctx;
189 TALLOC_FREE(frame);
190 return status;
193 /* Hook to allow handling of NTLM authentication for AD operation
194 * without directly linking the s4 auth stack */
195 static NTSTATUS make_auth4_context_s4(TALLOC_CTX *mem_ctx,
196 struct auth4_context **auth4_context)
198 NTSTATUS status;
199 struct loadparm_context *lp_ctx;
200 struct tevent_context *event_ctx;
201 TALLOC_CTX *frame = talloc_stackframe();
202 struct imessaging_context *msg_ctx;
203 struct server_id *server_id;
205 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
206 if (lp_ctx == NULL) {
207 DEBUG(1, ("loadparm_init_s3 failed\n"));
208 TALLOC_FREE(frame);
209 return NT_STATUS_INVALID_SERVER_STATE;
211 event_ctx = s4_event_context_init(frame);
212 if (event_ctx == NULL) {
213 DEBUG(1, ("s4_event_context_init failed\n"));
214 TALLOC_FREE(frame);
215 return NT_STATUS_INVALID_SERVER_STATE;
218 server_id = new_server_id_task(frame);
219 if (server_id == NULL) {
220 DEBUG(1, ("new_server_id_task failed\n"));
221 TALLOC_FREE(frame);
222 return NT_STATUS_INVALID_SERVER_STATE;
225 msg_ctx = imessaging_init(frame,
226 lp_ctx,
227 *server_id,
228 event_ctx, true);
229 if (msg_ctx == NULL) {
230 DEBUG(1, ("imessaging_init failed\n"));
231 TALLOC_FREE(frame);
232 return NT_STATUS_INVALID_SERVER_STATE;
234 talloc_reparent(frame, msg_ctx, server_id);
236 status = auth_context_create(mem_ctx,
237 event_ctx,
238 msg_ctx,
239 lp_ctx,
240 auth4_context);
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
244 TALLOC_FREE(frame);
245 return status;
248 talloc_reparent(frame, *auth4_context, msg_ctx);
249 talloc_reparent(frame, *auth4_context, event_ctx);
250 talloc_reparent(frame, *auth4_context, lp_ctx);
252 TALLOC_FREE(frame);
253 return status;
256 /* module initialisation */
257 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
258 const char *param,
259 auth_methods **auth_method)
261 struct auth_methods *result;
263 gensec_init();
265 result = talloc_zero(auth_context, struct auth_methods);
266 if (result == NULL) {
267 return NT_STATUS_NO_MEMORY;
269 result->name = "samba4";
270 result->auth = check_samba4_security;
271 result->prepare_gensec = prepare_gensec;
272 result->make_auth4_context = make_auth4_context_s4;
274 *auth_method = result;
275 return NT_STATUS_OK;
278 NTSTATUS auth_samba4_init(void)
280 smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
281 auth_init_samba4);
282 return NT_STATUS_OK;