1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include "rsh_locl.h"
35*ae771770SStanislav Sedov RCSID("$Id$");
368373020dSJacques Vidrine
37*ae771770SStanislav Sedov #if defined(KRB5)
38b528cefcSMark Murray
390cadf2f4SJacques Vidrine #ifdef KRB5
400cadf2f4SJacques Vidrine int key_usage = 1026;
410cadf2f4SJacques Vidrine
420cadf2f4SJacques Vidrine void *ivec_in[2];
430cadf2f4SJacques Vidrine void *ivec_out[2];
440cadf2f4SJacques Vidrine
450cadf2f4SJacques Vidrine void
init_ivecs(int client,int have_errsock)46c19800e8SDoug Rabson init_ivecs(int client, int have_errsock)
470cadf2f4SJacques Vidrine {
480cadf2f4SJacques Vidrine size_t blocksize;
490cadf2f4SJacques Vidrine
500cadf2f4SJacques Vidrine krb5_crypto_getblocksize(context, crypto, &blocksize);
510cadf2f4SJacques Vidrine
520cadf2f4SJacques Vidrine ivec_in[0] = malloc(blocksize);
530cadf2f4SJacques Vidrine memset(ivec_in[0], client, blocksize);
540cadf2f4SJacques Vidrine
55c19800e8SDoug Rabson if(have_errsock) {
560cadf2f4SJacques Vidrine ivec_in[1] = malloc(blocksize);
570cadf2f4SJacques Vidrine memset(ivec_in[1], 2 | client, blocksize);
58c19800e8SDoug Rabson } else
59c19800e8SDoug Rabson ivec_in[1] = ivec_in[0];
600cadf2f4SJacques Vidrine
610cadf2f4SJacques Vidrine ivec_out[0] = malloc(blocksize);
620cadf2f4SJacques Vidrine memset(ivec_out[0], !client, blocksize);
630cadf2f4SJacques Vidrine
64c19800e8SDoug Rabson if(have_errsock) {
650cadf2f4SJacques Vidrine ivec_out[1] = malloc(blocksize);
660cadf2f4SJacques Vidrine memset(ivec_out[1], 2 | !client, blocksize);
67c19800e8SDoug Rabson } else
68c19800e8SDoug Rabson ivec_out[1] = ivec_out[0];
690cadf2f4SJacques Vidrine }
700cadf2f4SJacques Vidrine #endif
710cadf2f4SJacques Vidrine
720cadf2f4SJacques Vidrine
73b528cefcSMark Murray ssize_t
do_read(int fd,void * buf,size_t sz,void * ivec)740cadf2f4SJacques Vidrine do_read (int fd, void *buf, size_t sz, void *ivec)
75b528cefcSMark Murray {
76b528cefcSMark Murray if (do_encrypt) {
778373020dSJacques Vidrine #ifdef KRB5
78b528cefcSMark Murray if(auth_method == AUTH_KRB5) {
798373020dSJacques Vidrine krb5_error_code ret;
80c19800e8SDoug Rabson uint32_t len, outer_len;
81b528cefcSMark Murray int status;
82b528cefcSMark Murray krb5_data data;
83b528cefcSMark Murray void *edata;
84b528cefcSMark Murray
85b528cefcSMark Murray ret = krb5_net_read (context, &fd, &len, 4);
86b528cefcSMark Murray if (ret <= 0)
87b528cefcSMark Murray return ret;
88b528cefcSMark Murray len = ntohl(len);
89b528cefcSMark Murray if (len > sz)
90b528cefcSMark Murray abort ();
910cadf2f4SJacques Vidrine /* ivec will be non null for protocol version 2 */
920cadf2f4SJacques Vidrine if(ivec != NULL)
930cadf2f4SJacques Vidrine outer_len = krb5_get_wrapped_length (context, crypto, len + 4);
940cadf2f4SJacques Vidrine else
95b528cefcSMark Murray outer_len = krb5_get_wrapped_length (context, crypto, len);
96b528cefcSMark Murray edata = malloc (outer_len);
97b528cefcSMark Murray if (edata == NULL)
98b528cefcSMark Murray errx (1, "malloc: cannot allocate %u bytes", outer_len);
99b528cefcSMark Murray ret = krb5_net_read (context, &fd, edata, outer_len);
100*ae771770SStanislav Sedov if (ret <= 0) {
101*ae771770SStanislav Sedov free(edata);
102b528cefcSMark Murray return ret;
103*ae771770SStanislav Sedov }
104b528cefcSMark Murray
1050cadf2f4SJacques Vidrine status = krb5_decrypt_ivec(context, crypto, key_usage,
1060cadf2f4SJacques Vidrine edata, outer_len, &data, ivec);
107b528cefcSMark Murray free (edata);
108b528cefcSMark Murray
109b528cefcSMark Murray if (status)
1100cadf2f4SJacques Vidrine krb5_err (context, 1, status, "decrypting data");
1110cadf2f4SJacques Vidrine if(ivec != NULL) {
1120cadf2f4SJacques Vidrine unsigned long l;
1130cadf2f4SJacques Vidrine if(data.length < len + 4)
1140cadf2f4SJacques Vidrine errx (1, "data received is too short");
1150cadf2f4SJacques Vidrine _krb5_get_int(data.data, &l, 4);
1160cadf2f4SJacques Vidrine if(l != len)
1170cadf2f4SJacques Vidrine errx (1, "inconsistency in received data");
1180cadf2f4SJacques Vidrine memcpy (buf, (unsigned char *)data.data+4, len);
1190cadf2f4SJacques Vidrine } else
120b528cefcSMark Murray memcpy (buf, data.data, len);
121b528cefcSMark Murray krb5_data_free (&data);
122b528cefcSMark Murray return len;
1238373020dSJacques Vidrine } else
1248373020dSJacques Vidrine #endif /* KRB5 */
125b528cefcSMark Murray abort ();
126b528cefcSMark Murray } else
127b528cefcSMark Murray return read (fd, buf, sz);
128b528cefcSMark Murray }
129b528cefcSMark Murray
130b528cefcSMark Murray ssize_t
do_write(int fd,void * buf,size_t sz,void * ivec)1310cadf2f4SJacques Vidrine do_write (int fd, void *buf, size_t sz, void *ivec)
132b528cefcSMark Murray {
133b528cefcSMark Murray if (do_encrypt) {
1348373020dSJacques Vidrine #ifdef KRB5
135b528cefcSMark Murray if(auth_method == AUTH_KRB5) {
136b528cefcSMark Murray krb5_error_code status;
137b528cefcSMark Murray krb5_data data;
1380cadf2f4SJacques Vidrine unsigned char len[4];
139b528cefcSMark Murray int ret;
140b528cefcSMark Murray
1410cadf2f4SJacques Vidrine _krb5_put_int(len, sz, 4);
1420cadf2f4SJacques Vidrine if(ivec != NULL) {
1430cadf2f4SJacques Vidrine unsigned char *tmp = malloc(sz + 4);
1440cadf2f4SJacques Vidrine if(tmp == NULL)
1450cadf2f4SJacques Vidrine err(1, "malloc");
1460cadf2f4SJacques Vidrine _krb5_put_int(tmp, sz, 4);
1470cadf2f4SJacques Vidrine memcpy(tmp + 4, buf, sz);
1480cadf2f4SJacques Vidrine status = krb5_encrypt_ivec(context, crypto, key_usage,
1490cadf2f4SJacques Vidrine tmp, sz + 4, &data, ivec);
1500cadf2f4SJacques Vidrine free(tmp);
1510cadf2f4SJacques Vidrine } else
1520cadf2f4SJacques Vidrine status = krb5_encrypt_ivec(context, crypto, key_usage,
1530cadf2f4SJacques Vidrine buf, sz, &data, ivec);
154b528cefcSMark Murray
155b528cefcSMark Murray if (status)
1560cadf2f4SJacques Vidrine krb5_err(context, 1, status, "encrypting data");
157b528cefcSMark Murray
1580cadf2f4SJacques Vidrine ret = krb5_net_write (context, &fd, len, 4);
159b528cefcSMark Murray if (ret != 4)
160b528cefcSMark Murray return ret;
161b528cefcSMark Murray ret = krb5_net_write (context, &fd, data.data, data.length);
162b528cefcSMark Murray if (ret != data.length)
163b528cefcSMark Murray return ret;
164b528cefcSMark Murray free (data.data);
165b528cefcSMark Murray return sz;
1668373020dSJacques Vidrine } else
1678373020dSJacques Vidrine #endif /* KRB5 */
168b528cefcSMark Murray abort();
169b528cefcSMark Murray } else
170b528cefcSMark Murray return write (fd, buf, sz);
171b528cefcSMark Murray }
172*ae771770SStanislav Sedov #endif /* KRB5 */
173