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