xref: /freebsd/usr.sbin/bootparamd/callbootd/callbootd.c (revision 112eace26d9e5d2600a474a4d4f387ff0ca2e0b3)
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[] =
12112eace2SPhilippe Charnier 	"$Id$";
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>
19112eace2SPhilippe Charnier #include <err.h>
20b9fefab7SBill Paul #include <netdb.h>
21b9fefab7SBill Paul 
22b9fefab7SBill Paul 
23b9fefab7SBill Paul /* #define bp_address_u bp_address */
24b9fefab7SBill Paul #include <stdio.h>
25112eace2SPhilippe Charnier #include <string.h>
26b9fefab7SBill Paul 
27b9fefab7SBill Paul int broadcast;
28b9fefab7SBill Paul 
29b9fefab7SBill Paul char cln[MAX_MACHINE_NAME+1];
30b9fefab7SBill Paul char dmn[MAX_MACHINE_NAME+1];
31b9fefab7SBill Paul char path[MAX_PATH_LEN+1];
32b9fefab7SBill Paul extern char *inet_ntoa();
33112eace2SPhilippe Charnier static void usage __P((void));
34112eace2SPhilippe Charnier int printgetfile __P((bp_getfile_res *));
35112eace2SPhilippe Charnier int printwhoami __P((bp_whoami_res *));
36b9fefab7SBill Paul 
37112eace2SPhilippe Charnier int
38b9fefab7SBill Paul eachres_whoami(resultp, raddr)
39b9fefab7SBill Paul bp_whoami_res *resultp;
40b9fefab7SBill Paul struct sockaddr_in *raddr;
41b9fefab7SBill Paul {
42b9fefab7SBill Paul   struct hostent *he;
43b9fefab7SBill Paul 
44b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
45b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
46b9fefab7SBill Paul   printwhoami(resultp);
47b9fefab7SBill Paul   printf("\n");
48b9fefab7SBill Paul   return(0);
49b9fefab7SBill Paul }
50b9fefab7SBill Paul 
51b9fefab7SBill Paul eachres_getfile(resultp, raddr)
52b9fefab7SBill Paul bp_getfile_res *resultp;
53b9fefab7SBill Paul struct sockaddr_in *raddr;
54b9fefab7SBill Paul {
55b9fefab7SBill Paul   struct hostent *he;
56b9fefab7SBill Paul 
57b9fefab7SBill Paul   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
58b9fefab7SBill Paul   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
59b9fefab7SBill Paul   printgetfile(resultp);
60b9fefab7SBill Paul   printf("\n");
61b9fefab7SBill Paul   return(0);
62b9fefab7SBill Paul }
63b9fefab7SBill Paul 
64b9fefab7SBill Paul 
65112eace2SPhilippe Charnier int
66b9fefab7SBill Paul main(argc, argv)
67b9fefab7SBill Paul int argc;
68b9fefab7SBill Paul char **argv;
69b9fefab7SBill Paul {
70b9fefab7SBill Paul   char *server;
71b9fefab7SBill Paul 
72b9fefab7SBill Paul   bp_whoami_arg whoami_arg;
73b9fefab7SBill Paul   bp_whoami_res *whoami_res, stat_whoami_res;
74b9fefab7SBill Paul   bp_getfile_arg getfile_arg;
75b9fefab7SBill Paul   bp_getfile_res *getfile_res, stat_getfile_res;
76b9fefab7SBill Paul 
77b9fefab7SBill Paul 
78b9fefab7SBill Paul   long the_inet_addr;
79b9fefab7SBill Paul   CLIENT *clnt;
80b9fefab7SBill Paul   enum clnt_stat clnt_stat;
81b9fefab7SBill Paul 
82b9fefab7SBill Paul   stat_whoami_res.client_name = cln;
83b9fefab7SBill Paul   stat_whoami_res.domain_name = dmn;
84b9fefab7SBill Paul 
85b9fefab7SBill Paul   stat_getfile_res.server_name = cln;
86b9fefab7SBill Paul   stat_getfile_res.server_path = path;
87b9fefab7SBill Paul 
88112eace2SPhilippe Charnier   if (argc < 3)
89112eace2SPhilippe Charnier     usage();
90b9fefab7SBill Paul 
91b9fefab7SBill Paul   server = argv[1];
92b9fefab7SBill Paul   if ( ! strcmp(server , "all") ) broadcast = 1;
93b9fefab7SBill Paul 
94b9fefab7SBill Paul   if ( ! broadcast ) {
95b9fefab7SBill Paul     clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
96b9fefab7SBill Paul   }
97b9fefab7SBill Paul 
98112eace2SPhilippe Charnier   if ( clnt == NULL )
99112eace2SPhilippe Charnier      errx(1, "could not contact bootparam server on host %s", server);
100b9fefab7SBill Paul 
101b9fefab7SBill Paul   switch (argc) {
102b9fefab7SBill Paul   case 3:
103b9fefab7SBill Paul     whoami_arg.client_address.address_type = IP_ADDR_TYPE;
104b9fefab7SBill Paul     the_inet_addr = inet_addr(argv[2]);
105112eace2SPhilippe Charnier     if ( the_inet_addr == -1)
106112eace2SPhilippe Charnier       errx(2, "bogus addr %s", argv[2]);
107b9fefab7SBill Paul     bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
108b9fefab7SBill Paul 
109b9fefab7SBill Paul     if (! broadcast ) {
110b9fefab7SBill Paul       whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
111b9fefab7SBill Paul       printf("Whoami returning:\n");
112b9fefab7SBill Paul       if (printwhoami(whoami_res)) {
113112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
114b9fefab7SBill Paul       } else
115b9fefab7SBill Paul 	exit(0);
116b9fefab7SBill Paul      } else {
117b9fefab7SBill Paul        clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
118b9fefab7SBill Paul 			       BOOTPARAMPROC_WHOAMI,
119b9fefab7SBill Paul 			       xdr_bp_whoami_arg, &whoami_arg,
120b9fefab7SBill Paul 			       xdr_bp_whoami_res, &stat_whoami_res, eachres_whoami);
121b9fefab7SBill Paul        exit(0);
122b9fefab7SBill Paul      }
123b9fefab7SBill Paul 
124b9fefab7SBill Paul   case 4:
125b9fefab7SBill Paul 
126b9fefab7SBill Paul     getfile_arg.client_name = argv[2];
127b9fefab7SBill Paul     getfile_arg.file_id = argv[3];
128b9fefab7SBill Paul 
129b9fefab7SBill Paul     if (! broadcast ) {
130b9fefab7SBill Paul       getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
131b9fefab7SBill Paul       printf("getfile returning:\n");
132b9fefab7SBill Paul       if (printgetfile(getfile_res)) {
133112eace2SPhilippe Charnier 	errx(1, "bad answer returned from server %s", server);
134b9fefab7SBill Paul       } else
135b9fefab7SBill Paul 	exit(0);
136b9fefab7SBill Paul     } else {
137b9fefab7SBill Paul       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
138b9fefab7SBill Paul 			       BOOTPARAMPROC_GETFILE,
139b9fefab7SBill Paul 			       xdr_bp_getfile_arg, &getfile_arg,
140b9fefab7SBill Paul 			       xdr_bp_getfile_res, &stat_getfile_res,eachres_getfile);
141b9fefab7SBill Paul       exit(0);
142b9fefab7SBill Paul     }
143b9fefab7SBill Paul 
144b9fefab7SBill Paul   default:
145b9fefab7SBill Paul 
146112eace2SPhilippe Charnier     usage();
147112eace2SPhilippe Charnier   }
148112eace2SPhilippe Charnier 
149112eace2SPhilippe Charnier }
150112eace2SPhilippe Charnier 
151112eace2SPhilippe Charnier 
152112eace2SPhilippe Charnier static void
153112eace2SPhilippe Charnier usage()
154112eace2SPhilippe Charnier {
155b9fefab7SBill Paul 	fprintf(stderr,
156112eace2SPhilippe Charnier 		"usage: callbootd server procnum (IP-addr | host fileid)\n");
157b9fefab7SBill Paul     exit(1);
158b9fefab7SBill Paul }
159b9fefab7SBill Paul 
160112eace2SPhilippe Charnier int
161112eace2SPhilippe Charnier printwhoami(res)
162b9fefab7SBill Paul bp_whoami_res *res;
163b9fefab7SBill Paul {
164b9fefab7SBill Paul       if ( res) {
165b9fefab7SBill Paul 	printf("client_name:\t%s\ndomain_name:\t%s\n",
166b9fefab7SBill Paul 	     res->client_name, res->domain_name);
167b9fefab7SBill Paul 	printf("router:\t%d.%d.%d.%d\n",
168b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.net,
169b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.host,
170b9fefab7SBill Paul 	     255 &  res->router_address.bp_address_u.ip_addr.lh,
171b9fefab7SBill Paul 	     255 & res->router_address.bp_address_u.ip_addr.impno);
172b9fefab7SBill Paul 	return(0);
173b9fefab7SBill Paul       } else {
174112eace2SPhilippe Charnier 	warnx("null answer!!!");
175b9fefab7SBill Paul 	return(1);
176b9fefab7SBill Paul       }
177b9fefab7SBill Paul     }
178b9fefab7SBill Paul 
179b9fefab7SBill Paul 
180b9fefab7SBill Paul 
181b9fefab7SBill Paul 
182b9fefab7SBill Paul int
183b9fefab7SBill Paul printgetfile(res)
184b9fefab7SBill Paul bp_getfile_res *res;
185b9fefab7SBill Paul {
186b9fefab7SBill Paul       if (res) {
187b9fefab7SBill Paul 	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
188b9fefab7SBill Paul 	       res->server_name,
189b9fefab7SBill Paul 	       inet_ntoa(res->server_address.bp_address_u.ip_addr),
190b9fefab7SBill Paul 	       res->server_path);
191b9fefab7SBill Paul 	return(0);
192b9fefab7SBill Paul       } else {
193112eace2SPhilippe Charnier 	warnx("null answer!!!");
194b9fefab7SBill Paul 	return(1);
195b9fefab7SBill Paul       }
196b9fefab7SBill Paul     }
197