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