Sync usage with man page.
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / convert_creds.c
blob30662078cc1a5bf30c7971a101aed09314350cbd
1 /*
2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
35 __RCSID("$Heimdal: convert_creds.c 22050 2007-11-11 11:20:46Z lha $"
36 "$NetBSD$");
38 #include "krb5-v4compat.h"
40 static krb5_error_code
41 check_ticket_flags(TicketFlags f)
43 return 0; /* maybe add some more tests here? */
46 /**
47 * Convert the v5 credentials in in_cred to v4-dito in v4creds. This
48 * is done by sending them to the 524 function in the KDC. If
49 * `in_cred' doesn't contain a DES session key, then a new one is
50 * gotten from the KDC and stored in the cred cache `ccache'.
52 * @param context Kerberos 5 context.
53 * @param in_cred the credential to convert
54 * @param v4creds the converted credential
56 * @return Returns 0 to indicate success. Otherwise an kerberos et
57 * error code is returned, see krb5_get_error_message().
59 * @ingroup krb5_v4compat
62 krb5_error_code KRB5_LIB_FUNCTION
63 krb524_convert_creds_kdc(krb5_context context,
64 krb5_creds *in_cred,
65 struct credentials *v4creds)
67 krb5_error_code ret;
68 krb5_data reply;
69 krb5_storage *sp;
70 int32_t tmp;
71 krb5_data ticket;
72 char realm[REALM_SZ];
73 krb5_creds *v5_creds = in_cred;
75 ret = check_ticket_flags(v5_creds->flags.b);
76 if(ret)
77 goto out2;
80 krb5_krbhst_handle handle;
82 ret = krb5_krbhst_init(context,
83 krb5_principal_get_realm(context,
84 v5_creds->server),
85 KRB5_KRBHST_KRB524,
86 &handle);
87 if (ret)
88 goto out2;
90 ret = krb5_sendto (context,
91 &v5_creds->ticket,
92 handle,
93 &reply);
94 krb5_krbhst_free(context, handle);
95 if (ret)
96 goto out2;
98 sp = krb5_storage_from_mem(reply.data, reply.length);
99 if(sp == NULL) {
100 ret = ENOMEM;
101 krb5_set_error_string (context, "malloc: out of memory");
102 goto out2;
104 krb5_ret_int32(sp, &tmp);
105 ret = tmp;
106 if(ret == 0) {
107 memset(v4creds, 0, sizeof(*v4creds));
108 ret = krb5_ret_int32(sp, &tmp);
109 if(ret)
110 goto out;
111 v4creds->kvno = tmp;
112 ret = krb5_ret_data(sp, &ticket);
113 if(ret)
114 goto out;
115 v4creds->ticket_st.length = ticket.length;
116 memcpy(v4creds->ticket_st.dat, ticket.data, ticket.length);
117 krb5_data_free(&ticket);
118 ret = krb5_524_conv_principal(context,
119 v5_creds->server,
120 v4creds->service,
121 v4creds->instance,
122 v4creds->realm);
123 if(ret)
124 goto out;
125 v4creds->issue_date = v5_creds->times.starttime;
126 v4creds->lifetime = _krb5_krb_time_to_life(v4creds->issue_date,
127 v5_creds->times.endtime);
128 ret = krb5_524_conv_principal(context, v5_creds->client,
129 v4creds->pname,
130 v4creds->pinst,
131 realm);
132 if(ret)
133 goto out;
134 memcpy(v4creds->session, v5_creds->session.keyvalue.data, 8);
135 } else {
136 krb5_set_error_string(context, "converting credentials: %s",
137 krb5_get_err_text(context, ret));
139 out:
140 krb5_storage_free(sp);
141 krb5_data_free(&reply);
142 out2:
143 if (v5_creds != in_cred)
144 krb5_free_creds (context, v5_creds);
145 return ret;
149 * Convert the v5 credentials in in_cred to v4-dito in v4creds,
150 * check the credential cache ccache before checking with the KDC.
152 * @param context Kerberos 5 context.
153 * @param ccache credential cache used to check for des-ticket.
154 * @param in_cred the credential to convert
155 * @param v4creds the converted credential
157 * @return Returns 0 to indicate success. Otherwise an kerberos et
158 * error code is returned, see krb5_get_error_message().
160 * @ingroup krb5_v4compat
163 krb5_error_code KRB5_LIB_FUNCTION
164 krb524_convert_creds_kdc_ccache(krb5_context context,
165 krb5_ccache ccache,
166 krb5_creds *in_cred,
167 struct credentials *v4creds)
169 krb5_error_code ret;
170 krb5_creds *v5_creds = in_cred;
171 krb5_keytype keytype;
173 keytype = v5_creds->session.keytype;
175 if (keytype != ENCTYPE_DES_CBC_CRC) {
176 /* MIT krb524d doesn't like nothing but des-cbc-crc tickets,
177 so go get one */
178 krb5_creds template;
180 memset (&template, 0, sizeof(template));
181 template.session.keytype = ENCTYPE_DES_CBC_CRC;
182 ret = krb5_copy_principal (context, in_cred->client, &template.client);
183 if (ret) {
184 krb5_free_cred_contents (context, &template);
185 return ret;
187 ret = krb5_copy_principal (context, in_cred->server, &template.server);
188 if (ret) {
189 krb5_free_cred_contents (context, &template);
190 return ret;
193 ret = krb5_get_credentials (context, 0, ccache,
194 &template, &v5_creds);
195 krb5_free_cred_contents (context, &template);
196 if (ret)
197 return ret;
200 ret = krb524_convert_creds_kdc(context, v5_creds, v4creds);
202 if (v5_creds != in_cred)
203 krb5_free_creds (context, v5_creds);
204 return ret;