1/*
2
3This code is not copyright, and is placed in the public domain. Feel free to
4use and modify. Please send modifications and/or suggestions + bug fixes to
5
6        Klas Heggemann <klas@nada.kth.se>
7
8*/
9
10#ifndef lint
11static const char rcsid[] =
12  "$FreeBSD$";
13#endif /* not lint */
14
15#include <ctype.h>
16#include <err.h>
17#include <netdb.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <syslog.h>
22#include <unistd.h>
23#include <rpc/rpc.h>
24#include <rpc/pmap_clnt.h>
25#include <sys/ioctl.h>
26#include <sys/socket.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31#include "bootparam_prot.h"
32
33int debug = 0;
34int dolog = 0;
35in_addr_t route_addr = -1;
36struct sockaddr_in my_addr;
37char *bootpfile = "/etc/bootparams";
38
39static void usage(void);
40
41int
42main(int argc, char **argv)
43{
44	SVCXPRT *transp;
45	struct hostent *he;
46	struct stat buf;
47	int c;
48
49	while ((c = getopt(argc, argv,"dsr:f:")) != -1)
50	  switch (c) {
51	  case 'd':
52	    debug = 1;
53	    break;
54	  case 'r':
55	      if (isdigit((unsigned char)*optarg)) {
56		route_addr = inet_addr(optarg);
57		break;
58	      } else {
59		he = gethostbyname(optarg);
60		if (he) {
61		   bcopy(he->h_addr, (char *)&route_addr, sizeof(route_addr));
62		   break;
63		} else {
64		   errx(1, "no such host %s", optarg);
65		}
66	      }
67	  case 'f':
68	    bootpfile = optarg;
69	    break;
70	  case 's':
71	    dolog = 1;
72#ifndef LOG_DAEMON
73	    openlog("bootparamd", 0 , 0);
74#else
75	    openlog("bootparamd", 0 , LOG_DAEMON);
76	    setlogmask(LOG_UPTO(LOG_NOTICE));
77#endif
78	    break;
79	  default:
80	    usage();
81	  }
82
83	if ( stat(bootpfile, &buf ) )
84	  err(1, "%s", bootpfile);
85
86	if (route_addr == -1) {
87	  get_myaddress(&my_addr);
88	  bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr));
89	}
90
91	if (!debug) {
92	  if (daemon(0,0))
93	    err(1, "fork");
94	}
95
96
97	(void)pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
98
99	transp = svcudp_create(RPC_ANYSOCK);
100	if (transp == NULL)
101		errx(1, "cannot create udp service");
102	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP))
103		errx(1, "unable to register (BOOTPARAMPROG, BOOTPARAMVERS, udp)");
104
105	svc_run();
106	errx(1, "svc_run returned");
107}
108
109static void
110usage(void)
111{
112	fprintf(stderr,
113		"usage: bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n");
114	exit(1);
115}
116