File: tli.c

package info (click to toggle)
tcp-wrappers 7.6.q-31
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,828 kB
  • sloc: ansic: 17,852; makefile: 2,308; sh: 39
file content (369 lines) | stat: -rw-r--r-- 10,471 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
 /*
  * tli_host() determines the type of transport (connected, connectionless),
  * the transport address of a client host, and the transport address of a
  * server endpoint. In addition, it provides methods to map a transport
  * address to a printable host name or address. Socket address results are
  * in static memory; tli structures are allocated from the heap.
  * 
  * The result from the hostname lookup method is STRING_PARANOID when a host
  * pretends to have someone elses name, or when a host name is available but
  * could not be verified.
  * 
  * Diagnostics are reported through syslog(3).
  * 
  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  */

#ifndef lint
static char sccsid[] = "@(#) tli.c 1.15 97/03/21 19:27:25";
#endif

#ifdef TLI

/* System libraries. */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stream.h>
#include <sys/stat.h>
#include <sys/mkdev.h>
#include <sys/tiuser.h>
#include <sys/timod.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <netconfig.h>
#include <netdir.h>
#include <string.h>

extern char *nc_sperror();
extern int errno;
extern char *sys_errlist[];
extern int sys_nerr;
extern int t_errno;
extern char *t_errlist[];
extern int t_nerr;

/* Local stuff. */

#include "tcpd.h"

/* Forward declarations. */

static void tli_endpoints();
static struct netconfig *tli_transport();
static void tli_hostname();
static void tli_hostaddr();
static void tli_cleanup();
static char *tli_error();
static void tli_sink();

/* tli_host - look up endpoint addresses and install conversion methods */

void    tli_host(request)
struct request_info *request;
{
#ifdef INET6
    static struct sockaddr_storage client;
    static struct sockaddr_storage server;
#else
    static struct sockaddr_in client;
    static struct sockaddr_in server;
#endif

    /*
     * If we discover that we are using an IP transport, pretend we never
     * were here. Otherwise, use the transport-independent method and stick
     * to generic network addresses. XXX hard-coded protocol family name.
     */

    tli_endpoints(request);
    if ((request->config = tli_transport(request->fd)) != 0
#ifdef INET6
	&& (STR_EQ(request->config->nc_protofmly, "inet") ||
	    STR_EQ(request->config->nc_protofmly, "inet6"))) {
#else
	&& STR_EQ(request->config->nc_protofmly, "inet")) {
#endif
	if (request->client->unit != 0) {
#ifdef INET6
	    client = *(struct sockaddr_storage *) request->client->unit->addr.buf;
	    request->client->sin = (struct sockaddr *) &client;
#else
	    client = *(struct sockaddr_in *) request->client->unit->addr.buf;
	    request->client->sin = &client;
#endif
	}
	if (request->server->unit != 0) {
#ifdef INET6
	    server = *(struct sockaddr_storage *) request->server->unit->addr.buf;
	    request->server->sin = (struct sockaddr *) &server;
#else
	    server = *(struct sockaddr_in *) request->server->unit->addr.buf;
	    request->server->sin = &server;
#endif
	}
	tli_cleanup(request);
	sock_methods(request);
    } else {
	request->hostname = tli_hostname;
	request->hostaddr = tli_hostaddr;
	request->cleanup = tli_cleanup;
    }
}

/* tli_cleanup - cleanup some dynamically-allocated data structures */

static void tli_cleanup(request)
struct request_info *request;
{
    if (request->config != 0)
	freenetconfigent(request->config);
    if (request->client->unit != 0)
	t_free((char *) request->client->unit, T_UNITDATA);
    if (request->server->unit != 0)
	t_free((char *) request->server->unit, T_UNITDATA);
}

/* tli_endpoints - determine TLI client and server endpoint information */

static void tli_endpoints(request)
struct request_info *request;
{
    struct t_unitdata *server;
    struct t_unitdata *client;
    int     fd = request->fd;
    int     flags;

    /*
     * Determine the client endpoint address. With unconnected services, peek
     * at the sender address of the pending protocol data unit without
     * popping it off the receive queue. This trick works because only the
     * address member of the unitdata structure has been allocated.
     * 
     * Beware of successful returns with zero-length netbufs (for example,
     * Solaris 2.3 with ticlts transport). The netdir(3) routines can't
     * handle that. Assume connection-less transport when TI_GETPEERNAME
     * produces no usable result, even when t_rcvudata() is unable to figure
     * out the peer address. Better to hang than to loop.
     */

    if ((client = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ADDR)) == 0) {
	tcpd_warn("t_alloc: %s", tli_error());
	return;
    }
    if (ioctl(fd, TI_GETPEERNAME, &client->addr) < 0 || client->addr.len == 0) {
	request->sink = tli_sink;
	if (t_rcvudata(fd, client, &flags) < 0 || client->addr.len == 0) {
	    tcpd_warn("can't get client address: %s", tli_error());
	    t_free((void *) client, T_UNITDATA);
	    return;
	}
    }
    request->client->unit = client;

    /*
     * Look up the server endpoint address. This can be used for filtering on
     * server address or name, or to look up the client user.
     */

    if ((server = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ADDR)) == 0) {
	tcpd_warn("t_alloc: %s", tli_error());
	return;
    }
    if (ioctl(fd, TI_GETMYNAME, &server->addr) < 0) {
	tcpd_warn("TI_GETMYNAME: %m");
	t_free((void *) server, T_UNITDATA);
	return;
    }
    request->server->unit = server;
}

/* tli_transport - find out TLI transport type */

static struct netconfig *tli_transport(fd)
int     fd;
{
    struct stat from_client;
    struct stat from_config;
    void   *handlep;
    struct netconfig *config;

    /*
     * Assuming that the network device is a clone device, we must compare
     * the major device number of stdin to the minor device number of the
     * devices listed in the netconfig table.
     */

    if (fstat(fd, &from_client) != 0) {
	tcpd_warn("fstat(fd %d): %m", fd);
	return (0);
    }
    if ((handlep = setnetconfig()) == 0) {
	tcpd_warn("setnetconfig: %m");
	return (0);
    }
    while (config = getnetconfig(handlep)) {
	if (stat(config->nc_device, &from_config) == 0) {
#ifdef NO_CLONE_DEVICE
	/*
	 * If the network devices are not cloned (as is the case for
	 * Solaris 8 Beta), we must compare the major device numbers.
	 */
	    if (major(from_config.st_rdev) == major(from_client.st_rdev))
#else
	    if (minor(from_config.st_rdev) == major(from_client.st_rdev))
#endif
		break;
	}
    }
    if (config == 0) {
	tcpd_warn("unable to identify transport protocol");
	return (0);
    }

    /*
     * Something else may clobber our getnetconfig() result, so we'd better
     * acquire our private copy.
     */

    if ((config = getnetconfigent(config->nc_netid)) == 0) {
	tcpd_warn("getnetconfigent(%s): %s", config->nc_netid, nc_sperror());
	return (0);
    }
    return (config);
}

/* tli_hostaddr - map TLI transport address to printable address */

static void tli_hostaddr(host)
struct host_info *host;
{
    struct request_info *request = host->request;
    struct netconfig *config = request->config;
    struct t_unitdata *unit = host->unit;
    char   *uaddr;

    if (config != 0 && unit != 0
	&& (uaddr = taddr2uaddr(config, &unit->addr)) != 0) {
	STRN_CPY(host->addr, uaddr, sizeof(host->addr));
	free(uaddr);
    }
}

/* tli_hostname - map TLI transport address to hostname */

static void tli_hostname(host)
struct host_info *host;
{
    struct request_info *request = host->request;
    struct netconfig *config = request->config;
    struct t_unitdata *unit = host->unit;
    struct nd_hostservlist *servlist;

    if (config != 0 && unit != 0
	&& netdir_getbyaddr(config, &servlist, &unit->addr) == ND_OK) {

	struct nd_hostserv *service = servlist->h_hostservs;
	struct nd_addrlist *addr_list;
	int     found = 0;

	if (netdir_getbyname(config, service, &addr_list) != ND_OK) {

	    /*
	     * Unable to verify that the name matches the address. This may
	     * be a transient problem or a botched name server setup. We
	     * decide to play safe.
	     */

	    tcpd_warn("can't verify hostname: netdir_getbyname(%.*s) failed",
		      STRING_LENGTH, service->h_host);

	} else {

	    /*
	     * Look up the host address in the address list we just got. The
	     * comparison is done on the textual representation, because the
	     * transport address is an opaque structure that may have holes
	     * with uninitialized garbage. This approach obviously loses when
	     * the address does not have a textual representation.
	     */

	    char   *uaddr = eval_hostaddr(host);
	    char   *ua;
	    int     i;

	    for (i = 0; found == 0 && i < addr_list->n_cnt; i++) {
		if ((ua = taddr2uaddr(config, &(addr_list->n_addrs[i]))) != 0) {
		    found = !strcmp(ua, uaddr);
		    free(ua);
		}
	    }
	    netdir_free((void *) addr_list, ND_ADDRLIST);

	    /*
	     * When the host name does not map to the initial address, assume
	     * someone has compromised a name server. More likely someone
	     * botched it, but that could be dangerous, too.
	     */

	    if (found == 0)
		tcpd_warn("host name/address mismatch: %s != %.*s",
			  host->addr, STRING_LENGTH, service->h_host);
	}
	STRN_CPY(host->name, found ? service->h_host : paranoid,
		 sizeof(host->name));
	netdir_free((void *) servlist, ND_HOSTSERVLIST);
    }
}

/* tli_error - convert tli error number to text */

static char *tli_error()
{
    static char buf[40];

    if (t_errno != TSYSERR) {
	if (t_errno < 0 || t_errno >= t_nerr) {
	    sprintf(buf, "Unknown TLI error %d", t_errno);
	    return (buf);
	} else {
	    return (t_errlist[t_errno]);
	}
    } else {
	if (errno < 0 || errno >= sys_nerr) {
	    sprintf(buf, "Unknown UNIX error %d", errno);
	    return (buf);
	} else {
	    return (sys_errlist[errno]);
	}
    }
}

/* tli_sink - absorb unreceived datagram */

static void tli_sink(fd)
int     fd;
{
    struct t_unitdata *unit;
    int     flags;

    /*
     * Something went wrong. Absorb the datagram to keep inetd from looping.
     * Allocate storage for address, control and data. If that fails, sleep
     * for a couple of seconds in an attempt to keep inetd from looping too
     * fast.
     */

    if ((unit = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ALL)) == 0) {
	tcpd_warn("t_alloc: %s", tli_error());
	sleep(5);
    } else {
	(void) t_rcvudata(fd, unit, &flags);
	t_free((void *) unit, T_UNITDATA);
    }
}

#endif /* TLI */