summaryrefslogtreecommitdiff
path: root/usr/src/cmd/rpcsvc/rpc.bootparamd/bootparam_subr.c
blob: 9a9dd01af34c37a093d4c12a90524469d7d0e0d7 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Subroutines that implement the bootparam services.
 */

#include <rpcsvc/bootparam_prot.h>
#include <netdb.h>
#include <nlist.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <nsswitch.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/socket.h>
#define	KERNEL		/* to get RTHASHSIZ */
#include <sys/stream.h>
#include <net/route.h>
#undef	KERNEL
#include <net/if.h>			/* for structs ifnet and ifaddr */
#include <netinet/in.h>
#include <netinet/in_var.h>		/* for struct in_ifaddr */
#include <arpa/inet.h>

#include "bootparam_private.h"

#define	LINESIZE	1024

extern int getdomainname(char *, int);
extern int bootparams_getbyname(char *, char *, int);

static char *wildcard = "*";
static char domainkey[] = "domain=";
static void getf_printres(bp_getfile_res *);
static void copydomain(char *, char *, int);

/*
 * Whoami turns a client address into a client name
 * and suggested route machine.
 */
/*ARGSUSED1*/
bp_whoami_res *
bootparamproc_whoami_1(bp_whoami_arg *argp, CLIENT *cl)
{
	static bp_whoami_res res;
	struct in_addr clnt_addr;
	struct in_addr route_addr;
	struct hostent *hp;
	static char clnt_entry[LINESIZE];
	static char domain[MAX_MACHINE_NAME];
	char *cp;

	if (argp->client_address.address_type != IP_ADDR_TYPE) {
		if (debug) {
			msgout("Whoami failed: unknown address type %d",
				argp->client_address.address_type);
		}
		return (NULL);
	}
	(void) memcpy(&clnt_addr, &argp->client_address.bp_address_u.ip_addr,
	    sizeof (clnt_addr));
	hp = gethostbyaddr((char *)&clnt_addr, sizeof (clnt_addr), AF_INET);
	if (hp == NULL) {
		if (debug) {
			msgout("Whoami failed: gethostbyaddr for %s.",
				inet_ntoa(clnt_addr));
		}
		return (NULL);
	}

	/*
	 * We only answer requests from clients listed in the database.
	 */
	if ((bootparams_getbyname(hp->h_name, clnt_entry,
	    sizeof (clnt_entry)) != __NSW_SUCCESS) &&
	    (bootparams_getbyname(wildcard, clnt_entry,
	    sizeof (clnt_entry)) != __NSW_SUCCESS))
		return (NULL);

	res.client_name = hp->h_name;

	/*
	 * The algorithm for determining the client's domain name is:
	 * 	1) look for "domain=" in the client's bootparams line.
	 *	   If found, use its value.
	 *	2) look for a "domain=" entry in the wildcard bootparams
	 *	   line (if any).  If found, use its value.  Otherwise,
	 * 	3) return the domain name of the server answering the
	 *	   request.
	 */
	if (cp = strstr(clnt_entry, domainkey)) {
		copydomain(cp + sizeof (domainkey) - 1, domain,
		    sizeof (domain));
	} else {
		/* "domain=" not found - try for wildcard */
		if ((bootparams_getbyname(wildcard, clnt_entry,
			sizeof (clnt_entry)) == __NSW_SUCCESS) &&
			(cp = strstr(clnt_entry, domainkey))) {
			copydomain(cp + sizeof (domainkey) - 1, domain,
			    sizeof (domain));
		} else {
			(void) getdomainname(domain, sizeof (domain));
		}
	}
	res.domain_name = domain;

	res.router_address.address_type = IP_ADDR_TYPE;
	route_addr.s_addr = get_ip_route(clnt_addr);
	(void) memcpy(&res.router_address.bp_address_u.ip_addr,
	    &route_addr,
	    sizeof (res.router_address.bp_address_u.ip_addr));

	if (debug) {
		struct in_addr	in;

		(void) memcpy(&in.s_addr,
		    &res.router_address.bp_address_u.ip_addr,
		    sizeof (in.s_addr));
		msgout("Whoami returning name = %s, router address = %s",
		    res.client_name,
		    inet_ntoa(in));
	}
	return (&res);
}

/*
 * Getfile gets the client name and the key and returns its server
 * and the pathname for that key.
 */
/*ARGSUSED1*/
bp_getfile_res *
bootparamproc_getfile_1(bp_getfile_arg *argp, CLIENT *cl)
{
	static bp_getfile_res res;
	static char clnt_entry[LINESIZE];
	struct hostent *hp;
	char *cp;
	char filekey[LINESIZE];
	char *server_hostname;
	char *path_on_server;
	int do_wildcard = 0;
	static char *zero_len_string = "";

	/*
	 * The bootparams_getbyname() library function looks up a
	 * "client entry" using using the client's hostname as the
	 * key.  A client entry consists of a string of "file entries"
	 * separated by white space.  Each file entry is of the form:
	 *
	 *	file_key=server_hostname:path_on_server
	 *
	 * In the getfile RPC call, the client gives us his hostname
	 * and a file_key.  We lookup his client entry, then locate a
	 * file entry matching that file_key.  We then parse out the
	 * server_hostname and path_on_server from the file entry, map
	 * the server_hostname to an IP address, and return both the
	 * IP address and path_on_server back to the client.
	 */

	/* make the client's file key int a string we can use for matching */
	(void) strncpy(filekey, argp->file_id, sizeof (filekey) - 2);
	filekey[sizeof (filekey) - 2] = '\0';
	(void) strcat(filekey, "=");

	if (bootparams_getbyname(argp->client_name, clnt_entry,
	    sizeof (clnt_entry)) == __NSW_SUCCESS) {
		/* locate the file_key in the client's entry */
		cp = strstr(clnt_entry, filekey);
		if (cp == NULL)
			do_wildcard++;

	} else
		do_wildcard++;

	if (do_wildcard) {
		if (bootparams_getbyname(wildcard, clnt_entry,
		    sizeof (clnt_entry)) != __NSW_SUCCESS)
			return (NULL);

		/* locate the file_key in the client's entry */
		cp = strstr(clnt_entry, filekey);
		if (cp == NULL)
			return (NULL);
	}

	/* locate the "data" part of file entry (r.h.s. of "=") */
	cp = strchr(cp, '=');
	if (cp == NULL)
		return (NULL);
	cp++;
	if (*cp == '\0')
		return (NULL);
	server_hostname = cp;

	/* null-terminate server_hostname and parse path_on_server */
	cp = strchr(server_hostname, ':');
	if (cp == NULL)
		return (NULL);
	*cp = '\0';
	cp++;
	/* strtok() will null-terminate path_on_server */
	path_on_server = strtok(cp, "\t\n ");
	if (path_on_server == NULL)
		path_on_server = zero_len_string;

	res.server_name = server_hostname;
	res.server_path = path_on_server;
	if (*res.server_name == 0) {
		res.server_address.address_type = IP_ADDR_TYPE;
		(void) memset(&res.server_address.bp_address_u.ip_addr, 0,
		    sizeof (res.server_address.bp_address_u.ip_addr));
	} else {
		in_addr_t addr;

		if ((hp = gethostbyname(server_hostname)) != NULL) {
			addr = find_best_server_int(hp->h_addr_list,
			    argp->client_name);
		} else {
			addr = inet_addr(server_hostname);
			if (addr == INADDR_BROADCAST) {
				if (debug) {
					msgout("getfile_1: gethostbyname(%s) "
					    "failed", res.server_name);
				}
				return (NULL);
			}
		}
		res.server_address.address_type = IP_ADDR_TYPE;
		(void) memcpy(&res.server_address.bp_address_u.ip_addr,
		    &addr, sizeof (res.server_address.bp_address_u.ip_addr));
	}
	if (debug) {
		getf_printres(&res);
	}
	return (&res);
}

void
getf_printres(bp_getfile_res *res)
{
	struct in_addr	in;

	(void) memcpy(&in.s_addr, &res->server_address.bp_address_u.ip_addr,
	    sizeof (in.s_addr));
	msgout("getfile_1: file is \"%s\" %s \"%s\"",
	    res->server_name,
	    inet_ntoa(in),
	    res->server_path);
}

/*
 * Used when we've found a "domain=" key, this function copies characters
 * from source to target until we come upon either a NULL or whitespace is
 * found in the source string, or we run out of room in the target.
 *
 */
void
copydomain(char *source, char *target, int len)
{
	int n;			/* number of characters copies */;

	len--;			/* leave room for terminating '\0' */
	if (source)
		for (n = 0; *source != '\0' && n < len; n++)
			if (isspace((int)*source))
				break;
			else
				*target++ = *source++;

	*target = '\0';
}