xref: /freebsd/usr.sbin/bootparamd/callbootd/callbootd.c (revision e03764d931d820185a019334259b18df2e3f6b6c)
1b9fefab7SBill Paul /*
2b9fefab7SBill Paul 
3b9fefab7SBill Paul This code is not copyright, and is placed in the public domain. Feel free to
4b9fefab7SBill Paul use and modify. Please send modifications and/or suggestions + bug fixes to
5b9fefab7SBill Paul 
6b9fefab7SBill Paul         Klas Heggemann <klas@nada.kth.se>
7b9fefab7SBill Paul 
8b9fefab7SBill Paul */
9b9fefab7SBill Paul 
10*e03764d9SYoshihiro Takahashi #include <sys/cdefs.h>
11*e03764d9SYoshihiro Takahashi __FBSDID("$FreeBSD$");
12b9fefab7SBill Paul 
13b9fefab7SBill Paul #include "bootparam_prot.h"
14b9fefab7SBill Paul #include <rpc/rpc.h>
15b9fefab7SBill Paul #include <sys/types.h>
16b9fefab7SBill Paul #include <sys/socket.h>
17ca979f0fSPeter Wemm #include <netinet/in.h>
18ca979f0fSPeter Wemm #include <arpa/inet.h>
19112eace2SPhilippe Charnier #include <err.h>
20b9fefab7SBill Paul #include <netdb.h>
2132d74204SJohn-Mark Gurney #include <stdlib.h>
22b9fefab7SBill Paul 
23b9fefab7SBill Paul 
24b9fefab7SBill Paul /* #define bp_address_u bp_address */
25b9fefab7SBill Paul #include <stdio.h>
26112eace2SPhilippe Charnier #include <string.h>
27b9fefab7SBill Paul 
28*e03764d9SYoshihiro Takahashi static int broadcast;
29*e03764d9SYoshihiro Takahashi static char cln[MAX_MACHINE_NAME+1];
30*e03764d9SYoshihiro Takahashi static char dmn[MAX_MACHINE_NAME+1];
31*e03764d9SYoshihiro Takahashi static char path[MAX_PATH_LEN+1];
32b9fefab7SBill Paul 
33784bddbcSKevin Lo static void usage(void);
34784bddbcSKevin Lo int printgetfile(bp_getfile_res *);
35784bddbcSKevin Lo int printwhoami(bp_whoami_res *);
36b9fefab7SBill Paul 
37c3545222SEnji Cooper static bool_t
38c3545222SEnji Cooper eachres_whoami(bp_whoami_res *resultp, struct sockaddr_in *raddr)
39b9fefab7SBill Paul {
40b9fefab7SBill Paul   struct hostent *he;
41b9fefab7SBill Paul 
42b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
43b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
44b9fefab7SBill Paul   printwhoami(resultp);
45b9fefab7SBill Paul   printf("\n");
46b9fefab7SBill Paul   return(0);
47b9fefab7SBill Paul }
48b9fefab7SBill Paul 
49c3545222SEnji Cooper static bool_t
50c3545222SEnji Cooper eachres_getfile(bp_getfile_res *resultp, struct sockaddr_in *raddr)
51b9fefab7SBill Paul {
52b9fefab7SBill Paul   struct hostent *he;
53b9fefab7SBill Paul 
54b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
55b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
56b9fefab7SBill Paul   printgetfile(resultp);
57b9fefab7SBill Paul   printf("\n");
58b9fefab7SBill Paul   return(0);
59b9fefab7SBill Paul }
60b9fefab7SBill Paul 
61b9fefab7SBill Paul 
62112eace2SPhilippe Charnier int
63c3545222SEnji Cooper main(int argc, char **argv)
64b9fefab7SBill Paul {
65b9fefab7SBill Paul   char *server;
66b9fefab7SBill Paul 
67b9fefab7SBill Paul   bp_whoami_arg whoami_arg;
68b9fefab7SBill Paul   bp_whoami_res *whoami_res, stat_whoami_res;
69b9fefab7SBill Paul   bp_getfile_arg getfile_arg;
70b9fefab7SBill Paul   bp_getfile_res *getfile_res, stat_getfile_res;
71b9fefab7SBill Paul 
72b9fefab7SBill Paul 
73*e03764d9SYoshihiro Takahashi   in_addr_t the_inet_addr;
74*e03764d9SYoshihiro Takahashi   CLIENT *clnt = NULL;		/* Silence warnings */
75b9fefab7SBill Paul 
76b9fefab7SBill Paul   stat_whoami_res.client_name = cln;
77b9fefab7SBill Paul   stat_whoami_res.domain_name = dmn;
78b9fefab7SBill Paul 
79b9fefab7SBill Paul   stat_getfile_res.server_name = cln;
80b9fefab7SBill Paul   stat_getfile_res.server_path = path;
81b9fefab7SBill Paul 
82112eace2SPhilippe Charnier   if (argc < 3)
83112eace2SPhilippe Charnier     usage();
84b9fefab7SBill Paul 
85b9fefab7SBill Paul   server = argv[1];
86b9fefab7SBill Paul   if ( ! strcmp(server , "all") ) broadcast = 1;
87b9fefab7SBill Paul 
88b9fefab7SBill Paul   if ( ! broadcast ) {
89b9fefab7SBill Paul     clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
90112eace2SPhilippe Charnier     if ( clnt == NULL )
91112eace2SPhilippe Charnier       errx(1, "could not contact bootparam server on host %s", server);
9232d74204SJohn-Mark Gurney   }
93b9fefab7SBill Paul 
94b9fefab7SBill Paul   switch (argc) {
95b9fefab7SBill Paul   case 3:
96b9fefab7SBill Paul     whoami_arg.client_address.address_type = IP_ADDR_TYPE;
97b9fefab7SBill Paul     the_inet_addr = inet_addr(argv[2]);
98482d8831SKevin Lo     if ( the_inet_addr == INADDR_NONE)
99112eace2SPhilippe Charnier       errx(2, "bogus addr %s", argv[2]);
100b9fefab7SBill Paul     bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
101b9fefab7SBill Paul 
102b9fefab7SBill Paul     if (! broadcast ) {
103b9fefab7SBill Paul       whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
104b9fefab7SBill Paul       printf("Whoami returning:\n");
105b9fefab7SBill Paul       if (printwhoami(whoami_res)) {
106112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
107b9fefab7SBill Paul       } else
108b9fefab7SBill Paul 	exit(0);
109b9fefab7SBill Paul      } else {
11030d3283bSEitan Adler        (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
111b9fefab7SBill Paul 			       BOOTPARAMPROC_WHOAMI,
11232d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_whoami_arg,
11332d74204SJohn-Mark Gurney 			       (char *)&whoami_arg,
11432d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_whoami_res,
11532d74204SJohn-Mark Gurney 			       (char *)&stat_whoami_res,
1168360efbdSAlfred Perlstein 			       (resultproc_t)eachres_whoami);
117b9fefab7SBill Paul        exit(0);
118b9fefab7SBill Paul      }
119b9fefab7SBill Paul 
120b9fefab7SBill Paul   case 4:
121b9fefab7SBill Paul 
122b9fefab7SBill Paul     getfile_arg.client_name = argv[2];
123b9fefab7SBill Paul     getfile_arg.file_id = argv[3];
124b9fefab7SBill Paul 
125b9fefab7SBill Paul     if (! broadcast ) {
126b9fefab7SBill Paul       getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
127b9fefab7SBill Paul       printf("getfile returning:\n");
128b9fefab7SBill Paul       if (printgetfile(getfile_res)) {
129112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
130b9fefab7SBill Paul       } else
131b9fefab7SBill Paul 	exit(0);
132b9fefab7SBill Paul     } else {
13330d3283bSEitan Adler       (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
134b9fefab7SBill Paul 			       BOOTPARAMPROC_GETFILE,
13532d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_getfile_arg,
13632d74204SJohn-Mark Gurney 			       (char *)&getfile_arg,
13732d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_getfile_res,
13832d74204SJohn-Mark Gurney 			       (char *)&stat_getfile_res,
1398360efbdSAlfred Perlstein 			       (resultproc_t)eachres_getfile);
140b9fefab7SBill Paul       exit(0);
141b9fefab7SBill Paul     }
142b9fefab7SBill Paul 
143b9fefab7SBill Paul   default:
144b9fefab7SBill Paul 
145112eace2SPhilippe Charnier     usage();
146112eace2SPhilippe Charnier   }
147112eace2SPhilippe Charnier 
148112eace2SPhilippe Charnier }
149112eace2SPhilippe Charnier 
150112eace2SPhilippe Charnier 
151112eace2SPhilippe Charnier static void
152*e03764d9SYoshihiro Takahashi usage(void)
153112eace2SPhilippe Charnier {
154b9fefab7SBill Paul 	fprintf(stderr,
155112eace2SPhilippe Charnier 		"usage: callbootd server procnum (IP-addr | host fileid)\n");
156b9fefab7SBill Paul     exit(1);
157b9fefab7SBill Paul }
158b9fefab7SBill Paul 
159112eace2SPhilippe Charnier int
160*e03764d9SYoshihiro Takahashi printwhoami(bp_whoami_res *res)
161b9fefab7SBill Paul {
162b9fefab7SBill Paul       if ( res) {
163b9fefab7SBill Paul 	printf("client_name:\t%s\ndomain_name:\t%s\n",
164b9fefab7SBill Paul 	     res->client_name, res->domain_name);
165b9fefab7SBill Paul 	printf("router:\t%d.%d.%d.%d\n",
166b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.net,
167b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.host,
168b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.lh,
169b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.impno);
170b9fefab7SBill Paul 	return(0);
171b9fefab7SBill Paul       } else {
172112eace2SPhilippe Charnier 	warnx("null answer!!!");
173b9fefab7SBill Paul 	return(1);
174b9fefab7SBill Paul       }
175b9fefab7SBill Paul     }
176b9fefab7SBill Paul 
177b9fefab7SBill Paul 
178b9fefab7SBill Paul 
179b9fefab7SBill Paul 
180b9fefab7SBill Paul int
181*e03764d9SYoshihiro Takahashi printgetfile(bp_getfile_res *res)
182b9fefab7SBill Paul {
183b9fefab7SBill Paul       if (res) {
184b9fefab7SBill Paul 	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
185b9fefab7SBill Paul 	       res->server_name,
186ca979f0fSPeter Wemm 	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
187b9fefab7SBill Paul 	       res->server_path);
188b9fefab7SBill Paul 	return(0);
189b9fefab7SBill Paul       } else {
190112eace2SPhilippe Charnier 	warnx("null answer!!!");
191b9fefab7SBill Paul 	return(1);
192b9fefab7SBill Paul       }
193b9fefab7SBill Paul     }
194