s3-param: Rename loadparm_s3_context -> loadparm_s3_helpers
[kai/samba.git] / source3 / auth / auth_samba4.c
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 "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"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_AUTH
33
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)
39 {
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;
46
47         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
48         if (lp_ctx == NULL) {
49                 DEBUG(10, ("loadparm_init_s3 failed\n"));
50                 talloc_free(frame);
51                 return NT_STATUS_INVALID_SERVER_STATE;
52         }
53
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);
61                 
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);
64
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);
67         
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);
74         }
75         talloc_free(auth4_context);
76
77         if (!NT_STATUS_IS_OK(nt_status)) {
78                 goto done;
79         }
80
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;
89         }
90
91         nt_status = NT_STATUS_OK;
92
93  done:
94         TALLOC_FREE(frame);
95         return nt_status;
96 }
97
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)
102 {
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 cli_credentials *server_credentials;
110         struct server_id *server_id;
111
112         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
113         if (lp_ctx == NULL) {
114                 DEBUG(1, ("loadparm_init_s3 failed\n"));
115                 TALLOC_FREE(frame);
116                 return NT_STATUS_INVALID_SERVER_STATE;
117         }
118         event_ctx = s4_event_context_init(frame);
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;
123         }
124
125         server_id = new_server_id_task(frame);
126         if (server_id == NULL) {
127                 DEBUG(1, ("new_server_id_task failed\n"));
128                 TALLOC_FREE(frame);
129                 return NT_STATUS_INVALID_SERVER_STATE;
130         }
131
132         msg_ctx = imessaging_init(frame,
133                                   lp_ctx,
134                                   *server_id,
135                                   event_ctx, true);
136         if (msg_ctx == NULL) {
137                 DEBUG(1, ("imessaging_init failed\n"));
138                 TALLOC_FREE(frame);
139                 return NT_STATUS_INVALID_SERVER_STATE;
140         }
141
142         talloc_reparent(frame, msg_ctx, server_id);
143
144         server_credentials
145                 = cli_credentials_init(frame);
146         if (!server_credentials) {
147                 DEBUG(1, ("Failed to init server credentials"));
148                 TALLOC_FREE(frame);
149                 return NT_STATUS_INVALID_SERVER_STATE;
150         }
151
152         cli_credentials_set_conf(server_credentials, lp_ctx);
153         status = cli_credentials_set_machine_account(server_credentials, lp_ctx);
154         if (!NT_STATUS_IS_OK(status)) {
155                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
156                 talloc_free(server_credentials);
157                 server_credentials = NULL;
158         }
159
160         status = samba_server_gensec_start(mem_ctx,
161                                            event_ctx, msg_ctx,
162                                            lp_ctx, server_credentials, "cifs",
163                                            &gensec_ctx);
164         if (!NT_STATUS_IS_OK(status)) {
165                 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
166                 TALLOC_FREE(frame);
167                 return status;
168         }
169
170         talloc_reparent(frame, gensec_ctx, msg_ctx);
171         talloc_reparent(frame, gensec_ctx, event_ctx);
172         talloc_reparent(frame, gensec_ctx, lp_ctx);
173         talloc_reparent(frame, gensec_ctx, server_credentials);
174
175         gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
176         gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
177
178         *gensec_context = gensec_ctx;
179         TALLOC_FREE(frame);
180         return status;
181 }
182
183 /* Hook to allow handling of NTLM authentication for AD operation
184  * without directly linking the s4 auth stack */
185 static NTSTATUS make_auth4_context_s4(TALLOC_CTX *mem_ctx,
186                                       struct auth4_context **auth4_context)
187 {
188         NTSTATUS status;
189         struct loadparm_context *lp_ctx;
190         struct tevent_context *event_ctx;
191         TALLOC_CTX *frame = talloc_stackframe();
192         struct imessaging_context *msg_ctx;
193         struct server_id *server_id;
194
195         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
196         if (lp_ctx == NULL) {
197                 DEBUG(1, ("loadparm_init_s3 failed\n"));
198                 TALLOC_FREE(frame);
199                 return NT_STATUS_INVALID_SERVER_STATE;
200         }
201         event_ctx = s4_event_context_init(frame);
202         if (event_ctx == NULL) {
203                 DEBUG(1, ("s4_event_context_init failed\n"));
204                 TALLOC_FREE(frame);
205                 return NT_STATUS_INVALID_SERVER_STATE;
206         }
207
208         server_id = new_server_id_task(frame);
209         if (server_id == NULL) {
210                 DEBUG(1, ("new_server_id_task failed\n"));
211                 TALLOC_FREE(frame);
212                 return NT_STATUS_INVALID_SERVER_STATE;
213         }
214
215         msg_ctx = imessaging_init(frame,
216                                   lp_ctx,
217                                   *server_id,
218                                   event_ctx, true);
219         if (msg_ctx == NULL) {
220                 DEBUG(1, ("imessaging_init failed\n"));
221                 TALLOC_FREE(frame);
222                 return NT_STATUS_INVALID_SERVER_STATE;
223         }
224         talloc_reparent(frame, msg_ctx, server_id);
225
226         status = auth_context_create(mem_ctx,
227                                         event_ctx,
228                                         msg_ctx,
229                                         lp_ctx,
230                                         auth4_context);
231
232         if (!NT_STATUS_IS_OK(status)) {
233                 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
234                 TALLOC_FREE(frame);
235                 return status;
236         }
237
238         talloc_reparent(frame, *auth4_context, msg_ctx);
239         talloc_reparent(frame, *auth4_context, event_ctx);
240         talloc_reparent(frame, *auth4_context, lp_ctx);
241
242         TALLOC_FREE(frame);
243         return status;
244 }
245
246 /* module initialisation */
247 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
248                                     const char *param,
249                                     auth_methods **auth_method)
250 {
251         struct auth_methods *result;
252
253         gensec_init();
254
255         result = talloc_zero(auth_context, struct auth_methods);
256         if (result == NULL) {
257                 return NT_STATUS_NO_MEMORY;
258         }
259         result->name = "samba4";
260         result->auth = check_samba4_security;
261         result->prepare_gensec = prepare_gensec;
262         result->make_auth4_context = make_auth4_context_s4;
263
264         *auth_method = result;
265         return NT_STATUS_OK;
266 }
267
268 NTSTATUS auth_samba4_init(void)
269 {
270         smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
271                           auth_init_samba4);
272         return NT_STATUS_OK;
273 }