capquery.c 3.54 KB
Newer Older
1
/*
2
 * Copyright (c) 2005-2014 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25 26 27 28 29
 */

#include "config.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
30
#include <rpc/rpc.h>
31 32 33 34 35 36 37 38 39 40 41 42
#include <netdb.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <assert.h>
#include <stdarg.h>
#include <sys/time.h>
#include <grp.h>
#include "capdecls.h"
43 44 45
#ifndef BOSSNODE
#define BOSSNODE "boss"
#endif
46 47 48 49 50 51 52 53 54 55 56 57 58

static int debug = 0;

static char *usagestr = 
 "usage: capquery [-d] [-s server] [-p #] name\n"
 " -d		   Turn on debugging.\n"
 " -s server	   Specify a name to connect to.\n"
 " -p portnum	   Specify a port number to connect to.\n"
 "\n";

void
usage()
{
59
	fprintf(stderr, "%s", usagestr);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	exit(1);
}

static int
mygethostbyname(struct sockaddr_in *host_addr, char *host, int port)
{
    struct hostent *host_ent;
    int retval = 0;
    
    assert(host_addr != NULL);
    assert(host != NULL);
    assert(strlen(host) > 0);
    
    memset(host_addr, 0, sizeof(struct sockaddr_in));
#ifndef linux
    host_addr->sin_len = sizeof(struct sockaddr_in);
#endif
    host_addr->sin_family = AF_INET;
    host_addr->sin_port = htons(port);
    if( (host_ent = gethostbyname(host)) != NULL ) {
        memcpy((char *)&host_addr->sin_addr.s_addr,
               host_ent->h_addr,
               host_ent->h_length);
        retval = 1;
    }
    else {
        retval = inet_aton(host, &host_addr->sin_addr);
    }
    return( retval );
}

int
main(int argc, char **argv)
{
	int			sock, ch, rc, retval = EXIT_FAILURE;
95
	int			port = SERVERPORT;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	char		       *server = BOSSNODE;
	struct sockaddr_in	sin;
	whoami_t		whoami;

	while ((ch = getopt(argc, argv, "ds:p:")) != -1)
		switch(ch) {
		case 's':
			server = optarg;
			break;
		case 'p':
			port = atoi(optarg);
			break;
		case 'd':
			debug++;
			break;
		case 'h':
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		usage();
	
	if (strlen(argv[0]) >= sizeof(whoami.name))
		fprintf(stderr, "Name too long: %s\n", argv[0]);
	
	if (getuid() != 0)
		fprintf(stderr, "Must be run as root\n");

	memset(&whoami, 0, sizeof(whoami));
	strcpy(whoami.name, argv[0]);
	whoami.portnum = -1;
	
	if (!mygethostbyname(&sin, server, port))
		fprintf(stderr, "Bad server name: %s\n", server);

	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		perror("socket");
	else if (bindresvport(sock, NULL) < 0)
		perror("bindresvport");
	else if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
		perror("connect");
	else if (write(sock, &whoami, sizeof(whoami)) != sizeof(whoami))
		perror("write");
	else if ((rc = read(sock, &port, sizeof(port))) != sizeof(port))
		perror("read");
	else {
		printf("%d\n", port);
		retval = EXIT_SUCCESS;
	}

	close(sock);

	return retval;
}