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