kdc: only pass HDB_F_CANON if the client specified b->kdc_options.canonicalize
[heimdal.git] / lib / kafs / afskrb5.c
blob62db543665ef13e26b08cc74c23011c6a069ba68
1 /*
2 * Copyright (c) 1995-2003 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 "kafs_locl.h"
36 struct krb5_kafs_data {
37 krb5_context context;
38 krb5_ccache id;
39 krb5_const_realm realm;
42 enum {
43 KAFS_RXKAD_2B_KVNO = 213,
44 KAFS_RXKAD_K5_KVNO = 256
47 static int
48 v5_to_kt(krb5_creds *cred, uid_t uid, struct kafs_token *kt, int local524)
50 int kvno, ret;
52 kt->ticket = NULL;
54 /* check if des key */
55 if (cred->session.keyvalue.length != 8)
56 return EINVAL;
58 if (local524) {
59 Ticket t;
60 unsigned char *buf;
61 size_t buf_len;
62 size_t len;
64 kvno = KAFS_RXKAD_2B_KVNO;
66 ret = decode_Ticket(cred->ticket.data, cred->ticket.length, &t, &len);
67 if (ret)
68 return ret;
69 if (t.tkt_vno != 5)
70 return -1;
72 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_len, &t.enc_part,
73 &len, ret);
74 free_Ticket(&t);
75 if (ret)
76 return ret;
77 if(buf_len != len) {
78 free(buf);
79 return KRB5KRB_ERR_GENERIC;
82 kt->ticket = buf;
83 kt->ticket_len = buf_len;
85 } else {
86 kvno = KAFS_RXKAD_K5_KVNO;
87 kt->ticket = malloc(cred->ticket.length);
88 if (kt->ticket == NULL)
89 return ENOMEM;
90 kt->ticket_len = cred->ticket.length;
91 memcpy(kt->ticket, cred->ticket.data, kt->ticket_len);
93 ret = 0;
98 * Build a struct ClearToken
101 kt->ct.AuthHandle = kvno;
102 memcpy(kt->ct.HandShakeKey, cred->session.keyvalue.data, 8);
103 kt->ct.ViceId = uid;
104 kt->ct.BeginTimestamp = cred->times.starttime;
105 kt->ct.EndTimestamp = cred->times.endtime;
107 _kafs_fixup_viceid(&kt->ct, uid);
109 return 0;
112 static krb5_error_code
113 v5_convert(krb5_context context, krb5_ccache id,
114 krb5_creds *cred, uid_t uid,
115 const char *cell,
116 struct kafs_token *kt)
118 krb5_error_code ret;
119 char *c, *val;
121 c = strdup(cell);
122 if (c == NULL)
123 return ENOMEM;
124 _kafs_foldup(c, c);
125 krb5_appdefault_string (context, "libkafs",
127 "afs-use-524", "2b", &val);
128 free(c);
130 if (strcasecmp(val, "local") == 0 ||
131 strcasecmp(val, "2b") == 0)
132 ret = v5_to_kt(cred, uid, kt, 1);
133 else
134 ret = v5_to_kt(cred, uid, kt, 0);
136 free(val);
137 return ret;
145 static int
146 get_cred(struct kafs_data *data, const char *name, const char *inst,
147 const char *realm, uid_t uid, struct kafs_token *kt)
149 krb5_error_code ret;
150 krb5_creds in_creds, *out_creds;
151 struct krb5_kafs_data *d = data->data;
152 int invalid;
154 memset(&in_creds, 0, sizeof(in_creds));
156 ret = krb5_make_principal(d->context, &in_creds.server,
157 realm, name, inst, NULL);
158 if(ret)
159 return ret;
160 ret = krb5_cc_get_principal(d->context, d->id, &in_creds.client);
161 if(ret){
162 krb5_free_principal(d->context, in_creds.server);
163 return ret;
166 in_creds.session.keytype = ETYPE_DES_CBC_CRC;
168 /* check if des is disable, and in that case enable it for afs */
169 invalid = krb5_enctype_valid(d->context, in_creds.session.keytype);
170 if (invalid)
171 krb5_enctype_enable(d->context, in_creds.session.keytype);
173 ret = krb5_get_credentials(d->context, 0, d->id, &in_creds, &out_creds);
175 if (invalid)
176 krb5_enctype_disable(d->context, in_creds.session.keytype);
178 krb5_free_principal(d->context, in_creds.server);
179 krb5_free_principal(d->context, in_creds.client);
180 if(ret)
181 return ret;
183 ret = v5_convert(d->context, d->id, out_creds, uid,
184 (inst != NULL && inst[0] != '\0') ? inst : realm, kt);
185 krb5_free_creds(d->context, out_creds);
187 return ret;
190 static const char *
191 get_error(struct kafs_data *data, int error)
193 struct krb5_kafs_data *d = data->data;
194 return krb5_get_error_message(d->context, error);
197 static void
198 free_error(struct kafs_data *data, const char *str)
200 struct krb5_kafs_data *d = data->data;
201 krb5_free_error_message(d->context, str);
204 static krb5_error_code
205 afslog_uid_int(struct kafs_data *data, const char *cell, const char *rh,
206 uid_t uid, const char *homedir)
208 krb5_error_code ret;
209 struct kafs_token kt;
210 krb5_principal princ;
211 const char *trealm; /* ticket realm */
212 struct krb5_kafs_data *d = data->data;
214 if (cell == 0 || cell[0] == 0)
215 return _kafs_afslog_all_local_cells (data, uid, homedir);
217 ret = krb5_cc_get_principal (d->context, d->id, &princ);
218 if (ret)
219 return ret;
221 trealm = krb5_principal_get_realm (d->context, princ);
223 kt.ticket = NULL;
224 ret = _kafs_get_cred(data, cell, d->realm, trealm, uid, &kt);
225 krb5_free_principal (d->context, princ);
227 if(ret == 0) {
228 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len);
229 free(kt.ticket);
231 return ret;
234 static char *
235 get_realm(struct kafs_data *data, const char *host)
237 struct krb5_kafs_data *d = data->data;
238 krb5_realm *realms;
239 char *r;
240 if(krb5_get_host_realm(d->context, host, &realms))
241 return NULL;
242 r = strdup(realms[0]);
243 krb5_free_host_realm(d->context, realms);
244 return r;
247 krb5_error_code
248 krb5_afslog_uid_home(krb5_context context,
249 krb5_ccache id,
250 const char *cell,
251 krb5_const_realm realm,
252 uid_t uid,
253 const char *homedir)
255 struct kafs_data kd;
256 struct krb5_kafs_data d;
257 krb5_error_code ret;
259 kd.name = "krb5";
260 kd.afslog_uid = afslog_uid_int;
261 kd.get_cred = get_cred;
262 kd.get_realm = get_realm;
263 kd.get_error = get_error;
264 kd.free_error = free_error;
265 kd.data = &d;
266 if (context == NULL) {
267 ret = krb5_init_context(&d.context);
268 if (ret)
269 return ret;
270 } else
271 d.context = context;
272 if (id == NULL) {
273 ret = krb5_cc_default(d.context, &d.id);
274 if (ret)
275 goto out;
276 } else
277 d.id = id;
278 d.realm = realm;
279 ret = afslog_uid_int(&kd, cell, 0, uid, homedir);
280 if (id == NULL)
281 krb5_cc_close(context, d.id);
282 out:
283 if (context == NULL)
284 krb5_free_context(d.context);
285 return ret;
288 krb5_error_code
289 krb5_afslog_uid(krb5_context context,
290 krb5_ccache id,
291 const char *cell,
292 krb5_const_realm realm,
293 uid_t uid)
295 return krb5_afslog_uid_home (context, id, cell, realm, uid, NULL);
298 krb5_error_code
299 krb5_afslog(krb5_context context,
300 krb5_ccache id,
301 const char *cell,
302 krb5_const_realm realm)
304 return krb5_afslog_uid (context, id, cell, realm, getuid());
307 krb5_error_code
308 krb5_afslog_home(krb5_context context,
309 krb5_ccache id,
310 const char *cell,
311 krb5_const_realm realm,
312 const char *homedir)
314 return krb5_afslog_uid_home (context, id, cell, realm, getuid(), homedir);
321 krb5_error_code
322 krb5_realm_of_cell(const char *cell, char **realm)
324 struct kafs_data kd;
326 kd.name = "krb5";
327 kd.get_realm = get_realm;
328 kd.get_error = get_error;
329 kd.free_error = free_error;
330 return _kafs_realm_of_cell(&kd, cell, realm);
338 kafs_settoken5(krb5_context context, const char *cell, uid_t uid,
339 krb5_creds *cred)
341 struct kafs_token kt;
342 int ret;
344 ret = v5_convert(context, NULL, cred, uid, cell, &kt);
345 if (ret)
346 return ret;
348 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len);
350 free(kt.ticket);
352 return ret;