s3:smbd: s/EVENT_FD/TEVENT_FD
[Samba/vl.git] / source3 / libads / authdata.c
blob2c667a66bcd3dcea68c0dda7991ddb4cdf768313
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
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/>.
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 "../libcli/auth/spnego.h"
35 #ifdef HAVE_KRB5
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,
45 TALLOC_CTX *mem_ctx,
46 struct smb_krb5_context *smb_krb5_context,
47 DATA_BLOB *pac_blob,
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)
53 TALLOC_CTX *tmp_ctx;
54 struct PAC_LOGON_INFO *logon_info = NULL;
55 NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
57 tmp_ctx = talloc_new(mem_ctx);
58 if (!tmp_ctx) {
59 return NT_STATUS_NO_MEMORY;
62 if (pac_blob) {
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)) {
66 goto done;
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);
74 if (!*session_info) {
75 status = NT_STATUS_NO_MEMORY;
76 goto done;
78 status = NT_STATUS_OK;
80 done:
81 TALLOC_FREE(tmp_ctx);
83 return status;
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
90 * groups).
92 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
93 const char *name,
94 const char *pass,
95 time_t time_offset,
96 time_t *expire_time,
97 time_t *renew_till_time,
98 const char *cache_name,
99 bool request_pac,
100 bool add_netbios_addr,
101 time_t renewable_time,
102 const char *impersonate_princ_s,
103 struct PAC_LOGON_INFO **_logon_info)
105 krb5_error_code ret;
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;
115 size_t idx = 0;
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);
123 ZERO_STRUCT(tkt);
124 ZERO_STRUCT(ap_rep);
125 ZERO_STRUCT(sesskey1);
127 if (!name || !pass) {
128 return NT_STATUS_INVALID_PARAMETER;
131 if (cache_name) {
132 cc = cache_name;
135 if (!strchr_m(name, '@')) {
136 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
137 lp_realm());
138 } else {
139 auth_princ = 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,
148 pass,
149 time_offset,
150 expire_time,
151 renew_till_time,
153 request_pac,
154 add_netbios_addr,
155 renewable_time,
156 &status);
157 if (ret) {
158 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
159 auth_princ, error_message(ret), ret));
160 /* status already set */
161 goto out;
164 DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
165 if (expire_time) {
166 DEBUGADD(10,("\tvalid until: %s (%d)\n",
167 http_timestring(talloc_tos(), *expire_time),
168 (int)*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,
185 local_service,
186 time_offset,
187 &tkt,
188 &sesskey1,
191 NULL,
192 impersonate_princ_s);
193 if (ret) {
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);
201 goto out;
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;
208 goto out;
211 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
212 if (auth_context == NULL) {
213 status = NT_STATUS_NO_MEMORY;
214 goto out;
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"));
222 goto out;
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"));
229 goto out;
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;
236 goto out;
239 gensec_init();
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)));
248 goto out;
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)));
258 goto out;
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)));
265 goto out;
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"));
274 goto out;
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;
282 goto out;
285 *_logon_info = talloc_move(mem_ctx, &logon_info);
287 out:
288 talloc_free(tmp_ctx);
289 if (cc != cache_name) {
290 ads_kdestroy(cc);
293 data_blob_free(&tkt);
294 data_blob_free(&ap_rep);
295 data_blob_free(&sesskey1);
297 return status;
300 #endif