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 "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"
32 #define DBGC_CLASS DBGC_AUTH
34 static NTSTATUS
make_auth4_context_s4(const struct auth_context
*auth_context
,
36 struct auth4_context
**auth4_context
);
38 static struct idr_context
*task_id_tree
;
40 static int free_task_id(struct server_id
*server_id
)
42 idr_remove(task_id_tree
, server_id
->task_id
);
46 /* Return a server_id with a unique task_id element. Free the
47 * returned pointer to de-allocate the task_id via a talloc destructor
48 * (ie, use talloc_free()) */
49 static struct server_id
*new_server_id_task(TALLOC_CTX
*mem_ctx
)
51 struct server_id
*server_id
;
54 task_id_tree
= idr_init(NULL
);
60 server_id
= talloc(mem_ctx
, struct server_id
);
65 *server_id
= procid_self();
67 /* 0 is the default server_id, so we need to start with 1 */
68 task_id
= idr_get_new_above(task_id_tree
, server_id
, 1, INT32_MAX
);
71 talloc_free(server_id
);
75 talloc_set_destructor(server_id
, free_task_id
);
76 server_id
->task_id
= task_id
;
81 * This module is not an ordinary authentication module. It is really
82 * a way to redirect the whole authentication and authorization stack
83 * to use the source4 auth code, not a way to just handle NTLM
86 * See the comments above each function for how that hook changes the
91 * This hook is currently used by winbindd only, as all other NTLM
92 * logins go via the hooks provided by make_auth4_context_s4() below.
94 * This is only left in case we find a way that it might become useful
95 * in future. Importantly, this routine returns the information
96 * needed for a NETLOGON SamLogon, not what is needed to establish a
99 * We expect we may use this hook in the source3/ winbind when this
100 * services the AD DC. It is tested via pdbtest.
103 static NTSTATUS
check_samba4_security(const struct auth_context
*auth_context
,
104 void *my_private_data
,
106 const struct auth_usersupplied_info
*user_info
,
107 struct auth_serversupplied_info
**server_info
)
109 TALLOC_CTX
*frame
= talloc_stackframe();
110 struct netr_SamInfo3
*info3
= NULL
;
112 struct auth_user_info_dc
*user_info_dc
;
113 struct auth4_context
*auth4_context
;
115 nt_status
= make_auth4_context_s4(auth_context
, mem_ctx
, &auth4_context
);
116 if (!NT_STATUS_IS_OK(nt_status
)) {
121 nt_status
= auth_context_set_challenge(auth4_context
, auth_context
->challenge
.data
, "auth_samba4");
122 if (!NT_STATUS_IS_OK(nt_status
)) {
123 TALLOC_FREE(auth4_context
);
128 nt_status
= auth_check_password(auth4_context
, auth4_context
, user_info
, &user_info_dc
);
129 if (!NT_STATUS_IS_OK(nt_status
)) {
130 TALLOC_FREE(auth4_context
);
135 nt_status
= auth_convert_user_info_dc_saminfo3(mem_ctx
,
138 if (NT_STATUS_IS_OK(nt_status
)) {
139 /* We need the strings from the server_info to be valid as long as the info3 is around */
140 talloc_steal(info3
, user_info_dc
);
142 talloc_free(auth4_context
);
144 if (!NT_STATUS_IS_OK(nt_status
)) {
148 if (user_info
->flags
& USER_INFO_INFO3_AND_NO_AUTHZ
) {
149 *server_info
= make_server_info(mem_ctx
);
150 if (*server_info
== NULL
) {
151 nt_status
= NT_STATUS_NO_MEMORY
;
154 (*server_info
)->info3
= talloc_steal(*server_info
, info3
);
157 nt_status
= make_server_info_info3(mem_ctx
, user_info
->client
.account_name
,
158 user_info
->mapped
.domain_name
, server_info
,
160 if (!NT_STATUS_IS_OK(nt_status
)) {
161 DEBUG(10, ("make_server_info_info3 failed: %s\n",
162 nt_errstr(nt_status
)));
167 nt_status
= NT_STATUS_OK
;
175 * Hook to allow the source4 set of GENSEC modules to handle
176 * blob-based authentication mechanisms, without directly linking the
179 * This may eventually go away, when the GSSAPI acceptors are merged,
180 * when we will just rely on the make_auth4_context_s4 hook instead.
182 * Even for NTLMSSP, which has a common module, significant parts of
183 * the behaviour are overridden here, because it uses the source4 NTLM
184 * stack and the source4 mapping between the PAC/SamLogon response and
187 * It is important to override all this to ensure that the exact same
188 * token is generated and used in the SMB and LDAP servers, for NTLM
191 static NTSTATUS
prepare_gensec(const struct auth_context
*auth_context
,
193 struct gensec_security
**gensec_context
)
196 struct loadparm_context
*lp_ctx
;
197 struct tevent_context
*event_ctx
;
198 TALLOC_CTX
*frame
= talloc_stackframe();
199 struct gensec_security
*gensec_ctx
;
200 struct imessaging_context
*msg_ctx
;
201 struct cli_credentials
*server_credentials
;
202 struct server_id
*server_id
;
204 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
205 if (lp_ctx
== NULL
) {
206 DEBUG(1, ("loadparm_init_s3 failed\n"));
208 return NT_STATUS_INVALID_SERVER_STATE
;
210 event_ctx
= s4_event_context_init(frame
);
211 if (event_ctx
== NULL
) {
212 DEBUG(1, ("s4_event_context_init failed\n"));
214 return NT_STATUS_INVALID_SERVER_STATE
;
217 server_id
= new_server_id_task(frame
);
218 if (server_id
== NULL
) {
219 DEBUG(1, ("new_server_id_task failed\n"));
221 return NT_STATUS_INVALID_SERVER_STATE
;
224 msg_ctx
= imessaging_init(frame
,
228 if (msg_ctx
== NULL
) {
229 DEBUG(1, ("imessaging_init failed\n"));
231 return NT_STATUS_INVALID_SERVER_STATE
;
234 talloc_reparent(frame
, msg_ctx
, server_id
);
237 = cli_credentials_init(frame
);
238 if (!server_credentials
) {
239 DEBUG(1, ("Failed to init server credentials"));
241 return NT_STATUS_INVALID_SERVER_STATE
;
244 cli_credentials_set_conf(server_credentials
, lp_ctx
);
245 status
= cli_credentials_set_machine_account(server_credentials
, lp_ctx
);
246 if (!NT_STATUS_IS_OK(status
)) {
247 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status
)));
248 talloc_free(server_credentials
);
249 server_credentials
= NULL
;
252 status
= samba_server_gensec_start(mem_ctx
,
254 lp_ctx
, server_credentials
, "cifs",
256 if (!NT_STATUS_IS_OK(status
)) {
257 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status
)));
262 talloc_reparent(frame
, gensec_ctx
, msg_ctx
);
263 talloc_reparent(frame
, gensec_ctx
, event_ctx
);
264 talloc_reparent(frame
, gensec_ctx
, lp_ctx
);
265 talloc_reparent(frame
, gensec_ctx
, server_credentials
);
267 gensec_want_feature(gensec_ctx
, GENSEC_FEATURE_SESSION_KEY
);
268 gensec_want_feature(gensec_ctx
, GENSEC_FEATURE_UNIX_TOKEN
);
270 *gensec_context
= gensec_ctx
;
276 * Hook to allow handling of NTLM authentication for AD operation
277 * without directly linking the s4 auth stack
279 * This ensures we use the source4 authentication stack, as well as
280 * the authorization stack to create the user's token. This ensures
281 * consistency between NTLM logins and NTLMSSP logins, as NTLMSSP is
282 * handled by the hook above.
284 static NTSTATUS
make_auth4_context_s4(const struct auth_context
*auth_context
,
286 struct auth4_context
**auth4_context
)
289 struct loadparm_context
*lp_ctx
;
290 struct tevent_context
*event_ctx
;
291 TALLOC_CTX
*frame
= talloc_stackframe();
292 struct imessaging_context
*msg_ctx
;
293 struct server_id
*server_id
;
295 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
296 if (lp_ctx
== NULL
) {
297 DEBUG(1, ("loadparm_init_s3 failed\n"));
299 return NT_STATUS_INVALID_SERVER_STATE
;
301 event_ctx
= s4_event_context_init(frame
);
302 if (event_ctx
== NULL
) {
303 DEBUG(1, ("s4_event_context_init failed\n"));
305 return NT_STATUS_INVALID_SERVER_STATE
;
308 server_id
= new_server_id_task(frame
);
309 if (server_id
== NULL
) {
310 DEBUG(1, ("new_server_id_task failed\n"));
312 return NT_STATUS_INVALID_SERVER_STATE
;
315 msg_ctx
= imessaging_init(frame
,
319 if (msg_ctx
== NULL
) {
320 DEBUG(1, ("imessaging_init failed\n"));
322 return NT_STATUS_INVALID_SERVER_STATE
;
324 talloc_reparent(frame
, msg_ctx
, server_id
);
326 /* Allow forcing a specific auth4 module */
327 if (!auth_context
->forced_samba4_methods
) {
328 status
= auth_context_create(mem_ctx
,
334 const char * const *forced_auth_methods
= (const char * const *)str_list_make(mem_ctx
, auth_context
->forced_samba4_methods
, NULL
);
335 status
= auth_context_create_methods(mem_ctx
, forced_auth_methods
, event_ctx
, msg_ctx
, lp_ctx
, NULL
, auth4_context
);
337 if (!NT_STATUS_IS_OK(status
)) {
338 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status
)));
343 talloc_reparent(frame
, *auth4_context
, msg_ctx
);
344 talloc_reparent(frame
, *auth4_context
, event_ctx
);
345 talloc_reparent(frame
, *auth4_context
, lp_ctx
);
351 /* module initialisation */
352 static NTSTATUS
auth_init_samba4(struct auth_context
*auth_context
,
354 auth_methods
**auth_method
)
356 struct auth_methods
*result
;
360 result
= talloc_zero(auth_context
, struct auth_methods
);
361 if (result
== NULL
) {
362 return NT_STATUS_NO_MEMORY
;
364 result
->name
= "samba4";
365 result
->auth
= check_samba4_security
;
366 result
->prepare_gensec
= prepare_gensec
;
367 result
->make_auth4_context
= make_auth4_context_s4
;
368 result
->flags
= AUTH_METHOD_LOCAL_SAM
;
370 if (param
&& *param
) {
371 auth_context
->forced_samba4_methods
= talloc_strdup(result
, param
);
372 if (!auth_context
->forced_samba4_methods
) {
373 return NT_STATUS_NO_MEMORY
;
377 *auth_method
= result
;
381 NTSTATUS
auth_samba4_init(void);
382 NTSTATUS
auth_samba4_init(void)
384 smb_register_auth(AUTH_INTERFACE_VERSION
, "samba4",