xref: /freebsd/contrib/tcp_wrappers/ptx.c (revision 2b833162)
1  /*
2   * The Dynix/PTX TLI implementation is not quite compatible with System V
3   * Release 4. Some important functions are not present so we are limited to
4   * IP-based services.
5   *
6   * Diagnostics are reported through syslog(3).
7   *
8   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
9   */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#) ptx.c 1.3 94/12/28 17:42:38";
13 #endif
14 
15 #ifdef PTX
16 
17 /* System libraries. */
18 
19 #include <sys/types.h>
20 #include <sys/tiuser.h>
21 #include <sys/socket.h>
22 #include <stropts.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <stdio.h>
26 #include <syslog.h>
27 
28 /* Local stuff. */
29 
30 #include "tcpd.h"
31 
32 /* Forward declarations. */
33 
34 static void ptx_sink();
35 
36 /* tli_host - determine TLI endpoint info, PTX version */
37 
38 void    tli_host(struct request_info *request)
39 {
40     static struct sockaddr_in client;
41     static struct sockaddr_in server;
42 
43     /*
44      * getpeerinaddr() was suggested by someone at Sequent. It seems to work
45      * with connection-oriented (TCP) services such as rlogind and telnetd,
46      * but it returns 0.0.0.0 with datagram (UDP) services. No problem: UDP
47      * needs special treatment anyway, in case we must refuse service.
48      */
49 
50     if (getpeerinaddr(request->fd, &client, sizeof(client)) == 0
51 	&& client.sin_addr.s_addr != 0) {
52 	request->client->sin = &client;
53 	if (getmyinaddr(request->fd, &server, sizeof(server)) == 0) {
54 	    request->server->sin = &server;
55 	} else {
56 	    tcpd_warn("warning: getmyinaddr: %m");
57 	}
58 	sock_methods(request);
59 
60     } else {
61 
62 	/*
63 	 * Another suggestion was to temporarily switch to the socket
64 	 * interface, identify the endpoint addresses with socket calls, then
65 	 * to switch back to TLI. This seems to works OK with UDP services,
66 	 * which is exactly what we should be looking at right now.
67 	 */
68 
69 #define SWAP_MODULE(f, old, new) (ioctl(f, I_POP, old), ioctl(f, I_PUSH, new))
70 
71 	if (SWAP_MODULE(request->fd, "timod", "sockmod") != 0)
72 	    tcpd_warn("replace timod by sockmod: %m");
73 	sock_host(request);
74 	if (SWAP_MODULE(request->fd, "sockmod", "timod") != 0)
75 	    tcpd_warn("replace sockmod by timod: %m");
76 	if (request->sink != 0)
77 	    request->sink = ptx_sink;
78     }
79 }
80 
81 /* ptx_sink - absorb unreceived IP datagram */
82 
83 static void ptx_sink(int fd)
84 {
85     char    buf[BUFSIZ];
86     struct sockaddr sa;
87     int     size = sizeof(sa);
88 
89     /*
90      * Eat up the not-yet received datagram. Where needed, switch to the
91      * socket programming interface.
92      */
93 
94     if (ioctl(fd, I_FIND, "timod") != 0)
95 	ioctl(fd, I_POP, "timod");
96     if (ioctl(fd, I_FIND, "sockmod") == 0)
97 	ioctl(fd, I_PUSH, "sockmod");
98     (void) recvfrom(fd, buf, sizeof(buf), 0, &sa, &size);
99 }
100 
101 #endif /* PTX */
102