Fix Error messages
[Samba/bjacke.git] / source3 / libads / authdata.c
blob5a8ca28b14ebae4f8564579f5ede7a8d568a1aae
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 struct smb_krb5_context;
39 /****************************************************************
40 Callback to get the PAC_LOGON_INFO from the blob
41 ****************************************************************/
42 static NTSTATUS kerberos_fetch_pac(struct auth4_context *auth_ctx,
43 TALLOC_CTX *mem_ctx,
44 struct smb_krb5_context *smb_krb5_context,
45 DATA_BLOB *pac_blob,
46 const char *princ_name,
47 const struct tsocket_address *remote_address,
48 uint32_t session_info_flags,
49 struct auth_session_info **session_info)
51 TALLOC_CTX *tmp_ctx;
52 struct PAC_DATA *pac_data = NULL;
53 struct PAC_LOGON_INFO *logon_info = NULL;
54 unsigned int i;
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_decode_pac(tmp_ctx,
64 *pac_blob,
65 NULL, NULL, NULL, NULL, 0, &pac_data);
66 if (!NT_STATUS_IS_OK(status)) {
67 goto done;
70 /* get logon name and logon info */
71 for (i = 0; i < pac_data->num_buffers; i++) {
72 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
74 switch (data_buf->type) {
75 case PAC_TYPE_LOGON_INFO:
76 if (!data_buf->info) {
77 break;
79 logon_info = data_buf->info->logon_info.info;
80 break;
81 default:
82 break;
85 if (!logon_info) {
86 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
87 status = NT_STATUS_NOT_FOUND;
88 goto done;
91 talloc_set_name_const(logon_info, "struct PAC_LOGON_INFO");
93 auth_ctx->private_data = talloc_steal(auth_ctx, logon_info);
94 *session_info = talloc_zero(mem_ctx, struct auth_session_info);
95 if (!*session_info) {
96 status = NT_STATUS_NO_MEMORY;
97 goto done;
99 status = NT_STATUS_OK;
101 done:
102 TALLOC_FREE(tmp_ctx);
104 return status;
108 * Given the username/password, do a kinit, store the ticket in
109 * cache_name if specified, and return the PAC_LOGON_INFO (the
110 * structure containing the important user information such as
111 * groups).
113 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
114 const char *name,
115 const char *pass,
116 time_t time_offset,
117 time_t *expire_time,
118 time_t *renew_till_time,
119 const char *cache_name,
120 bool request_pac,
121 bool add_netbios_addr,
122 time_t renewable_time,
123 const char *impersonate_princ_s,
124 struct PAC_LOGON_INFO **_logon_info)
126 krb5_error_code ret;
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 *local_service = NULL;
131 const char *cc = "MEMORY:kerberos_return_pac";
132 struct auth_session_info *session_info;
133 struct gensec_security *gensec_server_context;
135 struct gensec_settings *gensec_settings;
136 size_t idx = 0;
137 struct auth4_context *auth_context;
138 struct loadparm_context *lp_ctx;
139 struct PAC_LOGON_INFO *logon_info = NULL;
141 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
142 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
144 ZERO_STRUCT(tkt);
145 ZERO_STRUCT(ap_rep);
146 ZERO_STRUCT(sesskey1);
148 if (!name || !pass) {
149 return NT_STATUS_INVALID_PARAMETER;
152 if (cache_name) {
153 cc = cache_name;
156 if (!strchr_m(name, '@')) {
157 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
158 lp_realm());
159 } else {
160 auth_princ = name;
162 NT_STATUS_HAVE_NO_MEMORY(auth_princ);
164 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
165 lp_netbios_name(), lp_realm());
166 NT_STATUS_HAVE_NO_MEMORY(local_service);
168 ret = kerberos_kinit_password_ext(auth_princ,
169 pass,
170 time_offset,
171 expire_time,
172 renew_till_time,
174 request_pac,
175 add_netbios_addr,
176 renewable_time,
177 &status);
178 if (ret) {
179 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
180 auth_princ, error_message(ret), ret));
181 /* status already set */
182 goto out;
185 DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
186 if (expire_time) {
187 DEBUGADD(10,("\tvalid until: %s (%d)\n",
188 http_timestring(talloc_tos(), *expire_time),
189 (int)*expire_time));
191 if (renew_till_time) {
192 DEBUGADD(10,("\trenewable till: %s (%d)\n",
193 http_timestring(talloc_tos(), *renew_till_time),
194 (int)*renew_till_time));
197 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
198 * in that case fallback to NTLM - gd */
200 if (expire_time && renew_till_time &&
201 (*expire_time == 0) && (*renew_till_time == 0)) {
202 return NT_STATUS_INVALID_LOGON_TYPE;
205 ret = cli_krb5_get_ticket(mem_ctx,
206 local_service,
207 time_offset,
208 &tkt,
209 &sesskey1,
212 NULL,
213 impersonate_princ_s);
214 if (ret) {
215 DEBUG(1,("failed to get ticket for %s: %s\n",
216 local_service, error_message(ret)));
217 if (impersonate_princ_s) {
218 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
219 impersonate_princ_s));
221 status = krb5_to_nt_status(ret);
222 goto out;
225 /* wrap that up in a nice GSS-API wrapping */
226 tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
227 if (tkt_wrapped.data == NULL) {
228 status = NT_STATUS_NO_MEMORY;
229 goto out;
232 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
233 if (auth_context == NULL) {
234 status = NT_STATUS_NO_MEMORY;
235 goto out;
237 auth_context->generate_session_info_pac = kerberos_fetch_pac;
239 lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_context());
240 if (lp_ctx == NULL) {
241 status = NT_STATUS_INVALID_SERVER_STATE;
242 DEBUG(10, ("loadparm_init_s3 failed\n"));
243 goto out;
246 gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
247 if (lp_ctx == NULL) {
248 status = NT_STATUS_NO_MEMORY;
249 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
250 goto out;
253 gensec_settings->backends = talloc_zero_array(gensec_settings,
254 struct gensec_security_ops *, 2);
255 if (gensec_settings->backends == NULL) {
256 status = NT_STATUS_NO_MEMORY;
257 goto out;
260 gensec_init();
262 gensec_settings->backends[idx++] = &gensec_gse_krb5_security_ops;
264 status = gensec_server_start(tmp_ctx, gensec_settings,
265 auth_context, &gensec_server_context);
267 if (!NT_STATUS_IS_OK(status)) {
268 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
269 goto out;
272 talloc_unlink(tmp_ctx, lp_ctx);
273 talloc_unlink(tmp_ctx, gensec_settings);
274 talloc_unlink(tmp_ctx, auth_context);
276 status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
277 if (!NT_STATUS_IS_OK(status)) {
278 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
279 goto out;
282 /* Do a client-server update dance */
283 status = gensec_update(gensec_server_context, tmp_ctx, NULL, tkt_wrapped, &ap_rep);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
286 goto out;
289 /* Now return the PAC information to the callers. We ingore
290 * the session_info and instead pick out the PAC via the
291 * private_data on the auth_context */
292 status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
293 if (!NT_STATUS_IS_OK(status)) {
294 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
295 goto out;
298 logon_info = talloc_get_type_abort(gensec_server_context->auth_context->private_data,
299 struct PAC_LOGON_INFO);
300 if (logon_info == NULL) {
301 DEBUG(1,("no PAC\n"));
302 status = NT_STATUS_INVALID_PARAMETER;
303 goto out;
306 *_logon_info = talloc_move(mem_ctx, &logon_info);
308 out:
309 talloc_free(tmp_ctx);
310 if (cc != cache_name) {
311 ads_kdestroy(cc);
314 data_blob_free(&tkt);
315 data_blob_free(&ap_rep);
316 data_blob_free(&sesskey1);
318 return status;
321 #endif