| 1 |
/* |
| 2 |
* Copyright (c) 1995-2003 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 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. |
| 16 |
* |
| 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. |
| 20 |
* |
| 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. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "kafs_locl.h" |
| 35 |
|
| 36 |
struct krb5_kafs_data { |
| 37 |
krb5_context context; |
| 38 |
krb5_ccache id; |
| 39 |
krb5_const_realm realm; |
| 40 |
}; |
| 41 |
|
| 42 |
enum { |
| 43 |
KAFS_RXKAD_2B_KVNO = 213, |
| 44 |
KAFS_RXKAD_K5_KVNO = 256 |
| 45 |
}; |
| 46 |
|
| 47 |
static int |
| 48 |
v5_to_kt(krb5_creds *cred, uid_t uid, struct kafs_token *kt, int local524) |
| 49 |
{ |
| 50 |
int kvno, ret; |
| 51 |
|
| 52 |
kt->ticket = NULL; |
| 53 |
|
| 54 |
if (local524) { |
| 55 |
Ticket t; |
| 56 |
unsigned char *buf; |
| 57 |
size_t buf_len; |
| 58 |
size_t len; |
| 59 |
|
| 60 |
kvno = KAFS_RXKAD_2B_KVNO; |
| 61 |
|
| 62 |
ret = decode_Ticket(cred->ticket.data, cred->ticket.length, &t, &len); |
| 63 |
if (ret) |
| 64 |
return ret; |
| 65 |
if (t.tkt_vno != 5) |
| 66 |
return -1; |
| 67 |
|
| 68 |
ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_len, &t.enc_part, |
| 69 |
&len, ret); |
| 70 |
free_Ticket(&t); |
| 71 |
if (ret) |
| 72 |
return ret; |
| 73 |
if(buf_len != len) { |
| 74 |
free(buf); |
| 75 |
return KRB5KRB_ERR_GENERIC; |
| 76 |
} |
| 77 |
|
| 78 |
kt->ticket = buf; |
| 79 |
kt->ticket_len = buf_len; |
| 80 |
|
| 81 |
} else { |
| 82 |
kvno = KAFS_RXKAD_K5_KVNO; |
| 83 |
kt->ticket = malloc(cred->ticket.length); |
| 84 |
if (kt->ticket == NULL) |
| 85 |
return ENOMEM; |
| 86 |
kt->ticket_len = cred->ticket.length; |
| 87 |
memcpy(kt->ticket, cred->ticket.data, kt->ticket_len); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/* |
| 92 |
* Build a struct ClearToken |
| 93 |
*/ |
| 94 |
|
| 95 |
ret = _kafs_derive_des_key(cred->session.keytype, |
| 96 |
cred->session.keyvalue.data, |
| 97 |
cred->session.keyvalue.length, |
| 98 |
kt->ct.HandShakeKey); |
| 99 |
if (ret) { |
| 100 |
free(kt->ticket); |
| 101 |
kt->ticket = NULL; |
| 102 |
return ret; |
| 103 |
} |
| 104 |
kt->ct.AuthHandle = kvno; |
| 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 |
| 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 |
| 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 |
/* check if des is disable, and in that case enable it for afs */ |
| 169 |
invalid = krb5_enctype_valid(d->context, ETYPE_DES_CBC_CRC); |
| 170 |
if (invalid) |
| 171 |
krb5_enctype_enable(d->context, ETYPE_DES_CBC_CRC); |
| 172 |
|
| 173 |
ret = krb5_get_credentials(d->context, 0, d->id, &in_creds, &out_creds); |
| 174 |
|
| 175 |
if (invalid) |
| 176 |
krb5_enctype_disable(d->context, ETYPE_DES_CBC_CRC); |
| 177 |
|
| 178 |
krb5_free_principal(d->context, in_creds.server); |
| 179 |
krb5_free_principal(d->context, in_creds.client); |
| 180 |
if(ret) |
| 181 |
return ret; |
| 182 |
|
| 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); |
| 186 |
|
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
static const char * |
| 191 |
get_error(struct kafs_data *data, int error) |
| 192 |
{ |
| 193 |
struct krb5_kafs_data *d = data->data; |
| 194 |
return krb5_get_error_message(d->context, error); |
| 195 |
} |
| 196 |
|
| 197 |
static void |
| 198 |
free_error(struct kafs_data *data, const char *str) |
| 199 |
{ |
| 200 |
struct krb5_kafs_data *d = data->data; |
| 201 |
krb5_free_error_message(d->context, str); |
| 202 |
} |
| 203 |
|
| 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) |
| 207 |
{ |
| 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; |
| 213 |
|
| 214 |
if (cell == 0 || cell[0] == 0) |
| 215 |
return _kafs_afslog_all_local_cells (data, uid, homedir); |
| 216 |
|
| 217 |
ret = krb5_cc_get_principal (d->context, d->id, &princ); |
| 218 |
if (ret) |
| 219 |
return ret; |
| 220 |
|
| 221 |
trealm = krb5_principal_get_realm (d->context, princ); |
| 222 |
|
| 223 |
kt.ticket = NULL; |
| 224 |
ret = _kafs_get_cred(data, cell, d->realm, trealm, uid, &kt); |
| 225 |
krb5_free_principal (d->context, princ); |
| 226 |
|
| 227 |
if(ret == 0) { |
| 228 |
ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len); |
| 229 |
free(kt.ticket); |
| 230 |
} |
| 231 |
return ret; |
| 232 |
} |
| 233 |
|
| 234 |
static char * |
| 235 |
get_realm(struct kafs_data *data, const char *host) |
| 236 |
{ |
| 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; |
| 245 |
} |
| 246 |
|
| 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) |
| 254 |
{ |
| 255 |
struct kafs_data kd; |
| 256 |
struct krb5_kafs_data d; |
| 257 |
krb5_error_code ret; |
| 258 |
|
| 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; |
| 286 |
} |
| 287 |
|
| 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) |
| 294 |
{ |
| 295 |
return krb5_afslog_uid_home (context, id, cell, realm, uid, NULL); |
| 296 |
} |
| 297 |
|
| 298 |
krb5_error_code |
| 299 |
krb5_afslog(krb5_context context, |
| 300 |
krb5_ccache id, |
| 301 |
const char *cell, |
| 302 |
krb5_const_realm realm) |
| 303 |
{ |
| 304 |
return krb5_afslog_uid (context, id, cell, realm, getuid()); |
| 305 |
} |
| 306 |
|
| 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) |
| 313 |
{ |
| 314 |
return krb5_afslog_uid_home (context, id, cell, realm, getuid(), homedir); |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* |
| 319 |
*/ |
| 320 |
|
| 321 |
krb5_error_code |
| 322 |
krb5_realm_of_cell(const char *cell, char **realm) |
| 323 |
{ |
| 324 |
struct kafs_data kd; |
| 325 |
|
| 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); |
| 331 |
} |
| 332 |
|
| 333 |
/* |
| 334 |
* |
| 335 |
*/ |
| 336 |
|
| 337 |
int |
| 338 |
kafs_settoken5(krb5_context context, const char *cell, uid_t uid, |
| 339 |
krb5_creds *cred) |
| 340 |
{ |
| 341 |
struct kafs_token kt; |
| 342 |
int ret; |
| 343 |
|
| 344 |
ret = v5_convert(context, NULL, cred, uid, cell, &kt); |
| 345 |
if (ret) |
| 346 |
return ret; |
| 347 |
|
| 348 |
ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len); |
| 349 |
|
| 350 |
free(kt.ticket); |
| 351 |
|
| 352 |
return ret; |
| 353 |
} |