xref: /freebsd/usr.sbin/bootparamd/callbootd/callbootd.c (revision edf8578117e8844e02c0121147f45e4609b30680)
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 "bootparam_prot.h"
12 #include <rpc/rpc.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <err.h>
18 #include <netdb.h>
19 #include <stdlib.h>
20 
21 
22 /* #define bp_address_u bp_address */
23 #include <stdio.h>
24 #include <string.h>
25 
26 static int broadcast;
27 static char cln[MAX_MACHINE_NAME+1];
28 static char dmn[MAX_MACHINE_NAME+1];
29 static char path[MAX_PATH_LEN+1];
30 
31 static void usage(void) __dead2;
32 int printgetfile(bp_getfile_res *);
33 int printwhoami(bp_whoami_res *);
34 
35 static bool_t
36 eachres_whoami(bp_whoami_res *resultp, struct sockaddr_in *raddr)
37 {
38   struct hostent *he;
39 
40   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
41   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
42   printwhoami(resultp);
43   printf("\n");
44   return(0);
45 }
46 
47 static bool_t
48 eachres_getfile(bp_getfile_res *resultp, struct sockaddr_in *raddr)
49 {
50   struct hostent *he;
51 
52   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
53   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
54   printgetfile(resultp);
55   printf("\n");
56   return(0);
57 }
58 
59 
60 int
61 main(int argc, char **argv)
62 {
63   char *server;
64 
65   bp_whoami_arg whoami_arg;
66   bp_whoami_res *whoami_res, stat_whoami_res;
67   bp_getfile_arg getfile_arg;
68   bp_getfile_res *getfile_res, stat_getfile_res;
69 
70 
71   in_addr_t the_inet_addr;
72   CLIENT *clnt = NULL;		/* Silence warnings */
73 
74   stat_whoami_res.client_name = cln;
75   stat_whoami_res.domain_name = dmn;
76 
77   stat_getfile_res.server_name = cln;
78   stat_getfile_res.server_path = path;
79 
80   if (argc < 3)
81     usage();
82 
83   server = argv[1];
84   if ( ! strcmp(server , "all") ) broadcast = 1;
85 
86   if ( ! broadcast ) {
87     clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
88     if ( clnt == NULL )
89       errx(1, "could not contact bootparam server on host %s", server);
90   }
91 
92   switch (argc) {
93   case 3:
94     whoami_arg.client_address.address_type = IP_ADDR_TYPE;
95     the_inet_addr = inet_addr(argv[2]);
96     if ( the_inet_addr == INADDR_NONE)
97       errx(2, "bogus addr %s", argv[2]);
98     bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
99 
100     if (! broadcast ) {
101       whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
102       printf("Whoami returning:\n");
103       if (printwhoami(whoami_res)) {
104 	errx(1, "bad answer returned from server %s", server);
105       } else
106 	exit(0);
107      } else {
108        (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
109 			       BOOTPARAMPROC_WHOAMI,
110 			       (xdrproc_t)xdr_bp_whoami_arg,
111 			       (char *)&whoami_arg,
112 			       (xdrproc_t)xdr_bp_whoami_res,
113 			       (char *)&stat_whoami_res,
114 			       (resultproc_t)eachres_whoami);
115        exit(0);
116      }
117 
118   case 4:
119 
120     getfile_arg.client_name = argv[2];
121     getfile_arg.file_id = argv[3];
122 
123     if (! broadcast ) {
124       getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
125       printf("getfile returning:\n");
126       if (printgetfile(getfile_res)) {
127 	errx(1, "bad answer returned from server %s", server);
128       } else
129 	exit(0);
130     } else {
131       (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
132 			       BOOTPARAMPROC_GETFILE,
133 			       (xdrproc_t)xdr_bp_getfile_arg,
134 			       (char *)&getfile_arg,
135 			       (xdrproc_t)xdr_bp_getfile_res,
136 			       (char *)&stat_getfile_res,
137 			       (resultproc_t)eachres_getfile);
138       exit(0);
139     }
140 
141   default:
142 
143     usage();
144   }
145 
146 }
147 
148 
149 static void
150 usage(void)
151 {
152 	fprintf(stderr,
153 		"usage: callbootd server procnum (IP-addr | host fileid)\n");
154     exit(1);
155 }
156 
157 int
158 printwhoami(bp_whoami_res *res)
159 {
160       if ( res) {
161 	printf("client_name:\t%s\ndomain_name:\t%s\n",
162 	     res->client_name, res->domain_name);
163 	printf("router:\t%d.%d.%d.%d\n",
164 	     255 &  res->router_address.bp_address_u.ip_addr.net,
165 	     255 & res->router_address.bp_address_u.ip_addr.host,
166 	     255 &  res->router_address.bp_address_u.ip_addr.lh,
167 	     255 & res->router_address.bp_address_u.ip_addr.impno);
168 	return(0);
169       } else {
170 	warnx("null answer!!!");
171 	return(1);
172       }
173     }
174 
175 
176 
177 
178 int
179 printgetfile(bp_getfile_res *res)
180 {
181       if (res) {
182 	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
183 	       res->server_name,
184 	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
185 	       res->server_path);
186 	return(0);
187       } else {
188 	warnx("null answer!!!");
189 	return(1);
190       }
191     }
192