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