auth: Move wbcAuthUserInfo_to_netr_SamInfo3 to the top level
[nivanova/samba-autobuild/.git] / source3 / auth / auth_generic.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle GENSEC authentication, server side
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett 2001-2003,2011
8    Copyright (C) Simo Sorce 2010.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "auth.h"
26 #include "../lib/tsocket/tsocket.h"
27 #include "auth/gensec/gensec.h"
28 #include "lib/param/param.h"
29 #ifdef HAVE_KRB5
30 #include "auth/kerberos/pac_utils.h"
31 #endif
32 #include "librpc/crypto/gse.h"
33 #include "auth/credentials/credentials.h"
34 #include "lib/param/loadparm.h"
35 #include "librpc/gen_ndr/dcerpc.h"
36
37 static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
38                                                 TALLOC_CTX *mem_ctx,
39                                                 struct smb_krb5_context *smb_krb5_context,
40                                                 DATA_BLOB *pac_blob,
41                                                 const char *princ_name,
42                                                 const struct tsocket_address *remote_address,
43                                                 uint32_t session_info_flags,
44                                                 struct auth_session_info **session_info)
45 {
46         TALLOC_CTX *tmp_ctx;
47         struct PAC_LOGON_INFO *logon_info = NULL;
48         bool is_mapped;
49         bool is_guest;
50         char *ntuser;
51         char *ntdomain;
52         char *username;
53         char *rhost;
54         struct passwd *pw;
55         NTSTATUS status;
56         int rc;
57
58         tmp_ctx = talloc_new(mem_ctx);
59         if (!tmp_ctx) {
60                 return NT_STATUS_NO_MEMORY;
61         }
62
63         if (pac_blob) {
64 #ifdef HAVE_KRB5
65                 status = kerberos_pac_logon_info(tmp_ctx, *pac_blob, NULL, NULL,
66                                                  NULL, NULL, 0, &logon_info);
67 #else
68                 status = NT_STATUS_ACCESS_DENIED;
69 #endif
70                 if (!NT_STATUS_IS_OK(status)) {
71                         goto done;
72                 }
73         }
74
75         rc = get_remote_hostname(remote_address,
76                                  &rhost,
77                                  tmp_ctx);
78         if (rc < 0) {
79                 status = NT_STATUS_NO_MEMORY;
80                 goto done;
81         }
82         if (strequal(rhost, "UNKNOWN")) {
83                 rhost = tsocket_address_inet_addr_string(remote_address,
84                                                          tmp_ctx);
85                 if (rhost == NULL) {
86                         status = NT_STATUS_NO_MEMORY;
87                         goto done;
88                 }
89         }
90
91         status = get_user_from_kerberos_info(tmp_ctx, rhost,
92                                              princ_name, logon_info,
93                                              &is_mapped, &is_guest,
94                                              &ntuser, &ntdomain,
95                                              &username, &pw);
96         if (!NT_STATUS_IS_OK(status)) {
97                 DEBUG(1, ("Failed to map kerberos principal to system user "
98                           "(%s)\n", nt_errstr(status)));
99                 status = NT_STATUS_ACCESS_DENIED;
100                 goto done;
101         }
102
103         /* save the PAC data if we have it */
104         if (logon_info) {
105                 netsamlogon_cache_store(ntuser, &logon_info->info3);
106         }
107
108         /* setup the string used by %U */
109         sub_set_smb_name(username);
110
111         /* reload services so that the new %U is taken into account */
112         lp_load(get_dyn_CONFIGFILE(), false, false, true, true);
113
114         status = make_session_info_krb5(mem_ctx,
115                                         ntuser, ntdomain, username, pw,
116                                         logon_info, is_guest, is_mapped, NULL /* No session key for now, caller will sort it out */,
117                                         session_info);
118         if (!NT_STATUS_IS_OK(status)) {
119                 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
120                           nt_errstr(status)));
121                 status = NT_STATUS_ACCESS_DENIED;
122                 goto done;
123         }
124
125         DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
126                   ntuser, ntdomain, rhost));
127
128         status = NT_STATUS_OK;
129
130 done:
131         TALLOC_FREE(tmp_ctx);
132         return status;
133 }
134
135 static struct auth4_context *make_auth4_context_s3(TALLOC_CTX *mem_ctx, struct auth_context *auth_context)
136 {
137         struct auth4_context *auth4_context = talloc_zero(mem_ctx, struct auth4_context);
138         if (auth4_context == NULL) {
139                 DEBUG(10, ("failed to allocate auth4_context failed\n"));
140                 return NULL;
141         }
142         auth4_context->generate_session_info_pac = auth3_generate_session_info_pac;
143         auth4_context->generate_session_info = auth3_generate_session_info;
144         auth4_context->get_ntlm_challenge = auth3_get_challenge;
145         auth4_context->set_ntlm_challenge = auth3_set_challenge;
146         auth4_context->check_ntlm_password = auth3_check_password;
147         auth4_context->private_data = talloc_steal(auth4_context, auth_context);
148         return auth4_context;
149 }
150
151 NTSTATUS make_auth4_context(TALLOC_CTX *mem_ctx, struct auth4_context **auth4_context_out)
152 {
153         struct auth_context *auth_context;
154         NTSTATUS nt_status;
155
156         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
157         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
158
159         nt_status = make_auth_context_subsystem(tmp_ctx, &auth_context);
160         if (!NT_STATUS_IS_OK(nt_status)) {
161                 TALLOC_FREE(tmp_ctx);
162                 return nt_status;
163         }
164
165         if (auth_context->make_auth4_context) {
166                 nt_status = auth_context->make_auth4_context(mem_ctx, auth4_context_out);
167                 TALLOC_FREE(tmp_ctx);
168                 return nt_status;
169
170         } else {
171                 struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
172                 if (auth4_context == NULL) {
173                         TALLOC_FREE(tmp_ctx);
174                         return NT_STATUS_NO_MEMORY;
175                 }
176                 *auth4_context_out = talloc_steal(mem_ctx, auth4_context);
177                 TALLOC_FREE(tmp_ctx);
178                 return NT_STATUS_OK;
179         }
180 }
181
182 NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
183                               const struct tsocket_address *remote_address,
184                               struct gensec_security **gensec_security_out)
185 {
186         struct gensec_security *gensec_security;
187         struct auth_context *auth_context;
188         NTSTATUS nt_status;
189
190         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
191         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
192
193         nt_status = make_auth_context_subsystem(tmp_ctx, &auth_context);
194         if (!NT_STATUS_IS_OK(nt_status)) {
195                 TALLOC_FREE(tmp_ctx);
196                 return nt_status;
197         }
198
199         if (auth_context->prepare_gensec) {
200                 nt_status = auth_context->prepare_gensec(tmp_ctx,
201                                                          &gensec_security);
202                 if (!NT_STATUS_IS_OK(nt_status)) {
203                         TALLOC_FREE(tmp_ctx);
204                         return nt_status;
205                 }
206         } else {
207                 const struct gensec_security_ops **backends = NULL;
208                 struct gensec_settings *gensec_settings;
209                 struct loadparm_context *lp_ctx;
210                 size_t idx = 0;
211                 struct cli_credentials *server_credentials;
212                 const char *dns_name;
213                 const char *dns_domain;
214                 struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
215                 if (auth4_context == NULL) {
216                         TALLOC_FREE(tmp_ctx);
217                         return NT_STATUS_NO_MEMORY;
218                 }
219
220                 lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
221                 if (lp_ctx == NULL) {
222                         DEBUG(10, ("loadparm_init_s3 failed\n"));
223                         TALLOC_FREE(tmp_ctx);
224                         return NT_STATUS_INVALID_SERVER_STATE;
225                 }
226
227                 gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
228                 if (lp_ctx == NULL) {
229                         DEBUG(10, ("lpcfg_gensec_settings failed\n"));
230                         TALLOC_FREE(tmp_ctx);
231                         return NT_STATUS_NO_MEMORY;
232                 }
233
234                 /*
235                  * This should be a 'netbios domain -> DNS domain'
236                  * mapping, and can currently validly return NULL on
237                  * poorly configured systems.
238                  *
239                  * This is used for the NTLMSSP server
240                  *
241                  */
242                 dns_name = get_mydnsfullname();
243                 if (dns_name == NULL) {
244                         dns_name = "";
245                 }
246
247                 dns_domain = get_mydnsdomname(tmp_ctx);
248                 if (dns_domain == NULL) {
249                         dns_domain = "";
250                 }
251
252                 gensec_settings->server_dns_name = strlower_talloc(gensec_settings, dns_name);
253                 if (gensec_settings->server_dns_name == NULL) {
254                         TALLOC_FREE(tmp_ctx);
255                         return NT_STATUS_NO_MEMORY;
256                 }
257
258                 gensec_settings->server_dns_domain = strlower_talloc(gensec_settings, dns_domain);
259                 if (gensec_settings->server_dns_domain == NULL) {
260                         TALLOC_FREE(tmp_ctx);
261                         return NT_STATUS_NO_MEMORY;
262                 }
263
264                 backends = talloc_zero_array(gensec_settings,
265                                              const struct gensec_security_ops *, 5);
266                 if (backends == NULL) {
267                         TALLOC_FREE(tmp_ctx);
268                         return NT_STATUS_NO_MEMORY;
269                 }
270                 gensec_settings->backends = backends;
271
272                 gensec_init();
273
274                 /* These need to be in priority order, krb5 before NTLMSSP */
275 #if defined(HAVE_KRB5)
276                 backends[idx++] = &gensec_gse_krb5_security_ops;
277 #endif
278
279                 backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_NTLMSSP);
280
281                 backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_SPNEGO);
282
283                 backends[idx++] = gensec_security_by_auth_type(NULL, DCERPC_AUTH_TYPE_SCHANNEL);
284
285                 /*
286                  * This is anonymous for now, because we just use it
287                  * to set the kerberos state at the moment
288                  */
289                 server_credentials = cli_credentials_init_anon(tmp_ctx);
290                 if (!server_credentials) {
291                         DEBUG(0, ("auth_generic_prepare: Failed to init server credentials\n"));
292                         return NT_STATUS_NO_MEMORY;
293                 }
294
295                 cli_credentials_set_conf(server_credentials, lp_ctx);
296
297                 if (lp_security() == SEC_ADS || USE_KERBEROS_KEYTAB) {
298                         cli_credentials_set_kerberos_state(server_credentials, CRED_AUTO_USE_KERBEROS);
299                 } else {
300                         cli_credentials_set_kerberos_state(server_credentials, CRED_DONT_USE_KERBEROS);
301                 }
302
303                 nt_status = gensec_server_start(tmp_ctx, gensec_settings,
304                                                 auth4_context, &gensec_security);
305
306                 if (!NT_STATUS_IS_OK(nt_status)) {
307                         TALLOC_FREE(tmp_ctx);
308                         return nt_status;
309                 }
310
311                 gensec_set_credentials(gensec_security, server_credentials);
312
313                 talloc_unlink(tmp_ctx, lp_ctx);
314                 talloc_unlink(tmp_ctx, server_credentials);
315                 talloc_unlink(tmp_ctx, gensec_settings);
316                 talloc_unlink(tmp_ctx, auth4_context);
317         }
318
319         nt_status = gensec_set_remote_address(gensec_security,
320                                               remote_address);
321         if (!NT_STATUS_IS_OK(nt_status)) {
322                 TALLOC_FREE(tmp_ctx);
323                 return nt_status;
324         }
325
326         *gensec_security_out = talloc_steal(mem_ctx, gensec_security);
327         TALLOC_FREE(tmp_ctx);
328         return NT_STATUS_OK;
329 }
330
331 NTSTATUS auth_check_password_session_info(struct auth4_context *auth_context,
332                                           TALLOC_CTX *mem_ctx,
333                                           struct auth_usersupplied_info *user_info,
334                                           struct auth_session_info **session_info)
335 {
336         NTSTATUS nt_status;
337         void *server_info;
338
339         nt_status = auth_context->check_ntlm_password(auth_context,
340                                                       talloc_tos(),
341                                                       user_info,
342                                                       &server_info, NULL, NULL);
343
344         if (NT_STATUS_IS_OK(nt_status)) {
345                 nt_status = auth_context->generate_session_info(auth_context,
346                                                                 mem_ctx,
347                                                                 server_info,
348                                                                 user_info->client.account_name,
349                                                                 AUTH_SESSION_INFO_UNIX_TOKEN |
350                                                                 AUTH_SESSION_INFO_DEFAULT_GROUPS,
351                                                                 session_info);
352                 TALLOC_FREE(server_info);
353         }
354         return nt_status;
355 }