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