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