Line data Source code
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
6 :
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.
11 :
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.
16 :
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/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "source3/include/auth.h"
23 : #include "source3/include/messages.h"
24 : #include "source4/auth/auth.h"
25 : #include "auth/auth_sam_reply.h"
26 : #include "param/param.h"
27 : #include "source4/lib/events/events.h"
28 : #include "source4/lib/messaging/messaging.h"
29 : #include "auth/gensec/gensec.h"
30 : #include "auth/credentials/credentials.h"
31 : #include "lib/global_contexts.h"
32 :
33 : #undef DBGC_CLASS
34 : #define DBGC_CLASS DBGC_AUTH
35 :
36 : static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
37 : TALLOC_CTX *mem_ctx,
38 : struct auth4_context **auth4_context);
39 :
40 : static struct idr_context *task_id_tree;
41 :
42 9958 : static int free_task_id(struct server_id *server_id)
43 : {
44 9958 : idr_remove(task_id_tree, server_id->task_id);
45 9958 : return 0;
46 : }
47 :
48 : /* Return a server_id with a unique task_id element. Free the
49 : * returned pointer to de-allocate the task_id via a talloc destructor
50 : * (ie, use talloc_free()) */
51 9958 : static struct server_id *new_server_id_task(TALLOC_CTX *mem_ctx)
52 : {
53 : struct messaging_context *msg_ctx;
54 : struct server_id *server_id;
55 : int task_id;
56 9958 : if (!task_id_tree) {
57 3718 : task_id_tree = idr_init(NULL);
58 3718 : if (!task_id_tree) {
59 0 : return NULL;
60 : }
61 : }
62 :
63 9958 : msg_ctx = global_messaging_context();
64 9958 : if (msg_ctx == NULL) {
65 0 : return NULL;
66 : }
67 :
68 9958 : server_id = talloc(mem_ctx, struct server_id);
69 :
70 9958 : if (!server_id) {
71 0 : return NULL;
72 : }
73 9958 : *server_id = messaging_server_id(msg_ctx);
74 :
75 : /* 0 is the default server_id, so we need to start with 1 */
76 9958 : task_id = idr_get_new_above(task_id_tree, server_id, 1, INT32_MAX);
77 :
78 9958 : if (task_id == -1) {
79 0 : talloc_free(server_id);
80 0 : return NULL;
81 : }
82 :
83 9958 : talloc_set_destructor(server_id, free_task_id);
84 9958 : server_id->task_id = task_id;
85 9958 : return server_id;
86 : }
87 :
88 : /*
89 : * This module is not an ordinary authentication module. It is really
90 : * a way to redirect the whole authentication and authorization stack
91 : * to use the source4 auth code, not a way to just handle NTLM
92 : * authentication.
93 : *
94 : * See the comments above each function for how that hook changes the
95 : * behaviour.
96 : */
97 :
98 : /*
99 : * This hook is currently used by winbindd only, as all other NTLM
100 : * logins go via the hooks provided by make_auth4_context_s4() below.
101 : *
102 : * This is only left in case we find a way that it might become useful
103 : * in future. Importantly, this routine returns the information
104 : * needed for a NETLOGON SamLogon, not what is needed to establish a
105 : * session.
106 : *
107 : * We expect we may use this hook in the source3/ winbind when this
108 : * services the AD DC. It is tested via pdbtest.
109 : */
110 :
111 1 : static NTSTATUS check_samba4_security(
112 : const struct auth_context *auth_context,
113 : void *my_private_data,
114 : TALLOC_CTX *mem_ctx,
115 : const struct auth_usersupplied_info *user_info,
116 : struct auth_serversupplied_info **pserver_info)
117 : {
118 1 : TALLOC_CTX *frame = talloc_stackframe();
119 1 : struct netr_SamInfo3 *info3 = NULL;
120 : NTSTATUS nt_status;
121 : struct auth_user_info_dc *user_info_dc;
122 : struct auth4_context *auth4_context;
123 1 : uint8_t authoritative = 1;
124 1 : struct auth_serversupplied_info *server_info = NULL;
125 :
126 1 : nt_status = make_auth4_context_s4(auth_context, mem_ctx, &auth4_context);
127 1 : if (!NT_STATUS_IS_OK(nt_status)) {
128 0 : TALLOC_FREE(frame);
129 0 : goto done;
130 : }
131 :
132 1 : nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
133 1 : if (!NT_STATUS_IS_OK(nt_status)) {
134 0 : TALLOC_FREE(auth4_context);
135 0 : TALLOC_FREE(frame);
136 0 : return nt_status;
137 : }
138 :
139 1 : nt_status = auth_check_password(auth4_context, auth4_context, user_info,
140 : &user_info_dc, &authoritative);
141 1 : if (!NT_STATUS_IS_OK(nt_status)) {
142 0 : if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) &&
143 0 : authoritative == 0)
144 : {
145 0 : nt_status = NT_STATUS_NOT_IMPLEMENTED;
146 : }
147 0 : TALLOC_FREE(auth4_context);
148 0 : TALLOC_FREE(frame);
149 0 : return nt_status;
150 : }
151 :
152 1 : nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
153 : user_info_dc,
154 : &info3);
155 1 : if (NT_STATUS_IS_OK(nt_status)) {
156 : /* We need the strings from the server_info to be valid as long as the info3 is around */
157 1 : talloc_steal(info3, user_info_dc);
158 : }
159 1 : talloc_free(auth4_context);
160 :
161 1 : if (!NT_STATUS_IS_OK(nt_status)) {
162 0 : goto done;
163 : }
164 :
165 1 : if (user_info->flags & USER_INFO_INFO3_AND_NO_AUTHZ) {
166 0 : server_info = make_server_info(mem_ctx);
167 0 : if (server_info == NULL) {
168 0 : nt_status = NT_STATUS_NO_MEMORY;
169 0 : goto done;
170 : }
171 0 : server_info->info3 = talloc_move(server_info, &info3);
172 : } else {
173 1 : nt_status = make_server_info_info3(
174 : mem_ctx,
175 0 : user_info->client.account_name,
176 0 : user_info->mapped.domain_name,
177 : &server_info,
178 : info3);
179 1 : if (!NT_STATUS_IS_OK(nt_status)) {
180 0 : DEBUG(10, ("make_server_info_info3 failed: %s\n",
181 : nt_errstr(nt_status)));
182 0 : goto done;
183 : }
184 : }
185 :
186 1 : *pserver_info = server_info;
187 1 : nt_status = NT_STATUS_OK;
188 :
189 1 : done:
190 1 : TALLOC_FREE(frame);
191 1 : return nt_status;
192 : }
193 :
194 : /*
195 : * Hook to allow the source4 set of GENSEC modules to handle
196 : * blob-based authentication mechanisms, without directly linking the
197 : * mechanism code.
198 : *
199 : * This may eventually go away, when the GSSAPI acceptors are merged,
200 : * when we will just rely on the make_auth4_context_s4 hook instead.
201 : *
202 : * Even for NTLMSSP, which has a common module, significant parts of
203 : * the behaviour are overridden here, because it uses the source4 NTLM
204 : * stack and the source4 mapping between the PAC/SamLogon response and
205 : * the local token.
206 : *
207 : * It is important to override all this to ensure that the exact same
208 : * token is generated and used in the SMB and LDAP servers, for NTLM
209 : * and for Kerberos.
210 : */
211 9934 : static NTSTATUS prepare_gensec(const struct auth_context *auth_context,
212 : TALLOC_CTX *mem_ctx,
213 : struct gensec_security **gensec_context)
214 : {
215 : NTSTATUS status;
216 : struct loadparm_context *lp_ctx;
217 : struct tevent_context *event_ctx;
218 9934 : TALLOC_CTX *frame = talloc_stackframe();
219 : struct gensec_security *gensec_ctx;
220 : struct imessaging_context *msg_ctx;
221 : struct cli_credentials *server_credentials;
222 : struct server_id *server_id;
223 :
224 9934 : lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
225 9934 : if (lp_ctx == NULL) {
226 0 : DEBUG(1, ("loadparm_init_s3 failed\n"));
227 0 : TALLOC_FREE(frame);
228 0 : return NT_STATUS_INVALID_SERVER_STATE;
229 : }
230 9934 : event_ctx = s4_event_context_init(frame);
231 9934 : if (event_ctx == NULL) {
232 0 : DEBUG(1, ("s4_event_context_init failed\n"));
233 0 : TALLOC_FREE(frame);
234 0 : return NT_STATUS_INVALID_SERVER_STATE;
235 : }
236 :
237 9934 : server_id = new_server_id_task(frame);
238 9934 : if (server_id == NULL) {
239 0 : DEBUG(1, ("new_server_id_task failed\n"));
240 0 : TALLOC_FREE(frame);
241 0 : return NT_STATUS_INVALID_SERVER_STATE;
242 : }
243 :
244 9934 : msg_ctx = imessaging_init_discard_incoming(frame,
245 : lp_ctx,
246 : *server_id,
247 : event_ctx);
248 9934 : if (msg_ctx == NULL) {
249 0 : DEBUG(1, ("imessaging_init_discard_incoming failed\n"));
250 0 : TALLOC_FREE(frame);
251 0 : return NT_STATUS_INVALID_SERVER_STATE;
252 : }
253 :
254 9934 : talloc_reparent(frame, msg_ctx, server_id);
255 :
256 : server_credentials
257 9934 : = cli_credentials_init_server(frame, lp_ctx);
258 9934 : if (!server_credentials) {
259 0 : DEBUG(1, ("Failed to init server credentials"));
260 0 : TALLOC_FREE(frame);
261 0 : return NT_STATUS_INVALID_SERVER_STATE;
262 : }
263 :
264 9934 : status = samba_server_gensec_start(mem_ctx,
265 : event_ctx, msg_ctx,
266 : lp_ctx, server_credentials, "cifs",
267 : &gensec_ctx);
268 9934 : if (!NT_STATUS_IS_OK(status)) {
269 0 : DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
270 0 : TALLOC_FREE(frame);
271 0 : return status;
272 : }
273 :
274 9934 : talloc_reparent(frame, gensec_ctx, msg_ctx);
275 9934 : talloc_reparent(frame, gensec_ctx, event_ctx);
276 9934 : talloc_reparent(frame, gensec_ctx, lp_ctx);
277 9934 : talloc_reparent(frame, gensec_ctx, server_credentials);
278 :
279 9934 : gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
280 9934 : gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
281 :
282 9934 : *gensec_context = gensec_ctx;
283 9934 : TALLOC_FREE(frame);
284 9934 : return status;
285 : }
286 :
287 : /*
288 : * Hook to allow handling of NTLM authentication for AD operation
289 : * without directly linking the s4 auth stack
290 : *
291 : * This ensures we use the source4 authentication stack, as well as
292 : * the authorization stack to create the user's token. This ensures
293 : * consistency between NTLM logins and NTLMSSP logins, as NTLMSSP is
294 : * handled by the hook above.
295 : */
296 24 : static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
297 : TALLOC_CTX *mem_ctx,
298 : struct auth4_context **auth4_context)
299 : {
300 : NTSTATUS status;
301 : struct loadparm_context *lp_ctx;
302 : struct tevent_context *event_ctx;
303 24 : TALLOC_CTX *frame = talloc_stackframe();
304 : struct imessaging_context *msg_ctx;
305 : struct server_id *server_id;
306 :
307 24 : lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
308 24 : if (lp_ctx == NULL) {
309 0 : DEBUG(1, ("loadparm_init_s3 failed\n"));
310 0 : TALLOC_FREE(frame);
311 0 : return NT_STATUS_INVALID_SERVER_STATE;
312 : }
313 24 : event_ctx = s4_event_context_init(frame);
314 24 : if (event_ctx == NULL) {
315 0 : DEBUG(1, ("s4_event_context_init failed\n"));
316 0 : TALLOC_FREE(frame);
317 0 : return NT_STATUS_INVALID_SERVER_STATE;
318 : }
319 :
320 24 : server_id = new_server_id_task(frame);
321 24 : if (server_id == NULL) {
322 0 : DEBUG(1, ("new_server_id_task failed\n"));
323 0 : TALLOC_FREE(frame);
324 0 : return NT_STATUS_INVALID_SERVER_STATE;
325 : }
326 :
327 24 : msg_ctx = imessaging_init_discard_incoming(frame,
328 : lp_ctx,
329 : *server_id,
330 : event_ctx);
331 24 : if (msg_ctx == NULL) {
332 0 : DEBUG(1, ("imessaging_init_discard_incoming failed\n"));
333 0 : TALLOC_FREE(frame);
334 0 : return NT_STATUS_INVALID_SERVER_STATE;
335 : }
336 24 : talloc_reparent(frame, msg_ctx, server_id);
337 :
338 : /* Allow forcing a specific auth4 module */
339 24 : if (!auth_context->forced_samba4_methods) {
340 24 : status = auth_context_create(mem_ctx,
341 : event_ctx,
342 : msg_ctx,
343 : lp_ctx,
344 : auth4_context);
345 : } else {
346 0 : const char * const *forced_auth_methods = (const char * const *)str_list_make(mem_ctx, auth_context->forced_samba4_methods, NULL);
347 0 : status = auth_context_create_methods(mem_ctx, forced_auth_methods, event_ctx, msg_ctx, lp_ctx, NULL, auth4_context);
348 : }
349 24 : if (!NT_STATUS_IS_OK(status)) {
350 0 : DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
351 0 : TALLOC_FREE(frame);
352 0 : return status;
353 : }
354 :
355 24 : talloc_reparent(frame, *auth4_context, msg_ctx);
356 24 : talloc_reparent(frame, *auth4_context, event_ctx);
357 24 : talloc_reparent(frame, *auth4_context, lp_ctx);
358 :
359 24 : TALLOC_FREE(frame);
360 24 : return status;
361 : }
362 :
363 : /* module initialisation */
364 9958 : static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
365 : const char *param,
366 : struct auth_methods **auth_method)
367 : {
368 : struct auth_methods *result;
369 :
370 9958 : gensec_init();
371 :
372 9958 : result = talloc_zero(auth_context, struct auth_methods);
373 9958 : if (result == NULL) {
374 0 : return NT_STATUS_NO_MEMORY;
375 : }
376 9958 : result->name = "samba4";
377 9958 : result->auth = check_samba4_security;
378 9958 : result->prepare_gensec = prepare_gensec;
379 9958 : result->make_auth4_context = make_auth4_context_s4;
380 :
381 9958 : if (param && *param) {
382 0 : auth_context->forced_samba4_methods = talloc_strdup(result, param);
383 0 : if (!auth_context->forced_samba4_methods) {
384 0 : return NT_STATUS_NO_MEMORY;
385 : }
386 : }
387 :
388 9958 : *auth_method = result;
389 9958 : return NT_STATUS_OK;
390 : }
391 :
392 : NTSTATUS auth_samba4_init(TALLOC_CTX *mem_ctx);
393 5099 : NTSTATUS auth_samba4_init(TALLOC_CTX *mem_ctx)
394 : {
395 5099 : smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
396 : auth_init_samba4);
397 5099 : return NT_STATUS_OK;
398 : }
|