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
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.
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.
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/>.
26 #include "librpc/gen_ndr/ndr_krb5pac.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"
38 #include "auth/kerberos/pac_utils.h"
40 struct smb_krb5_context
;
42 /****************************************************************
43 Callback to get the PAC_LOGON_INFO from the blob
44 ****************************************************************/
45 static NTSTATUS
kerberos_fetch_pac(struct auth4_context
*auth_ctx
,
47 struct smb_krb5_context
*smb_krb5_context
,
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
)
55 struct PAC_DATA
*pac_data
= NULL
;
56 struct PAC_DATA_CTR
*pac_data_ctr
= NULL
;
57 NTSTATUS status
= NT_STATUS_INTERNAL_ERROR
;
59 tmp_ctx
= talloc_new(mem_ctx
);
61 return NT_STATUS_NO_MEMORY
;
64 if (pac_blob
!= NULL
) {
65 status
= kerberos_decode_pac(tmp_ctx
,
73 if (!NT_STATUS_IS_OK(status
)) {
77 pac_data_ctr
= talloc(mem_ctx
, struct PAC_DATA_CTR
);
78 if (pac_data_ctr
== NULL
) {
79 status
= NT_STATUS_NO_MEMORY
;
83 talloc_set_name_const(pac_data_ctr
, "struct PAC_DATA_CTR");
85 pac_data_ctr
->pac_data
= talloc_steal(pac_data_ctr
, pac_data
);
86 pac_data_ctr
->pac_blob
= data_blob_talloc(pac_data_ctr
,
90 auth_ctx
->private_data
= talloc_steal(auth_ctx
, pac_data_ctr
);
93 *session_info
= talloc_zero(mem_ctx
, struct auth_session_info
);
95 status
= NT_STATUS_NO_MEMORY
;
98 status
= NT_STATUS_OK
;
101 TALLOC_FREE(tmp_ctx
);
107 * Given the username/password, do a kinit, store the ticket in
108 * cache_name if specified, and return the PAC_LOGON_INFO (the
109 * structure containing the important user information such as
112 NTSTATUS
kerberos_return_pac(TALLOC_CTX
*mem_ctx
,
117 time_t *renew_till_time
,
118 const char *cache_name
,
120 bool add_netbios_addr
,
121 time_t renewable_time
,
122 const char *impersonate_princ_s
,
123 const char *local_service
,
124 struct PAC_DATA_CTR
**_pac_data_ctr
)
127 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
128 DATA_BLOB tkt
, tkt_wrapped
, ap_rep
, sesskey1
;
129 const char *auth_princ
= NULL
;
130 const char *cc
= "MEMORY:kerberos_return_pac";
131 struct auth_session_info
*session_info
;
132 struct gensec_security
*gensec_server_context
;
133 const struct gensec_security_ops
**backends
;
134 struct gensec_settings
*gensec_settings
;
136 struct auth4_context
*auth_context
;
137 struct loadparm_context
*lp_ctx
;
138 struct PAC_DATA_CTR
*pac_data_ctr
= NULL
;
140 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
141 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
145 ZERO_STRUCT(sesskey1
);
147 if (!name
|| !pass
) {
148 return NT_STATUS_INVALID_PARAMETER
;
155 if (!strchr_m(name
, '@')) {
156 auth_princ
= talloc_asprintf(mem_ctx
, "%s@%s", name
,
161 NT_STATUS_HAVE_NO_MEMORY(auth_princ
);
163 ret
= kerberos_kinit_password_ext(auth_princ
,
174 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
175 auth_princ
, error_message(ret
), ret
));
176 /* status already set */
180 DEBUG(10,("got TGT for %s in %s\n", auth_princ
, cc
));
182 DEBUGADD(10,("\tvalid until: %s (%d)\n",
183 http_timestring(talloc_tos(), *expire_time
),
186 if (renew_till_time
) {
187 DEBUGADD(10,("\trenewable till: %s (%d)\n",
188 http_timestring(talloc_tos(), *renew_till_time
),
189 (int)*renew_till_time
));
192 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
193 * in that case fallback to NTLM - gd */
195 if (expire_time
&& renew_till_time
&&
196 (*expire_time
== 0) && (*renew_till_time
== 0)) {
197 return NT_STATUS_INVALID_LOGON_TYPE
;
200 ret
= ads_krb5_cli_get_ticket(mem_ctx
,
208 impersonate_princ_s
);
210 DEBUG(1,("failed to get ticket for %s: %s\n",
211 local_service
, error_message(ret
)));
212 if (impersonate_princ_s
) {
213 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
214 impersonate_princ_s
));
216 status
= krb5_to_nt_status(ret
);
220 /* wrap that up in a nice GSS-API wrapping */
221 tkt_wrapped
= spnego_gen_krb5_wrap(tmp_ctx
, tkt
, TOK_ID_KRB_AP_REQ
);
222 if (tkt_wrapped
.data
== NULL
) {
223 status
= NT_STATUS_NO_MEMORY
;
227 auth_context
= talloc_zero(tmp_ctx
, struct auth4_context
);
228 if (auth_context
== NULL
) {
229 status
= NT_STATUS_NO_MEMORY
;
232 auth_context
->generate_session_info_pac
= kerberos_fetch_pac
;
234 lp_ctx
= loadparm_init_s3(tmp_ctx
, loadparm_s3_helpers());
235 if (lp_ctx
== NULL
) {
236 status
= NT_STATUS_INVALID_SERVER_STATE
;
237 DEBUG(10, ("loadparm_init_s3 failed\n"));
241 gensec_settings
= lpcfg_gensec_settings(tmp_ctx
, lp_ctx
);
242 if (gensec_settings
== NULL
) {
243 status
= NT_STATUS_NO_MEMORY
;
244 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
248 backends
= talloc_zero_array(gensec_settings
,
249 const struct gensec_security_ops
*, 2);
250 if (backends
== NULL
) {
251 status
= NT_STATUS_NO_MEMORY
;
254 gensec_settings
->backends
= backends
;
258 backends
[idx
++] = &gensec_gse_krb5_security_ops
;
260 status
= gensec_server_start(tmp_ctx
, gensec_settings
,
261 auth_context
, &gensec_server_context
);
263 if (!NT_STATUS_IS_OK(status
)) {
264 DEBUG(1, (__location__
"Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status
)));
268 talloc_unlink(tmp_ctx
, lp_ctx
);
269 talloc_unlink(tmp_ctx
, gensec_settings
);
270 talloc_unlink(tmp_ctx
, auth_context
);
272 /* Session info is not complete, do not pass to auth log */
273 gensec_want_feature(gensec_server_context
, GENSEC_FEATURE_NO_AUTHZ_LOG
);
275 status
= gensec_start_mech_by_oid(gensec_server_context
, GENSEC_OID_KERBEROS5
);
276 if (!NT_STATUS_IS_OK(status
)) {
277 DEBUG(1, (__location__
"Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status
)));
281 /* Do a client-server update dance */
282 status
= gensec_update(gensec_server_context
, tmp_ctx
, tkt_wrapped
, &ap_rep
);
283 if (!NT_STATUS_IS_OK(status
)) {
284 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status
)));
288 /* Now return the PAC information to the callers. We ingore
289 * the session_info and instead pick out the PAC via the
290 * private_data on the auth_context */
291 status
= gensec_session_info(gensec_server_context
, tmp_ctx
, &session_info
);
292 if (!NT_STATUS_IS_OK(status
)) {
293 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
297 pac_data_ctr
= talloc_get_type_abort(gensec_server_context
->auth_context
->private_data
,
298 struct PAC_DATA_CTR
);
299 if (pac_data_ctr
== NULL
) {
300 DEBUG(1,("no PAC\n"));
301 status
= NT_STATUS_INVALID_PARAMETER
;
305 *_pac_data_ctr
= talloc_move(mem_ctx
, &pac_data_ctr
);
308 talloc_free(tmp_ctx
);
309 if (cc
!= cache_name
) {
313 data_blob_free(&tkt
);
314 data_blob_free(&ap_rep
);
315 data_blob_free(&sesskey1
);