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