s3-gensec: Add hook to allow gensec to know if kerberos is permitted
[metze/samba/wip.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 "libcli/auth/krb5_wrap.h"
31 #endif
32 #include "librpc/crypto/gse.h"
33 #include "auth/credentials/credentials.h"
34
35 static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
36                                                 TALLOC_CTX *mem_ctx,
37                                                 struct smb_krb5_context *smb_krb5_context,
38                                                 DATA_BLOB *pac_blob,
39                                                 const char *princ_name,
40                                                 const struct tsocket_address *remote_address,
41                                                 uint32_t session_info_flags,
42                                                 struct auth_session_info **session_info)
43 {
44         TALLOC_CTX *tmp_ctx;
45         struct PAC_DATA *pac_data = NULL;
46         struct PAC_LOGON_INFO *logon_info = NULL;
47         unsigned int i;
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_decode_pac(tmp_ctx,
66                                      *pac_blob,
67                                      NULL, NULL, NULL, NULL, 0, &pac_data);
68 #else
69                 status = NT_STATUS_ACCESS_DENIED;
70 #endif
71                 if (!NT_STATUS_IS_OK(status)) {
72                         goto done;
73                 }
74
75                 /* get logon name and logon info */
76                 for (i = 0; i < pac_data->num_buffers; i++) {
77                         struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
78
79                         switch (data_buf->type) {
80                         case PAC_TYPE_LOGON_INFO:
81                                 if (!data_buf->info) {
82                                         break;
83                                 }
84                                 logon_info = data_buf->info->logon_info.info;
85                                 break;
86                         default:
87                                 break;
88                         }
89                 }
90                 if (!logon_info) {
91                         DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
92                         status = NT_STATUS_NOT_FOUND;
93                         goto done;
94                 }
95         }
96
97         rc = get_remote_hostname(remote_address,
98                                  &rhost,
99                                  tmp_ctx);
100         if (rc < 0) {
101                 status = NT_STATUS_NO_MEMORY;
102                 goto done;
103         }
104         if (strequal(rhost, "UNKNOWN")) {
105                 rhost = tsocket_address_inet_addr_string(remote_address,
106                                                          tmp_ctx);
107                 if (rhost == NULL) {
108                         status = NT_STATUS_NO_MEMORY;
109                         goto done;
110                 }
111         }
112
113         status = get_user_from_kerberos_info(tmp_ctx, rhost,
114                                              princ_name, logon_info,
115                                              &is_mapped, &is_guest,
116                                              &ntuser, &ntdomain,
117                                              &username, &pw);
118         if (!NT_STATUS_IS_OK(status)) {
119                 DEBUG(1, ("Failed to map kerberos principal to system user "
120                           "(%s)\n", nt_errstr(status)));
121                 status = NT_STATUS_ACCESS_DENIED;
122                 goto done;
123         }
124
125         /* save the PAC data if we have it */
126         if (logon_info) {
127                 netsamlogon_cache_store(ntuser, &logon_info->info3);
128         }
129
130         status = make_session_info_krb5(mem_ctx,
131                                         ntuser, ntdomain, username, pw,
132                                         logon_info, is_guest, is_mapped, NULL /* No session key for now, caller will sort it out */,
133                                         session_info);
134         if (!NT_STATUS_IS_OK(status)) {
135                 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
136                           nt_errstr(status)));
137                 status = NT_STATUS_ACCESS_DENIED;
138                 goto done;
139         }
140
141         DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
142                   ntuser, ntdomain, rhost));
143
144         status = NT_STATUS_OK;
145
146 done:
147         TALLOC_FREE(tmp_ctx);
148         return status;
149 }
150
151 NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
152                               const struct tsocket_address *remote_address,
153                               struct gensec_security **gensec_security_out)
154 {
155         struct gensec_security *gensec_security;
156         struct auth_context *auth_context;
157         NTSTATUS nt_status;
158
159         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
160         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
161
162         nt_status = make_auth_context_subsystem(tmp_ctx, &auth_context);
163         if (!NT_STATUS_IS_OK(nt_status)) {
164                 TALLOC_FREE(tmp_ctx);
165                 return nt_status;
166         }
167
168         if (auth_context->prepare_gensec) {
169                 nt_status = auth_context->prepare_gensec(tmp_ctx,
170                                                          &gensec_security);
171                 if (!NT_STATUS_IS_OK(nt_status)) {
172                         TALLOC_FREE(tmp_ctx);
173                         return nt_status;
174                 }
175         } else {
176                 struct gensec_settings *gensec_settings;
177                 struct loadparm_context *lp_ctx;
178
179                 struct cli_credentials *server_credentials;
180                 struct auth4_context *auth4_context = talloc_zero(tmp_ctx, struct auth4_context);
181                 if (auth4_context == NULL) {
182                         DEBUG(10, ("failed to allocate auth4_context failed\n"));
183                         TALLOC_FREE(tmp_ctx);
184                         return NT_STATUS_NO_MEMORY;
185                 }
186                 auth4_context->generate_session_info_pac = auth3_generate_session_info_pac;
187
188                 lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_context());
189                 if (lp_ctx == NULL) {
190                         DEBUG(10, ("loadparm_init_s3 failed\n"));
191                         TALLOC_FREE(tmp_ctx);
192                         return NT_STATUS_INVALID_SERVER_STATE;
193                 }
194
195                 gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
196                 if (lp_ctx == NULL) {
197                         DEBUG(10, ("lpcfg_gensec_settings failed\n"));
198                         TALLOC_FREE(tmp_ctx);
199                         return NT_STATUS_NO_MEMORY;
200                 }
201
202                 gensec_settings->backends = talloc_zero_array(gensec_settings, struct gensec_security_ops *, 3);
203                 if (gensec_settings->backends == NULL) {
204                         TALLOC_FREE(tmp_ctx);
205                         return NT_STATUS_NO_MEMORY;
206                 }
207
208                 gensec_settings->backends[0] = &gensec_ntlmssp3_server_ops;
209
210 #if defined(HAVE_KRB5) && defined(HAVE_GSS_WRAP_IOV)
211                 gensec_settings->backends[1] = &gensec_gse_krb5_security_ops;
212 #endif
213
214                 /*
215                  * This is anonymous for now, because we just use it
216                  * to set the kerberos state at the moment
217                  */
218                 server_credentials = cli_credentials_init_anon(tmp_ctx);
219                 if (!server_credentials) {
220                         DEBUG(0, ("auth_generic_prepare: Failed to init server credentials\n"));
221                         return NT_STATUS_NO_MEMORY;
222                 }
223
224                 cli_credentials_set_conf(server_credentials, lp_ctx);
225
226                 if (lp_security() == SEC_ADS || USE_KERBEROS_KEYTAB) {
227                         cli_credentials_set_kerberos_state(server_credentials, CRED_AUTO_USE_KERBEROS);
228                 } else {
229                         cli_credentials_set_kerberos_state(server_credentials, CRED_DONT_USE_KERBEROS);
230                 }
231
232                 nt_status = gensec_server_start(tmp_ctx, gensec_settings,
233                                                 auth4_context, &gensec_security);
234
235                 if (!NT_STATUS_IS_OK(nt_status)) {
236                         TALLOC_FREE(tmp_ctx);
237                         return nt_status;
238                 }
239
240                 gensec_set_credentials(gensec_security, server_credentials);
241
242                 talloc_unlink(tmp_ctx, lp_ctx);
243                 talloc_unlink(tmp_ctx, server_credentials);
244                 talloc_unlink(tmp_ctx, gensec_settings);
245                 talloc_unlink(tmp_ctx, auth4_context);
246         }
247
248         nt_status = gensec_set_remote_address(gensec_security,
249                                               remote_address);
250         if (!NT_STATUS_IS_OK(nt_status)) {
251                 TALLOC_FREE(tmp_ctx);
252                 return nt_status;
253         }
254
255         *gensec_security_out = talloc_steal(mem_ctx, gensec_security);
256         TALLOC_FREE(tmp_ctx);
257         return NT_STATUS_OK;
258 }