xref: /freebsd/usr.sbin/bootparamd/callbootd/callbootd.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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 
10e03764d9SYoshihiro Takahashi #include <sys/cdefs.h>
11b9fefab7SBill Paul #include "bootparam_prot.h"
12b9fefab7SBill Paul #include <rpc/rpc.h>
13b9fefab7SBill Paul #include <sys/types.h>
14b9fefab7SBill Paul #include <sys/socket.h>
15ca979f0fSPeter Wemm #include <netinet/in.h>
16ca979f0fSPeter Wemm #include <arpa/inet.h>
17112eace2SPhilippe Charnier #include <err.h>
18b9fefab7SBill Paul #include <netdb.h>
1932d74204SJohn-Mark Gurney #include <stdlib.h>
20b9fefab7SBill Paul 
21b9fefab7SBill Paul 
22b9fefab7SBill Paul /* #define bp_address_u bp_address */
23b9fefab7SBill Paul #include <stdio.h>
24112eace2SPhilippe Charnier #include <string.h>
25b9fefab7SBill Paul 
26e03764d9SYoshihiro Takahashi static int broadcast;
27e03764d9SYoshihiro Takahashi static char cln[MAX_MACHINE_NAME+1];
28e03764d9SYoshihiro Takahashi static char dmn[MAX_MACHINE_NAME+1];
29e03764d9SYoshihiro Takahashi static char path[MAX_PATH_LEN+1];
30b9fefab7SBill Paul 
31*72e1ea2fSAlfonso Gregory static void usage(void) __dead2;
32784bddbcSKevin Lo int printgetfile(bp_getfile_res *);
33784bddbcSKevin Lo int printwhoami(bp_whoami_res *);
34b9fefab7SBill Paul 
35c3545222SEnji Cooper static bool_t
eachres_whoami(bp_whoami_res * resultp,struct sockaddr_in * raddr)36c3545222SEnji Cooper eachres_whoami(bp_whoami_res *resultp, struct sockaddr_in *raddr)
37b9fefab7SBill Paul {
38b9fefab7SBill Paul   struct hostent *he;
39b9fefab7SBill Paul 
40b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
41b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
42b9fefab7SBill Paul   printwhoami(resultp);
43b9fefab7SBill Paul   printf("\n");
44b9fefab7SBill Paul   return(0);
45b9fefab7SBill Paul }
46b9fefab7SBill Paul 
47c3545222SEnji Cooper static bool_t
eachres_getfile(bp_getfile_res * resultp,struct sockaddr_in * raddr)48c3545222SEnji Cooper eachres_getfile(bp_getfile_res *resultp, struct sockaddr_in *raddr)
49b9fefab7SBill Paul {
50b9fefab7SBill Paul   struct hostent *he;
51b9fefab7SBill Paul 
52b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
53b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
54b9fefab7SBill Paul   printgetfile(resultp);
55b9fefab7SBill Paul   printf("\n");
56b9fefab7SBill Paul   return(0);
57b9fefab7SBill Paul }
58b9fefab7SBill Paul 
59b9fefab7SBill Paul 
60112eace2SPhilippe Charnier int
main(int argc,char ** argv)61c3545222SEnji Cooper main(int argc, char **argv)
62b9fefab7SBill Paul {
63b9fefab7SBill Paul   char *server;
64b9fefab7SBill Paul 
65b9fefab7SBill Paul   bp_whoami_arg whoami_arg;
66b9fefab7SBill Paul   bp_whoami_res *whoami_res, stat_whoami_res;
67b9fefab7SBill Paul   bp_getfile_arg getfile_arg;
68b9fefab7SBill Paul   bp_getfile_res *getfile_res, stat_getfile_res;
69b9fefab7SBill Paul 
70b9fefab7SBill Paul 
71e03764d9SYoshihiro Takahashi   in_addr_t the_inet_addr;
72e03764d9SYoshihiro Takahashi   CLIENT *clnt = NULL;		/* Silence warnings */
73b9fefab7SBill Paul 
74b9fefab7SBill Paul   stat_whoami_res.client_name = cln;
75b9fefab7SBill Paul   stat_whoami_res.domain_name = dmn;
76b9fefab7SBill Paul 
77b9fefab7SBill Paul   stat_getfile_res.server_name = cln;
78b9fefab7SBill Paul   stat_getfile_res.server_path = path;
79b9fefab7SBill Paul 
80112eace2SPhilippe Charnier   if (argc < 3)
81112eace2SPhilippe Charnier     usage();
82b9fefab7SBill Paul 
83b9fefab7SBill Paul   server = argv[1];
84b9fefab7SBill Paul   if ( ! strcmp(server , "all") ) broadcast = 1;
85b9fefab7SBill Paul 
86b9fefab7SBill Paul   if ( ! broadcast ) {
87b9fefab7SBill Paul     clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
88112eace2SPhilippe Charnier     if ( clnt == NULL )
89112eace2SPhilippe Charnier       errx(1, "could not contact bootparam server on host %s", server);
9032d74204SJohn-Mark Gurney   }
91b9fefab7SBill Paul 
92b9fefab7SBill Paul   switch (argc) {
93b9fefab7SBill Paul   case 3:
94b9fefab7SBill Paul     whoami_arg.client_address.address_type = IP_ADDR_TYPE;
95b9fefab7SBill Paul     the_inet_addr = inet_addr(argv[2]);
96482d8831SKevin Lo     if ( the_inet_addr == INADDR_NONE)
97112eace2SPhilippe Charnier       errx(2, "bogus addr %s", argv[2]);
98b9fefab7SBill Paul     bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
99b9fefab7SBill Paul 
100b9fefab7SBill Paul     if (! broadcast ) {
101b9fefab7SBill Paul       whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
102b9fefab7SBill Paul       printf("Whoami returning:\n");
103b9fefab7SBill Paul       if (printwhoami(whoami_res)) {
104112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
105b9fefab7SBill Paul       } else
106b9fefab7SBill Paul 	exit(0);
107b9fefab7SBill Paul      } else {
10830d3283bSEitan Adler        (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
109b9fefab7SBill Paul 			       BOOTPARAMPROC_WHOAMI,
11032d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_whoami_arg,
11132d74204SJohn-Mark Gurney 			       (char *)&whoami_arg,
11232d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_whoami_res,
11332d74204SJohn-Mark Gurney 			       (char *)&stat_whoami_res,
1148360efbdSAlfred Perlstein 			       (resultproc_t)eachres_whoami);
115b9fefab7SBill Paul        exit(0);
116b9fefab7SBill Paul      }
117b9fefab7SBill Paul 
118b9fefab7SBill Paul   case 4:
119b9fefab7SBill Paul 
120b9fefab7SBill Paul     getfile_arg.client_name = argv[2];
121b9fefab7SBill Paul     getfile_arg.file_id = argv[3];
122b9fefab7SBill Paul 
123b9fefab7SBill Paul     if (! broadcast ) {
124b9fefab7SBill Paul       getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
125b9fefab7SBill Paul       printf("getfile returning:\n");
126b9fefab7SBill Paul       if (printgetfile(getfile_res)) {
127112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
128b9fefab7SBill Paul       } else
129b9fefab7SBill Paul 	exit(0);
130b9fefab7SBill Paul     } else {
13130d3283bSEitan Adler       (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
132b9fefab7SBill Paul 			       BOOTPARAMPROC_GETFILE,
13332d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_getfile_arg,
13432d74204SJohn-Mark Gurney 			       (char *)&getfile_arg,
13532d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_getfile_res,
13632d74204SJohn-Mark Gurney 			       (char *)&stat_getfile_res,
1378360efbdSAlfred Perlstein 			       (resultproc_t)eachres_getfile);
138b9fefab7SBill Paul       exit(0);
139b9fefab7SBill Paul     }
140b9fefab7SBill Paul 
141b9fefab7SBill Paul   default:
142b9fefab7SBill Paul 
143112eace2SPhilippe Charnier     usage();
144112eace2SPhilippe Charnier   }
145112eace2SPhilippe Charnier 
146112eace2SPhilippe Charnier }
147112eace2SPhilippe Charnier 
148112eace2SPhilippe Charnier 
149112eace2SPhilippe Charnier static void
usage(void)150e03764d9SYoshihiro Takahashi usage(void)
151112eace2SPhilippe Charnier {
152b9fefab7SBill Paul 	fprintf(stderr,
153112eace2SPhilippe Charnier 		"usage: callbootd server procnum (IP-addr | host fileid)\n");
154b9fefab7SBill Paul     exit(1);
155b9fefab7SBill Paul }
156b9fefab7SBill Paul 
157112eace2SPhilippe Charnier int
printwhoami(bp_whoami_res * res)158e03764d9SYoshihiro Takahashi printwhoami(bp_whoami_res *res)
159b9fefab7SBill Paul {
160b9fefab7SBill Paul       if ( res) {
161b9fefab7SBill Paul 	printf("client_name:\t%s\ndomain_name:\t%s\n",
162b9fefab7SBill Paul 	     res->client_name, res->domain_name);
163b9fefab7SBill Paul 	printf("router:\t%d.%d.%d.%d\n",
164b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.net,
165b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.host,
166b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.lh,
167b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.impno);
168b9fefab7SBill Paul 	return(0);
169b9fefab7SBill Paul       } else {
170112eace2SPhilippe Charnier 	warnx("null answer!!!");
171b9fefab7SBill Paul 	return(1);
172b9fefab7SBill Paul       }
173b9fefab7SBill Paul     }
174b9fefab7SBill Paul 
175b9fefab7SBill Paul 
176b9fefab7SBill Paul 
177b9fefab7SBill Paul 
178b9fefab7SBill Paul int
printgetfile(bp_getfile_res * res)179e03764d9SYoshihiro Takahashi printgetfile(bp_getfile_res *res)
180b9fefab7SBill Paul {
181b9fefab7SBill Paul       if (res) {
182b9fefab7SBill Paul 	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
183b9fefab7SBill Paul 	       res->server_name,
184ca979f0fSPeter Wemm 	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
185b9fefab7SBill Paul 	       res->server_path);
186b9fefab7SBill Paul 	return(0);
187b9fefab7SBill Paul       } else {
188112eace2SPhilippe Charnier 	warnx("null answer!!!");
189b9fefab7SBill Paul 	return(1);
190b9fefab7SBill Paul       }
191b9fefab7SBill Paul     }
192