common.c revision 102644
117680Spst/* 217680Spst * Copyright (c) 1997 - 1999, 2002 Kungliga Tekniska H�gskolan 317680Spst * (Royal Institute of Technology, Stockholm, Sweden). 417680Spst * All rights reserved. 517680Spst * 617680Spst * Redistribution and use in source and binary forms, with or without 717680Spst * modification, are permitted provided that the following conditions 817680Spst * are met: 917680Spst * 1017680Spst * 1. Redistributions of source code must retain the above copyright 1117680Spst * notice, this list of conditions and the following disclaimer. 1217680Spst * 1317680Spst * 2. Redistributions in binary form must reproduce the above copyright 1417680Spst * notice, this list of conditions and the following disclaimer in the 1517680Spst * documentation and/or other materials provided with the distribution. 1617680Spst * 1717680Spst * 3. Neither the name of the Institute nor the names of its contributors 1817680Spst * may be used to endorse or promote products derived from this software 1917680Spst * without specific prior written permission. 2017680Spst * 2117680Spst * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2217680Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2326180Sfenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2426180Sfenner * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2517680Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2617680Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2717680Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2817680Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2917680Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3017680Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3117680Spst * SUCH DAMAGE. 3217680Spst */ 3317680Spst 3417680Spst#include "rsh_locl.h" 3517680SpstRCSID("$Id: common.c,v 1.14 2002/02/18 20:01:05 joda Exp $"); 3617680Spst 3717680Spst#if defined(KRB4) || defined(KRB5) 3817680Spst 3917680Spstssize_t 4017680Spstdo_read (int fd, 4117680Spst void *buf, 4217680Spst size_t sz) 4317680Spst{ 4417680Spst if (do_encrypt) { 4517680Spst#ifdef KRB4 4617680Spst if (auth_method == AUTH_KRB4) { 4717680Spst return des_enc_read (fd, buf, sz, schedule, &iv); 4817680Spst } else 4917680Spst#endif /* KRB4 */ 5017680Spst#ifdef KRB5 5117680Spst if(auth_method == AUTH_KRB5) { 5217680Spst krb5_error_code ret; 5317680Spst u_int32_t len, outer_len; 5417680Spst int status; 5517680Spst krb5_data data; 5617680Spst void *edata; 5717680Spst 5817680Spst ret = krb5_net_read (context, &fd, &len, 4); 5917680Spst if (ret <= 0) 6017680Spst return ret; 6117680Spst len = ntohl(len); 6217680Spst if (len > sz) 6317680Spst abort (); 6417680Spst outer_len = krb5_get_wrapped_length (context, crypto, len); 6517680Spst edata = malloc (outer_len); 6617680Spst if (edata == NULL) 6717680Spst errx (1, "malloc: cannot allocate %u bytes", outer_len); 6817680Spst ret = krb5_net_read (context, &fd, edata, outer_len); 6917680Spst if (ret <= 0) 7017680Spst return ret; 7117680Spst 7217680Spst status = krb5_decrypt(context, crypto, KRB5_KU_OTHER_ENCRYPTED, 7317680Spst edata, outer_len, &data); 7417680Spst free (edata); 7517680Spst 7617680Spst if (status) 7717680Spst errx (1, "%s", krb5_get_err_text (context, status)); 7817680Spst memcpy (buf, data.data, len); 7917680Spst krb5_data_free (&data); 8017680Spst return len; 8117680Spst } else 8217680Spst#endif /* KRB5 */ 8317680Spst abort (); 8417680Spst } else 8517680Spst return read (fd, buf, sz); 8617680Spst} 8717680Spst 8817680Spstssize_t 8917680Spstdo_write (int fd, void *buf, size_t sz) 9017680Spst{ 9117680Spst if (do_encrypt) { 9217680Spst#ifdef KRB4 9317680Spst if(auth_method == AUTH_KRB4) { 9417680Spst return des_enc_write (fd, buf, sz, schedule, &iv); 9517680Spst } else 9617680Spst#endif /* KRB4 */ 9717680Spst#ifdef KRB5 9817680Spst if(auth_method == AUTH_KRB5) { 9917680Spst krb5_error_code status; 10017680Spst krb5_data data; 10117680Spst u_int32_t len; 10217680Spst int ret; 10317680Spst 10417680Spst status = krb5_encrypt(context, crypto, KRB5_KU_OTHER_ENCRYPTED, 10517680Spst buf, sz, &data); 10617680Spst 10717680Spst if (status) 10817680Spst errx (1, "%s", krb5_get_err_text(context, status)); 10917680Spst 11017680Spst assert (krb5_get_wrapped_length (context, crypto, 11117680Spst sz) == data.length); 11217680Spst 11317680Spst len = htonl(sz); 11417680Spst ret = krb5_net_write (context, &fd, &len, 4); 11517680Spst if (ret != 4) 11617680Spst return ret; 11717680Spst ret = krb5_net_write (context, &fd, data.data, data.length); 11817680Spst if (ret != data.length) 11917680Spst return ret; 12017680Spst free (data.data); 12117680Spst return sz; 12217680Spst } else 12317680Spst#endif /* KRB5 */ 12417680Spst abort(); 12517680Spst } else 12617680Spst return write (fd, buf, sz); 12717680Spst} 12817680Spst#endif /* KRB4 || KRB5 */ 12917680Spst