callbootd.c revision 74462
16751Swpaul/*
26751Swpaul
38857SrgrimesThis code is not copyright, and is placed in the public domain. Feel free to
46751Swpauluse and modify. Please send modifications and/or suggestions + bug fixes to
56751Swpaul
66751Swpaul        Klas Heggemann <klas@nada.kth.se>
76751Swpaul
86751Swpaul*/
96751Swpaul
1029102Scharnier#ifndef lint
1129102Scharnierstatic const char rcsid[] =
1250479Speter  "$FreeBSD: head/usr.sbin/bootparamd/callbootd/callbootd.c 74462 2001-03-19 12:50:13Z alfred $";
1329102Scharnier#endif /* not lint */
146751Swpaul
156751Swpaul#include "bootparam_prot.h"
166751Swpaul#include <rpc/rpc.h>
176751Swpaul#include <sys/types.h>
186751Swpaul#include <sys/socket.h>
1936917Speter#include <netinet/in.h>
2036917Speter#include <arpa/inet.h>
2129102Scharnier#include <err.h>
226751Swpaul#include <netdb.h>
236751Swpaul
246751Swpaul
256751Swpaul/* #define bp_address_u bp_address */
266751Swpaul#include <stdio.h>
2729102Scharnier#include <string.h>
286751Swpaul
296751Swpaulint broadcast;
306751Swpaul
316751Swpaulchar cln[MAX_MACHINE_NAME+1];
326751Swpaulchar dmn[MAX_MACHINE_NAME+1];
336751Swpaulchar path[MAX_PATH_LEN+1];
346751Swpaulextern char *inet_ntoa();
3529102Scharnierstatic void usage __P((void));
3629102Scharnierint printgetfile __P((bp_getfile_res *));
3729102Scharnierint printwhoami __P((bp_whoami_res *));
386751Swpaul
3929102Scharnierint
406751Swpauleachres_whoami(resultp, raddr)
416751Swpaulbp_whoami_res *resultp;
426751Swpaulstruct sockaddr_in *raddr;
436751Swpaul{
446751Swpaul  struct hostent *he;
456751Swpaul
466751Swpaul  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
476751Swpaul  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
486751Swpaul  printwhoami(resultp);
496751Swpaul  printf("\n");
506751Swpaul  return(0);
516751Swpaul}
526751Swpaul
536751Swpauleachres_getfile(resultp, raddr)
546751Swpaulbp_getfile_res *resultp;
556751Swpaulstruct sockaddr_in *raddr;
566751Swpaul{
576751Swpaul  struct hostent *he;
586751Swpaul
596751Swpaul  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
606751Swpaul  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
616751Swpaul  printgetfile(resultp);
626751Swpaul  printf("\n");
636751Swpaul  return(0);
646751Swpaul}
656751Swpaul
666751Swpaul
6729102Scharnierint
686751Swpaulmain(argc, argv)
696751Swpaulint argc;
706751Swpaulchar **argv;
716751Swpaul{
726751Swpaul  char *server;
738857Srgrimes
746751Swpaul  bp_whoami_arg whoami_arg;
756751Swpaul  bp_whoami_res *whoami_res, stat_whoami_res;
766751Swpaul  bp_getfile_arg getfile_arg;
776751Swpaul  bp_getfile_res *getfile_res, stat_getfile_res;
786751Swpaul
798857Srgrimes
806751Swpaul  long the_inet_addr;
816751Swpaul  CLIENT *clnt;
826751Swpaul  enum clnt_stat clnt_stat;
836751Swpaul
846751Swpaul  stat_whoami_res.client_name = cln;
856751Swpaul  stat_whoami_res.domain_name = dmn;
866751Swpaul
876751Swpaul  stat_getfile_res.server_name = cln;
886751Swpaul  stat_getfile_res.server_path = path;
898857Srgrimes
9029102Scharnier  if (argc < 3)
9129102Scharnier    usage();
926751Swpaul
936751Swpaul  server = argv[1];
946751Swpaul  if ( ! strcmp(server , "all") ) broadcast = 1;
958857Srgrimes
966751Swpaul  if ( ! broadcast ) {
976751Swpaul    clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
988857Srgrimes  }
996751Swpaul
10029102Scharnier  if ( clnt == NULL )
10129102Scharnier     errx(1, "could not contact bootparam server on host %s", server);
1026751Swpaul
1036751Swpaul  switch (argc) {
1046751Swpaul  case 3:
1056751Swpaul    whoami_arg.client_address.address_type = IP_ADDR_TYPE;
1066751Swpaul    the_inet_addr = inet_addr(argv[2]);
10729102Scharnier    if ( the_inet_addr == -1)
10829102Scharnier      errx(2, "bogus addr %s", argv[2]);
1096751Swpaul    bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
1106751Swpaul
1116751Swpaul    if (! broadcast ) {
1126751Swpaul      whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
1136751Swpaul      printf("Whoami returning:\n");
1146751Swpaul      if (printwhoami(whoami_res)) {
11529102Scharnier	errx(1, "bad answer returned from server %s", server);
1166751Swpaul      } else
1176751Swpaul	exit(0);
1186751Swpaul     } else {
1198857Srgrimes       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1206751Swpaul			       BOOTPARAMPROC_WHOAMI,
12174462Salfred			       (xdrproc_t)xdr_bp_whoami_arg, (char *)&whoami_arg,
12274462Salfred			       xdr_bp_whoami_res, (char *)&stat_whoami_res,
12374462Salfred			       (resultproc_t)eachres_whoami);
1246751Swpaul       exit(0);
1256751Swpaul     }
1266751Swpaul
1276751Swpaul  case 4:
1286751Swpaul
1296751Swpaul    getfile_arg.client_name = argv[2];
1306751Swpaul    getfile_arg.file_id = argv[3];
1318857Srgrimes
1326751Swpaul    if (! broadcast ) {
1336751Swpaul      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
1346751Swpaul      printf("getfile returning:\n");
1356751Swpaul      if (printgetfile(getfile_res)) {
13629102Scharnier	errx(1, "bad answer returned from server %s", server);
1376751Swpaul      } else
1388857Srgrimes	exit(0);
1396751Swpaul    } else {
1408857Srgrimes      clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1416751Swpaul			       BOOTPARAMPROC_GETFILE,
14274462Salfred			       xdr_bp_getfile_arg, (char *)&getfile_arg,
14374462Salfred			       xdr_bp_getfile_res, (char *)&stat_getfile_res,
14474462Salfred			       (resultproc_t)eachres_getfile);
1456751Swpaul      exit(0);
1466751Swpaul    }
1478857Srgrimes
1486751Swpaul  default:
1498857Srgrimes
15029102Scharnier    usage();
1516751Swpaul  }
1528857Srgrimes
1536751Swpaul}
1546751Swpaul
1556751Swpaul
15629102Scharnierstatic void
15729102Scharnierusage()
15829102Scharnier{
15929102Scharnier	fprintf(stderr,
16029102Scharnier		"usage: callbootd server procnum (IP-addr | host fileid)\n");
16129102Scharnier    exit(1);
16229102Scharnier}
1638857Srgrimes
16429102Scharnierint
16529102Scharnierprintwhoami(res)
1666751Swpaulbp_whoami_res *res;
1676751Swpaul{
1686751Swpaul      if ( res) {
1696751Swpaul	printf("client_name:\t%s\ndomain_name:\t%s\n",
1706751Swpaul	     res->client_name, res->domain_name);
1716751Swpaul	printf("router:\t%d.%d.%d.%d\n",
1726751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.net,
1736751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.host,
1746751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.lh,
1756751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.impno);
1766751Swpaul	return(0);
1776751Swpaul      } else {
17829102Scharnier	warnx("null answer!!!");
1796751Swpaul	return(1);
1806751Swpaul      }
1816751Swpaul    }
1826751Swpaul
1836751Swpaul
1846751Swpaul
1858857Srgrimes
1866751Swpaulint
1876751Swpaulprintgetfile(res)
1886751Swpaulbp_getfile_res *res;
1896751Swpaul{
1906751Swpaul      if (res) {
1916751Swpaul	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
1928857Srgrimes	       res->server_name,
19336917Speter	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
1946751Swpaul	       res->server_path);
1956751Swpaul	return(0);
1966751Swpaul      } else {
19729102Scharnier	warnx("null answer!!!");
1986751Swpaul	return(1);
1996751Swpaul      }
2006751Swpaul    }
201