3 * Copyright (c) 1994 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software_Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
28 * This program is intended to run on afs DB servers.
29 * Its function is to forward KA requests to a fakeka server
30 * running on an MIT kerberos server.
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
49 int getopt (int, char * const *, const char *);
54 #define BUFFER_SIZE 2048
59 int num_servers, cur_server;
60 struct sockaddr_in *servers;
73 setup_servers(argc, argv)
83 servers = malloc(sizeof(*servers) * num_servers);
85 perrorexit("malloc failed");
87 for (i = 0; i < num_servers; i++) {
90 fwdport = htons(7004);
93 port = strchr(host, '/');
97 if (isdigit(port[0])) {
98 fwdport = htons(atoi(port));
101 struct servent *srv = getservbyname(port, "udp");
103 fprintf(stderr, "%s: unknown service %s\n", prog, port);
106 fwdport = srv->s_port;
110 if (isdigit(host[0])) {
111 fwdaddr = inet_addr(host);
114 struct hostent *h = gethostbyname(host);
116 fprintf(stderr, "%s: unknown host %s\n", prog, host);
119 bcopy(h->h_addr, &fwdaddr, 4);
122 servers[i].sin_family = AF_INET;
123 servers[i].sin_addr.s_addr = fwdaddr;
124 servers[i].sin_port = fwdport;
134 struct sockaddr_in sin;
136 s = socket(AF_INET, SOCK_DGRAM, 0);
138 perrorexit("Couldn't create socket");
140 sin.sin_family = AF_INET;
141 sin.sin_addr.s_addr = 0;
142 sin.sin_port = htons(port);
144 rv = bind(s, (struct sockaddr *)&sin, sizeof(sin));
146 perrorexit("Couldn't bind socket");
153 packet_is_reply(from)
154 struct sockaddr_in *from;
158 for (i = 0; i < num_servers; i++) {
159 struct sockaddr_in *sin = &servers[i];
161 if (from->sin_addr.s_addr == sin->sin_addr.s_addr &&
162 from->sin_port == sin->sin_port)
182 "usage: %s [-p port] <host>[/port] [host/port ...]\n",
190 while ((c = getopt(argc, argv, "p:")) != -1) {
196 fprintf(stderr, "%s: invalid option '%c'\n", prog, c);
202 * hmm, different implementations of getopt seem to do different things
203 * when there aren't any options. linux sets optind = 1, which I would
204 * call correct, but sunos sets optind = 0. try to do the right thing.
209 setup_servers(argc - optind, argv + optind);
210 s = setup_socket(port);
212 openlog("ka-forwarder", LOG_PID, LOG_DAEMON);
215 char buf[BUFFER_SIZE], *bufp, *sendptr;
216 struct sockaddr_in from, reply, *to;
217 int fromlen, sendlen;
220 fromlen = sizeof(from);
222 rv = recvfrom(s, bufp, sizeof(buf) - 8,
223 0, (struct sockaddr *)&from, &fromlen);
225 syslog(LOG_ERR, "recvfrom: %m");
230 if (packet_is_reply(&from)) {
231 /* this is a reply, forward back to user */
234 reply.sin_family = AF_INET;
235 bcopy(bufp, &reply.sin_addr.s_addr, 4);
236 bcopy(bufp + 4, &reply.sin_port, 2);
241 /* this is a request, forward to server */
243 cur_server = (cur_server + 1) % num_servers;
244 to = &servers[cur_server];
246 bcopy(&from.sin_addr.s_addr, bufp - 8, 4);
247 bcopy(&from.sin_port, bufp - 4, 2);
255 strcpy(a1, inet_ntoa(from.sin_addr));
256 strcpy(a2, inet_ntoa(to->sin_addr));
258 syslog(LOG_INFO, "forwarding %d bytes from %s/%d to %s/%d\n",
259 sendlen, a1, htons(from.sin_port), a2, htons(to->sin_port));
262 rv = sendto(s, sendptr, sendlen,
263 0, (struct sockaddr *)to, sizeof(*to));
265 syslog(LOG_ERR, "sendto: %m");