s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / auth / auth_samba4.c
blob1bf4227b9ef8dfbee43f5b5ccf7bc85e104f2860
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 static NTSTATUS check_samba4_security(const struct auth_context *auth_context,
35 void *my_private_data,
36 TALLOC_CTX *mem_ctx,
37 const struct auth_usersupplied_info *user_info,
38 struct auth_serversupplied_info **server_info)
40 TALLOC_CTX *frame = talloc_stackframe();
41 struct netr_SamInfo3 *info3 = NULL;
42 NTSTATUS nt_status;
43 struct auth_user_info_dc *user_info_dc;
44 struct auth4_context *auth4_context;
45 struct loadparm_context *lp_ctx;
47 lp_ctx = loadparm_init_s3(frame, loadparm_s3_context());
48 if (lp_ctx == NULL) {
49 DEBUG(10, ("loadparm_init_s3 failed\n"));
50 talloc_free(frame);
51 return NT_STATUS_INVALID_SERVER_STATE;
54 /* We create a private tevent context here to avoid nested loops in
55 * the s3 one, as that may not be expected */
56 nt_status = auth_context_create(mem_ctx,
57 s4_event_context_init(frame), NULL,
58 lp_ctx,
59 &auth4_context);
60 NT_STATUS_NOT_OK_RETURN(nt_status);
62 nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
63 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
65 nt_status = auth_check_password(auth4_context, auth4_context, user_info, &user_info_dc);
66 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
68 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
69 user_info_dc,
70 &info3);
71 if (NT_STATUS_IS_OK(nt_status)) {
72 /* We need the strings from the server_info to be valid as long as the info3 is around */
73 talloc_steal(info3, user_info_dc);
75 talloc_free(auth4_context);
77 if (!NT_STATUS_IS_OK(nt_status)) {
78 goto done;
81 nt_status = make_server_info_info3(mem_ctx, user_info->client.account_name,
82 user_info->mapped.domain_name, server_info,
83 info3);
84 if (!NT_STATUS_IS_OK(nt_status)) {
85 DEBUG(10, ("make_server_info_info3 failed: %s\n",
86 nt_errstr(nt_status)));
87 TALLOC_FREE(frame);
88 return nt_status;
91 nt_status = NT_STATUS_OK;
93 done:
94 TALLOC_FREE(frame);
95 return nt_status;
98 /* Hook to allow GENSEC to handle blob-based authentication
99 * mechanisms, without directly linking the mechansim code */
100 static NTSTATUS prepare_gensec(TALLOC_CTX *mem_ctx,
101 struct gensec_security **gensec_context)
103 NTSTATUS status;
104 struct loadparm_context *lp_ctx;
105 struct tevent_context *event_ctx;
106 TALLOC_CTX *frame = talloc_stackframe();
107 struct gensec_security *gensec_ctx;
108 struct imessaging_context *msg_ctx;
109 struct cli_credentials *server_credentials;
110 struct server_id *server_id;
112 lp_ctx = loadparm_init_s3(frame, loadparm_s3_context());
113 if (lp_ctx == NULL) {
114 DEBUG(1, ("loadparm_init_s3 failed\n"));
115 TALLOC_FREE(frame);
116 return NT_STATUS_INVALID_SERVER_STATE;
118 event_ctx = s4_event_context_init(frame);
119 if (event_ctx == NULL) {
120 DEBUG(1, ("s4_event_context_init failed\n"));
121 TALLOC_FREE(frame);
122 return NT_STATUS_INVALID_SERVER_STATE;
125 server_id = new_server_id_task(frame);
126 if (server_id == NULL) {
127 DEBUG(1, ("new_server_id_task failed\n"));
128 TALLOC_FREE(frame);
129 return NT_STATUS_INVALID_SERVER_STATE;
132 msg_ctx = imessaging_init(frame,
133 lp_ctx,
134 *server_id,
135 event_ctx, true);
136 if (msg_ctx == NULL) {
137 DEBUG(1, ("imessaging_init failed\n"));
138 TALLOC_FREE(frame);
139 return NT_STATUS_INVALID_SERVER_STATE;
142 talloc_reparent(frame, msg_ctx, server_id);
144 server_credentials
145 = cli_credentials_init(frame);
146 if (!server_credentials) {
147 DEBUG(1, ("Failed to init server credentials"));
148 TALLOC_FREE(frame);
149 return NT_STATUS_INVALID_SERVER_STATE;
152 cli_credentials_set_conf(server_credentials, lp_ctx);
153 status = cli_credentials_set_machine_account(server_credentials, lp_ctx);
154 if (!NT_STATUS_IS_OK(status)) {
155 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
156 talloc_free(server_credentials);
157 server_credentials = NULL;
160 status = samba_server_gensec_start(mem_ctx,
161 event_ctx, msg_ctx,
162 lp_ctx, server_credentials, "cifs",
163 &gensec_ctx);
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
166 TALLOC_FREE(frame);
167 return status;
170 talloc_reparent(frame, gensec_ctx, msg_ctx);
171 talloc_reparent(frame, gensec_ctx, event_ctx);
172 talloc_reparent(frame, gensec_ctx, lp_ctx);
173 talloc_reparent(frame, gensec_ctx, server_credentials);
175 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
176 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
178 *gensec_context = gensec_ctx;
179 TALLOC_FREE(frame);
180 return status;
183 /* Hook to allow handling of NTLM authentication for AD operation
184 * without directly linking the s4 auth stack */
185 static NTSTATUS make_auth4_context_s4(TALLOC_CTX *mem_ctx,
186 struct auth4_context **auth4_context)
188 NTSTATUS status;
189 struct loadparm_context *lp_ctx;
190 struct tevent_context *event_ctx;
191 TALLOC_CTX *frame = talloc_stackframe();
192 struct imessaging_context *msg_ctx;
193 struct server_id *server_id;
195 lp_ctx = loadparm_init_s3(frame, loadparm_s3_context());
196 if (lp_ctx == NULL) {
197 DEBUG(1, ("loadparm_init_s3 failed\n"));
198 TALLOC_FREE(frame);
199 return NT_STATUS_INVALID_SERVER_STATE;
201 event_ctx = s4_event_context_init(frame);
202 if (event_ctx == NULL) {
203 DEBUG(1, ("s4_event_context_init failed\n"));
204 TALLOC_FREE(frame);
205 return NT_STATUS_INVALID_SERVER_STATE;
208 server_id = new_server_id_task(frame);
209 if (server_id == NULL) {
210 DEBUG(1, ("new_server_id_task failed\n"));
211 TALLOC_FREE(frame);
212 return NT_STATUS_INVALID_SERVER_STATE;
215 msg_ctx = imessaging_init(frame,
216 lp_ctx,
217 *server_id,
218 event_ctx, true);
219 if (msg_ctx == NULL) {
220 DEBUG(1, ("imessaging_init failed\n"));
221 TALLOC_FREE(frame);
222 return NT_STATUS_INVALID_SERVER_STATE;
224 talloc_reparent(frame, msg_ctx, server_id);
226 status = auth_context_create(mem_ctx,
227 event_ctx,
228 msg_ctx,
229 lp_ctx,
230 auth4_context);
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
234 TALLOC_FREE(frame);
235 return status;
238 talloc_reparent(frame, *auth4_context, msg_ctx);
239 talloc_reparent(frame, *auth4_context, event_ctx);
240 talloc_reparent(frame, *auth4_context, lp_ctx);
242 TALLOC_FREE(frame);
243 return status;
246 /* module initialisation */
247 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
248 const char *param,
249 auth_methods **auth_method)
251 struct auth_methods *result;
253 gensec_init();
255 result = talloc_zero(auth_context, struct auth_methods);
256 if (result == NULL) {
257 return NT_STATUS_NO_MEMORY;
259 result->name = "samba4";
260 result->auth = check_samba4_security;
261 result->prepare_gensec = prepare_gensec;
262 result->make_auth4_context = make_auth4_context_s4;
264 *auth_method = result;
265 return NT_STATUS_OK;
268 NTSTATUS auth_samba4_init(void)
270 smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
271 auth_init_samba4);
272 return NT_STATUS_OK;