/*
	This is the network domain finger host client. It 
	simply reads in the remote host name and the requested
	service and sends it to the server program via an internet
	socket.

    Copyright (C) 1996 <Ed Alley, Email: wea@llnl.gov>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <unistd.h>
#include <stdio.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <syslog.h>

/*******************************************************************

	These defines must be set properly for fhost to work.

*******************************************************************/

#define SERV_PORT 6666			/* fhostd port number > 5000*/
#define SERV_ADDR "123.456.789.876"	/* fhostd server address */
#define LOCALHOST "virt.brew.com"	/* Host we are running on */

main(argc, argv, envp)
int argc;
char **argv;
char **envp;
{
	char c, cc[2];
	char localhost[64],host[64], service[64];
	char mess[192];
	int i, j, s, len, nbr;
	struct sockaddr_in sin;

	/*
	  Number of arguments must be three.
	*/

	if (argc != 3) {
		printf("Usage: fhost @host service\n");
		exit(1);
	}

	/*
	  Check argument lengths (must be less than 64 characters).
	*/

	for (i=0; i<argc; i++) {
	 if (strlen(argv[i]) > 63) {
	  printf("You have exceeded the argument length. ...Exiting.\n");
	  syslog(LOG_ERR,"%.32s: Possible buffer overrun attack.\n", argv[i]);
	  exit (1);
	 }
	}

	/*
	  Load arguments: arg[1] = "@remote_host_name"
			  arg[2] = "requested_service"
	*/

	strcpy(localhost, LOCALHOST);

	i = 0;
	while ((c = argv[1][i]) != '\0')
		host[i++] = c;
	host[i]   = '\0';

	cc[0] = host[0];
	cc[1] = '\0';

	/*
	  Check to see if we have a host name.
	*/

	if (strcmp(cc, "@") != 0) {
		printf("FHOST: hostname error.\n");
		exit(1);
	}

	i = 0;
	while ((c = argv[2][i]) != '\0')
		service[i++] = c;
	service[i] = '\0';

	/*
	  Get a socket. This socket will be in the internet domain,
	  and will be a stream socket.
	*/

	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("Socket");
		exit(1);
	}

	/*
	  Create the address we will be connecting to by
	  storing the address info into the socket structure.
	*/

	bzero((char *) &sin, sizeof(sin));
	sin.sin_family      = AF_INET;
	sin.sin_addr.s_addr = inet_addr(SERV_ADDR);
	sin.sin_port        = htons(SERV_PORT);

	/*
	  Connect to the socket.
	*/

	if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
		close(s);
		perror("Connect");
		exit(1);
	}

	/*
	  Send the local host's name, the remote host's name
	  and the name of the service which was requested.
	*/

	i = 0;
	j = 0;
	while ((c = localhost[i++]) != '\0')
		mess[j++] = c;
	mess[j++] = '\0';

	i = 0;
	while ((c = host[i++]) != '\0')
		mess[j++] = c;
	mess[j++] = '\0';

	i = 0;
	while ((c = service[i++]) != '\0')
		mess[j++] = c;
	mess[j++] = '\0';

	send(s, mess, j, 0);

	/*
	  Terminate the connection.
	*/

	close(s);

	exit(0);
}
