1 /* 2 * Copyright (c) 1997 - 1999 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 "rsh_locl.h" 35 RCSID("$Id: common.c,v 1.12 1999/12/02 17:04:56 joda Exp $"); 36 37 ssize_t 38 do_read (int fd, 39 void *buf, 40 size_t sz) 41 { 42 int ret; 43 44 if (do_encrypt) { 45 #ifdef KRB4 46 if (auth_method == AUTH_KRB4) { 47 return des_enc_read (fd, buf, sz, schedule, &iv); 48 } else 49 #endif /* KRB4 */ 50 if(auth_method == AUTH_KRB5) { 51 u_int32_t len, outer_len; 52 int status; 53 krb5_data data; 54 void *edata; 55 56 ret = krb5_net_read (context, &fd, &len, 4); 57 if (ret <= 0) 58 return ret; 59 len = ntohl(len); 60 if (len > sz) 61 abort (); 62 outer_len = krb5_get_wrapped_length (context, crypto, len); 63 edata = malloc (outer_len); 64 if (edata == NULL) 65 errx (1, "malloc: cannot allocate %u bytes", outer_len); 66 ret = krb5_net_read (context, &fd, edata, outer_len); 67 if (ret <= 0) 68 return ret; 69 70 status = krb5_decrypt(context, crypto, KRB5_KU_OTHER_ENCRYPTED, 71 edata, outer_len, &data); 72 free (edata); 73 74 if (status) 75 errx (1, "%s", krb5_get_err_text (context, status)); 76 memcpy (buf, data.data, len); 77 krb5_data_free (&data); 78 return len; 79 } else { 80 abort (); 81 } 82 } else 83 return read (fd, buf, sz); 84 } 85 86 ssize_t 87 do_write (int fd, void *buf, size_t sz) 88 { 89 if (do_encrypt) { 90 #ifdef KRB4 91 if(auth_method == AUTH_KRB4) { 92 return des_enc_write (fd, buf, sz, schedule, &iv); 93 } else 94 #endif /* KRB4 */ 95 if(auth_method == AUTH_KRB5) { 96 krb5_error_code status; 97 krb5_data data; 98 u_int32_t len; 99 int ret; 100 101 status = krb5_encrypt(context, crypto, KRB5_KU_OTHER_ENCRYPTED, 102 buf, sz, &data); 103 104 if (status) 105 errx (1, "%s", krb5_get_err_text(context, status)); 106 107 assert (krb5_get_wrapped_length (context, crypto, 108 sz) == data.length); 109 110 len = htonl(sz); 111 ret = krb5_net_write (context, &fd, &len, 4); 112 if (ret != 4) 113 return ret; 114 ret = krb5_net_write (context, &fd, data.data, data.length); 115 if (ret != data.length) 116 return ret; 117 free (data.data); 118 return sz; 119 } else { 120 abort(); 121 } 122 } else 123 return write (fd, buf, sz); 124 } 125