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
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
44 static NTSTATUS
check_samba4_security(const struct auth_context
*auth_context
,
45 void *my_private_data
,
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
;
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());
59 DEBUG(10, ("loadparm_init_s3 failed\n"));
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
,
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
,
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
)) {
91 nt_status
= make_server_info_info3(mem_ctx
, user_info
->client
.account_name
,
92 user_info
->mapped
.domain_name
, server_info
,
94 if (!NT_STATUS_IS_OK(nt_status
)) {
95 DEBUG(10, ("make_server_info_info3 failed: %s\n",
96 nt_errstr(nt_status
)));
101 nt_status
= NT_STATUS_OK
;
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
)
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"));
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"));
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"));
139 return NT_STATUS_INVALID_SERVER_STATE
;
142 msg_ctx
= imessaging_init(frame
,
146 if (msg_ctx
== NULL
) {
147 DEBUG(1, ("imessaging_init failed\n"));
149 return NT_STATUS_INVALID_SERVER_STATE
;
152 talloc_reparent(frame
, msg_ctx
, server_id
);
155 = cli_credentials_init(frame
);
156 if (!server_credentials
) {
157 DEBUG(1, ("Failed to init server credentials"));
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
,
172 lp_ctx
, server_credentials
, "cifs",
174 if (!NT_STATUS_IS_OK(status
)) {
175 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(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
;
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
)
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"));
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"));
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"));
222 return NT_STATUS_INVALID_SERVER_STATE
;
225 msg_ctx
= imessaging_init(frame
,
229 if (msg_ctx
== NULL
) {
230 DEBUG(1, ("imessaging_init failed\n"));
232 return NT_STATUS_INVALID_SERVER_STATE
;
234 talloc_reparent(frame
, msg_ctx
, server_id
);
236 status
= auth_context_create(mem_ctx
,
242 if (!NT_STATUS_IS_OK(status
)) {
243 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(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
);
256 /* module initialisation */
257 static NTSTATUS
auth_init_samba4(struct auth_context
*auth_context
,
259 auth_methods
**auth_method
)
261 struct auth_methods
*result
;
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
;
278 NTSTATUS
auth_samba4_init(void)
280 smb_register_auth(AUTH_INTERFACE_VERSION
, "samba4",