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 RCSID("$Id: afskrb5.c,v 1.18.2.1 2003/04/22 14:25:43 joda Exp $"); 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 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 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", "yes", &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 if(strcasecmp(val, "yes") == 0 || 136 strcasecmp(val, "true") == 0 || 137 atoi(val)) { 138 struct credentials c; 139 140 if (id == NULL) 141 ret = krb524_convert_creds_kdc(context, cred, &c); 142 else 143 ret = krb524_convert_creds_kdc_ccache(context, id, cred, &c); 144 if (ret) 145 goto out; 146 147 ret = _kafs_v4_to_kt(&c, uid, kt); 148 } else 149 ret = v5_to_kt(cred, uid, kt, 0); 150 151 out: 152 free(val); 153 return ret; 154 } 155 156 157 /* 158 * 159 */ 160 161 static int 162 get_cred(kafs_data *data, const char *name, const char *inst, 163 const char *realm, uid_t uid, struct kafs_token *kt) 164 { 165 krb5_error_code ret; 166 krb5_creds in_creds, *out_creds; 167 struct krb5_kafs_data *d = data->data; 168 169 memset(&in_creds, 0, sizeof(in_creds)); 170 ret = krb5_425_conv_principal(d->context, name, inst, realm, 171 &in_creds.server); 172 if(ret) 173 return ret; 174 ret = krb5_cc_get_principal(d->context, d->id, &in_creds.client); 175 if(ret){ 176 krb5_free_principal(d->context, in_creds.server); 177 return ret; 178 } 179 in_creds.session.keytype = KEYTYPE_DES; 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); 183 if(ret) 184 return ret; 185 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); 189 190 return ret; 191 } 192 193 static krb5_error_code 194 afslog_uid_int(kafs_data *data, const char *cell, const char *rh, uid_t uid, 195 const char *homedir) 196 { 197 krb5_error_code ret; 198 struct kafs_token kt; 199 krb5_principal princ; 200 krb5_realm *trealm; /* ticket realm */ 201 struct krb5_kafs_data *d = data->data; 202 203 if (cell == 0 || cell[0] == 0) 204 return _kafs_afslog_all_local_cells (data, uid, homedir); 205 206 ret = krb5_cc_get_principal (d->context, d->id, &princ); 207 if (ret) 208 return ret; 209 210 trealm = krb5_princ_realm (d->context, princ); 211 212 if (d->realm != NULL && strcmp (d->realm, *trealm) == 0) { 213 trealm = NULL; 214 krb5_free_principal (d->context, princ); 215 } 216 217 kt.ticket = NULL; 218 ret = _kafs_get_cred(data, cell, d->realm, *trealm, uid, &kt); 219 if(trealm) 220 krb5_free_principal (d->context, princ); 221 222 if(ret == 0) { 223 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len); 224 free(kt.ticket); 225 } 226 return ret; 227 } 228 229 static char * 230 get_realm(kafs_data *data, const char *host) 231 { 232 struct krb5_kafs_data *d = data->data; 233 krb5_realm *realms; 234 char *r; 235 if(krb5_get_host_realm(d->context, host, &realms)) 236 return NULL; 237 r = strdup(realms[0]); 238 krb5_free_host_realm(d->context, realms); 239 return r; 240 } 241 242 krb5_error_code 243 krb5_afslog_uid_home(krb5_context context, 244 krb5_ccache id, 245 const char *cell, 246 krb5_const_realm realm, 247 uid_t uid, 248 const char *homedir) 249 { 250 kafs_data kd; 251 struct krb5_kafs_data d; 252 kd.name = "krb5"; 253 kd.afslog_uid = afslog_uid_int; 254 kd.get_cred = get_cred; 255 kd.get_realm = get_realm; 256 kd.data = &d; 257 d.context = context; 258 d.id = id; 259 d.realm = realm; 260 return afslog_uid_int(&kd, cell, 0, uid, homedir); 261 } 262 263 krb5_error_code 264 krb5_afslog_uid(krb5_context context, 265 krb5_ccache id, 266 const char *cell, 267 krb5_const_realm realm, 268 uid_t uid) 269 { 270 return krb5_afslog_uid_home (context, id, cell, realm, uid, NULL); 271 } 272 273 krb5_error_code 274 krb5_afslog(krb5_context context, 275 krb5_ccache id, 276 const char *cell, 277 krb5_const_realm realm) 278 { 279 return krb5_afslog_uid (context, id, cell, realm, getuid()); 280 } 281 282 krb5_error_code 283 krb5_afslog_home(krb5_context context, 284 krb5_ccache id, 285 const char *cell, 286 krb5_const_realm realm, 287 const char *homedir) 288 { 289 return krb5_afslog_uid_home (context, id, cell, realm, getuid(), homedir); 290 } 291 292 /* 293 * 294 */ 295 296 krb5_error_code 297 krb5_realm_of_cell(const char *cell, char **realm) 298 { 299 kafs_data kd; 300 301 kd.name = "krb5"; 302 kd.get_realm = get_realm; 303 return _kafs_realm_of_cell(&kd, cell, realm); 304 } 305 306 /* 307 * 308 */ 309 310 int 311 kafs_settoken5(krb5_context context, const char *cell, uid_t uid, 312 krb5_creds *cred) 313 { 314 struct kafs_token kt; 315 int ret; 316 317 ret = v5_convert(context, NULL, cred, uid, cell, &kt); 318 if (ret) 319 return ret; 320 321 ret = kafs_settoken_rxkad(cell, &kt.ct, kt.ticket, kt.ticket_len); 322 323 free(kt.ticket); 324 325 return ret; 326 } 327