smbXsrv_session: Remove a "can't happen" NULL check
[Samba.git] / source3 / libads / authdata.c
blob10adc3ee8a9000eee5fef6ae34abc9d0652e3a2a
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 "auth/gensec/gensec_internal.h" /* TODO: remove this */
34 #include "../libcli/auth/spnego.h"
35 #include "lib/util/asn1.h"
37 #ifdef HAVE_KRB5
39 #include "auth/kerberos/pac_utils.h"
41 struct smb_krb5_context;
44 generate a krb5 GSS-API wrapper packet given a ticket
46 static DATA_BLOB spnego_gen_krb5_wrap(
47 TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8_t tok_id[2])
49 ASN1_DATA *data;
50 DATA_BLOB ret = data_blob_null;
52 data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH);
53 if (data == NULL) {
54 return data_blob_null;
57 if (!asn1_push_tag(data, ASN1_APPLICATION(0))) goto err;
58 if (!asn1_write_OID(data, OID_KERBEROS5)) goto err;
60 if (!asn1_write(data, tok_id, 2)) goto err;
61 if (!asn1_write(data, ticket.data, ticket.length)) goto err;
62 if (!asn1_pop_tag(data)) goto err;
64 if (!asn1_extract_blob(data, ctx, &ret)) {
65 goto err;
68 asn1_free(data);
69 data = NULL;
71 err:
73 if (data != NULL) {
74 if (asn1_has_error(data)) {
75 DEBUG(1, ("Failed to build krb5 wrapper at offset %d\n",
76 (int)asn1_current_ofs(data)));
79 asn1_free(data);
82 return ret;
86 * Given the username/password, do a kinit, store the ticket in
87 * cache_name if specified, and return the PAC_LOGON_INFO (the
88 * structure containing the important user information such as
89 * groups).
91 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
92 const char *name,
93 const char *pass,
94 time_t time_offset,
95 time_t *expire_time,
96 time_t *renew_till_time,
97 const char *cache_name,
98 bool request_pac,
99 bool add_netbios_addr,
100 time_t renewable_time,
101 const char *impersonate_princ_s,
102 const char *local_service,
103 char **_canon_principal,
104 char **_canon_realm,
105 struct PAC_DATA_CTR **_pac_data_ctr)
107 krb5_error_code ret;
108 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
109 DATA_BLOB tkt = data_blob_null;
110 DATA_BLOB tkt_wrapped = data_blob_null;
111 DATA_BLOB ap_rep = data_blob_null;
112 DATA_BLOB sesskey1 = data_blob_null;
113 const char *auth_princ = NULL;
114 const char *cc = "MEMORY:kerberos_return_pac";
115 struct auth_session_info *session_info;
116 struct gensec_security *gensec_server_context;
117 const struct gensec_security_ops **backends;
118 struct gensec_settings *gensec_settings;
119 size_t idx = 0;
120 struct auth4_context *auth_context;
121 struct loadparm_context *lp_ctx;
122 struct PAC_DATA_CTR *pac_data_ctr = NULL;
123 char *canon_principal = NULL;
124 char *canon_realm = NULL;
126 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
127 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
129 ZERO_STRUCT(tkt);
130 ZERO_STRUCT(ap_rep);
131 ZERO_STRUCT(sesskey1);
133 if (!name || !pass) {
134 status = NT_STATUS_INVALID_PARAMETER;
135 goto out;
138 if (_canon_principal != NULL) {
139 *_canon_principal = NULL;
142 if (_canon_realm != NULL) {
143 *_canon_realm = NULL;
146 if (cache_name) {
147 cc = cache_name;
150 if (!strchr_m(name, '@')) {
151 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
152 lp_realm());
153 } else {
154 auth_princ = name;
156 NT_STATUS_HAVE_NO_MEMORY(auth_princ);
158 ret = kerberos_kinit_password_ext(auth_princ,
159 pass,
160 time_offset,
161 expire_time,
162 renew_till_time,
164 request_pac,
165 add_netbios_addr,
166 renewable_time,
167 tmp_ctx,
168 &canon_principal,
169 &canon_realm,
170 &status);
171 if (ret) {
172 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
173 auth_princ, error_message(ret), ret));
174 /* status already set */
175 goto out;
178 DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
179 if (expire_time) {
180 DEBUGADD(10,("\tvalid until: %s (%d)\n",
181 http_timestring(talloc_tos(), *expire_time),
182 (int)*expire_time));
184 if (renew_till_time) {
185 DEBUGADD(10,("\trenewable till: %s (%d)\n",
186 http_timestring(talloc_tos(), *renew_till_time),
187 (int)*renew_till_time));
190 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
191 * in that case fallback to NTLM - gd */
193 if (expire_time && renew_till_time &&
194 (*expire_time == 0) && (*renew_till_time == 0)) {
195 status = NT_STATUS_INVALID_LOGON_TYPE;
196 goto out;
199 ret = ads_krb5_cli_get_ticket(mem_ctx,
200 local_service,
201 time_offset,
202 &tkt,
203 &sesskey1,
206 NULL,
207 impersonate_princ_s);
208 if (ret) {
209 DEBUG(1,("failed to get ticket for %s: %s\n",
210 local_service, error_message(ret)));
211 if (impersonate_princ_s) {
212 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
213 impersonate_princ_s));
215 status = krb5_to_nt_status(ret);
216 goto out;
219 /* wrap that up in a nice GSS-API wrapping */
220 tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
221 if (tkt_wrapped.data == NULL) {
222 status = NT_STATUS_NO_MEMORY;
223 goto out;
226 auth_context = auth4_context_for_PAC_DATA_CTR(tmp_ctx);
227 if (auth_context == NULL) {
228 status = NT_STATUS_NO_MEMORY;
229 goto out;
232 lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
233 if (lp_ctx == NULL) {
234 status = NT_STATUS_INVALID_SERVER_STATE;
235 DEBUG(10, ("loadparm_init_s3 failed\n"));
236 goto out;
239 gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
240 if (gensec_settings == NULL) {
241 status = NT_STATUS_NO_MEMORY;
242 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
243 goto out;
246 backends = talloc_zero_array(gensec_settings,
247 const struct gensec_security_ops *, 2);
248 if (backends == NULL) {
249 status = NT_STATUS_NO_MEMORY;
250 goto out;
252 gensec_settings->backends = backends;
254 gensec_init();
256 backends[idx++] = &gensec_gse_krb5_security_ops;
258 status = gensec_server_start(tmp_ctx, gensec_settings,
259 auth_context, &gensec_server_context);
261 if (!NT_STATUS_IS_OK(status)) {
262 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
263 goto out;
266 talloc_unlink(tmp_ctx, lp_ctx);
267 talloc_unlink(tmp_ctx, gensec_settings);
268 talloc_unlink(tmp_ctx, auth_context);
270 /* Session info is not complete, do not pass to auth log */
271 gensec_want_feature(gensec_server_context, GENSEC_FEATURE_NO_AUTHZ_LOG);
273 status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
274 if (!NT_STATUS_IS_OK(status)) {
275 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
276 goto out;
279 /* Do a client-server update dance */
280 status = gensec_update(gensec_server_context, tmp_ctx, tkt_wrapped, &ap_rep);
281 if (!NT_STATUS_IS_OK(status)) {
282 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
283 goto out;
286 /* Now return the PAC information to the callers. We ignore
287 * the session_info and instead pick out the PAC via the
288 * private_data on the auth_context */
289 status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
290 if (!NT_STATUS_IS_OK(status)) {
291 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
292 goto out;
295 pac_data_ctr = auth4_context_get_PAC_DATA_CTR(auth_context, mem_ctx);
296 if (pac_data_ctr == NULL) {
297 DEBUG(1,("no PAC\n"));
298 status = NT_STATUS_INVALID_PARAMETER;
299 goto out;
302 *_pac_data_ctr = talloc_move(mem_ctx, &pac_data_ctr);
303 if (_canon_principal != NULL) {
304 *_canon_principal = talloc_move(mem_ctx, &canon_principal);
306 if (_canon_realm != NULL) {
307 *_canon_realm = talloc_move(mem_ctx, &canon_realm);
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