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 "../libcli/auth/spnego.h"
37 #include "auth/kerberos/pac_utils.h"
39 struct smb_krb5_context
;
41 /****************************************************************
42 Callback to get the PAC_LOGON_INFO from the blob
43 ****************************************************************/
44 static NTSTATUS
kerberos_fetch_pac(struct auth4_context
*auth_ctx
,
46 struct smb_krb5_context
*smb_krb5_context
,
48 const char *princ_name
,
49 const struct tsocket_address
*remote_address
,
50 uint32_t session_info_flags
,
51 struct auth_session_info
**session_info
)
54 struct PAC_LOGON_INFO
*logon_info
= NULL
;
55 NTSTATUS status
= NT_STATUS_INTERNAL_ERROR
;
57 tmp_ctx
= talloc_new(mem_ctx
);
59 return NT_STATUS_NO_MEMORY
;
63 status
= kerberos_pac_logon_info(tmp_ctx
, *pac_blob
, NULL
, NULL
,
64 NULL
, NULL
, 0, &logon_info
);
65 if (!NT_STATUS_IS_OK(status
)) {
70 talloc_set_name_const(logon_info
, "struct PAC_LOGON_INFO");
72 auth_ctx
->private_data
= talloc_steal(auth_ctx
, logon_info
);
73 *session_info
= talloc_zero(mem_ctx
, struct auth_session_info
);
75 status
= NT_STATUS_NO_MEMORY
;
78 status
= NT_STATUS_OK
;
87 * Given the username/password, do a kinit, store the ticket in
88 * cache_name if specified, and return the PAC_LOGON_INFO (the
89 * structure containing the important user information such as
92 NTSTATUS
kerberos_return_pac(TALLOC_CTX
*mem_ctx
,
97 time_t *renew_till_time
,
98 const char *cache_name
,
100 bool add_netbios_addr
,
101 time_t renewable_time
,
102 const char *impersonate_princ_s
,
103 struct PAC_LOGON_INFO
**_logon_info
)
106 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
107 DATA_BLOB tkt
, tkt_wrapped
, ap_rep
, sesskey1
;
108 const char *auth_princ
= NULL
;
109 const char *local_service
= NULL
;
110 const char *cc
= "MEMORY:kerberos_return_pac";
111 struct auth_session_info
*session_info
;
112 struct gensec_security
*gensec_server_context
;
114 struct gensec_settings
*gensec_settings
;
116 struct auth4_context
*auth_context
;
117 struct loadparm_context
*lp_ctx
;
118 struct PAC_LOGON_INFO
*logon_info
= NULL
;
120 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
121 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
125 ZERO_STRUCT(sesskey1
);
127 if (!name
|| !pass
) {
128 return NT_STATUS_INVALID_PARAMETER
;
135 if (!strchr_m(name
, '@')) {
136 auth_princ
= talloc_asprintf(mem_ctx
, "%s@%s", name
,
141 NT_STATUS_HAVE_NO_MEMORY(auth_princ
);
143 local_service
= talloc_asprintf(mem_ctx
, "%s$@%s",
144 lp_netbios_name(), lp_realm());
145 NT_STATUS_HAVE_NO_MEMORY(local_service
);
147 ret
= kerberos_kinit_password_ext(auth_princ
,
158 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
159 auth_princ
, error_message(ret
), ret
));
160 /* status already set */
164 DEBUG(10,("got TGT for %s in %s\n", auth_princ
, cc
));
166 DEBUGADD(10,("\tvalid until: %s (%d)\n",
167 http_timestring(talloc_tos(), *expire_time
),
170 if (renew_till_time
) {
171 DEBUGADD(10,("\trenewable till: %s (%d)\n",
172 http_timestring(talloc_tos(), *renew_till_time
),
173 (int)*renew_till_time
));
176 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
177 * in that case fallback to NTLM - gd */
179 if (expire_time
&& renew_till_time
&&
180 (*expire_time
== 0) && (*renew_till_time
== 0)) {
181 return NT_STATUS_INVALID_LOGON_TYPE
;
184 ret
= cli_krb5_get_ticket(mem_ctx
,
192 impersonate_princ_s
);
194 DEBUG(1,("failed to get ticket for %s: %s\n",
195 local_service
, error_message(ret
)));
196 if (impersonate_princ_s
) {
197 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
198 impersonate_princ_s
));
200 status
= krb5_to_nt_status(ret
);
204 /* wrap that up in a nice GSS-API wrapping */
205 tkt_wrapped
= spnego_gen_krb5_wrap(tmp_ctx
, tkt
, TOK_ID_KRB_AP_REQ
);
206 if (tkt_wrapped
.data
== NULL
) {
207 status
= NT_STATUS_NO_MEMORY
;
211 auth_context
= talloc_zero(tmp_ctx
, struct auth4_context
);
212 if (auth_context
== NULL
) {
213 status
= NT_STATUS_NO_MEMORY
;
216 auth_context
->generate_session_info_pac
= kerberos_fetch_pac
;
218 lp_ctx
= loadparm_init_s3(tmp_ctx
, loadparm_s3_helpers());
219 if (lp_ctx
== NULL
) {
220 status
= NT_STATUS_INVALID_SERVER_STATE
;
221 DEBUG(10, ("loadparm_init_s3 failed\n"));
225 gensec_settings
= lpcfg_gensec_settings(tmp_ctx
, lp_ctx
);
226 if (lp_ctx
== NULL
) {
227 status
= NT_STATUS_NO_MEMORY
;
228 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
232 gensec_settings
->backends
= talloc_zero_array(gensec_settings
,
233 struct gensec_security_ops
*, 2);
234 if (gensec_settings
->backends
== NULL
) {
235 status
= NT_STATUS_NO_MEMORY
;
241 gensec_settings
->backends
[idx
++] = &gensec_gse_krb5_security_ops
;
243 status
= gensec_server_start(tmp_ctx
, gensec_settings
,
244 auth_context
, &gensec_server_context
);
246 if (!NT_STATUS_IS_OK(status
)) {
247 DEBUG(1, (__location__
"Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status
)));
251 talloc_unlink(tmp_ctx
, lp_ctx
);
252 talloc_unlink(tmp_ctx
, gensec_settings
);
253 talloc_unlink(tmp_ctx
, auth_context
);
255 status
= gensec_start_mech_by_oid(gensec_server_context
, GENSEC_OID_KERBEROS5
);
256 if (!NT_STATUS_IS_OK(status
)) {
257 DEBUG(1, (__location__
"Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status
)));
261 /* Do a client-server update dance */
262 status
= gensec_update(gensec_server_context
, tmp_ctx
, NULL
, tkt_wrapped
, &ap_rep
);
263 if (!NT_STATUS_IS_OK(status
)) {
264 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status
)));
268 /* Now return the PAC information to the callers. We ingore
269 * the session_info and instead pick out the PAC via the
270 * private_data on the auth_context */
271 status
= gensec_session_info(gensec_server_context
, tmp_ctx
, &session_info
);
272 if (!NT_STATUS_IS_OK(status
)) {
273 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
277 logon_info
= talloc_get_type_abort(gensec_server_context
->auth_context
->private_data
,
278 struct PAC_LOGON_INFO
);
279 if (logon_info
== NULL
) {
280 DEBUG(1,("no PAC\n"));
281 status
= NT_STATUS_INVALID_PARAMETER
;
285 *_logon_info
= talloc_move(mem_ctx
, &logon_info
);
288 talloc_free(tmp_ctx
);
289 if (cc
!= cache_name
) {
293 data_blob_free(&tkt
);
294 data_blob_free(&ap_rep
);
295 data_blob_free(&sesskey1
);