xref: /freebsd-src/contrib/tcp_wrappers/ptx.c (revision 14f102eacc8434a5a1f96466752578a4167140c9)
12aef6930SMark Murray  /*
22aef6930SMark Murray   * The Dynix/PTX TLI implementation is not quite compatible with System V
32aef6930SMark Murray   * Release 4. Some important functions are not present so we are limited to
42aef6930SMark Murray   * IP-based services.
52aef6930SMark Murray   *
62aef6930SMark Murray   * Diagnostics are reported through syslog(3).
72aef6930SMark Murray   *
82aef6930SMark Murray   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
92aef6930SMark Murray   */
102aef6930SMark Murray 
112aef6930SMark Murray #ifndef lint
122aef6930SMark Murray static char sccsid[] = "@(#) ptx.c 1.3 94/12/28 17:42:38";
132aef6930SMark Murray #endif
142aef6930SMark Murray 
152aef6930SMark Murray #ifdef PTX
162aef6930SMark Murray 
172aef6930SMark Murray /* System libraries. */
182aef6930SMark Murray 
192aef6930SMark Murray #include <sys/types.h>
202aef6930SMark Murray #include <sys/tiuser.h>
212aef6930SMark Murray #include <sys/socket.h>
222aef6930SMark Murray #include <stropts.h>
232aef6930SMark Murray #include <netinet/in.h>
242aef6930SMark Murray #include <netdb.h>
252aef6930SMark Murray #include <stdio.h>
262aef6930SMark Murray #include <syslog.h>
272aef6930SMark Murray 
282aef6930SMark Murray /* Local stuff. */
292aef6930SMark Murray 
302aef6930SMark Murray #include "tcpd.h"
312aef6930SMark Murray 
322aef6930SMark Murray /* Forward declarations. */
332aef6930SMark Murray 
342aef6930SMark Murray static void ptx_sink();
352aef6930SMark Murray 
362aef6930SMark Murray /* tli_host - determine TLI endpoint info, PTX version */
372aef6930SMark Murray 
tli_host(struct request_info * request)38*14f102eaSEd Maste void    tli_host(struct request_info *request)
392aef6930SMark Murray {
402aef6930SMark Murray     static struct sockaddr_in client;
412aef6930SMark Murray     static struct sockaddr_in server;
422aef6930SMark Murray 
432aef6930SMark Murray     /*
442aef6930SMark Murray      * getpeerinaddr() was suggested by someone at Sequent. It seems to work
452aef6930SMark Murray      * with connection-oriented (TCP) services such as rlogind and telnetd,
462aef6930SMark Murray      * but it returns 0.0.0.0 with datagram (UDP) services. No problem: UDP
472aef6930SMark Murray      * needs special treatment anyway, in case we must refuse service.
482aef6930SMark Murray      */
492aef6930SMark Murray 
502aef6930SMark Murray     if (getpeerinaddr(request->fd, &client, sizeof(client)) == 0
512aef6930SMark Murray 	&& client.sin_addr.s_addr != 0) {
522aef6930SMark Murray 	request->client->sin = &client;
532aef6930SMark Murray 	if (getmyinaddr(request->fd, &server, sizeof(server)) == 0) {
542aef6930SMark Murray 	    request->server->sin = &server;
552aef6930SMark Murray 	} else {
562aef6930SMark Murray 	    tcpd_warn("warning: getmyinaddr: %m");
572aef6930SMark Murray 	}
582aef6930SMark Murray 	sock_methods(request);
592aef6930SMark Murray 
602aef6930SMark Murray     } else {
612aef6930SMark Murray 
622aef6930SMark Murray 	/*
632aef6930SMark Murray 	 * Another suggestion was to temporarily switch to the socket
642aef6930SMark Murray 	 * interface, identify the endpoint addresses with socket calls, then
652aef6930SMark Murray 	 * to switch back to TLI. This seems to works OK with UDP services,
662aef6930SMark Murray 	 * which is exactly what we should be looking at right now.
672aef6930SMark Murray 	 */
682aef6930SMark Murray 
692aef6930SMark Murray #define SWAP_MODULE(f, old, new) (ioctl(f, I_POP, old), ioctl(f, I_PUSH, new))
702aef6930SMark Murray 
712aef6930SMark Murray 	if (SWAP_MODULE(request->fd, "timod", "sockmod") != 0)
722aef6930SMark Murray 	    tcpd_warn("replace timod by sockmod: %m");
732aef6930SMark Murray 	sock_host(request);
742aef6930SMark Murray 	if (SWAP_MODULE(request->fd, "sockmod", "timod") != 0)
752aef6930SMark Murray 	    tcpd_warn("replace sockmod by timod: %m");
762aef6930SMark Murray 	if (request->sink != 0)
772aef6930SMark Murray 	    request->sink = ptx_sink;
782aef6930SMark Murray     }
792aef6930SMark Murray }
802aef6930SMark Murray 
812aef6930SMark Murray /* ptx_sink - absorb unreceived IP datagram */
822aef6930SMark Murray 
ptx_sink(int fd)83*14f102eaSEd Maste static void ptx_sink(int fd)
842aef6930SMark Murray {
852aef6930SMark Murray     char    buf[BUFSIZ];
862aef6930SMark Murray     struct sockaddr sa;
872aef6930SMark Murray     int     size = sizeof(sa);
882aef6930SMark Murray 
892aef6930SMark Murray     /*
902aef6930SMark Murray      * Eat up the not-yet received datagram. Where needed, switch to the
912aef6930SMark Murray      * socket programming interface.
922aef6930SMark Murray      */
932aef6930SMark Murray 
942aef6930SMark Murray     if (ioctl(fd, I_FIND, "timod") != 0)
952aef6930SMark Murray 	ioctl(fd, I_POP, "timod");
962aef6930SMark Murray     if (ioctl(fd, I_FIND, "sockmod") == 0)
972aef6930SMark Murray 	ioctl(fd, I_PUSH, "sockmod");
982aef6930SMark Murray     (void) recvfrom(fd, buf, sizeof(buf), 0, &sa, &size);
992aef6930SMark Murray }
1002aef6930SMark Murray 
1012aef6930SMark Murray #endif /* PTX */
102