xref: /freebsd/usr.sbin/bootparamd/callbootd/callbootd.c (revision 30d3283b15c965aff5d553d817c99011900f036b)
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 
10112eace2SPhilippe Charnier #ifndef lint
11112eace2SPhilippe Charnier static const char rcsid[] =
1297d92980SPeter Wemm   "$FreeBSD$";
13112eace2SPhilippe Charnier #endif /* not lint */
14b9fefab7SBill Paul 
15b9fefab7SBill Paul #include "bootparam_prot.h"
16b9fefab7SBill Paul #include <rpc/rpc.h>
17b9fefab7SBill Paul #include <sys/types.h>
18b9fefab7SBill Paul #include <sys/socket.h>
19ca979f0fSPeter Wemm #include <netinet/in.h>
20ca979f0fSPeter Wemm #include <arpa/inet.h>
21112eace2SPhilippe Charnier #include <err.h>
22b9fefab7SBill Paul #include <netdb.h>
2332d74204SJohn-Mark Gurney #include <stdlib.h>
24b9fefab7SBill Paul 
25b9fefab7SBill Paul 
26b9fefab7SBill Paul /* #define bp_address_u bp_address */
27b9fefab7SBill Paul #include <stdio.h>
28112eace2SPhilippe Charnier #include <string.h>
29b9fefab7SBill Paul 
30b9fefab7SBill Paul int broadcast;
31b9fefab7SBill Paul 
32b9fefab7SBill Paul char cln[MAX_MACHINE_NAME+1];
33b9fefab7SBill Paul char dmn[MAX_MACHINE_NAME+1];
34b9fefab7SBill Paul char path[MAX_PATH_LEN+1];
35b9fefab7SBill Paul extern char *inet_ntoa();
36784bddbcSKevin Lo static void usage(void);
37784bddbcSKevin Lo int printgetfile(bp_getfile_res *);
38784bddbcSKevin Lo int printwhoami(bp_whoami_res *);
39b9fefab7SBill Paul 
4032d74204SJohn-Mark Gurney bool_t
41b9fefab7SBill Paul eachres_whoami(resultp, raddr)
42b9fefab7SBill Paul bp_whoami_res *resultp;
43b9fefab7SBill Paul struct sockaddr_in *raddr;
44b9fefab7SBill Paul {
45b9fefab7SBill Paul   struct hostent *he;
46b9fefab7SBill Paul 
47b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
48b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
49b9fefab7SBill Paul   printwhoami(resultp);
50b9fefab7SBill Paul   printf("\n");
51b9fefab7SBill Paul   return(0);
52b9fefab7SBill Paul }
53b9fefab7SBill Paul 
5432d74204SJohn-Mark Gurney bool_t
55b9fefab7SBill Paul eachres_getfile(resultp, raddr)
56b9fefab7SBill Paul bp_getfile_res *resultp;
57b9fefab7SBill Paul struct sockaddr_in *raddr;
58b9fefab7SBill Paul {
59b9fefab7SBill Paul   struct hostent *he;
60b9fefab7SBill Paul 
61b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
62b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
63b9fefab7SBill Paul   printgetfile(resultp);
64b9fefab7SBill Paul   printf("\n");
65b9fefab7SBill Paul   return(0);
66b9fefab7SBill Paul }
67b9fefab7SBill Paul 
68b9fefab7SBill Paul 
69112eace2SPhilippe Charnier int
70b9fefab7SBill Paul main(argc, argv)
71b9fefab7SBill Paul int argc;
72b9fefab7SBill Paul char **argv;
73b9fefab7SBill Paul {
74b9fefab7SBill Paul   char *server;
75b9fefab7SBill Paul 
76b9fefab7SBill Paul   bp_whoami_arg whoami_arg;
77b9fefab7SBill Paul   bp_whoami_res *whoami_res, stat_whoami_res;
78b9fefab7SBill Paul   bp_getfile_arg getfile_arg;
79b9fefab7SBill Paul   bp_getfile_res *getfile_res, stat_getfile_res;
80b9fefab7SBill Paul 
81b9fefab7SBill Paul 
82b9fefab7SBill Paul   long the_inet_addr;
83b9fefab7SBill Paul   CLIENT *clnt;
84b9fefab7SBill Paul 
85b9fefab7SBill Paul   stat_whoami_res.client_name = cln;
86b9fefab7SBill Paul   stat_whoami_res.domain_name = dmn;
87b9fefab7SBill Paul 
88b9fefab7SBill Paul   stat_getfile_res.server_name = cln;
89b9fefab7SBill Paul   stat_getfile_res.server_path = path;
90b9fefab7SBill Paul 
91112eace2SPhilippe Charnier   if (argc < 3)
92112eace2SPhilippe Charnier     usage();
93b9fefab7SBill Paul 
94b9fefab7SBill Paul   server = argv[1];
95b9fefab7SBill Paul   if ( ! strcmp(server , "all") ) broadcast = 1;
96b9fefab7SBill Paul 
97b9fefab7SBill Paul   if ( ! broadcast ) {
98b9fefab7SBill Paul     clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
99112eace2SPhilippe Charnier     if ( clnt == NULL )
100112eace2SPhilippe Charnier       errx(1, "could not contact bootparam server on host %s", server);
10132d74204SJohn-Mark Gurney   }
102b9fefab7SBill Paul 
103b9fefab7SBill Paul   switch (argc) {
104b9fefab7SBill Paul   case 3:
105b9fefab7SBill Paul     whoami_arg.client_address.address_type = IP_ADDR_TYPE;
106b9fefab7SBill Paul     the_inet_addr = inet_addr(argv[2]);
107112eace2SPhilippe Charnier     if ( the_inet_addr == -1)
108112eace2SPhilippe Charnier       errx(2, "bogus addr %s", argv[2]);
109b9fefab7SBill Paul     bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
110b9fefab7SBill Paul 
111b9fefab7SBill Paul     if (! broadcast ) {
112b9fefab7SBill Paul       whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
113b9fefab7SBill Paul       printf("Whoami returning:\n");
114b9fefab7SBill Paul       if (printwhoami(whoami_res)) {
115112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
116b9fefab7SBill Paul       } else
117b9fefab7SBill Paul 	exit(0);
118b9fefab7SBill Paul      } else {
119*30d3283bSEitan Adler        (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
120b9fefab7SBill Paul 			       BOOTPARAMPROC_WHOAMI,
12132d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_whoami_arg,
12232d74204SJohn-Mark Gurney 			       (char *)&whoami_arg,
12332d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_whoami_res,
12432d74204SJohn-Mark Gurney 			       (char *)&stat_whoami_res,
1258360efbdSAlfred Perlstein 			       (resultproc_t)eachres_whoami);
126b9fefab7SBill Paul        exit(0);
127b9fefab7SBill Paul      }
128b9fefab7SBill Paul 
129b9fefab7SBill Paul   case 4:
130b9fefab7SBill Paul 
131b9fefab7SBill Paul     getfile_arg.client_name = argv[2];
132b9fefab7SBill Paul     getfile_arg.file_id = argv[3];
133b9fefab7SBill Paul 
134b9fefab7SBill Paul     if (! broadcast ) {
135b9fefab7SBill Paul       getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
136b9fefab7SBill Paul       printf("getfile returning:\n");
137b9fefab7SBill Paul       if (printgetfile(getfile_res)) {
138112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
139b9fefab7SBill Paul       } else
140b9fefab7SBill Paul 	exit(0);
141b9fefab7SBill Paul     } else {
142*30d3283bSEitan Adler       (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
143b9fefab7SBill Paul 			       BOOTPARAMPROC_GETFILE,
14432d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_getfile_arg,
14532d74204SJohn-Mark Gurney 			       (char *)&getfile_arg,
14632d74204SJohn-Mark Gurney 			       (xdrproc_t)xdr_bp_getfile_res,
14732d74204SJohn-Mark Gurney 			       (char *)&stat_getfile_res,
1488360efbdSAlfred Perlstein 			       (resultproc_t)eachres_getfile);
149b9fefab7SBill Paul       exit(0);
150b9fefab7SBill Paul     }
151b9fefab7SBill Paul 
152b9fefab7SBill Paul   default:
153b9fefab7SBill Paul 
154112eace2SPhilippe Charnier     usage();
155112eace2SPhilippe Charnier   }
156112eace2SPhilippe Charnier 
157112eace2SPhilippe Charnier }
158112eace2SPhilippe Charnier 
159112eace2SPhilippe Charnier 
160112eace2SPhilippe Charnier static void
161112eace2SPhilippe Charnier usage()
162112eace2SPhilippe Charnier {
163b9fefab7SBill Paul 	fprintf(stderr,
164112eace2SPhilippe Charnier 		"usage: callbootd server procnum (IP-addr | host fileid)\n");
165b9fefab7SBill Paul     exit(1);
166b9fefab7SBill Paul }
167b9fefab7SBill Paul 
168112eace2SPhilippe Charnier int
169112eace2SPhilippe Charnier printwhoami(res)
170b9fefab7SBill Paul bp_whoami_res *res;
171b9fefab7SBill Paul {
172b9fefab7SBill Paul       if ( res) {
173b9fefab7SBill Paul 	printf("client_name:\t%s\ndomain_name:\t%s\n",
174b9fefab7SBill Paul 	     res->client_name, res->domain_name);
175b9fefab7SBill Paul 	printf("router:\t%d.%d.%d.%d\n",
176b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.net,
177b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.host,
178b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.lh,
179b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.impno);
180b9fefab7SBill Paul 	return(0);
181b9fefab7SBill Paul       } else {
182112eace2SPhilippe Charnier 	warnx("null answer!!!");
183b9fefab7SBill Paul 	return(1);
184b9fefab7SBill Paul       }
185b9fefab7SBill Paul     }
186b9fefab7SBill Paul 
187b9fefab7SBill Paul 
188b9fefab7SBill Paul 
189b9fefab7SBill Paul 
190b9fefab7SBill Paul int
191b9fefab7SBill Paul printgetfile(res)
192b9fefab7SBill Paul bp_getfile_res *res;
193b9fefab7SBill Paul {
194b9fefab7SBill Paul       if (res) {
195b9fefab7SBill Paul 	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
196b9fefab7SBill Paul 	       res->server_name,
197ca979f0fSPeter Wemm 	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
198b9fefab7SBill Paul 	       res->server_path);
199b9fefab7SBill Paul 	return(0);
200b9fefab7SBill Paul       } else {
201112eace2SPhilippe Charnier 	warnx("null answer!!!");
202b9fefab7SBill Paul 	return(1);
203b9fefab7SBill Paul       }
204b9fefab7SBill Paul     }
205