xref: /minix3/crypto/external/bsd/heimdal/dist/kcm/renew.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: renew.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2005, PADL Software Pty Ltd.
5ebfedea0SLionel Sambuc  * All rights reserved.
6ebfedea0SLionel Sambuc  *
7ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
9ebfedea0SLionel Sambuc  * are met:
10ebfedea0SLionel Sambuc  *
11ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
16ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
17ebfedea0SLionel Sambuc  *
18ebfedea0SLionel Sambuc  * 3. Neither the name of PADL Software nor the names of its contributors
19ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
20ebfedea0SLionel Sambuc  *    without specific prior written permission.
21ebfedea0SLionel Sambuc  *
22ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ebfedea0SLionel Sambuc  * SUCH DAMAGE.
33ebfedea0SLionel Sambuc  */
34ebfedea0SLionel Sambuc 
35ebfedea0SLionel Sambuc #include "kcm_locl.h"
36ebfedea0SLionel Sambuc 
37*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
38ebfedea0SLionel Sambuc 
39ebfedea0SLionel Sambuc krb5_error_code
kcm_ccache_refresh(krb5_context context,kcm_ccache ccache,krb5_creds ** credp)40ebfedea0SLionel Sambuc kcm_ccache_refresh(krb5_context context,
41ebfedea0SLionel Sambuc 		   kcm_ccache ccache,
42ebfedea0SLionel Sambuc 		   krb5_creds **credp)
43ebfedea0SLionel Sambuc {
44ebfedea0SLionel Sambuc     krb5_error_code ret;
45ebfedea0SLionel Sambuc     krb5_creds in, *out;
46ebfedea0SLionel Sambuc     krb5_kdc_flags flags;
47ebfedea0SLionel Sambuc     krb5_const_realm realm;
48ebfedea0SLionel Sambuc     krb5_ccache_data ccdata;
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc     memset(&in, 0, sizeof(in));
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(ccache);
53ebfedea0SLionel Sambuc 
54ebfedea0SLionel Sambuc     if (ccache->client == NULL) {
55ebfedea0SLionel Sambuc 	/* no primary principal */
56ebfedea0SLionel Sambuc 	kcm_log(0, "Refresh credentials requested but no client principal");
57ebfedea0SLionel Sambuc 	return KRB5_CC_NOTFOUND;
58ebfedea0SLionel Sambuc     }
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_lock(&ccache->mutex);
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc     /* Fake up an internal ccache */
63ebfedea0SLionel Sambuc     kcm_internal_ccache(context, ccache, &ccdata);
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc     /* Find principal */
66ebfedea0SLionel Sambuc     in.client = ccache->client;
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc     if (ccache->server != NULL) {
69ebfedea0SLionel Sambuc 	ret = krb5_copy_principal(context, ccache->server, &in.server);
70ebfedea0SLionel Sambuc 	if (ret) {
71ebfedea0SLionel Sambuc 	    kcm_log(0, "Failed to copy service principal: %s",
72ebfedea0SLionel Sambuc 		    krb5_get_err_text(context, ret));
73ebfedea0SLionel Sambuc 	    goto out;
74ebfedea0SLionel Sambuc 	}
75ebfedea0SLionel Sambuc     } else {
76ebfedea0SLionel Sambuc 	realm = krb5_principal_get_realm(context, in.client);
77ebfedea0SLionel Sambuc 	ret = krb5_make_principal(context, &in.server, realm,
78ebfedea0SLionel Sambuc 				  KRB5_TGS_NAME, realm, NULL);
79ebfedea0SLionel Sambuc 	if (ret) {
80ebfedea0SLionel Sambuc 	    kcm_log(0, "Failed to make TGS principal for realm %s: %s",
81ebfedea0SLionel Sambuc 		    realm, krb5_get_err_text(context, ret));
82ebfedea0SLionel Sambuc 	    goto out;
83ebfedea0SLionel Sambuc 	}
84ebfedea0SLionel Sambuc     }
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc     if (ccache->tkt_life)
87ebfedea0SLionel Sambuc 	in.times.endtime = time(NULL) + ccache->tkt_life;
88ebfedea0SLionel Sambuc     if (ccache->renew_life)
89ebfedea0SLionel Sambuc 	in.times.renew_till = time(NULL) + ccache->renew_life;
90ebfedea0SLionel Sambuc 
91ebfedea0SLionel Sambuc     flags.i = 0;
92ebfedea0SLionel Sambuc     flags.b.renewable = TRUE;
93ebfedea0SLionel Sambuc     flags.b.renew = TRUE;
94ebfedea0SLionel Sambuc 
95ebfedea0SLionel Sambuc     ret = krb5_get_kdc_cred(context,
96ebfedea0SLionel Sambuc 			    &ccdata,
97ebfedea0SLionel Sambuc 			    flags,
98ebfedea0SLionel Sambuc 			    NULL,
99ebfedea0SLionel Sambuc 			    NULL,
100ebfedea0SLionel Sambuc 			    &in,
101ebfedea0SLionel Sambuc 			    &out);
102ebfedea0SLionel Sambuc     if (ret) {
103ebfedea0SLionel Sambuc 	kcm_log(0, "Failed to renew credentials for cache %s: %s",
104ebfedea0SLionel Sambuc 		ccache->name, krb5_get_err_text(context, ret));
105ebfedea0SLionel Sambuc 	goto out;
106ebfedea0SLionel Sambuc     }
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc     /* Swap them in */
109ebfedea0SLionel Sambuc     kcm_ccache_remove_creds_internal(context, ccache);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     ret = kcm_ccache_store_cred_internal(context, ccache, out, 0, credp);
112ebfedea0SLionel Sambuc     if (ret) {
113ebfedea0SLionel Sambuc 	kcm_log(0, "Failed to store credentials for cache %s: %s",
114ebfedea0SLionel Sambuc 		ccache->name, krb5_get_err_text(context, ret));
115ebfedea0SLionel Sambuc 	krb5_free_creds(context, out);
116ebfedea0SLionel Sambuc 	goto out;
117ebfedea0SLionel Sambuc     }
118ebfedea0SLionel Sambuc 
119ebfedea0SLionel Sambuc     free(out); /* but not contents */
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc out:
122ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&ccache->mutex);
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc     return ret;
125ebfedea0SLionel Sambuc }
126ebfedea0SLionel Sambuc 
127