xref: /freebsd/contrib/tcp_wrappers/tli-sequent.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1  /*
2   * Warning - this relies heavily on the TLI implementation in PTX 2.X and will
3   * probably not work under PTX 4.
4   *
5   * Author: Tim Wright, Sequent Computer Systems Ltd., UK.
6   *
7   * Modified slightly to conform to the new internal interfaces - Wietse
8   */
9 
10 #ifndef lint
11 static char sccsid[] = "@(#) tli-sequent.c 1.1 94/12/28 17:42:51";
12 #endif
13 
14 #ifdef TLI_SEQUENT
15 
16 /* System libraries. */
17 
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/stat.h>
21 #include <sys/tiuser.h>
22 #include <sys/stream.h>
23 #include <sys/stropts.h>
24 #include <sys/tihdr.h>
25 #include <sys/timod.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <stdio.h>
29 #include <syslog.h>
30 #include <errno.h>
31 #include <string.h>
32 
33 extern char *sys_errlist[];
34 extern int sys_nerr;
35 extern int t_errno;
36 extern char *t_errlist[];
37 extern int t_nerr;
38 
39 /* Local stuff. */
40 
41 #include "tcpd.h"
42 #include "tli-sequent.h"
43 
44 /* Forward declarations. */
45 
46 static char *tli_error();
47 static void tli_sink();
48 
49 /* tli_host - determine endpoint info */
50 
51 int     tli_host(struct request_info *request)
52 {
53     static struct sockaddr_in client;
54     static struct sockaddr_in server;
55     struct _ti_user *tli_state_ptr;
56     union T_primitives *TSI_prim_ptr;
57     struct strpeek peek;
58     int     len;
59 
60     /*
61      * Use DNS and socket routines for name and address conversions.
62      */
63 
64     sock_methods(request);
65 
66     /*
67      * Find out the client address using getpeerinaddr(). This call is the
68      * TLI equivalent to getpeername() under Dynix/ptx.
69      */
70 
71     len = sizeof(client);
72     t_sync(request->fd);
73     if (getpeerinaddr(request->fd, &client, len) < 0) {
74 	tcpd_warn("can't get client address: %s", tli_error());
75 	return;
76     }
77     request->client->sin = &client;
78 
79     /* Call TLI utility routine to get information on endpoint */
80     if ((tli_state_ptr = _t_checkfd(request->fd)) == NULL)
81 	return;
82 
83     if (tli_state_ptr->ti_servtype == T_CLTS) {
84 	/* UDP - may need to get address the hard way */
85 	if (client.sin_addr.s_addr == 0) {
86 	    /* The UDP endpoint is not connected so we didn't get the */
87 	    /* remote address - get it the hard way ! */
88 
89 	    /* Look at the control part of the top message on the stream */
90 	    /* we don't want to remove it from the stream so we use I_PEEK */
91 	    peek.ctlbuf.maxlen = tli_state_ptr->ti_ctlsize;
92 	    peek.ctlbuf.len = 0;
93 	    peek.ctlbuf.buf = tli_state_ptr->ti_ctlbuf;
94 	    /* Don't even look at the data */
95 	    peek.databuf.maxlen = -1;
96 	    peek.databuf.len = 0;
97 	    peek.databuf.buf = 0;
98 	    peek.flags = 0;
99 
100 	    switch (ioctl(request->fd, I_PEEK, &peek)) {
101 	    case -1:
102 		tcpd_warn("can't peek at endpoint: %s", tli_error());
103 		return;
104 	    case 0:
105 		/* No control part - we're hosed */
106 		tcpd_warn("can't get UDP info: %s", tli_error());
107 		return;
108 	    default:
109 		/* FALL THROUGH */
110 		;
111 	    }
112 	    /* Can we even check the PRIM_type ? */
113 	    if (peek.ctlbuf.len < sizeof(long)) {
114 		tcpd_warn("UDP control info garbage");
115 		return;
116 	    }
117 	    TSI_prim_ptr = (union T_primitives *) peek.ctlbuf.buf;
118 	    if (TSI_prim_ptr->type != T_UNITDATA_IND) {
119 		tcpd_warn("wrong type for UDP control info");
120 		return;
121 	    }
122 	    /* Validate returned unitdata indication packet */
123 	    if ((peek.ctlbuf.len < sizeof(struct T_unitdata_ind)) ||
124 		((TSI_prim_ptr->unitdata_ind.OPT_length != 0) &&
125 		 (peek.ctlbuf.len <
126 		  TSI_prim_ptr->unitdata_ind.OPT_length +
127 		  TSI_prim_ptr->unitdata_ind.OPT_offset))) {
128 		tcpd_warn("UDP control info garbaged");
129 		return;
130 	    }
131 	    /* Extract the address */
132 	    memcpy(&client,
133 		   peek.ctlbuf.buf + TSI_prim_ptr->unitdata_ind.SRC_offset,
134 		   TSI_prim_ptr->unitdata_ind.SRC_length);
135 	}
136 	request->sink = tli_sink;
137     }
138     if (getmyinaddr(request->fd, &server, len) < 0)
139 	tcpd_warn("can't get local address: %s", tli_error());
140     else
141 	request->server->sin = &server;
142 }
143 
144 /* tli_error - convert tli error number to text */
145 
146 static char *tli_error(void)
147 {
148     static char buf[40];
149 
150     if (t_errno != TSYSERR) {
151 	if (t_errno < 0 || t_errno >= t_nerr) {
152 	    sprintf(buf, "Unknown TLI error %d", t_errno);
153 	    return (buf);
154 	} else {
155 	    return (t_errlist[t_errno]);
156 	}
157     } else {
158 	if (errno < 0 || errno >= sys_nerr) {
159 	    sprintf(buf, "Unknown UNIX error %d", errno);
160 	    return (buf);
161 	} else {
162 	    return (sys_errlist[errno]);
163 	}
164     }
165 }
166 
167 /* tli_sink - absorb unreceived datagram */
168 
169 static void tli_sink(int fd)
170 {
171     struct t_unitdata *unit;
172     int     flags;
173 
174     /*
175      * Something went wrong. Absorb the datagram to keep inetd from looping.
176      * Allocate storage for address, control and data. If that fails, sleep
177      * for a couple of seconds in an attempt to keep inetd from looping too
178      * fast.
179      */
180 
181     if ((unit = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ALL)) == 0) {
182 	tcpd_warn("t_alloc: %s", tli_error());
183 	sleep(5);
184     } else {
185 	(void) t_rcvudata(fd, unit, &flags);
186 	t_free((void *) unit, T_UNITDATA);
187     }
188 }
189 
190 #endif /* TLI_SEQUENT */
191