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_LOGON_INFO *logon_info = NULL;
56 NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
58 tmp_ctx = talloc_new(mem_ctx);
60 return NT_STATUS_NO_MEMORY;
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)) {
71 talloc_set_name_const(logon_info, "struct PAC_LOGON_INFO");
73 auth_ctx->private_data = talloc_steal(auth_ctx, logon_info);
74 *session_info = talloc_zero(mem_ctx, struct auth_session_info);
76 status = NT_STATUS_NO_MEMORY;
79 status = NT_STATUS_OK;
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
93 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
98 time_t *renew_till_time,
99 const char *cache_name,
101 bool add_netbios_addr,
102 time_t renewable_time,
103 const char *impersonate_princ_s,
104 struct PAC_LOGON_INFO **_logon_info)
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;
117 struct auth4_context *auth_context;
118 struct loadparm_context *lp_ctx;
119 struct PAC_LOGON_INFO *logon_info = NULL;
121 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
122 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
126 ZERO_STRUCT(sesskey1);
128 if (!name || !pass) {
129 return NT_STATUS_INVALID_PARAMETER;
136 if (!strchr_m(name, '@')) {
137 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
142 NT_STATUS_HAVE_NO_MEMORY(auth_princ);
144 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
145 lp_netbios_name(), lp_realm());
146 NT_STATUS_HAVE_NO_MEMORY(local_service);
148 ret = kerberos_kinit_password_ext(auth_princ,
159 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
160 auth_princ, error_message(ret), ret));
161 /* status already set */
165 DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
167 DEBUGADD(10,("\tvalid until: %s (%d)\n",
168 http_timestring(talloc_tos(), *expire_time),
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));
177 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
178 * in that case fallback to NTLM - gd */
180 if (expire_time && renew_till_time &&
181 (*expire_time == 0) && (*renew_till_time == 0)) {
182 return NT_STATUS_INVALID_LOGON_TYPE;
185 ret = cli_krb5_get_ticket(mem_ctx,
193 impersonate_princ_s);
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));
201 status = krb5_to_nt_status(ret);
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;
212 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
213 if (auth_context == NULL) {
214 status = NT_STATUS_NO_MEMORY;
217 auth_context->generate_session_info_pac = kerberos_fetch_pac;
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"));
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"));
233 backends = talloc_zero_array(gensec_settings,
234 const struct gensec_security_ops *, 2);
235 if (backends == NULL) {
236 status = NT_STATUS_NO_MEMORY;
239 gensec_settings->backends = backends;
243 backends[idx++] = &gensec_gse_krb5_security_ops;
245 status = gensec_server_start(tmp_ctx, gensec_settings,
246 auth_context, &gensec_server_context);
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)));
253 talloc_unlink(tmp_ctx, lp_ctx);
254 talloc_unlink(tmp_ctx, gensec_settings);
255 talloc_unlink(tmp_ctx, auth_context);
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)));
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)));
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"));
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;
287 *_logon_info = talloc_move(mem_ctx, &logon_info);
290 talloc_free(tmp_ctx);
291 if (cc != cache_name) {
295 data_blob_free(&tkt);
296 data_blob_free(&ap_rep);
297 data_blob_free(&sesskey1);