Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / appl / kip / kip.c
1 /*
2  * Copyright (c) 1995 - 2000 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 "kip.h"
35
36 RCSID("$Id: kip.c,v 1.18.2.1 2000/06/23 02:55:01 assar Exp $");
37
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;
43
44 struct getargs args[] = {
45     { "port",           'p',    arg_string,     &port_str,      "Use this port",
46       "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 }
53 };
54
55
56 static RETSIGTYPE
57 disconnecthandler (int sig)
58 {
59      disconnect = 1;
60      SIGRETURN(0);
61 }
62
63 /*
64  * Establish authenticated connection
65  */
66
67 static int
68 connect_host (char *host, int port,
69               des_cblock *key, des_key_schedule schedule)
70 {
71      CREDENTIALS cred;
72      KTEXT_ST text;
73      MSG_DAT msg;
74      int status;
75      struct sockaddr_in thisaddr, thataddr;
76      int addrlen;
77      struct hostent *hostent;
78      int s;
79      u_char b;
80      char **p;
81
82      hostent = gethostbyname (host);
83      if (hostent == NULL) {
84          warnx ("gethostbyname '%s': %s", host,
85                 hstrerror(h_errno));
86           return -1;
87      }
88
89      memset (&thataddr, 0, sizeof(thataddr));
90      thataddr.sin_family = AF_INET;
91      thataddr.sin_port   = port;
92
93      for(p = hostent->h_addr_list; *p; ++p) {
94          memcpy (&thataddr.sin_addr, *p, sizeof(thataddr.sin_addr));
95
96          s = socket (AF_INET, SOCK_STREAM, 0);
97          if (s < 0) {
98              warn ("socket");
99              return -1;
100          }
101
102 #if defined(TCP_NODELAY) && defined(HAVE_SETSOCKOPT)
103          {
104              int one = 1;
105
106              setsockopt (s, IPPROTO_TCP, TCP_NODELAY,
107                          (void *)&one, sizeof(one));
108          }
109 #endif
110
111          if (connect (s, (struct sockaddr *)&thataddr, sizeof(thataddr)) < 0) {
112              warn ("connect(%s)", host);
113              close (s);
114              continue;
115          } else {
116              break;
117          }
118      }
119      if (*p == NULL)
120          return -1;
121
122      addrlen = sizeof(thisaddr);
123      if (getsockname (s, (struct sockaddr *)&thisaddr, &addrlen) < 0 ||
124          addrlen != sizeof(thisaddr)) {
125          warn ("getsockname(%s)", host);
126          return -1;
127      }
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));
135          return -1;
136      }
137      if (read (s, &b, sizeof(b)) != sizeof(b)) {
138          warn ("read");
139          return -1;
140      }
141      if (b) {
142           char buf[BUFSIZ];
143
144           read (s, buf, sizeof(buf));
145           buf[BUFSIZ - 1] = '\0';
146
147           warnx ("%s: %s", host, buf);
148           return -1;
149      }
150
151      memcpy(key, &cred.session, sizeof(des_cblock));
152      return s;
153 }
154
155 /*
156  * Connect to the given host.
157  */
158
159 static int
160 doit (char *host, int port)
161 {
162      char tun_if_name[64];
163      des_key_schedule schedule;
164      des_cblock iv;
165      int other, this, ret;
166
167      other = connect_host (host, port, &iv, schedule);
168      if (other < 0)
169           return 1;
170      this = tunnel_open (tun_if_name, sizeof(tun_if_name));
171      if (this < 0)
172           return 1;
173
174      if (cmd_str) {
175          char buf[1024];
176          ret = kip_exec (cmd_str, buf, sizeof(buf),
177                          "kip-control", "up", tun_if_name, host, arg_str,
178                          NULL);
179          if (ret)
180              errx (1, "%s (up) failed: %s", cmd_str, buf);
181      }
182
183      ret = copy_packets (this, other, TUNMTU, &iv, schedule);
184
185      if (cmd_str) {
186          char buf[1024];
187          ret = kip_exec (cmd_str, buf, sizeof(buf),
188                          "kip-control", "down", tun_if_name, host, arg_str,
189                          NULL);
190          if (ret)
191              errx (1, "%s (down) failed: %s", cmd_str, buf);
192      }
193      return 0;
194 }
195
196 static void
197 usage(int ret)
198 {
199     arg_printusage (args,
200                     sizeof(args) / sizeof(args[0]),
201                     NULL,
202                     "hostname");
203     exit (ret);
204 }
205
206 /*
207  * kip - forward IP packets over a kerberos-encrypted channel.
208  *
209  */
210
211 int
212 main(int argc, char **argv)
213 {
214     int port;
215     int optind = 0;
216     char *hostname;
217
218     set_progname (argv[0]);
219     if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
220                 &optind))
221         usage (1);
222
223     if (help_flag)
224         usage (0);
225
226     if (version_flag) {
227         print_version (NULL);
228         return 0;
229     }
230
231     argv += optind;
232     argc -= optind;
233
234     if (argc != 1)
235         usage (1);
236     
237     hostname = argv[0];
238
239     if(port_str) {
240         struct servent *s = roken_getservbyname (port_str, "tcp");
241
242         if (s)
243             port = s->s_port;
244         else {
245             char *ptr;
246
247             port = strtol (port_str, &ptr, 10);
248             if (port == 0 && ptr == port_str)
249                 errx (1, "bad port `%s'", port_str);
250             port = htons(port);
251         }
252     } else {
253         port = k_getportbyname ("kip", "tcp", htons(KIPPORT));
254     }
255
256     signal (SIGCHLD, childhandler);
257     signal (SIGHUP,  disconnecthandler);
258     signal (SIGTERM, disconnecthandler);
259
260     return doit (hostname, port);
261 }