2 * Copyright (c) 1995-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "kafs_locl.h"
36 struct krb5_kafs_data
{
39 krb5_const_realm realm
;
43 KAFS_RXKAD_2B_KVNO
= 213,
44 KAFS_RXKAD_K5_KVNO
= 256
48 v5_to_kt(krb5_creds
*cred
, uid_t uid
, struct kafs_token
*kt
, int local524
)
54 /* check if des key */
55 if (cred
->session
.keyvalue
.length
!= 8)
64 kvno
= KAFS_RXKAD_2B_KVNO
;
66 ret
= decode_Ticket(cred
->ticket
.data
, cred
->ticket
.length
, &t
, &len
);
72 ASN1_MALLOC_ENCODE(EncryptedData
, buf
, buf_len
, &t
.enc_part
,
79 return KRB5KRB_ERR_GENERIC
;
83 kt
->ticket_len
= buf_len
;
86 kvno
= KAFS_RXKAD_K5_KVNO
;
87 kt
->ticket
= malloc(cred
->ticket
.length
);
88 if (kt
->ticket
== NULL
)
90 kt
->ticket_len
= cred
->ticket
.length
;
91 memcpy(kt
->ticket
, cred
->ticket
.data
, kt
->ticket_len
);
98 * Build a struct ClearToken
101 kt
->ct
.AuthHandle
= kvno
;
102 memcpy(kt
->ct
.HandShakeKey
, cred
->session
.keyvalue
.data
, 8);
104 kt
->ct
.BeginTimestamp
= cred
->times
.starttime
;
105 kt
->ct
.EndTimestamp
= cred
->times
.endtime
;
107 _kafs_fixup_viceid(&kt
->ct
, uid
);
112 static krb5_error_code
113 v5_convert(krb5_context context
, krb5_ccache id
,
114 krb5_creds
*cred
, uid_t uid
,
116 struct kafs_token
*kt
)
125 krb5_appdefault_string (context
, "libkafs",
127 "afs-use-524", "2b", &val
);
130 if (strcasecmp(val
, "local") == 0 ||
131 strcasecmp(val
, "2b") == 0)
132 ret
= v5_to_kt(cred
, uid
, kt
, 1);
134 ret
= v5_to_kt(cred
, uid
, kt
, 0);
146 get_cred(struct kafs_data
*data
, const char *name
, const char *inst
,
147 const char *realm
, uid_t uid
, struct kafs_token
*kt
)
150 krb5_creds in_creds
, *out_creds
;
151 struct krb5_kafs_data
*d
= data
->data
;
154 memset(&in_creds
, 0, sizeof(in_creds
));
156 ret
= krb5_make_principal(d
->context
, &in_creds
.server
,
157 realm
, name
, inst
, NULL
);
160 ret
= krb5_cc_get_principal(d
->context
, d
->id
, &in_creds
.client
);
162 krb5_free_principal(d
->context
, in_creds
.server
);
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
);
171 krb5_enctype_enable(d
->context
, in_creds
.session
.keytype
);
173 ret
= krb5_get_credentials(d
->context
, 0, d
->id
, &in_creds
, &out_creds
);
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
);
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
);
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
);
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
)
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
);
221 trealm
= krb5_principal_get_realm (d
->context
, princ
);
224 ret
= _kafs_get_cred(data
, cell
, d
->realm
, trealm
, uid
, &kt
);
225 krb5_free_principal (d
->context
, princ
);
228 ret
= kafs_settoken_rxkad(cell
, &kt
.ct
, kt
.ticket
, kt
.ticket_len
);
235 get_realm(struct kafs_data
*data
, const char *host
)
237 struct krb5_kafs_data
*d
= data
->data
;
240 if(krb5_get_host_realm(d
->context
, host
, &realms
))
242 r
= strdup(realms
[0]);
243 krb5_free_host_realm(d
->context
, realms
);
248 krb5_afslog_uid_home(krb5_context context
,
251 krb5_const_realm realm
,
256 struct krb5_kafs_data d
;
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
;
266 if (context
== NULL
) {
267 ret
= krb5_init_context(&d
.context
);
273 ret
= krb5_cc_default(d
.context
, &d
.id
);
279 ret
= afslog_uid_int(&kd
, cell
, 0, uid
, homedir
);
281 krb5_cc_close(context
, d
.id
);
284 krb5_free_context(d
.context
);
289 krb5_afslog_uid(krb5_context context
,
292 krb5_const_realm realm
,
295 return krb5_afslog_uid_home (context
, id
, cell
, realm
, uid
, NULL
);
299 krb5_afslog(krb5_context context
,
302 krb5_const_realm realm
)
304 return krb5_afslog_uid (context
, id
, cell
, realm
, getuid());
308 krb5_afslog_home(krb5_context context
,
311 krb5_const_realm realm
,
314 return krb5_afslog_uid_home (context
, id
, cell
, realm
, getuid(), homedir
);
322 krb5_realm_of_cell(const char *cell
, char **realm
)
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
,
341 struct kafs_token kt
;
344 ret
= v5_convert(context
, NULL
, cred
, uid
, cell
, &kt
);
348 ret
= kafs_settoken_rxkad(cell
, &kt
.ct
, kt
.ticket
, kt
.ticket_len
);