No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / kafs / afskrb5.c
blob7fee08d7599ef1e63b19a34b1b78837544d03eac
1 /*
2 * Copyright (c) 1995-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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 "kafs_locl.h"
36 __RCSID("$Heimdal: afskrb5.c 17032 2006-04-10 08:45:04Z lha $"
37 "$NetBSD$");
39 struct krb5_kafs_data {
40 krb5_context context;
41 krb5_ccache id;
42 krb5_const_realm realm;
45 enum {
46 KAFS_RXKAD_2B_KVNO = 213,
47 KAFS_RXKAD_K5_KVNO = 256
50 static int
51 v5_to_kt(krb5_creds *cred, uid_t uid, struct kafs_token *kt, int local524)
53 int kvno, ret;
55 kt->ticket = NULL;
57 /* check if des key */
58 if (cred->session.keyvalue.length != 8)
59 return EINVAL;
61 if (local524) {
62 Ticket t;
63 unsigned char *buf;
64 size_t buf_len;
65 size_t len;
67 kvno = KAFS_RXKAD_2B_KVNO;
69 ret = decode_Ticket(cred->ticket.data, cred->ticket.length, &t, &len);
70 if (ret)
71 return ret;
72 if (t.tkt_vno != 5)
73 return -1;
75 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_len, &t.enc_part,
76 &len, ret);
77 free_Ticket(&t);
78 if (ret)
79 return ret;
80 if(buf_len != len) {
81 free(buf);
82 return KRB5KRB_ERR_GENERIC;
85 kt->ticket = buf;
86 kt->ticket_len = buf_len;
88 } else {
89 kvno = KAFS_RXKAD_K5_KVNO;
90 kt->ticket = malloc(cred->ticket.length);
91 if (kt->ticket == NULL)
92 return ENOMEM;
93 kt->ticket_len = cred->ticket.length;
94 memcpy(kt->ticket, cred->ticket.data, kt->ticket_len);
96 ret = 0;
101 * Build a struct ClearToken
104 kt->ct.AuthHandle = kvno;
105 memcpy(kt->ct.HandShakeKey, cred->session.keyvalue.data, 8);
106 kt->ct.ViceId = uid;
107 kt->ct.BeginTimestamp = cred->times.starttime;
108 kt->ct.EndTimestamp = cred->times.endtime;
110 _kafs_fixup_viceid(&kt->ct, uid);
112 return 0;
115 static krb5_error_code
116 v5_convert(krb5_context context, krb5_ccache id,
117 krb5_creds *cred, uid_t uid,
118 const char *cell,
119 struct kafs_token *kt)
121 krb5_error_code ret;
122 char *c, *val;
124 c = strdup(cell);
125 if (c == NULL)
126 return ENOMEM;
127 _kafs_foldup(c, c);
128 krb5_appdefault_string (context, "libkafs",
130 "afs-use-524", "2b", &val);
131 free(c);
133 if (strcasecmp(val, "local") == 0 ||
134 strcasecmp(val, "2b") == 0)
135 ret = v5_to_kt(cred, uid, kt, 1);
136 else if(strcasecmp(val, "yes") == 0 ||
137 strcasecmp(val, "true") == 0 ||
138 atoi(val)) {
139 struct credentials cred4;
141 if (id == NULL)
142 ret = krb524_convert_creds_kdc(context, cred, &cred4);
143 else
144 ret = krb524_convert_creds_kdc_ccache(context, id, cred, &cred4);
145 if (ret)
146 goto out;
148 ret = _kafs_v4_to_kt(&cred4, uid, kt);
149 } else
150 ret = v5_to_kt(cred, uid, kt, 0);
152 out:
153 free(val);
154 return ret;
162 static int
163 get_cred(struct kafs_data *data, const char *name, const char *inst,
164 const char *realm, uid_t uid, struct kafs_token *kt)
166 krb5_error_code ret;
167 krb5_creds in_creds, *out_creds;
168 struct krb5_kafs_data *d = data->data;
170 memset(&in_creds, 0, sizeof(in_creds));
171 ret = krb5_425_conv_principal(d->context, name, inst, realm,
172 &in_creds.server);
173 if(ret)
174 return ret;
175 ret = krb5_cc_get_principal(d->context, d->id, &in_creds.client);
176 if(ret){
177 krb5_free_principal(d->context, in_creds.server);
178 return ret;
180 in_creds.session.keytype = ETYPE_DES_CBC_CRC;
181 ret = krb5_get_credentials(d->context, 0, d->id, &in_creds, &out_creds);
182 krb5_free_principal(d->context, in_creds.server);
183 krb5_free_principal(d->context, in_creds.client);
184 if(ret)
185 return ret;
187 ret = v5_convert(d->context, d->id, out_creds, uid,
188 (inst != NULL && inst[0] != '\0') ? inst : realm, kt);
189 krb5_free_creds(d->context, out_creds);
191 return ret;
194 static krb5_error_code
195 afslog_uid_int(struct kafs_data *data, const char *cell, const char *rh,
196 uid_t uid, const char *homedir)
198 krb5_error_code ret;
199 struct kafs_token kt;
200 krb5_principal princ;
201 const char *trealm; /* ticket realm */
202 struct krb5_kafs_data *d = data->data;
204 if (cell == 0 || cell[0] == 0)
205 return _kafs_afslog_all_local_cells (data, uid, homedir);
207 ret = krb5_cc_get_principal (d->context, d->id, &princ);
208 if (ret)
209 return ret;
211 trealm = krb5_principal_get_realm (d->context, princ);
213 kt.ticket = NULL;
214 ret = _kafs_get_cred(data, cell, d->realm, trealm, uid, &kt);
215 krb5_free_principal (d->context, princ);
217 if(ret == 0) {
218 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len);
219 free(kt.ticket);
221 return ret;
224 static char *
225 get_realm(struct kafs_data *data, const char *host)
227 struct krb5_kafs_data *d = data->data;
228 krb5_realm *realms;
229 char *r;
230 if(krb5_get_host_realm(d->context, host, &realms))
231 return NULL;
232 r = strdup(realms[0]);
233 krb5_free_host_realm(d->context, realms);
234 return r;
237 krb5_error_code
238 krb5_afslog_uid_home(krb5_context context,
239 krb5_ccache id,
240 const char *cell,
241 krb5_const_realm realm,
242 uid_t uid,
243 const char *homedir)
245 struct kafs_data kd;
246 struct krb5_kafs_data d;
247 krb5_error_code ret;
249 kd.name = "krb5";
250 kd.afslog_uid = afslog_uid_int;
251 kd.get_cred = get_cred;
252 kd.get_realm = get_realm;
253 kd.data = &d;
254 if (context == NULL) {
255 ret = krb5_init_context(&d.context);
256 if (ret)
257 return ret;
258 } else
259 d.context = context;
260 if (id == NULL) {
261 ret = krb5_cc_default(d.context, &d.id);
262 if (ret)
263 goto out;
264 } else
265 d.id = id;
266 d.realm = realm;
267 ret = afslog_uid_int(&kd, cell, 0, uid, homedir);
268 if (id == NULL)
269 krb5_cc_close(context, d.id);
270 out:
271 if (context == NULL)
272 krb5_free_context(d.context);
273 return ret;
276 krb5_error_code
277 krb5_afslog_uid(krb5_context context,
278 krb5_ccache id,
279 const char *cell,
280 krb5_const_realm realm,
281 uid_t uid)
283 return krb5_afslog_uid_home (context, id, cell, realm, uid, NULL);
286 krb5_error_code
287 krb5_afslog(krb5_context context,
288 krb5_ccache id,
289 const char *cell,
290 krb5_const_realm realm)
292 return krb5_afslog_uid (context, id, cell, realm, getuid());
295 krb5_error_code
296 krb5_afslog_home(krb5_context context,
297 krb5_ccache id,
298 const char *cell,
299 krb5_const_realm realm,
300 const char *homedir)
302 return krb5_afslog_uid_home (context, id, cell, realm, getuid(), homedir);
309 krb5_error_code
310 krb5_realm_of_cell(const char *cell, char **realm)
312 struct kafs_data kd;
314 kd.name = "krb5";
315 kd.get_realm = get_realm;
316 return _kafs_realm_of_cell(&kd, cell, realm);
324 kafs_settoken5(krb5_context context, const char *cell, uid_t uid,
325 krb5_creds *cred)
327 struct kafs_token kt;
328 int ret;
330 ret = v5_convert(context, NULL, cred, uid, cell, &kt);
331 if (ret)
332 return ret;
334 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len);
336 free(kt.ticket);
338 return ret;