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