/*
 * Copyright (c) 1983 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
 All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@(#)fingerd.c	5.3 (Berkeley) 11/3/88";
#endif /* not lint */

/*
 * Finger server.
 */
#include <sys/types.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <arpa/inet.h>	
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#if defined(DEBUG)
#include <syslog.h>
#endif

#ifdef __STDC__
const
#endif
char *nologin_fn = "/etc/nologin";

main(argc, argv)
	int argc;
	char *argv[];
{
	register char *sp;
	char line[512];
	char *hname;
	struct sockaddr_in sin;
	struct hostent *foohost;
	int i, p[2], pid, status, w_switch;
	FILE *fp;
	char *av[4];

#ifdef DEBUG
	openlog("fingerd", LOG_NOWAIT, LOG_LOCAL2);
	line[0] = '\0';
#endif

	i = sizeof (sin);
	bzero(&sin, sizeof(sin));
	if (getpeername(0, &sin, &i) < 0)
		perror("fingerd: getpeername");
	if (fgets(line, sizeof(line), stdin) == NULL) {
#ifdef DEBUG
		syslog(LOG_NOTICE, "connection closed prematurely");
#endif
		exit(1);
	}
	sp = line;
	av[0] = "finger";
	w_switch = 0;
	for (i = 1;;) {
		while (isspace(*sp))
			sp++;
		if (!*sp)
			break;
		if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
			sp += 2;
			if (!w_switch) {
				av[i++] = "-l";
				w_switch = 1;
			}          
		}
		if (*sp && !isspace(*sp)) {
			av[i++] = sp;
			while (*sp && !isspace(*sp))
				sp++;
			*sp = '\0';
			if (strchr(av[i - 1], '@')) {
				av[i - 1][0] = '\0';
			}
		}
	}
	av[i] = 0;
#ifdef DEBUG
	foohost = gethostbyaddr(&sin.sin_addr, sizeof sin.sin_addr, AF_INET);
	hname = foohost ? foohost->h_name : inet_ntoa(sin.sin_addr);
	if(line[0] == '\n' || line[0] == '\r')
	  syslog (LOG_INFO, "Finger connection from %s", hname);
	else
	  syslog (LOG_INFO, "Finger connection from %s: \"%s\"", hname, line);
#endif
	if (pipe(p) < 0)
		fatal(argv[0], "pipe");
	if ((pid = fork()) == 0) {
		close(p[0]);
		if (p[1] != 1) {
			dup2(p[1], 1);
			close(p[1]);
		}
		if(argc > 1 && (line[0] == '\n' || line[0] == '\r') && 
		   argv[1][0] == '-' && argv[1][1] == 'v')
		  {
#ifdef ibm032
			write(1, "Machine type is IBM RT/PC\n", 26);
#else
#ifdef vax
			write(1, "Machine type is VAX\n", 20);
#else
#if defined(mips) && defined(ultrix)
			write(1, "Machine type is DECstation\n", 27);
#endif	/* mips */
#endif	/* vax */
#endif	/* rt */
			if(access(nologin_fn, F_OK) == 0)
			  {
			    write(1, "Logins disabled\n", 16);
			  } else {
			    write(1, "Load information:\n", 18);
			    system("/usr/ucb/uptime");
			    write(1, "\n", 1);
			  }
		}
		execv("/usr/ucb/finger", av);
		_exit(1);
	}
	if (pid == -1)
		fatal(argv[0], "fork");
	close(p[1]);
	if ((fp = fdopen(p[0], "r")) == NULL)
		fatal(argv[0], "fdopen");
	while ((i = getc(fp)) != EOF) {
		if (i == '\n')
			putchar('\r');
		putchar(i);
	}
	fclose(fp);
	while ((i = wait(&status)) != pid && i != -1)
		;
	return(0);
}

fatal(prog, s)
	char *prog, *s;
{
#ifdef DEBUG
	syslog(LOG_ERR, "%s: %m", s);
#endif
	fprintf(stderr, "%s: ", prog);
	perror(s);
	exit(1);
}
