s3:smbd: move global smbd_msg_state to smbXsrv_connection
[Samba/gebeck_regimport.git] / source3 / libads / authdata.c
blobf4ec5b907563ac380382ad2964b5a6d6ed9afdb7
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_DATA *pac_data = NULL;
55 struct PAC_LOGON_INFO *logon_info = NULL;
56 unsigned int i;
57 NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
59 tmp_ctx = talloc_new(mem_ctx);
60 if (!tmp_ctx) {
61 return NT_STATUS_NO_MEMORY;
64 if (pac_blob) {
65 status = kerberos_decode_pac(tmp_ctx,
66 *pac_blob,
67 NULL, NULL, NULL, NULL, 0, &pac_data);
68 if (!NT_STATUS_IS_OK(status)) {
69 goto done;
72 /* get logon name and logon info */
73 for (i = 0; i < pac_data->num_buffers; i++) {
74 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
76 switch (data_buf->type) {
77 case PAC_TYPE_LOGON_INFO:
78 if (!data_buf->info) {
79 break;
81 logon_info = data_buf->info->logon_info.info;
82 break;
83 default:
84 break;
87 if (!logon_info) {
88 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
89 status = NT_STATUS_NOT_FOUND;
90 goto done;
93 talloc_set_name_const(logon_info, "struct PAC_LOGON_INFO");
95 auth_ctx->private_data = talloc_steal(auth_ctx, logon_info);
96 *session_info = talloc_zero(mem_ctx, struct auth_session_info);
97 if (!*session_info) {
98 status = NT_STATUS_NO_MEMORY;
99 goto done;
101 status = NT_STATUS_OK;
103 done:
104 TALLOC_FREE(tmp_ctx);
106 return status;
110 * Given the username/password, do a kinit, store the ticket in
111 * cache_name if specified, and return the PAC_LOGON_INFO (the
112 * structure containing the important user information such as
113 * groups).
115 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
116 const char *name,
117 const char *pass,
118 time_t time_offset,
119 time_t *expire_time,
120 time_t *renew_till_time,
121 const char *cache_name,
122 bool request_pac,
123 bool add_netbios_addr,
124 time_t renewable_time,
125 const char *impersonate_princ_s,
126 struct PAC_LOGON_INFO **_logon_info)
128 krb5_error_code ret;
129 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
130 DATA_BLOB tkt, tkt_wrapped, ap_rep, sesskey1;
131 const char *auth_princ = NULL;
132 const char *local_service = NULL;
133 const char *cc = "MEMORY:kerberos_return_pac";
134 struct auth_session_info *session_info;
135 struct gensec_security *gensec_server_context;
137 struct gensec_settings *gensec_settings;
138 size_t idx = 0;
139 struct auth4_context *auth_context;
140 struct loadparm_context *lp_ctx;
141 struct PAC_LOGON_INFO *logon_info = NULL;
143 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
144 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
146 ZERO_STRUCT(tkt);
147 ZERO_STRUCT(ap_rep);
148 ZERO_STRUCT(sesskey1);
150 if (!name || !pass) {
151 return NT_STATUS_INVALID_PARAMETER;
154 if (cache_name) {
155 cc = cache_name;
158 if (!strchr_m(name, '@')) {
159 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
160 lp_realm());
161 } else {
162 auth_princ = name;
164 NT_STATUS_HAVE_NO_MEMORY(auth_princ);
166 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
167 lp_netbios_name(), lp_realm());
168 NT_STATUS_HAVE_NO_MEMORY(local_service);
170 ret = kerberos_kinit_password_ext(auth_princ,
171 pass,
172 time_offset,
173 expire_time,
174 renew_till_time,
176 request_pac,
177 add_netbios_addr,
178 renewable_time,
179 &status);
180 if (ret) {
181 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
182 auth_princ, error_message(ret), ret));
183 /* status already set */
184 goto out;
187 DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
188 if (expire_time) {
189 DEBUGADD(10,("\tvalid until: %s (%d)\n",
190 http_timestring(talloc_tos(), *expire_time),
191 (int)*expire_time));
193 if (renew_till_time) {
194 DEBUGADD(10,("\trenewable till: %s (%d)\n",
195 http_timestring(talloc_tos(), *renew_till_time),
196 (int)*renew_till_time));
199 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
200 * in that case fallback to NTLM - gd */
202 if (expire_time && renew_till_time &&
203 (*expire_time == 0) && (*renew_till_time == 0)) {
204 return NT_STATUS_INVALID_LOGON_TYPE;
207 ret = cli_krb5_get_ticket(mem_ctx,
208 local_service,
209 time_offset,
210 &tkt,
211 &sesskey1,
214 NULL,
215 impersonate_princ_s);
216 if (ret) {
217 DEBUG(1,("failed to get ticket for %s: %s\n",
218 local_service, error_message(ret)));
219 if (impersonate_princ_s) {
220 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
221 impersonate_princ_s));
223 status = krb5_to_nt_status(ret);
224 goto out;
227 /* wrap that up in a nice GSS-API wrapping */
228 tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
229 if (tkt_wrapped.data == NULL) {
230 status = NT_STATUS_NO_MEMORY;
231 goto out;
234 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
235 if (auth_context == NULL) {
236 status = NT_STATUS_NO_MEMORY;
237 goto out;
239 auth_context->generate_session_info_pac = kerberos_fetch_pac;
241 lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_context());
242 if (lp_ctx == NULL) {
243 status = NT_STATUS_INVALID_SERVER_STATE;
244 DEBUG(10, ("loadparm_init_s3 failed\n"));
245 goto out;
248 gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
249 if (lp_ctx == NULL) {
250 status = NT_STATUS_NO_MEMORY;
251 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
252 goto out;
255 gensec_settings->backends = talloc_zero_array(gensec_settings,
256 struct gensec_security_ops *, 2);
257 if (gensec_settings->backends == NULL) {
258 status = NT_STATUS_NO_MEMORY;
259 goto out;
262 gensec_init();
264 gensec_settings->backends[idx++] = &gensec_gse_krb5_security_ops;
266 status = gensec_server_start(tmp_ctx, gensec_settings,
267 auth_context, &gensec_server_context);
269 if (!NT_STATUS_IS_OK(status)) {
270 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
271 goto out;
274 talloc_unlink(tmp_ctx, lp_ctx);
275 talloc_unlink(tmp_ctx, gensec_settings);
276 talloc_unlink(tmp_ctx, auth_context);
278 status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
279 if (!NT_STATUS_IS_OK(status)) {
280 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
281 goto out;
284 /* Do a client-server update dance */
285 status = gensec_update(gensec_server_context, tmp_ctx, NULL, tkt_wrapped, &ap_rep);
286 if (!NT_STATUS_IS_OK(status)) {
287 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
288 goto out;
291 /* Now return the PAC information to the callers. We ingore
292 * the session_info and instead pick out the PAC via the
293 * private_data on the auth_context */
294 status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
295 if (!NT_STATUS_IS_OK(status)) {
296 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
297 goto out;
300 logon_info = talloc_get_type_abort(gensec_server_context->auth_context->private_data,
301 struct PAC_LOGON_INFO);
302 if (logon_info == NULL) {
303 DEBUG(1,("no PAC\n"));
304 status = NT_STATUS_INVALID_PARAMETER;
305 goto out;
308 *_logon_info = talloc_move(mem_ctx, &logon_info);
310 out:
311 talloc_free(tmp_ctx);
312 if (cc != cache_name) {
313 ads_kdestroy(cc);
316 data_blob_free(&tkt);
317 data_blob_free(&ap_rep);
318 data_blob_free(&sesskey1);
320 return status;
323 #endif