1 /*	$OpenBSD: bootparamd.c,v 1.22 2021/11/15 15:14:24 millert Exp $	*/
2 
3 /*
4  * This code is not copyright, and is placed in the public domain.
5  * Feel free to use and modify. Please send modifications and/or
6  * suggestions + bug fixes to Klas Heggemann <klas@nada.kth.se>
7  *
8  * Various small changes by Theo de Raadt <deraadt@fsa.ca>
9  */
10 
11 #include <sys/types.h>
12 #include <sys/ioctl.h>
13 #include <sys/stat.h>
14 #include <sys/socket.h>
15 
16 #include <rpc/rpc.h>
17 #include <rpcsvc/bootparam_prot.h>
18 #include <rpcsvc/ypclnt.h>
19 #include <rpcsvc/yp_prot.h>
20 #include <arpa/inet.h>
21 
22 #include <stdio.h>
23 #include <netdb.h>
24 #include <ctype.h>
25 #include <syslog.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <err.h>
29 #include <stdlib.h>
30 
31 #include "pathnames.h"
32 
33 #define MAXLEN 800
34 
35 struct hostent *he;
36 static char hostname[MAX_MACHINE_NAME];
37 static char askname[MAX_MACHINE_NAME];
38 static char domain_name[MAX_MACHINE_NAME];
39 
40 extern void bootparamprog_1(struct svc_req *, SVCXPRT *);
41 int lookup_bootparam(char *client, char *client_canonical, char *id,
42     char **server, char **path);
43 
44 int	_rpcsvcdirty = 0;
45 int	_rpcpmstart = 0;
46 int	debug = 0;
47 int	dolog = 0;
48 struct in_addr route_addr;
49 struct sockaddr_in my_addr;
50 extern char *__progname;
51 char   *bootpfile = _PATH_BOOTPARAMS;
52 
53 
54 static void
55 usage(void)
56 {
57 	extern char *__progname;
58 	fprintf(stderr, "usage: %s [-ds] [-f file] [-r router]\n",
59 	    __progname);
60 	exit(1);
61 }
62 
63 /*
64  * ever familiar
65  */
66 int
67 main(int argc, char *argv[])
68 {
69 	struct hostent *he;
70 	struct stat buf;
71 	SVCXPRT *transp;
72 	int    c;
73 
74 	while ((c = getopt(argc, argv, "dsr:f:")) != -1)
75 		switch (c) {
76 		case 'd':
77 			debug = 1;
78 			break;
79 		case 'r':
80 			if (inet_aton(optarg, &route_addr) == 1)
81 				break;
82 			he = gethostbyname(optarg);
83 			if (!he) {
84 				warnx("no such host: %s", optarg);
85 				usage();
86 			}
87 			bcopy(he->h_addr, &route_addr.s_addr,
88 			    sizeof(route_addr.s_addr));
89 			break;
90 		case 'f':
91 			bootpfile = optarg;
92 			break;
93 		case 's':
94 			dolog = 1;
95 #ifndef LOG_DAEMON
96 			openlog(__progname, 0, 0);
97 #else
98 			openlog(__progname, 0, LOG_DAEMON);
99 			setlogmask(LOG_UPTO(LOG_NOTICE));
100 #endif
101 			break;
102 		default:
103 			usage();
104 		}
105 
106 	if (stat(bootpfile, &buf))
107 		err(1, "%s", bootpfile);
108 
109 	if (!route_addr.s_addr) {
110 		get_myaddress(&my_addr);
111 		bcopy(&my_addr.sin_addr.s_addr, &route_addr.s_addr,
112 		    sizeof(route_addr.s_addr));
113 	}
114 	if (!debug) {
115 		if (daemon(0, 0))
116 			err(1, "can't detach from terminal");
117 	}
118 
119 	(void) pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
120 
121 	transp = svcudp_create(RPC_ANYSOCK);
122 	if (transp == NULL)
123 		errx(1, "can't create udp service");
124 
125 	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1,
126 	    IPPROTO_UDP))
127 		errx(1, "unable to register BOOTPARAMPROG version %ld, udp",
128 		    BOOTPARAMVERS);
129 
130 	if (pledge("stdio rpath dns", NULL) == -1)
131 		err(1, "pledge");
132 
133 	svc_run();
134 	errx(1, "svc_run returned");
135 }
136 
137 bp_whoami_res *
138 bootparamproc_whoami_1_svc(bp_whoami_arg *whoami, struct svc_req *rqstp)
139 {
140 	in_addr_t haddr;
141 	static bp_whoami_res res;
142 
143 	if (debug)
144 		warnx("whoami got question for %d.%d.%d.%d",
145 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
146 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
147 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
148 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
149 	if (dolog)
150 		syslog(LOG_NOTICE, "whoami got question for %d.%d.%d.%d",
151 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
152 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
153 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
154 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
155 
156 	bcopy(&whoami->client_address.bp_address_u.ip_addr,
157 	    &haddr, sizeof(haddr));
158 	he = gethostbyaddr(&haddr, sizeof(haddr), AF_INET);
159 	if (!he)
160 		goto failed;
161 
162 	if (debug)
163 		warnx("This is host %s", he->h_name);
164 	if (dolog)
165 		syslog(LOG_NOTICE, "This is host %s", he->h_name);
166 
167 	strlcpy(askname, he->h_name, sizeof askname);
168 	if (!lookup_bootparam(askname, hostname, NULL, NULL, NULL)) {
169 		res.client_name = hostname;
170 		getdomainname(domain_name, MAX_MACHINE_NAME);
171 		res.domain_name = domain_name;
172 
173 		if (res.router_address.address_type != IP_ADDR_TYPE) {
174 			res.router_address.address_type = IP_ADDR_TYPE;
175 			bcopy(&route_addr.s_addr,
176 			    &res.router_address.bp_address_u.ip_addr, 4);
177 		}
178 		if (debug)
179 			warnx("Returning %s   %s    %d.%d.%d.%d",
180 			    res.client_name, res.domain_name,
181 			    255 & res.router_address.bp_address_u.ip_addr.net,
182 			    255 & res.router_address.bp_address_u.ip_addr.host,
183 			    255 & res.router_address.bp_address_u.ip_addr.lh,
184 			    255 & res.router_address.bp_address_u.ip_addr.impno);
185 		if (dolog)
186 			syslog(LOG_NOTICE, "Returning %s   %s    %d.%d.%d.%d",
187 			    res.client_name, res.domain_name,
188 			    255 & res.router_address.bp_address_u.ip_addr.net,
189 			    255 & res.router_address.bp_address_u.ip_addr.host,
190 			    255 & res.router_address.bp_address_u.ip_addr.lh,
191 			    255 & res.router_address.bp_address_u.ip_addr.impno);
192 		return (&res);
193 	}
194 failed:
195 	if (debug)
196 		warnx("whoami failed");
197 	if (dolog)
198 		syslog(LOG_NOTICE, "whoami failed");
199 	return (NULL);
200 }
201 
202 
203 bp_getfile_res *
204 bootparamproc_getfile_1_svc(bp_getfile_arg *getfile, struct svc_req *rqstp)
205 {
206 	static bp_getfile_res res;
207 	int error;
208 
209 	if (debug)
210 		warnx("getfile got question for \"%s\" and file \"%s\"",
211 		    getfile->client_name, getfile->file_id);
212 
213 	if (dolog)
214 		syslog(LOG_NOTICE,
215 		    "getfile got question for \"%s\" and file \"%s\"",
216 		    getfile->client_name, getfile->file_id);
217 
218 	he = NULL;
219 	he = gethostbyname(getfile->client_name);
220 	if (!he)
221 		goto failed;
222 
223 	strlcpy(askname, he->h_name, sizeof askname);
224 	error = lookup_bootparam(askname, NULL, getfile->file_id,
225 	    &res.server_name, &res.server_path);
226 	if (error == 0) {
227 		he = gethostbyname(res.server_name);
228 		if (!he)
229 			goto failed;
230 		bcopy(he->h_addr, &res.server_address.bp_address_u.ip_addr, 4);
231 		res.server_address.address_type = IP_ADDR_TYPE;
232 	} else if (error == ENOENT && !strcmp(getfile->file_id, "dump")) {
233 		/* Special for dump, answer with null strings. */
234 		res.server_name[0] = '\0';
235 		res.server_path[0] = '\0';
236 		bzero(&res.server_address.bp_address_u.ip_addr, 4);
237 	} else {
238 failed:
239 		if (debug)
240 			warnx("getfile failed for %s", getfile->client_name);
241 		if (dolog)
242 			syslog(LOG_NOTICE,
243 			    "getfile failed for %s", getfile->client_name);
244 		return (NULL);
245 	}
246 
247 	if (debug)
248 		warnx("returning server:%s path:%s address: %d.%d.%d.%d",
249 		    res.server_name, res.server_path,
250 		    255 & res.server_address.bp_address_u.ip_addr.net,
251 		    255 & res.server_address.bp_address_u.ip_addr.host,
252 		    255 & res.server_address.bp_address_u.ip_addr.lh,
253 		    255 & res.server_address.bp_address_u.ip_addr.impno);
254 	if (dolog)
255 		syslog(LOG_NOTICE,
256 		    "returning server:%s path:%s address: %d.%d.%d.%d",
257 		    res.server_name, res.server_path,
258 		    255 & res.server_address.bp_address_u.ip_addr.net,
259 		    255 & res.server_address.bp_address_u.ip_addr.host,
260 		    255 & res.server_address.bp_address_u.ip_addr.lh,
261 		    255 & res.server_address.bp_address_u.ip_addr.impno);
262 	return (&res);
263 }
264 
265 int
266 lookup_bootparam(char *client, char *client_canonical, char *id,
267     char **server, char **path)
268 {
269 	FILE   *f;
270 	static char buf[BUFSIZ];
271 	char   *bp, *word = NULL;
272 	size_t  idlen = id == NULL ? 0 : strlen(id);
273 	int	contin = 0, found = 0;
274 
275 	f = fopen(bootpfile, "r");
276 	if (f == NULL)
277 		return EINVAL;	/* ? */
278 
279 	while (fgets(buf, sizeof buf, f)) {
280 		int	wascontin = contin;
281 
282 		contin = buf[strlen(buf) - 2] == '\\';
283 		bp = buf + strspn(buf, " \t\n");
284 
285 		switch (wascontin) {
286 		case -1:
287 			/* Continuation of uninteresting line */
288 			contin *= -1;
289 			continue;
290 		case 0:
291 			/* New line */
292 			contin *= -1;
293 			if (*bp == '#')
294 				continue;
295 			if ((word = strsep(&bp, " \t\n")) == NULL)
296 				continue;
297 			/* See if this line's client is the one we are
298 			 * looking for */
299 			if (strcasecmp(word, client) != 0) {
300 				/*
301 				 * If it didn't match, try getting the
302 				 * canonical host name of the client
303 				 * on this line and comparing that to
304 				 * the client we are looking for
305 				 */
306 				struct hostent *hp = gethostbyname(word);
307 				if (hp == NULL || strcasecmp(hp->h_name, client))
308 					continue;
309 			}
310 			contin *= -1;
311 			break;
312 		case 1:
313 			/* Continued line we want to parse below */
314 			break;
315 		}
316 
317 		if (client_canonical)
318 			strlcpy(client_canonical, word, MAX_MACHINE_NAME);
319 
320 		/* We have found a line for CLIENT */
321 		if (id == NULL) {
322 			(void) fclose(f);
323 			return 0;
324 		}
325 
326 		/* Look for a value for the parameter named by ID */
327 		while ((word = strsep(&bp, " \t\n")) != NULL) {
328 			if (!strncmp(word, id, idlen) && word[idlen] == '=') {
329 				/* We have found the entry we want */
330 				*server = &word[idlen + 1];
331 				*path = strchr(*server, ':');
332 				if (*path == NULL)
333 					/* Malformed entry */
334 					continue;
335 				*(*path)++ = '\0';
336 				(void) fclose(f);
337 				return 0;
338 			}
339 		}
340 
341 		found = 1;
342 	}
343 
344 	(void) fclose(f);
345 	return found ? ENOENT : EPERM;
346 }
347