2 * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 RCSID("$Id: kip.c,v 1.18.2.1 2000/06/23 02:55:01 assar Exp $");
38 static char *cmd_str = NULL;
39 static char *arg_str = NULL;
40 static char *port_str = NULL;
41 static int version_flag = 0;
42 static int help_flag = 0;
44 struct getargs args[] = {
45 { "port", 'p', arg_string, &port_str, "Use this port",
47 { "cmd", 'c', arg_string, &cmd_str,
48 "command to run when starting", "cmd"},
49 { "arg", 'a', arg_string, &arg_str,
50 "argument to above command", "arg"},
51 { "version", 0, arg_flag, &version_flag },
52 { "help", 0, arg_flag, &help_flag }
57 disconnecthandler (int sig)
64 * Establish authenticated connection
68 connect_host (char *host, int port,
69 des_cblock *key, des_key_schedule schedule)
75 struct sockaddr_in thisaddr, thataddr;
77 struct hostent *hostent;
82 hostent = gethostbyname (host);
83 if (hostent == NULL) {
84 warnx ("gethostbyname '%s': %s", host,
89 memset (&thataddr, 0, sizeof(thataddr));
90 thataddr.sin_family = AF_INET;
91 thataddr.sin_port = port;
93 for(p = hostent->h_addr_list; *p; ++p) {
94 memcpy (&thataddr.sin_addr, *p, sizeof(thataddr.sin_addr));
96 s = socket (AF_INET, SOCK_STREAM, 0);
102 #if defined(TCP_NODELAY) && defined(HAVE_SETSOCKOPT)
106 setsockopt (s, IPPROTO_TCP, TCP_NODELAY,
107 (void *)&one, sizeof(one));
111 if (connect (s, (struct sockaddr *)&thataddr, sizeof(thataddr)) < 0) {
112 warn ("connect(%s)", host);
122 addrlen = sizeof(thisaddr);
123 if (getsockname (s, (struct sockaddr *)&thisaddr, &addrlen) < 0 ||
124 addrlen != sizeof(thisaddr)) {
125 warn ("getsockname(%s)", host);
128 status = krb_sendauth (KOPT_DO_MUTUAL, s, &text, "rcmd",
129 host, krb_realmofhost (host),
130 getpid(), &msg, &cred, schedule,
131 &thisaddr, &thataddr, KIP_VERSION);
132 if (status != KSUCCESS) {
133 warnx("%s: %s", host,
134 krb_get_err_text(status));
137 if (read (s, &b, sizeof(b)) != sizeof(b)) {
144 read (s, buf, sizeof(buf));
145 buf[BUFSIZ - 1] = '\0';
147 warnx ("%s: %s", host, buf);
151 memcpy(key, &cred.session, sizeof(des_cblock));
156 * Connect to the given host.
160 doit (char *host, int port)
162 char tun_if_name[64];
163 des_key_schedule schedule;
165 int other, this, ret;
167 other = connect_host (host, port, &iv, schedule);
170 this = tunnel_open (tun_if_name, sizeof(tun_if_name));
176 ret = kip_exec (cmd_str, buf, sizeof(buf),
177 "kip-control", "up", tun_if_name, host, arg_str,
180 errx (1, "%s (up) failed: %s", cmd_str, buf);
183 ret = copy_packets (this, other, TUNMTU, &iv, schedule);
187 ret = kip_exec (cmd_str, buf, sizeof(buf),
188 "kip-control", "down", tun_if_name, host, arg_str,
191 errx (1, "%s (down) failed: %s", cmd_str, buf);
199 arg_printusage (args,
200 sizeof(args) / sizeof(args[0]),
207 * kip - forward IP packets over a kerberos-encrypted channel.
212 main(int argc, char **argv)
218 set_progname (argv[0]);
219 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
227 print_version (NULL);
240 struct servent *s = roken_getservbyname (port_str, "tcp");
247 port = strtol (port_str, &ptr, 10);
248 if (port == 0 && ptr == port_str)
249 errx (1, "bad port `%s'", port_str);
253 port = k_getportbyname ("kip", "tcp", htons(KIPPORT));
256 signal (SIGCHLD, childhandler);
257 signal (SIGHUP, disconnecthandler);
258 signal (SIGTERM, disconnecthandler);
260 return doit (hostname, port);