callbootd.c revision 50479
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 50479 1999-08-28 01:35:59Z peter $";
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,
1218857Srgrimes			       xdr_bp_whoami_arg, &whoami_arg,
1226751Swpaul			       xdr_bp_whoami_res, &stat_whoami_res, eachres_whoami);
1236751Swpaul       exit(0);
1246751Swpaul     }
1256751Swpaul
1266751Swpaul  case 4:
1276751Swpaul
1286751Swpaul    getfile_arg.client_name = argv[2];
1296751Swpaul    getfile_arg.file_id = argv[3];
1308857Srgrimes
1316751Swpaul    if (! broadcast ) {
1326751Swpaul      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
1336751Swpaul      printf("getfile returning:\n");
1346751Swpaul      if (printgetfile(getfile_res)) {
13529102Scharnier	errx(1, "bad answer returned from server %s", server);
1366751Swpaul      } else
1378857Srgrimes	exit(0);
1386751Swpaul    } else {
1398857Srgrimes      clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1406751Swpaul			       BOOTPARAMPROC_GETFILE,
1418857Srgrimes			       xdr_bp_getfile_arg, &getfile_arg,
1426751Swpaul			       xdr_bp_getfile_res, &stat_getfile_res,eachres_getfile);
1436751Swpaul      exit(0);
1446751Swpaul    }
1458857Srgrimes
1466751Swpaul  default:
1478857Srgrimes
14829102Scharnier    usage();
1496751Swpaul  }
1508857Srgrimes
1516751Swpaul}
1526751Swpaul
1536751Swpaul
15429102Scharnierstatic void
15529102Scharnierusage()
15629102Scharnier{
15729102Scharnier	fprintf(stderr,
15829102Scharnier		"usage: callbootd server procnum (IP-addr | host fileid)\n");
15929102Scharnier    exit(1);
16029102Scharnier}
1618857Srgrimes
16229102Scharnierint
16329102Scharnierprintwhoami(res)
1646751Swpaulbp_whoami_res *res;
1656751Swpaul{
1666751Swpaul      if ( res) {
1676751Swpaul	printf("client_name:\t%s\ndomain_name:\t%s\n",
1686751Swpaul	     res->client_name, res->domain_name);
1696751Swpaul	printf("router:\t%d.%d.%d.%d\n",
1706751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.net,
1716751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.host,
1726751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.lh,
1736751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.impno);
1746751Swpaul	return(0);
1756751Swpaul      } else {
17629102Scharnier	warnx("null answer!!!");
1776751Swpaul	return(1);
1786751Swpaul      }
1796751Swpaul    }
1806751Swpaul
1816751Swpaul
1826751Swpaul
1838857Srgrimes
1846751Swpaulint
1856751Swpaulprintgetfile(res)
1866751Swpaulbp_getfile_res *res;
1876751Swpaul{
1886751Swpaul      if (res) {
1896751Swpaul	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
1908857Srgrimes	       res->server_name,
19136917Speter	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
1926751Swpaul	       res->server_path);
1936751Swpaul	return(0);
1946751Swpaul      } else {
19529102Scharnier	warnx("null answer!!!");
1966751Swpaul	return(1);
1976751Swpaul      }
1986751Swpaul    }
199