auth/gensec: treat struct gensec_security_ops as const if possible.
[bbaumbach/samba-autobuild/.git] / source3 / libads / authdata.c
1 /*
2    Unix SMB/CIFS implementation.
3    kerberos authorization data (PAC) utility library
4    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
6    Copyright (C) Andrew Tridgell 2001
7    Copyright (C) Luke Howard 2002-2003
8    Copyright (C) Stefan Metzmacher 2004-2005
9    Copyright (C) Guenther Deschner 2005,2007,2008
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "librpc/gen_ndr/ndr_krb5pac.h"
27 #include "smb_krb5.h"
28 #include "libads/kerberos_proto.h"
29 #include "auth/common_auth.h"
30 #include "lib/param/param.h"
31 #include "librpc/crypto/gse.h"
32 #include "auth/gensec/gensec.h"
33 #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
34 #include "../libcli/auth/spnego.h"
35
36 #ifdef HAVE_KRB5
37
38 #include "auth/kerberos/pac_utils.h"
39
40 struct smb_krb5_context;
41
42 /****************************************************************
43 Callback to get the PAC_LOGON_INFO from the blob
44 ****************************************************************/
45 static NTSTATUS kerberos_fetch_pac(struct auth4_context *auth_ctx,
46                                    TALLOC_CTX *mem_ctx,
47                                    struct smb_krb5_context *smb_krb5_context,
48                                    DATA_BLOB *pac_blob,
49                                    const char *princ_name,
50                                    const struct tsocket_address *remote_address,
51                                    uint32_t session_info_flags,
52                                    struct auth_session_info **session_info)
53 {
54         TALLOC_CTX *tmp_ctx;
55         struct PAC_LOGON_INFO *logon_info = NULL;
56         NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
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                 status = kerberos_pac_logon_info(tmp_ctx, *pac_blob, NULL, NULL,
65                                                  NULL, NULL, 0, &logon_info);
66                 if (!NT_STATUS_IS_OK(status)) {
67                         goto done;
68                 }
69         }
70
71         talloc_set_name_const(logon_info, "struct PAC_LOGON_INFO");
72
73         auth_ctx->private_data = talloc_steal(auth_ctx, logon_info);
74         *session_info = talloc_zero(mem_ctx, struct auth_session_info);
75         if (!*session_info) {
76                 status = NT_STATUS_NO_MEMORY;
77                 goto done;
78         }
79         status = NT_STATUS_OK;
80
81 done:
82         TALLOC_FREE(tmp_ctx);
83
84         return status;
85 }
86
87 /*
88  * Given the username/password, do a kinit, store the ticket in
89  * cache_name if specified, and return the PAC_LOGON_INFO (the
90  * structure containing the important user information such as
91  * groups).
92  */
93 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
94                              const char *name,
95                              const char *pass,
96                              time_t time_offset,
97                              time_t *expire_time,
98                              time_t *renew_till_time,
99                              const char *cache_name,
100                              bool request_pac,
101                              bool add_netbios_addr,
102                              time_t renewable_time,
103                              const char *impersonate_princ_s,
104                              struct PAC_LOGON_INFO **_logon_info)
105 {
106         krb5_error_code ret;
107         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
108         DATA_BLOB tkt, tkt_wrapped, ap_rep, sesskey1;
109         const char *auth_princ = NULL;
110         const char *local_service = NULL;
111         const char *cc = "MEMORY:kerberos_return_pac";
112         struct auth_session_info *session_info;
113         struct gensec_security *gensec_server_context;
114         const struct gensec_security_ops **backends;
115         struct gensec_settings *gensec_settings;
116         size_t idx = 0;
117         struct auth4_context *auth_context;
118         struct loadparm_context *lp_ctx;
119         struct PAC_LOGON_INFO *logon_info = NULL;
120
121         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
122         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
123
124         ZERO_STRUCT(tkt);
125         ZERO_STRUCT(ap_rep);
126         ZERO_STRUCT(sesskey1);
127
128         if (!name || !pass) {
129                 return NT_STATUS_INVALID_PARAMETER;
130         }
131
132         if (cache_name) {
133                 cc = cache_name;
134         }
135
136         if (!strchr_m(name, '@')) {
137                 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
138                         lp_realm());
139         } else {
140                 auth_princ = name;
141         }
142         NT_STATUS_HAVE_NO_MEMORY(auth_princ);
143
144         local_service = talloc_asprintf(mem_ctx, "%s$@%s",
145                                         lp_netbios_name(), lp_realm());
146         NT_STATUS_HAVE_NO_MEMORY(local_service);
147
148         ret = kerberos_kinit_password_ext(auth_princ,
149                                           pass,
150                                           time_offset,
151                                           expire_time,
152                                           renew_till_time,
153                                           cc,
154                                           request_pac,
155                                           add_netbios_addr,
156                                           renewable_time,
157                                           &status);
158         if (ret) {
159                 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
160                         auth_princ, error_message(ret), ret));
161                 /* status already set */
162                 goto out;
163         }
164
165         DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
166         if (expire_time) {
167                 DEBUGADD(10,("\tvalid until: %s (%d)\n",
168                         http_timestring(talloc_tos(), *expire_time),
169                         (int)*expire_time));
170         }
171         if (renew_till_time) {
172                 DEBUGADD(10,("\trenewable till: %s (%d)\n",
173                         http_timestring(talloc_tos(), *renew_till_time),
174                         (int)*renew_till_time));
175         }
176
177         /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
178          * in that case fallback to NTLM - gd */
179
180         if (expire_time && renew_till_time &&
181             (*expire_time == 0) && (*renew_till_time == 0)) {
182                 return NT_STATUS_INVALID_LOGON_TYPE;
183         }
184
185         ret = cli_krb5_get_ticket(mem_ctx,
186                                   local_service,
187                                   time_offset,
188                                   &tkt,
189                                   &sesskey1,
190                                   0,
191                                   cc,
192                                   NULL,
193                                   impersonate_princ_s);
194         if (ret) {
195                 DEBUG(1,("failed to get ticket for %s: %s\n",
196                         local_service, error_message(ret)));
197                 if (impersonate_princ_s) {
198                         DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
199                                 impersonate_princ_s));
200                 }
201                 status = krb5_to_nt_status(ret);
202                 goto out;
203         }
204
205         /* wrap that up in a nice GSS-API wrapping */
206         tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
207         if (tkt_wrapped.data == NULL) {
208                 status = NT_STATUS_NO_MEMORY;
209                 goto out;
210         }
211
212         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
213         if (auth_context == NULL) {
214                 status = NT_STATUS_NO_MEMORY;
215                 goto out;
216         }
217         auth_context->generate_session_info_pac = kerberos_fetch_pac;
218
219         lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
220         if (lp_ctx == NULL) {
221                 status = NT_STATUS_INVALID_SERVER_STATE;
222                 DEBUG(10, ("loadparm_init_s3 failed\n"));
223                 goto out;
224         }
225
226         gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
227         if (lp_ctx == NULL) {
228                 status = NT_STATUS_NO_MEMORY;
229                 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
230                 goto out;
231         }
232
233         backends = talloc_zero_array(gensec_settings,
234                                      const struct gensec_security_ops *, 2);
235         if (backends == NULL) {
236                 status = NT_STATUS_NO_MEMORY;
237                 goto out;
238         }
239         gensec_settings->backends = backends;
240
241         gensec_init();
242
243         backends[idx++] = &gensec_gse_krb5_security_ops;
244
245         status = gensec_server_start(tmp_ctx, gensec_settings,
246                                         auth_context, &gensec_server_context);
247
248         if (!NT_STATUS_IS_OK(status)) {
249                 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
250                 goto out;
251         }
252
253         talloc_unlink(tmp_ctx, lp_ctx);
254         talloc_unlink(tmp_ctx, gensec_settings);
255         talloc_unlink(tmp_ctx, auth_context);
256
257         status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
258         if (!NT_STATUS_IS_OK(status)) {
259                 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
260                 goto out;
261         }
262
263         /* Do a client-server update dance */
264         status = gensec_update(gensec_server_context, tmp_ctx, NULL, tkt_wrapped, &ap_rep);
265         if (!NT_STATUS_IS_OK(status)) {
266                 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
267                 goto out;
268         }
269
270         /* Now return the PAC information to the callers.  We ingore
271          * the session_info and instead pick out the PAC via the
272          * private_data on the auth_context */
273         status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
274         if (!NT_STATUS_IS_OK(status)) {
275                 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
276                 goto out;
277         }
278
279         logon_info = talloc_get_type_abort(gensec_server_context->auth_context->private_data,
280                                            struct PAC_LOGON_INFO);
281         if (logon_info == NULL) {
282                 DEBUG(1,("no PAC\n"));
283                 status = NT_STATUS_INVALID_PARAMETER;
284                 goto out;
285         }
286
287         *_logon_info = talloc_move(mem_ctx, &logon_info);
288
289 out:
290         talloc_free(tmp_ctx);
291         if (cc != cache_name) {
292                 ads_kdestroy(cc);
293         }
294
295         data_blob_free(&tkt);
296         data_blob_free(&ap_rep);
297         data_blob_free(&sesskey1);
298
299         return status;
300 }
301
302 #endif