s3: Fix a debug message
[Samba/gebeck_regimport.git] / source3 / auth / auth_samba4.c
blob7315c1621bd3aef35d603d4da04ac21f5feb35af
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 "source4/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 server_id *server_id;
110 struct cli_credentials *server_credentials;
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(mem_ctx);
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 msg_ctx = imessaging_client_init(frame,
126 lpcfg_imessaging_path(frame, lp_ctx),
127 event_ctx);
128 if (msg_ctx == NULL) {
129 DEBUG(1, ("imessaging_init failed\n"));
130 TALLOC_FREE(frame);
131 return NT_STATUS_INVALID_SERVER_STATE;
134 server_credentials
135 = cli_credentials_init(frame);
136 if (!server_credentials) {
137 DEBUG(1, ("Failed to init server credentials"));
138 TALLOC_FREE(frame);
139 return NT_STATUS_INVALID_SERVER_STATE;
142 cli_credentials_set_conf(server_credentials, lp_ctx);
143 status = cli_credentials_set_machine_account(server_credentials, lp_ctx);
144 if (!NT_STATUS_IS_OK(status)) {
145 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
146 talloc_free(server_credentials);
147 server_credentials = NULL;
150 status = samba_server_gensec_start(mem_ctx,
151 event_ctx, msg_ctx,
152 lp_ctx, server_credentials, "cifs",
153 &gensec_ctx);
154 if (!NT_STATUS_IS_OK(status)) {
155 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
156 TALLOC_FREE(frame);
157 return status;
160 talloc_reparent(frame, gensec_ctx, msg_ctx);
161 talloc_reparent(frame, gensec_ctx, event_ctx);
162 talloc_reparent(frame, gensec_ctx, lp_ctx);
163 talloc_reparent(frame, gensec_ctx, server_credentials);
165 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
166 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
168 *gensec_context = gensec_ctx;
169 TALLOC_FREE(frame);
170 return status;
173 /* module initialisation */
174 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
175 const char *param,
176 auth_methods **auth_method)
178 struct auth_methods *result;
180 gensec_init();
182 result = talloc_zero(auth_context, struct auth_methods);
183 if (result == NULL) {
184 return NT_STATUS_NO_MEMORY;
186 result->name = "samba4";
187 result->auth = check_samba4_security;
188 result->prepare_gensec = prepare_gensec;
189 result->gensec_start_mech_by_oid = gensec_start_mech_by_oid;
190 result->gensec_start_mech_by_authtype = gensec_start_mech_by_authtype;
192 *auth_method = result;
193 return NT_STATUS_OK;
196 NTSTATUS auth_samba4_init(void)
198 smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
199 auth_init_samba4);
200 return NT_STATUS_OK;