File: addr.c

package info (click to toggle)
host 20000331-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 540 kB
  • ctags: 998
  • sloc: ansic: 7,543; sh: 389; makefile: 133
file content (271 lines) | stat: -rw-r--r-- 6,577 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (c) 1985, 1989 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that: (1) source distributions retain this entire copyright
 * notice and comment, and (2) distributions including binaries display
 * the following acknowledgement:  ``This product includes software
 * developed by the University of California, Berkeley and its contributors''
 * in the documentation or other materials provided with the distribution
 * and in all advertising materials mentioning features or use of this
 * software. Neither the name of the University nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
static char Version[] = "@(#)addr.c	e07@nikhef.nl (Eric Wassenaar) 990605";
#endif

#include "host.h"
#include "glob.h"

/*
** CHECK_ADDR -- Check whether reverse address mappings revert to host
** -------------------------------------------------------------------
**
**	Returns:
**		TRUE if all addresses of host map back to host.
**		FALSE otherwise.
*/

bool
check_addr(name)
input char *name;			/* host name to check addresses for */
{
	struct hostent *hp;
	register int i;
	struct in_addr inaddr[MAXADDRS];
	int naddress;
	char *hname, hnamebuf[MAXDNAME+1];
	int matched = 0;

/*
 * Look up the specified host to fetch its addresses.
 */
	hp = gethostbyname(name);
	if (hp == NULL)
	{
		ns_error(name, T_A, C_IN, server);
		return(FALSE);
	}

	hname = strncpy(hnamebuf, hp->h_name, MAXDNAME);
	hname[MAXDNAME] = '\0';

	for (i = 0; i < MAXADDRS && hp->h_addr_list[i]; i++)
		inaddr[i] = incopy(hp->h_addr_list[i]);
	naddress = i;

	if (verbose)
		printf("Found %d address%s for %s\n",
			naddress, plurale(naddress), hname);

/*
 * Map back the addresses found, and check whether they revert to host.
 */
	for (i = 0; i < naddress; i++)
		if (check_addr_name(inaddr[i], hname))
			matched++;

	return((matched == naddress) ? TRUE : FALSE);
}

/*
** CHECK_ADDR_NAME -- Check whether reverse address mappings revert to host
** ------------------------------------------------------------------------
**
**	Returns:
**		TRUE if the given address of host maps back to host.
**		FALSE otherwise.
*/

bool
check_addr_name(inaddr, name)
input struct in_addr inaddr;		/* address of host to map back */
input char *name;			/* name of host to check */
{
	struct hostent *hp;
	register int i;
	char *iname, inamebuf[MAXDNAME+1];
	int matched = 0;

/*
 * Fetch the reverse mapping of the given host address.
 */
	iname = strcpy(inamebuf, inet_ntoa(inaddr));

	if (verbose)
		printf("Checking %s address %s\n", name, iname);

	hp = gethostbyaddr((char *)&inaddr, INADDRSZ, AF_INET);
	if (hp == NULL)
	{
		ns_error(iname, T_PTR, C_IN, server);
		return(FALSE);
	}

/*
 * Check whether the ``official'' host name matches.
 * This is the name in the first (or only) PTR record encountered.
 */
	if (!sameword(hp->h_name, name))
		pr_warning("%s address %s maps to %s",
			name, iname, hp->h_name);
	else
		matched++;

/*
 * If not, a match may be found among the aliases.
 * They are available (as of BIND 4.9) in case multipe PTR records are used.
 */
	if (!matched)
	{
		for (i = 0; hp->h_aliases[i]; i++)
		{
			pr_warning("%s address %s maps to alias %s",
				name, iname, hp->h_aliases[i]);

			if (sameword(hp->h_aliases[i], name))
				matched++;
		}
	}

	return(matched ? TRUE : FALSE);
}

/*
** CHECK_NAME -- Check whether address belongs to host addresses
** -------------------------------------------------------------
**
**	Returns:
**		TRUE if given address was found among host addresses.
**		FALSE otherwise.
*/

bool
check_name(addr)
input ipaddr_t addr;			/* address of host to check */
{
	struct hostent *hp;
	register int i;
	struct in_addr inaddr;
	char *iname, inamebuf[MAXDNAME+1];
	char *hname, hnamebuf[MAXDNAME+1];
	char *aname, anamebuf[MAXALIAS][MAXDNAME+1];
	int naliases;
	int matched = 0;

/*
 * Check whether the address is registered by fetching its host name.
 */
	inaddr.s_addr = addr;
	iname = strcpy(inamebuf, inet_ntoa(inaddr));

	hp = gethostbyaddr((char *)&inaddr, INADDRSZ, AF_INET);
	if (hp == NULL)
	{
		ns_error(iname, T_PTR, C_IN, server);
		return(FALSE);
	}

	hname = strncpy(hnamebuf, hp->h_name, MAXDNAME);
	hname[MAXDNAME] = '\0';

	if (verbose)
		printf("Address %s maps to %s\n", iname, hname);

/*
 * In case of multiple PTR records, additional names are stored as aliases.
 */
	for (i = 0; i < MAXALIAS && hp->h_aliases[i]; i++)
	{
		aname = strncpy(anamebuf[i], hp->h_aliases[i], MAXDNAME);
		aname[MAXDNAME] = '\0';

		if (verbose)
			printf("Address %s maps to alias %s\n", iname, aname);
	}
	naliases = i;

/*
 * Check whether the given address belongs to the host name and the aliases.
 */
	if (check_name_addr(hname, addr))
		matched++;

	for (i = 0; i < naliases; i++)
	{
		aname = anamebuf[i];
		if (check_name_addr(aname, addr))
			matched++;
	}

	return((matched == (naliases + 1)) ? TRUE : FALSE);
}

/*
** CHECK_NAME_ADDR -- Check whether address belongs to host
** --------------------------------------------------------
**
**	Returns:
**		TRUE if given address was found among host addresses.
**		FALSE otherwise.
*/

bool
check_name_addr(name, addr)
input char *name;			/* name of host to check */
input ipaddr_t addr;			/* address should belong to host */
{
	struct hostent *hp;
	register int i;
	struct in_addr inaddr;
	char *iname, inamebuf[MAXDNAME+1];
	int matched = 0;

	inaddr.s_addr = addr;
	iname = strcpy(inamebuf, inet_ntoa(inaddr));

/*
 * Lookup the host name found to fetch its addresses.
 */
	hp = gethostbyname(name);
	if (hp == NULL)
	{
		ns_error(name, T_A, C_IN, server);
		return(FALSE);
	}

/*
 * Verify whether the mapped host name is canonical.
 */
	if (!sameword(hp->h_name, name))
		pr_warning("%s host %s is not canonical (%s)",
			iname, name, hp->h_name);

/*
 * Check whether the given address is listed among the known addresses.
 */
	for (i = 0; hp->h_addr_list[i]; i++)
	{
		inaddr = incopy(hp->h_addr_list[i]);

		if (verbose)
			printf("Checking %s address %s\n",
				name, inet_ntoa(inaddr));

		if (inaddr.s_addr == addr)
			matched++;
	}

	if (!matched)
		pr_error("address %s does not belong to %s",
			iname, name);

	return(matched ? TRUE : FALSE);
}