callbootd.c revision 324142
1/*
2
3This code is not copyright, and is placed in the public domain. Feel free to
4use 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
11static const char rcsid[] =
12  "$FreeBSD: stable/10/usr.sbin/bootparamd/callbootd/callbootd.c 324142 2017-09-30 20:50:31Z ngie $";
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
30int broadcast;
31
32char cln[MAX_MACHINE_NAME+1];
33char dmn[MAX_MACHINE_NAME+1];
34char path[MAX_PATH_LEN+1];
35static void usage(void);
36int printgetfile(bp_getfile_res *);
37int printwhoami(bp_whoami_res *);
38
39static bool_t
40eachres_whoami(bp_whoami_res *resultp, struct sockaddr_in *raddr)
41{
42  struct hostent *he;
43
44  he = gethostbyaddr((char *)&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
51static bool_t
52eachres_getfile(bp_getfile_res *resultp, struct sockaddr_in *raddr)
53{
54  struct hostent *he;
55
56  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
57  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
58  printgetfile(resultp);
59  printf("\n");
60  return(0);
61}
62
63
64int
65main(int argc, char **argv)
66{
67  char *server;
68
69  bp_whoami_arg whoami_arg;
70  bp_whoami_res *whoami_res, stat_whoami_res;
71  bp_getfile_arg getfile_arg;
72  bp_getfile_res *getfile_res, stat_getfile_res;
73
74
75  long the_inet_addr;
76  CLIENT *clnt;
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    if ( clnt == NULL )
93      errx(1, "could not contact bootparam server on host %s", server);
94  }
95
96  switch (argc) {
97  case 3:
98    whoami_arg.client_address.address_type = IP_ADDR_TYPE;
99    the_inet_addr = inet_addr(argv[2]);
100    if ( the_inet_addr == -1)
101      errx(2, "bogus addr %s", argv[2]);
102    bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
103
104    if (! broadcast ) {
105      whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
106      printf("Whoami returning:\n");
107      if (printwhoami(whoami_res)) {
108	errx(1, "bad answer returned from server %s", server);
109      } else
110	exit(0);
111     } else {
112       (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
113			       BOOTPARAMPROC_WHOAMI,
114			       (xdrproc_t)xdr_bp_whoami_arg,
115			       (char *)&whoami_arg,
116			       (xdrproc_t)xdr_bp_whoami_res,
117			       (char *)&stat_whoami_res,
118			       (resultproc_t)eachres_whoami);
119       exit(0);
120     }
121
122  case 4:
123
124    getfile_arg.client_name = argv[2];
125    getfile_arg.file_id = argv[3];
126
127    if (! broadcast ) {
128      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
129      printf("getfile returning:\n");
130      if (printgetfile(getfile_res)) {
131	errx(1, "bad answer returned from server %s", server);
132      } else
133	exit(0);
134    } else {
135      (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
136			       BOOTPARAMPROC_GETFILE,
137			       (xdrproc_t)xdr_bp_getfile_arg,
138			       (char *)&getfile_arg,
139			       (xdrproc_t)xdr_bp_getfile_res,
140			       (char *)&stat_getfile_res,
141			       (resultproc_t)eachres_getfile);
142      exit(0);
143    }
144
145  default:
146
147    usage();
148  }
149
150}
151
152
153static void
154usage()
155{
156	fprintf(stderr,
157		"usage: callbootd server procnum (IP-addr | host fileid)\n");
158    exit(1);
159}
160
161int
162printwhoami(res)
163bp_whoami_res *res;
164{
165      if ( res) {
166	printf("client_name:\t%s\ndomain_name:\t%s\n",
167	     res->client_name, res->domain_name);
168	printf("router:\t%d.%d.%d.%d\n",
169	     255 &  res->router_address.bp_address_u.ip_addr.net,
170	     255 & res->router_address.bp_address_u.ip_addr.host,
171	     255 &  res->router_address.bp_address_u.ip_addr.lh,
172	     255 & res->router_address.bp_address_u.ip_addr.impno);
173	return(0);
174      } else {
175	warnx("null answer!!!");
176	return(1);
177      }
178    }
179
180
181
182
183int
184printgetfile(res)
185bp_getfile_res *res;
186{
187      if (res) {
188	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
189	       res->server_name,
190	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
191	       res->server_path);
192	return(0);
193      } else {
194	warnx("null answer!!!");
195	return(1);
196      }
197    }
198