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"
38 struct krb5_kafs_data
{
41 krb5_const_realm realm
;
45 KAFS_RXKAD_2B_KVNO
= 213,
46 KAFS_RXKAD_K5_KVNO
= 256
50 v5_to_kt(krb5_creds
*cred
, uid_t uid
, struct kafs_token
*kt
, int local524
)
56 /* check if des key */
57 if (cred
->session
.keyvalue
.length
!= 8)
66 kvno
= KAFS_RXKAD_2B_KVNO
;
68 ret
= decode_Ticket(cred
->ticket
.data
, cred
->ticket
.length
, &t
, &len
);
74 ASN1_MALLOC_ENCODE(EncryptedData
, buf
, buf_len
, &t
.enc_part
,
81 return KRB5KRB_ERR_GENERIC
;
85 kt
->ticket_len
= buf_len
;
88 kvno
= KAFS_RXKAD_K5_KVNO
;
89 kt
->ticket
= malloc(cred
->ticket
.length
);
90 if (kt
->ticket
== NULL
)
92 kt
->ticket_len
= cred
->ticket
.length
;
93 memcpy(kt
->ticket
, cred
->ticket
.data
, kt
->ticket_len
);
100 * Build a struct ClearToken
103 kt
->ct
.AuthHandle
= kvno
;
104 memcpy(kt
->ct
.HandShakeKey
, cred
->session
.keyvalue
.data
, 8);
106 kt
->ct
.BeginTimestamp
= cred
->times
.starttime
;
107 kt
->ct
.EndTimestamp
= cred
->times
.endtime
;
109 _kafs_fixup_viceid(&kt
->ct
, uid
);
114 static krb5_error_code
115 v5_convert(krb5_context context
, krb5_ccache id
,
116 krb5_creds
*cred
, uid_t uid
,
118 struct kafs_token
*kt
)
127 krb5_appdefault_string (context
, "libkafs",
129 "afs-use-524", "2b", &val
);
132 if (strcasecmp(val
, "local") == 0 ||
133 strcasecmp(val
, "2b") == 0)
134 ret
= v5_to_kt(cred
, uid
, kt
, 1);
135 else if(strcasecmp(val
, "yes") == 0 ||
136 strcasecmp(val
, "true") == 0 ||
138 struct credentials cred4
;
141 ret
= krb524_convert_creds_kdc(context
, cred
, &cred4
);
143 ret
= krb524_convert_creds_kdc_ccache(context
, id
, cred
, &cred4
);
147 ret
= _kafs_v4_to_kt(&cred4
, uid
, kt
);
149 ret
= v5_to_kt(cred
, uid
, kt
, 0);
162 get_cred(struct kafs_data
*data
, const char *name
, const char *inst
,
163 const char *realm
, uid_t uid
, struct kafs_token
*kt
)
166 krb5_creds in_creds
, *out_creds
;
167 struct krb5_kafs_data
*d
= data
->data
;
169 memset(&in_creds
, 0, sizeof(in_creds
));
170 ret
= krb5_425_conv_principal(d
->context
, name
, inst
, realm
,
174 ret
= krb5_cc_get_principal(d
->context
, d
->id
, &in_creds
.client
);
176 krb5_free_principal(d
->context
, in_creds
.server
);
179 in_creds
.session
.keytype
= ETYPE_DES_CBC_CRC
;
180 ret
= krb5_get_credentials(d
->context
, 0, d
->id
, &in_creds
, &out_creds
);
181 krb5_free_principal(d
->context
, in_creds
.server
);
182 krb5_free_principal(d
->context
, in_creds
.client
);
186 ret
= v5_convert(d
->context
, d
->id
, out_creds
, uid
,
187 (inst
!= NULL
&& inst
[0] != '\0') ? inst
: realm
, kt
);
188 krb5_free_creds(d
->context
, out_creds
);
193 static krb5_error_code
194 afslog_uid_int(struct kafs_data
*data
, const char *cell
, const char *rh
,
195 uid_t uid
, const char *homedir
)
198 struct kafs_token kt
;
199 krb5_principal princ
;
200 const char *trealm
; /* ticket realm */
201 struct krb5_kafs_data
*d
= data
->data
;
203 if (cell
== 0 || cell
[0] == 0)
204 return _kafs_afslog_all_local_cells (data
, uid
, homedir
);
206 ret
= krb5_cc_get_principal (d
->context
, d
->id
, &princ
);
210 trealm
= krb5_principal_get_realm (d
->context
, princ
);
213 ret
= _kafs_get_cred(data
, cell
, d
->realm
, trealm
, uid
, &kt
);
214 krb5_free_principal (d
->context
, princ
);
217 ret
= kafs_settoken_rxkad(cell
, &kt
.ct
, kt
.ticket
, kt
.ticket_len
);
224 get_realm(struct kafs_data
*data
, const char *host
)
226 struct krb5_kafs_data
*d
= data
->data
;
229 if(krb5_get_host_realm(d
->context
, host
, &realms
))
231 r
= strdup(realms
[0]);
232 krb5_free_host_realm(d
->context
, realms
);
237 krb5_afslog_uid_home(krb5_context context
,
240 krb5_const_realm realm
,
245 struct krb5_kafs_data d
;
249 kd
.afslog_uid
= afslog_uid_int
;
250 kd
.get_cred
= get_cred
;
251 kd
.get_realm
= get_realm
;
253 if (context
== NULL
) {
254 ret
= krb5_init_context(&d
.context
);
260 ret
= krb5_cc_default(d
.context
, &d
.id
);
266 ret
= afslog_uid_int(&kd
, cell
, 0, uid
, homedir
);
268 krb5_cc_close(context
, d
.id
);
271 krb5_free_context(d
.context
);
276 krb5_afslog_uid(krb5_context context
,
279 krb5_const_realm realm
,
282 return krb5_afslog_uid_home (context
, id
, cell
, realm
, uid
, NULL
);
286 krb5_afslog(krb5_context context
,
289 krb5_const_realm realm
)
291 return krb5_afslog_uid (context
, id
, cell
, realm
, getuid());
295 krb5_afslog_home(krb5_context context
,
298 krb5_const_realm realm
,
301 return krb5_afslog_uid_home (context
, id
, cell
, realm
, getuid(), homedir
);
309 krb5_realm_of_cell(const char *cell
, char **realm
)
314 kd
.get_realm
= get_realm
;
315 return _kafs_realm_of_cell(&kd
, cell
, realm
);
323 kafs_settoken5(krb5_context context
, const char *cell
, uid_t uid
,
326 struct kafs_token kt
;
329 ret
= v5_convert(context
, NULL
, cred
, uid
, cell
, &kt
);
333 ret
= kafs_settoken_rxkad(cell
, &kt
.ct
, kt
.ticket
, kt
.ticket_len
);