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/>.
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"
33 #define DBGC_CLASS DBGC_AUTH
35 static NTSTATUS
make_auth4_context_s4(const struct auth_context
*auth_context
,
37 struct auth4_context
**auth4_context
);
39 static struct idr_context
*task_id_tree
;
41 static int free_task_id(struct server_id
*server_id
)
43 idr_remove(task_id_tree
, server_id
->task_id
);
47 /* Return a server_id with a unique task_id element. Free the
48 * returned pointer to de-allocate the task_id via a talloc destructor
49 * (ie, use talloc_free()) */
50 static struct server_id
*new_server_id_task(TALLOC_CTX
*mem_ctx
)
52 struct messaging_context
*msg_ctx
;
53 struct server_id
*server_id
;
56 task_id_tree
= idr_init(NULL
);
62 msg_ctx
= server_messaging_context();
63 if (msg_ctx
== NULL
) {
67 server_id
= talloc(mem_ctx
, struct server_id
);
72 *server_id
= messaging_server_id(msg_ctx
);
74 /* 0 is the default server_id, so we need to start with 1 */
75 task_id
= idr_get_new_above(task_id_tree
, server_id
, 1, INT32_MAX
);
78 talloc_free(server_id
);
82 talloc_set_destructor(server_id
, free_task_id
);
83 server_id
->task_id
= task_id
;
88 * This module is not an ordinary authentication module. It is really
89 * a way to redirect the whole authentication and authorization stack
90 * to use the source4 auth code, not a way to just handle NTLM
93 * See the comments above each function for how that hook changes the
98 * This hook is currently used by winbindd only, as all other NTLM
99 * logins go via the hooks provided by make_auth4_context_s4() below.
101 * This is only left in case we find a way that it might become useful
102 * in future. Importantly, this routine returns the information
103 * needed for a NETLOGON SamLogon, not what is needed to establish a
106 * We expect we may use this hook in the source3/ winbind when this
107 * services the AD DC. It is tested via pdbtest.
110 static NTSTATUS
check_samba4_security(const struct auth_context
*auth_context
,
111 void *my_private_data
,
113 const struct auth_usersupplied_info
*user_info
,
114 struct auth_serversupplied_info
**server_info
)
116 TALLOC_CTX
*frame
= talloc_stackframe();
117 struct netr_SamInfo3
*info3
= NULL
;
119 struct auth_user_info_dc
*user_info_dc
;
120 struct auth4_context
*auth4_context
;
122 nt_status
= make_auth4_context_s4(auth_context
, mem_ctx
, &auth4_context
);
123 if (!NT_STATUS_IS_OK(nt_status
)) {
128 nt_status
= auth_context_set_challenge(auth4_context
, auth_context
->challenge
.data
, "auth_samba4");
129 if (!NT_STATUS_IS_OK(nt_status
)) {
130 TALLOC_FREE(auth4_context
);
135 nt_status
= auth_check_password(auth4_context
, auth4_context
, user_info
, &user_info_dc
);
136 if (!NT_STATUS_IS_OK(nt_status
)) {
137 TALLOC_FREE(auth4_context
);
142 nt_status
= auth_convert_user_info_dc_saminfo3(mem_ctx
,
145 if (NT_STATUS_IS_OK(nt_status
)) {
146 /* We need the strings from the server_info to be valid as long as the info3 is around */
147 talloc_steal(info3
, user_info_dc
);
149 talloc_free(auth4_context
);
151 if (!NT_STATUS_IS_OK(nt_status
)) {
155 if (user_info
->flags
& USER_INFO_INFO3_AND_NO_AUTHZ
) {
156 *server_info
= make_server_info(mem_ctx
);
157 if (*server_info
== NULL
) {
158 nt_status
= NT_STATUS_NO_MEMORY
;
161 (*server_info
)->info3
= talloc_steal(*server_info
, info3
);
164 nt_status
= make_server_info_info3(mem_ctx
, user_info
->client
.account_name
,
165 user_info
->mapped
.domain_name
, server_info
,
167 if (!NT_STATUS_IS_OK(nt_status
)) {
168 DEBUG(10, ("make_server_info_info3 failed: %s\n",
169 nt_errstr(nt_status
)));
174 nt_status
= NT_STATUS_OK
;
182 * Hook to allow the source4 set of GENSEC modules to handle
183 * blob-based authentication mechanisms, without directly linking the
186 * This may eventually go away, when the GSSAPI acceptors are merged,
187 * when we will just rely on the make_auth4_context_s4 hook instead.
189 * Even for NTLMSSP, which has a common module, significant parts of
190 * the behaviour are overridden here, because it uses the source4 NTLM
191 * stack and the source4 mapping between the PAC/SamLogon response and
194 * It is important to override all this to ensure that the exact same
195 * token is generated and used in the SMB and LDAP servers, for NTLM
198 static NTSTATUS
prepare_gensec(const struct auth_context
*auth_context
,
200 struct gensec_security
**gensec_context
)
203 struct loadparm_context
*lp_ctx
;
204 struct tevent_context
*event_ctx
;
205 TALLOC_CTX
*frame
= talloc_stackframe();
206 struct gensec_security
*gensec_ctx
;
207 struct imessaging_context
*msg_ctx
;
208 struct cli_credentials
*server_credentials
;
209 struct server_id
*server_id
;
211 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
212 if (lp_ctx
== NULL
) {
213 DEBUG(1, ("loadparm_init_s3 failed\n"));
215 return NT_STATUS_INVALID_SERVER_STATE
;
217 event_ctx
= s4_event_context_init(frame
);
218 if (event_ctx
== NULL
) {
219 DEBUG(1, ("s4_event_context_init failed\n"));
221 return NT_STATUS_INVALID_SERVER_STATE
;
224 server_id
= new_server_id_task(frame
);
225 if (server_id
== NULL
) {
226 DEBUG(1, ("new_server_id_task failed\n"));
228 return NT_STATUS_INVALID_SERVER_STATE
;
231 msg_ctx
= imessaging_init(frame
,
235 if (msg_ctx
== NULL
) {
236 DEBUG(1, ("imessaging_init failed\n"));
238 return NT_STATUS_INVALID_SERVER_STATE
;
241 talloc_reparent(frame
, msg_ctx
, server_id
);
244 = cli_credentials_init(frame
);
245 if (!server_credentials
) {
246 DEBUG(1, ("Failed to init server credentials"));
248 return NT_STATUS_INVALID_SERVER_STATE
;
251 cli_credentials_set_conf(server_credentials
, lp_ctx
);
252 status
= cli_credentials_set_machine_account(server_credentials
, lp_ctx
);
253 if (!NT_STATUS_IS_OK(status
)) {
254 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status
)));
255 talloc_free(server_credentials
);
256 server_credentials
= NULL
;
259 status
= samba_server_gensec_start(mem_ctx
,
261 lp_ctx
, server_credentials
, "cifs",
263 if (!NT_STATUS_IS_OK(status
)) {
264 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status
)));
269 talloc_reparent(frame
, gensec_ctx
, msg_ctx
);
270 talloc_reparent(frame
, gensec_ctx
, event_ctx
);
271 talloc_reparent(frame
, gensec_ctx
, lp_ctx
);
272 talloc_reparent(frame
, gensec_ctx
, server_credentials
);
274 gensec_want_feature(gensec_ctx
, GENSEC_FEATURE_SESSION_KEY
);
275 gensec_want_feature(gensec_ctx
, GENSEC_FEATURE_UNIX_TOKEN
);
277 *gensec_context
= gensec_ctx
;
283 * Hook to allow handling of NTLM authentication for AD operation
284 * without directly linking the s4 auth stack
286 * This ensures we use the source4 authentication stack, as well as
287 * the authorization stack to create the user's token. This ensures
288 * consistency between NTLM logins and NTLMSSP logins, as NTLMSSP is
289 * handled by the hook above.
291 static NTSTATUS
make_auth4_context_s4(const struct auth_context
*auth_context
,
293 struct auth4_context
**auth4_context
)
296 struct loadparm_context
*lp_ctx
;
297 struct tevent_context
*event_ctx
;
298 TALLOC_CTX
*frame
= talloc_stackframe();
299 struct imessaging_context
*msg_ctx
;
300 struct server_id
*server_id
;
302 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
303 if (lp_ctx
== NULL
) {
304 DEBUG(1, ("loadparm_init_s3 failed\n"));
306 return NT_STATUS_INVALID_SERVER_STATE
;
308 event_ctx
= s4_event_context_init(frame
);
309 if (event_ctx
== NULL
) {
310 DEBUG(1, ("s4_event_context_init failed\n"));
312 return NT_STATUS_INVALID_SERVER_STATE
;
315 server_id
= new_server_id_task(frame
);
316 if (server_id
== NULL
) {
317 DEBUG(1, ("new_server_id_task failed\n"));
319 return NT_STATUS_INVALID_SERVER_STATE
;
322 msg_ctx
= imessaging_init(frame
,
326 if (msg_ctx
== NULL
) {
327 DEBUG(1, ("imessaging_init failed\n"));
329 return NT_STATUS_INVALID_SERVER_STATE
;
331 talloc_reparent(frame
, msg_ctx
, server_id
);
333 /* Allow forcing a specific auth4 module */
334 if (!auth_context
->forced_samba4_methods
) {
335 status
= auth_context_create(mem_ctx
,
341 const char * const *forced_auth_methods
= (const char * const *)str_list_make(mem_ctx
, auth_context
->forced_samba4_methods
, NULL
);
342 status
= auth_context_create_methods(mem_ctx
, forced_auth_methods
, event_ctx
, msg_ctx
, lp_ctx
, NULL
, auth4_context
);
344 if (!NT_STATUS_IS_OK(status
)) {
345 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status
)));
350 talloc_reparent(frame
, *auth4_context
, msg_ctx
);
351 talloc_reparent(frame
, *auth4_context
, event_ctx
);
352 talloc_reparent(frame
, *auth4_context
, lp_ctx
);
358 /* module initialisation */
359 static NTSTATUS
auth_init_samba4(struct auth_context
*auth_context
,
361 auth_methods
**auth_method
)
363 struct auth_methods
*result
;
367 result
= talloc_zero(auth_context
, struct auth_methods
);
368 if (result
== NULL
) {
369 return NT_STATUS_NO_MEMORY
;
371 result
->name
= "samba4";
372 result
->auth
= check_samba4_security
;
373 result
->prepare_gensec
= prepare_gensec
;
374 result
->make_auth4_context
= make_auth4_context_s4
;
375 result
->flags
= AUTH_METHOD_LOCAL_SAM
;
377 if (param
&& *param
) {
378 auth_context
->forced_samba4_methods
= talloc_strdup(result
, param
);
379 if (!auth_context
->forced_samba4_methods
) {
380 return NT_STATUS_NO_MEMORY
;
384 *auth_method
= result
;
388 NTSTATUS
auth_samba4_init(void);
389 NTSTATUS
auth_samba4_init(void)
391 smb_register_auth(AUTH_INTERFACE_VERSION
, "samba4",