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 /* check if des key */ 55 if (cred->session.keyvalue.length != 8) 56 return EINVAL; 57 58 if (local524) { 59 Ticket t; 60 unsigned char *buf; 61 size_t buf_len; 62 size_t len; 63 64 kvno = KAFS_RXKAD_2B_KVNO; 65 66 ret = decode_Ticket(cred->ticket.data, cred->ticket.length, &t, &len); 67 if (ret) 68 return ret; 69 if (t.tkt_vno != 5) 70 return -1; 71 72 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_len, &t.enc_part, 73 &len, ret); 74 free_Ticket(&t); 75 if (ret) 76 return ret; 77 if(buf_len != len) { 78 free(buf); 79 return KRB5KRB_ERR_GENERIC; 80 } 81 82 kt->ticket = buf; 83 kt->ticket_len = buf_len; 84 85 } else { 86 kvno = KAFS_RXKAD_K5_KVNO; 87 kt->ticket = malloc(cred->ticket.length); 88 if (kt->ticket == NULL) 89 return ENOMEM; 90 kt->ticket_len = cred->ticket.length; 91 memcpy(kt->ticket, cred->ticket.data, kt->ticket_len); 92 93 ret = 0; 94 } 95 96 97 /* 98 * Build a struct ClearToken 99 */ 100 101 kt->ct.AuthHandle = kvno; 102 memcpy(kt->ct.HandShakeKey, cred->session.keyvalue.data, 8); 103 kt->ct.ViceId = uid; 104 kt->ct.BeginTimestamp = cred->times.starttime; 105 kt->ct.EndTimestamp = cred->times.endtime; 106 107 _kafs_fixup_viceid(&kt->ct, uid); 108 109 return 0; 110 } 111 112 static krb5_error_code 113 v5_convert(krb5_context context, krb5_ccache id, 114 krb5_creds *cred, uid_t uid, 115 const char *cell, 116 struct kafs_token *kt) 117 { 118 krb5_error_code ret; 119 char *c, *val; 120 121 c = strdup(cell); 122 if (c == NULL) 123 return ENOMEM; 124 _kafs_foldup(c, c); 125 krb5_appdefault_string (context, "libkafs", 126 c, 127 "afs-use-524", "2b", &val); 128 free(c); 129 130 if (strcasecmp(val, "local") == 0 || 131 strcasecmp(val, "2b") == 0) 132 ret = v5_to_kt(cred, uid, kt, 1); 133 else 134 ret = v5_to_kt(cred, uid, kt, 0); 135 136 free(val); 137 return ret; 138 } 139 140 141 /* 142 * 143 */ 144 145 static int 146 get_cred(struct kafs_data *data, const char *name, const char *inst, 147 const char *realm, uid_t uid, struct kafs_token *kt) 148 { 149 krb5_error_code ret; 150 krb5_creds in_creds, *out_creds; 151 struct krb5_kafs_data *d = data->data; 152 int invalid; 153 154 memset(&in_creds, 0, sizeof(in_creds)); 155 156 ret = krb5_make_principal(d->context, &in_creds.server, 157 realm, name, inst, NULL); 158 if(ret) 159 return ret; 160 ret = krb5_cc_get_principal(d->context, d->id, &in_creds.client); 161 if(ret){ 162 krb5_free_principal(d->context, in_creds.server); 163 return ret; 164 } 165 166 in_creds.session.keytype = ETYPE_DES_CBC_CRC; 167 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); 170 if (invalid) 171 krb5_enctype_enable(d->context, in_creds.session.keytype); 172 173 ret = krb5_get_credentials(d->context, 0, d->id, &in_creds, &out_creds); 174 if (ret) { 175 in_creds.session.keytype = ETYPE_DES_CBC_MD5; 176 ret = krb5_get_credentials(d->context, 0, d->id, &in_creds, &out_creds); 177 } 178 179 if (invalid) 180 krb5_enctype_disable(d->context, in_creds.session.keytype); 181 182 krb5_free_principal(d->context, in_creds.server); 183 krb5_free_principal(d->context, in_creds.client); 184 if(ret) 185 return ret; 186 187 ret = v5_convert(d->context, d->id, out_creds, uid, 188 (inst != NULL && inst[0] != '\0') ? inst : realm, kt); 189 krb5_free_creds(d->context, out_creds); 190 191 return ret; 192 } 193 194 static const char * 195 get_error(struct kafs_data *data, int error) 196 { 197 struct krb5_kafs_data *d = data->data; 198 return krb5_get_error_message(d->context, error); 199 } 200 201 static void 202 free_error(struct kafs_data *data, const char *str) 203 { 204 struct krb5_kafs_data *d = data->data; 205 krb5_free_error_message(d->context, str); 206 } 207 208 static krb5_error_code 209 afslog_uid_int(struct kafs_data *data, const char *cell, const char *rh, 210 uid_t uid, const char *homedir) 211 { 212 krb5_error_code ret; 213 struct kafs_token kt; 214 krb5_principal princ; 215 const char *trealm; /* ticket realm */ 216 struct krb5_kafs_data *d = data->data; 217 218 if (cell == 0 || cell[0] == 0) 219 return _kafs_afslog_all_local_cells (data, uid, homedir); 220 221 ret = krb5_cc_get_principal (d->context, d->id, &princ); 222 if (ret) 223 return ret; 224 225 trealm = krb5_principal_get_realm (d->context, princ); 226 227 kt.ticket = NULL; 228 ret = _kafs_get_cred(data, cell, d->realm, trealm, uid, &kt); 229 krb5_free_principal (d->context, princ); 230 231 if(ret == 0) { 232 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len); 233 free(kt.ticket); 234 } 235 return ret; 236 } 237 238 static char * 239 get_realm(struct kafs_data *data, const char *host) 240 { 241 struct krb5_kafs_data *d = data->data; 242 krb5_realm *realms; 243 char *r; 244 if(krb5_get_host_realm(d->context, host, &realms)) 245 return NULL; 246 r = strdup(realms[0]); 247 krb5_free_host_realm(d->context, realms); 248 return r; 249 } 250 251 krb5_error_code 252 krb5_afslog_uid_home(krb5_context context, 253 krb5_ccache id, 254 const char *cell, 255 krb5_const_realm realm, 256 uid_t uid, 257 const char *homedir) 258 { 259 struct kafs_data kd; 260 struct krb5_kafs_data d; 261 krb5_error_code ret; 262 263 kd.name = "krb5"; 264 kd.afslog_uid = afslog_uid_int; 265 kd.get_cred = get_cred; 266 kd.get_realm = get_realm; 267 kd.get_error = get_error; 268 kd.free_error = free_error; 269 kd.data = &d; 270 if (context == NULL) { 271 ret = krb5_init_context(&d.context); 272 if (ret) 273 return ret; 274 } else 275 d.context = context; 276 if (id == NULL) { 277 ret = krb5_cc_default(d.context, &d.id); 278 if (ret) 279 goto out; 280 } else 281 d.id = id; 282 d.realm = realm; 283 ret = afslog_uid_int(&kd, cell, 0, uid, homedir); 284 if (id == NULL) 285 krb5_cc_close(context, d.id); 286 out: 287 if (context == NULL) 288 krb5_free_context(d.context); 289 return ret; 290 } 291 292 krb5_error_code 293 krb5_afslog_uid(krb5_context context, 294 krb5_ccache id, 295 const char *cell, 296 krb5_const_realm realm, 297 uid_t uid) 298 { 299 return krb5_afslog_uid_home (context, id, cell, realm, uid, NULL); 300 } 301 302 krb5_error_code 303 krb5_afslog(krb5_context context, 304 krb5_ccache id, 305 const char *cell, 306 krb5_const_realm realm) 307 { 308 return krb5_afslog_uid (context, id, cell, realm, getuid()); 309 } 310 311 krb5_error_code 312 krb5_afslog_home(krb5_context context, 313 krb5_ccache id, 314 const char *cell, 315 krb5_const_realm realm, 316 const char *homedir) 317 { 318 return krb5_afslog_uid_home (context, id, cell, realm, getuid(), homedir); 319 } 320 321 /* 322 * 323 */ 324 325 krb5_error_code 326 krb5_realm_of_cell(const char *cell, char **realm) 327 { 328 struct kafs_data kd; 329 330 kd.name = "krb5"; 331 kd.get_realm = get_realm; 332 kd.get_error = get_error; 333 kd.free_error = free_error; 334 return _kafs_realm_of_cell(&kd, cell, realm); 335 } 336 337 /* 338 * 339 */ 340 341 int 342 kafs_settoken5(krb5_context context, const char *cell, uid_t uid, 343 krb5_creds *cred) 344 { 345 struct kafs_token kt; 346 int ret; 347 348 ret = v5_convert(context, NULL, cred, uid, cell, &kt); 349 if (ret) 350 return ret; 351 352 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len); 353 354 free(kt.ticket); 355 356 return ret; 357 } 358