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