1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1995-2003 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include "kafs_locl.h"
35b528cefcSMark Murray
36b528cefcSMark Murray struct krb5_kafs_data {
37b528cefcSMark Murray krb5_context context;
38b528cefcSMark Murray krb5_ccache id;
39b528cefcSMark Murray krb5_const_realm realm;
40b528cefcSMark Murray };
41b528cefcSMark Murray
42bbd80c28SJacques Vidrine enum {
43bbd80c28SJacques Vidrine KAFS_RXKAD_2B_KVNO = 213,
44bbd80c28SJacques Vidrine KAFS_RXKAD_K5_KVNO = 256
45bbd80c28SJacques Vidrine };
46bbd80c28SJacques Vidrine
47bbd80c28SJacques Vidrine static int
v5_to_kt(krb5_creds * cred,uid_t uid,struct kafs_token * kt,int local524)48bbd80c28SJacques Vidrine v5_to_kt(krb5_creds *cred, uid_t uid, struct kafs_token *kt, int local524)
49bbd80c28SJacques Vidrine {
50bbd80c28SJacques Vidrine int kvno, ret;
51bbd80c28SJacques Vidrine
52bbd80c28SJacques Vidrine kt->ticket = NULL;
53bbd80c28SJacques Vidrine
54bbd80c28SJacques Vidrine /* check if des key */
55bbd80c28SJacques Vidrine if (cred->session.keyvalue.length != 8)
56bbd80c28SJacques Vidrine return EINVAL;
57bbd80c28SJacques Vidrine
58bbd80c28SJacques Vidrine if (local524) {
59bbd80c28SJacques Vidrine Ticket t;
60bbd80c28SJacques Vidrine unsigned char *buf;
61bbd80c28SJacques Vidrine size_t buf_len;
62bbd80c28SJacques Vidrine size_t len;
63bbd80c28SJacques Vidrine
64bbd80c28SJacques Vidrine kvno = KAFS_RXKAD_2B_KVNO;
65bbd80c28SJacques Vidrine
66bbd80c28SJacques Vidrine ret = decode_Ticket(cred->ticket.data, cred->ticket.length, &t, &len);
67bbd80c28SJacques Vidrine if (ret)
68bbd80c28SJacques Vidrine return ret;
69bbd80c28SJacques Vidrine if (t.tkt_vno != 5)
70bbd80c28SJacques Vidrine return -1;
71bbd80c28SJacques Vidrine
72bbd80c28SJacques Vidrine ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_len, &t.enc_part,
73bbd80c28SJacques Vidrine &len, ret);
74bbd80c28SJacques Vidrine free_Ticket(&t);
75bbd80c28SJacques Vidrine if (ret)
76bbd80c28SJacques Vidrine return ret;
77bbd80c28SJacques Vidrine if(buf_len != len) {
78bbd80c28SJacques Vidrine free(buf);
79bbd80c28SJacques Vidrine return KRB5KRB_ERR_GENERIC;
80bbd80c28SJacques Vidrine }
81bbd80c28SJacques Vidrine
82bbd80c28SJacques Vidrine kt->ticket = buf;
83bbd80c28SJacques Vidrine kt->ticket_len = buf_len;
84bbd80c28SJacques Vidrine
85bbd80c28SJacques Vidrine } else {
86bbd80c28SJacques Vidrine kvno = KAFS_RXKAD_K5_KVNO;
87bbd80c28SJacques Vidrine kt->ticket = malloc(cred->ticket.length);
88bbd80c28SJacques Vidrine if (kt->ticket == NULL)
89bbd80c28SJacques Vidrine return ENOMEM;
90bbd80c28SJacques Vidrine kt->ticket_len = cred->ticket.length;
91bbd80c28SJacques Vidrine memcpy(kt->ticket, cred->ticket.data, kt->ticket_len);
92bbd80c28SJacques Vidrine }
93bbd80c28SJacques Vidrine
94bbd80c28SJacques Vidrine
95bbd80c28SJacques Vidrine /*
96bbd80c28SJacques Vidrine * Build a struct ClearToken
97bbd80c28SJacques Vidrine */
98bbd80c28SJacques Vidrine
99bbd80c28SJacques Vidrine kt->ct.AuthHandle = kvno;
100bbd80c28SJacques Vidrine memcpy(kt->ct.HandShakeKey, cred->session.keyvalue.data, 8);
101bbd80c28SJacques Vidrine kt->ct.ViceId = uid;
102bbd80c28SJacques Vidrine kt->ct.BeginTimestamp = cred->times.starttime;
103bbd80c28SJacques Vidrine kt->ct.EndTimestamp = cred->times.endtime;
104bbd80c28SJacques Vidrine
105bbd80c28SJacques Vidrine _kafs_fixup_viceid(&kt->ct, uid);
106bbd80c28SJacques Vidrine
107bbd80c28SJacques Vidrine return 0;
108bbd80c28SJacques Vidrine }
109bbd80c28SJacques Vidrine
110bbd80c28SJacques Vidrine static krb5_error_code
v5_convert(krb5_context context,krb5_ccache id,krb5_creds * cred,uid_t uid,const char * cell,struct kafs_token * kt)111bbd80c28SJacques Vidrine v5_convert(krb5_context context, krb5_ccache id,
112bbd80c28SJacques Vidrine krb5_creds *cred, uid_t uid,
113bbd80c28SJacques Vidrine const char *cell,
114bbd80c28SJacques Vidrine struct kafs_token *kt)
115bbd80c28SJacques Vidrine {
116bbd80c28SJacques Vidrine krb5_error_code ret;
117bbd80c28SJacques Vidrine char *c, *val;
118bbd80c28SJacques Vidrine
119bbd80c28SJacques Vidrine c = strdup(cell);
120bbd80c28SJacques Vidrine if (c == NULL)
121bbd80c28SJacques Vidrine return ENOMEM;
122bbd80c28SJacques Vidrine _kafs_foldup(c, c);
123bbd80c28SJacques Vidrine krb5_appdefault_string (context, "libkafs",
124bbd80c28SJacques Vidrine c,
125c19800e8SDoug Rabson "afs-use-524", "2b", &val);
126bbd80c28SJacques Vidrine free(c);
127bbd80c28SJacques Vidrine
128bbd80c28SJacques Vidrine if (strcasecmp(val, "local") == 0 ||
129bbd80c28SJacques Vidrine strcasecmp(val, "2b") == 0)
130bbd80c28SJacques Vidrine ret = v5_to_kt(cred, uid, kt, 1);
131bbd80c28SJacques Vidrine else
132bbd80c28SJacques Vidrine ret = v5_to_kt(cred, uid, kt, 0);
133bbd80c28SJacques Vidrine
134bbd80c28SJacques Vidrine free(val);
135bbd80c28SJacques Vidrine return ret;
136bbd80c28SJacques Vidrine }
137bbd80c28SJacques Vidrine
138bbd80c28SJacques Vidrine
139bbd80c28SJacques Vidrine /*
140bbd80c28SJacques Vidrine *
141bbd80c28SJacques Vidrine */
142bbd80c28SJacques Vidrine
143b528cefcSMark Murray static int
get_cred(struct kafs_data * data,const char * name,const char * inst,const char * realm,uid_t uid,struct kafs_token * kt)144c19800e8SDoug Rabson get_cred(struct kafs_data *data, const char *name, const char *inst,
145bbd80c28SJacques Vidrine const char *realm, uid_t uid, struct kafs_token *kt)
146b528cefcSMark Murray {
147b528cefcSMark Murray krb5_error_code ret;
148b528cefcSMark Murray krb5_creds in_creds, *out_creds;
149b528cefcSMark Murray struct krb5_kafs_data *d = data->data;
150*ae771770SStanislav Sedov int invalid;
151b528cefcSMark Murray
152b528cefcSMark Murray memset(&in_creds, 0, sizeof(in_creds));
153*ae771770SStanislav Sedov
154*ae771770SStanislav Sedov ret = krb5_make_principal(d->context, &in_creds.server,
155*ae771770SStanislav Sedov realm, name, inst, NULL);
156b528cefcSMark Murray if(ret)
157b528cefcSMark Murray return ret;
158b528cefcSMark Murray ret = krb5_cc_get_principal(d->context, d->id, &in_creds.client);
159b528cefcSMark Murray if(ret){
160b528cefcSMark Murray krb5_free_principal(d->context, in_creds.server);
161b528cefcSMark Murray return ret;
162b528cefcSMark Murray }
163*ae771770SStanislav Sedov
164c19800e8SDoug Rabson in_creds.session.keytype = ETYPE_DES_CBC_CRC;
165*ae771770SStanislav Sedov
166*ae771770SStanislav Sedov /* check if des is disable, and in that case enable it for afs */
167*ae771770SStanislav Sedov invalid = krb5_enctype_valid(d->context, in_creds.session.keytype);
168*ae771770SStanislav Sedov if (invalid)
169*ae771770SStanislav Sedov krb5_enctype_enable(d->context, in_creds.session.keytype);
170*ae771770SStanislav Sedov
171b528cefcSMark Murray ret = krb5_get_credentials(d->context, 0, d->id, &in_creds, &out_creds);
172*ae771770SStanislav Sedov if (ret) {
173*ae771770SStanislav Sedov in_creds.session.keytype = ETYPE_DES_CBC_MD5;
174*ae771770SStanislav Sedov ret = krb5_get_credentials(d->context, 0, d->id, &in_creds, &out_creds);
175*ae771770SStanislav Sedov }
176*ae771770SStanislav Sedov
177*ae771770SStanislav Sedov if (invalid)
178*ae771770SStanislav Sedov krb5_enctype_disable(d->context, in_creds.session.keytype);
179*ae771770SStanislav Sedov
180b528cefcSMark Murray krb5_free_principal(d->context, in_creds.server);
181b528cefcSMark Murray krb5_free_principal(d->context, in_creds.client);
182b528cefcSMark Murray if(ret)
183b528cefcSMark Murray return ret;
184bbd80c28SJacques Vidrine
185bbd80c28SJacques Vidrine ret = v5_convert(d->context, d->id, out_creds, uid,
186bbd80c28SJacques Vidrine (inst != NULL && inst[0] != '\0') ? inst : realm, kt);
187b528cefcSMark Murray krb5_free_creds(d->context, out_creds);
188bbd80c28SJacques Vidrine
189b528cefcSMark Murray return ret;
190b528cefcSMark Murray }
191b528cefcSMark Murray
192*ae771770SStanislav Sedov static const char *
get_error(struct kafs_data * data,int error)193*ae771770SStanislav Sedov get_error(struct kafs_data *data, int error)
194*ae771770SStanislav Sedov {
195*ae771770SStanislav Sedov struct krb5_kafs_data *d = data->data;
196*ae771770SStanislav Sedov return krb5_get_error_message(d->context, error);
197*ae771770SStanislav Sedov }
198*ae771770SStanislav Sedov
199*ae771770SStanislav Sedov static void
free_error(struct kafs_data * data,const char * str)200*ae771770SStanislav Sedov free_error(struct kafs_data *data, const char *str)
201*ae771770SStanislav Sedov {
202*ae771770SStanislav Sedov struct krb5_kafs_data *d = data->data;
203*ae771770SStanislav Sedov krb5_free_error_message(d->context, str);
204*ae771770SStanislav Sedov }
205*ae771770SStanislav Sedov
206b528cefcSMark Murray static krb5_error_code
afslog_uid_int(struct kafs_data * data,const char * cell,const char * rh,uid_t uid,const char * homedir)207c19800e8SDoug Rabson afslog_uid_int(struct kafs_data *data, const char *cell, const char *rh,
208c19800e8SDoug Rabson uid_t uid, const char *homedir)
209b528cefcSMark Murray {
210b528cefcSMark Murray krb5_error_code ret;
211bbd80c28SJacques Vidrine struct kafs_token kt;
212b528cefcSMark Murray krb5_principal princ;
213c19800e8SDoug Rabson const char *trealm; /* ticket realm */
214b528cefcSMark Murray struct krb5_kafs_data *d = data->data;
215b528cefcSMark Murray
216b528cefcSMark Murray if (cell == 0 || cell[0] == 0)
217b528cefcSMark Murray return _kafs_afslog_all_local_cells (data, uid, homedir);
218b528cefcSMark Murray
219b528cefcSMark Murray ret = krb5_cc_get_principal (d->context, d->id, &princ);
220b528cefcSMark Murray if (ret)
221b528cefcSMark Murray return ret;
222b528cefcSMark Murray
223c19800e8SDoug Rabson trealm = krb5_principal_get_realm (d->context, princ);
224b528cefcSMark Murray
225bbd80c28SJacques Vidrine kt.ticket = NULL;
226c19800e8SDoug Rabson ret = _kafs_get_cred(data, cell, d->realm, trealm, uid, &kt);
227b528cefcSMark Murray krb5_free_principal (d->context, princ);
228b528cefcSMark Murray
229bbd80c28SJacques Vidrine if(ret == 0) {
230bbd80c28SJacques Vidrine ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len);
231bbd80c28SJacques Vidrine free(kt.ticket);
232bbd80c28SJacques Vidrine }
233b528cefcSMark Murray return ret;
234b528cefcSMark Murray }
235b528cefcSMark Murray
236b528cefcSMark Murray static char *
get_realm(struct kafs_data * data,const char * host)237c19800e8SDoug Rabson get_realm(struct kafs_data *data, const char *host)
238b528cefcSMark Murray {
239b528cefcSMark Murray struct krb5_kafs_data *d = data->data;
240b528cefcSMark Murray krb5_realm *realms;
241b528cefcSMark Murray char *r;
242b528cefcSMark Murray if(krb5_get_host_realm(d->context, host, &realms))
243b528cefcSMark Murray return NULL;
244b528cefcSMark Murray r = strdup(realms[0]);
245b528cefcSMark Murray krb5_free_host_realm(d->context, realms);
246b528cefcSMark Murray return r;
247b528cefcSMark Murray }
248b528cefcSMark Murray
249b528cefcSMark Murray krb5_error_code
krb5_afslog_uid_home(krb5_context context,krb5_ccache id,const char * cell,krb5_const_realm realm,uid_t uid,const char * homedir)250b528cefcSMark Murray krb5_afslog_uid_home(krb5_context context,
251b528cefcSMark Murray krb5_ccache id,
252b528cefcSMark Murray const char *cell,
253b528cefcSMark Murray krb5_const_realm realm,
254b528cefcSMark Murray uid_t uid,
255b528cefcSMark Murray const char *homedir)
256b528cefcSMark Murray {
257c19800e8SDoug Rabson struct kafs_data kd;
258b528cefcSMark Murray struct krb5_kafs_data d;
259c19800e8SDoug Rabson krb5_error_code ret;
260c19800e8SDoug Rabson
261bbd80c28SJacques Vidrine kd.name = "krb5";
262b528cefcSMark Murray kd.afslog_uid = afslog_uid_int;
263b528cefcSMark Murray kd.get_cred = get_cred;
264b528cefcSMark Murray kd.get_realm = get_realm;
265*ae771770SStanislav Sedov kd.get_error = get_error;
266*ae771770SStanislav Sedov kd.free_error = free_error;
267b528cefcSMark Murray kd.data = &d;
268c19800e8SDoug Rabson if (context == NULL) {
269c19800e8SDoug Rabson ret = krb5_init_context(&d.context);
270c19800e8SDoug Rabson if (ret)
271c19800e8SDoug Rabson return ret;
272c19800e8SDoug Rabson } else
273b528cefcSMark Murray d.context = context;
274c19800e8SDoug Rabson if (id == NULL) {
275c19800e8SDoug Rabson ret = krb5_cc_default(d.context, &d.id);
276c19800e8SDoug Rabson if (ret)
277c19800e8SDoug Rabson goto out;
278c19800e8SDoug Rabson } else
279b528cefcSMark Murray d.id = id;
280b528cefcSMark Murray d.realm = realm;
281c19800e8SDoug Rabson ret = afslog_uid_int(&kd, cell, 0, uid, homedir);
282c19800e8SDoug Rabson if (id == NULL)
283c19800e8SDoug Rabson krb5_cc_close(context, d.id);
284c19800e8SDoug Rabson out:
285c19800e8SDoug Rabson if (context == NULL)
286c19800e8SDoug Rabson krb5_free_context(d.context);
287c19800e8SDoug Rabson return ret;
288b528cefcSMark Murray }
289b528cefcSMark Murray
290b528cefcSMark Murray krb5_error_code
krb5_afslog_uid(krb5_context context,krb5_ccache id,const char * cell,krb5_const_realm realm,uid_t uid)291b528cefcSMark Murray krb5_afslog_uid(krb5_context context,
292b528cefcSMark Murray krb5_ccache id,
293b528cefcSMark Murray const char *cell,
294b528cefcSMark Murray krb5_const_realm realm,
295b528cefcSMark Murray uid_t uid)
296b528cefcSMark Murray {
297b528cefcSMark Murray return krb5_afslog_uid_home (context, id, cell, realm, uid, NULL);
298b528cefcSMark Murray }
299b528cefcSMark Murray
300b528cefcSMark Murray krb5_error_code
krb5_afslog(krb5_context context,krb5_ccache id,const char * cell,krb5_const_realm realm)301b528cefcSMark Murray krb5_afslog(krb5_context context,
302b528cefcSMark Murray krb5_ccache id,
303b528cefcSMark Murray const char *cell,
304b528cefcSMark Murray krb5_const_realm realm)
305b528cefcSMark Murray {
306b528cefcSMark Murray return krb5_afslog_uid (context, id, cell, realm, getuid());
307b528cefcSMark Murray }
308b528cefcSMark Murray
309b528cefcSMark Murray krb5_error_code
krb5_afslog_home(krb5_context context,krb5_ccache id,const char * cell,krb5_const_realm realm,const char * homedir)310b528cefcSMark Murray krb5_afslog_home(krb5_context context,
311b528cefcSMark Murray krb5_ccache id,
312b528cefcSMark Murray const char *cell,
313b528cefcSMark Murray krb5_const_realm realm,
314b528cefcSMark Murray const char *homedir)
315b528cefcSMark Murray {
316b528cefcSMark Murray return krb5_afslog_uid_home (context, id, cell, realm, getuid(), homedir);
317b528cefcSMark Murray }
318b528cefcSMark Murray
319b528cefcSMark Murray /*
320b528cefcSMark Murray *
321b528cefcSMark Murray */
322b528cefcSMark Murray
323b528cefcSMark Murray krb5_error_code
krb5_realm_of_cell(const char * cell,char ** realm)324b528cefcSMark Murray krb5_realm_of_cell(const char *cell, char **realm)
325b528cefcSMark Murray {
326c19800e8SDoug Rabson struct kafs_data kd;
327b528cefcSMark Murray
328bbd80c28SJacques Vidrine kd.name = "krb5";
329b528cefcSMark Murray kd.get_realm = get_realm;
330*ae771770SStanislav Sedov kd.get_error = get_error;
331*ae771770SStanislav Sedov kd.free_error = free_error;
332b528cefcSMark Murray return _kafs_realm_of_cell(&kd, cell, realm);
333b528cefcSMark Murray }
334bbd80c28SJacques Vidrine
335bbd80c28SJacques Vidrine /*
336bbd80c28SJacques Vidrine *
337bbd80c28SJacques Vidrine */
338bbd80c28SJacques Vidrine
339bbd80c28SJacques Vidrine int
kafs_settoken5(krb5_context context,const char * cell,uid_t uid,krb5_creds * cred)340bbd80c28SJacques Vidrine kafs_settoken5(krb5_context context, const char *cell, uid_t uid,
341bbd80c28SJacques Vidrine krb5_creds *cred)
342bbd80c28SJacques Vidrine {
343bbd80c28SJacques Vidrine struct kafs_token kt;
344bbd80c28SJacques Vidrine int ret;
345bbd80c28SJacques Vidrine
346bbd80c28SJacques Vidrine ret = v5_convert(context, NULL, cred, uid, cell, &kt);
347bbd80c28SJacques Vidrine if (ret)
348bbd80c28SJacques Vidrine return ret;
349bbd80c28SJacques Vidrine
350bbd80c28SJacques Vidrine ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len);
351bbd80c28SJacques Vidrine
352bbd80c28SJacques Vidrine free(kt.ticket);
353bbd80c28SJacques Vidrine
354bbd80c28SJacques Vidrine return ret;
355bbd80c28SJacques Vidrine }
356