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